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>
36 #include <atalk/volinfo.h>
39 #define ADv2_DIRNAME ".AppleDouble"
41 #define DIR_DOT_OR_DOTDOT(a) \
42 ((strcmp(a, ".") == 0) || (strcmp(a, "..") == 0))
51 /* Used for pretty printing */
55 static char *netatalk_dirs[] = {
62 static char *labels[] = {
74 Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
75 Returns pointer to name or NULL.
77 static const char *check_netatalk_dirs(const char *name)
81 for (c=0; netatalk_dirs[c]; c++) {
82 if ((strcmp(name, netatalk_dirs[c])) == 0)
83 return netatalk_dirs[c];
89 static void usage_ls(void)
92 "Usage: ad ls [-dRl[u]] [file|dir, ...]\n\n"
93 " -l Long Output [-u: unix info]:\n"
94 " <unixinfo ...> <FinderFlags> <AFPAttributes> <Color> <Type> <Creator> <CNID from AppleDouble> <name>\n\n"
95 " FinderFlags (valid for (f)ile and/or (d)irectory):\n"
96 " d = On Desktop (f/d)\n"
97 " e = Hidden extension (f/d)\n"
98 " m = Shared (can run multiple times) (f)\n"
99 " n = No INIT resources (f)\n"
100 " i = Inited (f/d)\n"
101 " c = Custom icon (f/d)\n"
102 " t = Stationery (f)\n"
103 " s = Name locked (f/d)\n"
104 " b = Bundle (f/d)\n"
105 " v = Invisible (f/d)\n"
106 " a = Alias file (f/d)\n\n"
108 " y = System (f/d)\n"
109 " w = No write (f)\n"
110 " p = Needs backup (f/d)\n"
111 " r = No rename (f/d)\n"
112 " l = No delete (f/d)\n"
113 " o = No copy (f)\n\n"
114 " Note: any letter appearing in uppercase means the flag is set\n"
115 " but it's a directory for which the flag is not allowed.\n"
119 static void print_numlinks(const struct stat *statp)
121 printf("%5ld", (long)statp->st_nlink);
124 static void print_owner(const struct stat *statp)
126 struct passwd *pwd = getpwuid(statp->st_uid);
129 printf(" %-8ld", (long)statp->st_uid);
131 printf(" %-8s", pwd->pw_name);
134 static void print_group(const struct stat *statp)
136 struct group *grp = getgrgid(statp->st_gid);
139 printf(" %-8ld", (long)statp->st_gid);
141 printf(" %-8s", grp->gr_name);
144 static void print_size(const struct stat *statp)
146 switch (statp->st_mode & S_IFMT) {
149 printf("%4u,%4u", (unsigned)(statp->st_rdev >> 8),
150 (unsigned)(statp->st_rdev & 0xFF));
153 printf("%9lu", (unsigned long)statp->st_size);
157 static void print_date(const struct stat *statp)
163 if (time(&now) == -1) {
164 printf(" ????????????");
167 diff = difftime(now, statp->st_mtime);
168 if (diff < 0 || diff > 60 * 60 * 24 * 182.5)
172 strftime(buf, sizeof(buf), fmt, localtime(&statp->st_mtime));
176 static void print_flags(char *path, afpvol_t *vol, const struct stat *st)
181 uint16_t FinderFlags;
182 uint16_t AFPattributes;
183 char type[5] = "----";
184 char creator[5] = "----";
188 if (S_ISDIR(st->st_mode))
189 adflags = ADFLAGS_DIR;
191 if (vol->volinfo.v_path == NULL)
194 ad_init(&ad, vol->volinfo.v_adouble, vol->volinfo.v_ad_options);
196 if ( ad_metadata(path, adflags, &ad) < 0 )
199 FinderInfo = ad_entry(&ad, ADEID_FINDERI);
201 memcpy(&FinderFlags, FinderInfo + 8, 2);
202 FinderFlags = ntohs(FinderFlags);
204 memcpy(type, FinderInfo, 4);
205 memcpy(creator, FinderInfo + 4, 4);
207 ad_getattr(&ad, &AFPattributes);
208 AFPattributes = ntohs(AFPattributes);
211 Finder flags. Lowercase means valid, uppercase means invalid because
212 object is a dir and flag is only valid for files.
215 if (FinderFlags & FINDERINFO_ISONDESK)
220 if (FinderFlags & FINDERINFO_HIDEEXT)
225 if (FinderFlags & FINDERINFO_ISHARED) {
226 if (adflags & ADFLAGS_DIR)
233 if (FinderFlags & FINDERINFO_HASNOINITS) {
234 if (adflags & ADFLAGS_DIR)
241 if (FinderFlags & FINDERINFO_HASBEENINITED)
246 if (FinderFlags & FINDERINFO_HASCUSTOMICON)
251 if (FinderFlags & FINDERINFO_ISSTATIONNERY) {
252 if (adflags & ADFLAGS_DIR)
259 if (FinderFlags & FINDERINFO_NAMELOCKED)
264 if (FinderFlags & FINDERINFO_HASBUNDLE)
269 if (FinderFlags & FINDERINFO_INVISIBLE)
274 if (FinderFlags & FINDERINFO_ISALIAS)
282 if (AFPattributes & ATTRBIT_SYSTEM)
287 if (AFPattributes & ATTRBIT_NOWRITE) {
288 if (adflags & ADFLAGS_DIR)
295 if (AFPattributes & ATTRBIT_BACKUP)
300 if (AFPattributes & ATTRBIT_NORENAME)
305 if (AFPattributes & ATTRBIT_NODELETE)
310 if (AFPattributes & ATTRBIT_NOCOPY) {
311 if (adflags & ADFLAGS_DIR)
319 printf(" %s ", labels[(FinderFlags & FINDERINFO_COLOR) >> 1]);
323 if (isalnum(type[i]))
330 if (isalnum(creator[i]))
338 cnid = ad_forcegetid(&ad);
340 printf(" %10u ", ntohl(cnid));
342 printf(" !ADVOL_CACHE ");
344 ad_close_metadata(&ad);
347 #define TYPE(b) ((st->st_mode & (S_IFMT)) == (b))
348 #define MODE(b) ((st->st_mode & (b)) == (b))
350 static void print_mode(const struct stat *st)
354 else if (TYPE(S_IFCHR))
356 else if (TYPE(S_IFDIR))
358 else if (TYPE(S_IFIFO))
360 else if (TYPE(S_IFREG))
362 else if (TYPE(S_IFLNK))
364 else if (TYPE(S_IFSOCK))
368 putchar(MODE(S_IRUSR) ? 'r' : '-');
369 putchar(MODE(S_IWUSR) ? 'w' : '-');
376 else if (MODE(S_IXUSR))
380 putchar(MODE(S_IRGRP) ? 'r' : '-');
381 putchar(MODE(S_IWGRP) ? 'w' : '-');
388 else if (MODE(S_IXGRP))
392 putchar(MODE(S_IROTH) ? 'r' : '-');
393 putchar(MODE(S_IWOTH) ? 'w' : '-');
394 if (MODE(S_IFDIR) && MODE(S_ISVTX)) {
400 else if (MODE(S_IXOTH))
408 static int ad_print(char *path, const struct stat *st, afpvol_t *vol)
426 print_flags(path, vol, st);
427 printf(" %s\n", path);
433 static int ad_ls_r(char *path, afpvol_t *vol)
435 int ret = 0, cwd, dirprinted = 0, dirempty;
438 static char cwdpath[MAXPATHLEN+1];
441 static struct stat st; /* Save some stack space */
446 strcat(cwdpath, "/");
448 strcat(cwdpath, path);
451 if (lstat(path, &st) < 0) {
452 perror("Can't stat");
455 /* If its a file or a dir with -d option call ad_print and return */
456 if (S_ISREG(st.st_mode) || ls_d)
457 return ad_print(path, &st, vol);
459 /* Its a dir: chdir to it remembering where we started */
460 if ((cwd = open(".", O_RDONLY)) == -1) {
461 perror("Cant open .");
464 if (chdir(path) != 0) {
465 perror("Cant chdir");
470 if ((dp = opendir (".")) == NULL) {
471 perror("Couldn't opendir .");
475 /* First run: print everything */
477 while ((ep = readdir (dp))) {
478 /* Check if its "." or ".." */
479 if (DIR_DOT_OR_DOTDOT(ep->d_name))
482 /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
483 if ((name = check_netatalk_dirs(ep->d_name)) != NULL)
486 if ((ep->d_name[0] == '.') && ! ls_a)
491 if (recursion && ! dirprinted) {
492 printf("\n%s:\n", cwdpath);
496 if (lstat(ep->d_name, &st) < 0) {
497 perror("Can't stat");
501 ret = ad_print(ep->d_name, &st, vol);
506 if (! ls_l && ! dirempty)
509 /* Second run: recurse to dirs */
512 while ((ep = readdir (dp))) {
513 /* Check if its "." or ".." */
514 if (DIR_DOT_OR_DOTDOT(ep->d_name))
517 /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
518 if ((name = check_netatalk_dirs(ep->d_name)) != NULL)
521 if ((ret = lstat(ep->d_name, &st)) < 0) {
522 perror("Can't stat");
527 if (S_ISDIR(st.st_mode)) {
529 ret = ad_ls_r(ep->d_name, vol);
541 tmp = strrchr(cwdpath, '/');
548 int ad_ls(int argc, char **argv)
554 while ((c = getopt(argc, argv, ":adlRu")) != -1) {
580 if ((argc - optind) == 0) {
590 /* First run: only print files from argv paths */
591 while(optind < argc) {
592 if (stat(argv[optind], &st) != 0)
594 if (S_ISDIR(st.st_mode))
601 openvol(argv[optind], &vol);
602 ad_ls_r(argv[optind], &vol);
607 if (havefile && (! ls_l))
610 /* Second run: print dirs */
612 while(optind < argc) {
613 if (stat(argv[optind], &st) != 0)
615 if ( ! S_ISDIR(st.st_mode))
617 if ((optind > firstarg) || havefile)
618 printf("\n%s:\n", argv[optind]);
623 openvol(argv[optind], &vol);
624 ad_ls_r(argv[optind], &vol);