use got_opentemp_named_fd() instead of open() for tempfiles in got_clone()
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 56 57 58 59 60
diff --git a/lib/fetch.c b/lib/fetch.c
index be04a7d..d1b0036 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -47,6 +47,7 @@
#include "got_cancel.h"
#include "got_worktree.h"
#include "got_object.h"
+#include "got_opentemp.h"
#include "got_lib_delta.h"
#include "got_lib_inflate.h"
@@ -252,11 +253,12 @@ got_clone(char *uri, char *branch_filter, char *dirname)
char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
char repo[GOT_REPOMAX], path[GOT_PATHMAX];
int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
- int packfd, npackfd, idxfd, nidxfd, status;
+ int packfd = -1, npackfd, idxfd = -1, nidxfd, status;
struct got_object_id packhash;
const struct got_error *err;
struct imsgbuf ibuf;
pid_t pid;
+ char *packpath = NULL, *idxpath = NULL;
fetchfd = -1;
if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
@@ -270,15 +272,17 @@ got_clone(char *uri, char *branch_filter, char *dirname)
return got_error_from_errno("enter new repo");
if (mkpath(".git/objects/pack") == -1)
return got_error_from_errno("mkpath");
- packfd = open(".git/objects/pack/fetching.pack", O_CREAT|O_RDWR, 0644);
- if (packfd == -1)
- return got_error_from_errno("open pack");
+ err = got_opentemp_named_fd(&packpath, &packfd,
+ ".git/objects/pack/fetching.pack");
+ if (err)
+ return err;
npackfd = dup(packfd);
if (npackfd == -1)
return got_error_from_errno("dup");
- idxfd = open(".git/objects/pack/fetching.idx", O_CREAT|O_RDWR, 0644);
- if (idxfd == -1)
- return got_error_from_errno("open pack");
+ err = got_opentemp_named_fd(&idxpath, &idxfd,
+ ".git/objects/pack/fetching.idx");
+ if (err)
+ return err;
nidxfd = dup(idxfd);
if (nidxfd == -1)
return got_error_from_errno("dup");
@@ -355,6 +359,9 @@ got_clone(char *uri, char *branch_filter, char *dirname)
return got_error_from_errno("child exit");
+ free(packpath);
+ free(idxpath);
+
return NULL;
}