File: ad_ls.c

package info (click to toggle)
netatalk 3.1.12~ds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,756 kB
  • sloc: ansic: 104,976; sh: 8,247; xml: 7,394; perl: 1,936; makefile: 1,430; python: 1,342; yacc: 309; lex: 49
file content (687 lines) | stat: -rw-r--r-- 15,819 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/* 
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

#include <atalk/adouble.h>
#include <atalk/cnid.h>
#include "ad.h"

#define ADv2_DIRNAME ".AppleDouble"

#define DIR_DOT_OR_DOTDOT(a) \
        ((strcmp(a, ".") == 0) || (strcmp(a, "..") == 0))

static volatile sig_atomic_t alarmed;

/* ls options */
static int ls_a;
static int ls_l;
static int ls_R;
static int ls_d;
static int ls_u;

/* Used for pretty printing */
static int first = 1;
static int recursion;

static char           *netatalk_dirs[] = {
    ADv2_DIRNAME,
    ".AppleDB",
    ".AppleDesktop",
    NULL
};

static char *labels[] = {
    "---",
    "gry",
    "gre",
    "vio",
    "blu",
    "yel",
    "red",
    "ora"
};

/*
  SIGNAL handling:
  catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
*/

static void sig_handler(int signo)
{
    alarmed = 1;
    return;
}

static void set_signal(void)
{
    struct sigaction sv;

    sv.sa_handler = sig_handler;
    sv.sa_flags = SA_RESTART;
    sigemptyset(&sv.sa_mask);
    if (sigaction(SIGTERM, &sv, NULL) < 0)
        ERROR("error in sigaction(SIGTERM): %s", strerror(errno));

    if (sigaction(SIGINT, &sv, NULL) < 0)
        ERROR("error in sigaction(SIGINT): %s", strerror(errno));

    memset(&sv, 0, sizeof(struct sigaction));
    sv.sa_handler = SIG_IGN;
    sigemptyset(&sv.sa_mask);

    if (sigaction(SIGABRT, &sv, NULL) < 0)
        ERROR("error in sigaction(SIGABRT): %s", strerror(errno));

    if (sigaction(SIGHUP, &sv, NULL) < 0)
        ERROR("error in sigaction(SIGHUP): %s", strerror(errno));

    if (sigaction(SIGQUIT, &sv, NULL) < 0)
        ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
}

/*
  Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
  Returns pointer to name or NULL.
*/
static const char *check_netatalk_dirs(const char *name)
{
    int c;

    for (c=0; netatalk_dirs[c]; c++) {
        if ((strcmp(name, netatalk_dirs[c])) == 0)
            return netatalk_dirs[c];
    }
    return NULL;
}


static void usage_ls(void)
{
    printf(
        "Usage: ad ls [-dRl[u]] [file|dir, ...]\n\n"
        "  -l Long Output [-u: unix info]:\n"
        "     <unixinfo ...> <FinderFlags> <AFPAttributes> <Color> <Type> <Creator> <CNID from AppleDouble> <name>\n\n"
        "     FinderFlags (valid for (f)ile and/or (d)irectory):\n"
        "       d = On Desktop (f/d)\n"
        "       e = Hidden extension (f/d)\n"
        "       m = Shared (can run multiple times) (f)\n"
        "       n = No INIT resources (f)\n"
        "       i = Inited (f/d)\n"
        "       c = Custom icon (f/d)\n"
        "       t = Stationery (f)\n"
        "       s = Name locked (f/d)\n"
        "       b = Bundle (f/d)\n"
        "       v = Invisible (f/d)\n"
        "       a = Alias file (f/d)\n\n"
        "     AFPAttributes:\n"
        "       y = System (f/d)\n"
        "       w = No write (f)\n"
        "       p = Needs backup (f/d)\n"
        "       r = No rename (f/d)\n"
        "       l = No delete (f/d)\n"
        "       o = No copy (f)\n\n"
        "     Note: any letter appearing in uppercase means the flag is set\n"
        "           but it's a directory for which the flag is not allowed.\n"
        );
}

static void print_numlinks(const struct stat *statp)
{
    printf("%5ld", (long)statp->st_nlink);
}

static void print_owner(const struct stat *statp)
{
    struct passwd *pwd = getpwuid(statp->st_uid);

    if (pwd == NULL)
        printf(" %-8ld", (long)statp->st_uid);
    else
        printf(" %-8s", pwd->pw_name);
}

