diff --git a/src/mu-maildir.c b/src/mu-maildir.c index 0ee83851..2d124ae7 100644 --- a/src/mu-maildir.c +++ b/src/mu-maildir.c @@ -69,15 +69,6 @@ create_maildir (const char *path, mode_t mode, GError **err) int i; const gchar* subdirs[] = {"new", "cur", "tmp"}; - /* make sure it does not exist yet */ - if (access (path, F_OK) == 0) - errno = EEXIST; - - if (errno != ENOENT) { - g_set_error (err, 0, MU_ERROR_FILE, "%s", strerror (errno)); - return FALSE; - } - for (i = 0; i != G_N_ELEMENTS(subdirs); ++i) { const char *fullpath; @@ -85,10 +76,21 @@ create_maildir (const char *path, mode_t mode, GError **err) /* static buffer */ fullpath = mu_str_fullpath_s (path, subdirs[i]); + + /* if subdir already exists, don't try to re-create + * it */ + if (mu_util_check_dir (fullpath, TRUE, TRUE)) + continue; + rv = g_mkdir_with_parents (fullpath, (int)mode); - if (rv != 0) { + + /* note, g_mkdir_with_parents won't detect an error if + * there's already such a dir, but with the wrong + * permissions; so we need to check */ + if (rv != 0 || !mu_util_check_dir(fullpath, TRUE, TRUE)) { g_set_error (err, 0, MU_ERROR_FILE_CANNOT_MKDIR, - "g_mkdir_with_parents failed: %s", + "creating dir failed for %s: %s", + fullpath, strerror (errno)); return FALSE; } @@ -860,6 +862,3 @@ mu_maildir_move_message (const char* oldpath, const char* targetmdir, return newfullpath; } - - - diff --git a/src/mu-maildir.h b/src/mu-maildir.h index 40f3b2a5..f23be2f6 100644 --- a/src/mu-maildir.h +++ b/src/mu-maildir.h @@ -1,5 +1,5 @@ /* -** Copyright (C) 2008-2010 Dirk-Jan C. Binnema +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -30,19 +30,22 @@ G_BEGIN_DECLS /** - * create a new maildir. Note, if the function fails 'halfway', it - * will *not* try to remove the parts the were created. it *will* - * create any parent dirs that are not yet existant. + * create a new maildir. if parts of the maildir already exists, those + * will simply be ignored. IOW, if you try to create the same maildir + * twice, the second will simply be a no-op (without any errors). + * Note, if the function fails 'halfway', it will *not* try to remove + * the parts the were created. it *will* create any parent dirs that + * are not yet existent. * * * @param path the path (missing components will be created, as in 'mkdir -p') * @param mode the file mode (e.g., 0755) * @param noindex add a .noindex file to the maildir, so it will be excluded * from indexing by 'mu index' - * @param err if function returns FALSE, err may contain extra - * information. if err is NULL, does nothing + * @param err if function returns FALSE, receives error + * information. err may be NULL. * - * @return TRUE if creation succeeded, FALSE otherwise + * @return TRUE if creation succeeded (or already existed), FALSE otherwise */ gboolean mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex, GError **err); @@ -60,7 +63,8 @@ gboolean mu_maildir_mkdir (const char* path, mode_t mode, gboolean noindex, * * @return */ -gboolean mu_maildir_link (const char* src, const char *targetpath, GError **err); +gboolean mu_maildir_link (const char* src, const char *targetpath, + GError **err); /** * MuMaildirWalkMsgCallback -- callback function for @@ -72,7 +76,8 @@ gboolean mu_maildir_link (const char* src, const char *targetpath, GError **er * file (see stat(3)), and a user_data pointer */ typedef MuError (*MuMaildirWalkMsgCallback) -(const char* fullpath, const char* mdir, struct stat *statinfo, void *user_data); + (const char* fullpath, const char* mdir, struct stat *statinfo, + void *user_data); /** * MuPathWalkDirCallback -- callback function for mu_path_walk_maildir; see the