From 60809acdebeba584877ca8e76efb324ed87d46df Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Fri, 27 Sep 2019 18:12:27 -0700 Subject: [PATCH] Convert hcache_open to use buffer pool Change the namer function and mutt_hcache_per_folder to operate on a buffer parameter. Co-authored-by: Richard Russon --- hcache/hcache.c | 64 +++++++++++++++++++++++-------------------------- hcache/hcache.h | 4 +--- imap/util.c | 4 ++-- nntp/newsrc.c | 22 ++++++++--------- pop/pop.c | 4 ++-- 5 files changed, 45 insertions(+), 53 deletions(-) diff --git a/hcache/hcache.c b/hcache/hcache.c index ea936704f..ce95175f9 100644 --- a/hcache/hcache.c +++ b/hcache/hcache.c @@ -163,6 +163,7 @@ static bool create_hcache_dir(const char *path) /** * hcache_per_folder - Generate the hcache pathname + * @param hcpath Buffer for the result * @param path Base directory, from $header_cache * @param folder Mailbox name (including protocol) * @param namer Callback to generate database filename - Implements ::hcache_namer_t @@ -183,9 +184,9 @@ static bool create_hcache_dir(const char *path) * If @a path has a trailing '/' it is assumed to be a directory. * Otherwise @a path is assumed to be a file. */ -static const char *hcache_per_folder(const char *path, const char *folder, hcache_namer_t namer) +static void hcache_per_folder(struct Buffer *hcpath, const char *path, + const char *folder, hcache_namer_t namer) { - static char hcpath[PATH_MAX + 64]; struct stat sb; int plen = mutt_str_strlen(path); @@ -195,38 +196,32 @@ static const char *hcache_per_folder(const char *path, const char *folder, hcach if (((rc == 0) && !S_ISDIR(sb.st_mode)) || ((rc == -1) && !slash)) { /* An existing file or a non-existing path not ending with a slash */ - mutt_encode_path(hcpath, sizeof(hcpath), path); - return hcpath; + mutt_buffer_encode_path(hcpath, path); + return; } /* We have a directory - no matter whether it exists, or not */ + struct Buffer *hcfile = mutt_buffer_pool_get(); if (namer) { - /* We have a mailbox-specific namer function */ - snprintf(hcpath, sizeof(hcpath), "%s%s", path, slash ? "" : "/"); - if (!slash) - plen++; - - rc = namer(folder, hcpath + plen, sizeof(hcpath) - plen); + namer(folder, hcfile); } else { unsigned char m[16]; /* binary md5sum */ - char name[PATH_MAX]; - snprintf(name, sizeof(name), "%s|%s", hcache_get_ops()->name, folder); - mutt_md5(name, m); - mutt_md5_toascii(m, name); - rc = snprintf(hcpath, sizeof(hcpath), "%s%s%s", path, slash ? "" : "/", name); + struct Buffer *name = mutt_buffer_pool_get(); + mutt_buffer_printf(name, "%s|%s", hcache_get_ops()->name, folder); + mutt_md5(mutt_b2s(name), m); + mutt_md5_toascii(m, name->data); + mutt_buffer_printf(hcfile, "%s%s%s", path, slash ? "" : "/", name); } - mutt_encode_path(hcpath, sizeof(hcpath), hcpath); - - if (rc < 0) /* namer or fprintf failed.. should not happen */ - return path; + mutt_buffer_encode_path(hcpath, mutt_b2s(hcpath)); + mutt_buffer_concat_path(hcpath, path, mutt_b2s(hcfile)); + mutt_buffer_pool_release(&hcfile); - create_hcache_dir(hcpath); - return hcpath; + create_hcache_dir(mutt_b2s(hcpath)); } /** @@ -302,25 +297,26 @@ header_cache_t *mutt_hcache_open(const char *path, const char *folder, hcache_na return NULL; } - path = hcache_per_folder(path, hc->folder, namer); + struct Buffer *hcpath = mutt_buffer_pool_get(); + hcache_per_folder(hcpath, path, hc->folder, namer); - hc->ctx = ops->open(path); - if (hc->ctx) - return hc; - else + hc->ctx = ops->open(mutt_b2s(hcpath)); + if (!hc->ctx) { /* remove a possibly incompatible version */ - if (unlink(path) == 0) + if (unlink(mutt_b2s(hcpath)) == 0) { - hc->ctx = ops->open(path); - if (hc->ctx) - return hc; + hc->ctx = ops->open(mutt_b2s(hcpath)); + if (!hc->ctx) + { + FREE(&hc->folder); + FREE(&hc); + } } - FREE(&hc->folder); - FREE(&hc); - - return NULL; } + + mutt_buffer_pool_release(&hcpath); + return hc; } /** diff --git a/hcache/hcache.h b/hcache/hcache.h index 30835d588..71bca2c92 100644 --- a/hcache/hcache.h +++ b/hcache/hcache.h @@ -76,10 +76,8 @@ typedef struct EmailCache header_cache_t; * typedef hcache_namer_t - Prototype for function to compose hcache file names * @param path Path of message * @param dest Buffer for filename - * @param destlen Length of buffer - * @retval num Characters written to buffer */ -typedef int (*hcache_namer_t)(const char *path, char *dest, size_t dlen); +typedef void (*hcache_namer_t)(const char *path, struct Buffer *dest); /* These Config Variables are only used in hcache/hcache.c */ extern char *C_HeaderCacheBackend; diff --git a/imap/util.c b/imap/util.c index d880d5b76..d15ad627d 100644 --- a/imap/util.c +++ b/imap/util.c @@ -400,9 +400,9 @@ static void imap_msn_index_to_uid_seqset(struct Buffer *buf, struct ImapMboxData /** * imap_hcache_namer - Generate a filename for the header cache - Implements ::hcache_namer_t */ -static int imap_hcache_namer(const char *path, char *dest, size_t dlen) +static void imap_hcache_namer(const char *path, struct Buffer *dest) { - return snprintf(dest, dlen, "%s.hcache", path); + mutt_buffer_printf(dest, "%s.hcache", path); } /** diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 9063ebc4a..8cdfb6641 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -679,20 +679,17 @@ int nntp_active_save_cache(struct NntpAccountData *adata) /** * nntp_hcache_namer - Compose hcache file names - Implements ::hcache_namer_t */ -static int nntp_hcache_namer(const char *path, char *dest, size_t destlen) +static void nntp_hcache_namer(const char *path, struct Buffer *dest) { - int count = snprintf(dest, destlen, "%s.hcache", path); + mutt_buffer_printf(dest, "%s.hcache", path); /* Strip out any directories in the path */ - char *first = strchr(dest, '/'); - char *last = strrchr(dest, '/'); + char *first = strchr(mutt_b2s(dest), '/'); + char *last = strrchr(mutt_b2s(dest), '/'); if (first && last && (last > first)) { memmove(first, last, strlen(last) + 1); - count -= (last - first); } - - return count; } /** @@ -805,12 +802,13 @@ void nntp_delete_group_cache(struct NntpMboxData *mdata) return; #ifdef USE_HCACHE - char file[PATH_MAX]; - nntp_hcache_namer(mdata->group, file, sizeof(file)); - cache_expand(file, sizeof(file), &mdata->adata->conn->account, file); - unlink(file); + struct Buffer file = mutt_buffer_make(PATH_MAX); + nntp_hcache_namer(mdata->group, &file); + cache_expand(file.data, file.dsize, &mdata->adata->conn->account, mutt_b2s(&file)); + unlink(mutt_b2s(&file)); mdata->last_cached = 0; - mutt_debug(LL_DEBUG2, "%s\n", file); + mutt_debug(LL_DEBUG2, "%s\n", mutt_b2s(&file)); + mutt_buffer_dealloc(&file); #endif if (!mdata->bcache) diff --git a/pop/pop.c b/pop/pop.c index 584f88d8b..74328d15f 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -336,9 +336,9 @@ static int msg_cache_check(const char *id, struct BodyCache *bcache, void *data) /** * pop_hcache_namer - Create a header cache filename for a POP mailbox - Implements ::hcache_namer_t */ -static int pop_hcache_namer(const char *path, char *dest, size_t destlen) +static void pop_hcache_namer(const char *path, struct Buffer *dest) { - return snprintf(dest, destlen, "%s." HC_FEXT, path); + mutt_buffer_printf(dest, "%s." HC_FEXT, path); } /** -- 2.40.0