]> granicus.if.org Git - neomutt/commitdiff
Partial solution to #1165 (#1176)
authorLars Kellogg-Stedman <lars@oddbit.com>
Tue, 15 May 2018 16:43:35 +0000 (12:43 -0400)
committerRichard Russon <rich@flatcap.org>
Tue, 15 May 2018 16:43:35 +0000 (17:43 +0100)
* mutt/file.c: allow mutt_file_mkstemp to return a filename

modify mutt_file_mkstemp so that a caller may pass in a character
buffer that will be filled with the temporary filename. When a
non-null buffer is provided, mutt_file_mkstemp will *not* unlink the
temporary file and will leave cleanup to the caller.

Closes: #1165
attach.c
editmsg.c
mutt/file.c
mutt/file.h

index 186e1e01df0c0061fc4d66fb5e240b00e64ee1d2..db6a693faf270b3cb51e5eda302378d64e3cd2d8 100644 (file)
--- a/attach.c
+++ b/attach.c
@@ -176,8 +176,7 @@ int mutt_compose_attachment(struct Body *a)
              * copying the file back */
             fseeko(fp, b->offset, SEEK_SET);
             mutt_body_free(&b);
-            mutt_mktemp(tempfile, sizeof(tempfile));
-            FILE *tfp = mutt_file_fopen(tempfile, "w");
+           FILE *tfp = mutt_file_mkstemp_name(tempfile, sizeof(tempfile));
             if (!tfp)
             {
               mutt_perror(_("Failure to open file to strip headers."));
index c98025a08b4911d4e73562db977dac923d8baf85..b7ec2a428f36be532a46ad2f37cfe90e454975dc 100644 (file)
--- a/editmsg.c
+++ b/editmsg.c
@@ -73,11 +73,13 @@ static int edit_or_view_one_message(bool edit, struct Context *ctx, struct Heade
   struct Message *msg = NULL;
 
   FILE *fp = NULL;
+  FILE *tfp = NULL;
 
   struct stat sb;
   time_t mtime = 0;
 
-  mutt_mktemp(tmp, sizeof(tmp));
+  tfp = mutt_file_mkstemp_name(tmp, sizeof(tmp));
+  mutt_file_mkstemp_finish(tfp, tmp);
 
   omagic = MboxType;
   MboxType = MUTT_MBOX;
index 1cf3f91e76c30fe7f4dd8cc9f85a1c19ab66cf94..e47c72805e60a942b01628472a919cfecfa3f7ef 100644 (file)
@@ -861,11 +861,17 @@ int mutt_file_mkdir(const char *path, mode_t mode)
  *
  * Create and immediately unlink a temp file using mkstemp().
  */
-FILE *mutt_file_mkstemp_full(const char *file, int line, const char *func)
+FILE *mutt_file_mkstemp_full(char *name, size_t namelen,
+    const char *file, int line, const char *func)
 {
-  char name[PATH_MAX];
+  char noname[PATH_MAX];
 
-  int n = snprintf(name, sizeof(name), "%s/neomutt-XXXXXX", NONULL(Tmpdir));
+  if (name == NULL) {
+    name = noname;
+    namelen = PATH_MAX;
+  }
+
+  int n = snprintf(name, namelen, "%s/neomutt-XXXXXX", NONULL(Tmpdir));
   if (n < 0)
     return NULL;
 
@@ -874,17 +880,27 @@ FILE *mutt_file_mkstemp_full(const char *file, int line, const char *func)
     return NULL;
 
   FILE *fp = fdopen(fd, "w+");
+  MuttLogger(0, file, line, func, 1, "created temp file '%s'\n", name);
 
-  if ((unlink(name) != 0) && (errno != ENOENT))
-  {
-    mutt_file_fclose(&fp);
-    return NULL;
+  if (name == noname) {
+    if ((unlink(name) != 0) && (errno != ENOENT))
+      return NULL;
+
+    MuttLogger(0, file, line, func, 1, "unlinked temp file '%s'\n", name);
   }
 
-  MuttLogger(0, file, line, func, 1, "created temp file '%s'\n", name);
   return fp;
 }
 
+int mutt_file_mkstemp_finish(FILE *fp, const char *name) {
+  if (name != NULL) {
+    if (unlink(name) && (errno != ENOENT))
+      return -1;
+  }
+
+  return mutt_file_fclose(&fp);
+}
+
 /**
  * mutt_file_decrease_mtime - Decrease a file's modification time by 1 second
  * @param f  Filename
index ad1ed571a545083117194a0fce0b9922100bf8e7..e73ba2f25fda9d7622c1408dfba6e525ef7ea55a 100644 (file)
@@ -52,8 +52,10 @@ FILE *      mutt_file_fopen(const char *path, const char *mode);
 int         mutt_file_fsync_close(FILE **f);
 int         mutt_file_lock(int fd, int excl, int timeout);
 int         mutt_file_mkdir(const char *path, mode_t mode);
-FILE *      mutt_file_mkstemp_full(const char *file, int line, const char *func);
-#define     mutt_file_mkstemp() mutt_file_mkstemp_full(__FILE__, __LINE__, __func__)
+FILE *      mutt_file_mkstemp_full(char *name, size_t namelen, const char *file, int line, const char *func);
+#define     mutt_file_mkstemp() mutt_file_mkstemp_full(NULL, 0, __FILE__, __LINE__, __func__)
+#define     mutt_file_mkstemp_name(name, namelen) mutt_file_mkstemp_full(name, namelen, __FILE__, __LINE__, __func__)
+int         mutt_file_mkstemp_finish(FILE *, const char *);
 int         mutt_file_open(const char *path, int flags);
 size_t      mutt_file_quote_filename(char *d, size_t l, const char *f);
 char *      mutt_file_read_keyword(const char *file, char *buffer, size_t buflen);