2011-09-12 19:33:06 +02:00
|
|
|
/*
|
2020-05-25 20:26:00 +02:00
|
|
|
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-12-31 18:27:41 +01:00
|
|
|
**
|
|
|
|
** 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, 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
|
2013-05-13 21:30:27 +02:00
|
|
|
** along with this program; if not, write to 59the Free Software Foundation,
|
2011-09-12 19:33:06 +02:00
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
2009-12-31 18:27:41 +01:00
|
|
|
*/
|
|
|
|
|
2010-01-16 10:32:07 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2013-05-21 23:06:10 +02:00
|
|
|
#include <stdlib.h>
|
2009-12-31 18:27:41 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-01-01 14:51:50 +01:00
|
|
|
#include <glib/gprintf.h>
|
2020-05-25 20:40:02 +02:00
|
|
|
#include <gio/gio.h>
|
2009-12-31 18:27:41 +01:00
|
|
|
|
2020-11-28 09:15:49 +01:00
|
|
|
#include "mu-maildir.hh"
|
2019-12-16 20:44:35 +01:00
|
|
|
#include "utils/mu-str.h"
|
2009-12-31 18:27:41 +01:00
|
|
|
|
2020-11-28 09:15:49 +01:00
|
|
|
using namespace Mu;
|
|
|
|
|
2010-01-11 19:47:01 +01:00
|
|
|
#define MU_MAILDIR_NOINDEX_FILE ".noindex"
|
2012-05-22 09:19:49 +02:00
|
|
|
#define MU_MAILDIR_NOUPDATE_FILE ".noupdate"
|
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
/* On Linux (and some BSD), we have entry->d_type, but some file
|
|
|
|
* systems (XFS, ReiserFS) do not support it, and set it DT_UNKNOWN.
|
|
|
|
* On other OSs, notably Solaris, entry->d_type is not present at all.
|
|
|
|
* For these cases, we use lstat (in get_dtype) as a slower fallback,
|
|
|
|
* and return it in the d_type parameter
|
|
|
|
*/
|
2020-05-26 18:07:56 +02:00
|
|
|
static unsigned char
|
|
|
|
get_dtype (struct dirent* dentry, const char *path, gboolean use_lstat)
|
|
|
|
{
|
2010-12-11 12:52:03 +01:00
|
|
|
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2020-05-26 18:07:56 +02:00
|
|
|
if (dentry->d_type == DT_UNKNOWN)
|
|
|
|
goto slowpath;
|
|
|
|
if (dentry->d_type == DT_LNK && !use_lstat)
|
|
|
|
goto slowpath;
|
|
|
|
|
|
|
|
return dentry->d_type; /* fastpath */
|
|
|
|
|
|
|
|
slowpath:
|
|
|
|
#endif /*HAVE_STRUCT_DIRENT_D_TYPE*/
|
|
|
|
return mu_util_get_dtype (path, use_lstat);
|
|
|
|
}
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
static gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
create_maildir (const char *path, mode_t mode, GError **err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
|
|
|
int i;
|
2020-05-26 18:07:56 +02:00
|
|
|
const char* subdirs[] = {"new", "cur", "tmp"};
|
2009-12-31 18:27:41 +01:00
|
|
|
|
|
|
|
for (i = 0; i != G_N_ELEMENTS(subdirs); ++i) {
|
2011-01-16 12:54:32 +01:00
|
|
|
|
2010-01-24 13:55:22 +01:00
|
|
|
const char *fullpath;
|
2009-12-31 18:27:41 +01:00
|
|
|
int rv;
|
2010-01-24 13:55:22 +01:00
|
|
|
|
|
|
|
/* static buffer */
|
2010-12-11 12:52:03 +01:00
|
|
|
fullpath = mu_str_fullpath_s (path, subdirs[i]);
|
2012-02-26 11:16:57 +01:00
|
|
|
|
|
|
|
/* if subdir already exists, don't try to re-create
|
|
|
|
* it */
|
|
|
|
if (mu_util_check_dir (fullpath, TRUE, TRUE))
|
|
|
|
continue;
|
|
|
|
|
2010-09-26 16:29:54 +02:00
|
|
|
rv = g_mkdir_with_parents (fullpath, (int)mode);
|
2012-02-26 11:16:57 +01:00
|
|
|
|
|
|
|
/* note, g_mkdir_with_parents won't detect an error if
|
|
|
|
* there's already such a dir, but with the wrong
|
|
|
|
* permissions; so we need to check */
|
2012-05-22 09:19:49 +02:00
|
|
|
if (rv != 0 || !mu_util_check_dir(fullpath, TRUE, TRUE))
|
|
|
|
return mu_util_g_set_error
|
|
|
|
(err,MU_ERROR_FILE_CANNOT_MKDIR,
|
|
|
|
"creating dir failed for %s: %s",
|
|
|
|
fullpath, strerror (errno));
|
2009-12-31 18:27:41 +01:00
|
|
|
}
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
create_noindex (const char *path, GError **err)
|
2011-09-12 19:33:06 +02:00
|
|
|
{
|
2009-12-31 18:27:41 +01:00
|
|
|
/* create a noindex file if requested */
|
|
|
|
int fd;
|
2010-01-24 13:55:22 +01:00
|
|
|
const char *noindexpath;
|
|
|
|
|
|
|
|
/* static buffer */
|
2010-12-11 12:52:03 +01:00
|
|
|
noindexpath = mu_str_fullpath_s (path, MU_MAILDIR_NOINDEX_FILE);
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
fd = creat (noindexpath, 0644);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-01-16 12:54:32 +01:00
|
|
|
/* note, if the 'close' failed, creation may still have
|
|
|
|
* succeeded...*/
|
2012-05-22 09:19:49 +02:00
|
|
|
if (fd < 0 || close (fd) != 0)
|
|
|
|
return mu_util_g_set_error (err, MU_ERROR_FILE_CANNOT_CREATE,
|
|
|
|
"error in create_noindex: %s",
|
|
|
|
strerror (errno));
|
2009-12-31 18:27:41 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex, GError **err)
|
2011-09-12 19:33:06 +02:00
|
|
|
{
|
2009-12-31 18:27:41 +01:00
|
|
|
g_return_val_if_fail (path, FALSE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2020-05-30 12:24:53 +02:00
|
|
|
if (!create_maildir (path, mode, err))
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
|
|
|
|
2010-12-05 15:29:53 +01:00
|
|
|
if (noindex && !create_noindex (path, err))
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine whether the source message is in 'new' or in 'cur';
|
|
|
|
* we ignore messages in 'tmp' for obvious reasons */
|
|
|
|
static gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
check_subdir (const char *src, gboolean *in_cur, GError **err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
2012-05-22 09:19:49 +02:00
|
|
|
gboolean rv;
|
2020-05-26 18:07:56 +02:00
|
|
|
char *srcpath;
|
2009-12-31 18:27:41 +01:00
|
|
|
|
|
|
|
srcpath = g_path_get_dirname (src);
|
2012-06-01 12:35:46 +02:00
|
|
|
*in_cur = FALSE;
|
2012-05-22 09:19:49 +02:00
|
|
|
rv = TRUE;
|
2012-06-01 12:35:46 +02:00
|
|
|
|
|
|
|
if (g_str_has_suffix (srcpath, "cur"))
|
2009-12-31 18:27:41 +01:00
|
|
|
*in_cur = TRUE;
|
2012-06-01 12:35:46 +02:00
|
|
|
else if (!g_str_has_suffix (srcpath, "new"))
|
|
|
|
rv = mu_util_g_set_error (err,
|
|
|
|
MU_ERROR_FILE_INVALID_SOURCE,
|
|
|
|
"invalid source message '%s'",
|
|
|
|
src);
|
2009-12-31 18:27:41 +01:00
|
|
|
g_free (srcpath);
|
2012-05-22 09:19:49 +02:00
|
|
|
return rv;
|
2009-12-31 18:27:41 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 18:07:56 +02:00
|
|
|
static char*
|
|
|
|
get_target_fullpath (const char* src, const char *targetpath, GError **err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
2020-05-26 18:07:56 +02:00
|
|
|
char *targetfullpath, *srcfile;
|
2009-12-31 18:27:41 +01:00
|
|
|
gboolean in_cur;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2010-12-05 15:29:53 +01:00
|
|
|
if (!check_subdir (src, &in_cur, err))
|
2009-12-31 18:27:41 +01:00
|
|
|
return NULL;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
srcfile = g_path_get_basename (src);
|
|
|
|
|
2013-12-01 19:21:44 +01:00
|
|
|
/* create targetpath; note: make the filename *cough* unique
|
|
|
|
* by including a hash of the srcname in the targetname. This
|
|
|
|
* helps if there are copies of a message (which all have the
|
|
|
|
* same basename)
|
|
|
|
*/
|
2011-01-16 12:54:32 +01:00
|
|
|
targetfullpath = g_strdup_printf ("%s%c%s%c%u_%s",
|
2009-12-31 18:27:41 +01:00
|
|
|
targetpath,
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
in_cur ? "cur" : "new",
|
|
|
|
G_DIR_SEPARATOR,
|
2011-01-16 12:54:32 +01:00
|
|
|
g_str_hash(src),
|
2009-12-31 18:27:41 +01:00
|
|
|
srcfile);
|
|
|
|
g_free (srcfile);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
return targetfullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_link (const char* src, const char *targetpath, GError **err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
2020-05-26 18:07:56 +02:00
|
|
|
char *targetfullpath;
|
2009-12-31 18:27:41 +01:00
|
|
|
int rv;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
g_return_val_if_fail (src, FALSE);
|
|
|
|
g_return_val_if_fail (targetpath, FALSE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2010-12-05 15:29:53 +01:00
|
|
|
targetfullpath = get_target_fullpath (src, targetpath, err);
|
2009-12-31 18:27:41 +01:00
|
|
|
if (!targetfullpath)
|
|
|
|
return FALSE;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
rv = symlink (src, targetfullpath);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2012-05-22 09:19:49 +02:00
|
|
|
if (rv != 0)
|
|
|
|
mu_util_g_set_error (err, MU_ERROR_FILE_CANNOT_LINK,
|
|
|
|
"error creating link %s => %s: %s",
|
|
|
|
targetfullpath, src, strerror (errno));
|
2010-01-02 15:05:23 +01:00
|
|
|
g_free (targetfullpath);
|
2012-05-22 09:19:49 +02:00
|
|
|
|
|
|
|
return rv == 0 ? TRUE: FALSE;
|
2009-12-31 18:27:41 +01:00
|
|
|
}
|
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* determine if path is a maildir leaf-dir; ie. if it's 'cur' or 'new'
|
|
|
|
* (we're skipping 'tmp' for obvious reasons)
|
|
|
|
*/
|
2016-07-23 18:18:09 +02:00
|
|
|
gboolean
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_is_leaf_dir (const char *path)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* path is the full path; it cannot possibly be shorter
|
2010-11-14 20:53:24 +01:00
|
|
|
* than 4 for a maildir (/cur or /new) */
|
2016-07-23 18:18:09 +02:00
|
|
|
len = path ? strlen (path) : 0;
|
2011-04-16 13:26:06 +02:00
|
|
|
if (G_UNLIKELY(len < 4))
|
2010-01-01 14:51:50 +01:00
|
|
|
return FALSE;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2016-07-23 18:18:09 +02:00
|
|
|
/* optimization; one further idea would be cast the 4 bytes to an
|
|
|
|
* integer and compare that -- need to think about alignment,
|
|
|
|
* endianness */
|
2011-09-12 19:33:06 +02:00
|
|
|
|
|
|
|
if (path[len - 4] == G_DIR_SEPARATOR &&
|
2011-04-16 13:26:06 +02:00
|
|
|
path[len - 3] == 'c' &&
|
|
|
|
path[len - 2] == 'u' &&
|
|
|
|
path[len - 1] == 'r')
|
|
|
|
return TRUE;
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2011-09-12 19:33:06 +02:00
|
|
|
if (path[len - 4] == G_DIR_SEPARATOR &&
|
2011-04-16 13:26:06 +02:00
|
|
|
path[len - 3] == 'n' &&
|
|
|
|
path[len - 2] == 'e' &&
|
|
|
|
path[len - 1] == 'w')
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
|
|
|
|
2012-05-22 09:19:49 +02:00
|
|
|
|
2010-01-03 12:40:40 +01:00
|
|
|
static gboolean
|
2016-11-19 15:17:43 +01:00
|
|
|
clear_links (const char *path, DIR *dir)
|
2010-01-03 12:40:40 +01:00
|
|
|
{
|
2016-11-19 15:17:43 +01:00
|
|
|
gboolean rv;
|
|
|
|
struct dirent *dentry;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
rv = TRUE;
|
2010-12-11 12:52:03 +01:00
|
|
|
errno = 0;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
while ((dentry = readdir (dir))) {
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
guint8 d_type;
|
|
|
|
char *fullpath;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
if (dentry->d_name[0] == '.')
|
|
|
|
continue; /* ignore .,.. other dotdirs */
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
fullpath = g_build_path ("/", path, dentry->d_name, NULL);
|
2020-05-26 18:07:56 +02:00
|
|
|
d_type = get_dtype (dentry, fullpath, TRUE/*lstat*/);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
if (d_type == DT_LNK) {
|
2016-11-19 15:17:43 +01:00
|
|
|
if (unlink (fullpath) != 0 ) {
|
|
|
|
g_warning ("error unlinking %s: %s",
|
|
|
|
fullpath, strerror(errno));
|
2010-01-24 13:55:22 +01:00
|
|
|
rv = FALSE;
|
|
|
|
}
|
2016-11-19 15:17:43 +01:00
|
|
|
} else if (d_type == DT_DIR) {
|
|
|
|
DIR *subdir;
|
|
|
|
subdir = opendir (fullpath);
|
|
|
|
if (!subdir) {
|
|
|
|
g_warning ("failed to open dir %s: %s",
|
|
|
|
fullpath, strerror(errno));
|
|
|
|
rv = FALSE;
|
|
|
|
goto next;
|
|
|
|
}
|
2010-12-11 12:52:03 +01:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
if (!clear_links (fullpath, subdir))
|
|
|
|
rv = FALSE;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
closedir (subdir);
|
|
|
|
}
|
2010-01-03 12:40:40 +01:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
next:
|
|
|
|
g_free (fullpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-01-03 12:40:40 +01:00
|
|
|
|
|
|
|
gboolean
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_clear_links (const char *path, GError **err)
|
2010-01-03 12:40:40 +01:00
|
|
|
{
|
2016-11-19 15:17:43 +01:00
|
|
|
DIR *dir;
|
|
|
|
gboolean rv;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2010-01-03 12:40:40 +01:00
|
|
|
g_return_val_if_fail (path, FALSE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
|
|
|
dir = opendir (path);
|
2016-11-19 15:17:43 +01:00
|
|
|
if (!dir) {
|
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_FILE_CANNOT_OPEN,
|
|
|
|
"failed to open %s: %s", path, strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = clear_links (path, dir);
|
2010-01-03 12:40:40 +01:00
|
|
|
|
|
|
|
closedir (dir);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
MuFlags
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_get_flags_from_path (const char *path)
|
2011-05-06 07:11:47 +02:00
|
|
|
{
|
2011-08-13 01:00:04 +02:00
|
|
|
g_return_val_if_fail (path, MU_FLAG_INVALID);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-05-06 07:11:47 +02:00
|
|
|
/* try to find the info part */
|
2020-05-23 03:21:57 +02:00
|
|
|
/* note that we can use either the ':', ';', or '!' as separator;
|
2011-05-06 07:11:47 +02:00
|
|
|
* the former is the official, but as it does not work on e.g. VFAT
|
|
|
|
* file systems, some Maildir implementations use the latter instead
|
|
|
|
* (or both). For example, Tinymail/modest does this. The python
|
|
|
|
* documentation at http://docs.python.org/lib/mailbox-maildir.html
|
2020-05-23 03:21:57 +02:00
|
|
|
* mentions the '!' as well as a 'popular choice'. Isync uses ';' by
|
|
|
|
* default on Windows.
|
2011-05-06 07:11:47 +02:00
|
|
|
*/
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
/* we check the dir -- */
|
|
|
|
if (strstr (path, G_DIR_SEPARATOR_S "new" G_DIR_SEPARATOR_S)) {
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
char *dir, *dir2;
|
|
|
|
MuFlags flags;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
dir = g_path_get_dirname (path);
|
|
|
|
dir2 = g_path_get_basename (dir);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2013-05-13 21:30:27 +02:00
|
|
|
flags = MU_FLAG_NONE;
|
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
if (g_strcmp0 (dir2, "new") == 0)
|
|
|
|
flags = MU_FLAG_NEW;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
g_free (dir);
|
|
|
|
g_free (dir2);
|
|
|
|
|
|
|
|
/* NOTE: new/ message should not have :2,-stuff, as
|
|
|
|
* per http://cr.yp.to/proto/maildir.html. If they, do
|
|
|
|
* we ignore it
|
|
|
|
*/
|
|
|
|
if (flags == MU_FLAG_NEW)
|
|
|
|
return flags;
|
|
|
|
}
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
/* get the file flags */
|
|
|
|
{
|
2020-11-28 09:15:49 +01:00
|
|
|
const char *info;
|
2011-08-13 01:00:04 +02:00
|
|
|
|
|
|
|
info = strrchr (path, '2');
|
|
|
|
if (!info || info == path ||
|
2020-05-23 03:21:57 +02:00
|
|
|
(info[-1] != ':' && info[-1] != '!' && info[-1] != ';') ||
|
2011-08-13 01:00:04 +02:00
|
|
|
(info[1] != ','))
|
|
|
|
return MU_FLAG_NONE;
|
2011-09-12 19:33:06 +02:00
|
|
|
else
|
2012-06-01 14:03:43 +02:00
|
|
|
return mu_flags_from_str
|
|
|
|
(&info[2], MU_FLAG_TYPE_MAILFILE,
|
|
|
|
TRUE /*ignore invalid */);
|
2011-08-13 01:00:04 +02:00
|
|
|
}
|
2011-05-06 07:11:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2019-11-06 16:13:39 +01:00
|
|
|
* take an existing message path, and return a new path, based on
|
2011-05-06 07:33:37 +02:00
|
|
|
* whether it should be in 'new' or 'cur'; ie.
|
2011-05-06 07:11:47 +02:00
|
|
|
*
|
2011-08-11 21:44:23 +02:00
|
|
|
* /home/user/Maildir/foo/bar/cur/abc:2,F and flags == MU_FLAG_NEW
|
2011-05-06 07:11:47 +02:00
|
|
|
* => /home/user/Maildir/foo/bar/new
|
2011-09-12 19:33:06 +02:00
|
|
|
* and
|
2011-08-11 21:44:23 +02:00
|
|
|
* /home/user/Maildir/foo/bar/new/abc and flags == MU_FLAG_REPLIED
|
2011-05-06 07:11:47 +02:00
|
|
|
* => /home/user/Maildir/foo/bar/cur
|
|
|
|
*
|
2013-12-01 19:21:44 +01:00
|
|
|
* so the difference is whether MU_FLAG_NEW is set or not; and in the
|
|
|
|
* latter case, no other flags are allowed.
|
2011-09-12 19:33:06 +02:00
|
|
|
*
|
2011-05-06 07:11:47 +02:00
|
|
|
*/
|
2020-05-26 18:07:56 +02:00
|
|
|
static char*
|
2012-06-13 08:10:22 +02:00
|
|
|
get_new_path (const char *mdir, const char *mfile, MuFlags flags,
|
2020-05-23 03:21:57 +02:00
|
|
|
const char* custom_flags, char flags_sep)
|
2011-09-12 19:33:06 +02:00
|
|
|
{
|
2011-08-13 01:00:04 +02:00
|
|
|
if (flags & MU_FLAG_NEW)
|
|
|
|
return g_strdup_printf ("%s%cnew%c%s",
|
|
|
|
mdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR,
|
|
|
|
mfile);
|
|
|
|
else {
|
|
|
|
const char *flagstr;
|
|
|
|
flagstr = mu_flags_to_str_s (flags, MU_FLAG_TYPE_MAILFILE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2020-05-23 03:21:57 +02:00
|
|
|
return g_strdup_printf ("%s%ccur%c%s%c2,%s%s",
|
2011-08-13 01:00:04 +02:00
|
|
|
mdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR,
|
2020-05-23 03:21:57 +02:00
|
|
|
mfile, flags_sep, flagstr,
|
2012-06-13 08:10:22 +02:00
|
|
|
custom_flags ? custom_flags : "");
|
2011-08-13 01:00:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-12 19:33:06 +02:00
|
|
|
char*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_get_maildir_from_path (const char* path)
|
2011-09-12 19:33:06 +02:00
|
|
|
{
|
2020-05-26 18:07:56 +02:00
|
|
|
char *mdir;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
|
|
|
/* determine the maildir */
|
|
|
|
mdir = g_path_get_dirname (path);
|
2012-03-19 19:46:17 +01:00
|
|
|
if (!g_str_has_suffix (mdir, "cur") &&
|
2020-11-28 09:15:49 +01:00
|
|
|
|
|
|
|
!g_str_has_suffix (mdir, "new")) {
|
2011-09-12 19:33:06 +02:00
|
|
|
g_warning ("%s: not a valid maildir path: %s",
|
2015-04-22 20:06:31 +02:00
|
|
|
__func__, path);
|
2011-09-12 19:33:06 +02:00
|
|
|
g_free (mdir);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove the 'cur' or 'new' */
|
|
|
|
mdir[strlen(mdir) - 4] = '\0';
|
|
|
|
|
|
|
|
return mdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-21 23:06:10 +02:00
|
|
|
static char*
|
|
|
|
get_new_basename (void)
|
|
|
|
{
|
2015-11-18 08:50:36 +01:00
|
|
|
return g_strdup_printf ("%u.%08x%08x.%s",
|
2015-11-18 14:51:08 +01:00
|
|
|
(guint)time(NULL),
|
2015-10-02 16:43:38 +02:00
|
|
|
g_random_int(),
|
|
|
|
(gint32)g_get_monotonic_time (),
|
2016-10-25 19:34:08 +02:00
|
|
|
g_get_host_name ());
|
2013-05-21 23:06:10 +02:00
|
|
|
}
|
2015-11-18 14:51:08 +01:00
|
|
|
|
2020-10-16 19:22:52 +02:00
|
|
|
static char*
|
|
|
|
find_path_separator(const char *path)
|
2020-10-10 18:20:16 +02:00
|
|
|
{
|
2020-10-16 19:34:55 +02:00
|
|
|
const char *cur;
|
|
|
|
for (cur = &path[strlen(path)-1]; cur > path; --cur) {
|
|
|
|
if ((*cur == ':' || *cur == '!' || *cur == ';') &&
|
|
|
|
(cur[1] == '2' && cur[2] == ',')) {
|
|
|
|
return (char*)cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2020-10-10 18:20:16 +02:00
|
|
|
}
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
char*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
|
2013-05-25 18:27:54 +02:00
|
|
|
MuFlags newflags, gboolean new_name)
|
2011-08-13 01:00:04 +02:00
|
|
|
{
|
2020-10-16 19:34:55 +02:00
|
|
|
char *mfile, *mdir, *custom_flags, *cur, *newpath, flags_sep = ':';
|
2011-08-13 01:00:04 +02:00
|
|
|
|
2011-05-06 07:11:47 +02:00
|
|
|
g_return_val_if_fail (oldpath, NULL);
|
2011-08-13 01:00:04 +02:00
|
|
|
|
2012-06-13 08:10:22 +02:00
|
|
|
mfile = newpath = custom_flags = NULL;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
/* determine the maildir */
|
2011-09-12 19:33:06 +02:00
|
|
|
mdir = mu_maildir_get_maildir_from_path (oldpath);
|
|
|
|
if (!mdir)
|
|
|
|
return NULL;
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2020-10-16 19:22:52 +02:00
|
|
|
/* determine the name of the location of the flag separator */
|
2020-10-10 18:20:16 +02:00
|
|
|
|
|
|
|
if (new_name) {
|
2013-05-25 18:27:54 +02:00
|
|
|
mfile = get_new_basename ();
|
2020-10-16 19:34:55 +02:00
|
|
|
cur = find_path_separator (oldpath);
|
|
|
|
if (cur) {
|
|
|
|
/* preserve the existing flags separator
|
|
|
|
* in the new file name */
|
|
|
|
flags_sep = *cur;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mfile = g_path_get_basename (oldpath);
|
|
|
|
cur = find_path_separator (mfile);
|
|
|
|
if (cur) {
|
|
|
|
/* get the custom flags (if any) */
|
|
|
|
custom_flags = mu_flags_custom_from_str (cur + 3);
|
|
|
|
/* preserve the existing flags separator
|
|
|
|
* in the new file name */
|
|
|
|
flags_sep = *cur;
|
|
|
|
cur[0] = '\0'; /* strip the flags */
|
|
|
|
}
|
2013-05-25 18:27:54 +02:00
|
|
|
}
|
2011-08-13 01:00:04 +02:00
|
|
|
|
|
|
|
newpath = get_new_path (new_mdir ? new_mdir : mdir,
|
2020-05-23 03:21:57 +02:00
|
|
|
mfile, newflags, custom_flags, flags_sep);
|
2011-09-12 19:33:06 +02:00
|
|
|
g_free (mfile);
|
2011-08-13 01:00:04 +02:00
|
|
|
g_free (mdir);
|
2012-06-13 08:10:22 +02:00
|
|
|
g_free (custom_flags);
|
2011-08-13 01:00:04 +02:00
|
|
|
|
2011-05-06 07:11:47 +02:00
|
|
|
return newpath;
|
|
|
|
}
|
|
|
|
|
2015-12-24 15:06:51 +01:00
|
|
|
|
|
|
|
static gint64
|
|
|
|
get_file_size (const char* path)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
rv = stat (path, &statbuf);
|
|
|
|
if (rv != 0) {
|
|
|
|
/* g_warning ("error: %s", strerror (errno)); */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (gint64)statbuf.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
static gboolean
|
2020-05-26 18:07:56 +02:00
|
|
|
msg_move_check_pre (const char *src, const char *dst, GError **err)
|
2011-05-06 07:11:47 +02:00
|
|
|
{
|
2015-12-24 15:06:51 +01:00
|
|
|
gint size1, size2;
|
2016-06-05 18:29:53 +02:00
|
|
|
|
2012-05-22 09:19:49 +02:00
|
|
|
if (!g_path_is_absolute(src))
|
|
|
|
return mu_util_g_set_error
|
|
|
|
(err, MU_ERROR_FILE,
|
|
|
|
"source is not an absolute path: '%s'", src);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2012-05-22 09:19:49 +02:00
|
|
|
if (!g_path_is_absolute(dst))
|
|
|
|
return mu_util_g_set_error
|
|
|
|
(err, MU_ERROR_FILE,
|
|
|
|
"target is not an absolute path: '%s'", dst);
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2012-05-22 09:19:49 +02:00
|
|
|
if (access (src, R_OK) != 0)
|
|
|
|
return mu_util_g_set_error (err, MU_ERROR_FILE,
|
|
|
|
"cannot read %s", src);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2015-12-24 15:06:51 +01:00
|
|
|
if (access (dst, F_OK) != 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* target exist; we simply overwrite it, unless target has a different
|
|
|
|
* size. ignore the exceedingly rare case where have duplicate message
|
|
|
|
* file names with different content yet the same length. (md5 etc. is a
|
|
|
|
* bit slow) */
|
|
|
|
size1 = get_file_size (src);
|
|
|
|
size2 = get_file_size (dst);
|
|
|
|
if (size1 != size2)
|
2012-05-22 09:19:49 +02:00
|
|
|
return mu_util_g_set_error (err, MU_ERROR_FILE,
|
|
|
|
"%s already exists", dst);
|
2011-08-13 01:00:04 +02:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
msg_move_check_post (const char *src, const char *dst, GError **err)
|
|
|
|
{
|
|
|
|
/* double check -- is the target really there? */
|
2012-05-22 09:19:49 +02:00
|
|
|
if (access (dst, F_OK) != 0)
|
|
|
|
return mu_util_g_set_error
|
2020-10-22 19:16:41 +02:00
|
|
|
(err, MU_ERROR_FILE, "can't find target (%s->%s)", src, dst);
|
|
|
|
|
|
|
|
if (access (src, F_OK) == 0) {
|
2020-11-01 10:54:57 +01:00
|
|
|
|
2020-10-22 19:16:41 +02:00
|
|
|
if (g_strcmp0(src, dst) == 0) {
|
|
|
|
g_warning ("moved %s to itself", src);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2020-11-01 10:54:57 +01:00
|
|
|
/* this could happen if some other tool (for mail syncing) is
|
|
|
|
* interfering */
|
|
|
|
g_debug ("the source is still there (%s->%s)",
|
2020-10-22 19:16:41 +02:00
|
|
|
src, dst);
|
|
|
|
}
|
2011-08-13 01:00:04 +02:00
|
|
|
|
|
|
|
return TRUE;
|
2011-05-06 07:11:47 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 20:40:02 +02:00
|
|
|
/* use GIO to move files; this is slower than rename() so only use
|
|
|
|
* this when needed: when moving across filesystems */
|
|
|
|
static gboolean
|
|
|
|
msg_move_g_file (const char* src, const char *dst, GError **err)
|
|
|
|
{
|
|
|
|
GFile *srcfile, *dstfile;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
srcfile = g_file_new_for_path (src);
|
|
|
|
dstfile = g_file_new_for_path (dst);
|
|
|
|
|
|
|
|
res = g_file_move (srcfile, dstfile, G_FILE_COPY_NONE,
|
|
|
|
NULL, NULL, NULL, err);
|
|
|
|
|
|
|
|
g_clear_object (&srcfile);
|
|
|
|
g_clear_object (&dstfile);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2011-08-13 01:00:04 +02:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
msg_move (const char* src, const char *dst, GError **err)
|
2011-05-06 07:11:47 +02:00
|
|
|
{
|
2011-08-13 01:00:04 +02:00
|
|
|
if (!msg_move_check_pre (src, dst, err))
|
|
|
|
return FALSE;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2020-05-25 20:40:02 +02:00
|
|
|
if (rename (src, dst) == 0) /* seems it worked. */
|
|
|
|
return msg_move_check_post (src, dst, err);
|
|
|
|
|
2020-07-25 16:27:34 +02:00
|
|
|
if (errno != EXDEV) /* some unrecoverable error occurred */
|
2012-05-22 09:19:49 +02:00
|
|
|
return mu_util_g_set_error
|
2020-10-22 19:16:41 +02:00
|
|
|
(err, MU_ERROR_FILE, "error moving %s -> %s", src, dst);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2020-05-25 20:40:02 +02:00
|
|
|
/* he EXDEV case -- source and target live on different filesystems */
|
|
|
|
return msg_move_g_file (src, dst, err);
|
2011-08-13 01:00:04 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 18:07:56 +02:00
|
|
|
char*
|
2020-11-28 09:15:49 +01:00
|
|
|
Mu::mu_maildir_move_message (const char* oldpath, const char* targetmdir,
|
2013-06-24 21:44:36 +02:00
|
|
|
MuFlags newflags, gboolean ignore_dups,
|
|
|
|
gboolean new_name, GError **err)
|
2011-08-13 01:00:04 +02:00
|
|
|
{
|
2020-05-25 20:26:00 +02:00
|
|
|
char *newfullpath;
|
|
|
|
gboolean rv;
|
|
|
|
gboolean src_is_target;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
g_return_val_if_fail (oldpath, FALSE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2020-05-25 20:26:00 +02:00
|
|
|
/* first try *without* changing the name (as per new_name), since
|
|
|
|
* src_is_target shouldn't use a changed name */
|
2011-08-13 01:00:04 +02:00
|
|
|
newfullpath = mu_maildir_get_new_path (oldpath, targetmdir,
|
2020-05-25 20:26:00 +02:00
|
|
|
newflags, FALSE);
|
2011-08-13 01:00:04 +02:00
|
|
|
if (!newfullpath) {
|
2012-05-22 09:19:49 +02:00
|
|
|
mu_util_g_set_error (err, MU_ERROR_FILE,
|
|
|
|
"failed to determine targetpath");
|
|
|
|
return NULL;
|
2011-08-13 01:00:04 +02:00
|
|
|
}
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2011-08-16 22:42:47 +02:00
|
|
|
src_is_target = (g_strcmp0 (oldpath, newfullpath) == 0);
|
|
|
|
if (!ignore_dups && src_is_target) {
|
2012-05-22 09:19:49 +02:00
|
|
|
mu_util_g_set_error (err, MU_ERROR_FILE_TARGET_EQUALS_SOURCE,
|
|
|
|
"target equals source");
|
|
|
|
return NULL;
|
2011-05-06 07:11:47 +02:00
|
|
|
}
|
2011-08-11 07:23:03 +02:00
|
|
|
|
2020-05-25 20:26:00 +02:00
|
|
|
/* if we generated file is not the same (modulo flags), create a fully
|
|
|
|
* new name in the new_name case */
|
|
|
|
if (!src_is_target && new_name) {
|
|
|
|
g_free(newfullpath);
|
|
|
|
newfullpath = mu_maildir_get_new_path (oldpath, targetmdir,
|
|
|
|
newflags, new_name);
|
|
|
|
if (!newfullpath) {
|
|
|
|
mu_util_g_set_error (err, MU_ERROR_FILE,
|
|
|
|
"failed to determine targetpath");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 22:42:47 +02:00
|
|
|
if (!src_is_target) {
|
2020-11-01 10:54:57 +01:00
|
|
|
g_debug ("moving %s (%s, %x, %d) --> %s",
|
|
|
|
oldpath, targetmdir, newflags, new_name,
|
|
|
|
newfullpath);
|
2011-08-16 22:42:47 +02:00
|
|
|
rv = msg_move (oldpath, newfullpath, err);
|
|
|
|
if (!rv) {
|
|
|
|
g_free (newfullpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-13 01:00:04 +02:00
|
|
|
}
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
return newfullpath;
|
2011-05-06 07:11:47 +02:00
|
|
|
}
|