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[] = "ugoamcdtes";
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");
137 fprintf(stderr, " -r <file> : read checksum or manifest from file\n");
138 fprintf(stderr, " -[ugoamcdtes]: specify which fields to include in checksum calculation.\n");
139 fprintf(stderr, " u : include uid\n");
140 fprintf(stderr, " g : include gid\n");
141 fprintf(stderr, " o : include mode\n");
142 fprintf(stderr, " m : include mtime\n");
143 fprintf(stderr, " a : include atime\n");
144 fprintf(stderr, " c : include ctime\n");
145 fprintf(stderr, " d : include file data\n");
146 fprintf(stderr, " t : include xattrs\n");
147 fprintf(stderr, " e : include open errors (aborts otherwise)\n");
148 fprintf(stderr, " s : include block structure (holes)\n");
149 fprintf(stderr, " -[UGOAMCDTES]: exclude respective field from calculation\n");
150 fprintf(stderr, " -n : reset all flags\n");
151 fprintf(stderr, " -N : set all flags\n");
152 fprintf(stderr, " -x path : exclude path when building checksum (multiple ok)\n");
153 fprintf(stderr, " -h : this help\n\n");
154 fprintf(stderr, "The default field mask is ugoamCdtES. If the checksum/manifest is read from a\n");
155 fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
156 fprintf(stderr, "are ignored.\n");
160 static char buf[65536];
165 void *p = malloc(sz);
168 fprintf(stderr, "malloc failed\n");
184 MD5_Final(cs->out, &cs->md5);
188 sum_add(sum_t *cs, void *buf, int size)
190 MD5_Update(&cs->md5, buf, size);
194 sum_add_sum(sum_t *dst, sum_t *src)
196 sum_add(dst, src->out, sizeof(src->out));
200 sum_add_u64(sum_t *dst, uint64_t val)
202 uint64_t v = htobe64(val);
203 sum_add(dst, &v, sizeof(v));
207 sum_add_time(sum_t *dst, time_t t)
213 sum_to_string(sum_t *dst)
216 char *s = alloc(CS_SIZE * 2 + 1);
218 for (i = 0; i < CS_SIZE; ++i)
219 sprintf(s + i * 2, "%02x", dst->out[i]);
225 namecmp(const void *aa, const void *bb)
227 char * const *a = aa;
228 char * const *b = bb;
230 return strcmp(*a, *b);
234 sum_xattrs(int fd, sum_t *dst)
245 buflen = flistxattr(fd, NULL, 0);
248 /* no xattrs exist */
252 buf = malloc(buflen);
256 buflen = flistxattr(fd, buf, buflen);
263 * Keep the list of xattrs sorted, because the order in which they are
264 * listed is filesystem dependent, so we want to get the same checksum
265 * on different filesystems.
273 keylen = strlen(p) + 1; /* +1 for NULL terminator */
279 names = malloc(sizeof(char *) * num_xattrs);
286 for (i = 0; i < num_xattrs; i++) {
288 p += strlen(p) + 1; /* +1 for NULL terminator */
291 qsort(names, num_xattrs, sizeof(char *), namecmp);
293 for (i = 0; i < num_xattrs; i++) {
294 len = fgetxattr(fd, names[i], NULL, 0);
299 sum_add(dst, names[i], strlen(names[i]));
308 len = fgetxattr(fd, names[i], p, len);
314 sum_add(dst, p, len);
325 sum_file_data_permissive(int fd, sum_t *dst)
330 ret = read(fd, buf, sizeof(buf));
333 sum_add(dst, buf, ret);
334 if (ret < sizeof(buf))
341 sum_file_data_strict(int fd, sum_t *dst)
346 pos = lseek(fd, 0, SEEK_CUR);
347 if (pos == (off_t)-1)
348 return errno == ENXIO ? 0 : -2;
351 pos = lseek(fd, pos, SEEK_DATA);
352 if (pos == (off_t)-1)
353 return errno == ENXIO ? 0 : -2;
354 ret = read(fd, buf, sizeof(buf));
355 assert(ret); /* eof found by lseek */
360 "adding to sum at file offset %llu, %d bytes\n",
361 (unsigned long long)pos, ret);
362 sum_add_u64(dst, (uint64_t)pos);
363 sum_add(dst, buf, ret);
371 char *out = alloc(strlen(in) * 3 + 1);
375 for (; *src; ++src) {
376 if (*src >= 32 && *src < 127 && *src != '\\') {
379 sprintf(dst, "\\%02x", (unsigned char)*src);
389 excess_file(const char *fn)
391 printf("only in local fs: %s\n", fn);
395 missing_file(const char *fn)
397 printf("only in remote fs: %s\n", fn);
401 pathcmp(const char *a, const char *b)
403 int len_a = strlen(a);
404 int len_b = strlen(b);
407 * as the containing directory is sent after the files, it has to
408 * come out bigger in the comparison.
410 if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
412 if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
419 check_match(char *fn, char *local_m, char *remote_m,
420 char *local_c, char *remote_c)
422 int match_m = !strcmp(local_m, remote_m);
423 int match_c = !strcmp(local_c, remote_c);
425 if (match_m && !match_c) {
426 printf("data mismatch in %s\n", fn);
427 } else if (!match_m && match_c) {
428 printf("metadata mismatch in %s\n", fn);
429 } else if (!match_m && !match_c) {
430 printf("metadata and data mismatch in %s\n", fn);
438 check_manifest(char *fn, char *m, char *c, int last_call)
449 cmp = pathcmp(prev_fn, fn);
453 } else if (cmp < 0) {
454 missing_file(prev_fn);
456 check_match(fn, m, prev_m, c, prev_c);
467 while ((l = getln(line, sizeof(line), in_fp))) {
468 rem_c = strrchr(l, ' ');
471 checksum = strdup(l);
476 fprintf(stderr, "malformed input\n");
480 rem_m = strrchr(l, ' ');
488 cmp = pathcmp(l, fn);
490 check_match(fn, m, rem_m, c, rem_c);
492 } else if (cmp > 0) {
495 prev_m = strdup(rem_m);
496 prev_c = strdup(rem_c);
506 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
510 char **namelist = NULL;
517 sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
518 sum_file_data_strict : sum_file_data_permissive;
519 struct stat64 dir_st;
521 if (fstat64(dirfd, &dir_st)) {
526 d = fdopendir(dirfd);
531 while((de = readdir(d))) {
532 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
534 if (entries == alloclen) {
536 namelist = realloc(namelist,
537 alloclen * sizeof(*namelist));
539 fprintf(stderr, "malloc failed\n");
543 namelist[entries] = strdup(de->d_name);
544 if (!namelist[entries]) {
545 fprintf(stderr, "malloc failed\n");
550 qsort(namelist, entries, sizeof(*namelist), namecmp);
551 for (i = 0; i < entries; ++i) {
559 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
560 sprintf(path, "%s/%s", path_in, namelist[i]);
561 for (excl = 0; excl < n_excludes; ++excl) {
562 if (strncmp(excludes[excl].path, path,
563 excludes[excl].len) == 0)
572 ret = lstat64(namelist[i], &st);
574 fprintf(stderr, "stat failed for %s/%s: %s\n",
575 path_prefix, path, strerror(errno));
579 /* We are crossing into a different subvol, skip this subtree. */
580 if (st.st_dev != dir_st.st_dev)
583 sum_add_u64(&meta, level);
584 sum_add(&meta, namelist[i], strlen(namelist[i]));
585 if (!S_ISDIR(st.st_mode))
586 sum_add_u64(&meta, st.st_nlink);
588 sum_add_u64(&meta, st.st_uid);
590 sum_add_u64(&meta, st.st_gid);
591 if (flags[FLAG_MODE])
592 sum_add_u64(&meta, st.st_mode);
593 if (flags[FLAG_ATIME])
594 sum_add_time(&meta, st.st_atime);
595 if (flags[FLAG_MTIME])
596 sum_add_time(&meta, st.st_mtime);
597 if (flags[FLAG_CTIME])
598 sum_add_time(&meta, st.st_ctime);
599 if (flags[FLAG_XATTRS] &&
600 (S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
601 fd = openat(dirfd, namelist[i], 0);
602 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
603 sum_add_u64(&meta, errno);
604 } else if (fd == -1) {
605 fprintf(stderr, "open failed for %s/%s: %s\n",
606 path_prefix, path, strerror(errno));
609 ret = sum_xattrs(fd, &meta);
613 "failed to read xattrs from "
621 if (S_ISDIR(st.st_mode)) {
622 fd = openat(dirfd, namelist[i], 0);
623 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
624 sum_add_u64(&meta, errno);
625 } else if (fd == -1) {
626 fprintf(stderr, "open failed for %s/%s: %s\n",
627 path_prefix, path, strerror(errno));
630 sum(fd, level + 1, &cs, path_prefix, path);
633 } else if (S_ISREG(st.st_mode)) {
634 sum_add_u64(&meta, st.st_size);
635 if (flags[FLAG_DATA]) {
637 fprintf(stderr, "file %s\n",
639 fd = openat(dirfd, namelist[i], 0);
640 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
641 sum_add_u64(&meta, errno);
642 } else if (fd == -1) {
644 "open failed for %s/%s: %s\n",
650 ret = sum_file_data(fd, &cs);
662 } else if (S_ISLNK(st.st_mode)) {
663 ret = readlink(namelist[i], buf, sizeof(buf));
668 sum_add(&cs, buf, ret);
669 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
670 sum_add_u64(&cs, major(st.st_rdev));
671 sum_add_u64(&cs, minor(st.st_rdev));
675 if (gen_manifest || in_manifest) {
680 if (S_ISDIR(st.st_mode))
683 m = sum_to_string(&meta);
684 c = sum_to_string(&cs);
687 fprintf(out_fp, "%s %s %s\n", fn, m, c);
689 check_manifest(fn, m, c, 0);
694 sum_add_sum(dircs, &cs);
695 sum_add_sum(dircs, &meta);
702 main(int argc, char *argv[])
710 char flagstring[sizeof(flchar)];
715 const char *allopts = "heEfuUgGoOaAmMcCdDtTsSnNw:r:vx:";
718 while ((c = getopt(argc, argv, allopts)) != EOF) {
747 for (i = 0; i < NUM_FLAGS; ++i)
751 for (i = 0; i < NUM_FLAGS; ++i)
755 out_fp = fopen(optarg, "w");
758 "failed to open output file: %s\n",
764 in_fp = fopen(optarg, "r");
767 "failed to open input file: %s\n",
774 excludes = realloc(excludes,
775 sizeof(*excludes) * n_excludes);
778 "failed to alloc exclude space\n");
781 excludes[n_excludes - 1].path = optarg;
792 if (optind + 1 != argc) {
793 fprintf(stderr, "missing path\n");
798 char *l = getln(line, sizeof(line), in_fp);
802 fprintf(stderr, "failed to read line from input\n");
805 if (strncmp(l, "Flags: ", 7) == 0) {
809 } else if ((p = strchr(l, ':'))) {
812 checksum = strdup(p);
814 fprintf(stderr, "invalid input file format\n");
818 fprintf(stderr, "warning: "
819 "command line flags ignored in -r mode\n");
821 strcpy(flagstring, flchar);
822 for (i = 0; i < NUM_FLAGS; ++i) {
824 flagstring[i] -= 'a' - 'A';
829 if (path[plen - 1] == '/') {
834 for (i = 0; i < n_excludes; ++i) {
835 if (strncmp(path, excludes[i].path, plen) != 0)
837 "warning: exclude %s outside of path %s\n",
838 excludes[i].path, path);
840 excludes[i].path += plen;
841 elen = strlen(excludes[i].path);
842 if (excludes[i].path[elen - 1] == '/')
844 excludes[i].path[elen] = '\0';
845 excludes[i].len = elen;
848 fd = open(path, O_RDONLY);
850 fprintf(stderr, "failed to open %s: %s\n", path,
856 fprintf(out_fp, "Flags: %s\n", flagstring);
859 sum(fd, 1, &cs, path, "");
864 check_manifest("", "", "", 1);
868 fprintf(stderr, "malformed input\n");
872 fprintf(out_fp, "%s:", flagstring);
874 fprintf(out_fp, "%s\n", sum_to_string(&cs));
876 if (strcmp(checksum, sum_to_string(&cs)) == 0) {