NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
hcache.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stdbool.h>
31#include <string.h>
32#include <sys/stat.h>
33#include "mutt/lib.h"
34#include "config/lib.h"
35#include "email/lib.h"
36#include "core/lib.h"
37#include "hcache/lib.h"
38#include "edata.h"
39#include "mailbox.h"
40
46static const char *maildir_hcache_key(struct Email *e)
47{
48 /* Skip the subdir prefix ("cur/" or "new/") */
49 if (e->path &&
50 (mutt_strn_equal(e->path, "cur/", 4) || mutt_strn_equal(e->path, "new/", 4)))
51 {
52 return e->path + 4;
53 }
54
55 /* Fallback: return full path if prefix is unexpected */
56 return NONULL(e->path);
57}
58
66static size_t maildir_hcache_keylen(const char *fn)
67{
68 const char c_maildir_field_delimiter = *cc_maildir_field_delimiter();
69 const char *p = strrchr(fn, c_maildir_field_delimiter);
70 return p ? (size_t) (p - fn) : mutt_str_len(fn);
71}
72
78{
79 hcache_close(ptr);
80}
81
89int maildir_hcache_delete(struct HeaderCache *hc, struct Email *e)
90{
91 if (!hc || !e)
92 return 0;
93
94 const char *key = maildir_hcache_key(e);
95 size_t keylen = maildir_hcache_keylen(key);
96
97 return hcache_delete_email(hc, key, keylen);
98}
99
105{
106 if (!m)
107 return NULL;
108
109 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
110
111 return hcache_open(c_header_cache, mailbox_path(m), NULL, true);
112}
113
121struct Email *maildir_hcache_read(struct HeaderCache *hc, struct Email *e, const char *fn)
122{
123 if (!hc || !e)
124 return NULL;
125
126 struct stat st_lastchanged = { 0 };
127 int rc = 0;
128
129 const char *key = maildir_hcache_key(e);
130 size_t keylen = maildir_hcache_keylen(key);
131
132 struct HCacheEntry hce = hcache_fetch_email(hc, key, keylen, 0);
133 if (!hce.email)
134 return NULL;
135
136 const bool c_maildir_header_cache_verify = cs_subset_bool(NeoMutt->sub, "maildir_header_cache_verify");
137 if (c_maildir_header_cache_verify)
138 rc = stat(fn, &st_lastchanged);
139
140 if ((rc == 0) && (st_lastchanged.st_mtime <= hce.uidvalidity))
141 {
144 hce.email->old = e->old;
145 hce.email->path = mutt_str_dup(e->path);
146 maildir_parse_flags(hce.email, fn);
147 }
148 else
149 {
150 email_free(&hce.email);
151 }
152
153 return hce.email;
154}
155
163int maildir_hcache_store(struct HeaderCache *hc, struct Email *e)
164{
165 if (!hc || !e)
166 return 0;
167
168 const char *key = maildir_hcache_key(e);
169 size_t keylen = maildir_hcache_keylen(key);
170
171 return hcache_store_email(hc, key, keylen, e, 0);
172}
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition helpers.c:168
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
Convenience wrapper for the config headers.
const char * cc_maildir_field_delimiter(void)
Get the cached value of $maildir_field_delimiter.
Convenience wrapper for the core headers.
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:213
void email_free(struct Email **ptr)
Free an Email.
Definition email.c:46
Structs that make up an email.
void maildir_edata_free(void **ptr)
Free the private Email data - Implements Email::edata_free() -.
Definition edata.c:38
struct HeaderCache * hcache_open(const char *path, const char *folder, hcache_namer_t namer, bool create)
Multiplexor for StoreOps::open.
Definition hcache.c:476
int hcache_delete_email(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition hcache.c:744
void hcache_close(struct HeaderCache **ptr)
Multiplexor for StoreOps::close.
Definition hcache.c:547
struct HCacheEntry hcache_fetch_email(struct HeaderCache *hc, const char *key, size_t keylen, uint32_t uidvalidity)
Multiplexor for StoreOps::fetch.
Definition hcache.c:567
int hcache_store_email(struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
Multiplexor for StoreOps::store.
Definition hcache.c:675
Header cache multiplexor.
struct MaildirEmailData * maildir_edata_new(void)
Create a new MaildirEmailData object.
Definition edata.c:53
Maildir-specific Email data.
static size_t maildir_hcache_keylen(const char *fn)
Calculate the length of the Maildir path.
Definition hcache.c:66
int maildir_hcache_store(struct HeaderCache *hc, struct Email *e)
Save an Email to the Header Cache.
Definition hcache.c:163
struct Email * maildir_hcache_read(struct HeaderCache *hc, struct Email *e, const char *fn)
Read an Email from the Header Cache.
Definition hcache.c:121
struct HeaderCache * maildir_hcache_open(struct Mailbox *m)
Open the Header Cache.
Definition hcache.c:104
int maildir_hcache_delete(struct HeaderCache *hc, struct Email *e)
Delete an Email from the Header Cache.
Definition hcache.c:89
void maildir_hcache_close(struct HeaderCache **ptr)
Close the Header Cache.
Definition hcache.c:77
static const char * maildir_hcache_key(struct Email *e)
Get the header cache key for an Email.
Definition hcache.c:46
void maildir_parse_flags(struct Email *e, const char *path)
Parse Maildir file flags.
Definition mailbox.c:82
Maildir Mailbox.
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition string.c:429
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:500
#define NONULL(x)
Definition string2.h:44
The envelope/body of an email.
Definition email.h:39
void * edata
Driver-specific data.
Definition email.h:74
bool old
Email is seen, but unread.
Definition email.h:49
void(* edata_free)(void **ptr)
Definition email.h:90
char * path
Path of Email (for local Mailboxes)
Definition email.h:70
Wrapper for Email retrieved from the header cache.
Definition lib.h:100
uint32_t uidvalidity
IMAP-specific UIDVALIDITY.
Definition lib.h:101
struct Email * email
Retrieved email.
Definition lib.h:103
Header Cache.
Definition lib.h:87
A mailbox.
Definition mailbox.h:78
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49