Mercurial > dovecot > original-hg > dovecot-1.2
changeset 5487:befe6a6233c7 HEAD
Cleaned up maildir_create_tmp() a bit. It now also returns the filename
instead of the whole path, as was originally intended.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 02 Apr 2007 05:49:14 +0300 |
parents | 61ef8b18a12c |
children | 3a8a3b2badab |
files | src/lib-storage/index/maildir/maildir-save.c src/lib-storage/index/maildir/maildir-util.c |
diffstat | 2 files changed, 21 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib-storage/index/maildir/maildir-save.c Sat Mar 31 17:06:13 2007 +0300 +++ b/src/lib-storage/index/maildir/maildir-save.c Mon Apr 02 05:49:14 2007 +0300 @@ -334,7 +334,7 @@ struct maildir_save_context *ctx; struct maildir_mailbox *mbox = (struct maildir_mailbox *)t->ictx.ibox; struct ostream *output; - const char *fname, *path; + const char *fname; i_assert((t->ictx.flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0); @@ -346,17 +346,13 @@ /* create a new file in tmp/ directory */ ctx->fd = maildir_create_tmp(mbox, ctx->tmpdir, mbox->mail_create_mode, - &path); + &fname); if (ctx->fd == -1) { ctx->failed = TRUE; t_pop(); return -1; } - fname = strrchr(path, '/'); - i_assert(fname != NULL); - fname++; - ctx->received_date = received_date; ctx->input = input;
--- a/src/lib-storage/index/maildir/maildir-util.c Sat Mar 31 17:06:13 2007 +0300 +++ b/src/lib-storage/index/maildir/maildir-util.c Mon Apr 02 05:49:14 2007 +0300 @@ -94,52 +94,59 @@ int maildir_create_tmp(struct maildir_mailbox *mbox, const char *dir, mode_t mode, const char **fname_r) { - const char *path, *tmp_fname; struct stat st; struct timeval *tv, tv_now; - pool_t pool; + unsigned int prefix_len; + const char *tmp_fname = NULL; + string_t *path; int fd; tv = &ioloop_timeval; - pool = pool_alloconly_create("maildir_tmp", 4096); + path = t_str_new(256); + str_append(path, dir); + str_append_c(path, '/'); + prefix_len = str_len(path); + for (;;) { - p_clear(pool); tmp_fname = maildir_generate_tmp_filename(tv); + str_truncate(path, prefix_len); + str_append(path, tmp_fname); - path = p_strconcat(pool, dir, "/", tmp_fname, NULL); - if (stat(path, &st) < 0 && errno == ENOENT) { + /* stat() first to see if it exists. pretty much the only + possibility of that happening is if time had moved + backwards, but even then it's highly unlikely. */ + if (stat(str_c(path), &st) < 0 && errno == ENOENT) { /* doesn't exist */ mode_t old_mask = umask(0); - fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); + fd = open(str_c(path), O_WRONLY | O_CREAT | O_EXCL, + mode); umask(old_mask); if (fd != -1 || errno != EEXIST) break; } - /* wait and try again - very unlikely */ sleep(2); tv = &tv_now; if (gettimeofday(&tv_now, NULL) < 0) i_fatal("gettimeofday(): %m"); } - *fname_r = t_strdup(path); + *fname_r = tmp_fname; if (fd == -1) { if (ENOSPACE(errno)) { mail_storage_set_error(&mbox->storage->storage, "Not enough disk space"); } else { mail_storage_set_critical(&mbox->storage->storage, - "open(%s) failed: %m", path); + "open(%s) failed: %m", str_c(path)); } } else if (mbox->mail_create_gid != (gid_t)-1) { if (fchown(fd, (uid_t)-1, mbox->mail_create_gid) < 0) { mail_storage_set_critical(&mbox->storage->storage, - "fchown(%s) failed: %m", path); + "fchown(%s) failed: %m", str_c(path)); } } - pool_unref(pool); return fd; }