Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

tls.c

Go to the documentation of this file.
00001 /* -*- c-file-style: "linux" -*-
00002  *
00003  * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License version
00007  * 2 as published by the Free Software Foundation.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 /**
00020  * @file tls.c
00021  *
00022  * Trivial @c ls for comparing two directories after running an rsync.
00023  *
00024  * The problem with using the system's own ls is that some features
00025  * have little quirks that make directories look different when for
00026  * our purposes they're the same -- for example, the BSD braindamage
00027  * about setting the mode on symlinks based on your current umask.
00028  *
00029  * All the filenames must be given on the command line -- tls does not
00030  * even read directories, let alone recurse.  The typical usage is
00031  * "find|sort|xargs tls".
00032  *
00033  * The format is not exactly the same as any particular Unix ls(1).
00034  *
00035  * A key requirement for this program is that the output be "very
00036  * reproducible."  So we mask away information that can accidentally
00037  * change.
00038  **/
00039 
00040 
00041 #include "rsync.h"
00042 
00043 #define PROGRAM "tls"
00044 
00045 /* These are to make syscall.o shut up. */
00046 int dry_run = 0;
00047 int read_only = 1;
00048 int list_only = 0;
00049 
00050 
00051 static void failed (char const *what,
00052                     char const *where)
00053 {
00054         fprintf (stderr, PROGRAM ": %s %s: %s\n",
00055                  what, where, strerror (errno));
00056         exit (1);
00057 }
00058 
00059 
00060 
00061 static void list_file (const char *fname)
00062 {
00063         STRUCT_STAT buf;
00064         char permbuf[PERMSTRING_SIZE];
00065         struct tm *mt;
00066         char datebuf[50];
00067         char linkbuf[4096];
00068 
00069         if (do_lstat(fname, &buf) == -1)
00070                 failed ("stat", fname);
00071 
00072         /* The size of anything but a regular file is probably not
00073          * worth thinking about. */
00074         if (!S_ISREG(buf.st_mode))
00075                 buf.st_size = 0;
00076 
00077         /* On some BSD platforms the mode bits of a symlink are
00078          * undefined.  Also it tends not to be possible to reset a
00079          * symlink's mtime, so we have to ignore it too. */
00080         if (S_ISLNK(buf.st_mode)) {
00081                 int len;
00082                 buf.st_mode &= ~0777;
00083                 buf.st_mtime = (time_t)0;
00084                 buf.st_uid = buf.st_gid = 0;
00085                 strcpy(linkbuf, " -> ");
00086                 /* const-cast required for silly UNICOS headers */
00087                 len = readlink((char *) fname, linkbuf+4, sizeof(linkbuf) - 4);
00088                 if (len == -1) 
00089                         failed("readlink", fname);
00090                 else
00091                         /* it's not nul-terminated */
00092                         linkbuf[4+len] = 0;
00093         } else {
00094                 linkbuf[0] = 0;
00095         }
00096 
00097         permstring(permbuf, buf.st_mode);
00098 
00099         if (buf.st_mtime) {
00100                 mt = gmtime(&buf.st_mtime);
00101                 
00102                 sprintf(datebuf, "%04d-%02d-%02d %02d:%02d:%02d",
00103                         mt->tm_year + 1900,
00104                         mt->tm_mon + 1,
00105                         mt->tm_mday,
00106                         mt->tm_hour,
00107                         mt->tm_min,
00108                         mt->tm_sec);
00109         } else {
00110                 strcpy(datebuf, "                   ");
00111         }
00112         
00113         /* TODO: Perhaps escape special characters in fname? */
00114         
00115         
00116         /* NB: need to pass size as a double because it might be be
00117          * too large for a long. */
00118         printf("%s %12.0f %6ld.%-6ld %6d %s %s%s\n",
00119                permbuf, (double) buf.st_size,
00120                (long) buf.st_uid, (long) buf.st_gid,
00121                buf.st_nlink,
00122                datebuf, fname, linkbuf);
00123 }
00124 
00125 
00126 int main (int argc, char *argv[])
00127 {
00128         if (argc < 2) {
00129                 fprintf (stderr, "usage: " PROGRAM " DIR ...\n"
00130                          "Trivial file listing program for portably checking rsync\n");
00131                 return 1;
00132         }
00133 
00134         for (argv++; *argv; argv++) {
00135                 list_file (*argv);
00136         }
00137 
00138         return 0;
00139 }

Generated on Tue Apr 16 12:37:37 2002 for rsync by doxygen1.2.15