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