Annotation of embedaddon/rsync/tls.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Trivial ls for comparing two directories after running an rsync.
                      3:  *
                      4:  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
                      5:  * Copyright (C) 2003-2009 Wayne Davison
                      6:  *
                      7:  * This program is free software; you can redistribute it and/or modify
                      8:  * it under the terms of the GNU General Public License as published by
                      9:  * the Free Software Foundation; either version 3 of the License, or
                     10:  * (at your option) any later version.
                     11:  *
                     12:  * This program is distributed in the hope that it will be useful,
                     13:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15:  * GNU General Public License for more details.
                     16:  *
                     17:  * You should have received a copy of the GNU General Public License along
                     18:  * with this program; if not, write to the Free Software Foundation, Inc.,
                     19:  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
                     20:  */
                     21: 
                     22: /* The problem with using the system's own ls is that some features
                     23:  * have little quirks that make directories look different when for
                     24:  * our purposes they're the same -- for example, the BSD braindamage
                     25:  * about setting the mode on symlinks based on your current umask.
                     26:  *
                     27:  * All the filenames must be given on the command line -- tls does not
                     28:  * even read directories, let alone recurse.  The typical usage is
                     29:  * "find|sort|xargs tls".
                     30:  *
                     31:  * The format is not exactly the same as any particular Unix ls(1).
                     32:  *
                     33:  * A key requirement for this program is that the output be "very
                     34:  * reproducible."  So we mask away information that can accidentally
                     35:  * change. */
                     36: 
                     37: #include "rsync.h"
                     38: #include <popt.h>
                     39: #include "lib/sysxattrs.h"
                     40: 
                     41: #define PROGRAM "tls"
                     42: 
                     43: /* These are to make syscall.o shut up. */
                     44: int dry_run = 0;
                     45: int am_root = 0;
                     46: int read_only = 1;
                     47: int list_only = 0;
                     48: int link_times = 0;
                     49: int link_owner = 0;
                     50: int preserve_perms = 0;
                     51: int preserve_executability = 0;
                     52: 
                     53: #ifdef SUPPORT_XATTRS
                     54: 
                     55: #ifdef HAVE_LINUX_XATTRS
                     56: #define XSTAT_ATTR "user.rsync.%stat"
                     57: #else
                     58: #define XSTAT_ATTR "rsync.%stat"
                     59: #endif
                     60: 
                     61: static int stat_xattr(const char *fname, STRUCT_STAT *fst)
                     62: {
                     63:        int mode, rdev_major, rdev_minor, uid, gid, len;
                     64:        char buf[256];
                     65: 
                     66:        if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
                     67:                return -1;
                     68: 
                     69:        len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
                     70:        if (len >= (int)sizeof buf) {
                     71:                len = -1;
                     72:                errno = ERANGE;
                     73:        }
                     74:        if (len < 0) {
                     75:                if (errno == ENOTSUP || errno == ENOATTR)
                     76:                        return -1;
                     77:                if (errno == EPERM && S_ISLNK(fst->st_mode)) {
                     78:                        fst->st_uid = 0;
                     79:                        fst->st_gid = 0;
                     80:                        return 0;
                     81:                }
                     82:                fprintf(stderr, "failed to read xattr %s for %s: %s\n",
                     83:                        XSTAT_ATTR, fname, strerror(errno));
                     84:                return -1;
                     85:        }
                     86:        buf[len] = '\0';
                     87: 
                     88:        if (sscanf(buf, "%o %d,%d %d:%d",
                     89:                   &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
                     90:                fprintf(stderr, "Corrupt %s xattr attached to %s: \"%s\"\n",
                     91:                        XSTAT_ATTR, fname, buf);
                     92:                exit(1);
                     93:        }
                     94: 
                     95: #if _S_IFLNK != 0120000
                     96:        if ((mode & (_S_IFMT)) == 0120000)
                     97:                mode = (mode & ~(_S_IFMT)) | _S_IFLNK;
                     98: #endif
                     99:        fst->st_mode = mode;
                    100: 
                    101:        fst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
                    102:        fst->st_uid = uid;
                    103:        fst->st_gid = gid;
                    104: 
                    105:        return 0;
                    106: }
                    107: 
                    108: #endif
                    109: 
                    110: static void failed(char const *what, char const *where)
                    111: {
                    112:        fprintf(stderr, PROGRAM ": %s %s: %s\n",
                    113:                what, where, strerror(errno));
                    114:        exit(1);
                    115: }
                    116: 
                    117: static void list_file(const char *fname)
                    118: {
                    119:        STRUCT_STAT buf;
                    120:        char permbuf[PERMSTRING_SIZE];
                    121:        struct tm *mt;
                    122:        char datebuf[50];
                    123:        char linkbuf[4096];
                    124: 
                    125:        if (do_lstat(fname, &buf) < 0)
                    126:                failed("stat", fname);
                    127: #ifdef SUPPORT_XATTRS
                    128:        if (am_root < 0)
                    129:                stat_xattr(fname, &buf);
                    130: #endif
                    131: 
                    132:        /* The size of anything but a regular file is probably not
                    133:         * worth thinking about. */
                    134:        if (!S_ISREG(buf.st_mode))
                    135:                buf.st_size = 0;
                    136: 
                    137:        /* On some BSD platforms the mode bits of a symlink are
                    138:         * undefined.  Also it tends not to be possible to reset a
                    139:         * symlink's mtime, so we default to ignoring it too. */
                    140:        if (S_ISLNK(buf.st_mode)) {
                    141:                int len;
                    142:                buf.st_mode &= ~0777;
                    143:                if (!link_times)
                    144:                        buf.st_mtime = (time_t)0;
                    145:                if (!link_owner)
                    146:                        buf.st_uid = buf.st_gid = 0;
                    147:                strlcpy(linkbuf, " -> ", sizeof linkbuf);
                    148:                /* const-cast required for silly UNICOS headers */
                    149:                len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4);
                    150:                if (len == -1)
                    151:                        failed("readlink", fname);
                    152:                else
                    153:                        /* it's not nul-terminated */
                    154:                        linkbuf[4+len] = 0;
                    155:        } else {
                    156:                linkbuf[0] = 0;
                    157:        }
                    158: 
                    159:        permstring(permbuf, buf.st_mode);
                    160: 
                    161:        if (buf.st_mtime) {
                    162:                mt = gmtime(&buf.st_mtime);
                    163: 
                    164:                snprintf(datebuf, sizeof datebuf,
                    165:                        "%04d-%02d-%02d %02d:%02d:%02d",
                    166:                        (int)mt->tm_year + 1900,
                    167:                        (int)mt->tm_mon + 1,
                    168:                        (int)mt->tm_mday,
                    169:                        (int)mt->tm_hour,
                    170:                        (int)mt->tm_min,
                    171:                        (int)mt->tm_sec);
                    172:        } else
                    173:                strlcpy(datebuf, "                   ", sizeof datebuf);
                    174: 
                    175:        /* TODO: Perhaps escape special characters in fname? */
                    176: 
                    177:        printf("%s ", permbuf);
                    178:        if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) {
                    179:                printf("%5ld,%6ld",
                    180:                    (long)major(buf.st_rdev),
                    181:                    (long)minor(buf.st_rdev));
                    182:        } else /* NB: use double for size since it might not fit in a long. */
                    183:                printf("%12.0f", (double)buf.st_size);
                    184:        printf(" %6ld.%-6ld %6ld %s %s%s\n",
                    185:               (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
                    186:               datebuf, fname, linkbuf);
                    187: }
                    188: 
                    189: static struct poptOption long_options[] = {
                    190:   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
                    191:   {"link-times",      'l', POPT_ARG_NONE,   &link_times, 0, 0, 0 },
                    192:   {"link-owner",      'L', POPT_ARG_NONE,   &link_owner, 0, 0, 0 },
                    193: #ifdef SUPPORT_XATTRS
                    194:   {"fake-super",      'f', POPT_ARG_VAL,    &am_root, -1, 0, 0 },
                    195: #endif
                    196:   {"help",            'h', POPT_ARG_NONE,   0, 'h', 0, 0 },
                    197:   {0,0,0,0,0,0,0}
                    198: };
                    199: 
                    200: static void tls_usage(int ret)
                    201: {
                    202:   FILE *F = ret ? stderr : stdout;
                    203:   fprintf(F,"usage: " PROGRAM " [OPTIONS] FILE ...\n");
                    204:   fprintf(F,"Trivial file listing program for portably checking rsync\n");
                    205:   fprintf(F,"\nOptions:\n");
                    206:   fprintf(F," -l, --link-times            display the time on a symlink\n");
                    207:   fprintf(F," -L, --link-owner            display the owner+group on a symlink\n");
                    208: #ifdef SUPPORT_XATTRS
                    209:   fprintf(F," -f, --fake-super            display attributes including fake-super xattrs\n");
                    210: #endif
                    211:   fprintf(F," -h, --help                  show this help\n");
                    212:   exit(ret);
                    213: }
                    214: 
                    215: int
                    216: main(int argc, char *argv[])
                    217: {
                    218:        poptContext pc;
                    219:        const char **extra_args;
                    220:        int opt;
                    221: 
                    222:        pc = poptGetContext(PROGRAM, argc, (const char **)argv,
                    223:                            long_options, 0);
                    224:        while ((opt = poptGetNextOpt(pc)) != -1) {
                    225:                switch (opt) {
                    226:                case 'h':
                    227:                        tls_usage(0);
                    228:                default:
                    229:                        fprintf(stderr,
                    230:                                "%s: %s\n",
                    231:                                poptBadOption(pc, POPT_BADOPTION_NOALIAS),
                    232:                                poptStrerror(opt));
                    233:                        tls_usage(1);
                    234:                }
                    235:        }
                    236: 
                    237:        extra_args = poptGetArgs(pc);
                    238:        if (!extra_args || *extra_args == NULL)
                    239:                tls_usage(1);
                    240: 
                    241:        for (; *extra_args; extra_args++)
                    242:                list_file(*extra_args);
                    243:        poptFreeContext(pc);
                    244: 
                    245:        return 0;
                    246: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>