2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
20 #include <sys/types.h>
34 #include <atalk/adouble.h>
35 #include <atalk/cnid.h>
38 #define ADv2_DIRNAME ".AppleDouble"
40 #define DIR_DOT_OR_DOTDOT(a) \
41 ((strcmp(a, ".") == 0) || (strcmp(a, "..") == 0))
43 static volatile sig_atomic_t alarmed;
52 /* Used for pretty printing */
56 static char *netatalk_dirs[] = {
63 static char *labels[] = {
76 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
79 static void sig_handler(int signo)
85 static void set_signal(void)
89 sv.sa_handler = sig_handler;
90 sv.sa_flags = SA_RESTART;
91 sigemptyset(&sv.sa_mask);
92 if (sigaction(SIGTERM, &sv, NULL) < 0)
93 ERROR("error in sigaction(SIGTERM): %s", strerror(errno));
95 if (sigaction(SIGINT, &sv, NULL) < 0)
96 ERROR("error in sigaction(SIGINT): %s", strerror(errno));
98 memset(&sv, 0, sizeof(struct sigaction));
99 sv.sa_handler = SIG_IGN;
100 sigemptyset(&sv.sa_mask);
102 if (sigaction(SIGABRT, &sv, NULL) < 0)
103 ERROR("error in sigaction(SIGABRT): %s", strerror(errno));
105 if (sigaction(SIGHUP, &sv, NULL) < 0)
106 ERROR("error in sigaction(SIGHUP): %s", strerror(errno));
108 if (sigaction(SIGQUIT, &sv, NULL) < 0)
109 ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
113 Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
114 Returns pointer to name or NULL.
116 static const char *check_netatalk_dirs(const char *name)
120 for (c=0; netatalk_dirs[c]; c++) {
121 if ((strcmp(name, netatalk_dirs[c])) == 0)
122 return netatalk_dirs[c];
128 static void usage_ls(void)
131 "Usage: ad ls [-dRl[u]] [file|dir, ...]\n\n"
132 " -l Long Output [-u: unix info]:\n"
133 " <unixinfo ...> <FinderFlags> <AFPAttributes> <Color> <Type> <Creator> <CNID from AppleDouble> <name>\n\n"
134 " FinderFlags (valid for (f)ile and/or (d)irectory):\n"
135 " d = On Desktop (f/d)\n"
136 " e = Hidden extension (f/d)\n"
137 " m = Shared (can run multiple times) (f)\n"
138 " n = No INIT resources (f)\n"
139 " i = Inited (f/d)\n"
140 " c = Custom icon (f/d)\n"
141 " t = Stationery (f)\n"
142 " s = Name locked (f/d)\n"
143 " b = Bundle (f/d)\n"
144 " v = Invisible (f/d)\n"
145 " a = Alias file (f/d)\n\n"
147 " y = System (f/d)\n"
148 " w = No write (f)\n"
149 " p = Needs backup (f/d)\n"
150 " r = No rename (f/d)\n"
151 " l = No delete (f/d)\n"
152 " o = No copy (f)\n\n"
153 " Note: any letter appearing in uppercase means the flag is set\n"
154 " but it's a directory for which the flag is not allowed.\n"
158 static void print_numlinks(const struct stat *statp)
160 printf("%5ld", (long)statp->st_nlink);
163 static void print_owner(const struct stat *statp)
165 struct passwd *pwd = getpwuid(statp->st_uid);
168 printf(" %-8ld", (long)statp->st_uid);
170 printf(" %-8s", pwd->pw_name);
173 static void print_group(const struct stat *statp)
175 struct group *grp = getgrgid(statp->st_gid);
178 printf(" %-8ld", (long)statp->st_gid);
180 printf(" %-8s", grp->gr_name);
183 static void print_size(const struct stat *statp)
185 switch (statp->st_mode & S_IFMT) {
188 printf("%4u,%4u", (unsigned)(statp->st_rdev >> 8),
189 (unsigned)(statp->st_rdev & 0xFF));
192 printf("%9lu", (unsigned long)statp->st_size);
196 static void print_date(const struct stat *statp)
202 if (time(&now) == -1) {
203 printf(" ????????????");
206 diff = difftime(now, statp->st_mtime);
207 if (diff < 0 || diff > 60 * 60 * 24 * 182.5)
211 strftime(buf, sizeof(buf), fmt, localtime(&statp->st_mtime));
215 static void print_flags(char *path, afpvol_t *vol, const struct stat *st)
220 uint16_t FinderFlags;
221 uint16_t AFPattributes;
222 char type[5] = "----";
223 char creator[5] = "----";
227 if (S_ISDIR(st->st_mode))
228 adflags = ADFLAGS_DIR;
230 if (vol->vol->v_path == NULL)
233 ad_init(&ad, vol->vol);
235 if ( ad_metadata(path, adflags, &ad) < 0 )
238 FinderInfo = ad_entry(&ad, ADEID_FINDERI);
240 memcpy(&FinderFlags, FinderInfo + 8, 2);
241 FinderFlags = ntohs(FinderFlags);
243 memcpy(type, FinderInfo, 4);
244 memcpy(creator, FinderInfo + 4, 4);
246 ad_getattr(&ad, &AFPattributes);
247 AFPattributes = ntohs(AFPattributes);
250 Finder flags. Lowercase means valid, uppercase means invalid because
251 object is a dir and flag is only valid for files.
254 if (FinderFlags & FINDERINFO_ISONDESK)
259 if (FinderFlags & FINDERINFO_HIDEEXT)
264 if (FinderFlags & FINDERINFO_ISHARED) {
265 if (adflags & ADFLAGS_DIR)
272 if (FinderFlags & FINDERINFO_HASNOINITS) {
273 if (adflags & ADFLAGS_DIR)
280 if (FinderFlags & FINDERINFO_HASBEENINITED)
285 if (FinderFlags & FINDERINFO_HASCUSTOMICON)
290 if (FinderFlags & FINDERINFO_ISSTATIONNERY) {
291 if (adflags & ADFLAGS_DIR)
298 if (FinderFlags & FINDERINFO_NAMELOCKED)
303 if (FinderFlags & FINDERINFO_HASBUNDLE)
308 if (FinderFlags & FINDERINFO_INVISIBLE)
313 if (FinderFlags & FINDERINFO_ISALIAS)
321 if (AFPattributes & ATTRBIT_SYSTEM)
326 if (AFPattributes & ATTRBIT_NOWRITE) {
327 if (adflags & ADFLAGS_DIR)
334 if (AFPattributes & ATTRBIT_BACKUP)
339 if (AFPattributes & ATTRBIT_NORENAME)
344 if (AFPattributes & ATTRBIT_NODELETE)
349 if (AFPattributes & ATTRBIT_NOCOPY) {
350 if (adflags & ADFLAGS_DIR)
358 printf(" %s ", labels[(FinderFlags & FINDERINFO_COLOR) >> 1]);
362 if (isalnum(type[i]))
369 if (isalnum(creator[i]))
377 cnid = ad_forcegetid(&ad);
379 printf(" %10u ", ntohl(cnid));
381 printf(" !ADVOL_CACHE ");
383 ad_close(&ad, ADFLAGS_HF);
386 #define TYPE(b) ((st->st_mode & (S_IFMT)) == (b))
387 #define MODE(b) ((st->st_mode & (b)) == (b))
389 static void print_mode(const struct stat *st)
393 else if (TYPE(S_IFCHR))
395 else if (TYPE(S_IFDIR))
397 else if (TYPE(S_IFIFO))
399 else if (TYPE(S_IFREG))
401 else if (TYPE(S_IFLNK))
403 else if (TYPE(S_IFSOCK))
407 putchar(MODE(S_IRUSR) ? 'r' : '-');
408 putchar(MODE(S_IWUSR) ? 'w' : '-');
415 else if (MODE(S_IXUSR))
419 putchar(MODE(S_IRGRP) ? 'r' : '-');
420 putchar(MODE(S_IWGRP) ? 'w' : '-');
427 else if (MODE(S_IXGRP))
431 putchar(MODE(S_IROTH) ? 'r' : '-');
432 putchar(MODE(S_IWOTH) ? 'w' : '-');
433 if (MODE(S_IFDIR) && MODE(S_ISVTX)) {
439 else if (MODE(S_IXOTH))
447 static int ad_print(char *path, const struct stat *st, afpvol_t *vol)
465 print_flags(path, vol, st);
466 printf(" %s\n", path);
472 static int ad_ls_r(char *path, afpvol_t *vol)
474 int ret = 0, cwd, dirprinted = 0, dirempty;
477 static char cwdpath[MAXPATHLEN+1];
480 static struct stat st; /* Save some stack space */
485 strcat(cwdpath, "/");
487 strcat(cwdpath, path);
490 if (lstat(path, &st) < 0) {
491 perror("Can't stat");
494 /* If its a file or a dir with -d option call ad_print and return */
495 if (S_ISREG(st.st_mode) || ls_d)
496 return ad_print(path, &st, vol);
498 /* Its a dir: chdir to it remembering where we started */
499 if ((cwd = open(".", O_RDONLY)) == -1) {
500 perror("Cant open .");
503 if (chdir(path) != 0) {
504 perror("Cant chdir");
509 if ((dp = opendir (".")) == NULL) {
510 perror("Couldn't opendir .");
514 /* First run: print everything */
516 while ((ep = readdir (dp))) {
522 /* Check if its "." or ".." */
523 if (DIR_DOT_OR_DOTDOT(ep->d_name))
526 /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
527 if ((name = check_netatalk_dirs(ep->d_name)) != NULL)
530 if ((ep->d_name[0] == '.') && ! ls_a)
535 if (recursion && ! dirprinted) {
536 printf("\n%s:\n", cwdpath);
540 if (lstat(ep->d_name, &st) < 0) {
541 perror("Can't stat");
545 ret = ad_print(ep->d_name, &st, vol);
550 if (! ls_l && ! dirempty)
553 /* Second run: recurse to dirs */
556 while ((ep = readdir (dp))) {
562 /* Check if its "." or ".." */
563 if (DIR_DOT_OR_DOTDOT(ep->d_name))
566 /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
567 if ((name = check_netatalk_dirs(ep->d_name)) != NULL)
570 if ((ret = lstat(ep->d_name, &st)) < 0) {
571 perror("Can't stat");
576 if (S_ISDIR(st.st_mode)) {
578 ret = ad_ls_r(ep->d_name, vol);
590 tmp = strrchr(cwdpath, '/');
597 int ad_ls(int argc, char **argv)
603 while ((c = getopt(argc, argv, ":adlRu")) != -1) {
632 if ((argc - optind) == 0) {
642 /* First run: only print files from argv paths */
643 while(optind < argc) {
644 if (stat(argv[optind], &st) != 0)
646 if (S_ISDIR(st.st_mode))
653 openvol(argv[optind], &vol);
654 ad_ls_r(argv[optind], &vol);
659 if (havefile && (! ls_l))
662 /* Second run: print dirs */
664 while(optind < argc) {
665 if (stat(argv[optind], &st) != 0)
667 if ( ! S_ISDIR(st.st_mode))
669 if ((optind > firstarg) || havefile)
670 printf("\n%s:\n", argv[optind]);
675 openvol(argv[optind], &vol);
676 ad_ls_r(argv[optind], &vol);