about summary refs log tree commit diff stats
path: root/forg-import.c
diff options
context:
space:
mode:
Diffstat (limited to 'forg-import.c')
-rw-r--r--forg-import.c57
1 files changed, 45 insertions, 12 deletions
diff --git a/forg-import.c b/forg-import.c
index 30ee7fc..4cfbcb6 100644
--- a/forg-import.c
+++ b/forg-import.c
@@ -7,13 +7,16 @@
 #include <sysexits.h>
 #include <unistd.h>
 
+#include "filehash.h"
+#include "macros.h"
+
 #define optstr "0h:L:r:"
 
 typedef struct
 {
-  const char *hash;
+  const char *hash_program;
   const char *repository;
-  unsigned hash_max_lines;
+  unsigned max_per_invocation;
   char **files;
   unsigned n_files;
   char input_sep;
@@ -61,9 +64,9 @@ read_opts(int argc, char **argv)
   int o;
   const char *prgname;
   options_t options = {
-    .hash = "md5sum",
+    .hash_program = "md5sum",
     .repository = ".",
-    .hash_max_lines = 4096,
+    .max_per_invocation = 4096,
     .input_sep = '\n',
   };
 
@@ -75,10 +78,10 @@ read_opts(int argc, char **argv)
         options.input_sep = '\0';
         break;
       case 'h':
-        options.hash = optarg;
+        options.hash_program = optarg;
         break;
       case 'L':
-        if (str_to_uint(optarg, &options.hash_max_lines))
+        if (str_to_uint(optarg, &options.max_per_invocation))
           goto fail;
         break;
       case 'r':
@@ -167,22 +170,52 @@ done:
 }
 
 static int
-run(const options_t *opts)
+run(const options_t *opts, filehash_t *fh)
 {
+  int ret = 0;
   void *aux = NULL;
-  const char *file;
+  const char *file_name;
 
-  while (file = iter_files(opts, &aux), file)
-    warnx("[%s]", file);
+  while (file_name = iter_files(opts, &aux), file_name) {
 
-  return 0;
+    switch (filehash_send(fh, file_name)) {
+      case fhs_accepts:
+        continue;
+
+      case fhs_failure:
+        warnx("handle failure!");
+        ret = 1;
+        goto exit;
+
+      case fhs_full:
+        warnx("handle full!");
+        filehash_free(fh);
+        fh = filehash_new(opts->hash_program, opts->max_per_invocation);
+        break;
+
+      case fhs_rejected:
+        bug_abort;
+
+    }
+
+  }
+
+exit:
+  filehash_free(fh);
+  return ret;
 }
 
 int
 main(int argc, char **argv)
 {
   options_t opts;
+  filehash_t *fh;
 
   opts = read_opts(argc, argv);
-  return run(&opts);
+
+  fh = filehash_new(opts.hash_program, opts.max_per_invocation);
+  if (!fh)
+    err(1, "filehash_new");
+
+  return run(&opts, fh);
 }