diff --git a/src/mu-util.c b/src/mu-util.c index 08eb36e9..7afe9b19 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -27,6 +27,8 @@ #include #include +#include +#include #include "mu-util.h" @@ -41,7 +43,7 @@ mu_util_dir_expand (const char *path) dir = NULL; wordexp (path, &wexp, 0); if (wexp.we_wordc != 1) - g_printerr ("error expanding dir '%s'", path); + g_warning ("error expanding dir '%s'", path); else dir = g_strdup (wexp.we_wordv[0]); @@ -52,6 +54,7 @@ mu_util_dir_expand (const char *path) + static gboolean _is_readable_dir (const gchar* path) { @@ -81,3 +84,29 @@ mu_util_guess_maildir (void) /* nope; nothing found */ return NULL; } + + +gboolean +mu_util_create_dir_maybe (const gchar *path) +{ + struct stat statbuf; + + g_return_val_if_fail (path, FALSE); + + /* if it exists, it must be a readable dir */ + if (stat (path, &statbuf) == 0) { + if ((!S_ISDIR(statbuf.st_mode)) || + (access (path, W_OK|R_OK) != 0)) { + g_warning ("Not a rw-directory: %s", path); + return FALSE; + } + } + + if (g_mkdir_with_parents (path, 0700) != 0) { + g_warning ("failed to create %s: %s", + path, strerror(errno)); + return FALSE; + } + + return TRUE; +} diff --git a/src/mu-util.h b/src/mu-util.h index c2af1075..c143671d 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -45,6 +45,18 @@ char* mu_util_dir_expand (const char* path); char* mu_util_guess_maildir (void); + +/** + * if path exists, check that's a read/writeable dir; otherwise try to + * create it (with perms 0700) + * + * @param path path to the dir + * + * @return TRUE if a read/writeable directory `path' exists after + * leaving this function, FALSE otherwise + */ +gboolean mu_util_create_dir_maybe (const gchar *path); + G_END_DECLS #endif /*__MU_UTIL_H__*/