]> granicus.if.org Git - neomutt/commitdiff
Convert hcache_open to use buffer pool
authorKevin McCarthy <kevin@8t8.us>
Sat, 28 Sep 2019 01:12:27 +0000 (18:12 -0700)
committerRichard Russon <rich@flatcap.org>
Tue, 8 Oct 2019 16:43:58 +0000 (17:43 +0100)
Change the namer function and mutt_hcache_per_folder to operate on a
buffer parameter.

Co-authored-by: Richard Russon <rich@flatcap.org>
hcache/hcache.c
hcache/hcache.h
imap/util.c
nntp/newsrc.c
pop/pop.c

index ea936704f2531016403c4947b9ded4a7bfd20f95..ce95175f98b3a21a13f1a3c8cb98329e1ab4585d 100644 (file)
@@ -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;
 }
 
 /**
index 30835d5889cdfd33134b070c8de2d2542f388d1c..71bca2c9218170405c544ac874f1ef35bd20e2b3 100644 (file)
@@ -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;
index d880d5b7631ed01bea6ec8cc58be87c59352b333..d15ad627da82efb7b46d485992b949fbc4c216ba 100644 (file)
@@ -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);
 }
 
 /**
index 9063ebc4a0f4fc956773cd1479a3931caf7afab1..8cdfb6641765ea2ae6e241b54f77e70e2b5c5acc 100644 (file)
@@ -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)
index 584f88d8b6faaa87062383f5fb9b73613623ebfe..74328d15ff2063adb34a73fac4e4493e51c97793 100644 (file)
--- 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);
 }
 
 /**