# HG changeset patch # User Timo Sirainen # Date 1102197341 -7200 # Node ID ba9062032877e9ef7ff89853b79ece008156c5d6 # Parent 3470bb04fb5709220924f5dab8468e7fe7091263 Locking fixes and cleanups diff -r 3470bb04fb57 -r ba9062032877 src/lib-index/mail-cache-compress.c --- a/src/lib-index/mail-cache-compress.c Sat Dec 04 21:46:59 2004 +0200 +++ b/src/lib-index/mail-cache-compress.c Sat Dec 04 23:55:41 2004 +0200 @@ -233,20 +233,15 @@ return mail_index_transaction_commit(t, &seq, &offset); } -int mail_cache_compress(struct mail_cache *cache, struct mail_index_view *view) +static int mail_cache_compress_locked(struct mail_cache *cache, + struct mail_index_view *view) { mode_t old_mask; - int fd, ret, locked; - - if ((ret = mail_cache_lock(cache)) < 0) - return -1; - locked = ret > 0; + int fd; /* get the latest info on fields */ - if (mail_cache_header_fields_read(cache) < 0) { - if (locked) mail_cache_unlock(cache); + if (mail_cache_header_fields_read(cache) < 0) return -1; - } #ifdef DEBUG i_warning("Compressing cache file %s", cache->filepath); @@ -261,7 +256,6 @@ if (fd == -1) { mail_cache_set_syscall_error(cache, "file_dotlock_open()"); - if (locked) mail_cache_unlock(cache); return -1; } @@ -273,37 +267,51 @@ // FIXME: check that cache file wasn't just recreated - ret = 0; if (mail_cache_copy(cache, view, fd) < 0) { (void)file_dotlock_delete(cache->filepath, NULL, fd); - ret = -1; - } else { - if (file_dotlock_replace(cache->filepath, NULL, - -1, FALSE) < 0) { - mail_cache_set_syscall_error(cache, - "file_dotlock_replace()"); - (void)close(fd); - ret = -1; - } else { - mail_cache_file_close(cache); - cache->fd = fd; + return -1; + } - if (cache->file_cache != NULL) - file_cache_set_fd(cache->file_cache, cache->fd); - - if (mail_cache_map(cache, 0, 0) < 0) - ret = -1; - else if (mail_cache_header_fields_read(cache) < 0) - ret = -1; - } + if (file_dotlock_replace(cache->filepath, NULL, + -1, FALSE) < 0) { + mail_cache_set_syscall_error(cache, + "file_dotlock_replace()"); + (void)close(fd); + return -1; } - if (locked) - mail_cache_unlock(cache); + mail_cache_file_close(cache); + cache->fd = fd; + + if (cache->file_cache != NULL) + file_cache_set_fd(cache->file_cache, cache->fd); + + if (mail_cache_map(cache, 0, 0) < 0) + return -1; + if (mail_cache_header_fields_read(cache) < 0) + return -1; + + cache->need_compress = FALSE; + return 0; +} - if (ret == 0) - cache->need_compress = FALSE; - return ret; +int mail_cache_compress(struct mail_cache *cache, struct mail_index_view *view) +{ + int ret; + + switch (mail_cache_lock(cache)) { + case -1: + return -1; + case 0: + /* couldn't lock, either it's broken or doesn't exist. + just start creating it. */ + return mail_cache_compress_locked(cache, view); + default: + /* locking succeeded. */ + ret = mail_cache_compress_locked(cache, view); + mail_cache_unlock(cache); + return ret; + } } int mail_cache_need_compress(struct mail_cache *cache) diff -r 3470bb04fb57 -r ba9062032877 src/lib-index/mail-cache-fields.c --- a/src/lib-index/mail-cache-fields.c Sat Dec 04 21:46:59 2004 +0200 +++ b/src/lib-index/mail-cache-fields.c Sat Dec 04 23:55:41 2004 +0200 @@ -275,23 +275,15 @@ } } -int mail_cache_header_fields_update(struct mail_cache *cache) +static int mail_cache_header_fields_update_locked(struct mail_cache *cache) { - int locked = cache->locked; buffer_t *buffer; uint32_t i, offset; int ret = 0; - if (!locked) { - if (mail_cache_lock(cache) <= 0) - return -1; - } - if (mail_cache_header_fields_read(cache) < 0 || - mail_cache_header_fields_get_offset(cache, &offset) < 0) { - mail_cache_unlock(cache); + mail_cache_header_fields_get_offset(cache, &offset) < 0) return -1; - } t_push(); buffer = buffer_create_dynamic(pool_datastack_create(), 256); @@ -321,8 +313,21 @@ if (ret == 0) cache->field_header_write_pending = FALSE; - if (!locked) - mail_cache_unlock(cache); + return ret; +} + +int mail_cache_header_fields_update(struct mail_cache *cache) +{ + int ret; + + if (cache->locked) + return mail_cache_header_fields_update_locked(cache); + + if (mail_cache_lock(cache) <= 0) + return -1; + + ret = mail_cache_header_fields_update_locked(cache); + mail_cache_unlock(cache); return ret; } diff -r 3470bb04fb57 -r ba9062032877 src/lib-index/mail-cache.c --- a/src/lib-index/mail-cache.c Sat Dec 04 21:46:59 2004 +0200 +++ b/src/lib-index/mail-cache.c Sat Dec 04 23:55:41 2004 +0200 @@ -352,12 +352,15 @@ file_cache_invalidate(cache->file_cache, 0, sizeof(struct mail_cache_header)); } - if (mail_cache_map(cache, 0, 0) < 0) + if (mail_cache_map(cache, 0, 0) < 0) { + mail_cache_unlock(cache); ret = -1; + } cache->hdr_copy = *cache->hdr; } mail_index_view_close(view); + i_assert((ret <= 0 && !cache->locked) || (ret > 0 && cache->locked)); return ret; }