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.
19 #include "kerncompat.h"
31 #include "tests/sha.h"
41 /* TODO: add hardlink recognition */
42 /* TODO: add xattr/acl */
51 unsigned char out[CS_SIZE];
54 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
58 char *checksum = NULL;
59 struct excludes *excludes;
78 const char flchar[] = "ugoamcdes";
81 int flags[NUM_FLAGS] = {1, 1, 1, 1, 1, 0, 1, 0, 0};
84 getln(char *buf, int size, FILE *fp)
89 p = fgets(buf, size, fp);
94 while(l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r'))
106 if (c >= 'A' && c <= 'Z') {
110 for (i = 0; flchar[i]; ++i) {
111 if (flchar[i] == c) {
112 flags[i] = is_upper ? 0 : 1;
116 fprintf(stderr, "unrecognized flag %c\n", c);
130 fprintf(stderr, "usage: fssum <options> <path>\n");
131 fprintf(stderr, " options:\n");
132 fprintf(stderr, " -f : write out a full manifest file\n");
133 fprintf(stderr, " -w <file> : send output to file\n");
134 fprintf(stderr, " -v : verbose mode (debugging only)\n");
136 " -r <file> : read checksum or manifest from file\n");
137 fprintf(stderr, " -[ugoamcde] : specify which fields to include in checksum calculation.\n");
138 fprintf(stderr, " u : include uid\n");
139 fprintf(stderr, " g : include gid\n");
140 fprintf(stderr, " o : include mode\n");
141 fprintf(stderr, " m : include mtime\n");
142 fprintf(stderr, " a : include atime\n");
143 fprintf(stderr, " c : include ctime\n");
144 fprintf(stderr, " d : include file data\n");
145 fprintf(stderr, " e : include open errors (aborts otherwise)\n");
146 fprintf(stderr, " s : include block structure (holes)\n");
147 fprintf(stderr, " -[UGOAMCDES]: exclude respective field from calculation\n");
148 fprintf(stderr, " -n : reset all flags\n");
149 fprintf(stderr, " -N : set all flags\n");
150 fprintf(stderr, " -x path : exclude path when building checksum (multiple ok)\n");
151 fprintf(stderr, " -h : this help\n\n");
152 fprintf(stderr, "The default field mask is ugoamCdES. If the checksum/manifest is read from a\n");
153 fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
154 fprintf(stderr, "are ignored.\n");
158 static char buf[65536];
163 void *p = malloc(sz);
166 fprintf(stderr, "malloc failed\n");
176 SHA256Reset(&cs->sha);
182 SHA256Result(&cs->sha, cs->out);
186 sum_add(sum_t *cs, void *buf, int size)
188 SHA256Input(&cs->sha, buf, size);
192 sum_add_sum(sum_t *dst, sum_t *src)
194 sum_add(dst, src->out, sizeof(src->out));
198 sum_add_u64(sum_t *dst, uint64_t val)
200 uint64_t v = cpu_to_le64(val);
201 sum_add(dst, &v, sizeof(v));
205 sum_add_time(sum_t *dst, time_t t)
211 sum_to_string(sum_t *dst)
214 char *s = alloc(CS_SIZE * 2 + 1);
216 for (i = 0; i < CS_SIZE; ++i)
217 sprintf(s + i * 2, "%02x", dst->out[i]);
223 sum_file_data_permissive(int fd, sum_t *dst)
231 pos = lseek(fd, 0, SEEK_CUR);
232 if (pos == (off_t)-1)
233 return errno == ENXIO ? 0 : -2;
237 pos = lseek(fd, pos, SEEK_DATA);
238 if (pos == (off_t)-1) {
239 if (errno == ENXIO) {
241 pos = lseek(fd, 0, SEEK_END);
242 if (pos != (off_t)-1)
249 ret = read(fd, buf, sizeof(buf));
250 assert(ret); /* eof found by lseek */
253 if (old < pos) /* hole */
255 for (i = 0; i < ret; ++i) {
256 for (old = i; buf[i] == 0 && i < ret; ++i)
258 if (old < i) /* code like a hole */
265 "adding %llu zeros to sum\n",
266 (unsigned long long)zeros);
268 sum_add_u64(dst, zeros);
271 for (old = i; buf[i] != 0 && i < ret; ++i)
274 fprintf(stderr, "adding %u non-zeros to sum\n",
276 sum_add(dst, buf + old, i - old);
284 "adding %llu zeros to sum (finishing)\n",
285 (unsigned long long)zeros);
287 sum_add_u64(dst, zeros);
294 sum_file_data_strict(int fd, sum_t *dst)
299 pos = lseek(fd, 0, SEEK_CUR);
300 if (pos == (off_t)-1)
301 return errno == ENXIO ? 0 : -2;
304 pos = lseek(fd, pos, SEEK_DATA);
305 if (pos == (off_t)-1)
306 return errno == ENXIO ? 0 : -2;
307 ret = read(fd, buf, sizeof(buf));
308 assert(ret); /* eof found by lseek */
313 "adding to sum at file offset %llu, %d bytes\n",
314 (unsigned long long)pos, ret);
315 sum_add_u64(dst, (uint64_t)pos);
316 sum_add(dst, buf, ret);
324 char *out = alloc(strlen(in) * 3 + 1);
328 for (; *src; ++src) {
329 if (*src >= 32 && *src < 127 && *src != '\\') {
332 sprintf(dst, "\\%02x", (unsigned char)*src);
342 excess_file(const char *fn)
344 printf("only in local fs: %s\n", fn);
348 missing_file(const char *fn)
350 printf("only in remote fs: %s\n", fn);
354 pathcmp(const char *a, const char *b)
356 int len_a = strlen(a);
357 int len_b = strlen(b);
360 * as the containing directory is sent after the files, it has to
361 * come out bigger in the comparison.
363 if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
365 if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
372 check_match(char *fn, char *local_m, char *remote_m,
373 char *local_c, char *remote_c)
375 int match_m = !strcmp(local_m, remote_m);
376 int match_c = !strcmp(local_c, remote_c);
378 if (match_m && !match_c) {
379 printf("data mismatch in %s\n", fn);
380 } else if (!match_m && match_c) {
381 printf("metadata mismatch in %s\n", fn);
382 } else if (!match_m && !match_c) {
383 printf("metadata and data mismatch in %s\n", fn);
391 check_manifest(char *fn, char *m, char *c, int last_call)
402 cmp = pathcmp(prev_fn, fn);
406 } else if (cmp < 0) {
407 missing_file(prev_fn);
409 check_match(fn, m, prev_m, c, prev_c);
420 while ((l = getln(line, sizeof(line), in_fp))) {
421 rem_c = strrchr(l, ' ');
424 checksum = strdup(l);
429 fprintf(stderr, "malformed input\n");
433 rem_m = strrchr(l, ' ');
441 cmp = pathcmp(l, fn);
443 check_match(fn, m, rem_m, c, rem_c);
445 } else if (cmp > 0) {
448 prev_m = strdup(rem_m);
449 prev_c = strdup(rem_c);
459 namecmp(const void *aa, const void *bb)
461 char * const *a = aa;
462 char * const *b = bb;
464 return strcmp(*a, *b);
468 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
472 char **namelist = NULL;
479 sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
480 sum_file_data_strict : sum_file_data_permissive;
482 d = fdopendir(dirfd);
487 while((de = readdir(d))) {
488 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
490 if (entries == alloclen) {
492 namelist = realloc(namelist,
493 alloclen * sizeof(*namelist));
495 fprintf(stderr, "malloc failed\n");
499 namelist[entries] = strdup(de->d_name);
500 if (!namelist[entries]) {
501 fprintf(stderr, "malloc failed\n");
506 qsort(namelist, entries, sizeof(*namelist), namecmp);
507 for (i = 0; i < entries; ++i) {
515 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
516 sprintf(path, "%s/%s", path_in, namelist[i]);
517 for (excl = 0; excl < n_excludes; ++excl) {
518 if (strncmp(excludes[excl].path, path,
519 excludes[excl].len) == 0)
528 ret = lstat64(namelist[i], &st);
530 fprintf(stderr, "stat failed for %s/%s: %s\n",
531 path_prefix, path, strerror(errno));
534 sum_add_u64(&meta, level);
535 sum_add(&meta, namelist[i], strlen(namelist[i]));
536 if (!S_ISDIR(st.st_mode))
537 sum_add_u64(&meta, st.st_nlink);
539 sum_add_u64(&meta, st.st_uid);
541 sum_add_u64(&meta, st.st_gid);
542 if (flags[FLAG_MODE])
543 sum_add_u64(&meta, st.st_mode);
544 if (flags[FLAG_ATIME])
545 sum_add_time(&meta, st.st_atime);
546 if (flags[FLAG_MTIME])
547 sum_add_time(&meta, st.st_mtime);
548 if (flags[FLAG_CTIME])
549 sum_add_time(&meta, st.st_ctime);
550 if (S_ISDIR(st.st_mode)) {
551 fd = openat(dirfd, namelist[i], 0);
552 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
553 sum_add_u64(&meta, errno);
554 } else if (fd == -1) {
555 fprintf(stderr, "open failed for %s/%s: %s\n",
556 path_prefix, path, strerror(errno));
559 sum(fd, level + 1, &cs, path_prefix, path);
562 } else if (S_ISREG(st.st_mode)) {
563 sum_add_u64(&meta, st.st_size);
564 if (flags[FLAG_DATA]) {
566 fprintf(stderr, "file %s\n",
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) {
573 "open failed for %s/%s: %s\n",
579 ret = sum_file_data(fd, &cs);
591 } else if (S_ISLNK(st.st_mode)) {
592 ret = readlink(namelist[i], buf, sizeof(buf));
597 sum_add(&cs, buf, ret);
598 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
599 sum_add_u64(&cs, major(st.st_rdev));
600 sum_add_u64(&cs, minor(st.st_rdev));
604 if (gen_manifest || in_manifest) {
609 if (S_ISDIR(st.st_mode))
612 m = sum_to_string(&meta);
613 c = sum_to_string(&cs);
616 fprintf(out_fp, "%s %s %s\n", fn, m, c);
618 check_manifest(fn, m, c, 0);
623 sum_add_sum(dircs, &cs);
624 sum_add_sum(dircs, &meta);
631 main(int argc, char *argv[])
639 char flagstring[sizeof(flchar)];
644 const char *allopts = "heEfuUgGoOaAmMcCdDsSnNw:r:vx:";
647 while ((c = getopt(argc, argv, allopts)) != EOF) {
674 for (i = 0; i < NUM_FLAGS; ++i)
678 for (i = 0; i < NUM_FLAGS; ++i)
682 out_fp = fopen(optarg, "w");
685 "failed to open output file: %s\n",
691 in_fp = fopen(optarg, "r");
694 "failed to open input file: %s\n",
701 excludes = realloc(excludes,
702 sizeof(*excludes) * n_excludes);
705 "failed to alloc exclude space\n");
708 excludes[n_excludes - 1].path = optarg;
719 if (optind + 1 != argc) {
720 fprintf(stderr, "missing path\n");
725 char *l = getln(line, sizeof(line), in_fp);
729 fprintf(stderr, "failed to read line from input\n");
732 if (strncmp(l, "Flags: ", 7) == 0) {
736 } else if ((p = strchr(l, ':'))) {
739 checksum = strdup(p);
741 fprintf(stderr, "invalid input file format\n");
745 fprintf(stderr, "warning: "
746 "command line flags ignored in -r mode\n");
748 strcpy(flagstring, flchar);
749 for (i = 0; i < NUM_FLAGS; ++i) {
751 flagstring[i] -= 'a' - 'A';
756 if (path[plen - 1] == '/') {
761 for (i = 0; i < n_excludes; ++i) {
762 if (strncmp(path, excludes[i].path, plen) != 0)
764 "warning: exclude %s outside of path %s\n",
765 excludes[i].path, path);
767 excludes[i].path += plen;
768 elen = strlen(excludes[i].path);
769 if (excludes[i].path[elen - 1] == '/')
771 excludes[i].path[elen] = '\0';
772 excludes[i].len = elen;
775 fd = open(path, O_RDONLY);
777 fprintf(stderr, "failed to open %s: %s\n", path,
783 fprintf(out_fp, "Flags: %s\n", flagstring);
786 sum(fd, 1, &cs, path, "");
791 check_manifest("", "", "", 1);
795 fprintf(stderr, "malformed input\n");
799 fprintf(out_fp, "%s:", flagstring);
801 fprintf(out_fp, "%s\n", sum_to_string(&cs));
803 if (strcmp(checksum, sum_to_string(&cs)) == 0) {