X-Git-Url: https://git.cworth.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-restore.c;h=1cce004a06d4dd8b0d8a402474a771c94e8fe155;hp=0025e2c316bed501fc19bb4412ffb3c858dac14d;hb=HEAD;hpb=4a6721970a42a9f86149fb5d395d1001fed2d305 diff --git a/notmuch-restore.c b/notmuch-restore.c index 0025e2c3..1cce004a 100644 --- a/notmuch-restore.c +++ b/notmuch-restore.c @@ -25,18 +25,18 @@ #include "zlib-extra.h" static int -process_config_line (notmuch_database_t *notmuch, const char* line) +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; + size_t key_len, val_len; const char *delim = " \t\n"; int ret = EXIT_FAILURE; - void *local = talloc_new(NULL); + 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); + 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); @@ -52,14 +52,13 @@ process_config_line (notmuch_database_t *notmuch, const char* line) ret = EXIT_SUCCESS; - DONE: + DONE: talloc_free (local); return ret; } static int -process_properties_line (notmuch_database_t *notmuch, const char* line) - +process_properties_line (notmuch_database_t *notmuch, const char *line) { const char *id_p, *tok; size_t id_len = 0, tok_len = 0; @@ -211,7 +210,7 @@ parse_sup_line (void *ctx, char *line, tok_len++; } - if (tag_op_list_append (tag_ops, tok, FALSE)) + if (tag_op_list_append (tag_ops, tok, false)) return -1; } @@ -220,10 +219,9 @@ parse_sup_line (void *ctx, char *line, } int -notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) +notmuch_restore_command (notmuch_database_t *notmuch, int argc, char *argv[]) { - notmuch_database_t *notmuch; - notmuch_bool_t accumulate = FALSE; + bool accumulate = false; tag_op_flag_t flags = 0; tag_op_list_t *tag_ops; @@ -238,24 +236,29 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) int opt_index; int include = 0; int input_format = DUMP_FORMAT_AUTO; - - if (notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) + int errnum; + notmuch_bool_t synchronize_flags; + + if (print_status_database ( + "notmuch restore", + notmuch, + notmuch_config_get_bool (notmuch, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS, + &synchronize_flags))) return EXIT_FAILURE; - if (notmuch_config_get_maildir_synchronize_flags (config)) + if (synchronize_flags) flags |= TAG_FLAG_MAILDIR_SYNC; notmuch_opt_desc_t options[] = { { .opt_keyword = &input_format, .name = "format", .keywords = - (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO }, - { "batch-tag", DUMP_FORMAT_BATCH_TAG }, - { "sup", DUMP_FORMAT_SUP }, - { 0, 0 } } }, + (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO }, + { "batch-tag", DUMP_FORMAT_BATCH_TAG }, + { "sup", DUMP_FORMAT_SUP }, + { 0, 0 } } }, { .opt_flags = &include, .name = "include", .keywords = - (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG }, - { "properties", DUMP_INCLUDE_PROPERTIES }, - { "tags", DUMP_INCLUDE_TAGS} } }, + (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG }, + { "properties", DUMP_INCLUDE_PROPERTIES }, + { "tags", DUMP_INCLUDE_TAGS } } }, { .opt_string = &input_file_name, .name = "input" }, { .opt_bool = &accumulate, .name = "accumulate" }, @@ -269,8 +272,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) goto DONE; } - notmuch_process_shared_options (argv[0]); - notmuch_exit_if_unmatched_db_uuid (notmuch); + notmuch_process_shared_options (notmuch, argv[0]); if (include == 0) { include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_PROPERTIES | DUMP_INCLUDE_TAGS; @@ -310,7 +312,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) goto DONE; } - tag_ops = tag_op_list_create (config); + tag_ops = tag_op_list_create (notmuch); if (tag_ops == NULL) { fprintf (stderr, "Out of memory.\n"); ret = EXIT_FAILURE; @@ -330,17 +332,18 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) if (status) { fprintf (stderr, "Error reading (gzipped) input: %s\n", - gz_error_string(status, input)); + 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); + ret = process_config_line (notmuch, line + 2); if (ret) goto DONE; } - if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] == '=') { + if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] == + '=') { ret = process_properties_line (notmuch, line + 2); if (ret) goto DONE; @@ -348,8 +351,8 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) } 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)); + /* 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; @@ -357,6 +360,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) } char *p; + for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) { if (*p == '(') input_format = DUMP_FORMAT_SUP; @@ -377,9 +381,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) if (line_ctx != NULL) talloc_free (line_ctx); - line_ctx = talloc_new (config); + line_ctx = talloc_new (notmuch); - if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] == '=') { + if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] == + '=') { ret = process_properties_line (notmuch, line + 2); if (ret) goto DONE; @@ -435,24 +440,27 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) ret = EXIT_FAILURE; } - /* currently this should not be after DONE: since we don't + /* 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); - DONE: + DONE: if (line_ctx != NULL) talloc_free (line_ctx); if (notmuch) notmuch_database_destroy (notmuch); - if (input && gzclose_r (input)) { - fprintf (stderr, "Error closing %s: %s\n", - name_for_error, gzerror (input, NULL)); - ret = EXIT_FAILURE; + if (input) { + errnum = gzclose_r (input); + if (errnum) { + fprintf (stderr, "Error closing %s: %d\n", + name_for_error, errnum); + ret = EXIT_FAILURE; + } } return ret ? EXIT_FAILURE : EXIT_SUCCESS;