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
25 #include "kerncompat.h"
33 #include <sys/types.h>
35 #include <openssl/md5.h>
36 #include <netinet/in.h>
48 /* TODO: add hardlink recognition */
49 /* TODO: add xattr/acl */
58 unsigned char out[16];
61 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
65 char *checksum = NULL;
66 struct excludes *excludes;
85 const char flchar[] = "ugoamcdes";
88 int flags[NUM_FLAGS] = {1, 1, 1, 1, 1, 0, 1, 0, 0};
91 getln(char *buf, int size, FILE *fp)
96 p = fgets(buf, size, fp);
101 while(l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r'))
113 if (c >= 'A' && c <= 'Z') {
117 for (i = 0; flchar[i]; ++i) {
118 if (flchar[i] == c) {
119 flags[i] = is_upper ? 0 : 1;
123 fprintf(stderr, "unrecognized flag %c\n", c);
137 fprintf(stderr, "usage: fssum <options> <path>\n");
138 fprintf(stderr, " options:\n");
139 fprintf(stderr, " -f : write out a full manifest file\n");
140 fprintf(stderr, " -w <file> : send output to file\n");
141 fprintf(stderr, " -v : verbose mode (debugging only)\n");
143 " -r <file> : read checksum or manifest from file\n");
144 fprintf(stderr, " -[ugoamcde] : specify which fields to include in checksum calculation.\n");
145 fprintf(stderr, " u : include uid\n");
146 fprintf(stderr, " g : include gid\n");
147 fprintf(stderr, " o : include mode\n");
148 fprintf(stderr, " m : include mtime\n");
149 fprintf(stderr, " a : include atime\n");
150 fprintf(stderr, " c : include ctime\n");
151 fprintf(stderr, " d : include file data\n");
152 fprintf(stderr, " e : include open errors (aborts otherwise)\n");
153 fprintf(stderr, " s : include block structure (holes)\n");
154 fprintf(stderr, " -[UGOAMCDES]: exclude respective field from calculation\n");
155 fprintf(stderr, " -n : reset all flags\n");
156 fprintf(stderr, " -N : set all flags\n");
157 fprintf(stderr, " -x path : exclude path when building checksum (multiple ok)\n");
158 fprintf(stderr, " -h : this help\n\n");
159 fprintf(stderr, "The default field mask is ugoamCdES. If the checksum/manifest is read from a\n");
160 fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
161 fprintf(stderr, "are ignored.\n");
165 static char buf[65536];
170 void *p = malloc(sz);
173 fprintf(stderr, "malloc failed\n");
189 MD5_Final(cs->out, &cs->md5);
193 sum_add(sum_t *cs, void *buf, int size)
195 MD5_Update(&cs->md5, buf, size);
199 sum_add_sum(sum_t *dst, sum_t *src)
201 sum_add(dst, src->out, sizeof(src->out));
205 sum_add_u64(sum_t *dst, uint64_t val)
207 uint64_t v = cpu_to_le64(val);
208 sum_add(dst, &v, sizeof(v));
212 sum_add_time(sum_t *dst, time_t t)
218 sum_to_string(sum_t *dst)
221 char *s = alloc(CS_SIZE * 2 + 1);
223 for (i = 0; i < CS_SIZE; ++i)
224 sprintf(s + i * 2, "%02x", dst->out[i]);
230 sum_file_data_permissive(int fd, sum_t *dst)
238 pos = lseek(fd, 0, SEEK_CUR);
239 if (pos == (off_t)-1)
240 return errno == ENXIO ? 0 : -2;
244 pos = lseek(fd, pos, SEEK_DATA);
245 if (pos == (off_t)-1) {
246 if (errno == ENXIO) {
248 pos = lseek(fd, 0, SEEK_END);
249 if (pos != (off_t)-1)
256 ret = read(fd, buf, sizeof(buf));
257 assert(ret); /* eof found by lseek */
260 if (old < pos) /* hole */
262 for (i = 0; i < ret; ++i) {
263 for (old = i; buf[i] == 0 && i < ret; ++i)
265 if (old < i) /* code like a hole */
272 "adding %llu zeros to sum\n",
273 (unsigned long long)zeros);
275 sum_add_u64(dst, zeros);
278 for (old = i; buf[i] != 0 && i < ret; ++i)
281 fprintf(stderr, "adding %u non-zeros to sum\n",
283 sum_add(dst, buf + old, i - old);
291 "adding %llu zeros to sum (finishing)\n",
292 (unsigned long long)zeros);
294 sum_add_u64(dst, zeros);
301 sum_file_data_strict(int fd, sum_t *dst)
306 pos = lseek(fd, 0, SEEK_CUR);
307 if (pos == (off_t)-1)
308 return errno == ENXIO ? 0 : -2;
311 pos = lseek(fd, pos, SEEK_DATA);
312 if (pos == (off_t)-1)
313 return errno == ENXIO ? 0 : -2;
314 ret = read(fd, buf, sizeof(buf));
315 assert(ret); /* eof found by lseek */
320 "adding to sum at file offset %llu, %d bytes\n",
321 (unsigned long long)pos, ret);
322 sum_add_u64(dst, (uint64_t)pos);
323 sum_add(dst, buf, ret);
331 char *out = alloc(strlen(in) * 3 + 1);
335 for (; *src; ++src) {
336 if (*src >= 32 && *src < 127 && *src != '\\') {
339 sprintf(dst, "\\%02x", (unsigned char)*src);
349 excess_file(const char *fn)
351 printf("only in local fs: %s\n", fn);
355 missing_file(const char *fn)
357 printf("only in remote fs: %s\n", fn);
361 pathcmp(const char *a, const char *b)
363 int len_a = strlen(a);
364 int len_b = strlen(b);
367 * as the containing directory is sent after the files, it has to
368 * come out bigger in the comparison.
370 if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
372 if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
379 check_match(char *fn, char *local_m, char *remote_m,
380 char *local_c, char *remote_c)
382 int match_m = !strcmp(local_m, remote_m);
383 int match_c = !strcmp(local_c, remote_c);
385 if (match_m && !match_c) {
386 printf("data mismatch in %s\n", fn);
387 } else if (!match_m && match_c) {
388 printf("metadata mismatch in %s\n", fn);
389 } else if (!match_m && !match_c) {
390 printf("metadata and data mismatch in %s\n", fn);
398 check_manifest(char *fn, char *m, char *c, int last_call)
409 cmp = pathcmp(prev_fn, fn);
413 } else if (cmp < 0) {
414 missing_file(prev_fn);
416 check_match(fn, m, prev_m, c, prev_c);
427 while ((l = getln(line, sizeof(line), in_fp))) {
428 rem_c = strrchr(l, ' ');
431 checksum = strdup(l);
436 fprintf(stderr, "malformed input\n");
440 rem_m = strrchr(l, ' ');
448 cmp = pathcmp(l, fn);
450 check_match(fn, m, rem_m, c, rem_c);
452 } else if (cmp > 0) {
455 prev_m = strdup(rem_m);
456 prev_c = strdup(rem_c);
466 namecmp(const void *aa, const void *bb)
468 char * const *a = aa;
469 char * const *b = bb;
471 return strcmp(*a, *b);
475 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
479 char **namelist = NULL;
486 sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
487 sum_file_data_strict : sum_file_data_permissive;
489 d = fdopendir(dirfd);
494 while((de = readdir(d))) {
495 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
497 if (entries == alloclen) {
499 namelist = realloc(namelist,
500 alloclen * sizeof(*namelist));
502 fprintf(stderr, "malloc failed\n");
506 namelist[entries] = strdup(de->d_name);
507 if (!namelist[entries]) {
508 fprintf(stderr, "malloc failed\n");
513 qsort(namelist, entries, sizeof(*namelist), namecmp);
514 for (i = 0; i < entries; ++i) {
522 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
523 sprintf(path, "%s/%s", path_in, namelist[i]);
524 for (excl = 0; excl < n_excludes; ++excl) {
525 if (strncmp(excludes[excl].path, path,
526 excludes[excl].len) == 0)
535 ret = lstat64(namelist[i], &st);
537 fprintf(stderr, "stat failed for %s/%s: %s\n",
538 path_prefix, path, strerror(errno));
541 sum_add_u64(&meta, level);
542 sum_add(&meta, namelist[i], strlen(namelist[i]));
543 if (!S_ISDIR(st.st_mode))
544 sum_add_u64(&meta, st.st_nlink);
546 sum_add_u64(&meta, st.st_uid);
548 sum_add_u64(&meta, st.st_gid);
549 if (flags[FLAG_MODE])
550 sum_add_u64(&meta, st.st_mode);
551 if (flags[FLAG_ATIME])
552 sum_add_time(&meta, st.st_atime);
553 if (flags[FLAG_MTIME])
554 sum_add_time(&meta, st.st_mtime);
555 if (flags[FLAG_CTIME])
556 sum_add_time(&meta, st.st_ctime);
557 if (S_ISDIR(st.st_mode)) {
558 fd = openat(dirfd, namelist[i], 0);
559 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
560 sum_add_u64(&meta, errno);
561 } else if (fd == -1) {
562 fprintf(stderr, "open failed for %s/%s: %s\n",
563 path_prefix, path, strerror(errno));
566 sum(fd, level + 1, &cs, path_prefix, path);
569 } else if (S_ISREG(st.st_mode)) {
570 sum_add_u64(&meta, st.st_size);
571 if (flags[FLAG_DATA]) {
573 fprintf(stderr, "file %s\n",
575 fd = openat(dirfd, namelist[i], 0);
576 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
577 sum_add_u64(&meta, errno);
578 } else if (fd == -1) {
580 "open failed for %s/%s: %s\n",
586 ret = sum_file_data(fd, &cs);
598 } else if (S_ISLNK(st.st_mode)) {
599 ret = readlink(namelist[i], buf, sizeof(buf));
604 sum_add(&cs, buf, ret);
605 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
606 sum_add_u64(&cs, major(st.st_rdev));
607 sum_add_u64(&cs, minor(st.st_rdev));
611 if (gen_manifest || in_manifest) {
616 if (S_ISDIR(st.st_mode))
619 m = sum_to_string(&meta);
620 c = sum_to_string(&cs);
623 fprintf(out_fp, "%s %s %s\n", fn, m, c);
625 check_manifest(fn, m, c, 0);
630 sum_add_sum(dircs, &cs);
631 sum_add_sum(dircs, &meta);
638 main(int argc, char *argv[])
646 char flagstring[sizeof(flchar)];
651 const char *allopts = "heEfuUgGoOaAmMcCdDsSnNw:r:vx:";
654 while ((c = getopt(argc, argv, allopts)) != EOF) {
681 for (i = 0; i < NUM_FLAGS; ++i)
685 for (i = 0; i < NUM_FLAGS; ++i)
689 out_fp = fopen(optarg, "w");
692 "failed to open output file: %s\n",
698 in_fp = fopen(optarg, "r");
701 "failed to open input file: %s\n",
708 excludes = realloc(excludes,
709 sizeof(*excludes) * n_excludes);
712 "failed to alloc exclude space\n");
715 excludes[n_excludes - 1].path = optarg;
726 if (optind + 1 != argc) {
727 fprintf(stderr, "missing path\n");
732 char *l = getln(line, sizeof(line), in_fp);
736 fprintf(stderr, "failed to read line from input\n");
739 if (strncmp(l, "Flags: ", 7) == 0) {
743 } else if ((p = strchr(l, ':'))) {
746 checksum = strdup(p);
748 fprintf(stderr, "invalid input file format\n");
752 fprintf(stderr, "warning: "
753 "command line flags ignored in -r mode\n");
755 strcpy(flagstring, flchar);
756 for (i = 0; i < NUM_FLAGS; ++i) {
758 flagstring[i] -= 'a' - 'A';
763 if (path[plen - 1] == '/') {
768 for (i = 0; i < n_excludes; ++i) {
769 if (strncmp(path, excludes[i].path, plen) != 0)
771 "warning: exclude %s outside of path %s\n",
772 excludes[i].path, path);
774 excludes[i].path += plen;
775 elen = strlen(excludes[i].path);
776 if (excludes[i].path[elen - 1] == '/')
778 excludes[i].path[elen] = '\0';
779 excludes[i].len = elen;
782 fd = open(path, O_RDONLY);
784 fprintf(stderr, "failed to open %s: %s\n", path,
790 fprintf(out_fp, "Flags: %s\n", flagstring);
793 sum(fd, 1, &cs, path, "");
798 check_manifest("", "", "", 1);
802 fprintf(stderr, "malformed input\n");
806 fprintf(out_fp, "%s:", flagstring);
808 fprintf(out_fp, "%s\n", sum_to_string(&cs));
810 if (strcmp(checksum, sum_to_string(&cs)) == 0) {