* mu-maildir: make public mu_maildir_get_maildir_from_path, add tests

This commit is contained in:
Dirk-Jan C. Binnema 2011-09-12 20:33:06 +03:00
parent 1a2614ec98
commit b944da0ae1
3 changed files with 221 additions and 169 deletions

View File

@ -1,6 +1,6 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/* /*
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** This program is free software; you can redistribute it and/or modify it
@ -15,8 +15,8 @@
** **
** You should have received a copy of the GNU General Public License ** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, ** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
** **
*/ */
#if HAVE_CONFIG_H #if HAVE_CONFIG_H
@ -77,7 +77,7 @@ create_maildir (const char *path, mode_t mode, GError **err)
g_set_error (err, 0, MU_ERROR_FILE, "%s", strerror (errno)); g_set_error (err, 0, MU_ERROR_FILE, "%s", strerror (errno));
return FALSE; return FALSE;
} }
for (i = 0; i != G_N_ELEMENTS(subdirs); ++i) { for (i = 0; i != G_N_ELEMENTS(subdirs); ++i) {
const char *fullpath; const char *fullpath;
@ -93,13 +93,13 @@ create_maildir (const char *path, mode_t mode, GError **err)
return FALSE; return FALSE;
} }
} }
return TRUE; return TRUE;
} }
static gboolean static gboolean
create_noindex (const char *path, GError **err) create_noindex (const char *path, GError **err)
{ {
/* create a noindex file if requested */ /* create a noindex file if requested */
int fd; int fd;
const char *noindexpath; const char *noindexpath;
@ -108,7 +108,7 @@ create_noindex (const char *path, GError **err)
noindexpath = mu_str_fullpath_s (path, MU_MAILDIR_NOINDEX_FILE); noindexpath = mu_str_fullpath_s (path, MU_MAILDIR_NOINDEX_FILE);
fd = creat (noindexpath, 0644); fd = creat (noindexpath, 0644);
/* note, if the 'close' failed, creation may still have /* note, if the 'close' failed, creation may still have
* succeeded...*/ * succeeded...*/
if (fd < 0 || close (fd) != 0) { if (fd < 0 || close (fd) != 0) {
@ -117,24 +117,24 @@ create_noindex (const char *path, GError **err)
strerror (errno)); strerror (errno));
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
} }
gboolean gboolean
mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex, GError **err) mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex, GError **err)
{ {
g_return_val_if_fail (path, FALSE); g_return_val_if_fail (path, FALSE);
MU_WRITE_LOG ("%s (%s, %o, %s)", __FUNCTION__, MU_WRITE_LOG ("%s (%s, %o, %s)", __FUNCTION__,
path, mode, noindex ? "TRUE" : "FALSE"); path, mode, noindex ? "TRUE" : "FALSE");
if (!create_maildir (path, mode, err)) if (!create_maildir (path, mode, err))
return FALSE; return FALSE;
if (noindex && !create_noindex (path, err)) if (noindex && !create_noindex (path, err))
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
@ -157,7 +157,7 @@ check_subdir (const char *src, gboolean *in_cur, GError **err)
return FALSE; return FALSE;
} }
g_free (srcpath); g_free (srcpath);
return TRUE; return TRUE;
} }
@ -166,10 +166,10 @@ get_target_fullpath (const char* src, const gchar *targetpath, GError **err)
{ {
gchar *targetfullpath, *srcfile; gchar *targetfullpath, *srcfile;
gboolean in_cur; gboolean in_cur;
if (!check_subdir (src, &in_cur, err)) if (!check_subdir (src, &in_cur, err))
return NULL; return NULL;
srcfile = g_path_get_basename (src); srcfile = g_path_get_basename (src);
/* create targetpath; note: make the filename cough* unique by /* create targetpath; note: make the filename cough* unique by
@ -184,7 +184,7 @@ get_target_fullpath (const char* src, const gchar *targetpath, GError **err)
g_str_hash(src), g_str_hash(src),
srcfile); srcfile);
g_free (srcfile); g_free (srcfile);
return targetfullpath; return targetfullpath;
} }
@ -194,16 +194,16 @@ mu_maildir_link (const char* src, const char *targetpath, GError **err)
{ {
gchar *targetfullpath; gchar *targetfullpath;
int rv; int rv;
g_return_val_if_fail (src, FALSE); g_return_val_if_fail (src, FALSE);
g_return_val_if_fail (targetpath, FALSE); g_return_val_if_fail (targetpath, FALSE);
targetfullpath = get_target_fullpath (src, targetpath, err); targetfullpath = get_target_fullpath (src, targetpath, err);
if (!targetfullpath) if (!targetfullpath)
return FALSE; return FALSE;
rv = symlink (src, targetfullpath); rv = symlink (src, targetfullpath);
if (rv != 0) { if (rv != 0) {
g_set_error (err, 0, MU_ERROR_FILE_CANNOT_LINK, g_set_error (err, 0, MU_ERROR_FILE_CANNOT_LINK,
"error creating link %s => %s: %s", "error creating link %s => %s: %s",
@ -219,16 +219,16 @@ mu_maildir_link (const char* src, const char *targetpath, GError **err)
static MuError static MuError
process_dir (const char* path, const gchar *mdir, process_dir (const char* path, const gchar *mdir,
MuMaildirWalkMsgCallback msg_cb, MuMaildirWalkMsgCallback msg_cb,
MuMaildirWalkDirCallback dir_cb, void *data); MuMaildirWalkDirCallback dir_cb, void *data);
static MuError static MuError
process_file (const char* fullpath, const gchar* mdir, process_file (const char* fullpath, const gchar* mdir,
MuMaildirWalkMsgCallback msg_cb, void *data) MuMaildirWalkMsgCallback msg_cb, void *data)
{ {
MuError result; MuError result;
struct stat statbuf; struct stat statbuf;
if (!msg_cb) if (!msg_cb)
return MU_OK; return MU_OK;
@ -237,17 +237,17 @@ process_file (const char* fullpath, const gchar* mdir,
strerror(errno)); strerror(errno));
return MU_ERROR; return MU_ERROR;
} }
if (G_UNLIKELY(stat (fullpath, &statbuf) != 0)) { if (G_UNLIKELY(stat (fullpath, &statbuf) != 0)) {
g_warning ("cannot stat %s: %s", fullpath, strerror(errno)); g_warning ("cannot stat %s: %s", fullpath, strerror(errno));
return MU_ERROR; return MU_ERROR;
} }
result = (msg_cb)(fullpath, mdir, &statbuf, data); result = (msg_cb)(fullpath, mdir, &statbuf, data);
if (result == MU_STOP) if (result == MU_STOP)
g_debug ("callback said 'MU_STOP' for %s", fullpath); g_debug ("callback said 'MU_STOP' for %s", fullpath);
else if (result == MU_ERROR) else if (result == MU_ERROR)
g_warning ("%s: error in callback (%s)", g_warning ("%s: error in callback (%s)",
__FUNCTION__, fullpath); __FUNCTION__, fullpath);
return result; return result;
@ -258,29 +258,29 @@ process_file (const char* fullpath, const gchar* mdir,
* determine if path is a maildir leaf-dir; ie. if it's 'cur' or 'new' * determine if path is a maildir leaf-dir; ie. if it's 'cur' or 'new'
* (we're skipping 'tmp' for obvious reasons) * (we're skipping 'tmp' for obvious reasons)
*/ */
G_GNUC_CONST static gboolean G_GNUC_CONST static gboolean
is_maildir_new_or_cur (const char *path) is_maildir_new_or_cur (const char *path)
{ {
size_t len; size_t len;
g_return_val_if_fail (path, FALSE); g_return_val_if_fail (path, FALSE);
/* path is the full path; it cannot possibly be shorter /* path is the full path; it cannot possibly be shorter
* than 4 for a maildir (/cur or /new) */ * than 4 for a maildir (/cur or /new) */
len = strlen (path); len = strlen (path);
if (G_UNLIKELY(len < 4)) if (G_UNLIKELY(len < 4))
return FALSE; return FALSE;
/* optimization; one further idea would be cast the 4 bytes to an integer /* optimization; one further idea would be cast the 4 bytes to an integer
* and compare that -- need to think about alignment, endianness */ * and compare that -- need to think about alignment, endianness */
if (path[len - 4] == G_DIR_SEPARATOR && if (path[len - 4] == G_DIR_SEPARATOR &&
path[len - 3] == 'c' && path[len - 3] == 'c' &&
path[len - 2] == 'u' && path[len - 2] == 'u' &&
path[len - 1] == 'r') path[len - 1] == 'r')
return TRUE; return TRUE;
if (path[len - 4] == G_DIR_SEPARATOR && if (path[len - 4] == G_DIR_SEPARATOR &&
path[len - 3] == 'n' && path[len - 3] == 'n' &&
path[len - 2] == 'e' && path[len - 2] == 'e' &&
path[len - 1] == 'w') path[len - 1] == 'w')
@ -304,7 +304,7 @@ has_noindex_file (const char *path)
else if (G_UNLIKELY(errno != ENOENT)) else if (G_UNLIKELY(errno != ENOENT))
g_warning ("error testing for noindex file %s: %s", g_warning ("error testing for noindex file %s: %s",
noindexpath, strerror(errno)); noindexpath, strerror(errno));
return FALSE; return FALSE;
} }
@ -318,7 +318,7 @@ is_dotdir_to_ignore (const char* dir)
".nnmaildir", ".nnmaildir",
".#evolution" ".#evolution"
}; /* when adding names, check the optimization below */ }; /* when adding names, check the optimization below */
if (dir[0] != '.') if (dir[0] != '.')
return FALSE; /* not a dotdir */ return FALSE; /* not a dotdir */
@ -339,16 +339,12 @@ is_dotdir_to_ignore (const char* dir)
static gboolean static gboolean
ignore_dir_entry (struct dirent *entry, unsigned char d_type) ignore_dir_entry (struct dirent *entry, unsigned char d_type)
{ {
const char *name;
/* if it's not a dir and not a file, ignore it. /* if it's not a dir and not a file, ignore it.
* note, this means also symlinks (DT_LNK) are ignored, * note, this means also symlinks (DT_LNK) are ignored,
* maybe make this optional */ * maybe make this optional */
if (G_UNLIKELY(d_type != DT_REG && d_type != DT_DIR)) if (G_UNLIKELY(d_type != DT_REG && d_type != DT_DIR))
return TRUE; return TRUE;
name = entry->d_name;
/* ignore '.' and '..' dirs, as well as .notmuch and /* ignore '.' and '..' dirs, as well as .notmuch and
* .nnmaildir */ * .nnmaildir */
@ -371,13 +367,13 @@ get_mdir_for_path (const gchar *old_mdir, const gchar *dir)
static MuError static MuError
process_dir_entry (const char* path, const char* mdir, struct dirent *entry, process_dir_entry (const char* path, const char* mdir, struct dirent *entry,
MuMaildirWalkMsgCallback cb_msg, MuMaildirWalkMsgCallback cb_msg,
MuMaildirWalkDirCallback cb_dir, MuMaildirWalkDirCallback cb_dir,
void *data) void *data)
{ {
const char *fp; const char *fp;
char* fullpath; char* fullpath;
unsigned char d_type; unsigned char d_type;
/* we have to copy the buffer from fullpath_s, because it /* we have to copy the buffer from fullpath_s, because it
* returns a static buffer, and we maybe called reentrantly */ * returns a static buffer, and we maybe called reentrantly */
fp = mu_str_fullpath_s (path, entry->d_name); fp = mu_str_fullpath_s (path, entry->d_name);
@ -385,18 +381,18 @@ process_dir_entry (const char* path, const char* mdir, struct dirent *entry,
strcpy (fullpath, fp); strcpy (fullpath, fp);
d_type = GET_DTYPE(entry, fullpath); d_type = GET_DTYPE(entry, fullpath);
/* ignore special files/dirs */ /* ignore special files/dirs */
if (ignore_dir_entry (entry, d_type)) if (ignore_dir_entry (entry, d_type))
return MU_OK; return MU_OK;
switch (d_type) { switch (d_type) {
case DT_REG: /* we only want files in cur/ and new/ */ case DT_REG: /* we only want files in cur/ and new/ */
if (!is_maildir_new_or_cur (path)) if (!is_maildir_new_or_cur (path))
return MU_OK; return MU_OK;
return process_file (fullpath, mdir, cb_msg, data); return process_file (fullpath, mdir, cb_msg, data);
case DT_DIR: { case DT_DIR: {
char *my_mdir; char *my_mdir;
MuError rv; MuError rv;
@ -407,14 +403,14 @@ process_dir_entry (const char* path, const char* mdir, struct dirent *entry,
return rv; return rv;
} }
default: default:
return MU_OK; /* ignore other types */ return MU_OK; /* ignore other types */
} }
} }
static struct dirent* static struct dirent*
dirent_copy (struct dirent *entry) dirent_copy (struct dirent *entry)
{ {
struct dirent *d; struct dirent *d;
@ -432,7 +428,7 @@ dirent_destroy (struct dirent *entry)
g_slice_free (struct dirent, entry); g_slice_free (struct dirent, entry);
} }
#ifdef HAVE_STRUCT_DIRENT_D_INO #ifdef HAVE_STRUCT_DIRENT_D_INO
static int static int
dirent_cmp (struct dirent *d1, struct dirent *d2) dirent_cmp (struct dirent *d1, struct dirent *d2)
{ {
@ -456,20 +452,20 @@ process_dir_entries (DIR *dir, const char* path, const char* mdir,
MuError result; MuError result;
GSList *lst, *c; GSList *lst, *c;
struct dirent *entry; struct dirent *entry;
lst = NULL; lst = NULL;
while ((entry = readdir (dir))) while ((entry = readdir (dir)))
lst = g_slist_prepend (lst, dirent_copy(entry)); lst = g_slist_prepend (lst, dirent_copy(entry));
/* we sort by inode; this makes things much faster on /* we sort by inode; this makes things much faster on
* extfs2,3 */ * extfs2,3 */
#if HAVE_STRUCT_DIRENT_D_INO #if HAVE_STRUCT_DIRENT_D_INO
c = lst = g_slist_sort (lst, (GCompareFunc)dirent_cmp); c = lst = g_slist_sort (lst, (GCompareFunc)dirent_cmp);
#endif /*HAVE_STRUCT_DIRENT_D_INO*/ #endif /*HAVE_STRUCT_DIRENT_D_INO*/
for (c = lst, result = MU_OK; c && result == MU_OK; c = g_slist_next(c)) { for (c = lst, result = MU_OK; c && result == MU_OK; c = g_slist_next(c)) {
result = process_dir_entry (path, mdir, result = process_dir_entry (path, mdir,
(struct dirent*)c->data, (struct dirent*)c->data,
msg_cb, dir_cb, data); msg_cb, dir_cb, data);
/* hmmm, break on MU_ERROR as well? */ /* hmmm, break on MU_ERROR as well? */
if (result == MU_STOP) if (result == MU_STOP)
@ -478,32 +474,32 @@ process_dir_entries (DIR *dir, const char* path, const char* mdir,
g_slist_foreach (lst, (GFunc)dirent_destroy, NULL); g_slist_foreach (lst, (GFunc)dirent_destroy, NULL);
g_slist_free (lst); g_slist_free (lst);
return result; return result;
} }
static MuError static MuError
process_dir (const char* path, const char* mdir, process_dir (const char* path, const char* mdir,
MuMaildirWalkMsgCallback msg_cb, MuMaildirWalkMsgCallback msg_cb,
MuMaildirWalkDirCallback dir_cb, void *data) MuMaildirWalkDirCallback dir_cb, void *data)
{ {
MuError result; MuError result;
DIR* dir; DIR* dir;
/* if it has a noindex file, we ignore this dir */ /* if it has a noindex file, we ignore this dir */
if (has_noindex_file (path)) { if (has_noindex_file (path)) {
g_debug ("found .noindex: ignoring dir %s", path); g_debug ("found .noindex: ignoring dir %s", path);
return MU_OK; return MU_OK;
} }
dir = opendir (path); dir = opendir (path);
if (G_UNLIKELY(!dir)) { if (G_UNLIKELY(!dir)) {
g_warning ("%s: ignoring %s: %s", __FUNCTION__, g_warning ("%s: ignoring %s: %s", __FUNCTION__,
path, strerror(errno)); path, strerror(errno));
return MU_OK; return MU_OK;
} }
if (dir_cb) { if (dir_cb) {
MuError rv; MuError rv;
rv = dir_cb (path, TRUE, data); rv = dir_cb (path, TRUE, data);
@ -512,36 +508,36 @@ process_dir (const char* path, const char* mdir,
return rv; return rv;
} }
} }
result = process_dir_entries (dir, path, mdir, msg_cb, dir_cb, data); result = process_dir_entries (dir, path, mdir, msg_cb, dir_cb, data);
closedir (dir); closedir (dir);
/* only run dir_cb if it exists and so far, things went ok */ /* only run dir_cb if it exists and so far, things went ok */
if (dir_cb && result == MU_OK) if (dir_cb && result == MU_OK)
return dir_cb (path, FALSE, data); return dir_cb (path, FALSE, data);
return result; return result;
} }
MuError MuError
mu_maildir_walk (const char *path, MuMaildirWalkMsgCallback cb_msg, mu_maildir_walk (const char *path, MuMaildirWalkMsgCallback cb_msg,
MuMaildirWalkDirCallback cb_dir, void *data) MuMaildirWalkDirCallback cb_dir, void *data)
{ {
MuError rv; MuError rv;
char *mypath; char *mypath;
g_return_val_if_fail (path && cb_msg, MU_ERROR); g_return_val_if_fail (path && cb_msg, MU_ERROR);
g_return_val_if_fail (mu_util_check_dir(path, TRUE, FALSE), MU_ERROR); g_return_val_if_fail (mu_util_check_dir(path, TRUE, FALSE), MU_ERROR);
/* strip the final / or \ */ /* strip the final / or \ */
mypath = g_strdup (path); mypath = g_strdup (path);
if (mypath[strlen(mypath)-1] == G_DIR_SEPARATOR) if (mypath[strlen(mypath)-1] == G_DIR_SEPARATOR)
mypath[strlen(mypath)-1] = '\0'; mypath[strlen(mypath)-1] = '\0';
rv = process_dir (mypath, NULL, cb_msg, cb_dir, data); rv = process_dir (mypath, NULL, cb_msg, cb_dir, data);
g_free (mypath); g_free (mypath);
return rv; return rv;
} }
@ -551,32 +547,32 @@ clear_links (const gchar* dirname, DIR *dir, GError **err)
{ {
struct dirent *entry; struct dirent *entry;
gboolean rv; gboolean rv;
rv = TRUE; rv = TRUE;
errno = 0; errno = 0;
while ((entry = readdir (dir))) { while ((entry = readdir (dir))) {
const char *fp; const char *fp;
char *fullpath; char *fullpath;
unsigned char d_type; unsigned char d_type;
/* ignore empty, dot thingies */ /* ignore empty, dot thingies */
if (!entry->d_name || entry->d_name[0] == '.') if (!entry->d_name || entry->d_name[0] == '.')
continue; continue;
/* we have to copy the buffer from fullpath_s, because /* we have to copy the buffer from fullpath_s, because
* it returns a static buffer and we are * it returns a static buffer and we are
* recursive*/ * recursive*/
fp = mu_str_fullpath_s (dirname, entry->d_name); fp = mu_str_fullpath_s (dirname, entry->d_name);
fullpath = g_newa (char, strlen(fp) + 1); fullpath = g_newa (char, strlen(fp) + 1);
strcpy (fullpath, fp); strcpy (fullpath, fp);
d_type = GET_DTYPE (entry, fullpath); d_type = GET_DTYPE (entry, fullpath);
/* ignore non-links / non-dirs */ /* ignore non-links / non-dirs */
if (d_type != DT_LNK && d_type != DT_DIR) if (d_type != DT_LNK && d_type != DT_DIR)
continue; continue;
if (d_type == DT_LNK) { if (d_type == DT_LNK) {
if (unlink (fullpath) != 0) { if (unlink (fullpath) != 0) {
/* don't use err */ /* don't use err */
@ -591,7 +587,7 @@ clear_links (const gchar* dirname, DIR *dir, GError **err)
if (errno != 0) if (errno != 0)
g_set_error (err, 0, MU_ERROR_FILE, g_set_error (err, 0, MU_ERROR_FILE,
"file error: %s", strerror(errno)); "file error: %s", strerror(errno));
return (rv == FALSE && errno == 0); return (rv == FALSE && errno == 0);
} }
@ -601,10 +597,10 @@ mu_maildir_clear_links (const gchar* path, GError **err)
{ {
DIR *dir; DIR *dir;
gboolean rv; gboolean rv;
g_return_val_if_fail (path, FALSE); g_return_val_if_fail (path, FALSE);
dir = opendir (path); dir = opendir (path);
if (!dir) { if (!dir) {
g_set_error (err, 0, MU_ERROR_FILE_CANNOT_OPEN, g_set_error (err, 0, MU_ERROR_FILE_CANNOT_OPEN,
"failed to open %s: %s", path, "failed to open %s: %s", path,
@ -622,7 +618,7 @@ MuFlags
mu_maildir_get_flags_from_path (const char *path) mu_maildir_get_flags_from_path (const char *path)
{ {
g_return_val_if_fail (path, MU_FLAG_INVALID); g_return_val_if_fail (path, MU_FLAG_INVALID);
/* try to find the info part */ /* try to find the info part */
/* note that we can use either the ':' or '!' as separator; /* note that we can use either the ':' or '!' as separator;
* the former is the official, but as it does not work on e.g. VFAT * the former is the official, but as it does not work on e.g. VFAT
@ -631,19 +627,19 @@ mu_maildir_get_flags_from_path (const char *path)
* documentation at http://docs.python.org/lib/mailbox-maildir.html * documentation at http://docs.python.org/lib/mailbox-maildir.html
* mentions the '!' as well as a 'popular choice' * mentions the '!' as well as a 'popular choice'
*/ */
/* we check the dir -- */ /* we check the dir -- */
if (strstr (path, G_DIR_SEPARATOR_S "new" G_DIR_SEPARATOR_S)) { if (strstr (path, G_DIR_SEPARATOR_S "new" G_DIR_SEPARATOR_S)) {
char *dir, *dir2; char *dir, *dir2;
MuFlags flags; MuFlags flags;
dir = g_path_get_dirname (path); dir = g_path_get_dirname (path);
dir2 = g_path_get_basename (dir); dir2 = g_path_get_basename (dir);
if (g_strcmp0 (dir2, "new") == 0) if (g_strcmp0 (dir2, "new") == 0)
flags = MU_FLAG_NEW; flags = MU_FLAG_NEW;
g_free (dir); g_free (dir);
g_free (dir2); g_free (dir2);
@ -664,7 +660,7 @@ mu_maildir_get_flags_from_path (const char *path)
(info[-1] != ':' && info[-1] != '!') || (info[-1] != ':' && info[-1] != '!') ||
(info[1] != ',')) (info[1] != ','))
return MU_FLAG_NONE; return MU_FLAG_NONE;
else else
return mu_flags_from_str (&info[2], return mu_flags_from_str (&info[2],
MU_FLAG_TYPE_MAILFILE); MU_FLAG_TYPE_MAILFILE);
} }
@ -677,16 +673,16 @@ mu_maildir_get_flags_from_path (const char *path)
* *
* /home/user/Maildir/foo/bar/cur/abc:2,F and flags == MU_FLAG_NEW * /home/user/Maildir/foo/bar/cur/abc:2,F and flags == MU_FLAG_NEW
* => /home/user/Maildir/foo/bar/new * => /home/user/Maildir/foo/bar/new
* and * and
* /home/user/Maildir/foo/bar/new/abc and flags == MU_FLAG_REPLIED * /home/user/Maildir/foo/bar/new/abc and flags == MU_FLAG_REPLIED
* => /home/user/Maildir/foo/bar/cur * => /home/user/Maildir/foo/bar/cur
* *
* so only difference is whether MuFlags matches MU_FLAG_NEW is set or not * so only difference is whether MuFlags matches MU_FLAG_NEW is set or not
* *
*/ */
static gchar* static gchar*
get_new_path (const char *mdir, const char *mfile, MuFlags flags) get_new_path (const char *mdir, const char *mfile, MuFlags flags)
{ {
if (flags & MU_FLAG_NEW) if (flags & MU_FLAG_NEW)
return g_strdup_printf ("%s%cnew%c%s", return g_strdup_printf ("%s%cnew%c%s",
mdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR, mdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR,
@ -694,7 +690,7 @@ get_new_path (const char *mdir, const char *mfile, MuFlags flags)
else { else {
const char *flagstr; const char *flagstr;
flagstr = mu_flags_to_str_s (flags, MU_FLAG_TYPE_MAILFILE); flagstr = mu_flags_to_str_s (flags, MU_FLAG_TYPE_MAILFILE);
return g_strdup_printf ("%s%ccur%c%s:2,%s", return g_strdup_printf ("%s%ccur%c%s:2,%s",
mdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR, mdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR,
mfile, flagstr); mfile, flagstr);
@ -702,6 +698,28 @@ get_new_path (const char *mdir, const char *mfile, MuFlags flags)
} }
char*
mu_maildir_get_maildir_from_path (const char* path)
{
gchar *mdir;
/* determine the maildir */
mdir = g_path_get_dirname (path);
if (!g_str_has_suffix (mdir, "cur") && !g_str_has_suffix (mdir, "new")) {
g_warning ("%s: not a valid maildir path: %s",
__FUNCTION__, path);
g_free (mdir);
return NULL;
}
/* remove the 'cur' or 'new' */
mdir[strlen(mdir) - 4] = '\0';
return mdir;
}
char* char*
mu_maildir_get_new_path (const char *oldpath, const char *new_mdir, mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
MuFlags newflags) MuFlags newflags)
@ -711,18 +729,12 @@ mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
g_return_val_if_fail (oldpath, NULL); g_return_val_if_fail (oldpath, NULL);
mfile = newpath = NULL; mfile = newpath = NULL;
/* determine the maildir */
mdir = g_path_get_dirname (oldpath);
if (!g_str_has_suffix (mdir, "cur") && !g_str_has_suffix (mdir, "new")) {
g_warning ("%s: not a valid maildir path: %s",
__FUNCTION__, oldpath);
goto leave;
}
/* remove the 'cur' or 'new' */ /* determine the maildir */
mdir[strlen(mdir) - 4] = '\0'; mdir = mu_maildir_get_maildir_from_path (oldpath);
if (!mdir)
return NULL;
/* determine the name of the mailfile, stripped of its flags */ /* determine the name of the mailfile, stripped of its flags */
mfile = g_path_get_basename (oldpath); mfile = g_path_get_basename (oldpath);
for (cur = &mfile[strlen(mfile)-1]; cur > mfile; --cur) { for (cur = &mfile[strlen(mfile)-1]; cur > mfile; --cur) {
@ -731,12 +743,12 @@ mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
cur[0] = '\0'; /* strip the flags */ cur[0] = '\0'; /* strip the flags */
break; break;
} }
} }
newpath = get_new_path (new_mdir ? new_mdir : mdir, newpath = get_new_path (new_mdir ? new_mdir : mdir,
mfile, newflags); mfile, newflags);
leave: g_free (mfile); g_free (mfile);
g_free (mdir); g_free (mdir);
return newpath; return newpath;
@ -750,7 +762,7 @@ msg_move_check_pre (const gchar *src, const gchar *dst, GError **err)
"source is not an absolute path: '%s'", src); "source is not an absolute path: '%s'", src);
return FALSE; return FALSE;
} }
if (!g_path_is_absolute(dst)) { if (!g_path_is_absolute(dst)) {
g_set_error (err, 0, MU_ERROR_FILE, g_set_error (err, 0, MU_ERROR_FILE,
"target is not an absolute path: '%s'", dst); "target is not an absolute path: '%s'", dst);
@ -762,7 +774,7 @@ msg_move_check_pre (const gchar *src, const gchar *dst, GError **err)
src); src);
return FALSE; return FALSE;
} }
if (access (dst, F_OK) == 0) { if (access (dst, F_OK) == 0) {
g_set_error (err, 0, MU_ERROR_FILE, "%s already exists", g_set_error (err, 0, MU_ERROR_FILE, "%s already exists",
dst); dst);
@ -781,7 +793,7 @@ msg_move_check_post (const char *src, const char *dst, GError **err)
dst); dst);
return FALSE; return FALSE;
} }
if (access (src, F_OK) == 0) { if (access (src, F_OK) == 0) {
g_set_error (err, 0, MU_ERROR_FILE, "source is still there (%s)", g_set_error (err, 0, MU_ERROR_FILE, "source is still there (%s)",
src); src);
@ -797,7 +809,7 @@ msg_move (const char* src, const char *dst, GError **err)
{ {
if (!msg_move_check_pre (src, dst, err)) if (!msg_move_check_pre (src, dst, err))
return FALSE; return FALSE;
if (rename (src, dst) != 0) { if (rename (src, dst) != 0) {
g_set_error (err, 0, MU_ERROR_FILE, "error moving %s to %s", g_set_error (err, 0, MU_ERROR_FILE, "error moving %s to %s",
src, dst); src, dst);
@ -806,7 +818,7 @@ msg_move (const char* src, const char *dst, GError **err)
if (!msg_move_check_post (src, dst, err)) if (!msg_move_check_post (src, dst, err))
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
@ -818,9 +830,9 @@ mu_maildir_move_message (const char* oldpath, const char* targetmdir,
char *newfullpath; char *newfullpath;
gboolean rv; gboolean rv;
gboolean src_is_target; gboolean src_is_target;
g_return_val_if_fail (oldpath, FALSE); g_return_val_if_fail (oldpath, FALSE);
newfullpath = mu_maildir_get_new_path (oldpath, targetmdir, newfullpath = mu_maildir_get_new_path (oldpath, targetmdir,
newflags); newflags);
if (!newfullpath) { if (!newfullpath) {
@ -831,7 +843,7 @@ mu_maildir_move_message (const char* oldpath, const char* targetmdir,
src_is_target = (g_strcmp0 (oldpath, newfullpath) == 0); src_is_target = (g_strcmp0 (oldpath, newfullpath) == 0);
if (!ignore_dups && src_is_target) { if (!ignore_dups && src_is_target) {
g_set_error (err, 0, MU_ERROR_FILE_TARGET_EQUALS_SOURCE, g_set_error (err, 0, MU_ERROR_FILE_TARGET_EQUALS_SOURCE,
"target equals source"); "target equals source");
@ -845,7 +857,7 @@ mu_maildir_move_message (const char* oldpath, const char* targetmdir,
return NULL; return NULL;
} }
} }
return newfullpath; return newfullpath;
} }

View File

@ -1,4 +1,4 @@
/* /*
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** This program is free software; you can redistribute it and/or modify it
@ -13,8 +13,8 @@
** **
** You should have received a copy of the GNU General Public License ** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, ** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
** **
*/ */
#ifndef __MU_MAILDIR_H__ #ifndef __MU_MAILDIR_H__
@ -23,7 +23,7 @@
#include <glib.h> #include <glib.h>
#include <time.h> #include <time.h>
#include <sys/types.h> /* for mode_t */ #include <sys/types.h> /* for mode_t */
#include <mu-util.h> #include <mu-util.h>
#include <mu-flags.h> #include <mu-flags.h>
@ -33,15 +33,15 @@ G_BEGIN_DECLS
* create a new maildir. Note, if the function fails 'halfway', it * create a new maildir. Note, if the function fails 'halfway', it
* will *not* try to remove the parts the were created. it *will* * will *not* try to remove the parts the were created. it *will*
* create any parent dirs that are not yet existant. * create any parent dirs that are not yet existant.
* *
* *
* @param path the path (missing components will be created, as in 'mkdir -p') * @param path the path (missing components will be created, as in 'mkdir -p')
* @param mode the file mode (e.g., 0755) * @param mode the file mode (e.g., 0755)
* @param noindex add a .noindex file to the maildir, so it will be excluded * @param noindex add a .noindex file to the maildir, so it will be excluded
* from indexing by 'mu index' * from indexing by 'mu index'
* @param err if function returns FALSE, err may contain extra * @param err if function returns FALSE, err may contain extra
* information. if err is NULL, does nothing * information. if err is NULL, does nothing
* *
* @return TRUE if creation succeeded, FALSE otherwise * @return TRUE if creation succeeded, FALSE otherwise
*/ */
gboolean mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex, gboolean mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex,
@ -50,15 +50,15 @@ gboolean mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex,
/** /**
* create a symbolic link to a mail message * create a symbolic link to a mail message
* *
* @param src the full path to the source message * @param src the full path to the source message
* @param targetpath the path to the target maildir; ie., *not* * @param targetpath the path to the target maildir; ie., *not*
* MyMaildir/cur, but just MyMaildir/. The function will figure out * MyMaildir/cur, but just MyMaildir/. The function will figure out
* the correct subdir then. * the correct subdir then.
* @param err if function returns FALSE, err may contain extra * @param err if function returns FALSE, err may contain extra
* information. if err is NULL, does nothing * information. if err is NULL, does nothing
* *
* @return * @return
*/ */
gboolean mu_maildir_link (const char* src, const char *targetpath, GError **err); gboolean mu_maildir_link (const char* src, const char *targetpath, GError **err);
@ -77,7 +77,7 @@ typedef MuError (*MuMaildirWalkMsgCallback)
/** /**
* MuPathWalkDirCallback -- callback function for mu_path_walk_maildir; see the * MuPathWalkDirCallback -- callback function for mu_path_walk_maildir; see the
* documentation there. It will be called each time a dir is entered or left, * documentation there. It will be called each time a dir is entered or left,
* with 'enter' being TRUE upon entering, FALSE otherwise * with 'enter' being TRUE upon entering, FALSE otherwise
*/ */
typedef MuError (*MuMaildirWalkDirCallback) typedef MuError (*MuMaildirWalkDirCallback)
(const char* fullpath, gboolean enter, void *user_data); (const char* fullpath, gboolean enter, void *user_data);
@ -96,25 +96,25 @@ typedef MuError (*MuMaildirWalkDirCallback)
* mu_walk_maildir stops if the callbacks return something different * mu_walk_maildir stops if the callbacks return something different
* from MU_OK. For example, it can return MU_STOP to stop the scan, or * from MU_OK. For example, it can return MU_STOP to stop the scan, or
* some error. * some error.
* *
* @param path the maildir path to scan * @param path the maildir path to scan
* @param cb_msg the callback function called for each msg * @param cb_msg the callback function called for each msg
* @param cb_dir the callback function called for each dir * @param cb_dir the callback function called for each dir
* @param data user data pointer * @param data user data pointer
* *
* @return a scanner result; MU_OK if everything went ok, * @return a scanner result; MU_OK if everything went ok,
* MU_STOP if we want to stop, or MU_ERROR in * MU_STOP if we want to stop, or MU_ERROR in
* case of error * case of error
*/ */
MuError mu_maildir_walk (const char *path, MuMaildirWalkMsgCallback cb_msg, MuError mu_maildir_walk (const char *path, MuMaildirWalkMsgCallback cb_msg,
MuMaildirWalkDirCallback cb_dir, void *data); MuMaildirWalkDirCallback cb_dir, void *data);
/** /**
* recursively delete all the symbolic links in a directory tree * recursively delete all the symbolic links in a directory tree
* *
* @param dir top dir * @param dir top dir
* @param err if function returns FALSE, err may contain extra * @param err if function returns FALSE, err may contain extra
* information. if err is NULL, does nothing * information. if err is NULL, does nothing
* *
* @return TRUE if it worked, FALSE in case of error * @return TRUE if it worked, FALSE in case of error
*/ */
gboolean mu_maildir_clear_links (const gchar* dir, GError **err); gboolean mu_maildir_clear_links (const gchar* dir, GError **err);
@ -129,7 +129,7 @@ gboolean mu_maildir_clear_links (const gchar* dir, GError **err);
* *
* @param pathname of a mailfile; it does not have to refer to an * @param pathname of a mailfile; it does not have to refer to an
* actual message * actual message
* *
* @return the flags, or MU_MSG_FILE_FLAG_UNKNOWN in case of error * @return the flags, or MU_MSG_FILE_FLAG_UNKNOWN in case of error
*/ */
MuFlags mu_maildir_get_flags_from_path (const char* pathname); MuFlags mu_maildir_get_flags_from_path (const char* pathname);
@ -144,20 +144,31 @@ MuFlags mu_maildir_get_flags_from_path (const char* pathname);
* OR'ed. Note that the file does not have to exist; the flags are * OR'ed. Note that the file does not have to exist; the flags are
* based on the path only. * based on the path only.
* *
* *
* @param oldpath the old (current) full path to the message * @param oldpath the old (current) full path to the message
* (including the filename) * (including the filename)
* @param new_mdir the new maildir for this message, or NULL to keep * @param new_mdir the new maildir for this message, or NULL to keep
* it in the current one. The maildir is the absolute file system * it in the current one. The maildir is the absolute file system
* path, without the 'cur' or 'new' * path, without the 'cur' or 'new'
* @param new_flags the new flags for this message * @param new_flags the new flags for this message
* *
* @return a new path name; use g_free when done with. NULL in case of * @return a new path name; use g_free when done with. NULL in case of
* error. * error.
*/ */
char* mu_maildir_get_new_path (const char *oldpath, const char *new_mdir, char* mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
MuFlags new_flags); MuFlags new_flags);
/**
* get the maildir for a certain message path, ie, the path *before*
* cur/ or new/
*
* @param path path for some message
*
* @return the maildir (free with g_free), or NULL in case of error
*/
char* mu_maildir_get_maildir_from_path (const char* path);
/** /**
* move a message file to another maildir; the function returns the full * move a message file to another maildir; the function returns the full
* path to the new message. * path to the new message.
@ -175,7 +186,7 @@ char* mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
* @param err (may be NULL) may contain error information; note if the * @param err (may be NULL) may contain error information; note if the
* function return FALSE, err is not set for all error condition * function return FALSE, err is not set for all error condition
* (ie. not for parameter errors) * (ie. not for parameter errors)
* *
* @return return the full path name of the target file (g_free) if * @return return the full path name of the target file (g_free) if
* the move succeeded, NULL otherwise * the move succeeded, NULL otherwise
*/ */

View File

@ -1,4 +1,4 @@
/* /*
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** This program is free software; you can redistribute it and/or modify it
@ -13,8 +13,8 @@
** **
** You should have received a copy of the GNU General Public License ** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, ** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
** **
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -37,17 +37,17 @@ test_mu_maildir_mkmdir_01 (void)
int i; int i;
gchar *tmpdir, *mdir, *tmp; gchar *tmpdir, *mdir, *tmp;
const gchar *subs[] = {"tmp", "cur", "new"}; const gchar *subs[] = {"tmp", "cur", "new"};
tmpdir = test_mu_common_get_random_tmpdir (); tmpdir = test_mu_common_get_random_tmpdir ();
mdir = g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR, mdir = g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR,
"cuux"); "cuux");
g_assert_cmpuint (mu_maildir_mkdir (mdir, 0755, FALSE, NULL), g_assert_cmpuint (mu_maildir_mkdir (mdir, 0755, FALSE, NULL),
==, TRUE); ==, TRUE);
for (i = 0; i != G_N_ELEMENTS(subs); ++i) { for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
gchar* dir; gchar* dir;
dir = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, dir = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR,
subs[i]); subs[i]);
g_assert_cmpuint (g_access (dir, R_OK), ==, 0); g_assert_cmpuint (g_access (dir, R_OK), ==, 0);
@ -57,11 +57,11 @@ test_mu_maildir_mkmdir_01 (void)
tmp = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex"); tmp = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex");
g_assert_cmpuint (g_access (tmp, F_OK), !=, 0); g_assert_cmpuint (g_access (tmp, F_OK), !=, 0);
g_free (tmp); g_free (tmp);
g_free (tmpdir); g_free (tmpdir);
g_free (mdir); g_free (mdir);
} }
@ -71,17 +71,17 @@ test_mu_maildir_mkmdir_02 (void)
int i; int i;
gchar *tmpdir, *mdir, *tmp; gchar *tmpdir, *mdir, *tmp;
const gchar *subs[] = {"tmp", "cur", "new"}; const gchar *subs[] = {"tmp", "cur", "new"};
tmpdir = test_mu_common_get_random_tmpdir (); tmpdir = test_mu_common_get_random_tmpdir ();
mdir = g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR, mdir = g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR,
"cuux"); "cuux");
g_assert_cmpuint (mu_maildir_mkdir (mdir, 0755, TRUE, NULL), g_assert_cmpuint (mu_maildir_mkdir (mdir, 0755, TRUE, NULL),
==, TRUE); ==, TRUE);
for (i = 0; i != G_N_ELEMENTS(subs); ++i) { for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
gchar* dir; gchar* dir;
dir = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, dir = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR,
subs[i]); subs[i]);
g_assert_cmpuint (g_access (dir, R_OK), ==, 0); g_assert_cmpuint (g_access (dir, R_OK), ==, 0);
@ -89,13 +89,13 @@ test_mu_maildir_mkmdir_02 (void)
g_assert_cmpuint (g_access (dir, W_OK), ==, 0); g_assert_cmpuint (g_access (dir, W_OK), ==, 0);
g_free (dir); g_free (dir);
} }
tmp = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex"); tmp = g_strdup_printf ("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex");
g_assert_cmpuint (g_access (tmp, F_OK), ==, 0); g_assert_cmpuint (g_access (tmp, F_OK), ==, 0);
g_free (tmp); g_free (tmp);
g_free (tmpdir); g_free (tmpdir);
g_free (mdir); g_free (mdir);
} }
@ -109,25 +109,25 @@ ignore_error (const char* log_domain, GLogLevelFlags log_level, const gchar* msg
static void static void
test_mu_maildir_mkmdir_03 (void) test_mu_maildir_mkmdir_03 (void)
{ {
/* this must fail */ /* this must fail */
g_test_log_set_fatal_handler ((GTestLogFatalFunc)ignore_error, NULL); g_test_log_set_fatal_handler ((GTestLogFatalFunc)ignore_error, NULL);
g_assert_cmpuint (mu_maildir_mkdir (NULL, 0755, TRUE, NULL), g_assert_cmpuint (mu_maildir_mkdir (NULL, 0755, TRUE, NULL),
==, FALSE); ==, FALSE);
} }
static gchar* static gchar*
copy_test_data (void) copy_test_data (void)
{ {
gchar *dir, *cmd; gchar *dir, *cmd;
dir = test_mu_common_get_random_tmpdir(); dir = test_mu_common_get_random_tmpdir();
cmd = g_strdup_printf ("mkdir %s", dir); cmd = g_strdup_printf ("mkdir %s", dir);
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL)); g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd); g_free (cmd);
cmd = g_strdup_printf ("cp -R %s %s", MU_TESTMAILDIR, dir); cmd = g_strdup_printf ("cp -R %s %s", MU_TESTMAILDIR, dir);
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL)); g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd); g_free (cmd);
@ -136,7 +136,7 @@ copy_test_data (void)
cmd = g_strdup_printf ("chmod -R 700 %s", dir); cmd = g_strdup_printf ("chmod -R 700 %s", dir);
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL)); g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd); g_free (cmd);
return dir; return dir;
} }
@ -150,7 +150,7 @@ typedef struct {
static MuError static MuError
dir_cb (const char *fullpath, gboolean enter, WalkData *data) dir_cb (const char *fullpath, gboolean enter, WalkData *data)
{ {
if (enter) if (enter)
++data->_dir_entered; ++data->_dir_entered;
else else
++data->_dir_left; ++data->_dir_left;
@ -177,19 +177,19 @@ test_mu_maildir_walk_01 (void)
char *tmpdir; char *tmpdir;
WalkData data; WalkData data;
MuError rv; MuError rv;
tmpdir = copy_test_data (); tmpdir = copy_test_data ();
memset (&data, 0, sizeof(WalkData)); memset (&data, 0, sizeof(WalkData));
/* g_print ("tmpdir: %s\n", tmpdir); */ /* g_print ("tmpdir: %s\n", tmpdir); */
rv = mu_maildir_walk (tmpdir, rv = mu_maildir_walk (tmpdir,
(MuMaildirWalkMsgCallback)msg_cb, (MuMaildirWalkMsgCallback)msg_cb,
(MuMaildirWalkDirCallback)dir_cb, (MuMaildirWalkDirCallback)dir_cb,
&data); &data);
g_assert_cmpuint (MU_OK, ==, rv); g_assert_cmpuint (MU_OK, ==, rv);
g_assert_cmpuint (data._file_count, ==, 13); g_assert_cmpuint (data._file_count, ==, 13);
g_assert_cmpuint (data._dir_entered,==, 5); g_assert_cmpuint (data._dir_entered,==, 5);
g_assert_cmpuint (data._dir_left,==, 5); g_assert_cmpuint (data._dir_left,==, 5);
@ -203,19 +203,19 @@ test_mu_maildir_walk_02 (void)
char *tmpdir, *cmd; char *tmpdir, *cmd;
WalkData data; WalkData data;
MuError rv; MuError rv;
tmpdir = copy_test_data (); tmpdir = copy_test_data ();
memset (&data, 0, sizeof(WalkData)); memset (&data, 0, sizeof(WalkData));
/* mark the 'new' dir with '.noindex', to ignore it */ /* mark the 'new' dir with '.noindex', to ignore it */
cmd = g_strdup_printf ("touch %s%ctestdir%cnew%c.noindex", tmpdir, cmd = g_strdup_printf ("touch %s%ctestdir%cnew%c.noindex", tmpdir,
G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR,
G_DIR_SEPARATOR); G_DIR_SEPARATOR);
g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL)); g_assert (g_spawn_command_line_sync (cmd, NULL, NULL, NULL, NULL));
g_free (cmd); g_free (cmd);
rv = mu_maildir_walk (tmpdir, rv = mu_maildir_walk (tmpdir,
(MuMaildirWalkMsgCallback)msg_cb, (MuMaildirWalkMsgCallback)msg_cb,
(MuMaildirWalkDirCallback)dir_cb, (MuMaildirWalkDirCallback)dir_cb,
&data); &data);
@ -262,7 +262,7 @@ test_mu_maildir_get_flags_from_path (void)
}; };
for (i = 0; i != G_N_ELEMENTS(paths); ++i) { for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
MuFlags flags; MuFlags flags;
flags = mu_maildir_get_flags_from_path(paths[i].path); flags = mu_maildir_get_flags_from_path(paths[i].path);
g_assert_cmpuint(flags, ==, paths[i].flags); g_assert_cmpuint(flags, ==, paths[i].flags);
} }
@ -293,7 +293,7 @@ test_mu_maildir_get_new_path_01 (void)
}, { }, {
"/home/foo/Maildir/test/new/1313038887_0.697:2,", "/home/foo/Maildir/test/new/1313038887_0.697:2,",
MU_FLAG_SEEN | MU_FLAG_FLAGGED | MU_FLAG_PASSED, MU_FLAG_SEEN | MU_FLAG_FLAGGED | MU_FLAG_PASSED,
"/home/foo/Maildir/test/cur/1313038887_0.697:2,FPS" "/home/foo/Maildir/test/cur/1313038887_0.697:2,FPS"
}, { }, {
"/home/djcb/Maildir/trash/new/1312920597.2206_16.cthulhu", "/home/djcb/Maildir/trash/new/1312920597.2206_16.cthulhu",
MU_FLAG_SEEN, MU_FLAG_SEEN,
@ -354,6 +354,31 @@ test_mu_maildir_get_new_path_02 (void)
} }
static void
test_mu_maildir_get_maildir_from_path (void)
{
unsigned u;
struct {
const char *path, *exp;
} cases[] = {
{"/home/foo/Maildir/test/cur/123456:2,FR",
"/home/foo/Maildir/test"},
{"/home/foo/Maildir/lala/new/1313038887_0.697:2,",
"/home/foo/Maildir/lala"}
};
for (u = 0; u != G_N_ELEMENTS(cases); ++u) {
gchar *mdir;
mdir = mu_maildir_get_maildir_from_path (cases[u].path);
g_assert_cmpstr(mdir,==,cases[u].exp);
g_free (mdir);
}
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -380,10 +405,14 @@ main (int argc, char *argv[])
test_mu_maildir_get_new_path_02); test_mu_maildir_get_new_path_02);
g_test_add_func("/mu-maildir/mu-maildir-get-flags-from-path", g_test_add_func("/mu-maildir/mu-maildir-get-flags-from-path",
test_mu_maildir_get_flags_from_path); test_mu_maildir_get_flags_from_path);
g_test_add_func("/mu-maildir/mu-maildir-get-maildir-from-path",
test_mu_maildir_get_maildir_from_path);
g_log_set_handler (NULL, g_log_set_handler (NULL,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,
(GLogFunc)black_hole, NULL); (GLogFunc)black_hole, NULL);
return g_test_run (); return g_test_run ();
} }