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-2022 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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
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.
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.
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.
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".
31 * The format is not exactly the same as any particular Unix ls(1).
33 * A key requirement for this program is that the output be "very
34 * reproducible." So we mask away information that can accidentally
39 #include "lib/sysxattrs.h"
43 /* These are to make syscall.o shut up. */
55 #ifdef HAVE_LINUX_XATTRS
56 #define XSTAT_ATTR "user.rsync.%stat"
58 #define XSTAT_ATTR "rsync.%stat"
61 static int stat_xattr(const char *fname
, STRUCT_STAT
*fst
)
64 int rdev_major
, rdev_minor
, uid
, gid
, len
;
67 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
70 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
71 if (len
>= (int)sizeof buf
) {
76 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
78 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
83 fprintf(stderr
, "failed to read xattr %s for %s: %s\n",
84 XSTAT_ATTR
, fname
, strerror(errno
));
89 if (sscanf(buf
, "%o %d,%d %d:%d",
90 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
91 fprintf(stderr
, "Corrupt %s xattr attached to %s: \"%s\"\n",
92 XSTAT_ATTR
, fname
, buf
);
96 #if _S_IFLNK != 0120000
97 if ((mode
& (_S_IFMT
)) == 0120000)
98 mode
= (mode
& ~(_S_IFMT
)) | _S_IFLNK
;
102 fst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
111 static int display_atimes
= 0;
112 #ifdef SUPPORT_CRTIMES
113 static int display_crtimes
= 0;
116 static void failed(char const *what
, char const *where
)
118 fprintf(stderr
, PROGRAM
": %s %s: %s\n",
119 what
, where
, strerror(errno
));
123 static void storetime(char *dest
, size_t destsize
, time_t t
, int nsecs
)
127 struct tm
*mt
= gmtime(&t
);
129 len
= snprintf(dest
, destsize
,
130 " %04d-%02d-%02d %02d:%02d:%02d",
131 (int)mt
->tm_year
+ 1900,
137 if (nsecs
>= 0 && len
>= 0)
138 snprintf(dest
+ len
, destsize
- len
, ".%09d", nsecs
);
140 int has_nsecs
= nsecs
>= 0 ? 1 : 0;
141 int len
= MIN(20 + 10*has_nsecs
, (int)destsize
- 1);
142 memset(dest
, ' ', len
);
147 static void list_file(const char *fname
)
150 #ifdef SUPPORT_CRTIMES
153 char permbuf
[PERMSTRING_SIZE
];
160 if (do_lstat(fname
, &buf
) < 0)
161 failed("stat", fname
);
162 #ifdef SUPPORT_CRTIMES
163 if (display_crtimes
&& (crtime
= get_create_time(fname
, &buf
)) == 0)
164 failed("get_create_time", fname
);
166 #ifdef SUPPORT_XATTRS
168 stat_xattr(fname
, &buf
);
171 /* The size of anything but a regular file is probably not
172 * worth thinking about. */
173 if (!S_ISREG(buf
.st_mode
))
176 /* On some BSD platforms the mode bits of a symlink are
177 * undefined. Also it tends not to be possible to reset a
178 * symlink's mtime, so we default to ignoring it too. */
179 if (S_ISLNK(buf
.st_mode
)) {
181 buf
.st_mode
&= ~0777;
183 buf
.st_mtime
= (time_t)0;
185 buf
.st_uid
= buf
.st_gid
= 0;
186 strlcpy(linkbuf
, " -> ", sizeof linkbuf
);
187 /* const-cast required for silly UNICOS headers */
188 len
= do_readlink((char*)fname
, linkbuf
+4, sizeof linkbuf
- 4);
190 failed("do_readlink", fname
);
192 /* it's not nul-terminated */
198 permstring(permbuf
, buf
.st_mode
);
201 nsecs
= (int)buf
.ST_MTIME_NSEC
;
205 storetime(mtimebuf
, sizeof mtimebuf
, buf
.st_mtime
, nsecs
);
207 storetime(atimebuf
, sizeof atimebuf
, S_ISDIR(buf
.st_mode
) ? 0 : buf
.st_atime
, -1);
210 #ifdef SUPPORT_CRTIMES
212 storetime(crtimebuf
, sizeof crtimebuf
, crtime
, -1);
217 /* TODO: Perhaps escape special characters in fname? */
218 printf("%s ", permbuf
);
220 if (S_ISCHR(buf
.st_mode
) || S_ISBLK(buf
.st_mode
)) {
221 printf("%5ld,%6ld", (long)major(buf
.st_rdev
), (long)minor(buf
.st_rdev
));
223 printf("%15s", do_big_num(buf
.st_size
, 1, NULL
));
225 printf(" %6ld.%-6ld %6ld%s%s%s %s%s\n",
226 (long)buf
.st_uid
, (long)buf
.st_gid
, (long)buf
.st_nlink
,
227 mtimebuf
, atimebuf
, crtimebuf
, fname
, linkbuf
);
230 static struct poptOption long_options
[] = {
231 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
232 {"atimes", 'U', POPT_ARG_NONE
, &display_atimes
, 0, 0, 0},
233 #ifdef SUPPORT_CRTIMES
234 {"crtimes", 'N', POPT_ARG_NONE
, &display_crtimes
, 0, 0, 0},
236 {"link-times", 'l', POPT_ARG_NONE
, &link_times
, 0, 0, 0 },
237 {"link-owner", 'L', POPT_ARG_NONE
, &link_owner
, 0, 0, 0 },
238 #ifdef SUPPORT_XATTRS
239 {"fake-super", 'f', POPT_ARG_VAL
, &am_root
, -1, 0, 0 },
242 {"nsec", 's', POPT_ARG_NONE
, &nsec_times
, 0, 0, 0 },
244 {"help", 'h', POPT_ARG_NONE
, 0, 'h', 0, 0 },
248 static void NORETURN
tls_usage(int ret
)
250 FILE *F
= ret
? stderr
: stdout
;
251 fprintf(F
,"usage: " PROGRAM
" [OPTIONS] FILE ...\n");
252 fprintf(F
,"Trivial file listing program for portably checking rsync\n");
253 fprintf(F
,"\nOptions:\n");
254 fprintf(F
," -U, --atimes display access (last-used) times\n");
255 #ifdef SUPPORT_CRTIMES
256 fprintf(F
," -N, --crtimes display create times (newness)\n");
258 fprintf(F
," -l, --link-times display the time on a symlink\n");
259 fprintf(F
," -L, --link-owner display the owner+group on a symlink\n");
260 #ifdef SUPPORT_XATTRS
261 fprintf(F
," -f, --fake-super display attributes including fake-super xattrs\n");
263 fprintf(F
," -h, --help show this help\n");
268 main(int argc
, char *argv
[])
271 const char **extra_args
;
274 pc
= poptGetContext(PROGRAM
, argc
, (const char **)argv
, long_options
, 0);
275 while ((opt
= poptGetNextOpt(pc
)) != -1) {
280 fprintf(stderr
, "%s: %s\n",
281 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
287 extra_args
= poptGetArgs(pc
);
288 if (!extra_args
|| *extra_args
== NULL
)
291 for (; *extra_args
; extra_args
++)
292 list_file(*extra_args
);