From: Jani Nikula Date: Sun, 1 Oct 2017 20:53:10 +0000 (+0300) Subject: cli: strip trailing "/" from the final maildir path in notmuch insert X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d57da17fcd30a5cbe191ccd42906e489dd2bd6a3;p=notmuch.git cli: strip trailing "/" from the final maildir path in notmuch insert Several subtle interconnected changes here: - If the folder name passed as argument is the empty string "" or slash "/", the final maildir path would end up having "//" in it. We should strip the final maildir path, not folder. - The folder variable should really be const char *, another reason not to modify it. - The maildir variable is only const to let us point it at db_path directly. To be able to strip the maildir variable, always allocate it. Default folder to the empty string "", and don't treat folder not being present on the command line as anything special. As a side effect, we also create the cur/new/tmp in the top level directory if they're not there and --create-folder is given. --- diff --git a/notmuch-insert.c b/notmuch-insert.c index 648bd944..040b6aa0 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -452,12 +452,12 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) size_t new_tags_length; tag_op_list_t *tag_ops; char *query_string = NULL; - char *folder = NULL; + const char *folder = ""; notmuch_bool_t create_folder = FALSE; notmuch_bool_t keep = FALSE; notmuch_bool_t no_hooks = FALSE; notmuch_bool_t synchronize_flags; - const char *maildir; + char *maildir; char *newpath; int opt_index; unsigned int i; @@ -509,23 +509,21 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) return EXIT_FAILURE; } - if (folder == NULL) { - maildir = db_path; - } else { - strip_trailing (folder, '/'); - if (! is_valid_folder_name (folder)) { - fprintf (stderr, "Error: invalid folder name: '%s'\n", folder); - return EXIT_FAILURE; - } - maildir = talloc_asprintf (config, "%s/%s", db_path, folder); - if (! maildir) { - fprintf (stderr, "Out of memory\n"); - return EXIT_FAILURE; - } - if (create_folder && ! maildir_create_folder (config, maildir)) - return EXIT_FAILURE; + if (! is_valid_folder_name (folder)) { + fprintf (stderr, "Error: invalid folder name: '%s'\n", folder); + return EXIT_FAILURE; + } + + maildir = talloc_asprintf (config, "%s/%s", db_path, folder); + if (! maildir) { + fprintf (stderr, "Out of memory\n"); + return EXIT_FAILURE; } + strip_trailing (maildir, '/'); + if (create_folder && ! maildir_create_folder (config, maildir)) + return EXIT_FAILURE; + /* Set up our handler for SIGINT. We do not set SA_RESTART so that copying * from standard input may be interrupted. */ memset (&action, 0, sizeof (struct sigaction));