btrfs-progs: Beautify owner when printing leaf/nodes
[platform/upstream/btrfs-progs.git] / tests / fssum.c
index f5f83f9..2bda5df 100644 (file)
 #include <fcntl.h>
 #include <dirent.h>
 #include <errno.h>
-#include <sys/types.h>
+#include <sys/sysmacros.h>
 #include <sys/stat.h>
-#include <openssl/md5.h>
-#include <netinet/in.h>
-#include <inttypes.h>
 #include <assert.h>
+#include <time.h>
+#include <stdint.h>
+#include "tests/sha.h"
 
-#define CS_SIZE 16
+#define CS_SIZE 32
 #define CHUNKS 128
 
 #ifndef SEEK_DATA
@@ -48,8 +48,8 @@ struct excludes {
 };
 
 typedef struct _sum {
-       MD5_CTX         md5;
-       unsigned char   out[16];
+       SHA256Context   sha;
+       unsigned char   out[CS_SIZE];
 } sum_t;
 
 typedef int (*sum_file_data_t)(int fd, sum_t *dst);
@@ -174,19 +174,19 @@ alloc(size_t sz)
 void
 sum_init(sum_t *cs)
 {
-       MD5_Init(&cs->md5);
+       SHA256Reset(&cs->sha);
 }
 
 void
 sum_fini(sum_t *cs)
 {
-       MD5_Final(cs->out, &cs->md5);
+       SHA256Result(&cs->sha, cs->out);
 }
 
 void
 sum_add(sum_t *cs, void *buf, int size)
 {
-       MD5_Update(&cs->md5, buf, size);
+       SHA256Input(&cs->sha, buf, size);
 }
 
 void
@@ -421,6 +421,9 @@ check_manifest(char *fn, char *m, char *c, int last_call)
        while ((l = getln(line, sizeof(line), in_fp))) {
                rem_c = strrchr(l, ' ');
                if (!rem_c) {
+                       if (checksum)
+                               free(checksum);
+
                        /* final cs */
                        checksum = strdup(l);
                        break;
@@ -504,6 +507,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
                }
                ++entries;
        }
+
        qsort(namelist, entries, sizeof(*namelist), namecmp);
        for (i = 0; i < entries; ++i) {
                struct stat64 st;
@@ -528,8 +532,8 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
                }
                ret = lstat64(namelist[i], &st);
                if (ret) {
-                       fprintf(stderr, "stat failed for %s/%s: %s\n",
-                               path_prefix, path, strerror(errno));
+                       fprintf(stderr, "stat failed for %s/%s: %m\n",
+                               path_prefix, path);
                        exit(-1);
                }
                sum_add_u64(&meta, level);
@@ -553,8 +557,8 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
                        if (fd == -1 && flags[FLAG_OPEN_ERROR]) {
                                sum_add_u64(&meta, errno);
                        } else if (fd == -1) {
-                               fprintf(stderr, "open failed for %s/%s: %s\n",
-                                       path_prefix, path, strerror(errno));
+                               fprintf(stderr, "open failed for %s/%s: %m\n",
+                                       path_prefix, path);
                                exit(-1);
                        } else {
                                sum(fd, level + 1, &cs, path_prefix, path);
@@ -571,9 +575,8 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
                                        sum_add_u64(&meta, errno);
                                } else if (fd == -1) {
                                        fprintf(stderr,
-                                               "open failed for %s/%s: %s\n",
-                                               path_prefix, path,
-                                               strerror(errno));
+                                               "open failed for %s/%s: %m\n",
+                                               path_prefix, path);
                                        exit(-1);
                                }
                                if (fd != -1) {
@@ -581,9 +584,8 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
                                        if (ret < 0) {
                                                fprintf(stderr,
                                                        "read failed for "
-                                                       "%s/%s: %s\n",
-                                                       path_prefix, path,
-                                                       strerror(errno));
+                                                       "%s/%s: %m\n",
+                                                       path_prefix, path);
                                                exit(-1);
                                        }
                                        close(fd);
@@ -625,7 +627,11 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
                sum_add_sum(dircs, &meta);
 next:
                free(path);
+               free(namelist[i]);
        }
+
+       free(namelist);
+       closedir(d);
 }
 
 int
@@ -637,7 +643,9 @@ main(int argc, char *argv[])
        char *path;
        int fd;
        sum_t cs;
+       char *sumstring;
        char flagstring[sizeof(flchar)];
+       int ret = 0;
        int i;
        int plen;
        int elen;
@@ -683,8 +691,7 @@ main(int argc, char *argv[])
                        out_fp = fopen(optarg, "w");
                        if (!out_fp) {
                                fprintf(stderr,
-                                       "failed to open output file: %s\n",
-                                       strerror(errno));
+                                       "failed to open output file: %m\n");
                                exit(-1);
                        }
                        break;
@@ -692,8 +699,7 @@ main(int argc, char *argv[])
                        in_fp = fopen(optarg, "r");
                        if (!in_fp) {
                                fprintf(stderr,
-                                       "failed to open input file: %s\n",
-                                       strerror(errno));
+                                       "failed to open input file: %m\n");
                                exit(-1);
                        }
                        break;
@@ -737,6 +743,9 @@ main(int argc, char *argv[])
                } else if ((p = strchr(l, ':'))) {
                        *p++ = 0;
                        parse_flags(l);
+
+                       if (checksum)
+                               free(checksum);
                        checksum = strdup(p);
                } else {
                        fprintf(stderr, "invalid input file format\n");
@@ -775,8 +784,7 @@ main(int argc, char *argv[])
 
        fd = open(path, O_RDONLY);
        if (fd == -1) {
-               fprintf(stderr, "failed to open %s: %s\n", path,
-                       strerror(errno));
+               fprintf(stderr, "failed to open %s: %m\n", path);
                exit(-1);
        }
 
@@ -799,16 +807,28 @@ main(int argc, char *argv[])
                if (!gen_manifest)
                        fprintf(out_fp, "%s:", flagstring);
 
-               fprintf(out_fp, "%s\n", sum_to_string(&cs));
+               sumstring = sum_to_string(&cs);
+               fprintf(out_fp, "%s\n", sumstring);
+               free(sumstring);
        } else {
-               if (strcmp(checksum, sum_to_string(&cs)) == 0) {
+               sumstring = sum_to_string(&cs);
+               if (strcmp(checksum, sumstring) == 0) {
                        printf("OK\n");
-                       exit(0);
+                       ret = 0;
                } else {
                        printf("FAIL\n");
-                       exit(1);
+                       ret = 1;
                }
+
+               free(checksum);
+               free(sumstring);
        }
 
-       exit(0);
+       if (in_fp)
+               fclose(in_fp);
+
+       if (out_fp != stdout)
+               fclose(out_fp);
+
+       exit(ret);
 }