introduce got_opentempfd()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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);