btrfs-progs: tests: fssum, drop *_SOURCE defines, already provided via config.h
[platform/upstream/btrfs-progs.git] / tests / fssum.c
1 /*
2  * Copyright (C) 2012 STRATO AG.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include "kerncompat.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <openssl/md5.h>
30 #include <netinet/in.h>
31 #include <inttypes.h>
32 #include <assert.h>
33
34 #define CS_SIZE 16
35 #define CHUNKS  128
36
37 #ifndef SEEK_DATA
38 #define SEEK_DATA 3
39 #define SEEK_HOLE 4
40 #endif
41
42 /* TODO: add hardlink recognition */
43 /* TODO: add xattr/acl */
44
45 struct excludes {
46         char *path;
47         int len;
48 };
49
50 typedef struct _sum {
51         MD5_CTX         md5;
52         unsigned char   out[16];
53 } sum_t;
54
55 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
56
57 int gen_manifest = 0;
58 int in_manifest = 0;
59 char *checksum = NULL;
60 struct excludes *excludes;
61 int n_excludes = 0;
62 int verbose = 0;
63 FILE *out_fp;
64 FILE *in_fp;
65
66 enum _flags {
67         FLAG_UID,
68         FLAG_GID,
69         FLAG_MODE,
70         FLAG_ATIME,
71         FLAG_MTIME,
72         FLAG_CTIME,
73         FLAG_DATA,
74         FLAG_OPEN_ERROR,
75         FLAG_STRUCTURE,
76         NUM_FLAGS
77 };
78
79 const char flchar[] = "ugoamcdes";
80 char line[65536];
81
82 int flags[NUM_FLAGS] = {1, 1, 1, 1, 1, 0, 1, 0, 0};
83
84 char *
85 getln(char *buf, int size, FILE *fp)
86 {
87         char *p;
88         int l;
89
90         p = fgets(buf, size, fp);
91         if (!p)
92                 return NULL;
93
94         l = strlen(p);
95         while(l > 0  && (p[l - 1] == '\n' || p[l - 1] == '\r'))
96                 p[--l] = 0;
97
98         return p;
99 }
100
101 void
102 parse_flag(int c)
103 {
104         int i;
105         int is_upper = 0;
106
107         if (c >= 'A' && c <= 'Z') {
108                 is_upper = 1;
109                 c += 'a' - 'A';
110         }
111         for (i = 0; flchar[i]; ++i) {
112                 if (flchar[i] == c) {
113                         flags[i] = is_upper ? 0 : 1;
114                         return;
115                 }
116         }
117         fprintf(stderr, "unrecognized flag %c\n", c);
118         exit(-1);
119 }
120
121 void
122 parse_flags(char *p)
123 {
124         while (*p)
125                 parse_flag(*p++);
126 }
127
128 void
129 usage(void)
130 {
131         fprintf(stderr, "usage: fssum <options> <path>\n");
132         fprintf(stderr, "  options:\n");
133         fprintf(stderr, "    -f          : write out a full manifest file\n");
134         fprintf(stderr, "    -w <file>   : send output to file\n");
135         fprintf(stderr, "    -v          : verbose mode (debugging only)\n");
136         fprintf(stderr,
137                 "    -r <file>   : read checksum or manifest from file\n");
138         fprintf(stderr, "    -[ugoamcde] : 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, "         e      : include open errors (aborts otherwise)\n");
147         fprintf(stderr, "         s      : include block structure (holes)\n");
148         fprintf(stderr, "    -[UGOAMCDES]: exclude respective field from calculation\n");
149         fprintf(stderr, "    -n          : reset all flags\n");
150         fprintf(stderr, "    -N          : set all flags\n");
151         fprintf(stderr, "    -x path     : exclude path when building checksum (multiple ok)\n");
152         fprintf(stderr, "    -h          : this help\n\n");
153         fprintf(stderr, "The default field mask is ugoamCdES. If the checksum/manifest is read from a\n");
154         fprintf(stderr, "file, the mask is taken from there and the values given on the command line\n");
155         fprintf(stderr, "are ignored.\n");
156         exit(-1);
157 }
158
159 static char buf[65536];
160
161 void *
162 alloc(size_t sz)
163 {
164         void *p = malloc(sz);
165
166         if (!p) {
167                 fprintf(stderr, "malloc failed\n");
168                 exit(-1);
169         }
170
171         return p;
172 }
173
174 void
175 sum_init(sum_t *cs)
176 {
177         MD5_Init(&cs->md5);
178 }
179
180 void
181 sum_fini(sum_t *cs)
182 {
183         MD5_Final(cs->out, &cs->md5);
184 }
185
186 void
187 sum_add(sum_t *cs, void *buf, int size)
188 {
189         MD5_Update(&cs->md5, buf, size);
190 }
191
192 void
193 sum_add_sum(sum_t *dst, sum_t *src)
194 {
195         sum_add(dst, src->out, sizeof(src->out));
196 }
197
198 void
199 sum_add_u64(sum_t *dst, uint64_t val)
200 {
201         uint64_t v = cpu_to_le64(val);
202         sum_add(dst, &v, sizeof(v));
203 }
204
205 void
206 sum_add_time(sum_t *dst, time_t t)
207 {
208         sum_add_u64(dst, t);
209 }
210
211 char *
212 sum_to_string(sum_t *dst)
213 {
214         int i;
215         char *s = alloc(CS_SIZE * 2 + 1);
216
217         for (i = 0; i < CS_SIZE; ++i)
218                 sprintf(s + i * 2, "%02x", dst->out[i]);
219
220         return s;
221 }
222
223 int
224 sum_file_data_permissive(int fd, sum_t *dst)
225 {
226         int ret;
227         off_t pos;
228         off_t old;
229         int i;
230         uint64_t zeros = 0;
231
232         pos = lseek(fd, 0, SEEK_CUR);
233         if (pos == (off_t)-1)
234                 return errno == ENXIO ? 0 : -2;
235
236         while (1) {
237                 old = pos;
238                 pos = lseek(fd, pos, SEEK_DATA);
239                 if (pos == (off_t)-1) {
240                         if (errno == ENXIO) {
241                                 ret = 0;
242                                 pos = lseek(fd, 0, SEEK_END);
243                                 if (pos != (off_t)-1)
244                                         zeros += pos - old;
245                         } else {
246                                 ret = -2;
247                         }
248                         break;
249                 }
250                 ret = read(fd, buf, sizeof(buf));
251                 assert(ret); /* eof found by lseek */
252                 if (ret <= 0)
253                         break;
254                 if (old < pos) /* hole */
255                         zeros += pos - old;
256                 for (i = 0; i < ret; ++i) {
257                         for (old = i; buf[i] == 0 && i < ret; ++i)
258                                 ;
259                         if (old < i) /* code like a hole */
260                                 zeros += i - old;
261                         if (i == ret)
262                                 break;
263                         if (zeros) {
264                                 if (verbose >= 2)
265                                         fprintf(stderr,
266                                                 "adding %llu zeros to sum\n",
267                                                 (unsigned long long)zeros);
268                                 sum_add_u64(dst, 0);
269                                 sum_add_u64(dst, zeros);
270                                 zeros = 0;
271                         }
272                         for (old = i; buf[i] != 0 && i < ret; ++i)
273                                 ;
274                         if (verbose >= 2)
275                                 fprintf(stderr, "adding %u non-zeros to sum\n",
276                                         i - (int)old);
277                         sum_add(dst, buf + old, i - old);
278                 }
279                 pos += ret;
280         }
281
282         if (zeros) {
283                 if (verbose >= 2)
284                         fprintf(stderr,
285                                 "adding %llu zeros to sum (finishing)\n",
286                                 (unsigned long long)zeros);
287                 sum_add_u64(dst, 0);
288                 sum_add_u64(dst, zeros);
289         }
290
291         return ret;
292 }
293
294 int
295 sum_file_data_strict(int fd, sum_t *dst)
296 {
297         int ret;
298         off_t pos;
299
300         pos = lseek(fd, 0, SEEK_CUR);
301         if (pos == (off_t)-1)
302                 return errno == ENXIO ? 0 : -2;
303
304         while (1) {
305                 pos = lseek(fd, pos, SEEK_DATA);
306                 if (pos == (off_t)-1)
307                         return errno == ENXIO ? 0 : -2;
308                 ret = read(fd, buf, sizeof(buf));
309                 assert(ret); /* eof found by lseek */
310                 if (ret <= 0)
311                         return ret;
312                 if (verbose >= 2)
313                         fprintf(stderr,
314                                 "adding to sum at file offset %llu, %d bytes\n",
315                                 (unsigned long long)pos, ret);
316                 sum_add_u64(dst, (uint64_t)pos);
317                 sum_add(dst, buf, ret);
318                 pos += ret;
319         }
320 }
321
322 char *
323 escape(char *in)
324 {
325         char *out = alloc(strlen(in) * 3 + 1);
326         char *src = in;
327         char *dst = out;
328
329         for (; *src; ++src) {
330                 if (*src >= 32 && *src < 127 && *src != '\\') {
331                         *dst++ = *src;
332                 } else {
333                         sprintf(dst, "\\%02x", (unsigned char)*src);
334                         dst += 3;
335                 }
336         }
337         *dst = 0;
338
339         return out;
340 }
341
342 void
343 excess_file(const char *fn)
344 {
345         printf("only in local fs: %s\n", fn);
346 }
347
348 void
349 missing_file(const char *fn)
350 {
351         printf("only in remote fs: %s\n", fn);
352 }
353
354 int
355 pathcmp(const char *a, const char *b)
356 {
357         int len_a = strlen(a);
358         int len_b = strlen(b);
359
360         /*
361          * as the containing directory is sent after the files, it has to
362          * come out bigger in the comparison.
363          */
364         if (len_a < len_b && a[len_a - 1] == '/' && strncmp(a, b, len_a) == 0)
365                 return 1;
366         if (len_a > len_b && b[len_b - 1] == '/' && strncmp(a, b, len_b) == 0)
367                 return -1;
368
369         return strcmp(a, b);
370 }
371
372 void
373 check_match(char *fn, char *local_m, char *remote_m,
374             char *local_c, char *remote_c)
375 {
376         int match_m = !strcmp(local_m, remote_m);
377         int match_c = !strcmp(local_c, remote_c);
378
379         if (match_m && !match_c) {
380                 printf("data mismatch in %s\n", fn);
381         } else if (!match_m && match_c) {
382                 printf("metadata mismatch in %s\n", fn);
383         } else if (!match_m && !match_c) {
384                 printf("metadata and data mismatch in %s\n", fn);
385         }
386 }
387
388 char *prev_fn;
389 char *prev_m;
390 char *prev_c;
391 void
392 check_manifest(char *fn, char *m, char *c, int last_call)
393 {
394         char *rem_m;
395         char *rem_c;
396         char *l;
397         int cmp;
398
399         if (prev_fn) {
400                 if (last_call)
401                         cmp = -1;
402                 else
403                         cmp = pathcmp(prev_fn, fn);
404                 if (cmp > 0) {
405                         excess_file(fn);
406                         return;
407                 } else if (cmp < 0) {
408                         missing_file(prev_fn);
409                 } else {
410                         check_match(fn, m, prev_m, c, prev_c);
411                 }
412                 free(prev_fn);
413                 free(prev_m);
414                 free(prev_c);
415                 prev_fn = NULL;
416                 prev_m = NULL;
417                 prev_c = NULL;
418                 if (cmp == 0)
419                         return;
420         }
421         while ((l = getln(line, sizeof(line), in_fp))) {
422                 rem_c = strrchr(l, ' ');
423                 if (!rem_c) {
424                         /* final cs */
425                         checksum = strdup(l);
426                         break;
427                 }
428                 if (rem_c == l) {
429 malformed:
430                         fprintf(stderr, "malformed input\n");
431                         exit(-1);
432                 }
433                 *rem_c++ = 0;
434                 rem_m = strrchr(l, ' ');
435                 if (!rem_m)
436                         goto malformed;
437                 *rem_m++ = 0;
438
439                 if (last_call)
440                         cmp = -1;
441                 else
442                         cmp = pathcmp(l, fn);
443                 if (cmp == 0) {
444                         check_match(fn, m, rem_m, c, rem_c);
445                         return;
446                 } else if (cmp > 0) {
447                         excess_file(fn);
448                         prev_fn = strdup(l);
449                         prev_m = strdup(rem_m);
450                         prev_c = strdup(rem_c); 
451                         return;
452                 }
453                 missing_file(l);
454         }
455         if (!last_call)
456                 excess_file(fn);
457 }
458
459 int
460 namecmp(const void *aa, const void *bb)
461 {
462         char * const *a = aa;
463         char * const *b = bb;
464
465         return strcmp(*a, *b);
466 }
467
468 void
469 sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
470 {
471         DIR *d;
472         struct dirent *de;
473         char **namelist = NULL;
474         int alloclen = 0;
475         int entries = 0;
476         int i;
477         int ret;
478         int fd;
479         int excl;
480         sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
481                         sum_file_data_strict : sum_file_data_permissive;
482
483         d = fdopendir(dirfd);
484         if (!d) {
485                 perror("opendir");
486                 exit(-1);
487         }
488         while((de = readdir(d))) {
489                 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
490                         continue;
491                 if (entries == alloclen) {
492                         alloclen += CHUNKS;
493                         namelist = realloc(namelist,
494                                            alloclen * sizeof(*namelist));
495                         if (!namelist) {
496                                 fprintf(stderr, "malloc failed\n");
497                                 exit(-1);
498                         }
499                 }
500                 namelist[entries] = strdup(de->d_name);
501                 if (!namelist[entries]) {
502                         fprintf(stderr, "malloc failed\n");
503                         exit(-1);
504                 }
505                 ++entries;
506         }
507         qsort(namelist, entries, sizeof(*namelist), namecmp);
508         for (i = 0; i < entries; ++i) {
509                 struct stat64 st;
510                 sum_t cs;
511                 sum_t meta;
512                 char *path;
513
514                 sum_init(&cs);
515                 sum_init(&meta);
516                 path = alloc(strlen(path_in) + strlen(namelist[i]) + 3);
517                 sprintf(path, "%s/%s", path_in, namelist[i]);
518                 for (excl = 0; excl < n_excludes; ++excl) {
519                         if (strncmp(excludes[excl].path, path,
520                             excludes[excl].len) == 0)
521                                 goto next;
522                 }
523
524                 ret = fchdir(dirfd);
525                 if (ret == -1) {
526                         perror("fchdir");
527                         exit(-1);
528                 }
529                 ret = lstat64(namelist[i], &st);
530                 if (ret) {
531                         fprintf(stderr, "stat failed for %s/%s: %s\n",
532                                 path_prefix, path, strerror(errno));
533                         exit(-1);
534                 }
535                 sum_add_u64(&meta, level);
536                 sum_add(&meta, namelist[i], strlen(namelist[i]));
537                 if (!S_ISDIR(st.st_mode))
538                         sum_add_u64(&meta, st.st_nlink);
539                 if (flags[FLAG_UID])
540                         sum_add_u64(&meta, st.st_uid);
541                 if (flags[FLAG_GID])
542                         sum_add_u64(&meta, st.st_gid);
543                 if (flags[FLAG_MODE])
544                         sum_add_u64(&meta, st.st_mode);
545                 if (flags[FLAG_ATIME])
546                         sum_add_time(&meta, st.st_atime);
547                 if (flags[FLAG_MTIME])
548                         sum_add_time(&meta, st.st_mtime);
549                 if (flags[FLAG_CTIME])
550                         sum_add_time(&meta, st.st_ctime);
551                 if (S_ISDIR(st.st_mode)) {
552                         fd = openat(dirfd, namelist[i], 0);
553                         if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
554                                 sum_add_u64(&meta, errno);
555                         } else if (fd == -1) {
556                                 fprintf(stderr, "open failed for %s/%s: %s\n",
557                                         path_prefix, path, strerror(errno));
558                                 exit(-1);
559                         } else {
560                                 sum(fd, level + 1, &cs, path_prefix, path);
561                                 close(fd);
562                         }
563                 } else if (S_ISREG(st.st_mode)) {
564                         sum_add_u64(&meta, st.st_size);
565                         if (flags[FLAG_DATA]) {
566                                 if (verbose)
567                                         fprintf(stderr, "file %s\n",
568                                                 namelist[i]);
569                                 fd = openat(dirfd, namelist[i], 0);
570                                 if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
571                                         sum_add_u64(&meta, errno);
572                                 } else if (fd == -1) {
573                                         fprintf(stderr,
574                                                 "open failed for %s/%s: %s\n",
575                                                 path_prefix, path,
576                                                 strerror(errno));
577                                         exit(-1);
578                                 }
579                                 if (fd != -1) {
580                                         ret = sum_file_data(fd, &cs);
581                                         if (ret < 0) {
582                                                 fprintf(stderr,
583                                                         "read failed for "
584                                                         "%s/%s: %s\n",
585                                                         path_prefix, path,
586                                                         strerror(errno));
587                                                 exit(-1);
588                                         }
589                                         close(fd);
590                                 }
591                         }
592                 } else if (S_ISLNK(st.st_mode)) {
593                         ret = readlink(namelist[i], buf, sizeof(buf));
594                         if (ret == -1) {
595                                 perror("readlink");
596                                 exit(-1);
597                         }
598                         sum_add(&cs, buf, ret);
599                 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
600                         sum_add_u64(&cs, major(st.st_rdev));
601                         sum_add_u64(&cs, minor(st.st_rdev));
602                 }
603                 sum_fini(&cs);
604                 sum_fini(&meta);
605                 if (gen_manifest || in_manifest) {
606                         char *fn;
607                         char *m;
608                         char *c;
609
610                         if (S_ISDIR(st.st_mode))
611                                 strcat(path, "/");
612                         fn = escape(path);
613                         m = sum_to_string(&meta);
614                         c = sum_to_string(&cs);
615
616                         if (gen_manifest)
617                                 fprintf(out_fp, "%s %s %s\n", fn, m, c);
618                         if (in_manifest)
619                                 check_manifest(fn, m, c, 0);
620                         free(c);
621                         free(m);
622                         free(fn);
623                 }
624                 sum_add_sum(dircs, &cs);
625                 sum_add_sum(dircs, &meta);
626 next:
627                 free(path);
628         }
629 }
630
631 int
632 main(int argc, char *argv[])
633 {
634         extern char *optarg;
635         extern int optind;
636         int     c;
637         char *path;
638         int fd;
639         sum_t cs;
640         char flagstring[sizeof(flchar)];
641         int i;
642         int plen;
643         int elen;
644         int n_flags = 0;
645         const char *allopts = "heEfuUgGoOaAmMcCdDsSnNw:r:vx:";
646
647         out_fp = stdout;
648         while ((c = getopt(argc, argv, allopts)) != EOF) {
649                 switch(c) {
650                 case 'f':
651                         gen_manifest = 1;
652                         break;
653                 case 'u':
654                 case 'U':
655                 case 'g':
656                 case 'G':
657                 case 'o':
658                 case 'O':
659                 case 'a':
660                 case 'A':
661                 case 'm':
662                 case 'M':
663                 case 'c':
664                 case 'C':
665                 case 'd':
666                 case 'D':
667                 case 'e':
668                 case 'E':
669                 case 's':
670                 case 'S':
671                         ++n_flags;
672                         parse_flag(c);
673                         break;
674                 case 'n':
675                         for (i = 0; i < NUM_FLAGS; ++i)
676                                 flags[i] = 0;
677                         break;
678                 case 'N':
679                         for (i = 0; i < NUM_FLAGS; ++i)
680                                 flags[i] = 1;
681                         break;
682                 case 'w':
683                         out_fp = fopen(optarg, "w");
684                         if (!out_fp) {
685                                 fprintf(stderr,
686                                         "failed to open output file: %s\n",
687                                         strerror(errno));
688                                 exit(-1);
689                         }
690                         break;
691                 case 'r':
692                         in_fp = fopen(optarg, "r");
693                         if (!in_fp) {
694                                 fprintf(stderr,
695                                         "failed to open input file: %s\n",
696                                         strerror(errno));
697                                 exit(-1);
698                         }
699                         break;
700                 case 'x':
701                         ++n_excludes;
702                         excludes = realloc(excludes,
703                                            sizeof(*excludes) * n_excludes);
704                         if (!excludes) {
705                                 fprintf(stderr,
706                                         "failed to alloc exclude space\n");
707                                 exit(-1);
708                         }
709                         excludes[n_excludes - 1].path = optarg;
710                         break;
711                 case 'v':
712                         ++verbose;
713                         break;
714                 case 'h':
715                 case '?':
716                         usage();
717                 }
718         }
719
720         if (optind + 1 != argc) {
721                 fprintf(stderr, "missing path\n");
722                 usage();
723         }
724
725         if (in_fp) {
726                 char *l = getln(line, sizeof(line), in_fp);
727                 char *p;
728
729                 if (l == NULL) {
730                         fprintf(stderr, "failed to read line from input\n");
731                         exit(-1);
732                 }
733                 if (strncmp(l, "Flags: ", 7) == 0) {
734                         l += 7;
735                         in_manifest = 1;
736                         parse_flags(l);
737                 } else if ((p = strchr(l, ':'))) {
738                         *p++ = 0;
739                         parse_flags(l);
740                         checksum = strdup(p);
741                 } else {
742                         fprintf(stderr, "invalid input file format\n");
743                         exit(-1);
744                 }
745                 if (n_flags)
746                         fprintf(stderr, "warning: "
747                                 "command line flags ignored in -r mode\n");
748         }
749         strcpy(flagstring, flchar);
750         for (i = 0; i < NUM_FLAGS; ++i) {
751                 if (flags[i] == 0)
752                         flagstring[i] -= 'a' - 'A';
753         }
754
755         path = argv[optind];
756         plen = strlen(path);
757         if (path[plen - 1] == '/') {
758                 --plen;
759                 path[plen] = '\0';
760         }
761
762         for (i = 0; i < n_excludes; ++i) {
763                 if (strncmp(path, excludes[i].path, plen) != 0)
764                         fprintf(stderr,
765                                 "warning: exclude %s outside of path %s\n",
766                                 excludes[i].path, path);
767                 else
768                         excludes[i].path += plen;
769                 elen = strlen(excludes[i].path);
770                 if (excludes[i].path[elen - 1] == '/')
771                         --elen;
772                 excludes[i].path[elen] = '\0';
773                 excludes[i].len = elen;
774         }
775
776         fd = open(path, O_RDONLY);
777         if (fd == -1) {
778                 fprintf(stderr, "failed to open %s: %s\n", path,
779                         strerror(errno));
780                 exit(-1);
781         }
782
783         if (gen_manifest)
784                 fprintf(out_fp, "Flags: %s\n", flagstring);
785
786         sum_init(&cs);
787         sum(fd, 1, &cs, path, "");
788         sum_fini(&cs);
789
790         close(fd);
791         if (in_manifest)
792                 check_manifest("", "", "", 1);
793
794         if (!checksum) {
795                 if (in_manifest) {
796                         fprintf(stderr, "malformed input\n");
797                         exit(-1);
798                 }
799                 if (!gen_manifest)
800                         fprintf(out_fp, "%s:", flagstring);
801
802                 fprintf(out_fp, "%s\n", sum_to_string(&cs));
803         } else {
804                 if (strcmp(checksum, sum_to_string(&cs)) == 0) {
805                         printf("OK\n");
806                         exit(0);
807                 } else {
808                         printf("FAIL\n");
809                         exit(1);
810                 }
811         }
812
813         exit(0);
814 }