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, '/');
|
2019-06-13 12:31:01 +02:00
|
|
|
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) {
|
2019-06-13 12:31:01 +02:00
|
|
|
if (! S_ISDIR (st.st_mode)) {
|
2014-09-22 11:54:57 +02:00
|
|
|
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
|
2018-02-09 05:10:58 +01:00
|
|
|
maildir_create_folder (const void *ctx, const char *maildir, bool world_readable)
|
2013-06-23 06:24:03 +02:00
|
|
|
{
|
2014-09-22 11:54:57 +02:00
|
|
|
const char *subdirs[] = { "cur", "new", "tmp" };
|
2018-02-09 05:10:58 +01:00
|
|
|
const int mode = (world_readable ? 0755 : 0700);
|
2013-06-23 06:24:03 +02:00
|
|
|
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
|
2018-02-09 05:10:58 +01:00
|
|
|
maildir_mktemp (const void *ctx, const char *maildir, bool world_readable, char **path_out)
|
2013-06-23 14:20:45 +02:00
|
|
|
{
|
2014-09-22 11:54:59 +02:00
|
|
|
char *filename, *path;
|
|
|
|
int fd;
|
2018-02-09 05:10:58 +01:00
|
|
|
const int mode = (world_readable ? 0644 : 0600);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:10:58 +01:00
|
|
|
fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
return (! interrupted && ! empty);
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
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 *
|
2018-02-09 05:10:58 +01:00
|
|
|
maildir_write_tmp (const void *ctx, int fdin, const char *maildir, bool world_readable)
|
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
|
|
|
|
2018-02-09 05:10:58 +01:00
|
|
|
fdout = maildir_mktemp (ctx, maildir, world_readable, &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;
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
FAIL:
|
2014-09-22 11:54:59 +02:00
|
|
|
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 *
|
2018-02-09 05:10:58 +01:00
|
|
|
maildir_write_new (const void *ctx, int fdin, const char *maildir, bool world_readable)
|
2014-09-22 11:54:59 +02:00
|
|
|
{
|
|
|
|
char *cleanpath, *tmppath, *newpath, *newdir;
|
|
|
|
|
2018-02-09 05:10:58 +01:00
|
|
|
tmppath = maildir_write_tmp (ctx, fdin, maildir, world_readable);
|
2014-09-22 11:54:59 +02:00
|
|
|
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;
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
FAIL:
|
2014-09-22 11:54:59 +02:00
|
|
|
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-21 04:25:47 +02:00
|
|
|
bool synchronize_flags, bool keep,
|
|
|
|
notmuch_indexopts_t *indexopts)
|
2014-09-22 11:54:54 +02:00
|
|
|
{
|
|
|
|
notmuch_message_t *message;
|
|
|
|
notmuch_status_t status;
|
|
|
|
|
2017-10-21 04:25:47 +02:00
|
|
|
status = notmuch_database_index_file (notmuch, path, indexopts, &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
|
2021-02-27 15:07:45 +01:00
|
|
|
notmuch_insert_command (notmuch_database_t *notmuch, int argc, char *argv[])
|
2013-06-23 14:20:45 +02:00
|
|
|
{
|
2014-10-03 23:18:59 +02:00
|
|
|
notmuch_status_t status, close_status;
|
2013-06-23 14:20:45 +02:00
|
|
|
struct sigaction action;
|
2021-01-09 12:29:07 +01:00
|
|
|
const char *mail_root;
|
2020-08-26 13:43:33 +02:00
|
|
|
notmuch_config_values_t *new_tags = NULL;
|
2013-06-23 14:20:45 +02:00
|
|
|
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;
|
2017-10-14 15:16:07 +02:00
|
|
|
bool hooks = true;
|
2018-02-09 05:10:58 +01:00
|
|
|
bool world_readable = false;
|
2020-08-26 13:43:33 +02:00
|
|
|
notmuch_bool_t 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;
|
2020-08-26 13:43:33 +02:00
|
|
|
void *local = talloc_new (NULL);
|
2013-06-23 14:20:45 +02:00
|
|
|
|
2013-06-23 06:24:00 +02:00
|
|
|
notmuch_opt_desc_t options[] = {
|
2017-10-14 15:15:43 +02:00
|
|
|
{ .opt_string = &folder, .name = "folder", .allow_empty = true },
|
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_bool = &create_folder, .name = "create-folder" },
|
|
|
|
{ .opt_bool = &keep, .name = "keep" },
|
2018-02-09 05:10:58 +01:00
|
|
|
{ .opt_bool = &hooks, .name = "hooks" },
|
|
|
|
{ .opt_bool = &world_readable, .name = "world-readable" },
|
2017-10-21 04:25:47 +02:00
|
|
|
{ .opt_inherit = notmuch_shared_indexing_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_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]);
|
|
|
|
|
2021-01-09 12:29:07 +01:00
|
|
|
mail_root = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
|
2020-08-26 13:43:33 +02:00
|
|
|
|
|
|
|
new_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_NEW_TAGS);
|
|
|
|
|
|
|
|
if (print_status_database (
|
|
|
|
"notmuch insert",
|
|
|
|
notmuch,
|
|
|
|
notmuch_config_get_bool (notmuch, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS,
|
|
|
|
&synchronize_flags)))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
tag_ops = tag_op_list_create (local);
|
2013-06-23 14:20:45 +02:00
|
|
|
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
|
|
|
}
|
2020-08-26 13:43:33 +02:00
|
|
|
for (;
|
|
|
|
notmuch_config_values_valid (new_tags);
|
|
|
|
notmuch_config_values_move_to_next (new_tags)) {
|
2014-02-23 17:55:22 +01:00
|
|
|
const char *error_msg;
|
2020-08-26 13:43:33 +02:00
|
|
|
const char *tag = notmuch_config_values_get (new_tags);
|
|
|
|
error_msg = illegal_tag (tag, false);
|
2014-02-23 17:55:22 +01:00
|
|
|
if (error_msg) {
|
|
|
|
fprintf (stderr, "Error: tag '%s' in new.tags: %s\n",
|
2020-08-26 13:43:33 +02:00
|
|
|
tag, error_msg);
|
2014-02-23 17:55:22 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-08-26 13:43:33 +02:00
|
|
|
if (tag_op_list_append (tag_ops, tag, false))
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 13:43:33 +02:00
|
|
|
if (parse_tag_command_line (local, argc - opt_index, argv + opt_index,
|
2013-06-23 14:20:45 +02:00
|
|
|
&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;
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:29:07 +01:00
|
|
|
maildir = talloc_asprintf (local, "%s/%s", mail_root, folder);
|
2017-10-01 22:53:10 +02:00
|
|
|
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, '/');
|
2020-08-26 13:43:33 +02:00
|
|
|
if (create_folder && ! maildir_create_folder (local, maildir, world_readable))
|
2017-10-01 22:53:10 +02:00
|
|
|
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. */
|
2020-08-26 13:43:33 +02:00
|
|
|
newpath = maildir_write_new (local, STDIN_FILENO, maildir, world_readable);
|
2014-09-22 11:54:59 +02:00
|
|
|
if (! newpath) {
|
2014-04-16 14:59:21 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-11-27 16:24:58 +01:00
|
|
|
notmuch_exit_if_unmatched_db_uuid (notmuch);
|
|
|
|
|
2019-05-02 15:19:41 +02:00
|
|
|
status = notmuch_process_shared_indexing_options (notmuch);
|
2017-10-21 04:25:47 +02:00
|
|
|
if (status != NOTMUCH_STATUS_SUCCESS) {
|
|
|
|
fprintf (stderr, "Error: Failed to process index options. (%s)\n",
|
|
|
|
notmuch_status_to_string (status));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-11-27 16:24:58 +01:00
|
|
|
|
2014-10-03 23:18:59 +02:00
|
|
|
/* Index the message. */
|
2017-10-21 04:25:47 +02:00
|
|
|
status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep, indexing_cli_choices.opts);
|
2014-10-03 23:18:59 +02:00
|
|
|
|
|
|
|
/* Commit changes. */
|
2021-01-10 12:50:14 +01:00
|
|
|
close_status = notmuch_database_close (notmuch);
|
2014-10-03 23:18:59 +02:00
|
|
|
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
|
|
|
|
2017-10-14 15:16:07 +02:00
|
|
|
if (hooks && status == NOTMUCH_STATUS_SUCCESS) {
|
2014-09-28 16:40:59 +02:00
|
|
|
/* Ignore hook failures. */
|
2021-01-10 12:50:14 +01:00
|
|
|
notmuch_run_hook (notmuch, "post-insert");
|
2014-09-28 16:40:59 +02:00
|
|
|
}
|
|
|
|
|
2021-01-10 12:50:14 +01:00
|
|
|
notmuch_database_destroy (notmuch);
|
|
|
|
|
2020-08-26 13:43:33 +02:00
|
|
|
talloc_free (local);
|
|
|
|
|
2016-11-28 04:01:42 +01:00
|
|
|
return status_to_exit (status);
|
2013-06-23 14:20:45 +02:00
|
|
|
}
|