2009-11-10 21:03:05 +01:00
|
|
|
/* notmuch - Not much of an email program, (just index and search)
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Carl Worth
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2009-11-10 21:03:05 +01:00
|
|
|
*
|
|
|
|
* Author: Carl Worth <cworth@cworth.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-client.h"
|
2014-03-25 18:48:57 +01:00
|
|
|
#include "hex-escape.h"
|
2013-01-06 21:22:41 +01:00
|
|
|
#include "string-util.h"
|
2020-04-13 01:00:31 +02:00
|
|
|
#include "zlib-extra.h"
|
2014-03-29 02:14:51 +01:00
|
|
|
|
2016-03-22 11:54:50 +01:00
|
|
|
static int
|
|
|
|
database_dump_config (notmuch_database_t *notmuch, gzFile output)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "#@ %s", buffer);
|
2016-03-22 11:54:50 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-13 04:01:38 +02:00
|
|
|
GZPUTS (output, " ");
|
|
|
|
GZPUTS (output, buffer);
|
|
|
|
GZPUTS (output, "\n");
|
2016-03-22 11:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = EXIT_SUCCESS;
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
DONE:
|
2016-03-22 11:54:50 +01:00
|
|
|
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)
|
|
|
|
{
|
2016-06-13 03:05:54 +02:00
|
|
|
const char *sep = "";
|
|
|
|
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "#notmuch-dump %s:%d ",
|
2016-03-22 11:54:50 +01:00
|
|
|
(output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",
|
2016-06-13 03:05:54 +02:00
|
|
|
NOTMUCH_DUMP_VERSION);
|
|
|
|
|
|
|
|
if (include & DUMP_INCLUDE_CONFIG) {
|
2020-04-13 03:59:12 +02:00
|
|
|
GZPUTS (output, "config");
|
2016-06-13 03:05:54 +02:00
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
if (include & DUMP_INCLUDE_PROPERTIES) {
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "%sproperties", sep);
|
2016-06-13 03:05:54 +02:00
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
if (include & DUMP_INCLUDE_TAGS) {
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "%stags", sep);
|
2016-06-13 03:05:54 +02:00
|
|
|
}
|
2020-04-13 03:59:12 +02:00
|
|
|
GZPUTS (output, "\n");
|
2016-06-13 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-10-07 10:44:04 +02:00
|
|
|
bool first = true;
|
2016-06-13 03:05:54 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
for (list = notmuch_message_get_properties (message, "", false);
|
2016-06-13 03:05:54 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "#= %s", *buffer_p);
|
2017-10-07 10:44:04 +02:00
|
|
|
first = false;
|
2016-06-13 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, " %s", *buffer_p);
|
2016-06-13 03:05:54 +02:00
|
|
|
|
|
|
|
if (hex_encode (ctx, val, buffer_p, size_p) != HEX_SUCCESS) {
|
|
|
|
fprintf (stderr, "Error: failed to hex-encode value %s\n", val);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "=%s", *buffer_p);
|
2016-06-13 03:05:54 +02:00
|
|
|
}
|
|
|
|
notmuch_message_properties_destroy (list);
|
|
|
|
|
|
|
|
if (! first)
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "\n", *buffer_p);
|
2016-06-13 03:05:54 +02:00
|
|
|
|
|
|
|
return 0;
|
2016-03-22 11:54:50 +01:00
|
|
|
}
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2016-06-13 03:05:53 +02:00
|
|
|
static int
|
|
|
|
dump_tags_message (void *ctx,
|
|
|
|
notmuch_message_t *message, int output_format,
|
|
|
|
gzFile output,
|
|
|
|
char **buffer_p, size_t *size_p)
|
|
|
|
{
|
|
|
|
int first = 1;
|
|
|
|
const char *message_id;
|
|
|
|
|
|
|
|
message_id = notmuch_message_get_message_id (message);
|
|
|
|
|
|
|
|
if (output_format == DUMP_FORMAT_BATCH_TAG &&
|
|
|
|
strchr (message_id, '\n')) {
|
|
|
|
/* This will produce a line break in the output, which
|
|
|
|
* would be difficult to handle in tools. However, it's
|
|
|
|
* also impossible to produce an email containing a line
|
|
|
|
* break in a message ID because of unfolding, so we can
|
|
|
|
* safely disallow it. */
|
|
|
|
fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_format == DUMP_FORMAT_SUP) {
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "%s (", message_id);
|
2016-06-13 03:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (notmuch_tags_t *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)
|
2020-04-13 03:59:12 +02:00
|
|
|
GZPUTS (output, " ");
|
2016-06-13 03:05:53 +02:00
|
|
|
|
|
|
|
first = 0;
|
|
|
|
|
|
|
|
if (output_format == DUMP_FORMAT_SUP) {
|
2020-04-13 03:59:12 +02:00
|
|
|
GZPUTS (output, tag_str);
|
2016-06-13 03:05:53 +02:00
|
|
|
} else {
|
|
|
|
if (hex_encode (ctx, tag_str,
|
|
|
|
buffer_p, size_p) != HEX_SUCCESS) {
|
|
|
|
fprintf (stderr, "Error: failed to hex-encode tag %s\n",
|
|
|
|
tag_str);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, "+%s", *buffer_p);
|
2016-06-13 03:05:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_format == DUMP_FORMAT_SUP) {
|
2020-04-13 03:59:12 +02:00
|
|
|
GZPUTS (output, ")\n");
|
2016-06-13 03:05:53 +02:00
|
|
|
} else {
|
|
|
|
if (make_boolean_term (ctx, "id", message_id,
|
|
|
|
buffer_p, size_p)) {
|
|
|
|
fprintf (stderr, "Error quoting message id %s: %s\n",
|
|
|
|
message_id, strerror (errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-04-13 03:39:11 +02:00
|
|
|
GZPRINTF (output, " -- %s\n", *buffer_p);
|
2016-06-13 03:05:53 +02:00
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:07:49 +01:00
|
|
|
static int
|
2014-03-29 02:14:51 +01:00
|
|
|
database_dump_file (notmuch_database_t *notmuch, gzFile output,
|
2016-03-22 11:54:50 +01:00
|
|
|
const char *query_str, int output_format, int include)
|
2009-11-10 21:03:05 +01:00
|
|
|
{
|
|
|
|
notmuch_query_t *query;
|
|
|
|
notmuch_messages_t *messages;
|
|
|
|
notmuch_message_t *message;
|
2016-06-13 03:05:53 +02:00
|
|
|
notmuch_status_t status;
|
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_size = 0;
|
2012-06-15 00:08:42 +02:00
|
|
|
|
2016-03-22 11:54:50 +01:00
|
|
|
print_dump_header (output, output_format, include);
|
|
|
|
|
|
|
|
if (include & DUMP_INCLUDE_CONFIG) {
|
|
|
|
if (print_status_database ("notmuch dump", notmuch,
|
2019-06-13 12:31:01 +02:00
|
|
|
database_dump_config (notmuch, output)))
|
2016-03-22 11:54:50 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-06-13 03:05:54 +02:00
|
|
|
if (! (include & (DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES)))
|
2016-03-22 11:54:50 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
2014-03-25 18:07:49 +01:00
|
|
|
if (! query_str)
|
|
|
|
query_str = "";
|
2011-12-03 23:32:18 +01:00
|
|
|
|
2011-10-09 16:12:30 +02:00
|
|
|
query = notmuch_query_create (notmuch, query_str);
|
2011-10-09 15:57:03 +02:00
|
|
|
if (query == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2009-11-10 21:03:05 +01:00
|
|
|
}
|
2011-11-27 19:40:53 +01:00
|
|
|
/* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
|
|
|
|
* first results quickly at the expense of total time.
|
|
|
|
*/
|
|
|
|
notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2017-02-26 22:21:32 +01:00
|
|
|
status = notmuch_query_search_messages (query, &messages);
|
2015-09-06 15:15:47 +02:00
|
|
|
if (print_status_query ("notmuch dump", query, status))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
for (;
|
2010-03-09 18:22:29 +01:00
|
|
|
notmuch_messages_valid (messages);
|
2012-11-15 02:33:22 +01:00
|
|
|
notmuch_messages_move_to_next (messages)) {
|
2012-06-15 00:08:42 +02:00
|
|
|
|
2009-11-10 21:03:05 +01:00
|
|
|
message = notmuch_messages_get (messages);
|
2013-01-06 21:22:40 +01:00
|
|
|
|
2017-05-08 03:44:23 +02:00
|
|
|
if ((include & DUMP_INCLUDE_TAGS) &&
|
|
|
|
dump_tags_message (notmuch, message, output_format, output,
|
2016-06-13 03:05:53 +02:00
|
|
|
&buffer, &buffer_size))
|
|
|
|
return EXIT_FAILURE;
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2016-06-13 03:05:54 +02:00
|
|
|
if ((include & DUMP_INCLUDE_PROPERTIES) &&
|
|
|
|
dump_properties_message (notmuch, message, output,
|
|
|
|
&buffer, &buffer_size))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2009-11-10 21:03:05 +01:00
|
|
|
notmuch_message_destroy (message);
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:07:49 +01:00
|
|
|
notmuch_query_destroy (query);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:48:57 +01:00
|
|
|
/* 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,
|
2014-03-29 02:14:51 +01:00
|
|
|
const char *query_str,
|
|
|
|
dump_format_t output_format,
|
2016-03-22 11:54:50 +01:00
|
|
|
dump_include_t include,
|
2017-10-07 10:44:04 +02:00
|
|
|
bool gzip_output)
|
2014-03-25 18:48:57 +01:00
|
|
|
{
|
2014-03-29 02:14:51 +01:00
|
|
|
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;
|
2014-03-25 18:48:57 +01:00
|
|
|
|
|
|
|
if (output_file_name) {
|
2014-03-29 02:14:51 +01:00
|
|
|
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",
|
2019-06-13 12:31:01 +02:00
|
|
|
name_for_error, strerror (errno));
|
2014-03-29 02:14:51 +01:00
|
|
|
goto DONE;
|
2014-03-25 18:48:57 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 11:54:50 +01:00
|
|
|
ret = database_dump_file (notmuch, output, query_str, output_format, include);
|
2014-03-29 02:14:51 +01:00
|
|
|
if (ret) goto DONE;
|
|
|
|
|
|
|
|
ret = gzflush (output, Z_FINISH);
|
|
|
|
if (ret) {
|
2020-04-13 01:00:31 +02:00
|
|
|
fprintf (stderr, "Error flushing output: %s\n", gzerror_str (output));
|
2014-03-29 02:14:51 +01:00
|
|
|
goto DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_file_name) {
|
2014-05-08 13:57:07 +02:00
|
|
|
ret = fsync (outfd);
|
2014-03-29 02:14:51 +01:00
|
|
|
if (ret) {
|
|
|
|
fprintf (stderr, "Error syncing %s to disk: %s\n",
|
|
|
|
name_for_error, strerror (errno));
|
|
|
|
goto DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 22:48:23 +02:00
|
|
|
ret = gzclose_w (output);
|
|
|
|
if (ret) {
|
2014-03-29 02:14:51 +01:00
|
|
|
fprintf (stderr, "Error closing %s: %s\n", name_for_error,
|
2020-04-13 01:00:31 +02:00
|
|
|
gzerror_str (output));
|
2014-03-29 02:14:51 +01:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
output = NULL;
|
|
|
|
goto DONE;
|
2019-07-23 22:48:23 +02:00
|
|
|
} else
|
|
|
|
output = NULL;
|
2014-03-29 02:14:51 +01:00
|
|
|
|
|
|
|
if (output_file_name) {
|
|
|
|
ret = rename (tempname, output_file_name);
|
|
|
|
if (ret) {
|
|
|
|
fprintf (stderr, "Error renaming %s to %s: %s\n",
|
|
|
|
tempname, output_file_name, strerror (errno));
|
|
|
|
goto DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-06-13 12:31:01 +02:00
|
|
|
DONE:
|
2014-03-29 02:14:51 +01:00
|
|
|
if (ret != EXIT_SUCCESS && output)
|
|
|
|
(void) gzclose_w (output);
|
2014-03-25 18:48:57 +01:00
|
|
|
|
2014-03-29 02:14:51 +01:00
|
|
|
if (ret != EXIT_SUCCESS && output_file_name)
|
|
|
|
(void) unlink (tempname);
|
2014-03-25 18:48:57 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:07:49 +01:00
|
|
|
int
|
2020-12-21 13:55:14 +01:00
|
|
|
notmuch_dump_command (notmuch_config_t *config, unused(notmuch_database_t *notmuch), int argc, char *argv[])
|
2014-03-25 18:07:49 +01:00
|
|
|
{
|
|
|
|
notmuch_database_t *notmuch;
|
|
|
|
const char *query_str = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (notmuch_database_open (notmuch_config_get_database_path (config),
|
2014-06-23 22:12:29 +02:00
|
|
|
NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
|
2014-03-25 18:07:49 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2015-04-06 00:39:55 +02:00
|
|
|
notmuch_exit_if_unmatched_db_uuid (notmuch);
|
|
|
|
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
const char *output_file_name = NULL;
|
2014-03-25 18:07:49 +01:00
|
|
|
int opt_index;
|
|
|
|
|
|
|
|
int output_format = DUMP_FORMAT_BATCH_TAG;
|
2016-03-22 11:54:50 +01:00
|
|
|
int include = 0;
|
2017-10-07 10:44:04 +02:00
|
|
|
bool gzip_output = 0;
|
2014-03-25 18:07:49 +01:00
|
|
|
|
|
|
|
notmuch_opt_desc_t options[] = {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_keyword = &output_format, .name = "format", .keywords =
|
2019-06-13 12:31:01 +02:00
|
|
|
(notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
|
|
|
|
{ "batch-tag", DUMP_FORMAT_BATCH_TAG },
|
|
|
|
{ 0, 0 } } },
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_flags = &include, .name = "include", .keywords =
|
2019-06-13 12:31:01 +02:00
|
|
|
(notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
|
|
|
|
{ "properties", DUMP_INCLUDE_PROPERTIES },
|
|
|
|
{ "tags", DUMP_INCLUDE_TAGS } } },
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_string = &output_file_name, .name = "output" },
|
|
|
|
{ .opt_bool = &gzip_output, .name = "gzip" },
|
|
|
|
{ .opt_inherit = notmuch_shared_options },
|
|
|
|
{ }
|
2014-03-25 18:07:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
opt_index = parse_arguments (argc, argv, options, 1);
|
|
|
|
if (opt_index < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2015-04-05 15:13:03 +02:00
|
|
|
notmuch_process_shared_options (argv[0]);
|
|
|
|
|
2016-03-22 11:54:50 +01:00
|
|
|
if (include == 0)
|
2016-06-13 03:05:54 +02:00
|
|
|
include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
|
2016-03-22 11:54:50 +01:00
|
|
|
|
2014-03-25 18:07:49 +01:00
|
|
|
if (opt_index < argc) {
|
|
|
|
query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
|
|
|
|
if (query_str == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:48:57 +01:00
|
|
|
ret = notmuch_database_dump (notmuch, output_file_name, query_str,
|
2016-03-22 11:54:50 +01:00
|
|
|
output_format, include, gzip_output);
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2012-04-22 14:07:53 +02:00
|
|
|
notmuch_database_destroy (notmuch);
|
2009-11-12 05:29:30 +01:00
|
|
|
|
2014-03-25 18:07:49 +01:00
|
|
|
return ret;
|
2009-11-10 21:03:05 +01:00
|
|
|
}
|