Debian Codegraph @

tls.c

raw symbols SCIP
1 /*
2 * Trivial ls for comparing two directories after running an rsync.
3 *
4 * Copyright (C) 2001, 2002 Martin Pool <[email protected]>
5 * Copyright (C) 2003-2022 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 am_sender = 1;
47 int read_only = 1;
48 int list_only = 0;
49 int link_times = 0;
50 int link_owner = 0;
51 int nsec_times = 0;
52 int safe_symlinks = 0;
53 int copy_links = 0;
54 int copy_unsafe_links = 0;
55
56 #ifdef SUPPORT_XATTRS
57
58 #ifdef HAVE_LINUX_XATTRS
59 #define XSTAT_ATTR "user.rsync.%stat"
60 #else
61 #define XSTAT_ATTR "rsync.%stat"
62 #endif
63
64 static int stat_xattr(const char *fname, STRUCT_STAT *fst)
65 {
66 unsigned int mode;
67 int rdev_major, rdev_minor, uid, gid, len;
68 char buf[256];
69
70 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
71 return -1;
72
73 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
74 if (len >= (int)sizeof buf) {
75 len = -1;
76 errno = ERANGE;
77 }
78 if (len < 0) {
79 if (errno == ENOTSUP || errno == ENOATTR)
80 return -1;
81 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
82 fst->st_uid = 0;
83 fst->st_gid = 0;
84 return 0;
85 }
86 fprintf(stderr, "failed to read xattr %s for %s: %s\n",
87 XSTAT_ATTR, fname, strerror(errno));
88 return -1;
89 }
90 buf[len] = '\0';
91
92 if (sscanf(buf, "%o %d,%d %d:%d",
93 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
94 fprintf(stderr, "Corrupt %s xattr attached to %s: \"%s\"\n",
95 XSTAT_ATTR, fname, buf);
96 exit(1);
97 }
98
99 #if _S_IFLNK != 0120000
100 if ((mode & (_S_IFMT)) == 0120000)
101 mode = (mode & ~(_S_IFMT)) | _S_IFLNK;
102 #endif
103 fst->st_mode = mode;
104
105 fst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
106 fst->st_uid = uid;
107 fst->st_gid = gid;
108
109 return 0;
110 }
111
112 #endif
113
114 static int display_atimes = 0;
115 #ifdef SUPPORT_CRTIMES
116 static int display_crtimes = 0;
117 #endif
118
119 static void failed(char const *what, char const *where)
120 {
121 fprintf(stderr, PROGRAM ": %s %s: %s\n",
122 what, where, strerror(errno));
123 exit(1);
124 }
125
126 static void storetime(char *dest, size_t destsize, time_t t, int nsecs)
127 {
128 if (t) {
129 int len;
130 struct tm tmp, *mt = gmtime_r(&t, &tmp);
131
132 len = snprintf(dest, destsize,
133 " %04d-%02d-%02d %02d:%02d:%02d",
134 (int)mt->tm_year + 1900,
135 (int)mt->tm_mon + 1,
136 (int)mt->tm_mday,
137 (int)mt->tm_hour,
138 (int)mt->tm_min,
139 (int)mt->tm_sec);
140 if (nsecs >= 0 && len >= 0)
141 snprintf(dest + len, destsize - len, ".%09d", nsecs);
142 } else {
143 int has_nsecs = nsecs >= 0 ? 1 : 0;
144 int len = MIN(20 + 10*has_nsecs, (int)destsize - 1);
145 memset(dest, ' ', len);
146 dest[len] = '\0';
147 }
148 }
149
150 static void list_file(const char *fname)
151 {
152 STRUCT_STAT buf;
153 #ifdef SUPPORT_CRTIMES
154 time_t crtime = 0;
155 #endif
156 char permbuf[PERMSTRING_SIZE];
157 char mtimebuf[50];
158 char atimebuf[50];
159 char crtimebuf[50];
160 char linkbuf[4096];
161 int nsecs;
162
163 if (do_lstat(fname, &buf) < 0)
164 failed("stat", fname);
165 #ifdef SUPPORT_CRTIMES
166 if (display_crtimes && (crtime = get_create_time(fname, &buf)) == 0)
167 failed("get_create_time", fname);
168 #endif
169 #ifdef SUPPORT_XATTRS
170 if (am_root < 0)
171 stat_xattr(fname, &buf);
172 #endif
173
174 /* The size of anything but a regular file is probably not
175 * worth thinking about. */
176 if (!S_ISREG(buf.st_mode))
177 buf.st_size = 0;
178
179 /* On some BSD platforms the mode bits of a symlink are
180 * undefined. Also it tends not to be possible to reset a
181 * symlink's mtime, so we default to ignoring it too. */
182 if (S_ISLNK(buf.st_mode)) {
183 int len;
184 buf.st_mode &= ~0777;
185 if (!link_times)
186 buf.st_mtime = (time_t)0;
187 if (!link_owner)
188 buf.st_uid = buf.st_gid = 0;
189 strlcpy(linkbuf, " -> ", sizeof linkbuf);
190 /* const-cast required for silly UNICOS headers */
191 len = do_readlink((char*)fname, linkbuf+4, sizeof linkbuf - 4);
192 if (len == -1)
193 failed("do_readlink", fname);
194 else
195 /* it's not nul-terminated */
196 linkbuf[4+len] = 0;
197 } else {
198 linkbuf[0] = '\0';
199 }
200
201 permstring(permbuf, buf.st_mode);
202 #ifdef ST_MTIME_NSEC
203 if (nsec_times)
204 nsecs = (int)buf.ST_MTIME_NSEC;
205 else
206 #endif
207 nsecs = -1;
208 storetime(mtimebuf, sizeof mtimebuf, buf.st_mtime, nsecs);
209 if (display_atimes)
210 storetime(atimebuf, sizeof atimebuf, S_ISDIR(buf.st_mode) ? 0 : buf.st_atime, -1);
211 else
212 atimebuf[0] = '\0';
213 #ifdef SUPPORT_CRTIMES
214 if (display_crtimes)
215 storetime(crtimebuf, sizeof crtimebuf, crtime, -1);
216 else
217 #endif
218 crtimebuf[0] = '\0';
219
220 /* TODO: Perhaps escape special characters in fname? */
221 printf("%s ", permbuf);
222
223 if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) {
224 printf("%5ld,%6ld", (long)major(buf.st_rdev), (long)minor(buf.st_rdev));
225 } else
226 printf("%15s", do_big_num(buf.st_size, 1, NULL));
227
228 printf(" %6ld.%-6ld %6ld%s%s%s %s%s\n",
229 (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
230 mtimebuf, atimebuf, crtimebuf, fname, linkbuf);
231 }
232
233 static const struct poptOption long_options[] = {
234 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
235 {"atimes", 'U', POPT_ARG_NONE, &display_atimes, 0, 0, 0},
236 #ifdef SUPPORT_CRTIMES
237 {"crtimes", 'N', POPT_ARG_NONE, &display_crtimes, 0, 0, 0},
238 #endif
239 {"link-times", 'l', POPT_ARG_NONE, &link_times, 0, 0, 0 },
240 {"link-owner", 'L', POPT_ARG_NONE, &link_owner, 0, 0, 0 },
241 #ifdef SUPPORT_XATTRS
242 {"fake-super", 'f', POPT_ARG_VAL, &am_root, -1, 0, 0 },
243 #endif
244 #ifdef ST_MTIME_NSEC
245 {"nsec", 's', POPT_ARG_NONE, &nsec_times, 0, 0, 0 },
246 #endif
247 {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 },
248 {0,0,0,0,0,0,0}
249 };
250
251 static void NORETURN tls_usage(int ret)
252 {
253 FILE *F = ret ? stderr : stdout;
254 fprintf(F,"usage: " PROGRAM " [OPTIONS] FILE ...\n");
255 fprintf(F,"Trivial file listing program for portably checking rsync\n");
256 fprintf(F,"\nOptions:\n");
257 fprintf(F," -U, --atimes display access (last-used) times\n");
258 #ifdef SUPPORT_CRTIMES
259 fprintf(F," -N, --crtimes display create times (newness)\n");
260 #endif
261 fprintf(F," -l, --link-times display the time on a symlink\n");
262 fprintf(F," -L, --link-owner display the owner+group on a symlink\n");
263 #ifdef SUPPORT_XATTRS
264 fprintf(F," -f, --fake-super display attributes including fake-super xattrs\n");
265 #endif
266 fprintf(F," -h, --help show this help\n");
267 exit(ret);
268 }
269
270 int
271 main(int argc, char *argv[])
272 {
273 poptContext pc;
274 const char **extra_args;
275 int opt;
276
277 pc = poptGetContext(PROGRAM, argc, (const char **)argv, long_options, 0);
278 while ((opt = poptGetNextOpt(pc)) != -1) {
279 switch (opt) {
280 case 'h':
281 tls_usage(0);
282 default:
283 fprintf(stderr, "%s: %s\n",
284 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
285 poptStrerror(opt));
286 tls_usage(1);
287 }
288 }
289
290 extra_args = poptGetArgs(pc);
291 if (!extra_args || *extra_args == NULL)
292 tls_usage(1);
293
294 for (; *extra_args; extra_args++)
295 list_file(*extra_args);
296 poptFreeContext(pc);
297
298 return 0;
299 }