bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Locking should never fail or timeout. Exclusive locks must be kept as short
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen time as possible. Shared locks can be long living, so if we can't get
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen exclusive lock directly, we'll recreate the index. That means the shared
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen lock holders can keep using the old file.
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen lock_id is used to figure out if acquired lock is still valid. When index
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen file is reopened, the lock_id can become invalid. It doesn't matter however,
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen as no-one's going to modify the old file anymore.
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen lock_id also tells us if we're referring to a shared or an exclusive lock.
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen This allows us to drop back to shared locking once all exclusive locks
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen are dropped. Shared locks have even numbers, exclusive locks have odd numbers.
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen The number is increased by two every time the lock is dropped or index file
2af769daebd83719ac696a440e06f6020471cec0Timo Sirainenint mail_index_lock_fd(struct mail_index *index, const char *path, int fd,
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen return file_wait_lock(fd, path, lock_type, index->lock_method,
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainenvoid mail_index_flush_read_cache(struct mail_index *index, const char *path,
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen if ((index->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) == 0)
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen /* Assume flock() is emulated with fcntl(), because that's how most
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen OSes work nowadays. */
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen (index->lock_method == FILE_LOCK_METHOD_FCNTL ||