From f978b748bb50beb0ccbebc3aa118ad289e4c9cba Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 30 Aug 2011 13:34:14 +0200 Subject: [PATCH] compat: Move `mkstemp` to the POSIX compat layer --- src/fileops.c | 14 +++----------- src/unix/posix.h | 1 + src/win32/posix.c | 17 +++++++++++++++++ src/win32/posix.h | 1 + 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/fileops.c b/src/fileops.c index c7fddc623..d7413a138 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -55,18 +55,10 @@ int git_futils_mktmp(char *path_out, const char *filename) strcpy(path_out, filename); strcat(path_out, "_git2_XXXXXX"); -#if defined(_MSC_VER) - /* FIXME: there may be race conditions when multi-threading - * with the library */ - if (_mktemp_s(path_out, GIT_PATH_MAX) != 0) - return git__throw(GIT_EOSERR, "Failed to make temporary file %s", path_out); + if ((fd = p_mkstemp(path_out)) < 0) + return git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out); - fd = p_creat(path_out, 0744); -#else - fd = mkstemp(path_out); -#endif - - return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out); + return fd; } int git_futils_creat_withpath(const char *path, int mode) diff --git a/src/unix/posix.h b/src/unix/posix.h index f355844d9..a49a5cfe7 100644 --- a/src/unix/posix.h +++ b/src/unix/posix.h @@ -13,5 +13,6 @@ #define p_fnmatch(p, s, f) fnmatch(p, s, f) #define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a) #define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__) +#define p_mkstemp(p) mkstemp(p) #endif diff --git a/src/win32/posix.c b/src/win32/posix.c index 0e9d3f0f4..aac56ce84 100644 --- a/src/win32/posix.c +++ b/src/win32/posix.c @@ -1,6 +1,7 @@ #include "posix.h" #include "path.h" #include +#include int p_unlink(const char *path) { @@ -230,3 +231,19 @@ int p_snprintf(char *buffer, size_t count, const char *format, ...) return r; } + +int p_mkstemp(char *tmp_path) +{ + int r; + +#if defined(_MSC_VER) + r = _mktemp_s(tmp_path, GIT_PATH_MAX); +#else + r = _mktemp(tmp_path); +#endif + + if (r != 0) + return GIT_EOSERR; + + return p_creat(tmp_path, 0744); +} diff --git a/src/win32/posix.h b/src/win32/posix.h index 536089e44..28d978959 100644 --- a/src/win32/posix.h +++ b/src/win32/posix.h @@ -25,5 +25,6 @@ extern int p_hide_directory__w32(const char *path); extern char *p_realpath(const char *orig_path, char *buffer); extern int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr); extern int p_snprintf(char *buffer, size_t count, const char *format, ...) GIT_FORMAT_PRINTF(3, 4); +extern int p_mkstemp(char *tmp_path); #endif