* add mu_util_create_dir_maybe

to create a dir if it does not exist yet; if it does exist, make sure it's a
  read-writeable dir
This commit is contained in:
Dirk-Jan C. Binnema 2010-01-04 20:15:49 +02:00
parent 737754c03e
commit 16c305fcc3
2 changed files with 42 additions and 1 deletions

View File

@ -27,6 +27,8 @@
#include <sys/stat.h>
#include <unistd.h>
#include <glib/gstdio.h>
#include <errno.h>
#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;
}

View File

@ -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__*/