]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Mail cache compression: If we can't get lock immediately, do it later.
authorTimo Sirainen <tss@iki.fi>
Thu, 11 Sep 2008 14:01:32 +0000 (17:01 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 11 Sep 2008 14:01:32 +0000 (17:01 +0300)
--HG--
branch : HEAD

src/lib-index/mail-cache-compress.c
src/lib-index/mail-cache-private.h
src/lib-index/mail-cache.c

index e64209f5ae4efd3549dfeaa0624e2f2b3983b064..3ed982b226c85549762a13c2a5052880cbc80a29 100644 (file)
@@ -361,11 +361,12 @@ static int mail_cache_compress_locked(struct mail_cache *cache,
 
        old_mask = umask(cache->index->mode ^ 0666);
        fd = file_dotlock_open(&cache->dotlock_settings, cache->filepath,
-                              0, &dotlock);
+                              DOTLOCK_CREATE_FLAG_NONBLOCK, &dotlock);
        umask(old_mask);
 
        if (fd == -1) {
-               mail_cache_set_syscall_error(cache, "file_dotlock_open()");
+               if (errno != EAGAIN)
+                       mail_cache_set_syscall_error(cache, "file_dotlock_open()");
                return -1;
        }
 
@@ -467,7 +468,7 @@ int mail_cache_compress(struct mail_cache *cache,
                                                    FALSE);
                }
        } else {
-               switch (mail_cache_lock(cache, FALSE)) {
+               switch (mail_cache_try_lock(cache)) {
                case -1:
                        return -1;
                case 0:
index 57e3e48016075bd0dccc0d43f333392e6a20e967..aa40c86a4534e30f7e9ecb6f95b5d72846576eae 100644 (file)
@@ -224,6 +224,7 @@ int mail_cache_open_and_verify(struct mail_cache *cache);
 /* Explicitly lock the cache file. Returns -1 if error / timed out,
    1 if ok, 0 if cache is broken/doesn't exist */
 int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id);
+int mail_cache_try_lock(struct mail_cache *cache);
 /* Returns -1 if cache is / just got corrupted, 0 if ok. */
 int mail_cache_unlock(struct mail_cache *cache);
 
index b2df74da28361aa3af8d4b1b9a8fb3abceb92b88..d69a41823e1c5f0a33a62ef4fa562dab7c86e775 100644 (file)
@@ -460,7 +460,7 @@ void mail_cache_free(struct mail_cache **_cache)
        i_free(cache);
 }
 
-static int mail_cache_lock_file(struct mail_cache *cache)
+static int mail_cache_lock_file(struct mail_cache *cache, bool nonblock)
 {
        int ret;
 
@@ -468,12 +468,16 @@ static int mail_cache_lock_file(struct mail_cache *cache)
                i_assert(cache->file_lock == NULL);
                ret = mail_index_lock_fd(cache->index, cache->filepath,
                                         cache->fd, F_WRLCK,
-                                        MAIL_CACHE_LOCK_TIMEOUT,
+                                        nonblock ? 0 : MAIL_CACHE_LOCK_TIMEOUT,
                                         &cache->file_lock);
        } else {
+               enum dotlock_create_flags flags =
+                       nonblock ? DOTLOCK_CREATE_FLAG_NONBLOCK : 0;
+
                i_assert(cache->dotlock == NULL);
                ret = file_dotlock_create(&cache->dotlock_settings,
-                                         cache->filepath, 0, &cache->dotlock);
+                                         cache->filepath, flags,
+                                         &cache->dotlock);
                if (ret <= 0) {
                        mail_cache_set_syscall_error(cache,
                                                     "file_dotlock_create()");
@@ -496,7 +500,9 @@ static void mail_cache_unlock_file(struct mail_cache *cache)
                (void)file_dotlock_delete(&cache->dotlock);
 }
 
-int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id)
+static int
+mail_cache_lock_full(struct mail_cache *cache, bool require_same_reset_id,
+                    bool nonblock)
 {
        const struct mail_index_ext *ext;
        struct mail_index_view *iview;
@@ -538,7 +544,7 @@ int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id)
                                break;
                }
 
-               if ((ret = mail_cache_lock_file(cache)) <= 0) {
+               if ((ret = mail_cache_lock_file(cache, nonblock)) <= 0) {
                        ret = -1;
                        break;
                }
@@ -573,6 +579,16 @@ int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id)
        return ret;
 }
 
+int mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id)
+{
+       return mail_cache_lock_full(cache, require_same_reset_id, FALSE);
+}
+
+int mail_cache_try_lock(struct mail_cache *cache)
+{
+       return mail_cache_lock_full(cache, FALSE, TRUE);
+}
+
 static void mail_cache_update_need_compress(struct mail_cache *cache)
 {
        const struct mail_cache_header *hdr = cache->hdr;