de-duplicate some code I copied around in object.c
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
diff --git a/lib/object.c b/lib/object.c
index d47d917..d4be0fe 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -227,18 +227,13 @@ request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
}
static const struct got_error *
-read_packed_object_privsep(struct got_object **obj,
- struct got_repository *repo, struct got_pack *pack,
- struct got_packidx *packidx, int idx, struct got_object_id *id)
+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;
- if (pack->privsep_child)
- return request_packed_object(obj, pack, idx, id);
-
ibuf = calloc(1, sizeof(*ibuf));
if (ibuf == NULL)
return got_error_from_errno();
@@ -290,11 +285,27 @@ done:
free(ibuf);
free(pack->privsep_child);
pack->privsep_child = NULL;
- } else
- err = request_packed_object(obj, pack, idx, id);
+ }
return err;
}
+static const struct got_error *
+read_packed_object_privsep(struct got_object **obj,
+ struct got_repository *repo, struct got_pack *pack,
+ struct got_packidx *packidx, int idx, struct got_object_id *id)
+{
+ const struct got_error *err = NULL;
+
+ if (pack->privsep_child)
+ return request_packed_object(obj, pack, idx, id);
+
+ err = start_pack_privsep_child(pack, packidx);
+ if (err)
+ return err;
+
+ return request_packed_object(obj, pack, idx, id);
+}
+
static const struct got_error *
open_packed_object(struct got_object **obj, struct got_object_id *id,
@@ -432,70 +443,17 @@ read_packed_commit_privsep(struct got_commit_object **commit,
struct got_object_id *id)
{
const struct got_error *err = NULL;
- int imsg_fds[2];
- pid_t pid;
- struct imsgbuf *ibuf;
if (pack->privsep_child)
return request_packed_commit(commit, pack, idx, id);
- ibuf = calloc(1, sizeof(*ibuf));
- if (ibuf == NULL)
- return got_error_from_errno();
-
- pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
- if (pack->privsep_child == NULL) {
- err = got_error_from_errno();
- free(ibuf);
- return err;
- }
-
- if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
- err = got_error_from_errno();
- goto done;
- }
-
- pid = fork();
- if (pid == -1) {
- err = got_error_from_errno();
- goto done;
- } else if (pid == 0) {
- exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
- pack->path_packfile);
- /* not reached */
- }
-
- close(imsg_fds[1]);
- 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;
- free(ibuf);
- free(pack->privsep_child);
- pack->privsep_child = NULL;
+ err = start_pack_privsep_child(pack, packidx);
+ if (err)
return err;
- }
-done:
- if (err) {
- free(ibuf);
- free(pack->privsep_child);
- pack->privsep_child = NULL;
- } else
- err = request_packed_commit(commit, pack, idx, id);
- return err;
+ return request_packed_commit(commit, pack, idx, id);
}
-
static const struct got_error *
open_commit(struct got_commit_object **commit,
struct got_repository *repo, struct got_object_id *id, int check_cache)