2013-06-23 14:20:45 +02:00
|
|
|
/* notmuch - Not much of an email program, (just index and search)
|
|
|
|
*
|
|
|
|
* Copyright © 2013 Peter Wang
|
|
|
|
*
|
|
|
|
* Based in part on notmuch-deliver
|
|
|
|
* Copyright © 2010 Ali Polatel
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2013-06-23 14:20:45 +02:00
|
|
|
*
|
|
|
|
* Author: Peter Wang <novalazy@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-client.h"
|
|
|
|
#include "tag-util.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2017-08-21 17:44:46 +02:00
|
|
|
#include "string-util.h"
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
static volatile sig_atomic_t interrupted;
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_sigint (unused (int sig))
|
|
|
|
{
|
|
|
|
static char msg[] = "Stopping... \n";
|
|
|
|
|
|
|
|
/* This write is "opportunistic", so it's okay to ignore the
|
|
|
|
* result. It is not required for correctness, and if it does
|
|
|
|
* fail or produce a short write, we want to get out of the signal
|
|
|
|
* handler as quickly as possible, not retry it. */
|
|
|
|
IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
|
|
|
|
interrupted = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like gethostname but guarantees that a null-terminated hostname is
|
|
|
|
* returned, even if it has to make one up. Invalid characters are
|
|
|
|
* substituted such that the hostname can be used within a filename.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
safe_gethostname (char *hostname, size_t len)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (gethostname (hostname, len) == -1) {
|
|
|
|
strncpy (hostname, "unknown", len);
|
|
|
|
}
|
|
|
|
hostname[len - 1] = '\0';
|
|
|
|
|
|
|
|
for (p = hostname; *p != '\0'; p++) {
|
|
|
|
if (*p == '/' || *p == ':')
|
|
|
|
*p = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call fsync() on a directory path. */
|
2017-10-07 10:44:04 +02:00
|
|
|
static bool
|
2013-06-23 14:20:45 +02:00
|
|
|
sync_dir (const char *dir)
|
|
|
|
{
|
2014-09-22 11:54:56 +02:00
|
|
|
int fd, r;
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
fd = open (dir, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2014-09-22 11:54:56 +02:00
|
|
|
fprintf (stderr, "Error: open %s: %s\n", dir, strerror (errno));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
2014-09-22 11:54:56 +02:00
|
|
|
|
|
|
|
r = fsync (fd);
|
|
|
|
if (r)
|
|
|
|
fprintf (stderr, "Error: fsync %s: %s\n", dir, strerror (errno));
|
|
|
|
|
2013-06-23 14:20:45 +02:00
|
|
|
close (fd);
|
2014-09-22 11:54:56 +02:00
|
|
|
|
|
|
|
return r == 0;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:53 +02:00
|
|
|
/*
|
|
|
|
* Check the specified folder name does not contain a directory
|
|
|
|
* component ".." to prevent writes outside of the Maildir
|
2017-10-07 10:44:04 +02:00
|
|
|
* hierarchy. Return true on valid folder name, false otherwise.
|
2014-09-22 11:54:53 +02:00
|
|
|
*/
|
2017-10-07 10:44:04 +02:00
|
|
|
static bool
|
2014-09-22 11:54:53 +02:00
|
|
|
is_valid_folder_name (const char *folder)
|
2013-06-23 06:24:00 +02:00
|
|
|
{
|
|
|
|
const char *p = folder;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/'))
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2013-06-23 06:24:00 +02:00
|
|
|
p = strchr (p, '/');
|
|
|
|
if (!p)
|
2017-10-07 10:44:04 +02:00
|
|
|
return true;
|
2013-06-23 06:24:00 +02:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:57 +02:00
|
|
|
/*
|
|
|
|
* Make the given directory and its parents as necessary, using the
|
2017-10-07 10:44:04 +02:00
|
|
|
* given mode. Return true on success, false otherwise. Partial
|
2014-09-22 11:54:57 +02:00
|
|
|
* results are not cleaned up on errors.
|
|
|
|
*/
|
2017-10-07 10:44:04 +02:00
|
|
|
static bool
|
2014-09-22 11:54:57 +02:00
|
|
|
mkdir_recursive (const void *ctx, const char *path, int mode)
|
2013-06-23 06:24:03 +02:00
|
|
|
{
|
2014-09-22 11:54:57 +02:00
|
|
|
struct stat st;
|
|
|
|
int r;
|
|
|
|
char *parent = NULL, *slash;
|
2013-06-23 06:24:03 +02:00
|
|
|
|
2014-09-22 11:54:57 +02:00
|
|
|
/* First check the common case: directory already exists. */
|
|
|
|
r = stat (path, &st);
|
|
|
|
if (r == 0) {
|
|
|
|
if (! S_ISDIR (st.st_mode)) {
|
|
|
|
fprintf (stderr, "Error: '%s' is not a directory: %s\n",
|
|
|
|
path, strerror (EEXIST));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2014-09-22 11:54:57 +02:00
|
|
|
}
|
2013-06-23 06:24:03 +02:00
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
return true;
|
2014-09-22 11:54:57 +02:00
|
|
|
} else if (errno != ENOENT) {
|
|
|
|
fprintf (stderr, "Error: stat '%s': %s\n", path, strerror (errno));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2013-06-23 06:24:03 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:57 +02:00
|
|
|
/* mkdir parents, if any */
|
|
|
|
slash = strrchr (path, '/');
|
|
|
|
if (slash && slash != path) {
|
|
|
|
parent = talloc_strndup (ctx, path, slash - path);
|
|
|
|
if (! parent) {
|
|
|
|
fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2013-06-23 06:24:03 +02:00
|
|
|
}
|
2014-09-22 11:54:57 +02:00
|
|
|
|
|
|
|
if (! mkdir_recursive (ctx, parent, mode))
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2013-06-23 06:24:03 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:57 +02:00
|
|
|
if (mkdir (path, mode)) {
|
|
|
|
fprintf (stderr, "Error: mkdir '%s': %s\n", path, strerror (errno));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2014-09-22 11:54:57 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
return parent ? sync_dir (parent) : true;
|
2013-06-23 06:24:03 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:57 +02:00
|
|
|
/*
|
|
|
|
* Create the given maildir folder, i.e. maildir and its
|
2017-10-07 10:44:04 +02:00
|
|
|
* subdirectories cur/new/tmp. Return true on success, false
|
2014-09-22 11:54:57 +02:00
|
|
|
* otherwise. Partial results are not cleaned up on errors.
|
|
|
|
*/
|
2017-10-07 10:44:04 +02:00
|
|
|
static bool
|
2014-09-22 11:54:57 +02:00
|
|
|
maildir_create_folder (const void *ctx, const char *maildir)
|
2013-06-23 06:24:03 +02:00
|
|
|
{
|
2014-09-22 11:54:57 +02:00
|
|
|
const char *subdirs[] = { "cur", "new", "tmp" };
|
2013-06-23 06:24:03 +02:00
|
|
|
const int mode = 0700;
|
|
|
|
char *subdir;
|
2014-09-22 11:54:57 +02:00
|
|
|
unsigned int i;
|
2013-06-23 06:24:03 +02:00
|
|
|
|
2014-09-22 11:54:57 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE (subdirs); i++) {
|
|
|
|
subdir = talloc_asprintf (ctx, "%s/%s", maildir, subdirs[i]);
|
|
|
|
if (! subdir) {
|
|
|
|
fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2014-09-22 11:54:57 +02:00
|
|
|
}
|
2013-06-23 06:24:03 +02:00
|
|
|
|
2014-09-22 11:54:57 +02:00
|
|
|
if (! mkdir_recursive (ctx, subdir, mode))
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2014-09-22 11:54:57 +02:00
|
|
|
}
|
2013-06-23 06:24:03 +02:00
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
return true;
|
2013-06-23 06:24:03 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:58 +02:00
|
|
|
/*
|
|
|
|
* Generate a temporary file basename, no path, do not create an
|
|
|
|
* actual file. Return the basename, or NULL on errors.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
tempfilename (const void *ctx)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
char hostname[256];
|
|
|
|
struct timeval tv;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/* We follow the Dovecot file name generation algorithm. */
|
|
|
|
pid = getpid ();
|
|
|
|
safe_gethostname (hostname, sizeof (hostname));
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
|
|
|
|
filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s",
|
2014-10-28 09:17:26 +01:00
|
|
|
(long) tv.tv_sec, (long) tv.tv_usec, pid, hostname);
|
2014-09-22 11:54:58 +02:00
|
|
|
if (! filename)
|
|
|
|
fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
/*
|
|
|
|
* Create a unique temporary file in maildir/tmp, return fd and full
|
|
|
|
* path to file in *path_out, or -1 on errors (in which case *path_out
|
|
|
|
* is not touched).
|
|
|
|
*/
|
2013-06-23 14:20:45 +02:00
|
|
|
static int
|
2014-09-22 11:54:59 +02:00
|
|
|
maildir_mktemp (const void *ctx, const char *maildir, char **path_out)
|
2013-06-23 14:20:45 +02:00
|
|
|
{
|
2014-09-22 11:54:59 +02:00
|
|
|
char *filename, *path;
|
|
|
|
int fd;
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
do {
|
2014-09-22 11:54:58 +02:00
|
|
|
filename = tempfilename (ctx);
|
|
|
|
if (! filename)
|
2013-06-23 14:20:45 +02:00
|
|
|
return -1;
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
path = talloc_asprintf (ctx, "%s/tmp/%s", maildir, filename);
|
|
|
|
if (! path) {
|
|
|
|
fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
|
2013-06-23 14:20:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
|
2013-06-23 14:20:45 +02:00
|
|
|
} while (fd == -1 && errno == EEXIST);
|
|
|
|
|
|
|
|
if (fd == -1) {
|
2014-09-22 11:54:59 +02:00
|
|
|
fprintf (stderr, "Error: open '%s': %s\n", path, strerror (errno));
|
2013-06-23 14:20:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
*path_out = path;
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:55 +02:00
|
|
|
/*
|
2017-10-07 10:44:04 +02:00
|
|
|
* Copy fdin to fdout, return true on success, and false on errors and
|
2014-09-22 11:54:55 +02:00
|
|
|
* empty input.
|
|
|
|
*/
|
2017-10-07 10:44:04 +02:00
|
|
|
static bool
|
2014-09-22 11:54:55 +02:00
|
|
|
copy_fd (int fdout, int fdin)
|
2013-06-23 14:20:45 +02:00
|
|
|
{
|
2017-10-07 10:44:04 +02:00
|
|
|
bool empty = true;
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
while (! interrupted) {
|
|
|
|
ssize_t remain;
|
|
|
|
char buf[4096];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
remain = read (fdin, buf, sizeof (buf));
|
|
|
|
if (remain == 0)
|
|
|
|
break;
|
|
|
|
if (remain < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
fprintf (stderr, "Error: reading from standard input: %s\n",
|
|
|
|
strerror (errno));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
do {
|
|
|
|
ssize_t written = write (fdout, p, remain);
|
|
|
|
if (written < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (written <= 0) {
|
|
|
|
fprintf (stderr, "Error: writing to temporary file: %s",
|
|
|
|
strerror (errno));
|
2017-10-07 10:44:04 +02:00
|
|
|
return false;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
p += written;
|
|
|
|
remain -= written;
|
2017-10-07 10:44:04 +02:00
|
|
|
empty = false;
|
2013-06-23 14:20:45 +02:00
|
|
|
} while (remain > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (!interrupted && !empty);
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
/*
|
|
|
|
* Write fdin to a new temp file in maildir/tmp, return full path to
|
|
|
|
* the file, or NULL on errors.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
maildir_write_tmp (const void *ctx, int fdin, const char *maildir)
|
2013-06-23 14:20:45 +02:00
|
|
|
{
|
2014-09-22 11:54:59 +02:00
|
|
|
char *path;
|
2014-04-16 14:59:21 +02:00
|
|
|
int fdout;
|
2013-06-23 14:20:45 +02:00
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
fdout = maildir_mktemp (ctx, maildir, &path);
|
2013-06-23 14:20:45 +02:00
|
|
|
if (fdout < 0)
|
2014-09-22 11:54:59 +02:00
|
|
|
return NULL;
|
2013-06-23 14:20:45 +02:00
|
|
|
|
2014-09-22 11:54:55 +02:00
|
|
|
if (! copy_fd (fdout, fdin))
|
2013-06-23 14:20:45 +02:00
|
|
|
goto FAIL;
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
if (fsync (fdout)) {
|
|
|
|
fprintf (stderr, "Error: fsync '%s': %s\n", path, strerror (errno));
|
2013-06-23 14:20:45 +02:00
|
|
|
goto FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fdout);
|
2014-09-22 11:54:59 +02:00
|
|
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
FAIL:
|
|
|
|
close (fdout);
|
|
|
|
unlink (path);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write fdin to a new file in maildir/new, using an intermediate temp
|
|
|
|
* file in maildir/tmp, return full path to the new file, or NULL on
|
|
|
|
* errors.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
maildir_write_new (const void *ctx, int fdin, const char *maildir)
|
|
|
|
{
|
|
|
|
char *cleanpath, *tmppath, *newpath, *newdir;
|
|
|
|
|
|
|
|
tmppath = maildir_write_tmp (ctx, fdin, maildir);
|
|
|
|
if (! tmppath)
|
|
|
|
return NULL;
|
|
|
|
cleanpath = tmppath;
|
|
|
|
|
|
|
|
newpath = talloc_strdup (ctx, tmppath);
|
|
|
|
if (! newpath) {
|
|
|
|
fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
|
2013-06-23 14:20:45 +02:00
|
|
|
goto FAIL;
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
/* sanity checks needed? */
|
|
|
|
memcpy (newpath + strlen (maildir) + 1, "new", 3);
|
|
|
|
|
|
|
|
if (rename (tmppath, newpath)) {
|
|
|
|
fprintf (stderr, "Error: rename '%s' '%s': %s\n",
|
|
|
|
tmppath, newpath, strerror (errno));
|
|
|
|
goto FAIL;
|
|
|
|
}
|
|
|
|
cleanpath = newpath;
|
|
|
|
|
|
|
|
newdir = talloc_asprintf (ctx, "%s/%s", maildir, "new");
|
|
|
|
if (! newdir) {
|
|
|
|
fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
|
|
|
|
goto FAIL;
|
|
|
|
}
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
if (! sync_dir (newdir))
|
|
|
|
goto FAIL;
|
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
return newpath;
|
|
|
|
|
|
|
|
FAIL:
|
|
|
|
unlink (cleanpath);
|
2013-06-23 14:20:45 +02:00
|
|
|
|
2014-09-22 11:54:59 +02:00
|
|
|
return NULL;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
2014-10-03 23:18:58 +02:00
|
|
|
/*
|
|
|
|
* Add the specified message file to the notmuch database, applying
|
2017-10-07 10:44:04 +02:00
|
|
|
* tags in tag_ops. If synchronize_flags is true, the tags are
|
2014-10-03 23:18:58 +02:00
|
|
|
* synchronized to maildir flags (which may result in message file
|
|
|
|
* rename).
|
|
|
|
*
|
|
|
|
* Return NOTMUCH_STATUS_SUCCESS on success, errors otherwise. If keep
|
2017-10-07 10:44:04 +02:00
|
|
|
* is true, errors in tag changes and flag syncing are ignored and
|
2014-10-03 23:18:58 +02:00
|
|
|
* success status is returned; otherwise such errors cause the message
|
|
|
|
* to be removed from the database. Failure to add the message to the
|
|
|
|
* database results in error status regardless of keep.
|
|
|
|
*/
|
|
|
|
static notmuch_status_t
|
|
|
|
add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,
|
2017-10-07 10:44:04 +02:00
|
|
|
bool synchronize_flags, bool keep)
|
2014-09-22 11:54:54 +02:00
|
|
|
{
|
|
|
|
notmuch_message_t *message;
|
|
|
|
notmuch_status_t status;
|
|
|
|
|
database: add n_d_index_file (deprecates n_d_add_message)
We need a way to pass parameters to the indexing functionality on the
first index, not just on reindexing. The obvious place is in
notmuch_database_add_message. But since modifying the argument list
would break both API and ABI, we needed a new name.
I considered notmuch_database_add_message_with_params(), but the
functionality we're talking about doesn't always add a message. It
tries to index a specific file, possibly adding a message, but
possibly doing other things, like adding terms to an existing message,
or failing to deal with message objects entirely (e.g. because the
file didn't contain a message).
So i chose the function name notmuch_database_index_file.
I confess i'm a little concerned about confusing future notmuch
developers with the new name, since we already have a private
_notmuch_message_index_file function, and the two do rather different
things. But i think the added clarity for people linking against the
future libnotmuch and the capacity for using index parameters makes
this a worthwhile tradeoff. (that said, if anyone has another name
that they strongly prefer, i'd be happy to go with it)
This changeset also adjusts the tests so that we test whether the new,
preferred function returns bad values (since the deprecated function
just calls the new one).
We can keep the deprecated n_d_add_message function around as long as
we like, but at the next place where we're forced to break API or ABI
we can probably choose to drop the name relatively safely.
NOTE: there is probably more cleanup to do in the ruby and go bindings
to complete the deprecation directly. I don't know those languages
well enough to attempt a fix; i don't know how to test them; and i
don't know the culture around those languages about API additions or
deprecations.
2017-08-18 01:14:25 +02:00
|
|
|
status = notmuch_database_index_file (notmuch, path, NULL, &message);
|
2014-10-03 23:18:58 +02:00
|
|
|
if (status == NOTMUCH_STATUS_SUCCESS) {
|
|
|
|
status = tag_op_list_apply (message, tag_ops, 0);
|
|
|
|
if (status) {
|
|
|
|
fprintf (stderr, "%s: failed to apply tags to file '%s': %s\n",
|
|
|
|
keep ? "Warning" : "Error",
|
|
|
|
path, notmuch_status_to_string (status));
|
|
|
|
goto DONE;
|
2014-09-22 11:54:54 +02:00
|
|
|
}
|
2014-10-03 23:18:58 +02:00
|
|
|
} else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
|
|
|
|
status = NOTMUCH_STATUS_SUCCESS;
|
|
|
|
} else if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) {
|
|
|
|
fprintf (stderr, "Error: delivery of non-mail file: '%s'\n", path);
|
|
|
|
goto FAIL;
|
2014-09-22 11:54:54 +02:00
|
|
|
} else {
|
2014-10-03 23:18:58 +02:00
|
|
|
fprintf (stderr, "Error: failed to add '%s' to notmuch database: %s\n",
|
|
|
|
path, notmuch_status_to_string (status));
|
|
|
|
goto FAIL;
|
|
|
|
}
|
2014-09-22 11:54:54 +02:00
|
|
|
|
2014-10-03 23:18:58 +02:00
|
|
|
if (synchronize_flags) {
|
|
|
|
status = notmuch_message_tags_to_maildir_flags (message);
|
|
|
|
if (status != NOTMUCH_STATUS_SUCCESS)
|
|
|
|
fprintf (stderr, "%s: failed to sync tags to maildir flags for '%s': %s\n",
|
|
|
|
keep ? "Warning" : "Error",
|
|
|
|
path, notmuch_status_to_string (status));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: Unfortunately a failed maildir flag sync might
|
|
|
|
* already have renamed the file, in which case the cleanup
|
|
|
|
* path may fail.
|
|
|
|
*/
|
2014-09-22 11:54:54 +02:00
|
|
|
}
|
|
|
|
|
2014-10-03 23:18:58 +02:00
|
|
|
DONE:
|
2014-09-22 11:54:54 +02:00
|
|
|
notmuch_message_destroy (message);
|
2014-10-03 23:18:58 +02:00
|
|
|
|
|
|
|
if (status) {
|
|
|
|
if (keep) {
|
|
|
|
status = NOTMUCH_STATUS_SUCCESS;
|
|
|
|
} else {
|
|
|
|
notmuch_status_t cleanup_status;
|
|
|
|
|
|
|
|
cleanup_status = notmuch_database_remove_message (notmuch, path);
|
|
|
|
if (cleanup_status != NOTMUCH_STATUS_SUCCESS &&
|
|
|
|
cleanup_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
|
|
|
|
fprintf (stderr, "Warning: failed to remove '%s' from database "
|
|
|
|
"after errors: %s. Please run 'notmuch new' to fix.\n",
|
|
|
|
path, notmuch_status_to_string (cleanup_status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FAIL:
|
|
|
|
return status;
|
2014-09-22 11:54:54 +02:00
|
|
|
}
|
|
|
|
|
2013-06-23 14:20:45 +02:00
|
|
|
int
|
|
|
|
notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
|
|
|
|
{
|
2014-10-03 23:18:59 +02:00
|
|
|
notmuch_status_t status, close_status;
|
2013-06-23 14:20:45 +02:00
|
|
|
notmuch_database_t *notmuch;
|
|
|
|
struct sigaction action;
|
|
|
|
const char *db_path;
|
|
|
|
const char **new_tags;
|
|
|
|
size_t new_tags_length;
|
|
|
|
tag_op_list_t *tag_ops;
|
|
|
|
char *query_string = NULL;
|
2017-10-01 22:53:10 +02:00
|
|
|
const char *folder = "";
|
2017-10-07 10:44:04 +02:00
|
|
|
bool create_folder = false;
|
|
|
|
bool keep = false;
|
|
|
|
bool no_hooks = false;
|
|
|
|
bool synchronize_flags;
|
2017-10-01 22:53:10 +02:00
|
|
|
char *maildir;
|
2014-04-16 14:59:21 +02:00
|
|
|
char *newpath;
|
2013-06-23 06:24:00 +02:00
|
|
|
int opt_index;
|
2013-06-23 14:20:45 +02:00
|
|
|
unsigned int i;
|
|
|
|
|
2013-06-23 06:24:00 +02:00
|
|
|
notmuch_opt_desc_t options[] = {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_string = &folder, .name = "folder" },
|
|
|
|
{ .opt_bool = &create_folder, .name = "create-folder" },
|
|
|
|
{ .opt_bool = &keep, .name = "keep" },
|
|
|
|
{ .opt_bool = &no_hooks, .name = "no-hooks" },
|
|
|
|
{ .opt_inherit = notmuch_shared_options },
|
|
|
|
{ }
|
2013-06-23 06:24:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
opt_index = parse_arguments (argc, argv, options, 1);
|
2014-01-10 22:28:53 +01:00
|
|
|
if (opt_index < 0)
|
|
|
|
return EXIT_FAILURE;
|
2013-06-23 06:24:00 +02:00
|
|
|
|
2015-04-05 15:13:03 +02:00
|
|
|
notmuch_process_shared_options (argv[0]);
|
|
|
|
|
2013-06-23 14:20:45 +02:00
|
|
|
db_path = notmuch_config_get_database_path (config);
|
|
|
|
new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
|
2014-01-01 16:20:14 +01:00
|
|
|
synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
tag_ops = tag_op_list_create (config);
|
|
|
|
if (tag_ops == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < new_tags_length; i++) {
|
2014-02-23 17:55:22 +01:00
|
|
|
const char *error_msg;
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
error_msg = illegal_tag (new_tags[i], false);
|
2014-02-23 17:55:22 +01:00
|
|
|
if (error_msg) {
|
|
|
|
fprintf (stderr, "Error: tag '%s' in new.tags: %s\n",
|
|
|
|
new_tags[i], error_msg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
if (tag_op_list_append (tag_ops, new_tags[i], false))
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
|
|
|
|
&query_string, tag_ops))
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-06-23 14:20:45 +02:00
|
|
|
|
|
|
|
if (*query_string != '\0') {
|
|
|
|
fprintf (stderr, "Error: unexpected query string: %s\n", query_string);
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:53:10 +02:00
|
|
|
if (! is_valid_folder_name (folder)) {
|
|
|
|
fprintf (stderr, "Error: invalid folder name: '%s'\n", folder);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
maildir = talloc_asprintf (config, "%s/%s", db_path, folder);
|
|
|
|
if (! maildir) {
|
|
|
|
fprintf (stderr, "Out of memory\n");
|
|
|
|
return EXIT_FAILURE;
|
2013-06-23 06:24:00 +02:00
|
|
|
}
|
2013-06-23 14:20:45 +02:00
|
|
|
|
2017-10-01 22:53:10 +02:00
|
|
|
strip_trailing (maildir, '/');
|
|
|
|
if (create_folder && ! maildir_create_folder (config, maildir))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2015-05-27 19:53:52 +02:00
|
|
|
/* Set up our handler for SIGINT. We do not set SA_RESTART so that copying
|
2013-06-23 14:20:45 +02:00
|
|
|
* from standard input may be interrupted. */
|
|
|
|
memset (&action, 0, sizeof (struct sigaction));
|
|
|
|
action.sa_handler = handle_sigint;
|
|
|
|
sigemptyset (&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
sigaction (SIGINT, &action, NULL);
|
|
|
|
|
2014-04-16 14:59:21 +02:00
|
|
|
/* Write the message to the Maildir new directory. */
|
2014-09-22 11:54:59 +02:00
|
|
|
newpath = maildir_write_new (config, STDIN_FILENO, maildir);
|
|
|
|
if (! newpath) {
|
2014-04-16 14:59:21 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-11-28 04:01:42 +01:00
|
|
|
status = notmuch_database_open (notmuch_config_get_database_path (config),
|
|
|
|
NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much);
|
|
|
|
if (status)
|
|
|
|
return keep ? NOTMUCH_STATUS_SUCCESS : status_to_exit (status);
|
2016-11-27 16:24:58 +01:00
|
|
|
|
|
|
|
notmuch_exit_if_unmatched_db_uuid (notmuch);
|
|
|
|
|
|
|
|
|
2014-10-03 23:18:59 +02:00
|
|
|
/* Index the message. */
|
|
|
|
status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep);
|
|
|
|
|
|
|
|
/* Commit changes. */
|
|
|
|
close_status = notmuch_database_destroy (notmuch);
|
|
|
|
if (close_status) {
|
|
|
|
/* Hold on to the first error, if any. */
|
|
|
|
if (! status)
|
|
|
|
status = close_status;
|
|
|
|
fprintf (stderr, "%s: failed to commit database changes: %s\n",
|
|
|
|
keep ? "Warning" : "Error",
|
|
|
|
notmuch_status_to_string (close_status));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
if (keep) {
|
|
|
|
status = NOTMUCH_STATUS_SUCCESS;
|
|
|
|
} else {
|
|
|
|
/* If maildir flag sync failed, this might fail. */
|
|
|
|
if (unlink (newpath)) {
|
|
|
|
fprintf (stderr, "Warning: failed to remove '%s' from maildir "
|
|
|
|
"after errors: %s. Please run 'notmuch new' to fix.\n",
|
|
|
|
newpath, strerror (errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 14:20:45 +02:00
|
|
|
|
2014-09-28 16:40:59 +02:00
|
|
|
if (! no_hooks && status == NOTMUCH_STATUS_SUCCESS) {
|
|
|
|
/* Ignore hook failures. */
|
|
|
|
notmuch_run_hook (db_path, "post-insert");
|
|
|
|
}
|
|
|
|
|
2016-11-28 04:01:42 +01:00
|
|
|
return status_to_exit (status);
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|