2 * Copyright (C) 2012 STRATO AG. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #define _DEFAULT_SOURCE
21 #define _LARGEFILE64_SOURCE
33 #include <sys/types.h>
35 #include <sys/sysmacros.h>
37 #include <sys/mkdev.h>
40 #include <netinet/in.h>
55 /* TODO: add hardlink recognition */
56 /* TODO: add xattr/acl */
65 unsigned char out[16];
68 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
72 char *checksum = NULL;
73 struct excludes *excludes;
92 const char flchar[] = "ugoamcdes";
95 int flags[NUM_FLAGS] = {1, 1, 1, 1, 1, 0, 1, 0, 0};
98 getln(char *buf, int size, FILE *fp)
103 p = fgets(buf, size, fp);
108 while(l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r'))
120 if (c >= 'A' && c <= 'Z') {
124 for (i = 0; flchar[i]; ++i) {
125 if (flchar[i] == c) {
126 flags[i] = is_upper ? 0 : 1;
130 fprintf(stderr, "unrecognized flag %c\n", c);
144 fprintf(stderr, "usage: fssum <options> <path>\n");
145 fprintf(stderr, " options:\n");
146 fprintf(stderr, " -f : write out a full manifest file\n");
147 fprintf(stderr, " -w <file> : send output to file\n");
148 fprintf(stderr, " -v : verbose mode (debugging only)\n");
150 " -r <file> : read checksum or manifest from file\n");
151 fprintf(stderr, " -[ugoamcde] : specify which fields to include in checksum calculation.\n");
152 fprintf(stderr, " u : include uid\n");
153 fprintf(stderr, " g : include gid\n");
154 fprintf(stderr, " o : include mode\n");
155 fprintf(stderr, " m : include mtime\n");
156 fprintf(stderr, " a : include atime\n");
157 fprintf(stderr, " c : include ctime\n");
158 fprintf(stderr, " d : include file data\n");
159 fprintf(stderr, " e : include open errors (aborts otherwise)\n");
160 fprintf(stderr, " s : include block structure (holes)\n");
161 fprintf(stderr, " -[UGOAMCDES]: exclude respective field from calculation\n");
162 fprintf(stderr, " -n : reset all flags\n");
163 fprintf(stderr, " -N : set all flags\n");
164 fprintf(stderr, " -x path : exclude path when building checksum (multiple ok)\n");
165 fprintf(stderr, " -h : this help\n\n");
166 fprintf(stderr, "The default field mask is ugoamCdES. If the checksum/manifest is read from a\n");
167 fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
168 fprintf(stderr, "are ignored.\n");
172 static char buf[65536];
177 void *p = malloc(sz);
180 fprintf(stderr, "malloc failed\n");
196 MD5_Final(cs->out, &cs->md5);
200 sum_add(sum_t *cs, void *buf, int size)
202 MD5_Update(&cs->md5, buf, size);
206 sum_add_sum(sum_t *dst, sum_t *src)
208 sum_add(dst, src->out, sizeof(src->out));
212 sum_add_u64(sum_t *dst, uint64_t val)
214 uint64_t v = htobe64(val);
215 sum_add(dst, &v, sizeof(v));
219 sum_add_time(sum_t *dst, time_t t)
225 sum_to_string(sum_t *dst)
228 char *s = alloc(CS_SIZE * 2 + 1);
230 for (i = 0; i < CS_SIZE; ++i)
231 sprintf(s + i * 2, "%02x", dst->out[i]);
237 sum_file_data_permissive(int fd, sum_t *dst)
245 pos = lseek(fd, 0, SEEK_CUR);
246 if (pos == (off_t)-1)
247 return errno == ENXIO ? 0 : -2;
251 pos = lseek(fd, pos, SEEK_DATA);
252 if (pos == (off_t)-1) {
253 if (errno == ENXIO) {
255 pos = lseek(fd, 0, SEEK_END);
256 if (pos != (off_t)-1)
263 ret = read(fd, buf, sizeof(buf));
264 assert(ret); /* eof found by lseek */
267 if (old < pos) /* hole */
269 for (i = 0; i < ret; ++i) {
270 for (old = i; buf[i] == 0 && i < ret; ++i)
272 if (old < i) /* code like a hole */
279 "adding %llu zeros to sum\n",
280 (unsigned long long)zeros);
282 sum_add_u64(dst, zeros);
285 for (old = i; buf[i] != 0 && i < ret; ++i)
288 fprintf(stderr, "adding %u non-zeros to sum\n",
290 sum_add(dst, buf + old, i - old);
298 "adding %llu zeros to sum (finishing)\n",
299 (unsigned long long)zeros);
301 sum_add_u64(dst, zeros);
308 sum_file_data_strict(int fd, sum_t *dst)
313 pos = lseek(fd, 0, SEEK_CUR);
314 if (pos == (off_t)-1)
315 return errno == ENXIO ? 0 : -2;
318 pos = lseek(fd, pos, SEEK_DATA);
319 if (pos == (off_t)-1)
320 return errno == ENXIO ? 0 : -2;
321 ret = read(fd, buf, sizeof(buf));
322 assert(ret); /* eof found by lseek */
327 "adding to sum at file offset %llu, %d bytes\n",
328 (unsigned long long)pos, ret);
329 sum_add_u64(dst, (uint64_t)pos);
330 sum_add(dst, buf, ret);
338 char *out = alloc(strlen(in) * 3 + 1);
342 for (; *src; ++src) {
343 if (*src >= 32 && *src < 127 && *src != '\\') {
346 sprintf(dst, "\\%02x", (unsigned char)*src);
356 excess_file(const char *fn)
358 printf("only in local fs: %s\n", fn);
362 missing_file(const char *fn)
364 printf("only in remote fs: %s\n", fn);
368 pathcmp(const char *a, const char *b)
370 int len_a = strlen(a);
371 int len_b = strlen(b);
374 * as the containing directory is sent after the files, it has to
375 * come out bigger in the comparison.
377 if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
379 if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
386 check_match(char *fn, char *local_m, char *remote_m,
387 char *local_c, char *remote_c)
389 int match_m = !strcmp(local_m, remote_m);
390 int match_c = !strcmp(local_c, remote_c);
392 if (match_m && !match_c) {
393 printf("data mismatch in %s\n", fn);
394 } else if (!match_m && match_c) {
395 printf("metadata mismatch in %s\n", fn);
396 } else if (!match_m && !match_c) {
397 printf("metadata and data mismatch in %s\n", fn);
405 check_manifest(char *fn, char *m, char *c, int last_call)
416 cmp = pathcmp(prev_fn, fn);
420 } else if (cmp < 0) {
421 missing_file(prev_fn);
423 check_match(fn, m, prev_m, c, prev_c);
434 while ((l = getln(line, sizeof(line), in_fp))) {
435 rem_c = strrchr(l, ' ');
438 checksum = strdup(l);
443 fprintf(stderr, "malformed input\n");
447 rem_m = strrchr(l, ' ');
455 cmp = pathcmp(l, fn);
457 check_match(fn, m, rem_m, c, rem_c);
459 } else if (cmp > 0) {
462 prev_m = strdup(rem_m);
463 prev_c = strdup(rem_c);
473 namecmp(const void *aa, const void *bb)
475 char * const *a = aa;
476 char * const *b = bb;
478 return strcmp(*a, *b);
482 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
486 char **namelist = NULL;
493 sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
494 sum_file_data_strict : sum_file_data_permissive;
496 d = fdopendir(dirfd);
501 while((de = readdir(d))) {
502 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
504 if (entries == alloclen) {
506 namelist = realloc(namelist,
507 alloclen * sizeof(*namelist));
509 fprintf(stderr, "malloc failed\n");
513 namelist[entries] = strdup(de->d_name);
514 if (!namelist[entries]) {
515 fprintf(stderr, "malloc failed\n");
520 qsort(namelist, entries, sizeof(*namelist), namecmp);
521 for (i = 0; i < entries; ++i) {
529 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
530 sprintf(path, "%s/%s", path_in, namelist[i]);
531 for (excl = 0; excl < n_excludes; ++excl) {
532 if (strncmp(excludes[excl].path, path,
533 excludes[excl].len) == 0)
542 ret = lstat64(namelist[i], &st);
544 fprintf(stderr, "stat failed for %s/%s: %s\n",
545 path_prefix, path, strerror(errno));
548 sum_add_u64(&meta, level);
549 sum_add(&meta, namelist[i], strlen(namelist[i]));
550 if (!S_ISDIR(st.st_mode))
551 sum_add_u64(&meta, st.st_nlink);
553 sum_add_u64(&meta, st.st_uid);
555 sum_add_u64(&meta, st.st_gid);
556 if (flags[FLAG_MODE])
557 sum_add_u64(&meta, st.st_mode);
558 if (flags[FLAG_ATIME])
559 sum_add_time(&meta, st.st_atime);
560 if (flags[FLAG_MTIME])
561 sum_add_time(&meta, st.st_mtime);
562 if (flags[FLAG_CTIME])
563 sum_add_time(&meta, st.st_ctime);
564 if (S_ISDIR(st.st_mode)) {
565 fd = openat(dirfd, namelist[i], 0);
566 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
567 sum_add_u64(&meta, errno);
568 } else if (fd == -1) {
569 fprintf(stderr, "open failed for %s/%s: %s\n",
570 path_prefix, path, strerror(errno));
573 sum(fd, level + 1, &cs, path_prefix, path);
576 } else if (S_ISREG(st.st_mode)) {
577 sum_add_u64(&meta, st.st_size);
578 if (flags[FLAG_DATA]) {
580 fprintf(stderr, "file %s\n",
582 fd = openat(dirfd, namelist[i], 0);
583 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
584 sum_add_u64(&meta, errno);
585 } else if (fd == -1) {
587 "open failed for %s/%s: %s\n",
593 ret = sum_file_data(fd, &cs);
605 } else if (S_ISLNK(st.st_mode)) {
606 ret = readlink(namelist[i], buf, sizeof(buf));
611 sum_add(&cs, buf, ret);
612 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
613 sum_add_u64(&cs, major(st.st_rdev));
614 sum_add_u64(&cs, minor(st.st_rdev));
618 if (gen_manifest || in_manifest) {
623 if (S_ISDIR(st.st_mode))
626 m = sum_to_string(&meta);
627 c = sum_to_string(&cs);
630 fprintf(out_fp, "%s %s %s\n", fn, m, c);
632 check_manifest(fn, m, c, 0);
637 sum_add_sum(dircs, &cs);
638 sum_add_sum(dircs, &meta);
645 main(int argc, char *argv[])
653 char flagstring[sizeof(flchar)];
658 const char *allopts = "heEfuUgGoOaAmMcCdDsSnNw:r:vx:";
661 while ((c = getopt(argc, argv, allopts)) != EOF) {
688 for (i = 0; i < NUM_FLAGS; ++i)
692 for (i = 0; i < NUM_FLAGS; ++i)
696 out_fp = fopen(optarg, "w");
699 "failed to open output file: %s\n",
705 in_fp = fopen(optarg, "r");
708 "failed to open input file: %s\n",
715 excludes = realloc(excludes,
716 sizeof(*excludes) * n_excludes);
719 "failed to alloc exclude space\n");
722 excludes[n_excludes - 1].path = optarg;
733 if (optind + 1 != argc) {
734 fprintf(stderr, "missing path\n");
739 char *l = getln(line, sizeof(line), in_fp);
743 fprintf(stderr, "failed to read line from input\n");
746 if (strncmp(l, "Flags: ", 7) == 0) {
750 } else if ((p = strchr(l, ':'))) {
753 checksum = strdup(p);
755 fprintf(stderr, "invalid input file format\n");
759 fprintf(stderr, "warning: "
760 "command line flags ignored in -r mode\n");
762 strcpy(flagstring, flchar);
763 for (i = 0; i < NUM_FLAGS; ++i) {
765 flagstring[i] -= 'a' - 'A';
770 if (path[plen - 1] == '/') {
775 for (i = 0; i < n_excludes; ++i) {
776 if (strncmp(path, excludes[i].path, plen) != 0)
778 "warning: exclude %s outside of path %s\n",
779 excludes[i].path, path);
781 excludes[i].path += plen;
782 elen = strlen(excludes[i].path);
783 if (excludes[i].path[elen - 1] == '/')
785 excludes[i].path[elen] = '\0';
786 excludes[i].len = elen;
789 fd = open(path, O_RDONLY);
791 fprintf(stderr, "failed to open %s: %s\n", path,
797 fprintf(out_fp, "Flags: %s\n", flagstring);
800 sum(fd, 1, &cs, path, "");
805 check_manifest("", "", "", 1);
809 fprintf(stderr, "malformed input\n");
813 fprintf(out_fp, "%s:", flagstring);
815 fprintf(out_fp, "%s\n", sum_to_string(&cs));
817 if (strcmp(checksum, sum_to_string(&cs)) == 0) {