wrapper: add git_mkdtemp()

Extend git_mkstemps_mode() to optionally call mkdir(2) instead of
open(2), then use that ability to create a mkdtemp(3) replacement,
git_mkdtemp().  We'll start using it in the next commit.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe
2025-12-06 14:27:39 +01:00
committed by Junio C Hamano
parent 9a2fb147f2
commit e1ecf0dd68
2 changed files with 21 additions and 2 deletions

View File

@@ -446,7 +446,11 @@ int xmkstemp(char *filename_template)
#undef TMP_MAX
#define TMP_MAX 16384
int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
/*
* Returns -1 on error, 0 if it created a directory, or an open file
* descriptor to the created regular file.
*/
static int git_mkdstemps_mode(char *pattern, int suffix_len, int mode, bool dir)
{
static const char letters[] =
"abcdefghijklmnopqrstuvwxyz"
@@ -488,7 +492,10 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
v /= num_letters;
}
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
if (dir)
fd = mkdir(pattern, mode);
else
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
if (fd >= 0)
return fd;
/*
@@ -503,6 +510,16 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
return -1;
}
char *git_mkdtemp(char *pattern)
{
return git_mkdstemps_mode(pattern, 0, 0700, true) ? NULL : pattern;
}
int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
{
return git_mkdstemps_mode(pattern, suffix_len, mode, false);
}
int git_mkstemp_mode(char *pattern, int mode)
{
/* mkstemp is just mkstemps with no suffix */

View File

@@ -37,6 +37,8 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...);
int xgethostname(char *buf, size_t len);
char *git_mkdtemp(char *pattern);
/* set default permissions by passing mode arguments to open(2) */
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
int git_mkstemp_mode(char *pattern, int mode);