From: Timo Sirainen Date: Sun, 20 Jul 2008 20:03:09 +0000 (+0300) Subject: mkdir_parents() API was sometimes assumed to return EEXIST and sometimes not. X-Git-Tag: 1.2.alpha1~127 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a96307ca3944c434f2c50f47c12985bcd34445b5;p=thirdparty%2Fdovecot%2Fcore.git mkdir_parents() API was sometimes assumed to return EEXIST and sometimes not. Standardized it now so that the API does return EEXIST. --HG-- branch : HEAD --- diff --git a/src/lib-storage/index/dbox/dbox-file.c b/src/lib-storage/index/dbox/dbox-file.c index 8765cd6583..7a0c6548df 100644 --- a/src/lib-storage/index/dbox/dbox-file.c +++ b/src/lib-storage/index/dbox/dbox-file.c @@ -1240,7 +1240,7 @@ int dbox_file_move(struct dbox_file *file, bool alt_path) since we really don't want to break the file. */ out_fd = open(temp_path, O_WRONLY | O_CREAT | O_TRUNC, 0600); if (out_fd == -1 && errno == ENOENT) { - if (mkdir_parents(dest_dir, 0700) < 0) { + if (mkdir_parents(dest_dir, 0700) < 0 && errno != EEXIST) { i_error("mkdir_parents(%s) failed: %m", dest_dir); return -1; } diff --git a/src/lib-storage/index/mbox/mbox-storage.c b/src/lib-storage/index/mbox/mbox-storage.c index b4ce350f3b..85f07d67a7 100644 --- a/src/lib-storage/index/mbox/mbox-storage.c +++ b/src/lib-storage/index/mbox/mbox-storage.c @@ -265,7 +265,7 @@ static const char *create_root_dir(bool debug, const char **error_r) } path = t_strconcat(home, "/mail", NULL); - if (mkdir_parents(path, CREATE_MODE) < 0) { + if (mkdir_parents(path, CREATE_MODE) < 0 && errno != EEXIST) { *error_r = t_strdup_printf("mkdir(%s) failed: %m", path); return NULL; } @@ -719,7 +719,7 @@ static int mbox_mailbox_create(struct mail_storage *_storage, const char *name, p = directory ? path + strlen(path) : strrchr(path, '/'); if (p != NULL) { p = t_strdup_until(path, p); - if (mkdir_parents(p, CREATE_MODE) < 0) { + if (mkdir_parents(p, CREATE_MODE) < 0 && errno != EEXIST) { if (!mail_storage_set_error_from_errno(_storage)) { mail_storage_set_critical(_storage, "mkdir_parents(%s) failed: %m", p); diff --git a/src/lib-storage/list/mailbox-list-fs.c b/src/lib-storage/list/mailbox-list-fs.c index a3c4baaa5b..da62552c56 100644 --- a/src/lib-storage/list/mailbox-list-fs.c +++ b/src/lib-storage/list/mailbox-list-fs.c @@ -293,7 +293,7 @@ static int fs_list_rename_mailbox(struct mailbox_list *list, p = strrchr(newpath, '/'); if (p != NULL) { p = t_strdup_until(newpath, p); - if (mkdir_parents(p, CREATE_MODE) < 0) { + if (mkdir_parents(p, CREATE_MODE) < 0 && errno != EEXIST) { if (mailbox_list_set_error_from_errno(list)) return -1; diff --git a/src/lib-storage/list/subscription-file.c b/src/lib-storage/list/subscription-file.c index d8d7ab7f4e..69c5fff5c8 100644 --- a/src/lib-storage/list/subscription-file.c +++ b/src/lib-storage/list/subscription-file.c @@ -101,7 +101,8 @@ int subsfile_set_subscribed(struct mailbox_list *list, const char *path, /* directory hasn't been created yet. */ p = strrchr(path, '/'); dir = p == NULL ? NULL : t_strdup_until(path, p); - if (dir != NULL && mkdir_parents(dir, 0700) < 0) { + if (dir != NULL && mkdir_parents(dir, 0700) < 0 && + errno != EEXIST) { subsfile_set_syscall_error(list, "mkdir()", dir); return -1; } diff --git a/src/lib/mkdir-parents.c b/src/lib/mkdir-parents.c index c81dd8bbf8..19563cf9ed 100644 --- a/src/lib/mkdir-parents.c +++ b/src/lib/mkdir-parents.c @@ -10,16 +10,18 @@ int mkdir_parents(const char *path, mode_t mode) const char *p; int ret; - /* EISDIR check is for BSD/OS which returns it if path contains '/' - at the end and it exists. - - ENOSYS check is for NFS mount points. - */ - if (mkdir(path, mode) < 0 && errno != EEXIST && - errno != EISDIR && errno != ENOSYS) { - if (errno != ENOENT) - return -1; - + if (mkdir(path, mode) == 0) { + /* success */ + } else if (errno != ENOENT) { + /* EISDIR check is for BSD/OS which returns it if path + contains '/' at the end and it exists. + + ENOSYS check is for NFS mount points. + */ + if (errno == EISDIR && errno == ENOSYS) + errno = EEXIST; + return -1; + } else { p = strrchr(path, '/'); if (p == NULL || p == path) return -1; /* shouldn't happen */ diff --git a/src/lib/mkdir-parents.h b/src/lib/mkdir-parents.h index 4cf592b726..975033164f 100644 --- a/src/lib/mkdir-parents.h +++ b/src/lib/mkdir-parents.h @@ -1,8 +1,9 @@ #ifndef MKDIR_PARENTS_H #define MKDIR_PARENTS_H -/* Create path and all the directories under it if needed. - Returns 0 if ok, or if path already exists (not necessarily as directory). */ +/* Create path and all the directories under it if needed. Permissions for + existing directories isn't changed. Returns 0 if ok. If directory already + exists, returns -1 with errno=EXIST. */ int mkdir_parents(const char *path, mode_t mode); #endif diff --git a/src/plugins/fts-lucene/fts-backend-lucene.c b/src/plugins/fts-lucene/fts-backend-lucene.c index 99c5a3408a..77e0866b53 100644 --- a/src/plugins/fts-lucene/fts-backend-lucene.c +++ b/src/plugins/fts-lucene/fts-backend-lucene.c @@ -57,7 +57,7 @@ static struct fts_backend *fts_backend_lucene_init(struct mailbox *box) path = t_strconcat(path, "/"LUCENE_INDEX_DIR_NAME, NULL); lock_path = t_strdup_printf("%s/"LUCENE_LOCK_SUBDIR_NAME, path); - if (mkdir_parents(lock_path, 0700) < 0) { + if (mkdir_parents(lock_path, 0700) < 0 && errno != EEXIST) { i_error("mkdir_parents(%s) failed: %m", lock_path); return NULL; }