Simplify the setting of rdev & rdev_high in send_file_entry().
[rsync/rsync.git] / tls.c
CommitLineData
f22ee865
MP
1/* -*- c-file-style: "linux" -*-
2 *
ea1438da 3 * Copyright (C) 2001, 2002 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/**
ac13ad10 20 * @file tls.c
f22ee865 21 *
ac13ad10 22 * Trivial @c ls for comparing two directories after running an rsync.
f22ee865
MP
23 *
24 * The problem with using the system's own ls is that some features
25 * have little quirks that make directories look different when for
26 * our purposes they're the same -- for example, the BSD braindamage
27 * about setting the mode on symlinks based on your current umask.
28 *
4ed886ae
MP
29 * All the filenames must be given on the command line -- tls does not
30 * even read directories, let alone recurse. The typical usage is
31 * "find|sort|xargs tls".
f22ee865 32 *
4ed886ae 33 * The format is not exactly the same as any particular Unix ls(1).
f22ee865 34 *
4ed886ae
MP
35 * A key requirement for this program is that the output be "very
36 * reproducible." So we mask away information that can accidentally
37 * change.
38 **/
f22ee865
MP
39
40
f22ee865
MP
41#include "rsync.h"
42
43#define PROGRAM "tls"
44
45/* These are to make syscall.o shut up. */
46int dry_run = 0;
47int read_only = 1;
48int list_only = 0;
522c05cf 49int preserve_perms = 0;
f22ee865
MP
50
51
52static void failed (char const *what,
53 char const *where)
54{
55 fprintf (stderr, PROGRAM ": %s %s: %s\n",
56 what, where, strerror (errno));
57 exit (1);
58}
59
60
61
4ed886ae 62static void list_file (const char *fname)
f22ee865 63{
d0f821ad 64 STRUCT_STAT buf;
4ed886ae
MP
65 char permbuf[PERMSTRING_SIZE];
66 struct tm *mt;
dd0700b0 67 char datebuf[50];
1336e414 68 char linkbuf[4096];
4ed886ae
MP
69
70 if (do_lstat(fname, &buf) == -1)
71 failed ("stat", fname);
72
2d4c8e59
MP
73 /* The size of anything but a regular file is probably not
74 * worth thinking about. */
75 if (!S_ISREG(buf.st_mode))
4ed886ae 76 buf.st_size = 0;
f22ee865 77
dd0700b0
MP
78 /* On some BSD platforms the mode bits of a symlink are
79 * undefined. Also it tends not to be possible to reset a
80 * symlink's mtime, so we have to ignore it too. */
81 if (S_ISLNK(buf.st_mode)) {
0771727d 82 int len;
dd0700b0
MP
83 buf.st_mode &= ~0777;
84 buf.st_mtime = (time_t)0;
85 buf.st_uid = buf.st_gid = 0;
1336e414 86 strcpy(linkbuf, " -> ");
acf1af0c 87 /* const-cast required for silly UNICOS headers */
0771727d
MP
88 len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4);
89 if (len == -1)
90 failed("readlink", fname);
91 else
92 /* it's not nul-terminated */
93 linkbuf[4+len] = 0;
1336e414
MP
94 } else {
95 linkbuf[0] = 0;
dd0700b0 96 }
f22ee865 97
dd0700b0 98 permstring(permbuf, buf.st_mode);
4ed886ae 99
dd0700b0
MP
100 if (buf.st_mtime) {
101 mt = gmtime(&buf.st_mtime);
102
103 sprintf(datebuf, "%04d-%02d-%02d %02d:%02d:%02d",
104 mt->tm_year + 1900,
105 mt->tm_mon + 1,
106 mt->tm_mday,
107 mt->tm_hour,
108 mt->tm_min,
109 mt->tm_sec);
110 } else {
111 strcpy(datebuf, " ");
112 }
113
4ed886ae 114 /* TODO: Perhaps escape special characters in fname? */
dd0700b0
MP
115
116
4ed886ae
MP
117 /* NB: need to pass size as a double because it might be be
118 * too large for a long. */
f358487f
WD
119 printf("%s %12.0f %6ld.%-6ld %6ld %s %s%s\n",
120 permbuf, (double)buf.st_size, (long)buf.st_uid,
121 (long)buf.st_gid, (long)buf.st_nlink,
1336e414 122 datebuf, fname, linkbuf);
f22ee865
MP
123}
124
125
f69204ad
WD
126int
127main(int argc, char *argv[])
f22ee865
MP
128{
129 if (argc < 2) {
130 fprintf (stderr, "usage: " PROGRAM " DIR ...\n"
131 "Trivial file listing program for portably checking rsync\n");
132 return 1;
133 }
134
135 for (argv++; *argv; argv++) {
4ed886ae 136 list_file (*argv);
f22ee865
MP
137 }
138
139 return 0;
140}