A little more popt tweaking.
[rsync/rsync.git] / tls.c
... / ...
CommitLineData
1/*
2 * Trivial ls for comparing two directories after running an rsync.
3 *
4 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
5 * Copyright (C) 2003-2007 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 version 2 as
9 * published by the Free Software Foundation.
10 *
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.
15 *
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.
19 */
20
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.
25 *
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".
29 *
30 * The format is not exactly the same as any particular Unix ls(1).
31 *
32 * A key requirement for this program is that the output be "very
33 * reproducible." So we mask away information that can accidentally
34 * change. */
35
36#include "rsync.h"
37#include "popt.h"
38#include "lib/sysxattrs.h"
39
40#define PROGRAM "tls"
41
42/* These are to make syscall.o shut up. */
43int dry_run = 0;
44int am_root = 0;
45int read_only = 1;
46int list_only = 0;
47int preserve_perms = 0;
48
49#ifdef HAVE_LINUX_XATTRS
50#define XSTAT_ATTR "user.rsync.%stat"
51#else
52#define XSTAT_ATTR "rsync.%stat"
53#endif
54
55static int stat_xattr(const char *fname, STRUCT_STAT *fst)
56{
57 int mode, rdev_major, rdev_minor, uid, gid, len;
58 char buf[256];
59
60 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
61 return -1;
62
63 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
64 if (len >= (int)sizeof buf) {
65 len = -1;
66 errno = ERANGE;
67 }
68 if (len < 0) {
69 if (errno == ENOTSUP || errno == ENOATTR)
70 return -1;
71 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
72 fst->st_uid = 0;
73 fst->st_gid = 0;
74 return 0;
75 }
76 fprintf(stderr, "failed to read xattr %s for %s: %s\n",
77 XSTAT_ATTR, fname, strerror(errno));
78 return -1;
79 }
80 buf[len] = '\0';
81
82 if (sscanf(buf, "%o %d,%d %d:%d",
83 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
84 fprintf(stderr, "Corrupt %s xattr attached to %s: \"%s\"\n",
85 XSTAT_ATTR, fname, buf);
86 exit(1);
87 }
88
89 fst->st_mode = from_wire_mode(mode);
90 fst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
91 fst->st_uid = uid;
92 fst->st_gid = gid;
93
94 return 0;
95}
96
97static void failed(char const *what, char const *where)
98{
99 fprintf(stderr, PROGRAM ": %s %s: %s\n",
100 what, where, strerror(errno));
101 exit(1);
102}
103
104static void list_file(const char *fname)
105{
106 STRUCT_STAT buf;
107 char permbuf[PERMSTRING_SIZE];
108 struct tm *mt;
109 char datebuf[50];
110 char linkbuf[4096];
111
112 if (do_lstat(fname, &buf) < 0)
113 failed("stat", fname);
114 if (am_root < 0)
115 stat_xattr(fname, &buf);
116
117 /* The size of anything but a regular file is probably not
118 * worth thinking about. */
119 if (!S_ISREG(buf.st_mode))
120 buf.st_size = 0;
121
122 /* On some BSD platforms the mode bits of a symlink are
123 * undefined. Also it tends not to be possible to reset a
124 * symlink's mtime, so we have to ignore it too. */
125 if (S_ISLNK(buf.st_mode)) {
126 int len;
127 buf.st_mode &= ~0777;
128 buf.st_mtime = (time_t)0;
129 buf.st_uid = buf.st_gid = 0;
130 strlcpy(linkbuf, " -> ", sizeof linkbuf);
131 /* const-cast required for silly UNICOS headers */
132 len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4);
133 if (len == -1)
134 failed("readlink", fname);
135 else
136 /* it's not nul-terminated */
137 linkbuf[4+len] = 0;
138 } else {
139 linkbuf[0] = 0;
140 }
141
142 permstring(permbuf, buf.st_mode);
143
144 if (buf.st_mtime) {
145 mt = gmtime(&buf.st_mtime);
146
147 snprintf(datebuf, sizeof datebuf,
148 "%04d-%02d-%02d %02d:%02d:%02d",
149 (int)mt->tm_year + 1900,
150 (int)mt->tm_mon + 1,
151 (int)mt->tm_mday,
152 (int)mt->tm_hour,
153 (int)mt->tm_min,
154 (int)mt->tm_sec);
155 } else
156 strlcpy(datebuf, " ", sizeof datebuf);
157
158 /* TODO: Perhaps escape special characters in fname? */
159
160 printf("%s ", permbuf);
161 if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) {
162 printf("%5ld,%6ld",
163 (long)major(buf.st_rdev),
164 (long)minor(buf.st_rdev));
165 } else /* NB: use double for size since it might not fit in a long. */
166 printf("%12.0f", (double)buf.st_size);
167 printf(" %6ld.%-6ld %6ld %s %s%s\n",
168 (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
169 datebuf, fname, linkbuf);
170}
171
172static struct poptOption long_options[] = {
173 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
174 {"fake-super", 'f', POPT_ARG_VAL, &am_root, -1, 0, 0 },
175 {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 },
176 {0,0,0,0,0,0,0}
177};
178
179static void tls_usage(int ret)
180{
181 FILE *F = ret ? stderr : stdout;
182 fprintf(F,"usage: " PROGRAM " [OPTIONS] FILE ...\n");
183 fprintf(F,"Trivial file listing program for portably checking rsync\n");
184 fprintf(F,"\nOptions:\n");
185 fprintf(F," -f, --fake-super display attributes including fake-super xattrs\n");
186 fprintf(F," -h, --help show this help\n");
187 exit(ret);
188}
189
190int
191main(int argc, char *argv[])
192{
193 poptContext pc;
194 const char **extra_args;
195 int opt;
196
197 pc = poptGetContext(PROGRAM, argc, (const char **)argv,
198 long_options, 0);
199 while ((opt = poptGetNextOpt(pc)) != -1) {
200 switch (opt) {
201 case 'h':
202 tls_usage(0);
203 default:
204 fprintf(stderr,
205 "%s: %s\n",
206 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
207 poptStrerror(opt));
208 tls_usage(1);
209 }
210 }
211
212 extra_args = poptGetArgs(pc);
213 if (!extra_args || *extra_args == NULL)
214 tls_usage(1);
215
216 for (; *extra_args; extra_args++)
217 list_file(*extra_args);
218 poptFreeContext(pc);
219
220 return 0;
221}