Commit Diff


commit - 9b4458b41088db703d890881c32dfd242efce4df
commit + 68036464ea72a92fd21d72135814faed6abad71a
blob - 0aed8754f53747ab02a5563d0a6435959c59c3d4
blob + bfee6f4277cb3046739bccf25fc842f391a9b670
--- lib/got_lib_pack.h
+++ lib/got_lib_pack.h
@@ -30,6 +30,8 @@ struct got_pack {
 
 struct got_packidx;
 
+const struct got_error *got_pack_start_privsep_child(struct got_pack *,
+    struct got_packidx *);
 const struct got_error *got_pack_close(struct got_pack *);
 
 const struct got_error *got_pack_parse_offset_delta(off_t *, size_t *,
blob - b4a0b763969633b6075afe7960a4a01da96d421e
blob + dda75c34856cd363ca246441656445d61b9d1b0b
--- lib/object.c
+++ lib/object.c
@@ -19,7 +19,6 @@
 #include <sys/uio.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
-#include <sys/resource.h>
 #include <sys/mman.h>
 
 #include <errno.h>
@@ -242,80 +241,6 @@ request_packed_object_raw(uint8_t **outbuf, off_t *siz
 		return err;
 
 	return NULL;
-}
-
-static void
-set_max_datasize(void)
-{
-	struct rlimit rl;
-
-	if (getrlimit(RLIMIT_DATA, &rl) != 0)
-		return;
-
-	rl.rlim_cur = rl.rlim_max;
-	setrlimit(RLIMIT_DATA, &rl);
-}
-
-static const struct got_error *
-start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
-{
-	const struct got_error *err = NULL;
-	int imsg_fds[2];
-	pid_t pid;
-	struct imsgbuf *ibuf;
-
-	ibuf = calloc(1, sizeof(*ibuf));
-	if (ibuf == NULL)
-		return got_error_from_errno("calloc");
-
-	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
-	if (pack->privsep_child == NULL) {
-		err = got_error_from_errno("calloc");
-		free(ibuf);
-		return err;
-	}
-	pack->child_has_tempfiles = 0;
-	pack->child_has_delta_outfd = 0;
-
-	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
-		err = got_error_from_errno("socketpair");
-		goto done;
-	}
-
-	pid = fork();
-	if (pid == -1) {
-		err = got_error_from_errno("fork");
-		goto done;
-	} else if (pid == 0) {
-		set_max_datasize();
-		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
-		    pack->path_packfile);
-		/* not reached */
-	}
-
-	if (close(imsg_fds[1]) == -1)
-		return got_error_from_errno("close");
-	pack->privsep_child->imsg_fd = imsg_fds[0];
-	pack->privsep_child->pid = pid;
-	imsg_init(ibuf, imsg_fds[0]);
-	pack->privsep_child->ibuf = ibuf;
-
-	err = got_privsep_init_pack_child(ibuf, pack, packidx);
-	if (err) {
-		const struct got_error *child_err;
-		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
-		child_err = got_privsep_wait_for_child(
-		    pack->privsep_child->pid);
-		if (child_err && err == NULL)
-			err = child_err;
-	}
-done:
-	if (err) {
-		free(ibuf);
-		free(pack->privsep_child);
-		pack->privsep_child = NULL;
-	}
-	return err;
 }
 
 static const struct got_error *
