- notmuch_config_t *config;
- notmuch_database_t *notmuch;
- notmuch_query_t *query;
- FILE *output = stdout;
- notmuch_messages_t *messages;
- notmuch_message_t *message;
- notmuch_tags_t *tags;
-
- config = notmuch_config_open (ctx, NULL, NULL);
- if (config == NULL)
- return 1;
-
- notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
- NOTMUCH_DATABASE_MODE_READ_ONLY);
- if (notmuch == NULL)
- return 1;
-
- if (argc && strcmp (argv[0], "--") != 0) {
- output = fopen (argv[0], "w");
- if (output == NULL) {
- fprintf (stderr, "Error opening %s for writing: %s\n",
- argv[0], strerror (errno));
+ notmuch_config_list_t *list;
+ int ret = EXIT_FAILURE;
+ char *buffer = NULL;
+ size_t buffer_size = 0;
+
+ if (print_status_database ("notmuch dump", notmuch,
+ notmuch_database_get_config_list (notmuch, NULL, &list)))
+ goto DONE;
+
+ for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
+ if (hex_encode (notmuch, notmuch_config_list_key (list),
+ &buffer, &buffer_size) != HEX_SUCCESS) {
+ fprintf (stderr, "Error: failed to hex-encode config key %s\n",
+ notmuch_config_list_key (list));
+ goto DONE;
+ }
+ GZPRINTF (output, "#@ %s", buffer);
+
+ if (hex_encode (notmuch, notmuch_config_list_value (list),
+ &buffer, &buffer_size) != HEX_SUCCESS) {
+ fprintf (stderr, "Error: failed to hex-encode config value %s\n",
+ notmuch_config_list_value (list) );
+ goto DONE;
+ }
+
+ GZPUTS (output, " ");
+ GZPUTS (output, buffer);
+ GZPUTS (output, "\n");
+ }
+
+ ret = EXIT_SUCCESS;
+
+ DONE:
+ if (list)
+ notmuch_config_list_destroy (list);
+
+ if (buffer)
+ talloc_free (buffer);
+
+ return ret;
+}
+
+static void
+print_dump_header (gzFile output, int output_format, int include)
+{
+ const char *sep = "";
+
+ GZPRINTF (output, "#notmuch-dump %s:%d ",
+ (output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",
+ NOTMUCH_DUMP_VERSION);
+
+ if (include & DUMP_INCLUDE_CONFIG) {
+ GZPUTS (output, "config");
+ sep = ",";
+ }
+ if (include & DUMP_INCLUDE_PROPERTIES) {
+ GZPRINTF (output, "%sproperties", sep);
+ sep = ",";
+ }
+ if (include & DUMP_INCLUDE_TAGS) {
+ GZPRINTF (output, "%stags", sep);
+ }
+ GZPUTS (output, "\n");
+}
+
+static int
+dump_properties_message (void *ctx,
+ notmuch_message_t *message,
+ gzFile output,
+ char **buffer_p, size_t *size_p)
+{
+ const char *message_id;
+ notmuch_message_properties_t *list;
+ bool first = true;
+
+ message_id = notmuch_message_get_message_id (message);
+
+ if (strchr (message_id, '\n')) {
+ fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
+ return 0;
+ }
+
+ for (list = notmuch_message_get_properties (message, "", false);
+ notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
+ const char *key, *val;
+
+ if (first) {
+ if (hex_encode (ctx, message_id, buffer_p, size_p) != HEX_SUCCESS) {
+ fprintf (stderr, "Error: failed to hex-encode message-id %s\n", message_id);
+ return 1;
+ }
+ GZPRINTF (output, "#= %s", *buffer_p);
+ first = false;
+ }
+
+ key = notmuch_message_properties_key (list);
+ val = notmuch_message_properties_value (list);
+
+ if (hex_encode (ctx, key, buffer_p, size_p) != HEX_SUCCESS) {
+ fprintf (stderr, "Error: failed to hex-encode key %s\n", key);
+ return 1;
+ }
+ GZPRINTF (output, " %s", *buffer_p);
+
+ if (hex_encode (ctx, val, buffer_p, size_p) != HEX_SUCCESS) {
+ fprintf (stderr, "Error: failed to hex-encode value %s\n", val);