* clean-up mu_util_dir_expand, add unit test
This commit is contained in:
@ -45,29 +45,28 @@
|
|||||||
|
|
||||||
#include "mu-util.h"
|
#include "mu-util.h"
|
||||||
|
|
||||||
char*
|
static char*
|
||||||
mu_util_dir_expand (const char *path)
|
do_wordexp (const char *path)
|
||||||
{
|
{
|
||||||
wordexp_t wexp;
|
wordexp_t wexp;
|
||||||
char *dir;
|
char *dir;
|
||||||
char resolved[PATH_MAX + 1];
|
|
||||||
int rv;
|
|
||||||
|
|
||||||
g_return_val_if_fail (path, NULL);
|
|
||||||
|
|
||||||
dir = NULL;
|
if (!path) {
|
||||||
rv = wordexp (path, &wexp, 0);
|
g_debug ("%s: path is empty", __FUNCTION__);
|
||||||
if (rv != 0) {
|
|
||||||
g_debug ("%s: expansion failed for '%s' [%d]",
|
|
||||||
__FUNCTION__, path, rv);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
} else if (wexp.we_wordc != 1) {
|
}
|
||||||
g_debug ("%s: expansion ambiguous for '%s'",
|
|
||||||
__FUNCTION__, path);
|
if (wordexp (path, &wexp, 0) != 0) {
|
||||||
} else
|
g_debug ("%s: expansion failed for %s", __FUNCTION__, path);
|
||||||
dir = g_strdup (wexp.we_wordv[0]);
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wexp.we_wordc != 1) /* not an *error*, we just take the first one */
|
||||||
|
g_debug ("%s: expansion ambiguous for '%s'", __FUNCTION__, path);
|
||||||
|
|
||||||
|
dir = g_strdup (wexp.we_wordv[0]);
|
||||||
|
|
||||||
/* strangely, below seems to load to a crash on MacOS (BSD);
|
/* strangely, below seems to lead to a crash on MacOS (BSD);
|
||||||
so we have to allow for a tiny leak here on that
|
so we have to allow for a tiny leak here on that
|
||||||
platform... maybe instead of __APPLE__ it should be
|
platform... maybe instead of __APPLE__ it should be
|
||||||
__BSD__?*/
|
__BSD__?*/
|
||||||
@ -75,16 +74,32 @@ mu_util_dir_expand (const char *path)
|
|||||||
wordfree (&wexp);
|
wordfree (&wexp);
|
||||||
#endif /*__APPLE__*/
|
#endif /*__APPLE__*/
|
||||||
|
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char*
|
||||||
|
mu_util_dir_expand (const char *path)
|
||||||
|
{
|
||||||
|
char *dir;
|
||||||
|
char resolved[PATH_MAX + 1];
|
||||||
|
|
||||||
|
g_return_val_if_fail (path, NULL);
|
||||||
|
|
||||||
|
dir = do_wordexp (path);
|
||||||
|
if (!dir)
|
||||||
|
return NULL; /* error */
|
||||||
|
|
||||||
/* now, resolve any symlinks, .. etc. */
|
/* now, resolve any symlinks, .. etc. */
|
||||||
if (!realpath (dir, resolved)) {
|
if (!realpath (dir, resolved)) {
|
||||||
g_debug ("%s: good not get realpath for '%s': %s",
|
g_debug ("%s: could not get realpath for '%s': %s",
|
||||||
__FUNCTION__, dir, strerror(errno));
|
__FUNCTION__, dir, strerror(errno));
|
||||||
g_free (dir);
|
g_free (dir);
|
||||||
return NULL;
|
return NULL;
|
||||||
} else
|
} else
|
||||||
g_free (dir);
|
g_free (dir);
|
||||||
|
|
||||||
return g_strdup(resolved);
|
return g_strdup (resolved);
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
|
|||||||
@ -64,6 +64,20 @@ test_mu_util_dir_expand_02 (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_mu_util_dir_expand_03 (void)
|
||||||
|
{
|
||||||
|
gchar *got;
|
||||||
|
|
||||||
|
got = mu_util_dir_expand (".");
|
||||||
|
g_assert_cmpstr (got,==,ABS_SRCDIR);
|
||||||
|
|
||||||
|
g_free (got);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_mu_util_guess_maildir_01 (void)
|
test_mu_util_guess_maildir_01 (void)
|
||||||
{
|
{
|
||||||
@ -182,6 +196,7 @@ test_mu_util_str_from_strv_03 (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -192,7 +207,9 @@ main (int argc, char *argv[])
|
|||||||
test_mu_util_dir_expand_01);
|
test_mu_util_dir_expand_01);
|
||||||
g_test_add_func ("/mu-util/mu-util-dir-expand-02",
|
g_test_add_func ("/mu-util/mu-util-dir-expand-02",
|
||||||
test_mu_util_dir_expand_02);
|
test_mu_util_dir_expand_02);
|
||||||
|
g_test_add_func ("/mu-util/mu-util-dir-expand-03",
|
||||||
|
test_mu_util_dir_expand_03);
|
||||||
|
|
||||||
/* mu_util_guess_maildir */
|
/* mu_util_guess_maildir */
|
||||||
g_test_add_func ("/mu-util/mu-util-guess-maildir-01",
|
g_test_add_func ("/mu-util/mu-util-guess-maildir-01",
|
||||||
test_mu_util_guess_maildir_01);
|
test_mu_util_guess_maildir_01);
|
||||||
|
|||||||
Reference in New Issue
Block a user