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
32 #include <sys/types.h>
35 #include <sys/mkdev.h>
37 #include <openssl/md5.h>
38 #include <netinet/in.h>
51 #if __BYTE_ORDER == __LITTLE_ENDIAN
52 #define htonll(x) __bswap_64 (x)
58 /* TODO: add hardlink recognition */
59 /* TODO: add xattr/acl */
68 unsigned char out[16];
71 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
75 char *checksum = NULL;
76 struct excludes *excludes;
95 const char flchar[] = "ugoamcdes";
98 int flags[NUM_FLAGS] = {1, 1, 1, 1, 1, 0, 1, 0, 0};
101 getln(char *buf, int size, FILE *fp)
106 p = fgets(buf, size, fp);
111 while(l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r'))
123 if (c >= 'A' && c <= 'Z') {
127 for (i = 0; flchar[i]; ++i) {
128 if (flchar[i] == c) {
129 flags[i] = is_upper ? 0 : 1;
133 fprintf(stderr, "unrecognized flag %c\n", c);
147 fprintf(stderr, "usage: fssum <options> <path>\n");
148 fprintf(stderr, " options:\n");
149 fprintf(stderr, " -f : write out a full manifest file\n");
150 fprintf(stderr, " -w <file> : send output to file\n");
151 fprintf(stderr, " -v : verbose mode (debugging only)\n");
153 " -r <file> : read checksum or manifest from file\n");
154 fprintf(stderr, " -[ugoamcde] : specify which fields to include in checksum calculation.\n");
155 fprintf(stderr, " u : include uid\n");
156 fprintf(stderr, " g : include gid\n");
157 fprintf(stderr, " o : include mode\n");
158 fprintf(stderr, " m : include mtime\n");
159 fprintf(stderr, " a : include atime\n");
160 fprintf(stderr, " c : include ctime\n");
161 fprintf(stderr, " d : include file data\n");
162 fprintf(stderr, " e : include open errors (aborts otherwise)\n");
163 fprintf(stderr, " s : include block structure (holes)\n");
164 fprintf(stderr, " -[UGOAMCDES]: exclude respective field from calculation\n");
165 fprintf(stderr, " -n : reset all flags\n");
166 fprintf(stderr, " -N : set all flags\n");
167 fprintf(stderr, " -x path : exclude path when building checksum (multiple ok)\n");
168 fprintf(stderr, " -h : this help\n\n");
169 fprintf(stderr, "The default field mask is ugoamCdES. If the checksum/manifest is read from a\n");
170 fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
171 fprintf(stderr, "are ignored.\n");
175 static char buf[65536];
180 void *p = malloc(sz);
183 fprintf(stderr, "malloc failed\n");
199 MD5_Final(cs->out, &cs->md5);
203 sum_add(sum_t *cs, void *buf, int size)
205 MD5_Update(&cs->md5, buf, size);
209 sum_add_sum(sum_t *dst, sum_t *src)
211 sum_add(dst, src->out, sizeof(src->out));
215 sum_add_u64(sum_t *dst, uint64_t val)
217 uint64_t v = htonll(val);
218 sum_add(dst, &v, sizeof(v));
222 sum_add_time(sum_t *dst, time_t t)
228 sum_to_string(sum_t *dst)
231 char *s = alloc(CS_SIZE * 2 + 1);
233 for (i = 0; i < CS_SIZE; ++i)
234 sprintf(s + i * 2, "%02x", dst->out[i]);
240 sum_file_data_permissive(int fd, sum_t *dst)
248 pos = lseek(fd, 0, SEEK_CUR);
249 if (pos == (off_t)-1)
250 return errno == ENXIO ? 0 : -2;
254 pos = lseek(fd, pos, SEEK_DATA);
255 if (pos == (off_t)-1) {
256 if (errno == ENXIO) {
258 pos = lseek(fd, 0, SEEK_END);
259 if (pos != (off_t)-1)
266 ret = read(fd, buf, sizeof(buf));
267 assert(ret); /* eof found by lseek */
270 if (old < pos) /* hole */
272 for (i = 0; i < ret; ++i) {
273 for (old = i; buf[i] == 0 && i < ret; ++i)
275 if (old < i) /* code like a hole */
282 "adding %llu zeros to sum\n",
283 (unsigned long long)zeros);
285 sum_add_u64(dst, zeros);
288 for (old = i; buf[i] != 0 && i < ret; ++i)
291 fprintf(stderr, "adding %u non-zeros to sum\n",
293 sum_add(dst, buf + old, i - old);
301 "adding %llu zeros to sum (finishing)\n",
302 (unsigned long long)zeros);
304 sum_add_u64(dst, zeros);
311 sum_file_data_strict(int fd, sum_t *dst)
316 pos = lseek(fd, 0, SEEK_CUR);
317 if (pos == (off_t)-1)
318 return errno == ENXIO ? 0 : -2;
321 pos = lseek(fd, pos, SEEK_DATA);
322 if (pos == (off_t)-1)
323 return errno == ENXIO ? 0 : -2;
324 ret = read(fd, buf, sizeof(buf));
325 assert(ret); /* eof found by lseek */
330 "adding to sum at file offset %llu, %d bytes\n",
331 (unsigned long long)pos, ret);
332 sum_add_u64(dst, (uint64_t)pos);
333 sum_add(dst, buf, ret);
341 char *out = alloc(strlen(in) * 3 + 1);
345 for (; *src; ++src) {
346 if (*src >= 32 && *src < 127 && *src != '\\') {
349 sprintf(dst, "\\%02x", (unsigned char)*src);
359 excess_file(const char *fn)
361 printf("only in local fs: %s\n", fn);
365 missing_file(const char *fn)
367 printf("only in remote fs: %s\n", fn);
371 pathcmp(const char *a, const char *b)
373 int len_a = strlen(a);
374 int len_b = strlen(b);
377 * as the containing directory is sent after the files, it has to
378 * come out bigger in the comparison.
380 if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
382 if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
389 check_match(char *fn, char *local_m, char *remote_m,
390 char *local_c, char *remote_c)
392 int match_m = !strcmp(local_m, remote_m);
393 int match_c = !strcmp(local_c, remote_c);
395 if (match_m && !match_c) {
396 printf("data mismatch in %s\n", fn);
397 } else if (!match_m && match_c) {
398 printf("metadata mismatch in %s\n", fn);
399 } else if (!match_m && !match_c) {
400 printf("metadata and data mismatch in %s\n", fn);
408 check_manifest(char *fn, char *m, char *c, int last_call)
419 cmp = pathcmp(prev_fn, fn);
423 } else if (cmp < 0) {
424 missing_file(prev_fn);
426 check_match(fn, m, prev_m, c, prev_c);
437 while ((l = getln(line, sizeof(line), in_fp))) {
438 rem_c = strrchr(l, ' ');
441 checksum = strdup(l);
446 fprintf(stderr, "malformed input\n");
450 rem_m = strrchr(l, ' ');
458 cmp = pathcmp(l, fn);
460 check_match(fn, m, rem_m, c, rem_c);
462 } else if (cmp > 0) {
465 prev_m = strdup(rem_m);
466 prev_c = strdup(rem_c);
476 namecmp(const void *aa, const void *bb)
478 char * const *a = aa;
479 char * const *b = bb;
481 return strcmp(*a, *b);
485 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
489 char **namelist = NULL;
496 sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
497 sum_file_data_strict : sum_file_data_permissive;
499 d = fdopendir(dirfd);
504 while((de = readdir(d))) {
505 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
507 if (entries == alloclen) {
509 namelist = realloc(namelist,
510 alloclen * sizeof(*namelist));
512 fprintf(stderr, "malloc failed\n");
516 namelist[entries] = strdup(de->d_name);
517 if (!namelist[entries]) {
518 fprintf(stderr, "malloc failed\n");
523 qsort(namelist, entries, sizeof(*namelist), namecmp);
524 for (i = 0; i < entries; ++i) {
532 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
533 sprintf(path, "%s/%s", path_in, namelist[i]);
534 for (excl = 0; excl < n_excludes; ++excl) {
535 if (strncmp(excludes[excl].path, path,
536 excludes[excl].len) == 0)
545 ret = lstat64(namelist[i], &st);
547 fprintf(stderr, "stat failed for %s/%s: %s\n",
548 path_prefix, path, strerror(errno));
551 sum_add_u64(&meta, level);
552 sum_add(&meta, namelist[i], strlen(namelist[i]));
553 if (!S_ISDIR(st.st_mode))
554 sum_add_u64(&meta, st.st_nlink);
556 sum_add_u64(&meta, st.st_uid);
558 sum_add_u64(&meta, st.st_gid);
559 if (flags[FLAG_MODE])
560 sum_add_u64(&meta, st.st_mode);
561 if (flags[FLAG_ATIME])
562 sum_add_time(&meta, st.st_atime);
563 if (flags[FLAG_MTIME])
564 sum_add_time(&meta, st.st_mtime);
565 if (flags[FLAG_CTIME])
566 sum_add_time(&meta, st.st_ctime);
567 if (S_ISDIR(st.st_mode)) {
568 fd = openat(dirfd, namelist[i], 0);
569 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
570 sum_add_u64(&meta, errno);
571 } else if (fd == -1) {
572 fprintf(stderr, "open failed for %s/%s: %s\n",
573 path_prefix, path, strerror(errno));
576 sum(fd, level + 1, &cs, path_prefix, path);
579 } else if (S_ISREG(st.st_mode)) {
580 sum_add_u64(&meta, st.st_size);
581 if (flags[FLAG_DATA]) {
583 fprintf(stderr, "file %s\n",
585 fd = openat(dirfd, namelist[i], 0);
586 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
587 sum_add_u64(&meta, errno);
588 } else if (fd == -1) {
590 "open failed for %s/%s: %s\n",
596 ret = sum_file_data(fd, &cs);
608 } else if (S_ISLNK(st.st_mode)) {
609 ret = readlink(namelist[i], buf, sizeof(buf));
614 sum_add(&cs, buf, ret);
615 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
616 sum_add_u64(&cs, major(st.st_rdev));
617 sum_add_u64(&cs, minor(st.st_rdev));
621 if (gen_manifest || in_manifest) {
626 if (S_ISDIR(st.st_mode))
629 m = sum_to_string(&meta);
630 c = sum_to_string(&cs);
633 fprintf(out_fp, "%s %s %s\n", fn, m, c);
635 check_manifest(fn, m, c, 0);
640 sum_add_sum(dircs, &cs);
641 sum_add_sum(dircs, &meta);
648 main(int argc, char *argv[])
656 char flagstring[sizeof(flchar)];
661 const char *allopts = "heEfuUgGoOaAmMcCdDsSnNw:r:vx:";
664 while ((c = getopt(argc, argv, allopts)) != EOF) {
691 for (i = 0; i < NUM_FLAGS; ++i)
695 for (i = 0; i < NUM_FLAGS; ++i)
699 out_fp = fopen(optarg, "w");
702 "failed to open output file: %s\n",
708 in_fp = fopen(optarg, "r");
711 "failed to open input file: %s\n",
718 excludes = realloc(excludes,
719 sizeof(*excludes) * n_excludes);
722 "failed to alloc exclude space\n");
725 excludes[n_excludes - 1].path = optarg;
736 if (optind + 1 != argc) {
737 fprintf(stderr, "missing path\n");
742 char *l = getln(line, sizeof(line), in_fp);
746 fprintf(stderr, "failed to read line from input\n");
749 if (strncmp(l, "Flags: ", 7) == 0) {
753 } else if ((p = strchr(l, ':'))) {
756 checksum = strdup(p);
758 fprintf(stderr, "invalid input file format\n");
762 fprintf(stderr, "warning: "
763 "command line flags ignored in -r mode\n");
765 strcpy(flagstring, flchar);
766 for (i = 0; i < NUM_FLAGS; ++i) {
768 flagstring[i] -= 'a' - 'A';
773 if (path[plen - 1] == '/') {
778 for (i = 0; i < n_excludes; ++i) {
779 if (strncmp(path, excludes[i].path, plen) != 0)
781 "warning: exclude %s outside of path %s\n",
782 excludes[i].path, path);
784 excludes[i].path += plen;
785 elen = strlen(excludes[i].path);
786 if (excludes[i].path[elen - 1] == '/')
788 excludes[i].path[elen] = '\0';
789 excludes[i].len = elen;
792 fd = open(path, O_RDONLY);
794 fprintf(stderr, "failed to open %s: %s\n", path,
800 fprintf(out_fp, "Flags: %s\n", flagstring);
803 sum(fd, 1, &cs, path, "");
808 check_manifest("", "", "", 1);
812 fprintf(stderr, "malformed input\n");
816 fprintf(out_fp, "%s:", flagstring);
818 fprintf(out_fp, "%s\n", sum_to_string(&cs));
820 if (strcmp(checksum, sum_to_string(&cs)) == 0) {