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