[PATCH 2/2] cli: abstract dump file open from the dump command

Jani Nikula jani at nikula.org
Tue Mar 25 10:07:50 PDT 2014


No functional changes, except for slight improvement in error
handling.
---
 notmuch-dump.c | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/notmuch-dump.c b/notmuch-dump.c
index 179e2e97bf61..88bef3f77e82 100644
--- a/notmuch-dump.c
+++ b/notmuch-dump.c
@@ -115,11 +115,37 @@ 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.
+ */
+static int
+database_dump (notmuch_database_t *notmuch, const char *output_file_name,
+	       const char *query_str, int 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 +171,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 +179,7 @@ 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 = database_dump (notmuch, output_file_name, query_str, output_format);
 
     notmuch_database_destroy (notmuch);
 
-- 
1.9.0



More information about the notmuch mailing list