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