@@ -326,7 +251,7 @@ read_packed_object_privsep(struct got_object **obj,
 	const struct got_error *err = NULL;
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -342,7 +267,7 @@ read_packed_object_raw_privsep(uint8_t **outbuf, off_t
 	const struct got_error *err = NULL;
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -425,7 +350,7 @@ got_object_read_raw_delta(uint64_t *base_size, uint64_
 	}
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -450,55 +375,6 @@ got_object_read_raw_delta(uint64_t *base_size, uint64_
 	return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
 	    delta_compressed_size, delta_offset, delta_out_offset, base_id,
 	    pack->privsep_child->ibuf);
-}
-
-/*
- * XXX This function does not really belong in object.c. It is only here
- * because it needs start_pack_privsep_child(); relevant code should
- * probably be moved to pack.c/pack_create.c.
- */
-const struct got_error *
-got_object_prepare_delta_reuse(struct got_pack **pack,
-    struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
-{
-	const struct got_error *err = NULL;
-	char *path_packfile = NULL;
-
-	err = got_packidx_get_packfile_path(&path_packfile,
-	    packidx->path_packidx);
-	if (err)
-		return err;
-
-	*pack = got_repo_get_cached_pack(repo, path_packfile);
-	if (*pack == NULL) {
-		err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
-		if (err)
-			goto done;
-	}
-	if ((*pack)->privsep_child == NULL) {
-		err = start_pack_privsep_child(*pack, packidx);
-		if (err)
-			goto done;
-	}
-
-	if (!(*pack)->child_has_delta_outfd) {
-		int outfd_child;
-		outfd_child = dup(delta_outfd);
-		if (outfd_child == -1) {
-			err = got_error_from_errno("dup");
-			goto done;
-		}
-		err = got_privsep_send_raw_delta_outfd(
-		    (*pack)->privsep_child->ibuf, outfd_child);
-		if (err)
-			goto done;
-		(*pack)->child_has_delta_outfd = 1;
-	}
-
-	err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
-done:
-	free(path_packfile);
-	return err;
 }
 
 static const struct got_error *
@@ -840,7 +716,7 @@ read_packed_commit_privsep(struct got_commit_object **
 	if (pack->privsep_child)
 		return request_packed_commit(commit, pack, idx, id);
 
-	err = start_pack_privsep_child(pack, packidx);
+	err = got_pack_start_privsep_child(pack, packidx);
 	if (err)
 		return err;
 
@@ -1045,7 +921,7 @@ read_packed_tree_privsep(struct got_tree_object **tree
 	if (pack->privsep_child)
 		return request_packed_tree(tree, pack, idx, id);
 
-	err = start_pack_privsep_child(pack, packidx);
+	err = got_pack_start_privsep_child(pack, packidx);
 	if (err)
 		return err;
 
@@ -1368,7 +1244,7 @@ read_packed_blob_privsep(uint8_t **outbuf, size_t *siz
 	const struct got_error *err = NULL;
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -1744,7 +1620,7 @@ read_packed_tag_privsep(struct got_tag_object **tag,
 	if (pack->privsep_child)
 		return request_packed_tag(tag, pack, idx, id);
 
-	err = start_pack_privsep_child(pack, packidx);
+	err = got_pack_start_privsep_child(pack, packidx);
 	if (err)
 		return err;
 
@@ -2368,7 +2244,7 @@ got_traverse_packed_commits(struct got_object_id_queue
 	}
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			goto done;
 	}
@@ -2424,7 +2300,7 @@ got_object_enumerate(int *found_all_objects,
 	}
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			goto done;
 	}
blob - fb0db87c32da83d311bda6a69b946e48e3167f8f
blob + 054ae905f1c60527376ced496a88d48170382727
--- lib/pack.c
+++ lib/pack.c
@@ -18,6 +18,8 @@
 #include <sys/stat.h>
 #include <sys/uio.h>
 #include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/socket.h>
 
 #include <fcntl.h>
 #include <errno.h>
@@ -705,6 +707,80 @@ got_packidx_match_id_str_prefix(struct got_object_id_q
 
 	if (err)
 		got_object_id_queue_free(matched_ids);
+	return err;
+}
+
+static void
+set_max_datasize(void)
+{
+	struct rlimit rl;
+
+	if (getrlimit(RLIMIT_DATA, &rl) != 0)
+		return;
+
+	rl.rlim_cur = rl.rlim_max;
+	setrlimit(RLIMIT_DATA, &rl);
+}
+
+const struct got_error *
+got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
+{
+	const struct got_error *err = NULL;
+	int imsg_fds[2];
+	pid_t pid;
+	struct imsgbuf *ibuf;
+
+	ibuf = calloc(1, sizeof(*ibuf));
+	if (ibuf == NULL)
+		return got_error_from_errno("calloc");
+
+	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
+	if (pack->privsep_child == NULL) {
+		err = got_error_from_errno("calloc");
+		free(ibuf);
+		return err;
+	}
+	pack->child_has_tempfiles = 0;
+	pack->child_has_delta_outfd = 0;
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
+		err = got_error_from_errno("socketpair");
+		goto done;
+	}
+
+	pid = fork();
+	if (pid == -1) {
+		err = got_error_from_errno("fork");
+		goto done;
+	} else if (pid == 0) {
+		set_max_datasize();
+		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
+		    pack->path_packfile);
+		/* not reached */
+	}
+
+	if (close(imsg_fds[1]) == -1)
+		return got_error_from_errno("close");
+	pack->privsep_child->imsg_fd = imsg_fds[0];
+	pack->privsep_child->pid = pid;
+	imsg_init(ibuf, imsg_fds[0]);
+	pack->privsep_child->ibuf = ibuf;
+
+	err = got_privsep_init_pack_child(ibuf, pack, packidx);
+	if (err) {
+		const struct got_error *child_err;
+		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
+		child_err = got_privsep_wait_for_child(
+		    pack->privsep_child->pid);
+		if (child_err && err == NULL)
+			err = child_err;
+	}
+done:
+	if (err) {
+		free(ibuf);
+		free(pack->privsep_child);
+		pack->privsep_child = NULL;
+	}
 	return err;
 }
 
blob - 8ebcec37cc5884e4fd3f5f0986dee830277e39c8
blob + 207a6edd8617d1afeb8e3b428a73d56ebf40f146
--- lib/pack_create.c
+++ lib/pack_create.c
@@ -591,8 +591,53 @@ recv_reused_delta(struct got_imsg_reused_delta *delta,
 		return got_error_from_errno("got_object_id_dup");
 
 	return add_meta(m, v);
+}
+
+static const struct got_error *
+prepare_delta_reuse(struct got_pack **pack, struct got_packidx *packidx,
+    int delta_outfd, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	char *path_packfile = NULL;
+
+	err = got_packidx_get_packfile_path(&path_packfile,
+	    packidx->path_packidx);
+	if (err)
+		return err;
+
+	*pack = got_repo_get_cached_pack(repo, path_packfile);
+	if (*pack == NULL) {
+		err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
+		if (err)
+			goto done;
+	}
+	if ((*pack)->privsep_child == NULL) {
+		err = got_pack_start_privsep_child(*pack, packidx);
+		if (err)
+			goto done;
+	}
+
+	if (!(*pack)->child_has_delta_outfd) {
+		int outfd_child;
+		outfd_child = dup(delta_outfd);
+		if (outfd_child == -1) {
+			err = got_error_from_errno("dup");
+			goto done;
+		}
+		err = got_privsep_send_raw_delta_outfd(
+		    (*pack)->privsep_child->ibuf, outfd_child);
+		if (err)
+			goto done;
+		(*pack)->child_has_delta_outfd = 1;
+	}
+
+	err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
+done:
+	free(path_packfile);
+	return err;
 }
 
+
 static const struct got_error *
 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
     int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
@@ -614,8 +659,7 @@ search_deltas(struct got_pack_metavec *v, struct got_o
 	if (packidx == NULL)
 		return NULL;
 
-	err = got_object_prepare_delta_reuse(&pack, packidx,
-	    delta_cache_fd, repo);
+	err = prepare_delta_reuse(&pack, packidx, delta_cache_fd, repo);
 	if (err)
 		return err;