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 _LARGEFILE64_SOURCE
31 #include <sys/types.h>
33 #include <openssl/md5.h>
34 #include <netinet/in.h>
46 #if __BYTE_ORDER == __LITTLE_ENDIAN
47 #define htonll(x) __bswap_64 (x)
52 /* TODO: add hardlink recognition */
53 /* TODO: add xattr/acl */
62 unsigned char out[16];
65 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
69 char *checksum = NULL;
70 struct excludes *excludes;
89 const char flchar[] = "ugoamcdes";
92 int flags[NUM_FLAGS] = {1, 1, 1, 1, 1, 0, 1, 0, 0};
95 getln(char *buf, int size, FILE *fp)
100 p = fgets(buf, size, fp);
105 while(l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r'))
117 if (c >= 'A' && c <= 'Z') {
121 for (i = 0; flchar[i]; ++i) {
122 if (flchar[i] == c) {
123 flags[i] = is_upper ? 0 : 1;
127 fprintf(stderr, "unrecognized flag %c\n", c);
141 fprintf(stderr, "usage: fssum <options> <path>\n");
142 fprintf(stderr, " options:\n");
143 fprintf(stderr, " -f : write out a full manifest file\n");
144 fprintf(stderr, " -w <file> : send output to file\n");
145 fprintf(stderr, " -v : verbose mode (debugging only)\n");
147 " -r <file> : read checksum or manifest from file\n");
148 fprintf(stderr, " -[ugoamcde] : specify which fields to include in checksum calculation.\n");
149 fprintf(stderr, " u : include uid\n");
150 fprintf(stderr, " g : include gid\n");
151 fprintf(stderr, " o : include mode\n");
152 fprintf(stderr, " m : include mtime\n");
153 fprintf(stderr, " a : include atime\n");
154 fprintf(stderr, " c : include ctime\n");
155 fprintf(stderr, " d : include file data\n");
156 fprintf(stderr, " e : include open errors (aborts otherwise)\n");
157 fprintf(stderr, " s : include block structure (holes)\n");
158 fprintf(stderr, " -[UGOAMCDES]: exclude respective field from calculation\n");
159 fprintf(stderr, " -n : reset all flags\n");
160 fprintf(stderr, " -N : set all flags\n");
161 fprintf(stderr, " -x path : exclude path when building checksum (multiple ok)\n");
162 fprintf(stderr, " -h : this help\n\n");
163 fprintf(stderr, "The default field mask is ugoamCdES. If the checksum/manifest is read from a\n");
164 fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
165 fprintf(stderr, "are ignored.\n");
169 static char buf[65536];
174 void *p = malloc(sz);
177 fprintf(stderr, "malloc failed\n");
193 MD5_Final(cs->out, &cs->md5);
197 sum_add(sum_t *cs, void *buf, int size)
199 MD5_Update(&cs->md5, buf, size);
203 sum_add_sum(sum_t *dst, sum_t *src)
205 sum_add(dst, src->out, sizeof(src->out));
209 sum_add_u64(sum_t *dst, uint64_t val)
211 uint64_t v = htonll(val);
212 sum_add(dst, &v, sizeof(v));
216 sum_add_time(sum_t *dst, time_t t)
222 sum_to_string(sum_t *dst)
225 char *s = alloc(CS_SIZE * 2 + 1);
227 for (i = 0; i < CS_SIZE; ++i)
228 sprintf(s + i * 2, "%02x", dst->out[i]);
234 sum_file_data_permissive(int fd, sum_t *dst)
242 pos = lseek(fd, 0, SEEK_CUR);
243 if (pos == (off_t)-1)
244 return errno == ENXIO ? 0 : -2;
248 pos = lseek(fd, pos, SEEK_DATA);
249 if (pos == (off_t)-1) {
250 if (errno == ENXIO) {
252 pos = lseek(fd, 0, SEEK_END);
253 if (pos != (off_t)-1)
260 ret = read(fd, buf, sizeof(buf));
261 assert(ret); /* eof found by lseek */
264 if (old < pos) /* hole */
266 for (i = 0; i < ret; ++i) {
267 for (old = i; buf[i] == 0 && i < ret; ++i)
269 if (old < i) /* code like a hole */
276 "adding %llu zeros to sum\n",
277 (unsigned long long)zeros);
279 sum_add_u64(dst, zeros);
282 for (old = i; buf[i] != 0 && i < ret; ++i)
285 fprintf(stderr, "adding %u non-zeros to sum\n",
287 sum_add(dst, buf + old, i - old);
295 "adding %llu zeros to sum (finishing)\n",
296 (unsigned long long)zeros);
298 sum_add_u64(dst, zeros);
305 sum_file_data_strict(int fd, sum_t *dst)
310 pos = lseek(fd, 0, SEEK_CUR);
311 if (pos == (off_t)-1)
312 return errno == ENXIO ? 0 : -2;
315 pos = lseek(fd, pos, SEEK_DATA);
316 if (pos == (off_t)-1)
317 return errno == ENXIO ? 0 : -2;
318 ret = read(fd, buf, sizeof(buf));
319 assert(ret); /* eof found by lseek */
324 "adding to sum at file offset %llu, %d bytes\n",
325 (unsigned long long)pos, ret);
326 sum_add_u64(dst, (uint64_t)pos);
327 sum_add(dst, buf, ret);
335 char *out = alloc(strlen(in) * 3 + 1);
339 for (; *src; ++src) {
340 if (*src >= 32 && *src < 127 && *src != '\\') {
343 sprintf(dst, "\\%02x", (unsigned char)*src);
353 excess_file(const char *fn)
355 printf("only in local fs: %s\n", fn);
359 missing_file(const char *fn)
361 printf("only in remote fs: %s\n", fn);
365 pathcmp(const char *a, const char *b)
367 int len_a = strlen(a);
368 int len_b = strlen(b);
371 * as the containing directory is sent after the files, it has to
372 * come out bigger in the comparison.
374 if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
376 if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
383 check_match(char *fn, char *local_m, char *remote_m,
384 char *local_c, char *remote_c)
386 int match_m = !strcmp(local_m, remote_m);
387 int match_c = !strcmp(local_c, remote_c);
389 if (match_m && !match_c) {
390 printf("data mismatch in %s\n", fn);
391 } else if (!match_m && match_c) {
392 printf("metadata mismatch in %s\n", fn);
393 } else if (!match_m && !match_c) {
394 printf("metadata and data mismatch in %s\n", fn);
402 check_manifest(char *fn, char *m, char *c, int last_call)
413 cmp = pathcmp(prev_fn, fn);
417 } else if (cmp < 0) {
418 missing_file(prev_fn);
420 check_match(fn, m, prev_m, c, prev_c);
431 while ((l = getln(line, sizeof(line), in_fp))) {
432 rem_c = strrchr(l, ' ');
435 checksum = strdup(l);
440 fprintf(stderr, "malformed input\n");
444 rem_m = strrchr(l, ' ');
452 cmp = pathcmp(l, fn);
454 check_match(fn, m, rem_m, c, rem_c);
456 } else if (cmp > 0) {
459 prev_m = strdup(rem_m);
460 prev_c = strdup(rem_c);
470 namecmp(const void *aa, const void *bb)
472 char * const *a = aa;
473 char * const *b = bb;
475 return strcmp(*a, *b);
479 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
483 char **namelist = NULL;
490 sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
491 sum_file_data_strict : sum_file_data_permissive;
493 d = fdopendir(dirfd);
498 while((de = readdir(d))) {
499 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
501 if (entries == alloclen) {
503 namelist = realloc(namelist,
504 alloclen * sizeof(*namelist));
506 fprintf(stderr, "malloc failed\n");
510 namelist[entries] = strdup(de->d_name);
511 if (!namelist[entries]) {
512 fprintf(stderr, "malloc failed\n");
517 qsort(namelist, entries, sizeof(*namelist), namecmp);
518 for (i = 0; i < entries; ++i) {
526 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
527 sprintf(path, "%s/%s", path_in, namelist[i]);
528 for (excl = 0; excl < n_excludes; ++excl) {
529 if (strncmp(excludes[excl].path, path,
530 excludes[excl].len) == 0)
539 ret = lstat64(namelist[i], &st);
541 fprintf(stderr, "stat failed for %s/%s: %s\n",
542 path_prefix, path, strerror(errno));
545 sum_add_u64(&meta, level);
546 sum_add(&meta, namelist[i], strlen(namelist[i]));
547 if (!S_ISDIR(st.st_mode))
548 sum_add_u64(&meta, st.st_nlink);
550 sum_add_u64(&meta, st.st_uid);
552 sum_add_u64(&meta, st.st_gid);
553 if (flags[FLAG_MODE])
554 sum_add_u64(&meta, st.st_mode);
555 if (flags[FLAG_ATIME])
556 sum_add_time(&meta, st.st_atime);
557 if (flags[FLAG_MTIME])
558 sum_add_time(&meta, st.st_mtime);
559 if (flags[FLAG_CTIME])
560 sum_add_time(&meta, st.st_ctime);
561 if (S_ISDIR(st.st_mode)) {
562 fd = openat(dirfd, namelist[i], 0);
563 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
564 sum_add_u64(&meta, errno);
565 } else if (fd == -1) {
566 fprintf(stderr, "open failed for %s/%s: %s\n",
567 path_prefix, path, strerror(errno));
570 sum(fd, level + 1, &cs, path_prefix, path);
573 } else if (S_ISREG(st.st_mode)) {
574 sum_add_u64(&meta, st.st_size);
575 if (flags[FLAG_DATA]) {
577 fprintf(stderr, "file %s\n",
579 fd = openat(dirfd, namelist[i], 0);
580 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
581 sum_add_u64(&meta, errno);
582 } else if (fd == -1) {
584 "open failed for %s/%s: %s\n",
590 ret = sum_file_data(fd, &cs);
602 } else if (S_ISLNK(st.st_mode)) {
603 ret = readlink(namelist[i], buf, sizeof(buf));
608 sum_add(&cs, buf, ret);
609 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
610 sum_add_u64(&cs, major(st.st_rdev));
611 sum_add_u64(&cs, minor(st.st_rdev));
615 if (gen_manifest || in_manifest) {
620 if (S_ISDIR(st.st_mode))
623 m = sum_to_string(&meta);
624 c = sum_to_string(&cs);
627 fprintf(out_fp, "%s %s %s\n", fn, m, c);
629 check_manifest(fn, m, c, 0);
634 sum_add_sum(dircs, &cs);
635 sum_add_sum(dircs, &meta);
642 main(int argc, char *argv[])
650 char flagstring[sizeof(flchar)];
655 const char *allopts = "heEfuUgGoOaAmMcCdDsSnNw:r:vx:";
658 while ((c = getopt(argc, argv, allopts)) != EOF) {
685 for (i = 0; i < NUM_FLAGS; ++i)
689 for (i = 0; i < NUM_FLAGS; ++i)
693 out_fp = fopen(optarg, "w");
696 "failed to open output file: %s\n",
702 in_fp = fopen(optarg, "r");
705 "failed to open input file: %s\n",
712 excludes = realloc(excludes,
713 sizeof(*excludes) * n_excludes);
716 "failed to alloc exclude space\n");
719 excludes[n_excludes - 1].path = optarg;
730 if (optind + 1 != argc) {
731 fprintf(stderr, "missing path\n");
736 char *l = getln(line, sizeof(line), in_fp);
740 fprintf(stderr, "failed to read line from input\n");
743 if (strncmp(l, "Flags: ", 7) == 0) {
747 } else if ((p = strchr(l, ':'))) {
750 checksum = strdup(p);
752 fprintf(stderr, "invalid input file format\n");
756 fprintf(stderr, "warning: "
757 "command line flags ignored in -r mode\n");
759 strcpy(flagstring, flchar);
760 for (i = 0; i < NUM_FLAGS; ++i) {
762 flagstring[i] -= 'a' - 'A';
767 if (path[plen - 1] == '/') {
772 for (i = 0; i < n_excludes; ++i) {
773 if (strncmp(path, excludes[i].path, plen) != 0)
775 "warning: exclude %s outside of path %s\n",
776 excludes[i].path, path);
778 excludes[i].path += plen;
779 elen = strlen(excludes[i].path);
780 if (excludes[i].path[elen - 1] == '/')
782 excludes[i].path[elen] = '\0';
783 excludes[i].len = elen;
786 fd = open(path, O_RDONLY);
788 fprintf(stderr, "failed to open %s: %s\n", path,
794 fprintf(out_fp, "Flags: %s\n", flagstring);
797 sum(fd, 1, &cs, path, "");
802 check_manifest("", "", "", 1);
806 fprintf(stderr, "malformed input\n");
810 fprintf(out_fp, "%s:", flagstring);
812 fprintf(out_fp, "%s\n", sum_to_string(&cs));
814 if (strcmp(checksum, sum_to_string(&cs)) == 0) {