From: Timo Sirainen Date: Thu, 16 Apr 2020 11:46:03 +0000 (+0300) Subject: lib-index: Add mail_index_lock_sync() and mail_index_unlock() X-Git-Tag: 2.3.11.2~341 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8f58058f779a946064b556d1d587fdea3c7b76c8;p=thirdparty%2Fdovecot%2Fcore.git lib-index: Add mail_index_lock_sync() and mail_index_unlock() --- diff --git a/src/lib-index/mail-index.c b/src/lib-index/mail-index.c index fa9226efd6..6b54979e07 100644 --- a/src/lib-index/mail-index.c +++ b/src/lib-index/mail-index.c @@ -1037,6 +1037,25 @@ void mail_index_fchown(struct mail_index *index, int fd, const char *path) mail_index_file_set_syscall_error(index, path, "fchmod()"); } +int mail_index_lock_sync(struct mail_index *index, const char *lock_reason) +{ + uint32_t file_seq; + uoff_t file_offset; + + return mail_transaction_log_sync_lock(index->log, lock_reason, + &file_seq, &file_offset); +} + +void mail_index_unlock(struct mail_index *index, const char *long_lock_reason) +{ + mail_transaction_log_sync_unlock(index->log, long_lock_reason); +} + +bool mail_index_is_locked(struct mail_index *index) +{ + return index->log_sync_locked; +} + void mail_index_set_syscall_error(struct mail_index *index, const char *function) { diff --git a/src/lib-index/mail-index.h b/src/lib-index/mail-index.h index 6c725e8fc8..b9ab3aaf7f 100644 --- a/src/lib-index/mail-index.h +++ b/src/lib-index/mail-index.h @@ -490,6 +490,18 @@ int mail_index_sync_commit(struct mail_index_sync_ctx **ctx); actually written to index file. */ void mail_index_sync_rollback(struct mail_index_sync_ctx **ctx); +/* Lock the index exclusively. This is the same locking as what happens when + syncing the index. It's not necessary to normally call this function, unless + doing something special such as rebuilding the index outside syncing. + Returns 0 on success, -1 if locking failed for any reason. */ +int mail_index_lock_sync(struct mail_index *index, const char *lock_reason); +/* Unlock the locked index. The index must have been locked previously with + mail_index_lock_sync(). If the lock had been kept for excessively long, + a warning is logged with the long_lock_reason. */ +void mail_index_unlock(struct mail_index *index, const char *long_lock_reason); +/* Returns TRUE if index is currently exclusively locked. */ +bool mail_index_is_locked(struct mail_index *index); + /* Mark index file corrupted. Invalidates all views. */ void mail_index_mark_corrupted(struct mail_index *index); /* Check and fix any found problems. Returns -1 if we couldn't lock for sync,