meowsum.git

commit 324768a65541cf763b343ac7bda9ae571ddd8153

Author: Adam <git@apiote.tk>

check saved checksums

 meowsum.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-


diff --git a/meowsum.c b/meowsum.c
index 40c672ea9ed79e0c1b59202e6e0d3949299665ce..7fee45e9f24eac0b901219774b5600e5a28d3ba4 100644
--- a/meowsum.c
+++ b/meowsum.c
@@ -166,9 +166,92 @@
   return true;
 }
 
-bool check_files(char *args[], int start, int argc, bool quiet,
-                 bool stric_errors) {
-  // todo
+bool check_files(char *argv[], int start, int argc, bool quiet,
+                 bool strict_errors) {
+  char *line;
+  size_t line_size;
+  FILE *file;
+  char expected_hex_hash[33];
+  unsigned long expected_size, size;
+  char *path;
+  ssize_t read_bytes;
+  meow_u128 *hash;
+  char *hex_hash;
+  int hash_size;
+  bool result = true;
+
+  for (int i = start; i < argc; ++i) {
+    file = fopen(argv[i], "r");
+
+    while (true) {
+      line = NULL;
+      path = NULL;
+
+      read_bytes = getline(&line, &line_size, file);
+      if (read_bytes == -1) {
+        if (feof(file)) {
+          break;
+        } else {
+          perror("error");
+          printf("while reading line in %s\n", argv[i]);
+          return false;
+        }
+      }
+
+      path = malloc(line_size * sizeof(char));
+      sscanf(line, "%s %ld %s", expected_hex_hash, &expected_size, path);
+      hash_size = strlen(expected_hex_hash) / 2 * 8;
+
+      if (strcmp("-", path) == 0) {
+        if (!quiet) {
+          printf("[WARN] stdin on the list in %s\n", argv[i]);
+        }
+      } else {
+        // todo missing
+        hash = calculate_checksum(path, &size);
+        if (hash == NULL) {
+          printf("error while calculating %s\n", path);
+          free(hash);
+          free(path);
+          free(line);
+          continue;
+        }
+        hex_hash = format_sum(*hash, hash_size);
+        if (hex_hash == NULL) {
+          printf("error while formating hex hash for %s\n", path);
+          free(hex_hash);
+          free(hash);
+          free(path);
+          free(line);
+          continue;
+        }
+
+        if (strcmp(expected_hex_hash, hex_hash) != 0) {
+          if (!quiet) {
+            printf("ERH %s\n", path);
+          }
+          result &= false;
+        } else if (expected_size != size) {
+          if (!quiet) {
+            printf("ERS %s\n", path);
+          }
+          result &= false;
+        } else {
+          if (!quiet) {
+            printf("OK %s\n", path);
+          }
+        }
+      }
+
+      free(hex_hash);
+      free(hash);
+      free(path);
+      free(line);
+    }
+
+    fclose(file);
+  }
+  return result;
 }
 
 int main(int argc, char *argv[]) {