2 * Trivial ls for comparing two directories after running an rsync.
4 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
5 * Copyright (C) 2003-2007 Wayne Davison
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21 /* The problem with using the system's own ls is that some features
22 * have little quirks that make directories look different when for
23 * our purposes they're the same -- for example, the BSD braindamage
24 * about setting the mode on symlinks based on your current umask.
26 * All the filenames must be given on the command line -- tls does not
27 * even read directories, let alone recurse. The typical usage is
28 * "find|sort|xargs tls".
30 * The format is not exactly the same as any particular Unix ls(1).
32 * A key requirement for this program is that the output be "very
33 * reproducible." So we mask away information that can accidentally
37 #include "lib/sysxattrs.h"
41 /* These are to make syscall.o shut up. */
46 int preserve_perms = 0;
48 #ifdef HAVE_LINUX_XATTRS
49 #define XSTAT_ATTR "user.rsync.%stat"
51 #define XSTAT_ATTR "rsync.%stat"
54 static int stat_xattr(const char *fname, STRUCT_STAT *fst)
56 int mode, rdev_major, rdev_minor, uid, gid, len;
59 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
62 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
63 if (len >= (int)sizeof buf) {
68 if (errno == ENOTSUP || errno == ENOATTR)
70 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
75 fprintf(stderr, "failed to read xattr %s for %s: %s\n",
76 XSTAT_ATTR, fname, strerror(errno));
81 if (sscanf(buf, "%o %d,%d %d:%d",
82 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
83 fprintf(stderr, "Corrupt %s xattr attached to %s: \"%s\"\n",
84 XSTAT_ATTR, fname, buf);
88 fst->st_mode = from_wire_mode(mode);
89 fst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
96 static void failed(char const *what, char const *where)
98 fprintf(stderr, PROGRAM ": %s %s: %s\n",
99 what, where, strerror(errno));
103 static void list_file(const char *fname)
106 char permbuf[PERMSTRING_SIZE];
111 if (do_lstat(fname, &buf) < 0)
112 failed("stat", fname);
114 stat_xattr(fname, &buf);
116 /* The size of anything but a regular file is probably not
117 * worth thinking about. */
118 if (!S_ISREG(buf.st_mode))
121 /* On some BSD platforms the mode bits of a symlink are
122 * undefined. Also it tends not to be possible to reset a
123 * symlink's mtime, so we have to ignore it too. */
124 if (S_ISLNK(buf.st_mode)) {
126 buf.st_mode &= ~0777;
127 buf.st_mtime = (time_t)0;
128 buf.st_uid = buf.st_gid = 0;
129 strlcpy(linkbuf, " -> ", sizeof linkbuf);
130 /* const-cast required for silly UNICOS headers */
131 len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4);
133 failed("readlink", fname);
135 /* it's not nul-terminated */
141 permstring(permbuf, buf.st_mode);
144 mt = gmtime(&buf.st_mtime);
146 snprintf(datebuf, sizeof datebuf,
147 "%04d-%02d-%02d %02d:%02d:%02d",
148 (int)mt->tm_year + 1900,
155 strlcpy(datebuf, " ", sizeof datebuf);
157 /* TODO: Perhaps escape special characters in fname? */
159 printf("%s ", permbuf);
160 if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) {
162 (long)major(buf.st_rdev),
163 (long)minor(buf.st_rdev));
164 } else /* NB: use double for size since it might not fit in a long. */
165 printf("%12.0f", (double)buf.st_size);
166 printf(" %6ld.%-6ld %6ld %s %s%s\n",
167 (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
168 datebuf, fname, linkbuf);
172 main(int argc, char *argv[])
175 fprintf(stderr, "usage: " PROGRAM " FILE ...\n"
176 "Trivial file listing program for portably checking rsync\n");
180 if (getenv("RSYNC_FAKE_SUPER"))
183 for (argv++; *argv; argv++)