From: Peter Wang Date: Tue, 2 Apr 2013 22:41:52 +0000 (+1100) Subject: [PATCH v5 07/12] insert: add --folder option X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d24c001292cdc4e6575fab08ed587d03fd8b027b;p=notmuch-archives.git [PATCH v5 07/12] insert: add --folder option --- diff --git a/98/86c5c24d21e55c7f89f9a4ff1ce6a9ec3c0cd6 b/98/86c5c24d21e55c7f89f9a4ff1ce6a9ec3c0cd6 new file mode 100644 index 000000000..fab8a3bf3 --- /dev/null +++ b/98/86c5c24d21e55c7f89f9a4ff1ce6a9ec3c0cd6 @@ -0,0 +1,147 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 76EA2431FAF + for ; Tue, 2 Apr 2013 15:42:59 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.099 +X-Spam-Level: +X-Spam-Status: No, score=-0.099 tagged_above=-999 required=5 + tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, + FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 2-63gk4Sy69I for ; + Tue, 2 Apr 2013 15:42:58 -0700 (PDT) +Received: from mail-pd0-f180.google.com (mail-pd0-f180.google.com + [209.85.192.180]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 345E2431FB6 + for ; Tue, 2 Apr 2013 15:42:51 -0700 (PDT) +Received: by mail-pd0-f180.google.com with SMTP id q11so488555pdj.39 + for ; Tue, 02 Apr 2013 15:42:50 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to + :references; bh=jV//+uBvocbFAKz2inXvoDkxaR0m4vsBVApXE8uXQxc=; + b=nIjDz9IFgbJzDGbE+x5//bJwtXdL9ed0GGuu2FsGCCgKoBnEqq/DcIens3izPJsEk5 + lSAjujXHxyfjYXmb1mXl6JEhzpm8yTWpcQUhnVS1oAeFH0DrzkDJsPJlbRRhsRxWdn5g + e2PC6GyBD8SV8hlQCBkQj4DcMQJMWH+pYZOvZhHhO0LD7/q7u6dTA33X5xHTUpvpwQ6W + o6y8NmKkbPJEUtkiKEWnrE3e4fj0aacf8WQhrc6KDaFk2/qQxJPPciAMUzhmKx4LTMhB + x/aa2zR2H+KF/rs+YUYH6kgIy4NyxTa3x3XiYNhShC5EO0pqyzz+8UM46AoG4moORyiM + wZrQ== +X-Received: by 10.68.253.68 with SMTP id zy4mr26830147pbc.38.1364942570501; + Tue, 02 Apr 2013 15:42:50 -0700 (PDT) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) + by mx.google.com with ESMTPS id sl9sm3272125pbc.17.2013.04.02.15.42.48 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Tue, 02 Apr 2013 15:42:49 -0700 (PDT) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v5 07/12] insert: add --folder option +Date: Wed, 3 Apr 2013 09:41:52 +1100 +Message-Id: <1364942517-6982-8-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.7.12.1 +In-Reply-To: <1364942517-6982-1-git-send-email-novalazy@gmail.com> +References: <1364942517-6982-1-git-send-email-novalazy@gmail.com> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Tue, 02 Apr 2013 22:43:00 -0000 + +Allow the new message to be inserted into a folder within the Maildir +hierarchy instead of the top-level folder. +--- + notmuch-insert.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- + 1 file changed, 45 insertions(+), 2 deletions(-) + +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 19b1cf9..778ac04 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -83,6 +83,23 @@ sync_dir (const char *dir) + return ret; + } + ++/* Check the specified folder name does not contain a directory ++ * component ".." to prevent writes outside of the Maildir hierarchy. */ ++static notmuch_bool_t ++check_folder_name (const char *folder) ++{ ++ const char *p = folder; ++ ++ for (;;) { ++ if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/')) ++ return FALSE; ++ p = strchr (p, '/'); ++ if (!p) ++ return TRUE; ++ p++; ++ } ++} ++ + /* Open a unique file in the Maildir 'tmp' directory. + * Returns the file descriptor on success, or -1 on failure. + * On success, file paths for the message in the 'tmp' and 'new' +@@ -287,11 +304,25 @@ 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; ++ const char *folder = NULL; + const char *maildir; +- int opt_index = 1; ++ int opt_index; + unsigned int i; + notmuch_bool_t ret; + ++ notmuch_opt_desc_t options[] = { ++ { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 }, ++ { NOTMUCH_OPT_END, 0, 0, 0, 0 } ++ }; ++ ++ opt_index = parse_arguments (argc, argv, options, 1); ++ ++ if (opt_index < 0) { ++ fprintf (stderr, "Error: bad argument to notmuch insert: %s\n", ++ argv[-opt_index]); ++ return 1; ++ } ++ + db_path = notmuch_config_get_database_path (config); + new_tags = notmuch_config_get_new_tags (config, &new_tags_length); + +@@ -314,7 +345,19 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + return 1; + } + +- maildir = db_path; ++ if (folder == NULL) { ++ maildir = db_path; ++ } else { ++ if (! check_folder_name (folder)) { ++ fprintf (stderr, "Error: bad folder name: %s\n", folder); ++ return 1; ++ } ++ maildir = talloc_asprintf (config, "%s/%s", db_path, folder); ++ if (! maildir) { ++ fprintf (stderr, "Out of memory\n"); ++ return 1; ++ } ++ } + + /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying + * from standard input may be interrupted. */ +-- +1.7.12.1 +