Commit f978b748 by Vicent Marti

compat: Move `mkstemp` to the POSIX compat layer

parent 3ef7d063
...@@ -55,18 +55,10 @@ int git_futils_mktmp(char *path_out, const char *filename) ...@@ -55,18 +55,10 @@ int git_futils_mktmp(char *path_out, const char *filename)
strcpy(path_out, filename); strcpy(path_out, filename);
strcat(path_out, "_git2_XXXXXX"); strcat(path_out, "_git2_XXXXXX");
#if defined(_MSC_VER) if ((fd = p_mkstemp(path_out)) < 0)
/* FIXME: there may be race conditions when multi-threading return git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out);
* 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);
fd = p_creat(path_out, 0744); return fd;
#else
fd = mkstemp(path_out);
#endif
return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out);
} }
int git_futils_creat_withpath(const char *path, int mode) int git_futils_creat_withpath(const char *path, int mode)
......
...@@ -13,5 +13,6 @@ ...@@ -13,5 +13,6 @@
#define p_fnmatch(p, s, f) fnmatch(p, s, f) #define p_fnmatch(p, s, f) fnmatch(p, s, f)
#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a) #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_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__)
#define p_mkstemp(p) mkstemp(p)
#endif #endif
#include "posix.h" #include "posix.h"
#include "path.h" #include "path.h"
#include <errno.h> #include <errno.h>
#include <io.h>
int p_unlink(const char *path) int p_unlink(const char *path)
{ {
...@@ -230,3 +231,19 @@ int p_snprintf(char *buffer, size_t count, const char *format, ...) ...@@ -230,3 +231,19 @@ int p_snprintf(char *buffer, size_t count, const char *format, ...)
return r; 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);
}
...@@ -25,5 +25,6 @@ extern int p_hide_directory__w32(const char *path); ...@@ -25,5 +25,6 @@ extern int p_hide_directory__w32(const char *path);
extern char *p_realpath(const char *orig_path, char *buffer); 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_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_snprintf(char *buffer, size_t count, const char *format, ...) GIT_FORMAT_PRINTF(3, 4);
extern int p_mkstemp(char *tmp_path);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment