commit 3d589bee0bbbe812bb91f3b0284fbf2596304132 from: Stefan Sperling date: Sat Jun 25 14:54:41 2022 UTC allow start_pack_privsep_child() to be called from outside lib/object.c commit - 8469d82143a591d423c000c47c63bababe6f5716 commit + 3d589bee0bbbe812bb91f3b0284fbf2596304132 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 - 7a1fbe3ab0e107d0ec9075abdf9379a3b9de0071 blob + 06dc960b0d46db574b800f47ce6891fc926fa989 --- lib/object.c +++ lib/object.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -244,80 +243,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 * @@ -328,7 +253,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; } @@ -344,7 +269,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; } @@ -427,7 +352,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; } @@ -452,55 +377,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 * @@ -842,7 +718,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; @@ -1047,7 +923,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; @@ -1370,7 +1246,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; } @@ -1746,7 +1622,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; @@ -2370,7 +2246,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; } @@ -2426,7 +2302,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 - bbeafedb2d303742ae359d0ab03b005700ed75e2 blob + 57864428e7494edbd3e3308f2be2e34368445bf9 --- lib/pack.c +++ lib/pack.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -709,6 +711,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 - 2b957c17029c594fc2791494cf62d176ad552645 blob + 16d1a758fc03fec245de789888abc9cb072f408d --- lib/pack_create.c +++ lib/pack_create.c @@ -592,8 +592,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, @@ -615,8 +660,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;