1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012 STRATO AG. All rights reserved.
7 #define _DEFAULT_SOURCE
8 #define _LARGEFILE64_SOURCE
20 #include <sys/types.h>
22 #include <sys/sysmacros.h>
23 #include <sys/xattr.h>
25 #include <sys/mkdev.h>
28 #include <netinet/in.h>
43 /* TODO: add hardlink recognition */
52 unsigned char out[16];
55 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
59 char *checksum = NULL;
60 struct excludes *excludes;
80 const char flchar[] = "ugoamcdxes";
83 int flags[NUM_FLAGS] = {1, 1, 1, 1, 1, 0, 1, 1, 0, 0};
86 getln(char *buf, int size, FILE *fp)
91 p = fgets(buf, size, fp);
96 while(l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r'))
108 if (c >= 'A' && c <= 'Z') {
112 for (i = 0; flchar[i]; ++i) {
113 if (flchar[i] == c) {
114 flags[i] = is_upper ? 0 : 1;
118 fprintf(stderr, "unrecognized flag %c\n", c);
132 fprintf(stderr, "usage: fssum <options> <path>\n");
133 fprintf(stderr, " options:\n");
134 fprintf(stderr, " -f : write out a full manifest file\n");
135 fprintf(stderr, " -w <file> : send output to file\n");
136 fprintf(stderr, " -v : verbose mode (debugging only)\n");
138 " -r <file> : read checksum or manifest from file\n");
139 fprintf(stderr, " -[ugoamcdxe]: specify which fields to include in checksum calculation.\n");
140 fprintf(stderr, " u : include uid\n");
141 fprintf(stderr, " g : include gid\n");
142 fprintf(stderr, " o : include mode\n");
143 fprintf(stderr, " m : include mtime\n");
144 fprintf(stderr, " a : include atime\n");
145 fprintf(stderr, " c : include ctime\n");
146 fprintf(stderr, " d : include file data\n");
147 fprintf(stderr, " x : include xattrs\n");
148 fprintf(stderr, " e : include open errors (aborts otherwise)\n");
149 fprintf(stderr, " s : include block structure (holes)\n");
150 fprintf(stderr, " -[UGOAMCDXES]: exclude respective field from calculation\n");
151 fprintf(stderr, " -n : reset all flags\n");
152 fprintf(stderr, " -N : set all flags\n");
153 fprintf(stderr, " -x path : exclude path when building checksum (multiple ok)\n");
154 fprintf(stderr, " -h : this help\n\n");
155 fprintf(stderr, "The default field mask is ugoamCdES. If the checksum/manifest is read from a\n");
156 fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
157 fprintf(stderr, "are ignored.\n");
161 static char buf[65536];
166 void *p = malloc(sz);
169 fprintf(stderr, "malloc failed\n");
185 MD5_Final(cs->out, &cs->md5);
189 sum_add(sum_t *cs, void *buf, int size)
191 MD5_Update(&cs->md5, buf, size);
195 sum_add_sum(sum_t *dst, sum_t *src)
197 sum_add(dst, src->out, sizeof(src->out));
201 sum_add_u64(sum_t *dst, uint64_t val)
203 uint64_t v = htobe64(val);
204 sum_add(dst, &v, sizeof(v));
208 sum_add_time(sum_t *dst, time_t t)
214 sum_to_string(sum_t *dst)
217 char *s = alloc(CS_SIZE * 2 + 1);
219 for (i = 0; i < CS_SIZE; ++i)
220 sprintf(s + i * 2, "%02x", dst->out[i]);
226 namecmp(const void *aa, const void *bb)
228 char * const *a = aa;
229 char * const *b = bb;
231 return strcmp(*a, *b);
235 sum_xattrs(int fd, sum_t *dst)
246 buflen = flistxattr(fd, NULL, 0);
249 /* no xattrs exist */
253 buf = malloc(buflen);
257 buflen = flistxattr(fd, buf, buflen);
264 * Keep the list of xattrs sorted, because the order in which they are
265 * listed is filesystem dependent, so we want to get the same checksum
266 * on different filesystems.
274 keylen = strlen(p) + 1; /* +1 for NULL terminator */
280 names = malloc(sizeof(char *) * num_xattrs);
287 for (i = 0; i < num_xattrs; i++) {
289 p += strlen(p) + 1; /* +1 for NULL terminator */
292 qsort(names, num_xattrs, sizeof(char *), namecmp);
294 for (i = 0; i < num_xattrs; i++) {
295 len = fgetxattr(fd, names[i], NULL, 0);
300 sum_add(dst, names[i], strlen(names[i]));
309 len = fgetxattr(fd, names[i], p, len);
315 sum_add(dst, p, len);
326 sum_file_data_permissive(int fd, sum_t *dst)
331 ret = read(fd, buf, sizeof(buf));
334 sum_add(dst, buf, ret);
335 if (ret < sizeof(buf))
342 sum_file_data_strict(int fd, sum_t *dst)
347 pos = lseek(fd, 0, SEEK_CUR);
348 if (pos == (off_t)-1)
349 return errno == ENXIO ? 0 : -2;
352 pos = lseek(fd, pos, SEEK_DATA);
353 if (pos == (off_t)-1)
354 return errno == ENXIO ? 0 : -2;
355 ret = read(fd, buf, sizeof(buf));
356 assert(ret); /* eof found by lseek */
361 "adding to sum at file offset %llu, %d bytes\n",
362 (unsigned long long)pos, ret);
363 sum_add_u64(dst, (uint64_t)pos);
364 sum_add(dst, buf, ret);
372 char *out = alloc(strlen(in) * 3 + 1);
376 for (; *src; ++src) {
377 if (*src >= 32 && *src < 127 && *src != '\\') {
380 sprintf(dst, "\\%02x", (unsigned char)*src);
390 excess_file(const char *fn)
392 printf("only in local fs: %s\n", fn);
396 missing_file(const char *fn)
398 printf("only in remote fs: %s\n", fn);
402 pathcmp(const char *a, const char *b)
404 int len_a = strlen(a);
405 int len_b = strlen(b);
408 * as the containing directory is sent after the files, it has to
409 * come out bigger in the comparison.
411 if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
413 if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
420 check_match(char *fn, char *local_m, char *remote_m,
421 char *local_c, char *remote_c)
423 int match_m = !strcmp(local_m, remote_m);
424 int match_c = !strcmp(local_c, remote_c);
426 if (match_m && !match_c) {
427 printf("data mismatch in %s\n", fn);
428 } else if (!match_m && match_c) {
429 printf("metadata mismatch in %s\n", fn);
430 } else if (!match_m && !match_c) {
431 printf("metadata and data mismatch in %s\n", fn);
439 check_manifest(char *fn, char *m, char *c, int last_call)
450 cmp = pathcmp(prev_fn, fn);
454 } else if (cmp < 0) {
455 missing_file(prev_fn);
457 check_match(fn, m, prev_m, c, prev_c);
468 while ((l = getln(line, sizeof(line), in_fp))) {
469 rem_c = strrchr(l, ' ');
472 checksum = strdup(l);
477 fprintf(stderr, "malformed input\n");
481 rem_m = strrchr(l, ' ');
489 cmp = pathcmp(l, fn);
491 check_match(fn, m, rem_m, c, rem_c);
493 } else if (cmp > 0) {
496 prev_m = strdup(rem_m);
497 prev_c = strdup(rem_c);
507 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
511 char **namelist = NULL;
518 sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
519 sum_file_data_strict : sum_file_data_permissive;
521 d = fdopendir(dirfd);
526 while((de = readdir(d))) {
527 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
529 if (entries == alloclen) {
531 namelist = realloc(namelist,
532 alloclen * sizeof(*namelist));
534 fprintf(stderr, "malloc failed\n");
538 namelist[entries] = strdup(de->d_name);
539 if (!namelist[entries]) {
540 fprintf(stderr, "malloc failed\n");
545 qsort(namelist, entries, sizeof(*namelist), namecmp);
546 for (i = 0; i < entries; ++i) {
554 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
555 sprintf(path, "%s/%s", path_in, namelist[i]);
556 for (excl = 0; excl < n_excludes; ++excl) {
557 if (strncmp(excludes[excl].path, path,
558 excludes[excl].len) == 0)
567 ret = lstat64(namelist[i], &st);
569 fprintf(stderr, "stat failed for %s/%s: %s\n",
570 path_prefix, path, strerror(errno));
573 sum_add_u64(&meta, level);
574 sum_add(&meta, namelist[i], strlen(namelist[i]));
575 if (!S_ISDIR(st.st_mode))
576 sum_add_u64(&meta, st.st_nlink);
578 sum_add_u64(&meta, st.st_uid);
580 sum_add_u64(&meta, st.st_gid);
581 if (flags[FLAG_MODE])
582 sum_add_u64(&meta, st.st_mode);
583 if (flags[FLAG_ATIME])
584 sum_add_time(&meta, st.st_atime);
585 if (flags[FLAG_MTIME])
586 sum_add_time(&meta, st.st_mtime);
587 if (flags[FLAG_CTIME])
588 sum_add_time(&meta, st.st_ctime);
589 if (flags[FLAG_XATTRS] &&
590 (S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
591 fd = openat(dirfd, namelist[i], 0);
592 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
593 sum_add_u64(&meta, errno);
594 } else if (fd == -1) {
595 fprintf(stderr, "open failed for %s/%s: %s\n",
596 path_prefix, path, strerror(errno));
599 ret = sum_xattrs(fd, &meta);
603 "failed to read xattrs from "
611 if (S_ISDIR(st.st_mode)) {
612 fd = openat(dirfd, namelist[i], 0);
613 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
614 sum_add_u64(&meta, errno);
615 } else if (fd == -1) {
616 fprintf(stderr, "open failed for %s/%s: %s\n",
617 path_prefix, path, strerror(errno));
620 sum(fd, level + 1, &cs, path_prefix, path);
623 } else if (S_ISREG(st.st_mode)) {
624 sum_add_u64(&meta, st.st_size);
625 if (flags[FLAG_DATA]) {
627 fprintf(stderr, "file %s\n",
629 fd = openat(dirfd, namelist[i], 0);
630 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
631 sum_add_u64(&meta, errno);
632 } else if (fd == -1) {
634 "open failed for %s/%s: %s\n",
640 ret = sum_file_data(fd, &cs);
652 } else if (S_ISLNK(st.st_mode)) {
653 ret = readlink(namelist[i], buf, sizeof(buf));
658 sum_add(&cs, buf, ret);
659 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
660 sum_add_u64(&cs, major(st.st_rdev));
661 sum_add_u64(&cs, minor(st.st_rdev));
665 if (gen_manifest || in_manifest) {
670 if (S_ISDIR(st.st_mode))
673 m = sum_to_string(&meta);
674 c = sum_to_string(&cs);
677 fprintf(out_fp, "%s %s %s\n", fn, m, c);
679 check_manifest(fn, m, c, 0);
684 sum_add_sum(dircs, &cs);
685 sum_add_sum(dircs, &meta);
692 main(int argc, char *argv[])
700 char flagstring[sizeof(flchar)];
705 const char *allopts = "heEfuUgGoOaAmMcCdDsSnNw:r:vx:";
708 while ((c = getopt(argc, argv, allopts)) != EOF) {
735 for (i = 0; i < NUM_FLAGS; ++i)
739 for (i = 0; i < NUM_FLAGS; ++i)
743 out_fp = fopen(optarg, "w");
746 "failed to open output file: %s\n",
752 in_fp = fopen(optarg, "r");
755 "failed to open input file: %s\n",
762 excludes = realloc(excludes,
763 sizeof(*excludes) * n_excludes);
766 "failed to alloc exclude space\n");
769 excludes[n_excludes - 1].path = optarg;
780 if (optind + 1 != argc) {
781 fprintf(stderr, "missing path\n");
786 char *l = getln(line, sizeof(line), in_fp);
790 fprintf(stderr, "failed to read line from input\n");
793 if (strncmp(l, "Flags: ", 7) == 0) {
797 } else if ((p = strchr(l, ':'))) {
800 checksum = strdup(p);
802 fprintf(stderr, "invalid input file format\n");
806 fprintf(stderr, "warning: "
807 "command line flags ignored in -r mode\n");
809 strcpy(flagstring, flchar);
810 for (i = 0; i < NUM_FLAGS; ++i) {
812 flagstring[i] -= 'a' - 'A';
817 if (path[plen - 1] == '/') {
822 for (i = 0; i < n_excludes; ++i) {
823 if (strncmp(path, excludes[i].path, plen) != 0)
825 "warning: exclude %s outside of path %s\n",
826 excludes[i].path, path);
828 excludes[i].path += plen;
829 elen = strlen(excludes[i].path);
830 if (excludes[i].path[elen - 1] == '/')
832 excludes[i].path[elen] = '\0';
833 excludes[i].len = elen;
836 fd = open(path, O_RDONLY);
838 fprintf(stderr, "failed to open %s: %s\n", path,
844 fprintf(out_fp, "Flags: %s\n", flagstring);
847 sum(fd, 1, &cs, path, "");
852 check_manifest("", "", "", 1);
856 fprintf(stderr, "malformed input\n");
860 fprintf(out_fp, "%s:", flagstring);
862 fprintf(out_fp, "%s\n", sum_to_string(&cs));
864 if (strcmp(checksum, sum_to_string(&cs)) == 0) {