- for (tags = notmuch_message_get_tags (message);
- notmuch_tags_valid (tags);
- notmuch_tags_move_to_next (tags)) {
- const char *tag_str = notmuch_tags_get (tags);
-
- if (! first)
- fputs (" ", output);
-
- first = 0;
-
- if (output_format == DUMP_FORMAT_SUP) {
- fputs (tag_str, output);
- } else {
- if (hex_encode (notmuch, tag_str,
- &buffer, &buffer_size) != HEX_SUCCESS) {
- fprintf (stderr, "Error: failed to hex-encode tag %s\n",
- tag_str);
- return 1;
- }
- fprintf (output, "+%s", buffer);
- }
+ if ((include & DUMP_INCLUDE_PROPERTIES) &&
+ dump_properties_message (notmuch, message, output,
+ &buffer, &buffer_size))
+ return EXIT_FAILURE;
+
+ notmuch_message_destroy (message);
+ }
+
+ notmuch_query_destroy (query);
+
+ 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,
+ dump_include_t include,
+ bool gzip_output)
+{
+ gzFile output = NULL;
+ const char *mode = gzip_output ? "w9" : "wT";
+ const char *name_for_error = output_file_name ? output_file_name : "stdout";
+
+ char *tempname = NULL;
+ int outfd = -1;
+
+ int ret = -1;
+
+ if (output_file_name) {
+ tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
+ outfd = mkstemp (tempname);
+ } else {
+ outfd = dup (STDOUT_FILENO);
+ }
+
+ if (outfd < 0) {
+ fprintf (stderr, "Bad output file %s\n", name_for_error);
+ goto DONE;
+ }
+
+ output = gzdopen (outfd, mode);
+
+ if (output == NULL) {
+ fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
+ name_for_error, strerror (errno));
+ if (close (outfd))
+ fprintf (stderr, "Error closing %s during shutdown: %s\n",
+ name_for_error, strerror (errno));
+ goto DONE;
+ }
+
+ ret = database_dump_file (notmuch, output, query_str, output_format, include);
+ if (ret) goto DONE;
+
+ ret = gzflush (output, Z_FINISH);
+ if (ret) {
+ fprintf (stderr, "Error flushing output: %s\n", gzerror_str (output));
+ goto DONE;
+ }
+
+ if (output_file_name) {
+ ret = fsync (outfd);
+ if (ret) {
+ fprintf (stderr, "Error syncing %s to disk: %s\n",
+ name_for_error, strerror (errno));
+ goto DONE;