1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2013 Peter Wang
5 * Based in part on notmuch-deliver
6 * Copyright © 2010 Ali Polatel
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses/ .
21 * Author: Peter Wang <novalazy@gmail.com>
24 #include "notmuch-client.h"
27 #include <sys/types.h>
31 static volatile sig_atomic_t interrupted;
34 handle_sigint (unused (int sig))
36 static char msg[] = "Stopping... \n";
38 /* This write is "opportunistic", so it's okay to ignore the
39 * result. It is not required for correctness, and if it does
40 * fail or produce a short write, we want to get out of the signal
41 * handler as quickly as possible, not retry it. */
42 IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
46 /* Like gethostname but guarantees that a null-terminated hostname is
47 * returned, even if it has to make one up. Invalid characters are
48 * substituted such that the hostname can be used within a filename.
51 safe_gethostname (char *hostname, size_t len)
55 if (gethostname (hostname, len) == -1) {
56 strncpy (hostname, "unknown", len);
58 hostname[len - 1] = '\0';
60 for (p = hostname; *p != '\0'; p++) {
61 if (*p == '/' || *p == ':')
66 /* Call fsync() on a directory path. */
68 sync_dir (const char *dir)
73 fd = open (dir, O_RDONLY);
75 fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno));
78 ret = (fsync (fd) == 0);
80 fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno));
86 /* Open a unique file in the 'tmp' sub-directory of dir.
87 * Returns the file descriptor on success, or -1 on failure.
88 * On success, file paths for the message in the 'tmp' and 'new'
89 * directories are returned via tmppath and newpath,
90 * and the path of the 'new' directory itself in newdir. */
92 maildir_open_tmp_file (void *ctx, const char *dir,
93 char **tmppath, char **newpath, char **newdir)
101 /* We follow the Dovecot file name generation algorithm. */
103 safe_gethostname (hostname, sizeof (hostname));
105 gettimeofday (&tv, NULL);
106 filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s",
107 tv.tv_sec, tv.tv_usec, pid, hostname);
109 fprintf (stderr, "Out of memory\n");
113 *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);
115 fprintf (stderr, "Out of memory\n");
119 fd = open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
120 } while (fd == -1 && errno == EEXIST);
123 fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno));
127 *newdir = talloc_asprintf (ctx, "%s/new", dir);
128 *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename);
129 if (! *newdir || ! *newpath) {
130 fprintf (stderr, "Out of memory\n");
136 talloc_free (filename);
141 /* Copy the contents of standard input (fdin) into fdout.
142 * Returns TRUE if a non-empty file was written successfully.
143 * Otherwise, return FALSE. */
144 static notmuch_bool_t
145 copy_stdin (int fdin, int fdout)
147 notmuch_bool_t empty = TRUE;
149 while (! interrupted) {
154 remain = read (fdin, buf, sizeof (buf));
160 fprintf (stderr, "Error: reading from standard input: %s\n",
167 ssize_t written = write (fdout, p, remain);
168 if (written < 0 && errno == EINTR)
171 fprintf (stderr, "Error: writing to temporary file: %s",
178 } while (remain > 0);
181 return (!interrupted && !empty);
184 /* Add the specified message file to the notmuch database, applying tags.
185 * The file is renamed to encode notmuch tags as maildir flags. */
187 add_file_to_database (notmuch_database_t *notmuch, const char *path,
188 tag_op_list_t *tag_ops)
190 notmuch_message_t *message;
191 notmuch_status_t status;
193 status = notmuch_database_add_message (notmuch, path, &message);
195 case NOTMUCH_STATUS_SUCCESS:
196 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
199 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
200 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
201 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
202 case NOTMUCH_STATUS_OUT_OF_MEMORY:
203 case NOTMUCH_STATUS_FILE_ERROR:
204 case NOTMUCH_STATUS_NULL_POINTER:
205 case NOTMUCH_STATUS_TAG_TOO_LONG:
206 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
207 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
208 case NOTMUCH_STATUS_LAST_STATUS:
209 fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",
210 path, notmuch_status_to_string (status));
214 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
215 /* Don't change tags of an existing message. */
216 status = notmuch_message_tags_to_maildir_flags (message);
217 if (status != NOTMUCH_STATUS_SUCCESS)
218 fprintf (stderr, "Error: failed to sync tags to maildir flags\n");
220 tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);
223 notmuch_message_destroy (message);
226 static notmuch_bool_t
227 insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
228 const char *dir, tag_op_list_t *tag_ops)
236 fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir);
240 cleanup_path = tmppath;
242 if (! copy_stdin (fdin, fdout))
245 if (fsync (fdout) != 0) {
246 fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno));
253 /* Atomically move the new message file from the Maildir 'tmp' directory
254 * to the 'new' directory. We follow the Dovecot recommendation to
255 * simply use rename() instead of link() and unlink().
256 * See also: http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery
258 if (rename (tmppath, newpath) != 0) {
259 fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno));
263 cleanup_path = newpath;
265 if (! sync_dir (newdir))
268 /* Even if adding the message to the notmuch database fails,
269 * the message is on disk and we consider the delivery completed. */
270 add_file_to_database (notmuch, newpath, tag_ops);
277 unlink (cleanup_path);
282 notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
284 notmuch_database_t *notmuch;
285 struct sigaction action;
287 const char **new_tags;
288 size_t new_tags_length;
289 tag_op_list_t *tag_ops;
290 char *query_string = NULL;
296 db_path = notmuch_config_get_database_path (config);
297 new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
299 tag_ops = tag_op_list_create (config);
300 if (tag_ops == NULL) {
301 fprintf (stderr, "Out of memory.\n");
304 for (i = 0; i < new_tags_length; i++) {
305 if (tag_op_list_append (tag_ops, new_tags[i], FALSE))
309 if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
310 &query_string, tag_ops))
313 if (*query_string != '\0') {
314 fprintf (stderr, "Error: unexpected query string: %s\n", query_string);
320 /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying
321 * from standard input may be interrupted. */
322 memset (&action, 0, sizeof (struct sigaction));
323 action.sa_handler = handle_sigint;
324 sigemptyset (&action.sa_mask);
326 sigaction (SIGINT, &action, NULL);
328 if (notmuch_database_open (notmuch_config_get_database_path (config),
329 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
332 ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops);
334 notmuch_database_destroy (notmuch);
336 return (ret) ? 0 : 1;