Commit | Line | Data |
---|---|---|
f22ee865 MP |
1 | /* -*- c-file-style: "linux" -*- |
2 | * | |
4ed886ae | 3 | * Copyright (C) 2001 by Martin Pool <mbp@samba.org> |
f22ee865 MP |
4 | * |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License version | |
7 | * 2 as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | */ | |
18 | ||
19 | /** | |
20 | * \section tls | |
21 | * | |
22 | * tls -- Trivial recursive ls, for comparing two directories after | |
23 | * running an rsync. | |
24 | * | |
25 | * The problem with using the system's own ls is that some features | |
26 | * have little quirks that make directories look different when for | |
27 | * our purposes they're the same -- for example, the BSD braindamage | |
28 | * about setting the mode on symlinks based on your current umask. | |
29 | * | |
4ed886ae MP |
30 | * All the filenames must be given on the command line -- tls does not |
31 | * even read directories, let alone recurse. The typical usage is | |
32 | * "find|sort|xargs tls". | |
f22ee865 | 33 | * |
4ed886ae | 34 | * The format is not exactly the same as any particular Unix ls(1). |
f22ee865 | 35 | * |
4ed886ae MP |
36 | * A key requirement for this program is that the output be "very |
37 | * reproducible." So we mask away information that can accidentally | |
38 | * change. | |
39 | **/ | |
f22ee865 MP |
40 | |
41 | ||
42 | ||
43 | #include "rsync.h" | |
44 | ||
45 | #define PROGRAM "tls" | |
46 | ||
47 | /* These are to make syscall.o shut up. */ | |
48 | int dry_run = 0; | |
49 | int read_only = 1; | |
50 | int list_only = 0; | |
51 | ||
52 | ||
53 | static void failed (char const *what, | |
54 | char const *where) | |
55 | { | |
56 | fprintf (stderr, PROGRAM ": %s %s: %s\n", | |
57 | what, where, strerror (errno)); | |
58 | exit (1); | |
59 | } | |
60 | ||
61 | ||
62 | ||
4ed886ae | 63 | static void list_file (const char *fname) |
f22ee865 | 64 | { |
4ed886ae MP |
65 | struct stat buf; |
66 | char permbuf[PERMSTRING_SIZE]; | |
67 | struct tm *mt; | |
dd0700b0 | 68 | char datebuf[50]; |
1336e414 | 69 | char linkbuf[4096]; |
4ed886ae MP |
70 | |
71 | if (do_lstat(fname, &buf) == -1) | |
72 | failed ("stat", fname); | |
73 | ||
2d4c8e59 MP |
74 | /* The size of anything but a regular file is probably not |
75 | * worth thinking about. */ | |
76 | if (!S_ISREG(buf.st_mode)) | |
4ed886ae | 77 | buf.st_size = 0; |
f22ee865 | 78 | |
dd0700b0 MP |
79 | /* On some BSD platforms the mode bits of a symlink are |
80 | * undefined. Also it tends not to be possible to reset a | |
81 | * symlink's mtime, so we have to ignore it too. */ | |
82 | if (S_ISLNK(buf.st_mode)) { | |
83 | buf.st_mode &= ~0777; | |
84 | buf.st_mtime = (time_t)0; | |
85 | buf.st_uid = buf.st_gid = 0; | |
1336e414 MP |
86 | strcpy(linkbuf, " -> "); |
87 | readlink(fname, linkbuf+4, sizeof(linkbuf) - 4); | |
88 | } else { | |
89 | linkbuf[0] = 0; | |
dd0700b0 | 90 | } |
f22ee865 | 91 | |
dd0700b0 | 92 | permstring(permbuf, buf.st_mode); |
4ed886ae | 93 | |
dd0700b0 MP |
94 | if (buf.st_mtime) { |
95 | mt = gmtime(&buf.st_mtime); | |
96 | ||
97 | sprintf(datebuf, "%04d-%02d-%02d %02d:%02d:%02d", | |
98 | mt->tm_year + 1900, | |
99 | mt->tm_mon + 1, | |
100 | mt->tm_mday, | |
101 | mt->tm_hour, | |
102 | mt->tm_min, | |
103 | mt->tm_sec); | |
104 | } else { | |
105 | strcpy(datebuf, " "); | |
106 | } | |
107 | ||
4ed886ae | 108 | /* TODO: Perhaps escape special characters in fname? */ |
dd0700b0 MP |
109 | |
110 | ||
4ed886ae MP |
111 | /* NB: need to pass size as a double because it might be be |
112 | * too large for a long. */ | |
1336e414 | 113 | printf("%s %12.0f %6d.%-6d %s %s%s\n", |
4ed886ae MP |
114 | permbuf, (double) buf.st_size, |
115 | buf.st_uid, buf.st_gid, | |
1336e414 | 116 | datebuf, fname, linkbuf); |
f22ee865 MP |
117 | } |
118 | ||
119 | ||
120 | int main (int argc, char *argv[]) | |
121 | { | |
122 | if (argc < 2) { | |
123 | fprintf (stderr, "usage: " PROGRAM " DIR ...\n" | |
124 | "Trivial file listing program for portably checking rsync\n"); | |
125 | return 1; | |
126 | } | |
127 | ||
128 | for (argv++; *argv; argv++) { | |
4ed886ae | 129 | list_file (*argv); |
f22ee865 MP |
130 | } |
131 | ||
132 | return 0; | |
133 | } |