* mu-maildir: make public mu_maildir_get_maildir_from_path, add tests
This commit is contained in:
@ -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 */
|
||||||
|
|
||||||
@ -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)
|
||||||
@ -713,15 +731,9 @@ mu_maildir_get_new_path (const char *oldpath, const char *new_mdir,
|
|||||||
mfile = newpath = NULL;
|
mfile = newpath = NULL;
|
||||||
|
|
||||||
/* determine the maildir */
|
/* determine the maildir */
|
||||||
mdir = g_path_get_dirname (oldpath);
|
mdir = mu_maildir_get_maildir_from_path (oldpath);
|
||||||
if (!g_str_has_suffix (mdir, "cur") && !g_str_has_suffix (mdir, "new")) {
|
if (!mdir)
|
||||||
g_warning ("%s: not a valid maildir path: %s",
|
return NULL;
|
||||||
__FUNCTION__, oldpath);
|
|
||||||
goto leave;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* remove the 'cur' or 'new' */
|
|
||||||
mdir[strlen(mdir) - 4] = '\0';
|
|
||||||
|
|
||||||
/* 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);
|
||||||
@ -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,
|
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;
|
||||||
|
|||||||
@ -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,
|
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.
|
||||||
|
|||||||
@ -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[])
|
||||||
{
|
{
|
||||||
@ -381,6 +406,10 @@ main (int argc, char *argv[])
|
|||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user