Re: notmuch and "mute" -- useful to anyone?
[notmuch-archives.git] / 62 / f3aeb071b241771170e497c7fcc9d90918970b
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 A9BC1431FC0\r
6         for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:25:38 -0700 (PDT)\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 er+1xHe7a6Dm for <notmuch@notmuchmail.org>;\r
17         Sat, 22 Jun 2013 21:25:33 -0700 (PDT)\r
18 Received: from mail-pb0-f44.google.com (mail-pb0-f44.google.com\r
19         [209.85.160.44]) (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 BC9EA429E27\r
22         for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:48 -0700 (PDT)\r
23 Received: by mail-pb0-f44.google.com with SMTP id uo1so9534267pbc.31\r
24         for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:48 -0700 (PDT)\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=qS2cXxe91dFezu3yAB8P4nKJrfVOwE9mUljEiB+ZXqU=;\r
28         b=EGOId9U/S3cyUq0F4HonoS6voioG47JOxShSrxRhfyYOH7ZsGBuZ/5Gce70weiCh44\r
29         tD3PLLHugUg4WmVhE3nXqE3rqi+Ovd5dujuhIzTSuXGYtIf7E8k8qXOsg+izj1FusI1q\r
30         Pc8CgbrjRHekRWUQK8ZKGXKM+Xy4+qFJOD82ShXMVsP5Mvmr2l0tzZlEy+kXpp9bX1KT\r
31         kE8nJuZLzt0FqVuRxsvCqywLhyHLNnl8BW0sA7QZjySuddJU0ZuUKNjRQ+MjTbxMVARa\r
32         n51s8PaHWB6LUW3j6IA2WTV6RvlBsqHe1mpTpj1ZhiEtghoCwUb0sTePm4Es9I+LraM/\r
33         0GoQ==\r
34 X-Received: by 10.66.102.70 with SMTP id fm6mr22202082pab.78.1371961488073;\r
35         Sat, 22 Jun 2013 21:24:48 -0700 (PDT)\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.\r
37         [220.233.42.215]) by mx.google.com with ESMTPSA id\r
38         dj5sm11850307pbc.25.2013.06.22.21.24.45 for <multiple recipients>\r
39         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
40         Sat, 22 Jun 2013 21:24:47 -0700 (PDT)\r
41 From: Peter Wang <novalazy@gmail.com>\r
42 To: notmuch@notmuchmail.org\r
43 Subject: [PATCH v7 10/12] insert: add --create-folder option\r
44 Date: Sun, 23 Jun 2013 14:24:03 +1000\r
45 Message-Id: <1371961445-15182-11-git-send-email-novalazy@gmail.com>\r
46 X-Mailer: git-send-email 1.7.12.1\r
47 In-Reply-To: <1371961445-15182-1-git-send-email-novalazy@gmail.com>\r
48 References: <1371961445-15182-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, 23 Jun 2013 04:25:38 -0000\r
62 \r
63 Allow the insert command to create the maildir folder\r
64 into which the new message should be delivered.\r
65 ---\r
66  notmuch-insert.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
67  1 file changed, 100 insertions(+)\r
68 \r
69 diff --git a/notmuch-insert.c b/notmuch-insert.c\r
70 index 5a4b3ea..8d0235d 100644\r
71 --- a/notmuch-insert.c\r
72 +++ b/notmuch-insert.c\r
73 @@ -100,6 +100,99 @@ check_folder_name (const char *folder)\r
74      }\r
75  }\r
76  \r
77 +/* Make the given directory, succeeding if it already exists. */\r
78 +static notmuch_bool_t\r
79 +make_directory (char *path, int mode)\r
80 +{\r
81 +    notmuch_bool_t ret;\r
82 +    char *slash;\r
83 +\r
84 +    if (mkdir (path, mode) != 0)\r
85 +       return (errno == EEXIST);\r
86 +\r
87 +    /* Sync the parent directory for durability. */\r
88 +    ret = TRUE;\r
89 +    slash = strrchr (path, '/');\r
90 +    if (slash) {\r
91 +       *slash = '\0';\r
92 +       ret = sync_dir (path);\r
93 +       *slash = '/';\r
94 +    }\r
95 +    return ret;\r
96 +}\r
97 +\r
98 +/* Make the given directory including its parent directories as necessary.\r
99 + * Return TRUE on success, FALSE on error. */\r
100 +static notmuch_bool_t\r
101 +make_directory_and_parents (char *path, int mode)\r
102 +{\r
103 +    struct stat st;\r
104 +    char *start;\r
105 +    char *end;\r
106 +    notmuch_bool_t ret;\r
107 +\r
108 +    /* First check the common case: directory already exists. */\r
109 +    if (stat (path, &st) == 0)\r
110 +       return S_ISDIR (st.st_mode) ? TRUE : FALSE;\r
111 +\r
112 +    for (start = path; *start != '\0'; start = end + 1) {\r
113 +       /* start points to the first unprocessed character.\r
114 +        * Find the next slash from start onwards. */\r
115 +       end = strchr (start, '/');\r
116 +\r
117 +       /* If there are no more slashes then all the parent directories\r
118 +        * have been made.  Now attempt to make the whole path. */\r
119 +       if (end == NULL)\r
120 +           return make_directory (path, mode);\r
121 +\r
122 +       /* Make the path up to the next slash, unless the current\r
123 +        * directory component is actually empty. */\r
124 +       if (end > start) {\r
125 +           *end = '\0';\r
126 +           ret = make_directory (path, mode);\r
127 +           *end = '/';\r
128 +           if (! ret)\r
129 +               return FALSE;\r
130 +       }\r
131 +    }\r
132 +\r
133 +    return TRUE;\r
134 +}\r
135 +\r
136 +/* Create the given maildir folder, i.e. dir and its subdirectories\r
137 + * 'cur', 'new', 'tmp'. */\r
138 +static notmuch_bool_t\r
139 +maildir_create_folder (void *ctx, const char *dir)\r
140 +{\r
141 +    const int mode = 0700;\r
142 +    char *subdir;\r
143 +    char *tail;\r
144 +\r
145 +    /* Create 'cur' directory, including parent directories. */\r
146 +    subdir = talloc_asprintf (ctx, "%s/cur", dir);\r
147 +    if (! subdir) {\r
148 +       fprintf (stderr, "Out of memory.\n");\r
149 +       return FALSE;\r
150 +    }\r
151 +    if (! make_directory_and_parents (subdir, mode))\r
152 +       return FALSE;\r
153 +\r
154 +    tail = subdir + strlen (subdir) - 3;\r
155 +\r
156 +    /* Create 'new' directory. */\r
157 +    strcpy (tail, "new");\r
158 +    if (! make_directory (subdir, mode))\r
159 +       return FALSE;\r
160 +\r
161 +    /* Create 'tmp' directory. */\r
162 +    strcpy (tail, "tmp");\r
163 +    if (! make_directory (subdir, mode))\r
164 +       return FALSE;\r
165 +\r
166 +    talloc_free (subdir);\r
167 +    return TRUE;\r
168 +}\r
169 +\r
170  /* Open a unique file in the 'tmp' sub-directory of dir.\r
171   * Returns the file descriptor on success, or -1 on failure.\r
172   * On success, file paths for the message in the 'tmp' and 'new'\r
173 @@ -304,6 +397,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
174      tag_op_list_t *tag_ops;\r
175      char *query_string = NULL;\r
176      const char *folder = NULL;\r
177 +    notmuch_bool_t create_folder = FALSE;\r
178      const char *maildir;\r
179      int opt_index;\r
180      unsigned int i;\r
181 @@ -311,6 +405,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
182  \r
183      notmuch_opt_desc_t options[] = {\r
184         { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
185 +       { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },\r
186         { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
187      };\r
188  \r
189 @@ -355,6 +450,11 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
190             fprintf (stderr, "Out of memory\n");\r
191             return 1;\r
192         }\r
193 +       if (create_folder && ! maildir_create_folder (config, maildir)) {\r
194 +           fprintf (stderr, "Error: creating maildir %s: %s\n",\r
195 +                    maildir, strerror (errno));\r
196 +           return 1;\r
197 +       }\r
198      }\r
199  \r
200      /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying\r
201 -- \r
202 1.7.12.1\r
203 \r