X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-restore.c;h=d6429efb404519fd23dfaf6c1450c22b62782937;hp=c54d51328b51b31d9e3e500ec3fc5a06286a95e4;hb=651da30c0941081cf730930fc1a7cac34954ca0e;hpb=3863755f6d9f3e7666c1484822384c036a4426c3 diff --git a/notmuch-restore.c b/notmuch-restore.c index c54d5132..d6429efb 100644 --- a/notmuch-restore.c +++ b/notmuch-restore.c @@ -13,7 +13,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/ . + * along with this program. If not, see https://www.gnu.org/licenses/ . * * Author: Carl Worth */ @@ -22,6 +22,106 @@ #include "hex-escape.h" #include "tag-util.h" #include "string-util.h" +#include "zlib-extra.h" + +static int +process_config_line (notmuch_database_t *notmuch, const char* line) +{ + const char *key_p, *val_p; + char *key, *val; + size_t key_len,val_len; + const char *delim = " \t\n"; + int ret = EXIT_FAILURE; + + void *local = talloc_new(NULL); + + key_p = strtok_len_c (line, delim, &key_len); + val_p = strtok_len_c (key_p+key_len, delim, &val_len); + + key = talloc_strndup (local, key_p, key_len); + val = talloc_strndup (local, val_p, val_len); + if (hex_decode_inplace (key) != HEX_SUCCESS || + hex_decode_inplace (val) != HEX_SUCCESS ) { + fprintf (stderr, "hex decoding failure on line %s\n", line); + goto DONE; + } + + if (print_status_database ("notmuch restore", notmuch, + notmuch_database_set_config (notmuch, key, val))) + goto DONE; + + ret = EXIT_SUCCESS; + + DONE: + talloc_free (local); + return ret; +} + +static int +process_properties_line (notmuch_database_t *notmuch, const char* line) + +{ + const char *id_p, *tok; + size_t id_len = 0, tok_len = 0; + char *id; + + notmuch_message_t *message = NULL; + const char *delim = " \t\n"; + int ret = EXIT_FAILURE; + + void *local = talloc_new (NULL); + + id_p = strtok_len_c (line, delim, &id_len); + id = talloc_strndup (local, id_p, id_len); + if (hex_decode_inplace (id) != HEX_SUCCESS) { + fprintf (stderr, "hex decoding failure on line %s\n", line); + goto DONE; + } + + if (print_status_database ("notmuch restore", notmuch, + notmuch_database_find_message (notmuch, id, &message))) + goto DONE; + + if (print_status_database ("notmuch restore", notmuch, + notmuch_message_remove_all_properties (message, NULL))) + goto DONE; + + tok = id_p + id_len; + + while ((tok = strtok_len_c (tok + tok_len, delim, &tok_len)) != NULL) { + char *key, *value; + size_t off = strcspn (tok, "="); + if (off > tok_len) { + fprintf (stderr, "unparsable token %s\n", tok); + goto DONE; + } + + key = talloc_strndup (local, tok, off); + value = talloc_strndup (local, tok + off + 1, tok_len - off - 1); + + if (hex_decode_inplace (key) != HEX_SUCCESS) { + fprintf (stderr, "hex decoding failure on key %s\n", key); + goto DONE; + } + + if (hex_decode_inplace (value) != HEX_SUCCESS) { + fprintf (stderr, "hex decoding failure on value %s\n", value); + goto DONE; + } + + if (print_status_database ("notmuch restore", notmuch, + notmuch_message_add_property (message, key, value))) + goto DONE; + + } + + ret = EXIT_SUCCESS; + + DONE: + talloc_free (local); + return ret; +} + static regex_t regex; @@ -128,14 +228,15 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) tag_op_list_t *tag_ops; char *input_file_name = NULL; - FILE *input = stdin; + const char *name_for_error = NULL; + gzFile input = NULL; char *line = NULL; void *line_ctx = NULL; - size_t line_size; ssize_t line_len; int ret = 0; int opt_index; + int include = 0; int input_format = DUMP_FORMAT_AUTO; if (notmuch_database_open (notmuch_config_get_database_path (config), @@ -151,50 +252,110 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) { "batch-tag", DUMP_FORMAT_BATCH_TAG }, { "sup", DUMP_FORMAT_SUP }, { 0, 0 } } }, + { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I', + (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG }, + { "properties", DUMP_INCLUDE_PROPERTIES }, + { "tags", DUMP_INCLUDE_TAGS} } }, + { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 }, { NOTMUCH_OPT_BOOLEAN, &accumulate, "accumulate", 'a', 0 }, + { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 }, { 0, 0, 0, 0, 0 } }; opt_index = parse_arguments (argc, argv, options, 1); - if (opt_index < 0) - return EXIT_FAILURE; + if (opt_index < 0) { + ret = EXIT_FAILURE; + goto DONE; + } + + notmuch_process_shared_options (argv[0]); + notmuch_exit_if_unmatched_db_uuid (notmuch); + + if (include == 0) { + include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_PROPERTIES | DUMP_INCLUDE_TAGS; + } + + name_for_error = input_file_name ? input_file_name : "stdin"; if (! accumulate) flags |= TAG_FLAG_REMOVE_ALL; - if (input_file_name) { - input = fopen (input_file_name, "r"); - if (input == NULL) { - fprintf (stderr, "Error opening %s for reading: %s\n", - input_file_name, strerror (errno)); - return EXIT_FAILURE; + errno = 0; + if (input_file_name) + input = gzopen (input_file_name, "r"); + else { + int infd = dup (STDIN_FILENO); + if (infd < 0) { + fprintf (stderr, "Error duping stdin: %s\n", + strerror (errno)); + ret = EXIT_FAILURE; + goto DONE; } + input = gzdopen (infd, "r"); + if (! input) + close (infd); + } + + if (input == NULL) { + fprintf (stderr, "Error opening %s for (gzip) reading: %s\n", + name_for_error, strerror (errno)); + ret = EXIT_FAILURE; + goto DONE; } if (opt_index < argc) { fprintf (stderr, "Unused positional parameter: %s\n", argv[opt_index]); - return EXIT_FAILURE; + ret = EXIT_FAILURE; + goto DONE; } tag_ops = tag_op_list_create (config); if (tag_ops == NULL) { fprintf (stderr, "Out of memory.\n"); - return EXIT_FAILURE; + ret = EXIT_FAILURE; + goto DONE; } do { - line_len = getline (&line, &line_size, input); + util_status_t status; + + status = gz_getline (line_ctx, &line, &line_len, input); /* empty input file not considered an error */ - if (line_len < 0) - return EXIT_SUCCESS; + if (status == UTIL_EOF) { + ret = EXIT_SUCCESS; + goto DONE; + } + + if (status) { + fprintf (stderr, "Error reading (gzipped) input: %s\n", + gz_error_string(status, input)); + ret = EXIT_FAILURE; + goto DONE; + } + + if ((include & DUMP_INCLUDE_CONFIG) && line_len >= 2 && line[0] == '#' && line[1] == '@') { + ret = process_config_line(notmuch, line+2); + if (ret) + goto DONE; + } + if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] == '=') { + ret = process_properties_line (notmuch, line + 2); + if (ret) + goto DONE; + } } while ((line_len == 0) || (line[0] == '#') || /* the cast is safe because we checked about for line_len < 0 */ (strspn (line, " \t\n") == (unsigned)line_len)); + if (! ((include & DUMP_INCLUDE_TAGS) || (include & DUMP_INCLUDE_PROPERTIES))) { + ret = EXIT_SUCCESS; + goto DONE; + } + char *p; for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) { if (*p == '(') @@ -217,6 +378,13 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) talloc_free (line_ctx); line_ctx = talloc_new (config); + + if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] == '=') { + ret = process_properties_line (notmuch, line + 2); + if (ret) + goto DONE; + } + if (input_format == DUMP_FORMAT_SUP) { ret = parse_sup_line (line_ctx, line, &query_string, tag_ops); } else { @@ -254,21 +422,38 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) if (ret) break; - } while ((line_len = getline (&line, &line_size, input)) != -1); + } while (! (ret = gz_getline (line_ctx, &line, &line_len, input))); - if (line_ctx != NULL) - talloc_free (line_ctx); + + /* EOF is normal loop termination condition, UTIL_SUCCESS is + * impossible here */ + if (ret == UTIL_EOF) { + ret = EXIT_SUCCESS; + } else { + fprintf (stderr, "Error reading (gzipped) input: %s\n", + gz_error_string (ret, input)); + ret = EXIT_FAILURE; + } + + /* currently this should not be after DONE: since we don't + * know if the xregcomp was reached + */ if (input_format == DUMP_FORMAT_SUP) regfree (®ex); - if (line) - free (line); + DONE: + if (line_ctx != NULL) + talloc_free (line_ctx); - notmuch_database_destroy (notmuch); + if (notmuch) + notmuch_database_destroy (notmuch); - if (input != stdin) - fclose (input); + if (input && gzclose_r (input)) { + fprintf (stderr, "Error closing %s: %s\n", + name_for_error, gzerror (input, NULL)); + ret = EXIT_FAILURE; + } return ret ? EXIT_FAILURE : EXIT_SUCCESS; }