]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-index: Fix mail_index_get_modification_time() to work when index isn't open.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 18 Jul 2017 11:42:23 +0000 (14:42 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 18 Jul 2017 12:41:28 +0000 (15:41 +0300)
index->filepath may be NULL after a failed index open, and it's a bit unsafe
to trust that index->log->filepath isn't NULL either.  So just build the full
path from elements that are definitely non-NULL.

Also stat() only dovecot.index.log, because it's always supposed to exist.
If it doesn't, something's broken and stat()ing dovecot.index doesn't make
much sense.

This commit removes mail_transaction_log_get_mtime(), which is no longer
needed.

Fixes:
Panic: file mail-index.c: line 931 (mail_index_file_set_syscall_error): assertion failed: (filepath != NULL)

src/lib-index/mail-index.c
src/lib-index/mail-index.h
src/lib-index/mail-transaction-log.c
src/lib-index/mail-transaction-log.h

index b07a9b927f69b659b7f3a5ae18b4d482af316be5..8c2a65c837992bd1816c47056bcb08d9856ca909 100644 (file)
@@ -870,17 +870,27 @@ bool mail_index_is_deleted(struct mail_index *index)
 int mail_index_get_modification_time(struct mail_index *index, time_t *mtime_r)
 {
        struct stat st;
+       const char *path;
 
-       if (mail_transaction_log_get_mtime(index->log, mtime_r) < 0)
-               return -1;
+       *mtime_r = 0;
+       if (MAIL_INDEX_IS_IN_MEMORY(index)) {
+               /* this function doesn't make sense for in-memory indexes */
+               return 0;
+       }
 
-       if (*mtime_r == 0) {
-               if (stat(index->filepath, &st) < 0) {
-                       mail_index_set_syscall_error(index, "stat()");
-                       return -1;
+       /* index may not be open, so index->filepath may be NULL */
+       path = t_strconcat(index->dir, "/", index->prefix,
+                          MAIL_TRANSACTION_LOG_SUFFIX, NULL);
+       if (stat(path, &st) < 0) {
+               if (errno == ENOENT) {
+                       /* .log is always supposed to exist - don't bother
+                          trying to stat(dovecot.index) */
+                       return 0;
                }
-               *mtime_r = st.st_mtime;
+               mail_index_file_set_syscall_error(index, path, "stat()");
+               return -1;
        }
+       *mtime_r = st.st_mtime;
        return 0;
 }
 
index 9d6f2897836059f5eae50a734753e6a4f7c5b28a..f750d0bc1a0704bd2a26b8925d71f5a4c1d38a6a 100644 (file)
@@ -545,7 +545,8 @@ void mail_index_set_undeleted(struct mail_index_transaction *t);
 /* Returns TRUE if index has been set deleted. This gets set only after
    index has been opened/refreshed and the transaction has been seen. */
 bool mail_index_is_deleted(struct mail_index *index);
-/* Returns the last time mailbox was modified. */
+/* Returns the last time the index was modified. This can be called even if the
+   index isn't open. If the index doesn't exist, sets mtime to 0. */
 int mail_index_get_modification_time(struct mail_index *index, time_t *mtime_r);
 
 /* Lookup a keyword, returns TRUE if found, FALSE if not. */
index 5181409853966d69a8c889a41c1368d8faeb71ef..1738e367fc96150519471e456c7ffbadc70d32c9 100644 (file)
@@ -594,24 +594,6 @@ bool mail_transaction_log_is_head_prev(struct mail_transaction_log *log,
                log->head->hdr.prev_file_offset == file_offset;
 }
 
-int mail_transaction_log_get_mtime(struct mail_transaction_log *log,
-                                  time_t *mtime_r)
-{
-       struct stat st;
-
-       *mtime_r = 0;
-       if (stat(log->filepath, &st) < 0) {
-               if (errno == ENOENT)
-                       return 0;
-
-               mail_index_file_set_syscall_error(log->index, log->filepath,
-                                                 "stat()");
-               return -1;
-       }
-       *mtime_r = st.st_mtime;
-       return 0;
-}
-
 int mail_transaction_log_unlink(struct mail_transaction_log *log)
 {
        if (unlink(log->filepath) < 0 &&
index 41a635c17ea33416cb4f33efa393ecfe51cc5e31..0620f467be742fcf99b6e3b9e7488b8c0434e6ad 100644 (file)
@@ -307,10 +307,6 @@ bool mail_transaction_log_is_head_prev(struct mail_transaction_log *log,
 /* Move currently opened log head file to memory (called by
    mail_index_move_to_memory()) */
 void mail_transaction_log_move_to_memory(struct mail_transaction_log *log);
-/* Returns mtime of the transaction log head file.
-   If it doesn't exist, mtime_r is set to 0. */
-int mail_transaction_log_get_mtime(struct mail_transaction_log *log,
-                                  time_t *mtime_r);
 /* Unlink transaction log files */
 int mail_transaction_log_unlink(struct mail_transaction_log *log);