Tweaked rsync.spec to use "rsync" in place of "%{name}" in a few
[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-2008 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. */
44int dry_run = 0;
45int am_root = 0;
46int read_only = 1;
47int list_only = 0;
48int preserve_perms = 0;
49int preserve_executability = 0;
50
51#ifdef SUPPORT_XATTRS
52
53#ifdef HAVE_LINUX_XATTRS
54#define XSTAT_ATTR "user.rsync.%stat"
55#else
56#define XSTAT_ATTR "rsync.%stat"
57#endif
58
59static int stat_xattr(const char *fname, STRUCT_STAT *fst)
60{
61 int mode, rdev_major, rdev_minor, uid, gid, len;
62 char buf[256];
63
64 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
65 return -1;
66
67 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
68 if (len >= (int)sizeof buf) {
69 len = -1;
70 errno = ERANGE;
71 }
72 if (len < 0) {
73 if (errno == ENOTSUP || errno == ENOATTR)
74 return -1;
75 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
76 fst->st_uid = 0;
77 fst->st_gid = 0;
78 return 0;
79 }
80 fprintf(stderr, "failed to read xattr %s for %s: %s\n",
81 XSTAT_ATTR, fname, strerror(errno));
82 return -1;
83 }
84 buf[len] = '\0';
85
86 if (sscanf(buf, "%o %d,%d %d:%d",
87 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
88 fprintf(stderr, "Corrupt %s xattr attached to %s: \"%s\"\n",
89 XSTAT_ATTR, fname, buf);
90 exit(1);
91 }
92
93#if _S_IFLNK != 0120000
94 if ((mode & (_S_IFMT)) == 0120000)
95 mode = (mode & ~(_S_IFMT)) | _S_IFLNK;
96#endif
97 fst->st_mode = mode;
98
99 fst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
100 fst->st_uid = uid;
101 fst->st_gid = gid;
102
103 return 0;
104}
105
106#endif
107
108static void failed(char const *what, char const *where)
109{
110 fprintf(stderr, PROGRAM ": %s %s: %s\n",
111 what, where, strerror(errno));
112 exit(1);
113}
114
115static void list_file(const char *fname)
116{
117 STRUCT_STAT buf;
118 char permbuf[PERMSTRING_SIZE];
119 struct tm *mt;
120 char datebuf[50];
121 char linkbuf[4096];
122
123 if (do_lstat(fname, &buf) < 0)
124 failed("stat", fname);
125#ifdef SUPPORT_XATTRS
126 if (am_root < 0)
127 stat_xattr(fname, &buf);
128#endif
129
130 /* The size of anything but a regular file is probably not
131 * worth thinking about. */
132 if (!S_ISREG(buf.st_mode))
133 buf.st_size = 0;
134
135 /* On some BSD platforms the mode bits of a symlink are
136 * undefined. Also it tends not to be possible to reset a
137 * symlink's mtime, so we have to ignore it too. */
138 if (S_ISLNK(buf.st_mode)) {
139 int len;
140 buf.st_mode &= ~0777;
141 buf.st_mtime = (time_t)0;
142 buf.st_uid = buf.st_gid = 0;
143 strlcpy(linkbuf, " -> ", sizeof linkbuf);
144 /* const-cast required for silly UNICOS headers */
145 len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4);
146 if (len == -1)
147 failed("readlink", fname);
148 else
149 /* it's not nul-terminated */
150 linkbuf[4+len] = 0;
151 } else {
152 linkbuf[0] = 0;
153 }
154
155 permstring(permbuf, buf.st_mode);
156
157 if (buf.st_mtime) {
158 mt = gmtime(&buf.st_mtime);
159
160 snprintf(datebuf, sizeof datebuf,
161 "%04d-%02d-%02d %02d:%02d:%02d",
162 (int)mt->tm_year + 1900,
163 (int)mt->tm_mon + 1,
164 (int)mt->tm_mday,
165 (int)mt->tm_hour,
166 (int)mt->tm_min,
167 (int)mt->tm_sec);
168 } else
169 strlcpy(datebuf, " ", sizeof datebuf);
170
171 /* TODO: Perhaps escape special characters in fname? */
172
173 printf("%s ", permbuf);
174 if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) {
175 printf("%5ld,%6ld",
176 (long)major(buf.st_rdev),
177 (long)minor(buf.st_rdev));
178 } else /* NB: use double for size since it might not fit in a long. */
179 printf("%12.0f", (double)buf.st_size);
180 printf(" %6ld.%-6ld %6ld %s %s%s\n",
181 (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
182 datebuf, fname, linkbuf);
183}
184
185static struct poptOption long_options[] = {
186 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
187#ifdef SUPPORT_XATTRS
188 {"fake-super", 'f', POPT_ARG_VAL, &am_root, -1, 0, 0 },
189#endif
190 {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 },
191 {0,0,0,0,0,0,0}
192};
193
194static void tls_usage(int ret)
195{
196 FILE *F = ret ? stderr : stdout;
197 fprintf(F,"usage: " PROGRAM " [OPTIONS] FILE ...\n");
198 fprintf(F,"Trivial file listing program for portably checking rsync\n");
199 fprintf(F,"\nOptions:\n");
200#ifdef SUPPORT_XATTRS
201 fprintf(F," -f, --fake-super display attributes including fake-super xattrs\n");
202#endif
203 fprintf(F," -h, --help show this help\n");
204 exit(ret);
205}
206
207int
208main(int argc, char *argv[])
209{
210 poptContext pc;
211 const char **extra_args;
212 int opt;
213
214 pc = poptGetContext(PROGRAM, argc, (const char **)argv,
215 long_options, 0);
216 while ((opt = poptGetNextOpt(pc)) != -1) {
217 switch (opt) {
218 case 'h':
219 tls_usage(0);
220 default:
221 fprintf(stderr,
222 "%s: %s\n",
223 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
224 poptStrerror(opt));
225 tls_usage(1);
226 }
227 }
228
229 extra_args = poptGetArgs(pc);
230 if (!extra_args || *extra_args == NULL)
231 tls_usage(1);
232
233 for (; *extra_args; extra_args++)
234 list_file(*extra_args);
235 poptFreeContext(pc);
236
237 return 0;
238}