From: Kevin McCarthy Date: Sat, 28 Sep 2019 02:21:26 +0000 (-0700) Subject: Convert hcache fetch and store raw to use buffer pool X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=66fbc7da4cc72bc9523ad2956c8e8775b213cf6d;p=neomutt Convert hcache fetch and store raw to use buffer pool Upstream-commit: https://gitlab.com/muttmua/mutt/commit/36ee8d39e2eed5d78fecc21c045fbdbe0953f233 Co-authored-by: Richard Russon --- diff --git a/hcache/hcache.c b/hcache/hcache.c index 18b713ca3..f9f3670ca 100644 --- a/hcache/hcache.c +++ b/hcache/hcache.c @@ -143,22 +143,25 @@ static bool crc_matches(const char *d, unsigned int crc) */ static bool create_hcache_dir(const char *path) { - if (!path) + char *dir = mutt_str_strdup(path); + if (!dir) return false; - static char dir[PATH_MAX]; - mutt_str_strfcpy(dir, path, sizeof(dir)); - char *p = strrchr(dir, '/'); if (!p) + { + FREE(&dir); return true; + } *p = '\0'; - if (mutt_file_mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == 0) - return true; - mutt_error(_("Can't create %s: %s"), dir, strerror(errno)); - return false; + int rc = mutt_file_mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO); + if (rc != 0) + mutt_error(_("Can't create %s: %s"), dir, strerror(errno)); + + FREE(&dir); + return (rc == 0); } /** @@ -368,15 +371,18 @@ void *mutt_hcache_fetch(header_cache_t *hc, const char *key, size_t keylen) */ void *mutt_hcache_fetch_raw(header_cache_t *hc, const char *key, size_t keylen) { - char path[PATH_MAX]; const struct HcacheOps *ops = hcache_get_ops(); if (!hc || !ops) return NULL; - keylen = snprintf(path, sizeof(path), "%s%s", hc->folder, key); + struct Buffer path = mutt_buffer_make(1024); + + keylen = mutt_buffer_printf(&path, "%s%s", hc->folder, key); - return ops->fetch(hc->ctx, path, keylen); + void *blob = ops->fetch(hc->ctx, mutt_b2s(&path), keylen); + mutt_buffer_dealloc(&path); + return blob; } /** @@ -424,15 +430,18 @@ int mutt_hcache_store(header_cache_t *hc, const char *key, size_t keylen, int mutt_hcache_store_raw(header_cache_t *hc, const char *key, size_t keylen, void *data, size_t dlen) { - char path[PATH_MAX]; const struct HcacheOps *ops = hcache_get_ops(); if (!hc || !ops) return -1; - keylen = snprintf(path, sizeof(path), "%s%s", hc->folder, key); + struct Buffer path = mutt_buffer_make(1024); - return ops->store(hc->ctx, path, keylen, data, dlen); + keylen = mutt_buffer_printf(&path, "%s%s", hc->folder, key); + + int rc = ops->store(hc->ctx, mutt_b2s(&path), keylen, data, dlen); + mutt_buffer_dealloc(&path); + return rc; } /** @@ -440,15 +449,17 @@ int mutt_hcache_store_raw(header_cache_t *hc, const char *key, size_t keylen, */ int mutt_hcache_delete_header(header_cache_t *hc, const char *key, size_t keylen) { - char path[PATH_MAX]; const struct HcacheOps *ops = hcache_get_ops(); - if (!hc) return -1; - keylen = snprintf(path, sizeof(path), "%s%s", hc->folder, key); + struct Buffer path = mutt_buffer_make(1024); - return ops->delete_header(hc->ctx, path, keylen); + keylen = mutt_buffer_printf(&path, "%s%s", hc->folder, key); + + int rc = ops->delete_header(hc->ctx, mutt_b2s(&path), keylen); + mutt_buffer_dealloc(&path); + return rc; } /** diff --git a/hcache/kc.c b/hcache/kc.c index e01381553..a8e831656 100644 --- a/hcache/kc.c +++ b/hcache/kc.c @@ -42,30 +42,26 @@ */ static void *hcache_kyotocabinet_open(const char *path) { - char kcdbpath[PATH_MAX]; - int printfresult; - - printfresult = snprintf(kcdbpath, sizeof(kcdbpath), "%s#type=kct#opts=%s#rcomp=lex", - path, C_HeaderCacheCompress ? "lc" : "l"); - if ((printfresult < 0) || (printfresult >= sizeof(kcdbpath))) - { - return NULL; - } - KCDB *db = kcdbnew(); if (!db) return NULL; - if (kcdbopen(db, kcdbpath, KCOWRITER | KCOCREATE)) - return db; - else + struct Buffer kcdbpath = mutt_buffer_make(1024); + + mutt_buffer_printf(&kcdbpath, "%s#type=kct#opts=%s#rcomp=lex", path, + C_HeaderCacheCompress ? "lc" : "l"); + + if (!kcdbopen(db, mutt_b2s(&kcdbpath), KCOWRITER | KCOCREATE)) { int ecode = kcdbecode(db); - mutt_debug(LL_DEBUG2, "kcdbopen failed for %s: %s (ecode %d)\n", kcdbpath, - kcdbemsg(db), ecode); + mutt_debug(LL_DEBUG2, "kcdbopen failed for %s: %s (ecode %d)\n", + mutt_b2s(&kcdbpath), kcdbemsg(db), ecode); kcdbdel(db); - return NULL; + db = NULL; } + + mutt_buffer_dealloc(&kcdbpath); + return db; } /**