From: Timo Sirainen Date: Wed, 13 Oct 2021 08:45:05 +0000 (+0300) Subject: lib-index: Add mail_index_alloc_cache_find() X-Git-Tag: 2.3.18~194 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b3f32368aa95c54790f28db2ed28ffef2a339a8a;p=thirdparty%2Fdovecot%2Fcore.git lib-index: Add mail_index_alloc_cache_find() --- diff --git a/src/lib-index/mail-index-alloc-cache.c b/src/lib-index/mail-index-alloc-cache.c index e348fabfa7..fe52754ea5 100644 --- a/src/lib-index/mail-index-alloc-cache.c +++ b/src/lib-index/mail-index-alloc-cache.c @@ -81,8 +81,9 @@ mail_index_alloc_cache_list_free(struct mail_index_alloc_cache_list *list) } static struct mail_index_alloc_cache_list * -mail_index_alloc_cache_find(const char *mailbox_path, const char *index_dir, - const struct stat *index_st) +mail_index_alloc_cache_find_and_expire(const char *mailbox_path, + const char *index_dir, + const struct stat *index_st) { struct mail_index_alloc_cache_list **indexp, *rec, *match; unsigned int destroy_count; @@ -157,7 +158,8 @@ mail_index_alloc_cache_get(struct event *parent_event, const char *mailbox_path, } } - match = mail_index_alloc_cache_find(mailbox_path, index_dir, &st); + match = mail_index_alloc_cache_find_and_expire(mailbox_path, + index_dir, &st); if (match == NULL) { struct mail_index *index = mail_index_alloc(parent_event, index_dir, prefix); @@ -169,6 +171,26 @@ mail_index_alloc_cache_get(struct event *parent_event, const char *mailbox_path, return match->index; } +struct mail_index * +mail_index_alloc_cache_find(const char *index_dir) +{ + struct mail_index_alloc_cache_list *rec; + struct stat st; + + if (stat(index_dir, &st) < 0) { + if (errno != ENOENT) + i_error("stat(%s) failed: %m", index_dir); + return NULL; + } + + for (rec = indexes; rec != NULL; rec = rec->next) { + if (st.st_ino == rec->index_dir_ino && + CMP_DEV_T(st.st_dev, rec->index_dir_dev)) + return rec->index; + } + return NULL; +} + static bool destroy_unrefed(unsigned int min_destroy_count) { struct mail_index_alloc_cache_list **list, *rec; diff --git a/src/lib-index/mail-index-alloc-cache.h b/src/lib-index/mail-index-alloc-cache.h index eadfc30f03..5f285639a0 100644 --- a/src/lib-index/mail-index-alloc-cache.h +++ b/src/lib-index/mail-index-alloc-cache.h @@ -7,6 +7,10 @@ mail_index_alloc_cache_get(struct event *parent_event, const char *mailbox_path, const char *index_dir, const char *prefix); void mail_index_alloc_cache_unref(struct mail_index **index); +/* Find an existing already opened index from a given index directory. */ +struct mail_index * +mail_index_alloc_cache_find(const char *index_dir); + void mail_index_alloc_cache_destroy_unrefed(void); /* internal: */