git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/5] wrapper: add git_mkdtemp()

From
René Scharfe <l.s.r@web.de>
Date
Dec 6, 2025, 13:27 UTC
Message-ID
<8045d953-2cee-4ffd-b3d2-cbf732d5c839@web.de>
In-Reply-To
<64e62623-b911-4ddd-a481-05191853c0a6@web.de>

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>
---
 wrapper.c | 21 +++++++++++++++++++--
 wrapper.h |  2 ++
 2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/wrapper.c b/wrapper.c
index d5976b3e7e..b794fb20e7 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -429,7 +429,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"
@@ -471,7 +475,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;
 		/*
@@ -486,6 +493,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 */
diff --git a/wrapper.h b/wrapper.h
index 44a8597ac3..15ac3bab6e 100644
--- a/wrapper.h
+++ b/wrapper.h
@@ -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);
-- 
2.52.0
Previous: René ScharfeNext: René Scharfe
Message 14 of 19 in “ban mktemp(3)”
  1. 0/4 ban mktemp(3)René Scharfe, Dec 3, 2025
  2. 1/4 wrapper: add git_mkdtemp()René Scharfe, Dec 3, 2025
  3. Chris TorekDec 4, 2025
  4. Junio C HamanoDec 5, 2025
  5. 2/4 compat: use git_mkdtemp()René Scharfe, Dec 3, 2025
  6. Jeff KingDec 3, 2025
  7. René ScharfeDec 5, 2025
  8. Jeff KingDec 6, 2025
  9. Junio C HamanoDec 5, 2025
  10. 3/4 compat: remove mingw_mktemp()René Scharfe, Dec 3, 2025
  11. 4/4 banned.h: ban mktemp(3)René Scharfe, Dec 3, 2025
  12. Jeff KingDec 3, 2025
  13. 0/5 ban mktemp(3)René Scharfe, Dec 6, 2025
  14. 1/5 wrapper: add git_mkdtemp()René Scharfe, Dec 6, 2025
  15. 2/5 compat: use git_mkdtemp()René Scharfe, Dec 6, 2025
  16. 3/5 compat: remove mingw_mktemp()René Scharfe, Dec 6, 2025
  17. 4/5 banned.h: ban mktemp(3)René Scharfe, Dec 6, 2025
  18. 5/5 compat: remove gitmkdtemp()René Scharfe, Dec 6, 2025
  19. Jeff KingDec 8, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.