static void print_group(const struct stat *statp)
{
    struct group *grp = getgrgid(statp->st_gid);

    if (grp == NULL)
        printf(" %-8ld", (long)statp->st_gid);
    else
        printf(" %-8s", grp->gr_name);
}

static void print_size(const struct stat *statp)
{
    switch (statp->st_mode & S_IFMT) {
    case S_IFCHR:
    case S_IFBLK:
        printf("%4u,%4u", (unsigned)(statp->st_rdev >> 8),
               (unsigned)(statp->st_rdev & 0xFF));
        break;
    default:
        printf("%9lu", (unsigned long)statp->st_size);
    }
}

static void print_date(const struct stat *statp)
{
    time_t now;
    double diff;
    char buf[100], *fmt;

    if (time(&now) == -1) {
        printf(" ????????????");
        return;
    }
    diff = difftime(now, statp->st_mtime);
    if (diff < 0 || diff > 60 * 60 * 24 * 182.5)
        fmt = "%b %e  %Y";
    else
        fmt = "%b %e %H:%M";
    strftime(buf, sizeof(buf), fmt, localtime(&statp->st_mtime));
    printf(" %s", buf);
}

static void print_flags(char *path, afpvol_t *vol, const struct stat *st)
{
    int adflags = 0;
    struct adouble ad;
    char *FinderInfo;
    uint16_t FinderFlags;
    uint16_t AFPattributes;
    char type[5] = "----";
    char creator[5] = "----";
    int i;
    uint32_t cnid;

    if (S_ISDIR(st->st_mode))
        adflags = ADFLAGS_DIR;

    if (vol->vol == NULL || vol->vol->v_path == NULL)
        return;

    ad_init(&ad, vol->vol);

    if ( ad_metadata(path, adflags, &ad) < 0 )
        return;

    FinderInfo = ad_entry(&ad, ADEID_FINDERI);

    memcpy(&FinderFlags, FinderInfo + 8, 2);
    FinderFlags = ntohs(FinderFlags);

    memcpy(type, FinderInfo, 4);
    memcpy(creator, FinderInfo + 4, 4);

    ad_getattr(&ad, &AFPattributes);
    AFPattributes = ntohs(AFPattributes);

    /*
      Finder flags. Lowercase means valid, uppercase means invalid because
      object is a dir and flag is only valid for files.
    */
    putchar(' ');
    if (FinderFlags & FINDERINFO_ISONDESK)
        putchar('d');
    else
        putchar('-');

    if (FinderFlags & FINDERINFO_HIDEEXT)
        putchar('e');
    else
        putchar('-');

    if (FinderFlags & FINDERINFO_ISHARED) {
        if (adflags & ADFLAGS_DIR)
            putchar('M');
        else
            putchar('m');
    } else
        putchar('-');

    if (FinderFlags & FINDERINFO_HASNOINITS) {
        if (adflags & ADFLAGS_DIR)
            putchar('N');
        else
            putchar('n');
    } else
        putchar('-');

    if (FinderFlags & FINDERINFO_HASBEENINITED)
        putchar('i');
    else
        putchar('-');

    if (FinderFlags & FINDERINFO_HASCUSTOMICON)
        putchar('c');
    else
        putchar('-');

    if (FinderFlags & FINDERINFO_ISSTATIONNERY) {
        if (adflags & ADFLAGS_DIR)
            putchar('T');
        else
            putchar('t');
    } else
        putchar('-');

    if (FinderFlags & FINDERINFO_NAMELOCKED)
        putchar('s');
    else
        putchar('-');

    if (FinderFlags & FINDERINFO_HASBUNDLE)
        putchar('b');
    else
        putchar('-');

    if (FinderFlags & FINDERINFO_INVISIBLE)
        putchar('v');
    else
        putchar('-');

    if (FinderFlags & FINDERINFO_ISALIAS)
        putchar('a');
    else
        putchar('-');

    putchar(' ');

    /* AFP attributes */
    if (AFPattributes & ATTRBIT_SYSTEM)
        putchar('y');
    else
        putchar('-');

    if (AFPattributes & ATTRBIT_NOWRITE) {
        if (adflags & ADFLAGS_DIR)
            putchar('W');
        else
            putchar('w');
    } else
        putchar('-');

    if (AFPattributes & ATTRBIT_BACKUP)
        putchar('p');
    else
        putchar('-');

    if (AFPattributes & ATTRBIT_NORENAME)
        putchar('r');
    else
        putchar('-');

    if (AFPattributes & ATTRBIT_NODELETE)
        putchar('l');
    else
        putchar('-');

    if (AFPattributes & ATTRBIT_NOCOPY) {
        if (adflags & ADFLAGS_DIR)
            putchar('O');
        else
            putchar('o');                
    } else
        putchar('-');

    /* Color */
    printf(" %s ", labels[(FinderFlags & FINDERINFO_COLOR) >> 1]);

    /* Type & Creator */
    for(i=0; i<4; i++) {
        if (isalnum(type[i]))
            putchar(type[i]);
        else
            putchar('-');
    }
    putchar(' '); 
    for(i=0; i<4; i++) {
        if (isalnum(creator[i]))
            putchar(creator[i]);
        else
            putchar('-');
    }
    putchar(' '); 

    /* CNID */
    cnid = ad_forcegetid(&ad);
    if (cnid)
        printf(" %10u ", ntohl(cnid));
    else
        printf(" !ADVOL_CACHE ");

    ad_close(&ad, ADFLAGS_HF);
}

