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;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
#define MU_MAILDIR_NOINDEX_FILE ".noindex"
|
|
|
|
#define MU_MAILDIR_NOUPDATE_FILE ".noupdate"
|
2012-05-22 09:19:49 +02:00
|
|
|
|
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
|
2021-10-20 11:18:15 +02:00
|
|
|
get_dtype(struct dirent* dentry, const char* path, gboolean use_lstat)
|
2020-05-26 18:07:56 +02:00
|
|
|
{
|
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*/
|
2021-10-20 11:18:15 +02:00
|
|
|
return mu_util_get_dtype(path, use_lstat);
|
2020-05-26 18:07:56 +02:00
|
|
|
}
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
static gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
create_maildir(const char* path, mode_t mode, GError** err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02: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) {
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* fullpath;
|
|
|
|
int rv;
|
2010-01-24 13:55:22 +01:00
|
|
|
|
|
|
|
/* static buffer */
|
2021-10-20 11:18:15 +02: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 */
|
2021-10-20 11:18:15 +02:00
|
|
|
if (mu_util_check_dir(fullpath, TRUE, TRUE))
|
2012-02-26 11:16:57 +01:00
|
|
|
continue;
|
|
|
|
|
2021-10-20 11:18:15 +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))
|
2021-10-20 11:18:15 +02:00
|
|
|
return mu_util_g_set_error(err,
|
|
|
|
MU_ERROR_FILE_CANNOT_MKDIR,
|
|
|
|
"creating dir failed for %s: %s",
|
|
|
|
fullpath,
|
|
|
|
g_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
|
2021-10-20 11:18:15 +02: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 */
|
2021-10-20 11:18:15 +02:00
|
|
|
int fd;
|
|
|
|
const char* noindexpath;
|
2010-01-24 13:55:22 +01:00
|
|
|
|
|
|
|
/* static buffer */
|
2021-10-20 11:18:15 +02:00
|
|
|
noindexpath = mu_str_fullpath_s(path, MU_MAILDIR_NOINDEX_FILE);
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2021-10-20 11:18:15 +02: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...*/
|
2021-10-20 11:18:15 +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",
|
|
|
|
g_strerror(errno));
|
2009-12-31 18:27:41 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_maildir_mkdir(const char* path, mode_t mode, gboolean noindex, GError** err)
|
2011-09-12 19:33:06 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_val_if_fail(path, FALSE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!create_maildir(path, mode, err))
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
|
|
|
|
2021-10-20 11:18:15 +02: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
|
2021-10-20 11:18:15 +02: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;
|
2021-10-20 11:18:15 +02:00
|
|
|
char* srcpath;
|
2009-12-31 18:27:41 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
srcpath = g_path_get_dirname(src);
|
2012-06-01 12:35:46 +02:00
|
|
|
*in_cur = FALSE;
|
2021-10-20 11:18:15 +02:00
|
|
|
rv = TRUE;
|
2012-06-01 12:35:46 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (g_str_has_suffix(srcpath, "cur"))
|
2009-12-31 18:27:41 +01:00
|
|
|
*in_cur = TRUE;
|
2021-10-20 11:18:15 +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);
|
|
|
|
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*
|
2021-10-20 11:18:15 +02:00
|
|
|
get_target_fullpath(const char* src, const char* targetpath, GError** err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
char * targetfullpath, *srcfile;
|
2009-12-31 18:27:41 +01:00
|
|
|
gboolean in_cur;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02: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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
srcfile = g_path_get_basename(src);
|
2009-12-31 18:27:41 +01:00
|
|
|
|
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)
|
|
|
|
*/
|
2021-10-20 11:18:15 +02:00
|
|
|
targetfullpath = g_strdup_printf("%s%c%s%c%u_%s",
|
|
|
|
targetpath,
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
in_cur ? "cur" : "new",
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
g_str_hash(src),
|
|
|
|
srcfile);
|
|
|
|
g_free(srcfile);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
return targetfullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_maildir_link(const char* src, const char* targetpath, GError** err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
char* targetfullpath;
|
|
|
|
int rv;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_val_if_fail(src, FALSE);
|
|
|
|
g_return_val_if_fail(targetpath, FALSE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02: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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
rv = symlink(src, targetfullpath);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2012-05-22 09:19:49 +02:00
|
|
|
if (rv != 0)
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_util_g_set_error(err,
|
|
|
|
MU_ERROR_FILE_CANNOT_LINK,
|
|
|
|
"error creating link %s => %s: %s",
|
|
|
|
targetfullpath,
|
|
|
|
src,
|
|
|
|
g_strerror(errno));
|
|
|
|
g_free(targetfullpath);
|
|
|
|
|
|
|
|
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
|
2021-10-20 11:18:15 +02: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) */
|
2021-10-20 11:18:15 +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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (path[len - 4] == G_DIR_SEPARATOR && path[len - 3] == 'c' && path[len - 2] == 'u' &&
|
2011-04-16 13:26:06 +02:00
|
|
|
path[len - 1] == 'r')
|
|
|
|
return TRUE;
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (path[len - 4] == G_DIR_SEPARATOR && path[len - 3] == 'n' && path[len - 2] == 'e' &&
|
2011-04-16 13:26:06 +02:00
|
|
|
path[len - 1] == 'w')
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
|
|
|
|
2010-01-03 12:40:40 +01:00
|
|
|
static gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
clear_links(const char* path, DIR* dir)
|
2010-01-03 12:40:40 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02: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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
while ((dentry = readdir(dir))) {
|
|
|
|
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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
fullpath = g_build_path("/", path, dentry->d_name, NULL);
|
|
|
|
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) {
|
2021-10-20 11:18:15 +02:00
|
|
|
if (unlink(fullpath) != 0) {
|
|
|
|
g_warning("error unlinking %s: %s", fullpath, g_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) {
|
2021-10-20 11:18:15 +02:00
|
|
|
DIR* subdir;
|
|
|
|
subdir = opendir(fullpath);
|
2016-11-19 15:17:43 +01:00
|
|
|
if (!subdir) {
|
2021-10-20 11:18:15 +02:00
|
|
|
g_warning("failed to open dir %s: %s", fullpath, g_strerror(errno));
|
2016-11-19 15:17:43 +01:00
|
|
|
rv = FALSE;
|
|
|
|
goto next;
|
|
|
|
}
|
2010-12-11 12:52:03 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!clear_links(fullpath, subdir))
|
2016-11-19 15:17:43 +01:00
|
|
|
rv = FALSE;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
closedir(subdir);
|
2016-11-19 15:17:43 +01:00
|
|
|
}
|
2010-01-03 12:40:40 +01:00
|
|
|
|
2016-11-19 15:17:43 +01:00
|
|
|
next:
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(fullpath);
|
2016-11-19 15:17:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-01-03 12:40:40 +01:00
|
|
|
|
|
|
|
gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_maildir_clear_links(const char* path, GError** err)
|
2010-01-03 12:40:40 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
DIR* dir;
|
|
|
|
gboolean rv;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_val_if_fail(path, FALSE);
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
dir = opendir(path);
|
2016-11-19 15:17:43 +01:00
|
|
|
if (!dir) {
|
2021-10-20 11:18:15 +02:00
|
|
|
g_set_error(err,
|
|
|
|
MU_ERROR_DOMAIN,
|
|
|
|
MU_ERROR_FILE_CANNOT_OPEN,
|
|
|
|
"failed to open %s: %s",
|
|
|
|
path,
|
|
|
|
g_strerror(errno));
|
2016-11-19 15:17:43 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
rv = clear_links(path, dir);
|
2010-01-03 12:40:40 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
closedir(dir);
|
2010-01-03 12:40:40 +01:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
MuFlags
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_maildir_get_flags_from_path(const char* path)
|
2011-05-06 07:11:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +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 -- */
|
2021-10-20 11:18:15 +02:00
|
|
|
if (strstr(path, G_DIR_SEPARATOR_S "new" G_DIR_SEPARATOR_S)) {
|
|
|
|
char * dir, *dir2;
|
2011-08-13 01:00:04 +02:00
|
|
|
MuFlags flags;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +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;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (g_strcmp0(dir2, "new") == 0)
|
2011-08-13 01:00:04 +02:00
|
|
|
flags = MU_FLAG_NEW;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(dir);
|
|
|
|
g_free(dir2);
|
2011-08-13 01:00:04 +02:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* info;
|
2011-08-13 01:00:04 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
info = strrchr(path, '2');
|
2011-08-13 01:00:04 +02:00
|
|
|
if (!info || info == path ||
|
2021-10-20 11:18:15 +02:00
|
|
|
(info[-1] != ':' && info[-1] != '!' && info[-1] != ';') || (info[1] != ','))
|
2011-08-13 01:00:04 +02:00
|
|
|
return MU_FLAG_NONE;
|
2011-09-12 19:33:06 +02:00
|
|
|
else
|
2021-10-20 11:18:15 +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*
|
2021-10-20 11:18:15 +02:00
|
|
|
get_new_path(const char* mdir,
|
|
|
|
const char* mfile,
|
|
|
|
MuFlags flags,
|
|
|
|
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)
|
2021-10-20 11:18:15 +02:00
|
|
|
return g_strdup_printf("%s%cnew%c%s",
|
|
|
|
mdir,
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
mfile);
|
2011-08-13 01:00:04 +02:00
|
|
|
else {
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* flagstr;
|
|
|
|
flagstr = mu_flags_to_str_s(flags, MU_FLAG_TYPE_MAILFILE);
|
|
|
|
|
|
|
|
return g_strdup_printf("%s%ccur%c%s%c2,%s%s",
|
|
|
|
mdir,
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
mfile,
|
|
|
|
flags_sep,
|
|
|
|
flagstr,
|
|
|
|
custom_flags ? custom_flags : "");
|
2011-08-13 01:00:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 19:33:06 +02:00
|
|
|
char*
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_maildir_get_maildir_from_path(const char* path)
|
2011-09-12 19:33:06 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
char* mdir;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
|
|
|
/* determine the maildir */
|
2021-10-20 11:18:15 +02:00
|
|
|
mdir = g_path_get_dirname(path);
|
|
|
|
if (!g_str_has_suffix(mdir, "cur") &&
|
2020-11-28 09:15:49 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
!g_str_has_suffix(mdir, "new")) {
|
|
|
|
g_warning("%s: not a valid maildir path: %s", __func__, path);
|
|
|
|
g_free(mdir);
|
2011-09-12 19:33:06 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove the 'cur' or 'new' */
|
|
|
|
mdir[strlen(mdir) - 4] = '\0';
|
|
|
|
|
|
|
|
return mdir;
|
|
|
|
}
|
|
|
|
|
2013-05-21 23:06:10 +02:00
|
|
|
static char*
|
2021-10-20 11:18:15 +02:00
|
|
|
get_new_basename(void)
|
2013-05-21 23:06:10 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
return g_strdup_printf("%u.%08x%08x.%s",
|
|
|
|
(guint)time(NULL),
|
|
|
|
g_random_int(),
|
|
|
|
(gint32)g_get_monotonic_time(),
|
|
|
|
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*
|
2021-10-20 11:18:15 +02:00
|
|
|
find_path_separator(const char* path)
|
2020-10-10 18:20:16 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* cur;
|
|
|
|
for (cur = &path[strlen(path) - 1]; cur > path; --cur) {
|
2020-10-16 19:34:55 +02:00
|
|
|
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*
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_maildir_get_new_path(const char* oldpath,
|
|
|
|
const char* new_mdir,
|
|
|
|
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
|
|
|
|
2021-10-20 11:18:15 +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 */
|
2021-10-20 11:18:15 +02:00
|
|
|
mdir = mu_maildir_get_maildir_from_path(oldpath);
|
2011-09-12 19:33:06 +02:00
|
|
|
if (!mdir)
|
|
|
|
return NULL;
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
/* determine the name of the location of the flag separator */
|
2020-10-10 18:20:16 +02:00
|
|
|
|
|
|
|
if (new_name) {
|
2021-10-20 11:18:15 +02:00
|
|
|
mfile = get_new_basename();
|
|
|
|
cur = find_path_separator(oldpath);
|
2020-10-16 19:34:55 +02:00
|
|
|
if (cur) {
|
|
|
|
/* preserve the existing flags separator
|
|
|
|
* in the new file name */
|
|
|
|
flags_sep = *cur;
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-20 11:18:15 +02:00
|
|
|
mfile = g_path_get_basename(oldpath);
|
|
|
|
cur = find_path_separator(mfile);
|
2020-10-16 19:34:55 +02:00
|
|
|
if (cur) {
|
|
|
|
/* get the custom flags (if any) */
|
2021-10-20 11:18:15 +02:00
|
|
|
custom_flags = mu_flags_custom_from_str(cur + 3);
|
2020-10-16 19:34:55 +02:00
|
|
|
/* preserve the existing flags separator
|
|
|
|
* in the new file name */
|
|
|
|
flags_sep = *cur;
|
2021-10-20 11:18:15 +02:00
|
|
|
cur[0] = '\0'; /* strip the flags */
|
2020-10-16 19:34:55 +02:00
|
|
|
}
|
2013-05-25 18:27:54 +02:00
|
|
|
}
|
2011-08-13 01:00:04 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
newpath =
|
|
|
|
get_new_path(new_mdir ? new_mdir : mdir, mfile, newflags, custom_flags, flags_sep);
|
|
|
|
g_free(mfile);
|
|
|
|
g_free(mdir);
|
|
|
|
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
|
2021-10-20 11:18:15 +02:00
|
|
|
get_file_size(const char* path)
|
2015-12-24 15:06:51 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
int rv;
|
|
|
|
struct stat statbuf;
|
2015-12-24 15:06:51 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
rv = stat(path, &statbuf);
|
2015-12-24 15:06:51 +01:00
|
|
|
if (rv != 0) {
|
2021-07-28 22:58:55 +02:00
|
|
|
/* g_warning ("error: %s", g_strerror (errno)); */
|
2015-12-24 15:06:51 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (gint64)statbuf.st_size;
|
|
|
|
}
|
|
|
|
|
2011-08-13 01:00:04 +02:00
|
|
|
static gboolean
|
2021-10-20 11:18:15 +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))
|
2021-10-20 11:18:15 +02:00
|
|
|
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))
|
2021-10-20 11:18:15 +02:00
|
|
|
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
|
|
|
|
2021-10-20 11:18:15 +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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (access(dst, F_OK) != 0)
|
2015-12-24 15:06:51 +01:00
|
|
|
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) */
|
2021-10-20 11:18:15 +02:00
|
|
|
size1 = get_file_size(src);
|
|
|
|
size2 = get_file_size(dst);
|
2015-12-24 15:06:51 +01:00
|
|
|
if (size1 != size2)
|
2021-10-20 11:18:15 +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
|
2021-10-20 11:18:15 +02:00
|
|
|
msg_move_check_post(const char* src, const char* dst, GError** err)
|
2011-08-13 01:00:04 +02:00
|
|
|
{
|
|
|
|
/* double check -- is the target really there? */
|
2021-10-20 11:18:15 +02:00
|
|
|
if (access(dst, F_OK) != 0)
|
|
|
|
return mu_util_g_set_error(err,
|
|
|
|
MU_ERROR_FILE,
|
|
|
|
"can't find target (%s->%s)",
|
|
|
|
src,
|
|
|
|
dst);
|
|
|
|
|
|
|
|
if (access(src, F_OK) == 0) {
|
|
|
|
if (g_strcmp0(src, dst) == 0) {
|
|
|
|
g_warning("moved %s to itself", src);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
/* this could happen if some other tool (for mail syncing) is
|
|
|
|
* interfering */
|
|
|
|
g_debug("the source is still there (%s->%s)", 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
|
2021-10-20 11:18:15 +02:00
|
|
|
msg_move_g_file(const char* src, const char* dst, GError** err)
|
2020-05-25 20:40:02 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
GFile * srcfile, *dstfile;
|
|
|
|
gboolean res;
|
2020-05-25 20:40:02 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
srcfile = g_file_new_for_path(src);
|
|
|
|
dstfile = g_file_new_for_path(dst);
|
2020-05-25 20:40:02 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
res = g_file_move(srcfile, dstfile, G_FILE_COPY_NONE, NULL, NULL, NULL, err);
|
2020-05-25 20:40:02 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_clear_object(&srcfile);
|
|
|
|
g_clear_object(&dstfile);
|
2020-05-25 20:40:02 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2011-08-13 01:00:04 +02:00
|
|
|
|
|
|
|
static gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
msg_move(const char* src, const char* dst, GError** err)
|
2011-05-06 07:11:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!msg_move_check_pre(src, dst, err))
|
2011-08-13 01:00:04 +02:00
|
|
|
return FALSE;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (rename(src, dst) == 0) /* seems it worked. */
|
|
|
|
return msg_move_check_post(src, dst, err);
|
2020-05-25 20:40:02 +02:00
|
|
|
|
2020-07-25 16:27:34 +02:00
|
|
|
if (errno != EXDEV) /* some unrecoverable error occurred */
|
2021-10-20 11:18:15 +02:00
|
|
|
return mu_util_g_set_error(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 */
|
2021-10-20 11:18:15 +02:00
|
|
|
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*
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_maildir_move_message(const char* oldpath,
|
|
|
|
const char* targetmdir,
|
|
|
|
MuFlags newflags,
|
|
|
|
gboolean ignore_dups,
|
|
|
|
gboolean new_name,
|
|
|
|
GError** err)
|
2011-08-13 01:00:04 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
char* newfullpath;
|
|
|
|
gboolean rv;
|
|
|
|
gboolean src_is_target;
|
2011-09-12 19:33:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +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 */
|
2021-10-20 11:18:15 +02:00
|
|
|
newfullpath = mu_maildir_get_new_path(oldpath, targetmdir, newflags, FALSE);
|
2011-08-13 01:00:04 +02:00
|
|
|
if (!newfullpath) {
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_util_g_set_error(err, MU_ERROR_FILE, "failed to determine targetpath");
|
2012-05-22 09:19:49 +02:00
|
|
|
return NULL;
|
2011-08-13 01:00:04 +02:00
|
|
|
}
|
2011-05-06 07:11:47 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
src_is_target = (g_strcmp0(oldpath, newfullpath) == 0);
|
2011-08-16 22:42:47 +02:00
|
|
|
if (!ignore_dups && src_is_target) {
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_util_g_set_error(err,
|
|
|
|
MU_ERROR_FILE_TARGET_EQUALS_SOURCE,
|
|
|
|
"target equals source");
|
2012-05-22 09:19:49 +02:00
|
|
|
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);
|
2021-10-20 11:18:15 +02:00
|
|
|
newfullpath = mu_maildir_get_new_path(oldpath, targetmdir, newflags, new_name);
|
2020-05-25 20:26:00 +02:00
|
|
|
if (!newfullpath) {
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_util_g_set_error(err, MU_ERROR_FILE, "failed to determine targetpath");
|
2020-05-25 20:26:00 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 22:42:47 +02:00
|
|
|
if (!src_is_target) {
|
2021-10-20 11:18:15 +02:00
|
|
|
g_debug("moving %s (%s, %x, %d) --> %s",
|
|
|
|
oldpath,
|
|
|
|
targetmdir,
|
|
|
|
newflags,
|
|
|
|
new_name,
|
|
|
|
newfullpath);
|
|
|
|
rv = msg_move(oldpath, newfullpath, err);
|
2011-08-16 22:42:47 +02:00
|
|
|
if (!rv) {
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(newfullpath);
|
2011-08-16 22:42:47 +02:00
|
|
|
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
|
|
|
}
|