mbox-sync.c revision 99b621cc5076398c5d780d2ea33dd7391341d630
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainenstatic uoff_t get_indexed_mbox_size(struct mail_index *index)
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen if (!mail_index_set_lock(index, MAIL_LOCK_SHARED))
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen /* get the last record */
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen rec = index->header->messages_count == 0 ? NULL :
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen index->lookup(index, index->header->messages_count);
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen /* get the offset + size of last message, which tells the
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen last known mbox file size */
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen if (mbox_mail_get_location(index, rec, &offset,
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen /* too large to fit in off_t */
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainenstatic int mbox_lock_and_sync_full(struct mail_index *index,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen /* syncing needs exclusive index lock and shared
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen mbox lock, but if we'd want exclusive mbox lock
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen we need to set it here already */
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen (void)mail_index_set_lock(index, MAIL_LOCK_UNLOCK);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen if (index->mbox_lock_type == MAIL_LOCK_UNLOCK) {
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen lock_type = data_lock_type == MAIL_LOCK_EXCLUSIVE ?
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen enum mail_lock_type data_lock_type, int *changes)
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen i_assert(index->lock_type != MAIL_LOCK_SHARED);
900d1f655065fa0cfe601f537142eb212ef5c011Timo Sirainen if (index->mbox_sync_counter == index->mbox_lock_counter) {
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen /* we've already synced in this locking session */
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen /* anon-mmaped */
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen return index_set_syscall_error(index, "fstat()");
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen return mbox_set_syscall_error(index, "stat()");
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen /* mbox was deleted by someone - happens with some MUAs
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen when all mail is expunged. easiest way to deal with this
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen is to recreate the file. */
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen fd = open(index->mailbox_path, O_RDWR | O_CREAT | O_EXCL, 0660);
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen return mbox_set_syscall_error(index, "open()");
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen /* mbox file was overwritten, close it if it was open */
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen if (index_mtime != st.st_mtime || index->mbox_size != filesize) {
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen index->mbox_size = get_indexed_mbox_size(index);
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen /* just opened the mailbox, and the file size is same
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen as we expected. don't bother checking it any
ab170d6cf8aeb515bac7c07b37e525cbad96c299Timo Sirainen if (!mbox_lock_and_sync_full(index, data_lock_type))
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen if ((index->set_flags & MAIL_INDEX_FLAG_REBUILD) != 0) {
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen /* uidvalidity probably changed, rebuild */
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen /* we need some index lock to be able to lock mbox */
2efe6c8cb2773ac6316638fd9a61d7e8c908153fTimo Sirainen if (!index->set_lock(index, MAIL_LOCK_SHARED))