#define TYPE(b) ((st->st_mode & (S_IFMT)) == (b))
#define MODE(b) ((st->st_mode & (b)) == (b))

static void print_mode(const struct stat *st)
{
    if (TYPE(S_IFBLK))
        putchar('b');
    else if (TYPE(S_IFCHR))
        putchar('c');
    else if (TYPE(S_IFDIR))
        putchar('d');
    else if (TYPE(S_IFIFO))
        putchar('p');
    else if (TYPE(S_IFREG))
        putchar('-');
    else if (TYPE(S_IFLNK))
        putchar('l');
    else if (TYPE(S_IFSOCK))
        putchar('s');
    else
        putchar('?');
    putchar(MODE(S_IRUSR) ? 'r' : '-');
    putchar(MODE(S_IWUSR) ? 'w' : '-');
    if (MODE(S_ISUID)) {
        if (MODE(S_IXUSR))
            putchar('s');
        else
            putchar('S');
    }
    else if (MODE(S_IXUSR))
        putchar('x');
    else
        putchar('-');
    putchar(MODE(S_IRGRP) ? 'r' : '-');
    putchar(MODE(S_IWGRP) ? 'w' : '-');
    if (MODE(S_ISGID)) {
        if (MODE(S_IXGRP))
            putchar('s');
        else
            putchar('S');
    }
    else if (MODE(S_IXGRP))
        putchar('x');
    else
        putchar('-');
    putchar(MODE(S_IROTH) ? 'r' : '-');
    putchar(MODE(S_IWOTH) ? 'w' : '-');
    if (MODE(S_IFDIR) && MODE(S_ISVTX)) {
        if (MODE(S_IXOTH))
            putchar('t');
        else
            putchar('T');
    }
    else if (MODE(S_IXOTH))
        putchar('x');
    else
        putchar('-');
}
#undef TYPE
#undef MODE

static int ad_print(char *path, const struct stat *st, afpvol_t *vol)
{
    if ( ! ls_l) {
        printf("%s  ", path);
        if (ls_d)
            printf("\n");
        return 0;
    }

    /* Long output */
    if (ls_u) {
        print_mode(st);
        print_numlinks(st);
        print_owner(st);
        print_group(st);
        print_size(st);
        print_date(st);
    }
    print_flags(path, vol, st);
    printf("  %s\n", path);    


    return 0;
}

