]>
Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Trivial ls for comparing two directories after running an rsync. | |
f22ee865 | 3 | * |
0f78b815 | 4 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
c3b553a9 | 5 | * Copyright (C) 2003-2022 Wayne Davison |
f26ac1e8 | 6 | * |
ba2133d6 | 7 | * This program is free software; you can redistribute it and/or modify |
8e41b68e WD |
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. | |
f26ac1e8 | 11 | * |
f22ee865 MP |
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. | |
f26ac1e8 | 16 | * |
e7c67065 WD |
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. | |
f22ee865 MP |
20 | */ |
21 | ||
0f78b815 | 22 | /* The problem with using the system's own ls is that some features |
f22ee865 MP |
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 | * | |
4ed886ae MP |
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". | |
f22ee865 | 30 | * |
4ed886ae | 31 | * The format is not exactly the same as any particular Unix ls(1). |
f22ee865 | 32 | * |
4ed886ae MP |
33 | * A key requirement for this program is that the output be "very |
34 | * reproducible." So we mask away information that can accidentally | |
0f78b815 | 35 | * change. */ |
f22ee865 | 36 | |
f22ee865 | 37 | #include "rsync.h" |
8afaef42 | 38 | #include <popt.h> |
9439c0cb | 39 | #include "lib/sysxattrs.h" |
f22ee865 MP |
40 | |
41 | #define PROGRAM "tls" | |
42 | ||
43 | /* These are to make syscall.o shut up. */ | |
44 | int dry_run = 0; | |
9439c0cb | 45 | int am_root = 0; |
6e310d38 | 46 | int am_sender = 1; |
f22ee865 MP |
47 | int read_only = 1; |
48 | int list_only = 0; | |
5b385336 WD |
49 | int link_times = 0; |
50 | int link_owner = 0; | |
1a2e41af | 51 | int nsec_times = 0; |
f22ee865 | 52 | |
acac1f5c WD |
53 | #ifdef SUPPORT_XATTRS |
54 | ||
9439c0cb WD |
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 | { | |
8f151118 WD |
63 | unsigned int mode; |
64 | int rdev_major, rdev_minor, uid, gid, len; | |
9439c0cb WD |
65 | char buf[256]; |
66 | ||
67 | if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode)) | |
68 | return -1; | |
69 | ||
70 | len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1); | |
71 | if (len >= (int)sizeof buf) { | |
72 | len = -1; | |
73 | errno = ERANGE; | |
74 | } | |
75 | if (len < 0) { | |
76 | if (errno == ENOTSUP || errno == ENOATTR) | |
77 | return -1; | |
78 | if (errno == EPERM && S_ISLNK(fst->st_mode)) { | |
79 | fst->st_uid = 0; | |
80 | fst->st_gid = 0; | |
81 | return 0; | |
82 | } | |
83 | fprintf(stderr, "failed to read xattr %s for %s: %s\n", | |
84 | XSTAT_ATTR, fname, strerror(errno)); | |
85 | return -1; | |
86 | } | |
87 | buf[len] = '\0'; | |
88 | ||
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); | |
93 | exit(1); | |
94 | } | |
95 | ||
1b42f628 WD |
96 | #if _S_IFLNK != 0120000 |
97 | if ((mode & (_S_IFMT)) == 0120000) | |
98 | mode = (mode & ~(_S_IFMT)) | _S_IFLNK; | |
99 | #endif | |
100 | fst->st_mode = mode; | |
101 | ||
9439c0cb WD |
102 | fst->st_rdev = MAKEDEV(rdev_major, rdev_minor); |
103 | fst->st_uid = uid; | |
104 | fst->st_gid = gid; | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
acac1f5c WD |
109 | #endif |
110 | ||
b9367410 | 111 | static int display_atimes = 0; |
974f49e2 WD |
112 | #ifdef SUPPORT_CRTIMES |
113 | static int display_crtimes = 0; | |
114 | #endif | |
b9367410 | 115 | |
630e3c40 | 116 | static void failed(char const *what, char const *where) |
f22ee865 | 117 | { |
630e3c40 WD |
118 | fprintf(stderr, PROGRAM ": %s %s: %s\n", |
119 | what, where, strerror(errno)); | |
120 | exit(1); | |
f22ee865 MP |
121 | } |
122 | ||
b9367410 WD |
123 | static void storetime(char *dest, size_t destsize, time_t t, int nsecs) |
124 | { | |
125 | if (t) { | |
126 | int len; | |
127 | struct tm *mt = gmtime(&t); | |
128 | ||
129 | len = snprintf(dest, destsize, | |
130 | " %04d-%02d-%02d %02d:%02d:%02d", | |
131 | (int)mt->tm_year + 1900, | |
132 | (int)mt->tm_mon + 1, | |
133 | (int)mt->tm_mday, | |
134 | (int)mt->tm_hour, | |
135 | (int)mt->tm_min, | |
136 | (int)mt->tm_sec); | |
137 | if (nsecs >= 0 && len >= 0) | |
138 | snprintf(dest + len, destsize - len, ".%09d", nsecs); | |
139 | } else { | |
140 | int has_nsecs = nsecs >= 0 ? 1 : 0; | |
141 | int len = MIN(20 + 10*has_nsecs, (int)destsize - 1); | |
142 | memset(dest, ' ', len); | |
143 | dest[len] = '\0'; | |
144 | } | |
145 | } | |
146 | ||
630e3c40 | 147 | static void list_file(const char *fname) |
f22ee865 | 148 | { |
d0f821ad | 149 | STRUCT_STAT buf; |
974f49e2 WD |
150 | #ifdef SUPPORT_CRTIMES |
151 | time_t crtime = 0; | |
152 | #endif | |
4ed886ae | 153 | char permbuf[PERMSTRING_SIZE]; |
b9367410 WD |
154 | char mtimebuf[50]; |
155 | char atimebuf[50]; | |
974f49e2 | 156 | char crtimebuf[50]; |
1336e414 | 157 | char linkbuf[4096]; |
b9367410 | 158 | int nsecs; |
4ed886ae | 159 | |
018b2832 | 160 | if (do_lstat(fname, &buf) < 0) |
630e3c40 | 161 | failed("stat", fname); |
974f49e2 | 162 | #ifdef SUPPORT_CRTIMES |
291a042b | 163 | if (display_crtimes && (crtime = get_create_time(fname, &buf)) == 0) |
974f49e2 WD |
164 | failed("get_create_time", fname); |
165 | #endif | |
acac1f5c | 166 | #ifdef SUPPORT_XATTRS |
9439c0cb WD |
167 | if (am_root < 0) |
168 | stat_xattr(fname, &buf); | |
acac1f5c | 169 | #endif |
4ed886ae | 170 | |
2d4c8e59 MP |
171 | /* The size of anything but a regular file is probably not |
172 | * worth thinking about. */ | |
173 | if (!S_ISREG(buf.st_mode)) | |
4ed886ae | 174 | buf.st_size = 0; |
f22ee865 | 175 | |
dd0700b0 MP |
176 | /* On some BSD platforms the mode bits of a symlink are |
177 | * undefined. Also it tends not to be possible to reset a | |
5b385336 | 178 | * symlink's mtime, so we default to ignoring it too. */ |
dd0700b0 | 179 | if (S_ISLNK(buf.st_mode)) { |
0771727d | 180 | int len; |
dd0700b0 | 181 | buf.st_mode &= ~0777; |
5b385336 WD |
182 | if (!link_times) |
183 | buf.st_mtime = (time_t)0; | |
184 | if (!link_owner) | |
185 | buf.st_uid = buf.st_gid = 0; | |
c9bce0b8 | 186 | strlcpy(linkbuf, " -> ", sizeof linkbuf); |
acf1af0c | 187 | /* const-cast required for silly UNICOS headers */ |
b9367410 | 188 | len = do_readlink((char*)fname, linkbuf+4, sizeof linkbuf - 4); |
f26ac1e8 | 189 | if (len == -1) |
6e310d38 | 190 | failed("do_readlink", fname); |
0771727d MP |
191 | else |
192 | /* it's not nul-terminated */ | |
193 | linkbuf[4+len] = 0; | |
1336e414 | 194 | } else { |
b9367410 | 195 | linkbuf[0] = '\0'; |
dd0700b0 | 196 | } |
f22ee865 | 197 | |
dd0700b0 | 198 | permstring(permbuf, buf.st_mode); |
1a2e41af | 199 | #ifdef ST_MTIME_NSEC |
b9367410 WD |
200 | if (nsec_times) |
201 | nsecs = (int)buf.ST_MTIME_NSEC; | |
202 | else | |
1a2e41af | 203 | #endif |
b9367410 WD |
204 | nsecs = -1; |
205 | storetime(mtimebuf, sizeof mtimebuf, buf.st_mtime, nsecs); | |
206 | if (display_atimes) | |
207 | storetime(atimebuf, sizeof atimebuf, S_ISDIR(buf.st_mode) ? 0 : buf.st_atime, -1); | |
208 | else | |
209 | atimebuf[0] = '\0'; | |
974f49e2 WD |
210 | #ifdef SUPPORT_CRTIMES |
211 | if (display_crtimes) | |
212 | storetime(crtimebuf, sizeof crtimebuf, crtime, -1); | |
213 | else | |
214 | #endif | |
215 | crtimebuf[0] = '\0'; | |
f26ac1e8 | 216 | |
4ed886ae | 217 | /* TODO: Perhaps escape special characters in fname? */ |
f26ac1e8 | 218 | printf("%s ", permbuf); |
b9367410 | 219 | |
48d3ff94 | 220 | if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) { |
e63ff70e | 221 | printf("%5ld,%6ld", (long)major(buf.st_rdev), (long)minor(buf.st_rdev)); |
7f0db4fd | 222 | } else |
adc2476f | 223 | printf("%15s", do_big_num(buf.st_size, 1, NULL)); |
b9367410 | 224 | |
974f49e2 | 225 | printf(" %6ld.%-6ld %6ld%s%s%s %s%s\n", |
f26ac1e8 | 226 | (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink, |
974f49e2 | 227 | mtimebuf, atimebuf, crtimebuf, fname, linkbuf); |
f22ee865 MP |
228 | } |
229 | ||
db9c9e27 WD |
230 | static struct poptOption long_options[] = { |
231 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
b9367410 | 232 | {"atimes", 'U', POPT_ARG_NONE, &display_atimes, 0, 0, 0}, |
974f49e2 WD |
233 | #ifdef SUPPORT_CRTIMES |
234 | {"crtimes", 'N', POPT_ARG_NONE, &display_crtimes, 0, 0, 0}, | |
235 | #endif | |
5b385336 WD |
236 | {"link-times", 'l', POPT_ARG_NONE, &link_times, 0, 0, 0 }, |
237 | {"link-owner", 'L', POPT_ARG_NONE, &link_owner, 0, 0, 0 }, | |
acac1f5c | 238 | #ifdef SUPPORT_XATTRS |
db9c9e27 | 239 | {"fake-super", 'f', POPT_ARG_VAL, &am_root, -1, 0, 0 }, |
1a2e41af WD |
240 | #endif |
241 | #ifdef ST_MTIME_NSEC | |
242 | {"nsec", 's', POPT_ARG_NONE, &nsec_times, 0, 0, 0 }, | |
acac1f5c | 243 | #endif |
db9c9e27 WD |
244 | {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 }, |
245 | {0,0,0,0,0,0,0} | |
246 | }; | |
247 | ||
ad17b218 | 248 | static void NORETURN tls_usage(int ret) |
db9c9e27 WD |
249 | { |
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"); | |
b9367410 | 254 | fprintf(F," -U, --atimes display access (last-used) times\n"); |
974f49e2 WD |
255 | #ifdef SUPPORT_CRTIMES |
256 | fprintf(F," -N, --crtimes display create times (newness)\n"); | |
257 | #endif | |
5b385336 WD |
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"); | |
acac1f5c | 260 | #ifdef SUPPORT_XATTRS |
9a234269 | 261 | fprintf(F," -f, --fake-super display attributes including fake-super xattrs\n"); |
acac1f5c | 262 | #endif |
9a234269 | 263 | fprintf(F," -h, --help show this help\n"); |
db9c9e27 WD |
264 | exit(ret); |
265 | } | |
266 | ||
f69204ad WD |
267 | int |
268 | main(int argc, char *argv[]) | |
f22ee865 | 269 | { |
db9c9e27 WD |
270 | poptContext pc; |
271 | const char **extra_args; | |
272 | int opt; | |
273 | ||
e63ff70e | 274 | pc = poptGetContext(PROGRAM, argc, (const char **)argv, long_options, 0); |
db9c9e27 WD |
275 | while ((opt = poptGetNextOpt(pc)) != -1) { |
276 | switch (opt) { | |
277 | case 'h': | |
278 | tls_usage(0); | |
279 | default: | |
e63ff70e | 280 | fprintf(stderr, "%s: %s\n", |
db9c9e27 WD |
281 | poptBadOption(pc, POPT_BADOPTION_NOALIAS), |
282 | poptStrerror(opt)); | |
283 | tls_usage(1); | |
284 | } | |
f22ee865 MP |
285 | } |
286 | ||
db9c9e27 WD |
287 | extra_args = poptGetArgs(pc); |
288 | if (!extra_args || *extra_args == NULL) | |
289 | tls_usage(1); | |
9439c0cb | 290 | |
db9c9e27 WD |
291 | for (; *extra_args; extra_args++) |
292 | list_file(*extra_args); | |
293 | poptFreeContext(pc); | |
f22ee865 MP |
294 | |
295 | return 0; | |
296 | } |