]> git.cworth.org Git - notmuch/commitdiff
cli: abstract dump file open from the dump command
authorJani Nikula <jani@nikula.org>
Tue, 25 Mar 2014 17:48:57 +0000 (19:48 +0200)
committerDavid Bremner <david@tethera.net>
Sun, 30 Mar 2014 22:23:16 +0000 (19:23 -0300)
Also expose the dump function to the rest of notmuch. No functional
changes, except for slight improvement in error handling.

dump-restore-private.h [deleted file]
notmuch-client.h
notmuch-dump.c
notmuch-restore.c

diff --git a/dump-restore-private.h b/dump-restore-private.h
deleted file mode 100644 (file)
index 896a004..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef DUMP_RESTORE_PRIVATE_H
-#define DUMP_RESTORE_PRIVATE_H
-
-#include "hex-escape.h"
-#include "command-line-arguments.h"
-
-typedef enum dump_formats {
-    DUMP_FORMAT_AUTO,
-    DUMP_FORMAT_BATCH_TAG,
-    DUMP_FORMAT_SUP
-} dump_format_t;
-
-#endif
index 278b498a246adac024778e8d403f30e4db8155ad..d11064824ddf3ad0eed8fa5944a2e9b6e6477e19 100644 (file)
@@ -441,5 +441,16 @@ mime_node_child (mime_node_t *parent, int child);
 mime_node_t *
 mime_node_seek_dfs (mime_node_t *node, int n);
 
+typedef enum dump_formats {
+    DUMP_FORMAT_AUTO,
+    DUMP_FORMAT_BATCH_TAG,
+    DUMP_FORMAT_SUP
+} dump_format_t;
+
+int
+notmuch_database_dump (notmuch_database_t *notmuch,
+                      const char *output_file_name,
+                      const char *query_str, dump_format_t output_format);
+
 #include "command-line-arguments.h"
 #endif
index 179e2e97bf61e964b9a9b63b7939b4d805b8f103..21702d793bcf1bd0ec7c379976bb040e5903d9de 100644 (file)
@@ -19,7 +19,7 @@
  */
 
 #include "notmuch-client.h"
-#include "dump-restore-private.h"
+#include "hex-escape.h"
 #include "string-util.h"
 
 static int
@@ -115,11 +115,38 @@ database_dump_file (notmuch_database_t *notmuch, FILE *output,
     return EXIT_SUCCESS;
 }
 
+/* Dump database into output_file_name if it's non-NULL, stdout
+ * otherwise.
+ */
+int
+notmuch_database_dump (notmuch_database_t *notmuch,
+                      const char *output_file_name,
+                      const char *query_str, dump_format_t output_format)
+{
+    FILE *output = stdout;
+    int ret;
+
+    if (output_file_name) {
+       output = fopen (output_file_name, "w");
+       if (output == NULL) {
+           fprintf (stderr, "Error opening %s for writing: %s\n",
+                    output_file_name, strerror (errno));
+           return EXIT_FAILURE;
+       }
+    }
+
+    ret = database_dump_file (notmuch, output, query_str, output_format);
+
+    if (output != stdout)
+       fclose (output);
+
+    return ret;
+}
+
 int
 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
 {
     notmuch_database_t *notmuch;
-    FILE *output = stdout;
     const char *query_str = NULL;
     int ret;
 
@@ -145,16 +172,6 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
-    if (output_file_name) {
-       output = fopen (output_file_name, "w");
-       if (output == NULL) {
-           fprintf (stderr, "Error opening %s for writing: %s\n",
-                    output_file_name, strerror (errno));
-           return EXIT_FAILURE;
-       }
-    }
-
-
     if (opt_index < argc) {
        query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
        if (query_str == NULL) {
@@ -163,10 +180,8 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
        }
     }
 
-    ret = database_dump_file (notmuch, output, query_str, output_format);
-
-    if (output != stdout)
-       fclose (output);
+    ret = notmuch_database_dump (notmuch, output_file_name, query_str,
+                                output_format);
 
     notmuch_database_destroy (notmuch);
 
index f23ab983a93da0a84020ee460300f4f631225743..c54d51328b51b31d9e3e500ec3fc5a06286a95e4 100644 (file)
@@ -19,7 +19,7 @@
  */
 
 #include "notmuch-client.h"
-#include "dump-restore-private.h"
+#include "hex-escape.h"
 #include "tag-util.h"
 #include "string-util.h"