* 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

@ -339,16 +339,12 @@ is_dotdir_to_ignore (const char* dir)
static gboolean
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.
* note, this means also symlinks (DT_LNK) are ignored,
* maybe make this optional */
if (G_UNLIKELY(d_type != DT_REG && d_type != DT_DIR))
return TRUE;
name = entry->d_name;
/* ignore '.' and '..' dirs, as well as .notmuch and
* .nnmaildir */
@ -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*
mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
MuFlags newflags)
@ -713,15 +731,9 @@ mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
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' */
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 */
mfile = g_path_get_basename (oldpath);
@ -736,7 +748,7 @@ mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
newpath = get_new_path (new_mdir ? new_mdir : mdir,
mfile, newflags);
leave: g_free (mfile);
g_free (mfile);
g_free (mdir);
return newpath;

View File

@ -158,6 +158,17 @@ MuFlags mu_maildir_get_flags_from_path (const char* pathname);
char* mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
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
* path to the new message.

View File

@ -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
main (int argc, char *argv[])
{
@ -381,6 +406,10 @@ main (int argc, char *argv[])
g_test_add_func("/mu-maildir/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_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,
(GLogFunc)black_hole, NULL);