Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f978b74

Browse files
committed
compat: Move mkstemp to the POSIX compat layer
1 parent 3ef7d06 commit f978b74

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/fileops.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,10 @@ int git_futils_mktmp(char *path_out, const char *filename)
5555
strcpy(path_out, filename);
5656
strcat(path_out, "_git2_XXXXXX");
5757

58-
#if defined(_MSC_VER)
59-
/* FIXME: there may be race conditions when multi-threading
60-
* with the library */
61-
if (_mktemp_s(path_out, GIT_PATH_MAX) != 0)
62-
return git__throw(GIT_EOSERR, "Failed to make temporary file %s", path_out);
58+
if ((fd = p_mkstemp(path_out)) < 0)
59+
return git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out);
6360

64-
fd = p_creat(path_out, 0744);
65-
#else
66-
fd = mkstemp(path_out);
67-
#endif
68-
69-
return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out);
61+
return fd;
7062
}
7163

7264
int git_futils_creat_withpath(const char *path, int mode)

src/unix/posix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
#define p_fnmatch(p, s, f) fnmatch(p, s, f)
1414
#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a)
1515
#define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__)
16+
#define p_mkstemp(p) mkstemp(p)
1617

1718
#endif

src/win32/posix.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "posix.h"
22
#include "path.h"
33
#include <errno.h>
4+
#include <io.h>
45

56
int p_unlink(const char *path)
67
{
@@ -230,3 +231,19 @@ int p_snprintf(char *buffer, size_t count, const char *format, ...)
230231

231232
return r;
232233
}
234+
235+
int p_mkstemp(char *tmp_path)
236+
{
237+
int r;
238+
239+
#if defined(_MSC_VER)
240+
r = _mktemp_s(tmp_path, GIT_PATH_MAX);
241+
#else
242+
r = _mktemp(tmp_path);
243+
#endif
244+
245+
if (r != 0)
246+
return GIT_EOSERR;
247+
248+
return p_creat(tmp_path, 0744);
249+
}

src/win32/posix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ extern int p_hide_directory__w32(const char *path);
2525
extern char *p_realpath(const char *orig_path, char *buffer);
2626
extern int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr);
2727
extern int p_snprintf(char *buffer, size_t count, const char *format, ...) GIT_FORMAT_PRINTF(3, 4);
28+
extern int p_mkstemp(char *tmp_path);
2829

2930
#endif

0 commit comments

Comments
 (0)