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