]> granicus.if.org Git - neomutt/commitdiff
Add mutt_file_mkstemp() to create temp file using mkstemp()
authorWill Curry <will@robotican.org>
Wed, 18 Apr 2018 03:31:03 +0000 (22:31 -0500)
committerRichard Russon <rich@flatcap.org>
Sat, 21 Apr 2018 12:19:45 +0000 (13:19 +0100)
Relocated Tmpdir from globals.h to mutt library.

globals.h
mutt/file.c
mutt/file.h

index d5ca5093bec3dba558a0e661e269f43568b31127..ee574833fd19b19dff9ef05507db819760cdc590 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -219,7 +219,6 @@ WHERE char *Spoolfile;
 WHERE char *SpamSeparator;
 WHERE struct MbTable *StatusChars;
 WHERE char *StatusFormat;
-WHERE char *Tmpdir;
 WHERE struct MbTable *ToChars;
 WHERE struct MbTable *FlagChars;
 WHERE char *Trash;
index b58cec6b648aab19e3860ce5545887fbad00eed1..74c2a8a19b4f0b5a659d30a3759d9d829cd140ce 100644 (file)
@@ -47,6 +47,8 @@
 #include "message.h"
 #include "string2.h"
 
+char *Tmpdir; /**< Temporary directory path */
+
 /* these characters must be escaped in regular expressions */
 static const char rx_special_chars[] = "^.[$()|*+?{\\";
 
@@ -849,6 +851,37 @@ int mutt_file_mkdir(const char *path, mode_t mode)
   return 0;
 }
 
+/**
+ * mutt_file_mkstemp_full - Create temporary file safely
+ * @param file Temp filename
+ * @param line Line number of caller
+ * @param func Function name
+ * @retval ptr FILE handle
+ * @retval NULL Error, see errno
+ *
+ * Create and immediately unlink a temp file using mkstemp().
+ */
+FILE *mutt_file_mkstemp_full(const char *file, int line, const char *func)
+{
+  char name[PATH_MAX];
+
+  int n = snprintf(name, sizeof(name), "%s/neomutt-XXXXXX", NONULL(Tmpdir));
+  if (n < 0)
+    return NULL;
+
+  int fd = mkstemp(name);
+  if (fd == -1)
+    return NULL;
+
+  FILE *fp = fdopen(fd, "w+");
+
+  if (unlink(name) && (errno != ENOENT))
+    return NULL;
+
+  MuttLogger(0, file, line, func, 1, "created temp file '%s'\n", name);
+  return fp;
+}
+
 /**
  * mutt_file_decrease_mtime - Decrease a file's modification time by 1 second
  * @param f  Filename
index f6da93bde2023e8977e1da3b76909dc7ff59b499..f83e1ee6d004fa918fc964d93e43162d6234f1dd 100644 (file)
@@ -28,6 +28,7 @@
 #include <time.h>
 
 struct stat;
+extern char *Tmpdir;
 
 /* Flags for mutt_file_read_line() */
 #define MUTT_CONT (1 << 0) /**< \-continuation */
@@ -51,6 +52,8 @@ 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__)
 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);