* mu-maildir.[ch]: make create_maildir work even if the maildir already

partially exists and double-check permissions
This commit is contained in:
Dirk-Jan C. Binnema 2012-02-26 12:16:57 +02:00
parent dcc0d5fc18
commit f27af1158b
2 changed files with 27 additions and 23 deletions

View File

@ -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;
}

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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