From: Will Curry Date: Wed, 18 Apr 2018 03:31:03 +0000 (-0500) Subject: Add mutt_file_mkstemp() to create temp file using mkstemp() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=96ee093aa1a0ca93844cf804c7810a3c635a8949;p=neomutt Add mutt_file_mkstemp() to create temp file using mkstemp() Relocated Tmpdir from globals.h to mutt library. --- diff --git a/globals.h b/globals.h index d5ca5093b..ee574833f 100644 --- 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; diff --git a/mutt/file.c b/mutt/file.c index b58cec6b6..74c2a8a19 100644 --- a/mutt/file.c +++ b/mutt/file.c @@ -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 diff --git a/mutt/file.h b/mutt/file.h index f6da93bde..f83e1ee6d 100644 --- a/mutt/file.h +++ b/mutt/file.h @@ -28,6 +28,7 @@ #include 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);