Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Trivial ls for comparing two directories after running an rsync. | |
f22ee865 | 3 | * |
0f78b815 WD |
4 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
5 | * Copyright (C) 2003, 2004, 2005 Wayne Davison | |
f26ac1e8 | 6 | * |
f22ee865 MP |
7 | * This program is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as 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 MP |
36 | #include "rsync.h" |
37 | ||
38 | #define PROGRAM "tls" | |
39 | ||
40 | /* These are to make syscall.o shut up. */ | |
41 | int dry_run = 0; | |
42 | int read_only = 1; | |
43 | int list_only = 0; | |
522c05cf | 44 | int preserve_perms = 0; |
f22ee865 | 45 | |
630e3c40 | 46 | static void failed(char const *what, char const *where) |
f22ee865 | 47 | { |
630e3c40 WD |
48 | fprintf(stderr, PROGRAM ": %s %s: %s\n", |
49 | what, where, strerror(errno)); | |
50 | exit(1); | |
f22ee865 MP |
51 | } |
52 | ||
630e3c40 | 53 | static void list_file(const char *fname) |
f22ee865 | 54 | { |
d0f821ad | 55 | STRUCT_STAT buf; |
4ed886ae MP |
56 | char permbuf[PERMSTRING_SIZE]; |
57 | struct tm *mt; | |
dd0700b0 | 58 | char datebuf[50]; |
1336e414 | 59 | char linkbuf[4096]; |
4ed886ae | 60 | |
018b2832 | 61 | if (do_lstat(fname, &buf) < 0) |
630e3c40 | 62 | failed("stat", fname); |
4ed886ae | 63 | |
2d4c8e59 MP |
64 | /* The size of anything but a regular file is probably not |
65 | * worth thinking about. */ | |
66 | if (!S_ISREG(buf.st_mode)) | |
4ed886ae | 67 | buf.st_size = 0; |
f22ee865 | 68 | |
dd0700b0 MP |
69 | /* On some BSD platforms the mode bits of a symlink are |
70 | * undefined. Also it tends not to be possible to reset a | |
71 | * symlink's mtime, so we have to ignore it too. */ | |
72 | if (S_ISLNK(buf.st_mode)) { | |
0771727d | 73 | int len; |
dd0700b0 MP |
74 | buf.st_mode &= ~0777; |
75 | buf.st_mtime = (time_t)0; | |
76 | buf.st_uid = buf.st_gid = 0; | |
1336e414 | 77 | strcpy(linkbuf, " -> "); |
acf1af0c | 78 | /* const-cast required for silly UNICOS headers */ |
0771727d | 79 | len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4); |
f26ac1e8 | 80 | if (len == -1) |
0771727d MP |
81 | failed("readlink", fname); |
82 | else | |
83 | /* it's not nul-terminated */ | |
84 | linkbuf[4+len] = 0; | |
1336e414 MP |
85 | } else { |
86 | linkbuf[0] = 0; | |
dd0700b0 | 87 | } |
f22ee865 | 88 | |
dd0700b0 | 89 | permstring(permbuf, buf.st_mode); |
4ed886ae | 90 | |
dd0700b0 MP |
91 | if (buf.st_mtime) { |
92 | mt = gmtime(&buf.st_mtime); | |
f26ac1e8 | 93 | |
dd0700b0 | 94 | sprintf(datebuf, "%04d-%02d-%02d %02d:%02d:%02d", |
e31058d4 WD |
95 | (int)mt->tm_year + 1900, |
96 | (int)mt->tm_mon + 1, | |
97 | (int)mt->tm_mday, | |
98 | (int)mt->tm_hour, | |
99 | (int)mt->tm_min, | |
100 | (int)mt->tm_sec); | |
dd0700b0 MP |
101 | } else { |
102 | strcpy(datebuf, " "); | |
103 | } | |
f26ac1e8 | 104 | |
4ed886ae | 105 | /* TODO: Perhaps escape special characters in fname? */ |
f26ac1e8 WD |
106 | |
107 | printf("%s ", permbuf); | |
48d3ff94 | 108 | if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) { |
0e1d98ae WD |
109 | printf("%5ld,%6ld", |
110 | (long)major(buf.st_rdev), | |
111 | (long)minor(buf.st_rdev)); | |
112 | } else /* NB: use double for size since it might not fit in a long. */ | |
f26ac1e8 WD |
113 | printf("%12.0f", (double)buf.st_size); |
114 | printf(" %6ld.%-6ld %6ld %s %s%s\n", | |
115 | (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink, | |
1336e414 | 116 | datebuf, fname, linkbuf); |
f22ee865 MP |
117 | } |
118 | ||
f69204ad WD |
119 | int |
120 | main(int argc, char *argv[]) | |
f22ee865 MP |
121 | { |
122 | if (argc < 2) { | |
630e3c40 WD |
123 | fprintf(stderr, "usage: " PROGRAM " DIR ...\n" |
124 | "Trivial file listing program for portably checking rsync\n"); | |
f22ee865 MP |
125 | return 1; |
126 | } | |
127 | ||
128 | for (argv++; *argv; argv++) { | |
630e3c40 | 129 | list_file(*argv); |
f22ee865 MP |
130 | } |
131 | ||
132 | return 0; | |
133 | } |