static int ad_ls_r(char *path, afpvol_t *vol)
{
    int ret = 0, cwd, dirprinted = 0, dirempty;
    const char *name;
    char *tmp;
    static char cwdpath[MAXPATHLEN+1];
    DIR *dp;
    struct dirent *ep;
    static struct stat st;      /* Save some stack space */

    if ( first)
        cwdpath[0] = 0;
    else
        strcat(cwdpath, "/");

    strcat(cwdpath, path);
    first = 0;

    if (lstat(path, &st) < 0) {
        perror("Can't stat");
        return -1;
    }
    /* If its a file or a dir with -d option call ad_print and return */
    if (S_ISREG(st.st_mode) || ls_d)
        return ad_print(path, &st, vol);

    /* Its a dir: chdir to it remembering where we started */
    if ((cwd = open(".", O_RDONLY)) == -1) {
        perror("Can't open .");
        return -1;
    }
    if (chdir(path) != 0) {
        perror("Can't chdir");
        close(cwd);
        return -1;
    }

    if ((dp = opendir (".")) == NULL) {
        perror("Couldn't opendir .");
        return -1;
    }

    /* First run: print everything */
    dirempty = 1;
    while ((ep = readdir (dp))) {
        if (alarmed) {
            ret = -1;
            goto exit;
        }

        /* Check if its "." or ".." */
        if (DIR_DOT_OR_DOTDOT(ep->d_name))
            continue;

        /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
        if ((name = check_netatalk_dirs(ep->d_name)) != NULL)
            continue;

        if ((ep->d_name[0] == '.') && ! ls_a)
            continue;

        dirempty = 0;

        if (recursion && ! dirprinted) {
            printf("\n%s:\n", cwdpath);
            dirprinted = 1;
        }

        if (lstat(ep->d_name, &st) < 0) {
            perror("Can't stat");
            return -1;
        }

        ret = ad_print(ep->d_name, &st, vol);
        if (ret != 0)
            goto exit;
    }

    if (! ls_l && ! dirempty)
        printf("\n");

    /* Second run: recurse to dirs */
    if (ls_R) {
        rewinddir(dp);
        while ((ep = readdir (dp))) {
            if (alarmed) {
                ret = -1;
                goto exit;
            }

            /* Check if its "." or ".." */
            if (DIR_DOT_OR_DOTDOT(ep->d_name))
                continue;

            /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
            if ((name = check_netatalk_dirs(ep->d_name)) != NULL)
                continue;

            if ((ret = lstat(ep->d_name, &st)) < 0) {
                perror("Can't stat");
                goto exit;
            }

            /* Recursion */
            if (S_ISDIR(st.st_mode)) {
                recursion = 1;
                ret = ad_ls_r(ep->d_name, vol);
            }
            if (ret != 0)
                goto exit;
        }
    }

exit:
    closedir(dp);
    fchdir(cwd);
    close(cwd);

    tmp = strrchr(cwdpath, '/');
    if (tmp)
        *tmp = 0;

    return ret;
}

int ad_ls(int argc, char **argv, AFPObj *obj)
{
    int c, firstarg;
    afpvol_t vol;
    struct stat st;

    while ((c = getopt(argc, argv, ":adlRu")) != -1) {
        switch(c) {
        case 'a':
            ls_a = 1;
            break;
        case 'd':
            ls_d = 1;
            break;
        case 'l':
            ls_l = 1;
            break;
        case 'R':
            ls_R = 1;
            break;
        case 'u':
            ls_u = 1;
            break;
        case ':':
        case '?':
            usage_ls();
            return -1;
            break;
        }

    }

    set_signal();
    cnid_init();

    if ((argc - optind) == 0) {
        openvol(obj, ".", &vol);
        ad_ls_r(".", &vol);
        closevol(&vol);
    }
    else {
        int havefile = 0;

        firstarg = optind;

        /* First run: only print files from argv paths */
        while(optind < argc) {
            if (stat(argv[optind], &st) != 0)
                goto next;
            if (S_ISDIR(st.st_mode))
                goto next;

            havefile = 1;
            first = 1;
            recursion = 0;

            openvol(obj, argv[optind], &vol);
            ad_ls_r(argv[optind], &vol);
            closevol(&vol);
        next:
            optind++;
        }
        if (havefile && (! ls_l))
            printf("\n");

        /* Second run: print dirs */
        optind = firstarg;
        while(optind < argc) {
            if (stat(argv[optind], &st) != 0)
                goto next2;
            if ( ! S_ISDIR(st.st_mode))
                goto next2;
            if ((optind > firstarg) || havefile)
                printf("\n%s:\n", argv[optind]);

            first = 1;
            recursion = 0;

            openvol(obj, argv[optind], &vol);
            ad_ls_r(argv[optind], &vol);
            closevol(&vol);

        next2:
            optind++;
        }


    }

    return 0;
}