From 3b1b4926bf47aa8c963ad2bc799409b14132b74d Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 6 Jan 2010 01:18:24 +0200 Subject: [PATCH] * mu-util.[ch]: add mu_util_check_dir (to check for existence/readability/writability of dirs --- src/mu-util.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/mu-util.c b/src/mu-util.c index 7afe9b19..e919ccb9 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -54,18 +54,31 @@ mu_util_dir_expand (const char *path) - -static gboolean -_is_readable_dir (const gchar* path) +gboolean +mu_util_check_dir (const gchar* path, gboolean readable, gboolean writeable) { + mode_t mode; struct stat statbuf; + + if (!path) + return FALSE; + + mode = F_OK | (readable ? R_OK : 0) | (writeable ? W_OK : 0); - return path && - access (path, R_OK) == 0 && - stat (path, &statbuf) == 0 && - S_ISDIR(statbuf.st_mode); + if (access (path, mode) != 0) { + MU_WRITE_LOG ("Cannot access %s: %s", path, strerror (errno)); + return FALSE; + } + + if (stat (path, &statbuf) != 0) { + MU_WRITE_LOG ("Cannot stat %s: %s", path, strerror (errno)); + return FALSE; + } + + return S_ISDIR(statbuf.st_mode); } + gchar* mu_util_guess_maildir (void) { @@ -73,12 +86,12 @@ mu_util_guess_maildir (void) /* first, try MAILDIR */ dir = getenv ("MAILDIR"); - if (_is_readable_dir (dir)) + if (mu_util_check_dir (dir, TRUE, FALSE)) return g_strdup (dir); /* then, try ~/Maildir */ dir = mu_util_dir_expand ("~/Maildir"); - if (_is_readable_dir (dir)) + if (!mu_util_check_dir (dir, TRUE, FALSE)) return dir; /* nope; nothing found */ @@ -86,6 +99,7 @@ mu_util_guess_maildir (void) } + gboolean mu_util_create_dir_maybe (const gchar *path) {