3 * cat startpoints.list | summer >data.list
4 * summer startpoints... >data.list
5 * prints md5sum of data-list to stderr
12 #include <sys/types.h>
23 #include "nettle/md5-compat.h"
29 static int quiet=0, hidectime=0, hideatime=0, hidemtime=0;
30 static int hidedirsize=0, hidelinkmtime=0, onefilesystem=0;
31 static int filenamefieldsep=' ';
34 #define nodeflag_fsvalid 1u
36 static void malloc_fail(void) { perror("summer: alloc failed"); exit(12); }
38 static void *mmalloc(size_t sz) {
40 r= malloc(sz); if (!r) malloc_fail();
44 static void *mrealloc(void *p, size_t sz) {
46 r= realloc(p, sz); if (!r && sz) malloc_fail();
50 static void fn_escaped(FILE *f, const char *fn) {
53 if (c>=33 && c<=126 && c!='\\') putc(c,f);
54 else fprintf(f,"\\x%02x",(int)(unsigned char)c);
58 static void add_pr(int *pr, int printf_ret) {
59 if (printf_ret == EOF) return;
63 static void vproblemx(const char *path, int padto, int per,
64 const char *fmt, va_list al) {
67 if (errfile==stderr) fputs("summer: error: ",stderr);
68 else add_pr(&pr, fprintf(errfile,"\\["));
70 add_pr(&pr, vfprintf(errfile,fmt,al));
71 if (per) add_pr(&pr, fprintf(errfile,": %s",strerror(e)));
73 if (errfile==stderr) {
75 fn_escaped(stderr,path);
80 add_pr(&pr, printf("]"));
86 static void problem_e(const char *path, int padto, const char *fmt, ...) {
89 vproblemx(path,padto,1,fmt,al);
93 static void problem(const char *path, int padto, const char *fmt, ...) {
96 vproblemx(path,padto,0,fmt,al);
100 static void csum_file(const char *path) {
104 unsigned char digest[16];
109 if (!f) { problem_e(path,sizeof(digest)*2,"open"); return; }
113 r= fread(db,1,sizeof(db),f);
115 problem_e(path,sizeof(digest)*2,"read");
118 if (!r) { assert(feof(f)); break; }
121 MD5Final(digest,&mc);
122 if (fclose(f)) { problem_e(path,sizeof(digest)*2,"close"); return; }
124 for (i=0; i<sizeof(digest); i++)
125 printf("%02x", digest[i]);
128 static void csum_dev(int cb, const struct stat *stab) {
129 printf("%c 0x%08lx %3lu %3lu %3lu %3lu ", cb,
130 (unsigned long)stab->st_rdev,
131 ((unsigned long)stab->st_rdev & 0x0ff000000U) >> 24,
132 ((unsigned long)stab->st_rdev & 0x000ff0000U) >> 16,
133 ((unsigned long)stab->st_rdev & 0x00000ff00U) >> 8,
134 ((unsigned long)stab->st_rdev & 0x0000000ffU) >> 0);
137 static void csum_str(const char *s) {
138 printf("%-*s", CSUMXL, s);
141 static void linktargpath(const char *linktarg) {
143 fn_escaped(stdout, linktarg);
151 static void *hardlinks;
153 static void pu10(void) { printf(" %10s", "?"); }
155 static int hardlink_compar(const void *av, const void *bv) {
156 const struct hardlink *a=av, *b=bv;
157 if (a->ino != b->ino) return b->ino - a->ino;
158 return b->dev - a->dev;
161 static void recurse(const char *path, unsigned nodeflags, dev_t fs);
163 static void node(const char *path, unsigned nodeflags, dev_t fs) {
164 char linktarg[MAXFN+1];
165 struct hardlink *foundhl;
166 const struct stat *stab;
170 r= lstat(path, &stabuf);
171 stab= r ? 0 : &stabuf;
174 if (stab && stab->st_nlink>1) {
175 struct hardlink *newhl, **foundhl_node;
176 newhl= mmalloc(sizeof(*newhl) + strlen(path));
177 newhl->dev= stab->st_dev;
178 newhl->ino= stab->st_ino;
179 foundhl_node= tsearch(newhl, &hardlinks, hardlink_compar);
180 if (!foundhl_node) malloc_fail();
181 foundhl= *foundhl_node;
182 if (foundhl!=newhl) {
183 free(newhl); /* hardlink to an earlier object */
185 foundhl= 0; /* new object with link count>1 */
186 strcpy(newhl->path, path);
191 if ((nodeflags & nodeflag_fsvalid) && stab->st_dev != fs)
194 nodeflags |= nodeflag_fsvalid;
197 if (!stab) problem_e(path,CSUMXL,"inaccessible");
198 else if (foundhl) csum_str("hardlink");
199 else if (S_ISREG(stab->st_mode)) csum_file(path);
200 else if (S_ISCHR(stab->st_mode)) csum_dev('c',stab);
201 else if (S_ISBLK(stab->st_mode)) csum_dev('b',stab);
202 else if (S_ISFIFO(stab->st_mode)) csum_str("pipe");
203 else if (S_ISLNK(stab->st_mode)) csum_str("symlink");
204 else if (S_ISSOCK(stab->st_mode)) csum_str("sock");
205 else if (S_ISDIR(stab->st_mode)) csum_str(mountpoint ? "mountpoint" : "dir");
206 else problem(path,CSUMXL,"badobj: 0x%lx", (unsigned long)stab->st_mode);
208 if (stab && S_ISLNK(stab->st_mode)) {
209 r= readlink(path, linktarg, sizeof(linktarg)-1);
210 if (r==sizeof(linktarg)) { problem(path,-1,"readlink too big"); r=-1; }
211 else if (r<0) { problem_e(path,-1,"readlink"); }
212 else assert(r<sizeof(linktarg));
214 if (r<0) strcpy(linktarg,"\\?");
219 if (S_ISDIR(stab->st_mode) && hidedirsize)
220 printf(" %10s","dir");
223 (unsigned long)stab->st_size);
225 printf(" %4o %10ld %10ld",
226 (unsigned)stab->st_mode & 07777U,
227 (unsigned long)stab->st_uid,
228 (unsigned long)stab->st_gid);
230 printf(" %10s %4s %10s %10s", "?","?","?","?");
236 (unsigned long)stab->st_atime);
243 if (S_ISLNK(stab->st_mode) && hidelinkmtime)
244 printf(" %10s","link");
247 (unsigned long)stab->st_mtime);
255 (unsigned long)stab->st_ctime);
260 putchar(filenamefieldsep);
261 fn_escaped(stdout, path);
263 if (foundhl) linktargpath(foundhl->path);
264 if (stab && S_ISLNK(stab->st_mode)) linktargpath(linktarg);
268 if (ferror(stdout)) { perror("summer: stdout"); exit(12); }
270 if (stab && S_ISDIR(stab->st_mode) && !(mountpoint && onefilesystem))
271 recurse(path, nodeflags, fs);
274 static void process(const char *startpoint) {
276 fprintf(stderr,"summer: processing: %s\n",startpoint);
277 node(startpoint, 0,0);
278 tdestroy(hardlinks,free);
282 static int recurse_maxlen;
284 static int recurse_filter(const struct dirent *de) {
286 if (de->d_name[0]=='.' &&
288 (de->d_name[1]=='.' &&
291 l= strlen(de->d_name);
292 if (l > recurse_maxlen) recurse_maxlen= l;
296 static int recurse_compar(const void *av, const void *bv) {
297 const struct dirent *const *a=av, *const *b=bv;
298 return strcmp((*a)->d_name, (*b)->d_name);
301 static void recurse(const char *path_or_buf, unsigned nodeflags, dev_t fs) {
303 static int buf_allocd;
305 struct dirent **namelist, *const *de;
306 const char *path_or_0= path_or_buf==buf ? 0 : path_or_buf;
307 int nentries, pathl, esave, buf_want, i;
309 pathl= strlen(path_or_buf);
311 nentries= scandir(path_or_buf, &namelist, recurse_filter, recurse_compar);
314 buf_want= pathl+1+recurse_maxlen+1;
315 if (buf_want > buf_allocd) {
316 buf= mrealloc(buf, buf_want);
317 buf_allocd= buf_want;
319 /* NOTE that path_or_buf is invalid after this point because
320 * it might have been realloc'd ! */
321 if (path_or_0) strcpy(buf,path_or_0);
326 buf[pathl]= 0; errno= esave;
327 problem_e(buf,CSUMXL+72,"scandir failed");
328 fn_escaped(stdout,buf); putchar('\n');
331 for (i=0, de=namelist; i<nentries; i++, de++) {
332 strcpy(buf+pathl, (*de)->d_name);
333 node(buf, nodeflags, fs);
339 static void from_stdin(void) {
345 fprintf(stderr, "summer: processing stdin lines as startpoints\n");
347 s= fgets(buf,sizeof(buf),stdin);
348 if (ferror(stdin)) { perror("summer: stdin"); exit(12); }
349 if (!s) { if (feof(stdin)) return; else abort(); }
352 if (buf[l-1]!='\n') { fprintf(stderr,"summer: line too long\n"); exit(8); }
358 int main(int argc, const char *const *argv) {
364 while ((arg=argv[1]) && *arg++=='-') {
369 "summer: usage: summer startpoint... >data.list\n"
370 " cat startpoints.list | summer >data.list\n");
376 filenamefieldsep= '\t';
400 fprintf(stderr,"summer: bad usage, try -h\n");
411 fprintf(stderr, "summer: processing command line args as startpoints\n");
412 while ((arg=*++argv)) {
416 if (ferror(stdout) || fclose(stdout)) {
417 perror("summer: stdout (at end)"); exit(12);
420 fputs("summer: done.\n", stderr);