Re: [PATCH v2 3/7] fix thread breakage via ghost-on-removal
[notmuch-archives.git] / ad / bc465cf984d93d63ccc46a13b4c03b1b1688a8
1 Return-Path: <novalazy@gmail.com>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id CD93F431FAF\r
6         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:19 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.799\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
12         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
13         FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id 55G8P7IW1bWR for <notmuch@notmuchmail.org>;\r
17         Sat, 24 Nov 2012 17:18:18 -0800 (PST)\r
18 Received: from mail-pa0-f53.google.com (mail-pa0-f53.google.com\r
19         [209.85.220.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id A82FD431FAE\r
22         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:18 -0800 (PST)\r
23 Received: by mail-pa0-f53.google.com with SMTP id hz1so3911431pad.26\r
24         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:18 -0800 (PST)\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
26         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
27         bh=xn5U2n/VUXiTHcThk3h/4CVQiEsyI/PGZWaFXJa0Eaw=;\r
28         b=KVRQpQkWj0ShcjtmeWQPz1KQK+vyf4exBK1+AJurUmaK1GBal6PoiCZV1F3z71B+Sq\r
29         clOWmOB1XmdN5YerYnY90PK23aREg3jWXVGgQO+Mvt+QkLwrATqBPFW5hPT7TzgxzXM6\r
30         M1Z1MkuoD1DZILTNZ8UmPsE3QkH4j37ACB8ZChX8n7z5QyZE4osCEfmBHF5ofeGZNyYz\r
31         PmMedYVnHhDw4Eos80p57XMriyGhfBtcxkoZb9fns2+ygz/JDjTTJGV9VIyN4ygLs3Hb\r
32         8Frfgw6rsSYRjV0FLHY6KxqYIeNsp/1jaxZPFhsT/CY/J2g+JQZWkF8/SXXpMhnLfuSR\r
33         lvFw==\r
34 Received: by 10.68.218.97 with SMTP id pf1mr26282231pbc.96.1353806298424;\r
35         Sat, 24 Nov 2012 17:18:18 -0800 (PST)\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.\r
37         [220.233.42.215])\r
38         by mx.google.com with ESMTPS id gu5sm6232468pbc.10.2012.11.24.17.18.15\r
39         (version=TLSv1/SSLv3 cipher=OTHER);\r
40         Sat, 24 Nov 2012 17:18:17 -0800 (PST)\r
41 From: Peter Wang <novalazy@gmail.com>\r
42 To: notmuch@notmuchmail.org\r
43 Subject: [PATCH v2 13/20] insert: add --create-folder option\r
44 Date: Sun, 25 Nov 2012 12:16:39 +1100\r
45 Message-Id: <1353806206-29133-14-git-send-email-novalazy@gmail.com>\r
46 X-Mailer: git-send-email 1.7.12.1\r
47 In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com>\r
48 References: <1353806206-29133-1-git-send-email-novalazy@gmail.com>\r
49 X-BeenThere: notmuch@notmuchmail.org\r
50 X-Mailman-Version: 2.1.13\r
51 Precedence: list\r
52 List-Id: "Use and development of the notmuch mail system."\r
53         <notmuch.notmuchmail.org>\r
54 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
56 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
57 List-Post: <mailto:notmuch@notmuchmail.org>\r
58 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
59 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
60         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
61 X-List-Received-Date: Sun, 25 Nov 2012 01:18:19 -0000\r
62 \r
63 Support an option to create a new folder in the maildir.\r
64 ---\r
65  notmuch-insert.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
66  1 file changed, 92 insertions(+)\r
67 \r
68 diff --git a/notmuch-insert.c b/notmuch-insert.c\r
69 index 81a528c..b7aef95 100644\r
70 --- a/notmuch-insert.c\r
71 +++ b/notmuch-insert.c\r
72 @@ -55,6 +55,91 @@ check_folder_name (const char *folder)\r
73      }\r
74  }\r
75  \r
76 +/* Make the given directory but succeed if it already exists. */\r
77 +static int\r
78 +mkdir_exists_ok (const char *path, int mode)\r
79 +{\r
80 +    int ret;\r
81 +\r
82 +    ret = mkdir (path, mode);\r
83 +    if (ret == 0 || errno == EEXIST)\r
84 +       return 0;\r
85 +    else\r
86 +       return ret;\r
87 +}\r
88 +\r
89 +/* Make the given directory including its parent directory as necessary.\r
90 + * Return 0 on success, -1 on error. */\r
91 +static int\r
92 +mkdir_parents (char *path, int mode)\r
93 +{\r
94 +    struct stat st;\r
95 +    char *start;\r
96 +    char *end;\r
97 +    int ret;\r
98 +\r
99 +    /* First check the common case: directory already exists. */\r
100 +    if (stat (path, &st) == 0)\r
101 +       return (S_ISDIR (st.st_mode)) ? 0 : -1;\r
102 +\r
103 +    for (start = path; *start != '\0'; start = end + 1) {\r
104 +       /* start points to the first unprocessed character.\r
105 +        * Find the next slash from start onwards. */\r
106 +       end = strchr (start, '/');\r
107 +\r
108 +       /* If there are no more slashes then all the parent directories\r
109 +        * have been made.  Now attempt to make the whole path. */\r
110 +       if (end == NULL)\r
111 +           return mkdir_exists_ok (path, mode);\r
112 +\r
113 +       /* Make the path up to the next slash, unless the current\r
114 +        * directory component is actually empty. */\r
115 +       if (end > start) {\r
116 +           *end = '\0';\r
117 +           ret = mkdir_exists_ok (path, mode);\r
118 +           *end = '/';\r
119 +           if (ret != 0)\r
120 +               return ret;\r
121 +       }\r
122 +    }\r
123 +\r
124 +    return 0;\r
125 +}\r
126 +\r
127 +/* Create the given maildir folder, i.e. dir and its subdirectories\r
128 + * 'cur', 'new', 'tmp'. */\r
129 +static notmuch_bool_t\r
130 +maildir_create_folder (void *ctx, const char *dir)\r
131 +{\r
132 +    const int mode = 0700;\r
133 +    char *subdir;\r
134 +    char *tail;\r
135 +\r
136 +    /* Create 'cur' directory, including parent directories. */\r
137 +    subdir = talloc_asprintf (ctx, "%s/cur", dir);\r
138 +    if (! subdir) {\r
139 +       fprintf (stderr, "Out of memory.\n");\r
140 +       return FALSE;\r
141 +    }\r
142 +    if (mkdir_parents (subdir, mode) != 0)\r
143 +       return FALSE;\r
144 +\r
145 +    tail = subdir + strlen (subdir) - 3;\r
146 +\r
147 +    /* Create 'new' directory. */\r
148 +    strcpy (tail, "new");\r
149 +    if (mkdir_exists_ok (subdir, mode) != 0)\r
150 +       return FALSE;\r
151 +\r
152 +    /* Create 'tmp' directory. */\r
153 +    strcpy (tail, "tmp");\r
154 +    if (mkdir_exists_ok (subdir, mode) != 0)\r
155 +       return FALSE;\r
156 +\r
157 +    talloc_free (subdir);\r
158 +    return TRUE;\r
159 +}\r
160 +\r
161  /* Open a unique file in the Maildir 'tmp' directory.\r
162   * Returns the file descriptor on success, or -1 on failure.\r
163   * On success, file paths into the 'tmp' and 'new' directories are returned\r
164 @@ -272,6 +357,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
165      const char **new_tags;\r
166      size_t new_tags_length;\r
167      const char *folder = NULL;\r
168 +    notmuch_bool_t create_folder = FALSE;\r
169      notmuch_tag_operation_t *tag_ops;\r
170      int tag_ops_count;\r
171      char *maildir;\r
172 @@ -280,6 +366,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
173  \r
174      notmuch_opt_desc_t options[] = {\r
175         { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
176 +       { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },\r
177         { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
178      };\r
179  \r
180 @@ -332,6 +419,11 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
181         fprintf (stderr, "Out of memory\n");\r
182         return 1;\r
183      }\r
184 +    if (create_folder && ! maildir_create_folder (ctx, maildir)) {\r
185 +       fprintf (stderr, "Error: creating maildir %s: %s\n",\r
186 +                maildir, strerror (errno));\r
187 +       return 1;\r
188 +    }\r
189  \r
190      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
191                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
192 -- \r
193 1.7.12.1\r
194 \r