]> granicus.if.org Git - neomutt/commitdiff
Convert hcache fetch and store raw to use buffer pool
authorKevin McCarthy <kevin@8t8.us>
Sat, 28 Sep 2019 02:21:26 +0000 (19:21 -0700)
committerRichard Russon <rich@flatcap.org>
Sat, 26 Oct 2019 22:55:43 +0000 (23:55 +0100)
Upstream-commit: https://gitlab.com/muttmua/mutt/commit/36ee8d39e2eed5d78fecc21c045fbdbe0953f233
Co-authored-by: Richard Russon <rich@flatcap.org>
hcache/hcache.c
hcache/kc.c

index 18b713ca382f9ee46f91c9f29a946cd919f2de9f..f9f3670ca46194d961e6ca9e0ba1eda9b11f1b71 100644 (file)
@@ -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;
 }
 
 /**
index e01381553978c6db66c8d0bc43dc619e7a320a3c..a8e8316561f159a15f9653a45909109852ab3f4e 100644 (file)
  */
 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;
 }
 
 /**