mbox-sync.c revision 100e980900f652c9114997d7153e010e0639effe
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody/* Copyright (C) 2002 Timo Sirainen */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstatic uoff_t get_indexed_mbox_size(MailIndex *index)
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen if (!mail_index_set_lock(index, MAIL_LOCK_SHARED))
5afc76d0215c5f7631dec06ef864d59f0686a0a8Timo Sirainen /* get the last record */
b624773984e35dd894db8dff976c1a2114c70782Timo Sirainen rec = index->header->messages_count == 0 ? NULL :
12d38e76ba7f70d6219c89ec7416fea0d5de7e02Timo Sirainen index->lookup(index, index->header->messages_count);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen /* get the offset + size of last message, which tells the
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen last known mbox file size */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen if (mbox_mail_get_location(index, rec, &offset,
be5c76fabc7439fd33bc799bc3ab3f570799977bTimo Sirainen /* too large to fit in off_t */
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainenstatic int mbox_lock_and_sync_full(MailIndex *index,
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen /* syncing needs exclusive index lock and shared
788a0754cfd38dcfec1902844b085e4e84cfe7e6Timo Sirainen mbox lock, but if we'd want exclusive mbox lock
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen we need to set it here already */
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen (void)mail_index_set_lock(index, MAIL_LOCK_UNLOCK);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen if (index->mbox_lock_type == MAIL_LOCK_UNLOCK) {
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen lock_type = data_lock_type == MAIL_LOCK_EXCLUSIVE ?
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenint mbox_index_sync(MailIndex *index, MailLockType data_lock_type,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen i_assert(index->lock_type != MAIL_LOCK_SHARED);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen /* anon-mmaped */
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainen return index_set_syscall_error(index, "fstat()");
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen return mbox_set_syscall_error(index, "stat()");
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainen if (index->mbox_dev != st.st_dev || index->mbox_ino != st.st_ino) {
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainen /* mbox file was overwritten, close it if it was open */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen if (index_mtime != st.st_mtime || index->mbox_size != filesize) {
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen index->mbox_size = get_indexed_mbox_size(index);
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen /* just opened the mailbox, and the file size is same
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen as we expected. don't bother checking it any
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen further. the +1 comes from the extra \n at end. */
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen if (!mbox_lock_and_sync_full(index, data_lock_type))
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen /* we need some index lock to be able to lock mbox */
d6bffcdf187c155dccc04fb4267b4f82ce59347dTimo Sirainen if (!index->set_lock(index, MAIL_LOCK_SHARED))