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