2009-12-31 18:27:41 +01:00
|
|
|
/*
|
2010-08-26 23:25:07 +02:00
|
|
|
** Copyright (C) 2010-2010 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
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2010-11-08 07:46:24 +01:00
|
|
|
#if HAVE_CONFIG_H
|
2010-01-16 10:32:07 +01:00
|
|
|
#include "config.h"
|
2010-08-26 23:25:07 +02:00
|
|
|
#endif /*HAVE_CONFIG_H*/
|
2010-01-16 10:32:07 +01:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-01-01 14:51:50 +01:00
|
|
|
#include <glib/gprintf.h>
|
2009-12-31 18:27:41 +01:00
|
|
|
|
2010-01-21 19:04:30 +01:00
|
|
|
#include "mu-util.h"
|
2009-12-31 18:27:41 +01:00
|
|
|
#include "mu-maildir.h"
|
2010-12-11 12:52:03 +01:00
|
|
|
#include "mu-str.h"
|
2009-12-31 18:27:41 +01:00
|
|
|
|
2010-01-04 19:17:12 +01:00
|
|
|
#define MU_MAILDIR_WALK_MAX_FILE_SIZE (32*1000*1000)
|
2010-01-11 19:47:01 +01:00
|
|
|
#define MU_MAILDIR_NOINDEX_FILE ".noindex"
|
2010-01-01 14:51:50 +01: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
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
|
|
|
|
#define GET_DTYPE(DE,FP) \
|
|
|
|
((DE)->d_type == DT_UNKNOWN ? mu_util_get_dtype_with_lstat((FP)) : (DE)->d_type)
|
|
|
|
#else
|
|
|
|
#define GET_DTYPE(DE,FP) \
|
|
|
|
mu_util_get_dtype_with_lstat((FP))
|
|
|
|
#endif /*HAVE_STRUCT_DIRENT_D_TYPE*/
|
2010-01-24 13:55:22 +01:00
|
|
|
|
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;
|
|
|
|
const gchar* subdirs[] = {"new", "cur", "tmp"};
|
|
|
|
|
|
|
|
/* make sure it does not exist yet */
|
|
|
|
if (access (path, F_OK) == 0)
|
|
|
|
errno = EEXIST;
|
|
|
|
if (errno != ENOENT) {
|
2010-01-01 15:15:41 +01:00
|
|
|
g_warning ("%s", strerror (errno));
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i != G_N_ELEMENTS(subdirs); ++i) {
|
|
|
|
|
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]);
|
2010-09-26 16:29:54 +02:00
|
|
|
rv = g_mkdir_with_parents (fullpath, (int)mode);
|
2009-12-31 18:27:41 +01:00
|
|
|
if (rv != 0) {
|
2010-12-05 15:29:53 +01:00
|
|
|
g_set_error (err, 0, MU_FILE_ERROR_CANNOT_MKDIR,
|
|
|
|
"g_mkdir_with_parents failed: %s",
|
|
|
|
strerror (errno));
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
create_noindex (const char *path, GError **err)
|
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);
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
if (fd < 0 || close (fd) != 0) {
|
2010-12-05 15:29:53 +01:00
|
|
|
g_set_error (err, 0, MU_ERROR_FILE_CANNOT_CREATE,
|
|
|
|
"error in create_noindex: %s",
|
|
|
|
strerror (errno));
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-01 15:15:41 +01:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex, GError **err)
|
2010-01-16 14:28:17 +01:00
|
|
|
{
|
2009-12-31 18:27:41 +01:00
|
|
|
g_return_val_if_fail (path, FALSE);
|
2010-12-05 14:40:40 +01:00
|
|
|
|
|
|
|
MU_WRITE_LOG ("%s (%s, %o, %s)", __FUNCTION__,
|
2010-12-05 15:29:53 +01:00
|
|
|
path, mode, noindex ? "TRUE" : "FALSE");
|
2009-12-31 18:27:41 +01:00
|
|
|
|
2010-12-05 15:29:53 +01: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;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
gchar *srcpath;
|
|
|
|
|
|
|
|
srcpath = g_path_get_dirname (src);
|
|
|
|
|
|
|
|
if (g_str_has_suffix (srcpath, "new"))
|
|
|
|
*in_cur = FALSE;
|
|
|
|
else if (g_str_has_suffix (srcpath, "cur"))
|
|
|
|
*in_cur = TRUE;
|
|
|
|
else {
|
2010-12-05 15:29:53 +01:00
|
|
|
g_set_error(err, 0, MU_ERROR_FILE_INVALID_SOURCE,
|
|
|
|
"invalid source message '%s'", src);
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
g_free (srcpath);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar*
|
2010-12-05 15:29:53 +01:00
|
|
|
get_target_fullpath (const char* src, const gchar *targetpath, GError **err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
2010-01-01 15:34:40 +01:00
|
|
|
gchar *targetfullpath, *srcfile, *srcpath, *c;
|
2009-12-31 18:27:41 +01:00
|
|
|
gboolean in_cur;
|
|
|
|
|
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;
|
|
|
|
|
2010-01-01 15:34:40 +01:00
|
|
|
/* note: make the filename *cough* unique by making the pathname
|
|
|
|
* part of the file name. This helps if there are copies of a
|
|
|
|
* message (which all have the same basename)*/
|
|
|
|
c = srcpath = g_path_get_dirname (src);
|
|
|
|
while (c && *c) {
|
|
|
|
if (*c == G_DIR_SEPARATOR)
|
|
|
|
*c = '_';
|
|
|
|
++c;
|
|
|
|
}
|
2009-12-31 18:27:41 +01:00
|
|
|
srcfile = g_path_get_basename (src);
|
|
|
|
|
|
|
|
/* create & test targetpath */
|
2010-01-01 15:34:40 +01:00
|
|
|
targetfullpath = g_strdup_printf ("%s%c%s%c%s_%s",
|
2009-12-31 18:27:41 +01:00
|
|
|
targetpath,
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
in_cur ? "cur" : "new",
|
|
|
|
G_DIR_SEPARATOR,
|
2010-01-01 15:34:40 +01:00
|
|
|
srcpath,
|
2009-12-31 18:27:41 +01:00
|
|
|
srcfile);
|
|
|
|
g_free (srcfile);
|
2010-01-01 15:34:40 +01:00
|
|
|
g_free (srcpath);
|
2009-12-31 18:27:41 +01:00
|
|
|
|
|
|
|
return targetfullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
mu_maildir_link (const char* src, const char *targetpath, GError **err)
|
2009-12-31 18:27:41 +01:00
|
|
|
{
|
|
|
|
gchar *targetfullpath;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (src, FALSE);
|
|
|
|
g_return_val_if_fail (targetpath, FALSE);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
rv = symlink (src, targetfullpath);
|
2010-01-02 15:05:23 +01:00
|
|
|
|
2009-12-31 18:27:41 +01:00
|
|
|
if (rv != 0) {
|
2010-12-05 15:29:53 +01:00
|
|
|
g_set_error (err, 0, 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);
|
2009-12-31 18:27:41 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-01-02 15:05:23 +01:00
|
|
|
g_free (targetfullpath);
|
2009-12-31 18:27:41 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2010-02-08 20:18:46 +01:00
|
|
|
static MuResult process_dir (const char* path, const gchar *mdir,
|
2010-01-24 13:55:22 +01:00
|
|
|
MuMaildirWalkMsgCallback msg_cb,
|
2010-01-01 14:51:50 +01:00
|
|
|
MuMaildirWalkDirCallback dir_cb, void *data);
|
|
|
|
|
|
|
|
static MuResult
|
2010-02-08 20:18:46 +01:00
|
|
|
process_file (const char* fullpath, const gchar* mdir,
|
|
|
|
MuMaildirWalkMsgCallback msg_cb, void *data)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
|
|
|
MuResult result;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
2010-01-25 08:28:08 +01:00
|
|
|
if (!msg_cb)
|
2010-01-01 14:51:50 +01:00
|
|
|
return MU_OK;
|
|
|
|
|
|
|
|
if (G_UNLIKELY(access(fullpath, R_OK) != 0)) {
|
2010-01-24 13:55:22 +01:00
|
|
|
g_warning ("cannot access %s: %s", fullpath,
|
|
|
|
strerror(errno));
|
2010-01-01 14:51:50 +01:00
|
|
|
return MU_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_UNLIKELY(stat (fullpath, &statbuf) != 0)) {
|
|
|
|
g_warning ("cannot stat %s: %s", fullpath, strerror(errno));
|
|
|
|
return MU_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_UNLIKELY(statbuf.st_size > MU_MAILDIR_WALK_MAX_FILE_SIZE)) {
|
|
|
|
g_warning ("ignoring because bigger than %d bytes: %s",
|
|
|
|
MU_MAILDIR_WALK_MAX_FILE_SIZE, fullpath);
|
2010-01-04 19:17:12 +01:00
|
|
|
return MU_OK; /* not an error */
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
2010-02-09 20:53:12 +01:00
|
|
|
|
2010-01-07 08:00:48 +01:00
|
|
|
/*
|
|
|
|
* use the ctime, so any status change will be visible (perms,
|
|
|
|
* filename etc.)
|
|
|
|
*/
|
2010-02-08 20:18:46 +01:00
|
|
|
result = (msg_cb)(fullpath, mdir, statbuf.st_ctime, data);
|
2010-01-25 08:28:08 +01:00
|
|
|
if (result == MU_STOP)
|
|
|
|
g_debug ("callback said 'MU_STOP' for %s", fullpath);
|
|
|
|
else if (result == MU_ERROR)
|
2010-01-01 14:51:50 +01:00
|
|
|
g_warning ("%s: failed %d in callback (%s)",
|
|
|
|
__FUNCTION__, result, fullpath);
|
2010-02-08 20:18:46 +01:00
|
|
|
|
2010-01-25 08:28:08 +01:00
|
|
|
return result;
|
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)
|
|
|
|
*/
|
|
|
|
G_GNUC_CONST static gboolean
|
2010-01-15 21:11:51 +01:00
|
|
|
is_maildir_new_or_cur (const char *path)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char *sfx;
|
|
|
|
|
|
|
|
/* 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) */
|
2010-01-01 14:51:50 +01:00
|
|
|
if (!path||(len = strlen(path)) < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sfx = &path[len - 4];
|
|
|
|
|
|
|
|
if (sfx[0] != G_DIR_SEPARATOR) /* small optimization */
|
|
|
|
return FALSE;
|
|
|
|
else if (sfx[1] != 'c' && sfx[1] != 'n')
|
|
|
|
return FALSE; /* optimization */
|
|
|
|
else
|
|
|
|
return (strcmp (sfx + 1, "cur") == 0 ||
|
|
|
|
strcmp (sfx + 1, "new") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if there is a noindex file (MU_WALK_NOINDEX_FILE) in this dir; */
|
|
|
|
static gboolean
|
2010-01-15 21:11:51 +01:00
|
|
|
has_noindex_file (const char *path)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
2010-01-24 13:55:22 +01:00
|
|
|
const char* noindexpath;
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2010-01-24 13:55:22 +01:00
|
|
|
/* static buffer */
|
2010-12-11 12:52:03 +01:00
|
|
|
noindexpath = mu_str_fullpath_s (path, MU_MAILDIR_NOINDEX_FILE);
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2010-01-24 13:55:22 +01:00
|
|
|
if (access (noindexpath, F_OK) == 0)
|
2010-01-01 14:51:50 +01:00
|
|
|
return TRUE;
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2010-11-14 20:53:24 +01:00
|
|
|
else if (G_UNLIKELY(errno != ENOENT))
|
2010-01-24 13:55:22 +01:00
|
|
|
g_warning ("error testing for noindex file %s: %s",
|
|
|
|
noindexpath, strerror(errno));
|
2010-01-01 14:51:50 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
static gboolean
|
2010-12-11 12:52:03 +01:00
|
|
|
ignore_dir_entry (struct dirent *entry, unsigned char d_type)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
|
|
|
const char *name;
|
2010-12-11 12:52:03 +01:00
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
/* if it's not a dir and not a file, ignore it.
|
|
|
|
* note, this means also symlinks (DT_LNK) are ignored,
|
2010-12-11 12:52:03 +01:00
|
|
|
* maybe make this optional */
|
|
|
|
if (d_type != DT_REG && d_type != DT_DIR)
|
2010-01-01 14:51:50 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
name = entry->d_name;
|
|
|
|
|
|
|
|
if (name[0] != '.')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* ignore . and .. */
|
|
|
|
if (name[1] == '\0' ||
|
|
|
|
(name[1] == '.' && name[2] == '\0'))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ignore .notmuch, .nnmaildir */
|
2010-01-08 19:53:21 +01:00
|
|
|
if ((name[1] == 'n') && /* optimization */
|
2010-01-01 20:00:09 +01:00
|
|
|
(strcmp (name, ".notmuch") == 0 ||
|
|
|
|
strcmp (name, ".nnmaildir") == 0))
|
|
|
|
return TRUE;
|
2010-01-01 14:51:50 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-02-08 20:18:46 +01:00
|
|
|
|
|
|
|
static gchar*
|
|
|
|
get_mdir_for_path (const gchar *old_mdir, const gchar *dir)
|
|
|
|
{
|
|
|
|
if (dir[0] != 'n' && dir[0] != 'c' &&
|
2010-02-15 19:24:00 +01:00
|
|
|
strcmp(dir, "cur") != 0 && strcmp(dir, "new") != 0)
|
|
|
|
return g_strconcat (old_mdir ? old_mdir : "",
|
|
|
|
G_DIR_SEPARATOR_S, dir, NULL);
|
2010-02-08 20:18:46 +01:00
|
|
|
else
|
2010-02-15 19:24:00 +01:00
|
|
|
return strdup (old_mdir ? old_mdir : G_DIR_SEPARATOR_S);
|
2010-02-08 20:18:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
static MuResult
|
2010-11-14 20:53:24 +01:00
|
|
|
process_dir_entry (const char* path, const char* mdir, struct dirent *entry,
|
2010-01-03 12:40:40 +01:00
|
|
|
MuMaildirWalkMsgCallback cb_msg,
|
|
|
|
MuMaildirWalkDirCallback cb_dir,
|
2010-01-01 14:51:50 +01:00
|
|
|
void *data)
|
|
|
|
{
|
2010-01-24 13:55:22 +01:00
|
|
|
const char *fp;
|
2010-01-01 14:51:50 +01:00
|
|
|
char* fullpath;
|
2010-12-11 12:52:03 +01:00
|
|
|
unsigned char d_type;
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2010-01-24 13:55:22 +01:00
|
|
|
/* we have to copy the buffer from fullpath_s, because it
|
|
|
|
* returns a static buffer */
|
2010-12-11 12:52:03 +01:00
|
|
|
fp = mu_str_fullpath_s (path, entry->d_name);
|
2010-01-24 13:55:22 +01:00
|
|
|
fullpath = g_newa (char, strlen(fp) + 1);
|
|
|
|
strcpy (fullpath, fp);
|
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
d_type = GET_DTYPE(entry, fullpath);
|
|
|
|
|
|
|
|
/* ignore special files/dirs */
|
|
|
|
if (ignore_dir_entry (entry, d_type))
|
|
|
|
return MU_OK;
|
|
|
|
|
|
|
|
switch (d_type) {
|
|
|
|
case DT_REG: /* we only want files in cur/ and new/ */
|
2010-01-15 21:11:51 +01:00
|
|
|
if (!is_maildir_new_or_cur (path))
|
2010-12-11 12:52:03 +01:00
|
|
|
return MU_OK;
|
2010-01-07 08:00:48 +01:00
|
|
|
|
2010-02-08 20:18:46 +01:00
|
|
|
return process_file (fullpath, mdir, cb_msg, data);
|
2010-01-01 14:51:50 +01:00
|
|
|
|
|
|
|
case DT_DIR: {
|
2010-02-08 20:18:46 +01:00
|
|
|
char *my_mdir;
|
|
|
|
MuResult rv;
|
|
|
|
|
|
|
|
my_mdir = get_mdir_for_path (mdir, entry->d_name);
|
|
|
|
rv = process_dir (fullpath, my_mdir, cb_msg, cb_dir, data);
|
|
|
|
g_free (my_mdir);
|
2010-12-11 12:52:03 +01:00
|
|
|
|
2010-02-08 20:18:46 +01:00
|
|
|
return rv;
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return MU_OK; /* ignore other types */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-11 19:47:01 +01:00
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
static struct dirent*
|
2010-01-15 21:11:51 +01:00
|
|
|
dirent_copy (struct dirent *entry)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
2010-12-11 12:52:03 +01:00
|
|
|
struct dirent *d;
|
2010-09-09 07:19:00 +02:00
|
|
|
|
|
|
|
d = g_slice_new (struct dirent);
|
2010-12-11 12:52:03 +01:00
|
|
|
|
|
|
|
/* NOTE: simply memcpy'ing sizeof(struct dirent) bytes will
|
|
|
|
* give memory errors. */
|
2010-09-09 07:19:00 +02:00
|
|
|
return (struct dirent*) memcpy (d, entry, entry->d_reclen);
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-01-15 21:11:51 +01:00
|
|
|
dirent_destroy (struct dirent *entry)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
2010-09-09 07:19:00 +02:00
|
|
|
g_slice_free (struct dirent, entry);
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
|
|
|
|
2010-01-16 10:32:07 +01:00
|
|
|
#ifdef HAVE_STRUCT_DIRENT_D_INO
|
2010-09-26 16:29:54 +02:00
|
|
|
static int
|
2010-01-15 21:11:51 +01:00
|
|
|
dirent_cmp (struct dirent *d1, struct dirent *d2)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
2010-09-26 16:29:54 +02:00
|
|
|
/* we do it his way instead of a simple d1->d_ino - d2->d_ino
|
|
|
|
* because this way, we don't need 64-bit numbers for the
|
|
|
|
* actual sorting */
|
|
|
|
if (d1->d_ino < d2->d_ino)
|
|
|
|
return -1;
|
|
|
|
else if (d1->d_ino > d2->d_ino)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
2010-01-16 10:32:07 +01:00
|
|
|
#endif /*HAVE_STRUCT_DIRENT_D_INO*/
|
2010-01-01 14:51:50 +01:00
|
|
|
|
|
|
|
static MuResult
|
2010-12-11 12:52:03 +01:00
|
|
|
process_dir_entries (DIR *dir, const char* path, const char* mdir,
|
|
|
|
MuMaildirWalkMsgCallback msg_cb,
|
|
|
|
MuMaildirWalkDirCallback dir_cb, void *data)
|
2010-01-01 14:51:50 +01:00
|
|
|
{
|
2010-01-07 08:00:48 +01:00
|
|
|
MuResult result;
|
2010-01-01 14:51:50 +01:00
|
|
|
GList *lst, *c;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
|
|
|
lst = NULL;
|
2010-12-11 12:52:03 +01:00
|
|
|
while ((entry = readdir (dir)))
|
2010-01-15 21:11:51 +01:00
|
|
|
lst = g_list_prepend (lst, dirent_copy(entry));
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
/* we sort by inode; this makes things much faster on
|
|
|
|
* extfs2,3 */
|
2010-01-16 10:32:07 +01:00
|
|
|
#if HAVE_STRUCT_DIRENT_D_INO
|
2010-01-15 21:11:51 +01:00
|
|
|
c = lst = g_list_sort (lst, (GCompareFunc)dirent_cmp);
|
2010-01-16 10:32:07 +01:00
|
|
|
#endif /*HAVE_STRUCT_DIRENT_D_INO*/
|
|
|
|
|
2010-01-25 08:28:08 +01:00
|
|
|
for (c = lst, result = MU_OK; c && result == MU_OK; c = c->next) {
|
2010-12-11 12:52:03 +01:00
|
|
|
result = process_dir_entry (path, mdir,
|
|
|
|
(struct dirent*)c->data,
|
2010-01-01 14:51:50 +01:00
|
|
|
msg_cb, dir_cb, data);
|
2010-01-25 08:28:08 +01:00
|
|
|
/* hmmm, break on MU_ERROR as well? */
|
|
|
|
if (result == MU_STOP)
|
|
|
|
break;
|
|
|
|
}
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2010-01-15 21:11:51 +01:00
|
|
|
g_list_foreach (lst, (GFunc)dirent_destroy, NULL);
|
2010-01-01 14:51:50 +01:00
|
|
|
g_list_free (lst);
|
|
|
|
|
2010-02-02 07:46:01 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MuResult
|
2010-02-08 20:18:46 +01:00
|
|
|
process_dir (const char* path, const char* mdir,
|
|
|
|
MuMaildirWalkMsgCallback msg_cb,
|
2010-02-02 07:46:01 +01:00
|
|
|
MuMaildirWalkDirCallback dir_cb, void *data)
|
|
|
|
{
|
|
|
|
MuResult result;
|
|
|
|
DIR* dir;
|
|
|
|
|
2010-02-09 20:53:12 +01:00
|
|
|
/* if it has a noindex file, we ignore this dir */
|
|
|
|
if (has_noindex_file (path)) {
|
|
|
|
g_debug ("found .noindex: ignoring dir %s", path);
|
|
|
|
return MU_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-02 07:46:01 +01:00
|
|
|
dir = opendir (path);
|
|
|
|
if (G_UNLIKELY(!dir)) {
|
2010-09-12 20:39:15 +02:00
|
|
|
g_warning ("%s: ignoring %s: %s", __FUNCTION__,
|
|
|
|
path, strerror(errno));
|
2010-02-02 07:46:01 +01:00
|
|
|
return MU_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir_cb) {
|
2010-09-12 20:39:15 +02:00
|
|
|
MuResult rv;
|
|
|
|
rv = dir_cb (path, TRUE, data);
|
2010-02-02 07:46:01 +01:00
|
|
|
if (rv != MU_OK) {
|
|
|
|
closedir (dir);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
result = process_dir_entries (dir, path, mdir, msg_cb, dir_cb, data);
|
2010-01-01 14:51:50 +01:00
|
|
|
closedir (dir);
|
|
|
|
|
2010-09-12 20:39:15 +02:00
|
|
|
/* only run dir_cb if it exists and so far, things went ok */
|
|
|
|
if (dir_cb && result == MU_OK)
|
2010-01-01 14:51:50 +01:00
|
|
|
return dir_cb (path, FALSE, data);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-01-02 15:05:23 +01:00
|
|
|
|
2010-01-01 14:51:50 +01:00
|
|
|
MuResult
|
|
|
|
mu_maildir_walk (const char *path, MuMaildirWalkMsgCallback cb_msg,
|
|
|
|
MuMaildirWalkDirCallback cb_dir, void *data)
|
|
|
|
{
|
2010-02-08 20:18:46 +01:00
|
|
|
MuResult rv;
|
|
|
|
char *mypath;
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2010-01-02 15:05:23 +01:00
|
|
|
g_return_val_if_fail (path && cb_msg, MU_ERROR);
|
2010-02-08 20:18:46 +01:00
|
|
|
g_return_val_if_fail (mu_util_check_dir(path, TRUE, FALSE), MU_ERROR);
|
2010-01-01 14:51:50 +01:00
|
|
|
|
2010-02-08 20:18:46 +01:00
|
|
|
/* skip the final slash from dirnames */
|
|
|
|
mypath = g_strdup (path);
|
2010-01-02 15:05:23 +01:00
|
|
|
|
2010-02-15 19:24:00 +01:00
|
|
|
/* strip the final / or \ */
|
2010-02-08 20:18:46 +01:00
|
|
|
if (mypath[strlen(mypath)-1] == G_DIR_SEPARATOR)
|
|
|
|
mypath[strlen(mypath)-1] = '\0';
|
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
rv = process_dir (mypath, NULL, cb_msg, cb_dir, data);
|
2010-02-08 20:18:46 +01:00
|
|
|
g_free (mypath);
|
|
|
|
|
|
|
|
return rv;
|
2010-01-01 14:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-03 12:40:40 +01:00
|
|
|
static gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
clear_links (const gchar* dirname, DIR *dir, GError **err)
|
2010-01-03 12:40:40 +01:00
|
|
|
{
|
|
|
|
struct dirent *entry;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
rv = TRUE;
|
2010-12-11 12:52:03 +01:00
|
|
|
errno = 0;
|
|
|
|
while ((entry = readdir (dir))) {
|
2010-01-03 12:40:40 +01:00
|
|
|
|
2010-01-24 13:55:22 +01:00
|
|
|
const char *fp;
|
|
|
|
char *fullpath;
|
2010-12-11 12:52:03 +01:00
|
|
|
unsigned char d_type;
|
|
|
|
|
2010-01-24 13:55:22 +01:00
|
|
|
/* ignore empty, dot thingies */
|
|
|
|
if (!entry->d_name || entry->d_name[0] == '.')
|
2010-01-03 12:40:40 +01:00
|
|
|
continue;
|
|
|
|
|
2010-02-15 19:24:00 +01:00
|
|
|
/* we have to copy the buffer from fullpath_s, because
|
2010-12-11 12:52:03 +01:00
|
|
|
* it returns a static buffer and we are
|
|
|
|
* recursive*/
|
|
|
|
fp = mu_str_fullpath_s (dirname, entry->d_name);
|
2010-01-24 13:55:22 +01:00
|
|
|
fullpath = g_newa (char, strlen(fp) + 1);
|
|
|
|
strcpy (fullpath, fp);
|
2010-12-11 12:52:03 +01:00
|
|
|
|
|
|
|
d_type = GET_DTYPE (entry, fullpath);
|
|
|
|
|
|
|
|
/* ignore non-links / non-dirs */
|
|
|
|
if (d_type != DT_LNK && d_type != DT_DIR)
|
|
|
|
continue;
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
if (d_type == DT_LNK) {
|
2010-01-24 13:55:22 +01:00
|
|
|
if (unlink (fullpath) != 0) {
|
2010-12-05 15:29:53 +01:00
|
|
|
/* don't use err */
|
|
|
|
g_warning ("error unlinking %s: %s",
|
|
|
|
fullpath, strerror(errno));
|
2010-01-24 13:55:22 +01:00
|
|
|
rv = FALSE;
|
|
|
|
}
|
|
|
|
} else /* DT_DIR, see check before*/
|
2010-12-05 15:29:53 +01:00
|
|
|
rv = mu_maildir_clear_links (fullpath, err);
|
2010-01-03 12:40:40 +01:00
|
|
|
}
|
2010-12-11 12:52:03 +01:00
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
g_set_error (err, 0, MU_ERROR_FILE,"file error: %s", strerror(errno));
|
2010-01-24 13:55:22 +01:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
return (rv == FALSE && errno == 0);
|
2010-01-03 12:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2010-12-05 15:29:53 +01:00
|
|
|
mu_maildir_clear_links (const gchar* path, GError **err)
|
2010-01-03 12:40:40 +01:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (path, FALSE);
|
|
|
|
|
|
|
|
dir = opendir (path);
|
|
|
|
if (!dir) {
|
2010-12-05 15:29:53 +01:00
|
|
|
g_set_error (err, 0, MU_ERROR_FILE_CANNOT_OPEN,
|
|
|
|
"failed to open %s: %s", path,
|
|
|
|
strerror(errno));
|
2010-01-03 12:40:40 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-12-05 15:29:53 +01:00
|
|
|
rv = clear_links (path, dir, err);
|
2010-01-03 12:40:40 +01:00
|
|
|
closedir (dir);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|