Commit 80f4afe898106e3eae350da9d42ecaf3a7d19124

Stefan Sperling 2018-04-24T11:47:09

introduce got_opentempfd()

diff --git a/lib/got_lib_path.h b/lib/got_lib_path.h
index 53b63ef..d1ceebb 100644
--- a/lib/got_lib_path.h
+++ b/lib/got_lib_path.h
@@ -34,6 +34,10 @@ char *got_path_get_absolute(const char *);
  */
 char *got_path_normalize(const char *);
 
+/* Open a file descriptor to a new temporary file for writing.
+ * The file is not visible in the filesystem. */
+int got_opentempfd(void);
+
 /* Open a new temporary file for writing.
  * The file is not visible in the filesystem. */
 FILE *got_opentemp(void);
diff --git a/lib/path.c b/lib/path.c
index 8f96a5a..659eaea 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -82,21 +82,30 @@ got_path_segment_count(int *count, const char *path)
 	return NULL;
 }
 
-FILE *
-got_opentemp(void)
+int
+got_opentempfd(void)
 {
 	char name[PATH_MAX];
 	int fd;
-	FILE *f;
 
 	if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
-		return NULL;
+		return -1;
 
 	fd = mkstemp(name);
+	unlink(name);
+	return fd;
+}
+
+FILE *
+got_opentemp(void)
+{
+	int fd;
+	FILE *f;
+
+	fd = got_opentempfd();
 	if (fd < 0)
 		return NULL;
 
-	unlink(name);
 	f = fdopen(fd, "w+");
 	if (f == NULL) {
 		close(fd);