"GOT", but the "O" is a cute, smiling sun Index | Thread

From:
Stefan Sperling <stsp@stsp.name>
Subject:
move got_opentempfd() call out of got_pack_create()
To:
gameoftrees@openbsd.org
Date:
Sat, 15 Oct 2022 18:02:22 +0200

Download raw body.

Future gotd(8) needs to run got_pack_create() in a chroot environment,
so we can no longer open new temporary files inside got_pack_create().

This patch must be applied on top of my previous patch to make
got_pack_create() write to a file descriptor.

diff 6fcead43e92195e0e3bf9fd73237f18a870410a6 53e2255ccbdc9dbdf8bd3a1fe4333f346c65ccb1
commit - 6fcead43e92195e0e3bf9fd73237f18a870410a6
commit + 53e2255ccbdc9dbdf8bd3a1fe4333f346c65ccb1
blob - 699f13578180bc1e991c1ced4b845dda7fa3a633
blob + bc54ae58a9ac550958c259f684367576576d5209
--- lib/got_lib_pack_create.h
+++ lib/got_lib_pack_create.h
@@ -22,7 +22,7 @@ const struct got_error *got_pack_create(uint8_t *pack_
  * be pre-allocated by the caller with at least SHA1_DIGEST_LENGTH bytes.
  */
 const struct got_error *got_pack_create(uint8_t *pack_sha1, int packfd,
-    struct got_object_id **theirs, int ntheirs,
+    FILE *delta_cache, struct got_object_id **theirs, int ntheirs,
     struct got_object_id **ours, int nours,
     struct got_repository *repo, int loose_obj_only, int allow_empty,
     got_pack_progress_cb progress_cb, void *progress_arg,
blob - 4b2c3294522e2a180c7438c962c007f6b988e399
blob + a3679a51361ead3fefdf763047f2b5756aaa45ee
--- lib/pack_create.c
+++ lib/pack_create.c
@@ -43,7 +43,6 @@
 #include "got_path.h"
 #include "got_reference.h"
 #include "got_repository_admin.h"
-#include "got_opentemp.h"
 
 #include "got_lib_deltify.h"
 #include "got_lib_delta.h"
@@ -2510,7 +2509,7 @@ got_pack_create(uint8_t *packsha1, int packfd,
 }
 
 const struct got_error *
-got_pack_create(uint8_t *packsha1, int packfd,
+got_pack_create(uint8_t *packsha1, int packfd, FILE *delta_cache,
     struct got_object_id **theirs, int ntheirs,
     struct got_object_id **ours, int nours,
     struct got_repository *repo, int loose_obj_only, int allow_empty,
@@ -2519,7 +2518,6 @@ got_pack_create(uint8_t *packsha1, int packfd,
 {
 	const struct got_error *err;
 	int delta_cache_fd = -1;
-	FILE *delta_cache = NULL;
 	struct got_object_idset *idset;
 	struct got_ratelimit rl;
 	struct got_pack_metavec deltify, reuse;
@@ -2556,9 +2554,9 @@ got_pack_create(uint8_t *packsha1, int packfd,
 		goto done;
 	}
 
-	delta_cache_fd = got_opentempfd();
+	delta_cache_fd = dup(fileno(delta_cache));
 	if (delta_cache_fd == -1) {
-		err = got_error_from_errno("got_opentemp");
+		err = got_error_from_errno("dup");
 		goto done;
 	}
 
@@ -2576,13 +2574,6 @@ got_pack_create(uint8_t *packsha1, int packfd,
 	if (err)
 		goto done;
 
-	delta_cache = fdopen(delta_cache_fd, "a+");
-	if (delta_cache == NULL) {
-		err = got_error_from_errno("fdopen");
-		goto done;
-	}
-	delta_cache_fd = -1;
-
 	if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
 		err = got_error_from_errno("fseeko");
 		goto done;
@@ -2627,7 +2618,5 @@ done:
 	got_object_idset_free(idset);
 	if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
-	if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
-		err = got_error_from_errno("fclose");
 	return err;
 }
blob - 77fcb6b22fcd226378437eda19e4fe0c96478ada
blob + d6850e32bdb9622e2e43e0854788c80dadefc6a9
--- lib/repository_admin.c
+++ lib/repository_admin.c
@@ -154,6 +154,7 @@ got_repo_pack_objects(FILE **packfile, struct got_obje
 	int nours = 0, ntheirs = 0, packfd = -1, i;
 	char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
 	char *sha1_str = NULL;
+	FILE *delta_cache = NULL;
 
 	*packfile = NULL;
 	*pack_hash = NULL;
@@ -172,6 +173,12 @@ got_repo_pack_objects(FILE **packfile, struct got_obje
 		goto done;
 	}
 
+	delta_cache = got_opentemp();
+	if (delta_cache == NULL) {
+		err = got_error_from_errno("got_opentemp");
+		goto done;
+	}
+
 	err = get_reflist_object_ids(&ours, &nours,
 	    (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
 	    include_refs, repo, cancel_cb, cancel_arg);
@@ -198,9 +205,9 @@ got_repo_pack_objects(FILE **packfile, struct got_obje
 		goto done;
 	}
 
-	err = got_pack_create((*pack_hash)->sha1, packfd, theirs, ntheirs,
-	    ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
-	    cancel_cb, cancel_arg);
+	err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
+	    theirs, ntheirs, ours, nours, repo, loose_obj_only, 0,
+	    progress_cb, progress_arg, cancel_cb, cancel_arg);
 	if (err)
 		goto done;
 
@@ -241,6 +248,8 @@ done:
 	free(theirs);
 	if (packfd != -1 && close(packfd) == -1 && err == NULL)
 		err = got_error_from_errno2("close", packfile_path);
+	if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
+		err = got_error_from_errno("fclose");
 	if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
 		err = got_error_from_errno2("unlink", tmpfile_path);
 	free(tmpfile_path);
blob - 4393fbc3fe2bb713de61b06db047c3fcff6de871
blob + 999423dd36a6b128a64253ae2261a4d4aadb3796
--- lib/send.c
+++ lib/send.c
@@ -346,6 +346,7 @@ got_send_pack(const char *remote_name, struct got_path
 	struct pack_progress_arg ppa;
 	uint8_t packsha1[SHA1_DIGEST_LENGTH];
 	int packfd = -1;
+	FILE *delta_cache = NULL;
 
 	TAILQ_INIT(&refs);
 	TAILQ_INIT(&have_refs);
@@ -442,6 +443,12 @@ got_send_pack(const char *remote_name, struct got_path
 		goto done;
 	}
 
+	delta_cache = got_opentemp();
+	if (delta_cache == NULL) {
+		err = got_error_from_errno("got_opentemp");
+		goto done;
+	}
+
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
 		err = got_error_from_errno("socketpair");
 		goto done;
@@ -636,9 +643,9 @@ got_send_pack(const char *remote_name, struct got_path
 		memset(&ppa, 0, sizeof(ppa));
 		ppa.progress_cb = progress_cb;
 		ppa.progress_arg = progress_arg;
-		err = got_pack_create(packsha1, packfd, their_ids, ntheirs,
-		    our_ids, nours, repo, 0, 1, pack_progress, &ppa,
-		    cancel_cb, cancel_arg);
+		err = got_pack_create(packsha1, packfd, delta_cache,
+		    their_ids, ntheirs, our_ids, nours, repo, 0, 1,
+		    pack_progress, &ppa, cancel_cb, cancel_arg);
 		if (err)
 			goto done;
 
@@ -708,6 +715,8 @@ done:
 	}
 	if (packfd != -1 && close(packfd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
+	if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
+		err = got_error_from_errno("fclose");
 	if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	if (npackfd != -1 && close(npackfd) == -1 && err == NULL)