From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 9721E431FB6 for ; Wed, 25 Jul 2012 06:44:50 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 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_LOW=-0.7] 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 58KsLlYWi8tb for ; Wed, 25 Jul 2012 06:44:49 -0700 (PDT) Received: from mail-gg0-f181.google.com (mail-gg0-f181.google.com [209.85.161.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id ACCE5431FAE for ; Wed, 25 Jul 2012 06:44:49 -0700 (PDT) Received: by ggnv5 with SMTP id v5so884306ggn.26 for ; Wed, 25 Jul 2012 06:44:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=7rnN/E5XAtJQxCnn8sc23jdqIk71FhaHAWCuDX+2vAs=; b=igq4KXvhdTjsQCA2yR/YRcfmfYggplcVgv3zoQnueDfYHPJEjS3lmNyZEPHhfGZ7OP 0fzjFZW5zCOTfOiqecvpPDBJGjMPo1xhawn0aV7JppvSQTFLd7yq3c0gwqeBLJ9OX0bK /tvkST9nJ9QZHUJKqwntQof+maHjLA0IWi3kuQF984QbPubzP85vPkOPeV1E9HFECiSx tZ692xu3tbhUV//VGMO2dXDDZU0oy+lFugoAoJqspT36+f85dcqNrcrGYG9arFNR2vjv OAfIdwMapReLzAQ0OBE2DAqGNQy+H+ytfbiaMEFyOF2/lNnkm5yr9Acja5mf63dm4NAK SfWw== Received: by 10.66.73.70 with SMTP id j6mr12832814pav.5.1343223888780; Wed, 25 Jul 2012 06:44:48 -0700 (PDT) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPS id qd10sm14414282pbb.38.2012.07.25.06.44.46 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 25 Jul 2012 06:44:47 -0700 (PDT) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH 16/18] insert: add --create-folder option Date: Wed, 25 Jul 2012 23:42:45 +1000 Message-Id: <1343223767-9812-16-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1343223767-9812-1-git-send-email-novalazy@gmail.com> References: <1343223767-9812-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: Wed, 25 Jul 2012 13:44:50 -0000 Support an option to create a new folder in the maildir. --- notmuch-insert.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 71 insertions(+), 0 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index a69dfe6..380c520 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -48,6 +48,70 @@ check_folder_name (const char *folder) } } +static int +mkdir_parents (void *ctx, const char *path, int mode) +{ + struct stat st; + char *pathcopy; + char *start; + char *end; + int ret; + + /* First check the common case: directory already exists. */ + if (stat (path, &st) == 0) { + return (S_ISDIR (st.st_mode)) ? 0 : -1; + } + + pathcopy = talloc_strdup (ctx, path); + ret = 0; + + for (start = pathcopy; *start != '\0'; start = end + 1) { + end = strchr (start + 1, '/'); + if (!end) { + ret = mkdir (path, mode); + break; + } + *end = '\0'; + ret = mkdir (pathcopy, mode); + if (ret != 0 && errno != EEXIST) { + break; + } + *end = '/'; + } + + talloc_free (pathcopy); + + return ret; +} + +static notmuch_bool_t +maildir_create (void *ctx, const char *dir) +{ + const int mode = 0755; + char *subdir; + char *end; + + /* Create 'cur' directory, including parent directories. */ + subdir = talloc_asprintf (ctx, "%s/cur", dir); + if (mkdir_parents (ctx, subdir, mode) != 0) + return FALSE; + + end = subdir + strlen (subdir); + + /* Create 'new' directory. */ + strcpy (end - 3, "new"); + if (mkdir (subdir, mode) != 0 && errno != EEXIST) + return FALSE; + + /* Create 'tmp' directory. */ + strcpy (end - 3, "tmp"); + if (mkdir (subdir, mode) != 0 && errno != EEXIST) + return FALSE; + + talloc_free (subdir); + return TRUE; +} + static notmuch_bool_t safe_gethostname (char *hostname, size_t hostname_size) { @@ -253,6 +317,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) const char **new_tags; size_t new_tags_length; const char *folder = NULL; + notmuch_bool_t create_folder = FALSE; tag_operation_t *tag_ops; int tag_ops_count = 0; char *maildir; @@ -262,6 +327,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) notmuch_opt_desc_t options[] = { { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 }, + { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 }, { NOTMUCH_OPT_END, 0, 0, 0, 0 } }; @@ -328,6 +394,11 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) return 1; } maildir = talloc_asprintf (ctx, "%s/%s", db_path, folder); + if (create_folder && ! maildir_create (ctx, maildir)) { + fprintf (stderr, "Error: creating maildir %s: %s\n", + maildir, strerror (errno)); + return 1; + } } else { maildir = talloc_asprintf (ctx, "%s", db_path); } -- 1.7.4.4