Commit f978b748bb50beb0ccbebc3aa118ad289e4c9cba

Vicent Marti 2011-08-30T13:34:14

compat: Move `mkstemp` to the POSIX compat layer

diff --git a/src/fileops.c b/src/fileops.c
index c7fddc6..d7413a1 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 f355844..a49a5cf 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 0e9d3f0..aac56ce 100644
--- a/src/win32/posix.c
+++ b/src/win32/posix.c
@@ -1,6 +1,7 @@
 #include "posix.h"
 #include "path.h"
 #include <errno.h>
+#include <io.h>
 
 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 536089e..28d9789 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