Factor out common logic of unchanged_attrs and itemize into report_ATTR
[rsync/rsync.git] / tls.c
CommitLineData
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>
b3bf9b9d 5 * Copyright (C) 2003-2009 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. */
44int dry_run = 0;
9439c0cb 45int am_root = 0;
6e310d38 46int am_sender = 1;
f22ee865
MP
47int read_only = 1;
48int list_only = 0;
5b385336
WD
49int link_times = 0;
50int link_owner = 0;
1a2e41af 51int nsec_times = 0;
522c05cf 52int preserve_perms = 0;
e35ad79b 53int preserve_executability = 0;
adc2476f 54char number_separator;
f22ee865 55
acac1f5c
WD
56#ifdef SUPPORT_XATTRS
57
9439c0cb
WD
58#ifdef HAVE_LINUX_XATTRS
59#define XSTAT_ATTR "user.rsync.%stat"
60#else
61#define XSTAT_ATTR "rsync.%stat"
62#endif
63
64static int stat_xattr(const char *fname, STRUCT_STAT *fst)
65{
66 int mode, rdev_major, rdev_minor, uid, gid, len;
67 char buf[256];
68
69 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
70 return -1;
71
72 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
73 if (len >= (int)sizeof buf) {
74 len = -1;
75 errno = ERANGE;
76 }
77 if (len < 0) {
78 if (errno == ENOTSUP || errno == ENOATTR)
79 return -1;
80 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
81 fst->st_uid = 0;
82 fst->st_gid = 0;
83 return 0;
84 }
85 fprintf(stderr, "failed to read xattr %s for %s: %s\n",
86 XSTAT_ATTR, fname, strerror(errno));
87 return -1;
88 }
89 buf[len] = '\0';
90
91 if (sscanf(buf, "%o %d,%d %d:%d",
92 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
93 fprintf(stderr, "Corrupt %s xattr attached to %s: \"%s\"\n",
94 XSTAT_ATTR, fname, buf);
95 exit(1);
96 }
97
1b42f628
WD
98#if _S_IFLNK != 0120000
99 if ((mode & (_S_IFMT)) == 0120000)
100 mode = (mode & ~(_S_IFMT)) | _S_IFLNK;
101#endif
102 fst->st_mode = mode;
103
9439c0cb
WD
104 fst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
105 fst->st_uid = uid;
106 fst->st_gid = gid;
107
108 return 0;
109}
110
acac1f5c
WD
111#endif
112
630e3c40 113static void failed(char const *what, char const *where)
f22ee865 114{
630e3c40
WD
115 fprintf(stderr, PROGRAM ": %s %s: %s\n",
116 what, where, strerror(errno));
117 exit(1);
f22ee865
MP
118}
119
630e3c40 120static void list_file(const char *fname)
f22ee865 121{
d0f821ad 122 STRUCT_STAT buf;
4ed886ae
MP
123 char permbuf[PERMSTRING_SIZE];
124 struct tm *mt;
dd0700b0 125 char datebuf[50];
1336e414 126 char linkbuf[4096];
4ed886ae 127
018b2832 128 if (do_lstat(fname, &buf) < 0)
630e3c40 129 failed("stat", fname);
acac1f5c 130#ifdef SUPPORT_XATTRS
9439c0cb
WD
131 if (am_root < 0)
132 stat_xattr(fname, &buf);
acac1f5c 133#endif
4ed886ae 134
2d4c8e59
MP
135 /* The size of anything but a regular file is probably not
136 * worth thinking about. */
137 if (!S_ISREG(buf.st_mode))
4ed886ae 138 buf.st_size = 0;
f22ee865 139
dd0700b0
MP
140 /* On some BSD platforms the mode bits of a symlink are
141 * undefined. Also it tends not to be possible to reset a
5b385336 142 * symlink's mtime, so we default to ignoring it too. */
dd0700b0 143 if (S_ISLNK(buf.st_mode)) {
0771727d 144 int len;
dd0700b0 145 buf.st_mode &= ~0777;
5b385336
WD
146 if (!link_times)
147 buf.st_mtime = (time_t)0;
148 if (!link_owner)
149 buf.st_uid = buf.st_gid = 0;
c9bce0b8 150 strlcpy(linkbuf, " -> ", sizeof linkbuf);
acf1af0c 151 /* const-cast required for silly UNICOS headers */
6e310d38 152 len = do_readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4);
f26ac1e8 153 if (len == -1)
6e310d38 154 failed("do_readlink", fname);
0771727d
MP
155 else
156 /* it's not nul-terminated */
157 linkbuf[4+len] = 0;
1336e414
MP
158 } else {
159 linkbuf[0] = 0;
dd0700b0 160 }
f22ee865 161
dd0700b0 162 permstring(permbuf, buf.st_mode);
4ed886ae 163
dd0700b0 164 if (buf.st_mtime) {
1a2e41af 165 int len;
dd0700b0 166 mt = gmtime(&buf.st_mtime);
f26ac1e8 167
1a2e41af 168 len = snprintf(datebuf, sizeof datebuf,
185aa5b0 169 "%04d-%02d-%02d %02d:%02d:%02d",
e31058d4
WD
170 (int)mt->tm_year + 1900,
171 (int)mt->tm_mon + 1,
172 (int)mt->tm_mday,
173 (int)mt->tm_hour,
174 (int)mt->tm_min,
175 (int)mt->tm_sec);
1a2e41af
WD
176#ifdef ST_MTIME_NSEC
177 if (nsec_times) {
178 snprintf(datebuf + len, sizeof datebuf - len,
179 ".%09d", (int)buf.ST_MTIME_NSEC);
180 }
181#endif
182 } else {
183 int len = MIN(19 + 9*nsec_times, (int)sizeof datebuf - 1);
184 memset(datebuf, ' ', len);
185 datebuf[len] = '\0';
186 }
f26ac1e8 187
4ed886ae 188 /* TODO: Perhaps escape special characters in fname? */
f26ac1e8
WD
189
190 printf("%s ", permbuf);
48d3ff94 191 if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) {
0e1d98ae
WD
192 printf("%5ld,%6ld",
193 (long)major(buf.st_rdev),
194 (long)minor(buf.st_rdev));
7f0db4fd 195 } else
adc2476f 196 printf("%15s", do_big_num(buf.st_size, 1, NULL));
f26ac1e8
WD
197 printf(" %6ld.%-6ld %6ld %s %s%s\n",
198 (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
1336e414 199 datebuf, fname, linkbuf);
f22ee865
MP
200}
201
db9c9e27
WD
202static struct poptOption long_options[] = {
203 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
5b385336
WD
204 {"link-times", 'l', POPT_ARG_NONE, &link_times, 0, 0, 0 },
205 {"link-owner", 'L', POPT_ARG_NONE, &link_owner, 0, 0, 0 },
acac1f5c 206#ifdef SUPPORT_XATTRS
db9c9e27 207 {"fake-super", 'f', POPT_ARG_VAL, &am_root, -1, 0, 0 },
1a2e41af
WD
208#endif
209#ifdef ST_MTIME_NSEC
210 {"nsec", 's', POPT_ARG_NONE, &nsec_times, 0, 0, 0 },
acac1f5c 211#endif
db9c9e27
WD
212 {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 },
213 {0,0,0,0,0,0,0}
214};
215
216static void tls_usage(int ret)
217{
218 FILE *F = ret ? stderr : stdout;
219 fprintf(F,"usage: " PROGRAM " [OPTIONS] FILE ...\n");
220 fprintf(F,"Trivial file listing program for portably checking rsync\n");
221 fprintf(F,"\nOptions:\n");
5b385336
WD
222 fprintf(F," -l, --link-times display the time on a symlink\n");
223 fprintf(F," -L, --link-owner display the owner+group on a symlink\n");
acac1f5c 224#ifdef SUPPORT_XATTRS
9a234269 225 fprintf(F," -f, --fake-super display attributes including fake-super xattrs\n");
acac1f5c 226#endif
9a234269 227 fprintf(F," -h, --help show this help\n");
db9c9e27
WD
228 exit(ret);
229}
230
f69204ad
WD
231int
232main(int argc, char *argv[])
f22ee865 233{
db9c9e27
WD
234 poptContext pc;
235 const char **extra_args;
adc2476f 236 char buf[32];
db9c9e27
WD
237 int opt;
238
239 pc = poptGetContext(PROGRAM, argc, (const char **)argv,
240 long_options, 0);
241 while ((opt = poptGetNextOpt(pc)) != -1) {
242 switch (opt) {
243 case 'h':
244 tls_usage(0);
245 default:
246 fprintf(stderr,
247 "%s: %s\n",
248 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
249 poptStrerror(opt));
250 tls_usage(1);
251 }
f22ee865
MP
252 }
253
db9c9e27
WD
254 extra_args = poptGetArgs(pc);
255 if (!extra_args || *extra_args == NULL)
256 tls_usage(1);
9439c0cb 257
adc2476f
WD
258 snprintf(buf, sizeof buf, "%f", 3.14);
259 if (strchr(buf, '.') != NULL)
260 number_separator = ',';
261 else
262 number_separator = '.';
263
db9c9e27
WD
264 for (; *extra_args; extra_args++)
265 list_file(*extra_args);
266 poptFreeContext(pc);
f22ee865
MP
267
268 return 0;
269}