Commit | Line | Data |
---|---|---|
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 WD |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
f26ac1e8 | 10 | * |
f22ee865 MP |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
f26ac1e8 | 15 | * |
e7c67065 WD |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
f22ee865 MP |
19 | */ |
20 | ||
0f78b815 | 21 | /* The problem with using the system's own ls is that some features |
f22ee865 MP |
22 | * have little quirks that make directories look different when for |
23 | * our purposes they're the same -- for example, the BSD braindamage | |
24 | * about setting the mode on symlinks based on your current umask. | |
25 | * | |
4ed886ae MP |
26 | * All the filenames must be given on the command line -- tls does not |
27 | * even read directories, let alone recurse. The typical usage is | |
28 | * "find|sort|xargs tls". | |
f22ee865 | 29 | * |
4ed886ae | 30 | * The format is not exactly the same as any particular Unix ls(1). |
f22ee865 | 31 | * |
4ed886ae MP |
32 | * A key requirement for this program is that the output be "very |
33 | * reproducible." So we mask away information that can accidentally | |
0f78b815 | 34 | * change. */ |
f22ee865 | 35 | |
f22ee865 | 36 | #include "rsync.h" |
db9c9e27 | 37 | #include "popt.h" |
9439c0cb | 38 | #include "lib/sysxattrs.h" |
f22ee865 MP |
39 | |
40 | #define PROGRAM "tls" | |
41 | ||
42 | /* These are to make syscall.o shut up. */ | |
43 | int dry_run = 0; | |
9439c0cb | 44 | int am_root = 0; |
f22ee865 MP |
45 | int read_only = 1; |
46 | int list_only = 0; | |
522c05cf | 47 | int preserve_perms = 0; |
f22ee865 | 48 | |
acac1f5c WD |
49 | #ifdef SUPPORT_XATTRS |
50 | ||
9439c0cb WD |
51 | #ifdef HAVE_LINUX_XATTRS |
52 | #define XSTAT_ATTR "user.rsync.%stat" | |
53 | #else | |
54 | #define XSTAT_ATTR "rsync.%stat" | |
55 | #endif | |
56 | ||
57 | static int stat_xattr(const char *fname, STRUCT_STAT *fst) | |
58 | { | |
59 | int mode, rdev_major, rdev_minor, uid, gid, len; | |
60 | char buf[256]; | |
61 | ||
62 | if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode)) | |
63 | return -1; | |
64 | ||
65 | len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1); | |
66 | if (len >= (int)sizeof buf) { | |
67 | len = -1; | |
68 | errno = ERANGE; | |
69 | } | |
70 | if (len < 0) { | |
71 | if (errno == ENOTSUP || errno == ENOATTR) | |
72 | return -1; | |
73 | if (errno == EPERM && S_ISLNK(fst->st_mode)) { | |
74 | fst->st_uid = 0; | |
75 | fst->st_gid = 0; | |
76 | return 0; | |
77 | } | |
78 | fprintf(stderr, "failed to read xattr %s for %s: %s\n", | |
79 | XSTAT_ATTR, fname, strerror(errno)); | |
80 | return -1; | |
81 | } | |
82 | buf[len] = '\0'; | |
83 | ||
84 | if (sscanf(buf, "%o %d,%d %d:%d", | |
85 | &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) { | |
86 | fprintf(stderr, "Corrupt %s xattr attached to %s: \"%s\"\n", | |
87 | XSTAT_ATTR, fname, buf); | |
88 | exit(1); | |
89 | } | |
90 | ||
91 | fst->st_mode = from_wire_mode(mode); | |
92 | fst->st_rdev = MAKEDEV(rdev_major, rdev_minor); | |
93 | fst->st_uid = uid; | |
94 | fst->st_gid = gid; | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
acac1f5c WD |
99 | #endif |
100 | ||
630e3c40 | 101 | static void failed(char const *what, char const *where) |
f22ee865 | 102 | { |
630e3c40 WD |
103 | fprintf(stderr, PROGRAM ": %s %s: %s\n", |
104 | what, where, strerror(errno)); | |
105 | exit(1); | |
f22ee865 MP |
106 | } |
107 | ||
630e3c40 | 108 | static void list_file(const char *fname) |
f22ee865 | 109 | { |
d0f821ad | 110 | STRUCT_STAT buf; |
4ed886ae MP |
111 | char permbuf[PERMSTRING_SIZE]; |
112 | struct tm *mt; | |
dd0700b0 | 113 | char datebuf[50]; |
1336e414 | 114 | char linkbuf[4096]; |
4ed886ae | 115 | |
018b2832 | 116 | if (do_lstat(fname, &buf) < 0) |
630e3c40 | 117 | failed("stat", fname); |
acac1f5c | 118 | #ifdef SUPPORT_XATTRS |
9439c0cb WD |
119 | if (am_root < 0) |
120 | stat_xattr(fname, &buf); | |
acac1f5c | 121 | #endif |
4ed886ae | 122 | |
2d4c8e59 MP |
123 | /* The size of anything but a regular file is probably not |
124 | * worth thinking about. */ | |
125 | if (!S_ISREG(buf.st_mode)) | |
4ed886ae | 126 | buf.st_size = 0; |
f22ee865 | 127 | |
dd0700b0 MP |
128 | /* On some BSD platforms the mode bits of a symlink are |
129 | * undefined. Also it tends not to be possible to reset a | |
130 | * symlink's mtime, so we have to ignore it too. */ | |
131 | if (S_ISLNK(buf.st_mode)) { | |
0771727d | 132 | int len; |
dd0700b0 MP |
133 | buf.st_mode &= ~0777; |
134 | buf.st_mtime = (time_t)0; | |
135 | buf.st_uid = buf.st_gid = 0; | |
c9bce0b8 | 136 | strlcpy(linkbuf, " -> ", sizeof linkbuf); |
acf1af0c | 137 | /* const-cast required for silly UNICOS headers */ |
0771727d | 138 | len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4); |
f26ac1e8 | 139 | if (len == -1) |
0771727d MP |
140 | failed("readlink", fname); |
141 | else | |
142 | /* it's not nul-terminated */ | |
143 | linkbuf[4+len] = 0; | |
1336e414 MP |
144 | } else { |
145 | linkbuf[0] = 0; | |
dd0700b0 | 146 | } |
f22ee865 | 147 | |
dd0700b0 | 148 | permstring(permbuf, buf.st_mode); |
4ed886ae | 149 | |
dd0700b0 MP |
150 | if (buf.st_mtime) { |
151 | mt = gmtime(&buf.st_mtime); | |
f26ac1e8 | 152 | |
185aa5b0 WD |
153 | snprintf(datebuf, sizeof datebuf, |
154 | "%04d-%02d-%02d %02d:%02d:%02d", | |
e31058d4 WD |
155 | (int)mt->tm_year + 1900, |
156 | (int)mt->tm_mon + 1, | |
157 | (int)mt->tm_mday, | |
158 | (int)mt->tm_hour, | |
159 | (int)mt->tm_min, | |
160 | (int)mt->tm_sec); | |
185aa5b0 | 161 | } else |
c9bce0b8 | 162 | strlcpy(datebuf, " ", sizeof datebuf); |
f26ac1e8 | 163 | |
4ed886ae | 164 | /* TODO: Perhaps escape special characters in fname? */ |
f26ac1e8 WD |
165 | |
166 | printf("%s ", permbuf); | |
48d3ff94 | 167 | if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) { |
0e1d98ae WD |
168 | printf("%5ld,%6ld", |
169 | (long)major(buf.st_rdev), | |
170 | (long)minor(buf.st_rdev)); | |
171 | } else /* NB: use double for size since it might not fit in a long. */ | |
f26ac1e8 WD |
172 | printf("%12.0f", (double)buf.st_size); |
173 | printf(" %6ld.%-6ld %6ld %s %s%s\n", | |
174 | (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink, | |
1336e414 | 175 | datebuf, fname, linkbuf); |
f22ee865 MP |
176 | } |
177 | ||
db9c9e27 WD |
178 | static struct poptOption long_options[] = { |
179 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
acac1f5c | 180 | #ifdef SUPPORT_XATTRS |
db9c9e27 | 181 | {"fake-super", 'f', POPT_ARG_VAL, &am_root, -1, 0, 0 }, |
acac1f5c | 182 | #endif |
db9c9e27 WD |
183 | {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 }, |
184 | {0,0,0,0,0,0,0} | |
185 | }; | |
186 | ||
187 | static void tls_usage(int ret) | |
188 | { | |
189 | FILE *F = ret ? stderr : stdout; | |
190 | fprintf(F,"usage: " PROGRAM " [OPTIONS] FILE ...\n"); | |
191 | fprintf(F,"Trivial file listing program for portably checking rsync\n"); | |
192 | fprintf(F,"\nOptions:\n"); | |
acac1f5c | 193 | #ifdef SUPPORT_XATTRS |
9a234269 | 194 | fprintf(F," -f, --fake-super display attributes including fake-super xattrs\n"); |
acac1f5c | 195 | #endif |
9a234269 | 196 | fprintf(F," -h, --help show this help\n"); |
db9c9e27 WD |
197 | exit(ret); |
198 | } | |
199 | ||
f69204ad WD |
200 | int |
201 | main(int argc, char *argv[]) | |
f22ee865 | 202 | { |
db9c9e27 WD |
203 | poptContext pc; |
204 | const char **extra_args; | |
205 | int opt; | |
206 | ||
207 | pc = poptGetContext(PROGRAM, argc, (const char **)argv, | |
208 | long_options, 0); | |
209 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
210 | switch (opt) { | |
211 | case 'h': | |
212 | tls_usage(0); | |
213 | default: | |
214 | fprintf(stderr, | |
215 | "%s: %s\n", | |
216 | poptBadOption(pc, POPT_BADOPTION_NOALIAS), | |
217 | poptStrerror(opt)); | |
218 | tls_usage(1); | |
219 | } | |
f22ee865 MP |
220 | } |
221 | ||
db9c9e27 WD |
222 | extra_args = poptGetArgs(pc); |
223 | if (!extra_args || *extra_args == NULL) | |
224 | tls_usage(1); | |
9439c0cb | 225 | |
db9c9e27 WD |
226 | for (; *extra_args; extra_args++) |
227 | list_file(*extra_args); | |
228 | poptFreeContext(pc); | |
f22ee865 MP |
229 | |
230 | return 0; | |
231 | } |