2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
21 #include <sys/types.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_object.h"
40 #include "got_reference.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_delta_cache.h"
45 #include "got_lib_hash.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_object_idset.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_ratelimit.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_pack_index.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_poll.h"
58 #include "repo_write.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static struct repo_write {
67 struct got_repository *repo;
71 struct gotd_imsgev session_iev;
72 struct got_pathlist_head *protected_tag_namespaces;
73 struct got_pathlist_head *protected_branch_namespaces;
74 struct got_pathlist_head *protected_branches;
77 struct gotd_ref_update {
78 STAILQ_ENTRY(gotd_ref_update) entry;
79 struct got_reference *ref;
82 struct got_object_id old_id;
83 struct got_object_id new_id;
85 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
87 static struct repo_write_client {
92 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
94 struct gotd_ref_updates ref_updates;
101 static volatile sig_atomic_t sigint_received;
102 static volatile sig_atomic_t sigterm_received;
105 catch_sigint(int signo)
111 catch_sigterm(int signo)
113 sigterm_received = 1;
116 static const struct got_error *
117 check_cancelled(void *arg)
119 if (sigint_received || sigterm_received)
120 return got_error(GOT_ERR_CANCELLED);
125 static const struct got_error *
126 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
127 struct imsgbuf *ibuf)
129 const struct got_error *err = NULL;
130 struct got_tag_object *tag;
132 char *peeled_refname = NULL;
133 struct got_object_id *id;
136 err = got_object_tag_open(&tag, repo_write.repo, obj);
140 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
141 err = got_error_from_errno("asprintf");
145 id = got_object_tag_get_object_id(tag);
146 namelen = strlen(peeled_refname);
148 len = sizeof(struct gotd_imsg_ref) + namelen;
149 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
150 err = got_error(GOT_ERR_NO_SPACE);
154 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
155 repo_write.pid, len);
157 err = got_error_from_errno("imsg_create REF");
161 /* Keep in sync with struct gotd_imsg_ref definition. */
162 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
163 err = got_error_from_errno("imsg_add REF");
166 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
167 err = got_error_from_errno("imsg_add REF");
170 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
171 err = got_error_from_errno("imsg_add REF");
176 imsg_close(ibuf, wbuf);
178 got_object_tag_close(tag);
182 static const struct got_error *
183 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
185 const struct got_error *err;
186 const char *refname = got_ref_get_name(ref);
188 struct got_object_id *id = NULL;
189 struct got_object *obj = NULL;
193 namelen = strlen(refname);
195 len = sizeof(struct gotd_imsg_ref) + namelen;
196 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
197 return got_error(GOT_ERR_NO_SPACE);
199 err = got_ref_resolve(&id, repo_write.repo, ref);
203 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
204 repo_write.pid, len);
206 err = got_error_from_errno("imsg_create REF");
210 /* Keep in sync with struct gotd_imsg_ref definition. */
211 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
212 return got_error_from_errno("imsg_add REF");
213 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
214 return got_error_from_errno("imsg_add REF");
215 if (imsg_add(wbuf, refname, namelen) == -1)
216 return got_error_from_errno("imsg_add REF");
219 imsg_close(ibuf, wbuf);
221 err = got_object_open(&obj, repo_write.repo, id);
224 if (obj->type == GOT_OBJ_TYPE_TAG)
225 err = send_peeled_tag_ref(ref, obj, ibuf);
228 got_object_close(obj);
233 static const struct got_error *
234 list_refs(struct imsg *imsg)
236 const struct got_error *err;
237 struct repo_write_client *client = &repo_write_client;
238 struct got_reflist_head refs;
239 struct got_reflist_entry *re;
240 struct gotd_imsg_list_refs_internal ireq;
242 struct gotd_imsg_reflist irefs;
244 int client_fd = imsg->fd;
249 return got_error(GOT_ERR_PRIVSEP_NO_FD);
251 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
252 if (datalen != sizeof(ireq))
253 return got_error(GOT_ERR_PRIVSEP_LEN);
254 memcpy(&ireq, imsg->data, sizeof(ireq));
256 if (ireq.client_id == 0)
257 return got_error(GOT_ERR_CLIENT_ID);
258 if (client->id != 0) {
259 return got_error_msg(GOT_ERR_CLIENT_ID,
260 "duplicate list-refs request");
262 client->id = ireq.client_id;
263 client->fd = client_fd;
264 client->nref_updates = 0;
265 client->nref_del = 0;
266 client->nref_new = 0;
267 client->nref_move = 0;
269 imsg_init(&ibuf, client_fd);
271 err = got_ref_list(&refs, repo_write.repo, "",
272 got_ref_cmp_by_name, NULL);
276 memset(&irefs, 0, sizeof(irefs));
277 TAILQ_FOREACH(re, &refs, entry) {
278 struct got_object_id *id;
281 if (got_ref_is_symbolic(re->ref))
286 /* Account for a peeled tag refs. */
287 err = got_ref_resolve(&id, repo_write.repo, re->ref);
290 err = got_object_get_type(&obj_type, repo_write.repo, id);
294 if (obj_type == GOT_OBJ_TYPE_TAG)
298 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
299 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
300 err = got_error_from_errno("imsg_compose REFLIST");
304 TAILQ_FOREACH(re, &refs, entry) {
305 if (got_ref_is_symbolic(re->ref))
307 err = send_ref(re->ref, &ibuf);
312 err = gotd_imsg_flush(&ibuf);
314 got_ref_list_free(&refs);
319 static const struct got_error *
320 validate_namespace(const char *namespace)
322 size_t len = strlen(namespace);
324 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
325 namespace[len -1] != '/') {
326 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
327 "reference namespace '%s'", namespace);
333 static const struct got_error *
334 protect_ref_namespace(const char *refname, const char *namespace)
336 const struct got_error *err;
338 err = validate_namespace(namespace);
342 if (strncmp(namespace, refname, strlen(namespace)) == 0)
343 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
348 static const struct got_error *
349 verify_object_type(struct got_object_id *id, int expected_obj_type,
350 struct got_pack *pack, struct got_packidx *packidx)
352 const struct got_error *err;
353 char hex[SHA1_DIGEST_STRING_LENGTH];
354 struct got_object *obj;
358 idx = got_packidx_get_object_idx(packidx, id);
360 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
361 return got_error_fmt(GOT_ERR_BAD_PACKFILE,
362 "object %s is missing from pack file", hex);
365 err = got_object_open_from_packfile(&obj, id, pack, packidx,
366 idx, repo_write.repo);
370 if (obj->type != expected_obj_type) {
371 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
372 got_object_type_label(&typestr, expected_obj_type);
373 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
374 "%s is not pointing at a %s object", hex, typestr);
376 got_object_close(obj);
380 static const struct got_error *
381 protect_tag_namespace(const char *namespace, struct got_pack *pack,
382 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
384 const struct got_error *err;
386 err = validate_namespace(namespace);
390 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
391 strlen(namespace)) != 0)
394 if (!ref_update->ref_is_new)
395 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
397 return verify_object_type(&ref_update->new_id, GOT_OBJ_TYPE_TAG,
401 static const struct got_error *
402 protect_require_yca(struct got_object_id *tip_id,
403 size_t max_commits_to_traverse, struct got_pack *pack,
404 struct got_packidx *packidx, struct got_reference *ref)
406 const struct got_error *err;
409 struct got_object_id *expected_yca_id = NULL;
410 struct got_object *obj = NULL;
411 struct got_commit_object *commit = NULL;
412 char hex[SHA1_DIGEST_STRING_LENGTH];
413 const struct got_object_id_queue *parent_ids;
414 struct got_object_id_queue ids;
415 struct got_object_qid *pid, *qid;
416 struct got_object_idset *traversed_set = NULL;
417 int found_yca = 0, obj_type;
421 err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
425 err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
429 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
430 got_sha1_digest_to_str(expected_yca_id->sha1, hex, sizeof(hex));
431 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
432 "%s is not pointing at a commit object", hex);
436 traversed_set = got_object_idset_alloc();
437 if (traversed_set == NULL) {
438 err = got_error_from_errno("got_object_idset_alloc");
442 err = got_object_qid_alloc(&qid, tip_id);
445 STAILQ_INSERT_TAIL(&ids, qid, entry);
446 while (!STAILQ_EMPTY(&ids)) {
447 err = check_cancelled(NULL);
451 qid = STAILQ_FIRST(&ids);
452 if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
457 if (got_object_idset_num_elements(traversed_set) >=
458 max_commits_to_traverse)
461 if (got_object_idset_contains(traversed_set, &qid->id)) {
462 STAILQ_REMOVE_HEAD(&ids, entry);
463 got_object_qid_free(qid);
467 err = got_object_idset_add(traversed_set, &qid->id, NULL);
471 err = got_object_open(&obj, repo_write.repo, &qid->id);
472 if (err && err->code != GOT_ERR_NO_OBJ)
476 err = got_object_commit_open(&commit, repo_write.repo,
483 idx = got_packidx_get_object_idx(packidx, &qid->id);
485 got_sha1_digest_to_str(qid->id.sha1,
487 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
488 "object %s is missing from pack file", hex);
492 err = got_object_open_from_packfile(&obj, &qid->id,
493 pack, packidx, idx, repo_write.repo);
497 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
498 got_sha1_digest_to_str(qid->id.sha1,
500 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
501 "%s is not pointing at a commit object",
506 err = got_packfile_extract_object_to_mem(&buf, &len,
511 err = got_object_parse_commit(&commit, buf, len);
519 got_object_close(obj);
522 STAILQ_REMOVE_HEAD(&ids, entry);
523 got_object_qid_free(qid);
526 if (got_object_commit_get_nparents(commit) == 0)
529 parent_ids = got_object_commit_get_parent_ids(commit);
530 STAILQ_FOREACH(pid, parent_ids, entry) {
531 err = check_cancelled(NULL);
534 err = got_object_qid_alloc(&qid, &pid->id);
537 STAILQ_INSERT_TAIL(&ids, qid, entry);
540 got_object_commit_close(commit);
545 err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
546 got_ref_get_name(ref));
549 got_object_idset_free(traversed_set);
550 got_object_id_queue_free(&ids);
553 got_object_close(obj);
555 got_object_commit_close(commit);
556 free(expected_yca_id);
560 static const struct got_error *
561 protect_branch_namespace(const char *namespace, struct got_pack *pack,
562 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
564 const struct got_error *err;
566 err = validate_namespace(namespace);
570 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
571 strlen(namespace)) != 0)
574 if (ref_update->ref_is_new) {
575 return verify_object_type(&ref_update->new_id,
576 GOT_OBJ_TYPE_COMMIT, pack, packidx);
579 return protect_require_yca(&ref_update->new_id,
580 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
584 static const struct got_error *
585 protect_branch(const char *refname, struct got_pack *pack,
586 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
588 if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
591 /* Always allow new branches to be created. */
592 if (ref_update->ref_is_new) {
593 return verify_object_type(&ref_update->new_id,
594 GOT_OBJ_TYPE_COMMIT, pack, packidx);
597 return protect_require_yca(&ref_update->new_id,
598 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
602 static const struct got_error *
603 recv_ref_update(struct imsg *imsg)
605 static const char zero_id[SHA1_DIGEST_LENGTH];
606 const struct got_error *err = NULL;
607 struct repo_write_client *client = &repo_write_client;
608 struct gotd_imsg_ref_update iref;
610 char *refname = NULL;
611 struct got_reference *ref = NULL;
612 struct got_object_id *id = NULL;
614 struct gotd_ref_update *ref_update = NULL;
616 log_debug("ref-update received");
618 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
619 if (datalen < sizeof(iref))
620 return got_error(GOT_ERR_PRIVSEP_LEN);
621 memcpy(&iref, imsg->data, sizeof(iref));
622 if (datalen != sizeof(iref) + iref.name_len)
623 return got_error(GOT_ERR_PRIVSEP_LEN);
625 imsg_init(&ibuf, client->fd);
627 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
629 return got_error_from_errno("strndup");
631 ref_update = calloc(1, sizeof(*ref_update));
632 if (ref_update == NULL) {
633 err = got_error_from_errno("malloc");
637 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
638 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
640 err = got_ref_open(&ref, repo_write.repo, refname, 0);
642 if (err->code != GOT_ERR_NOT_REF)
644 if (memcmp(ref_update->new_id.sha1,
645 zero_id, sizeof(zero_id)) == 0) {
646 err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
650 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
653 ref_update->ref_is_new = 1;
656 if (got_ref_is_symbolic(ref)) {
657 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
658 "'%s' is a symbolic reference and cannot "
659 "be updated", got_ref_get_name(ref));
662 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
663 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
664 "%s: does not begin with 'refs/'",
665 got_ref_get_name(ref));
669 err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
672 err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
676 if (!ref_update->ref_is_new) {
678 * Ensure the client's idea of this update is still valid.
679 * At this point we can only return an error, to prevent
680 * the client from uploading a pack file which will likely
681 * have to be discarded.
683 err = got_ref_resolve(&id, repo_write.repo, ref);
687 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
688 err = got_error_fmt(GOT_ERR_REF_BUSY,
689 "%s has been modified by someone else "
690 "while transaction was in progress",
691 got_ref_get_name(ref));
696 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
699 ref_update->ref = ref;
700 if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
701 ref_update->delete_ref = 1;
704 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
705 client->nref_updates++;
717 static const struct got_error *
718 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
719 uint32_t nobj_loose, uint32_t nobj_resolved)
721 int p_indexed = 0, p_resolved = 0;
722 int nobj_delta = nobj_total - nobj_loose;
725 p_indexed = (nobj_indexed * 100) / nobj_total;
728 p_resolved = (nobj_resolved * 100) / nobj_delta;
730 if (p_resolved > 0) {
731 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
732 nobj_total, p_indexed, nobj_delta, p_resolved);
734 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
739 static const struct got_error *
740 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
742 const struct got_error *err = NULL;
743 uint8_t readahead[65536];
746 err = got_poll_read_full(infd, &have,
747 readahead, sizeof(readahead), minsize);
751 err = buf_append(&newlen, buf, readahead, have);
757 static const struct got_error *
758 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
759 off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
761 const struct got_error *err = NULL;
766 off_t obj_offset = *outsize;
769 /* We do not support size values which don't fit in 64 bit. */
771 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
772 "packfile offset %lld", (long long)obj_offset);
774 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
775 err = read_more_pack_stream(infd, buf,
781 sizebuf[i] = buf_getc(buf, *buf_pos);
782 *buf_pos += sizeof(sizebuf[i]);
785 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
786 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
787 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
789 size_t shift = 4 + 7 * (i - 1);
790 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
794 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
796 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
806 static const struct got_error *
807 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
808 struct got_hash *ctx)
810 const struct got_error *err = NULL;
811 size_t remain = buf_len(buf) - *buf_pos;
813 if (remain < SHA1_DIGEST_LENGTH) {
814 err = read_more_pack_stream(infd, buf,
815 SHA1_DIGEST_LENGTH - remain);
820 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
821 SHA1_DIGEST_LENGTH, ctx);
825 *buf_pos += SHA1_DIGEST_LENGTH;
829 static const struct got_error *
830 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
831 struct got_hash *ctx)
833 const struct got_error *err = NULL;
837 off_t obj_offset = *outsize;
840 /* We do not support offset values which don't fit in 64 bit. */
842 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
843 "packfile offset %lld", (long long)obj_offset);
845 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
846 err = read_more_pack_stream(infd, buf,
852 offbuf[i] = buf_getc(buf, *buf_pos);
853 *buf_pos += sizeof(offbuf[i]);
856 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
860 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
863 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
865 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
866 return got_error(GOT_ERR_PACK_OFFSET);
868 err = got_pack_hwrite(outfd, offbuf, i, ctx);
876 static const struct got_error *
877 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
878 struct got_hash *ctx)
880 const struct got_error *err = NULL;
884 size_t consumed_total = 0;
885 off_t zstream_offset = *outsize;
887 memset(&z, 0, sizeof(z));
891 zret = inflateInit(&z);
894 return got_error_from_errno("inflateInit");
895 if (zret == Z_MEM_ERROR) {
897 return got_error_from_errno("inflateInit");
899 return got_error_msg(GOT_ERR_DECOMPRESSION,
900 "inflateInit failed");
903 while (zret != Z_STREAM_END) {
904 size_t last_total_in, consumed;
907 * Decompress into the void. Object data will be parsed
908 * later, when the pack file is indexed. For now, we just
909 * want to locate the end of the compressed stream.
911 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
912 last_total_in = z.total_in;
913 z.next_in = buf_get(buf) + *buf_pos;
914 z.avail_in = buf_len(buf) - *buf_pos;
915 z.next_out = voidbuf;
916 z.avail_out = sizeof(voidbuf);
918 zret = inflate(&z, Z_SYNC_FLUSH);
919 if (zret != Z_OK && zret != Z_BUF_ERROR &&
920 zret != Z_STREAM_END) {
921 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
922 "packfile offset %lld",
923 (long long)zstream_offset);
926 consumed = z.total_in - last_total_in;
928 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
933 err = buf_discard(buf, *buf_pos + consumed);
938 consumed_total += consumed;
941 if (zret != Z_STREAM_END) {
942 err = read_more_pack_stream(infd, buf, 1);
949 *outsize += consumed_total;
955 static const struct got_error *
956 validate_object_type(int obj_type)
959 case GOT_OBJ_TYPE_BLOB:
960 case GOT_OBJ_TYPE_COMMIT:
961 case GOT_OBJ_TYPE_TREE:
962 case GOT_OBJ_TYPE_TAG:
963 case GOT_OBJ_TYPE_REF_DELTA:
964 case GOT_OBJ_TYPE_OFFSET_DELTA:
970 return got_error(GOT_ERR_OBJ_TYPE);
973 static const struct got_error *
974 ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
976 const struct got_error *err = NULL;
977 struct gotd_ref_update *ref_update;
978 struct got_object *obj;
980 STAILQ_FOREACH(ref_update, ref_updates, entry) {
981 err = got_object_open(&obj, repo_write.repo,
982 &ref_update->new_id);
985 got_object_close(obj);
991 static const struct got_error *
992 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
995 const struct got_error *err;
996 struct repo_write_client *client = &repo_write_client;
997 struct got_packfile_hdr hdr;
1000 struct got_hash ctx;
1001 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1002 char hex[SHA1_DIGEST_STRING_LENGTH];
1004 size_t buf_pos = 0, remain;
1010 /* if only deleting references there's nothing to read */
1011 if (client->nref_updates == client->nref_del)
1014 got_hash_init(&ctx, GOT_HASH_SHA1);
1016 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1019 if (have != sizeof(hdr))
1020 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1023 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1024 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1025 "bad packfile signature");
1026 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1027 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1028 "bad packfile version");
1030 *nobj = be32toh(hdr.nobjects);
1033 * Clients which are creating new references only
1034 * will send us an empty pack file.
1036 if (client->nref_updates > 0 &&
1037 client->nref_updates == client->nref_new)
1041 * Clients which only move existing refs will send us an empty
1042 * pack file. All referenced objects must exist locally.
1044 err = ensure_all_objects_exist_locally(&client->ref_updates);
1046 if (err->code != GOT_ERR_NO_OBJ)
1048 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1049 "bad packfile with zero objects");
1052 client->nref_move = client->nref_updates;
1056 log_debug("expecting %d objects", *nobj);
1058 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1062 err = buf_alloc(&buf, 65536);
1066 while (nhave != *nobj) {
1070 err = copy_object_type_and_size(&obj_type, &obj_size,
1071 infd, outfd, outsize, buf, &buf_pos, &ctx);
1075 err = validate_object_type(obj_type);
1079 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1080 err = copy_ref_delta(infd, outfd, outsize,
1081 buf, &buf_pos, &ctx);
1084 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1085 err = copy_offset_delta(infd, outfd, outsize,
1086 buf, &buf_pos, &ctx);
1091 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1098 log_debug("received %u objects", *nobj);
1100 got_hash_final(&ctx, expected_sha1);
1102 remain = buf_len(buf) - buf_pos;
1103 if (remain < SHA1_DIGEST_LENGTH) {
1104 err = read_more_pack_stream(infd, buf,
1105 SHA1_DIGEST_LENGTH - remain);
1110 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1111 log_debug("expect SHA1: %s", hex);
1112 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1113 log_debug("actual SHA1: %s", hex);
1115 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1116 SHA1_DIGEST_LENGTH) != 0) {
1117 err = got_error(GOT_ERR_PACKFILE_CSUM);
1121 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1123 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1125 err = got_error_from_errno("write");
1128 if (w != SHA1_DIGEST_LENGTH) {
1129 err = got_error(GOT_ERR_IO);
1133 *outsize += SHA1_DIGEST_LENGTH;
1135 if (fsync(outfd) == -1) {
1136 err = got_error_from_errno("fsync");
1139 if (lseek(outfd, 0L, SEEK_SET) == -1) {
1140 err = got_error_from_errno("lseek");
1148 static const struct got_error *
1149 report_pack_status(const struct got_error *unpack_err)
1151 const struct got_error *err = NULL;
1152 struct repo_write_client *client = &repo_write_client;
1153 struct gotd_imsg_packfile_status istatus;
1155 struct imsgbuf ibuf;
1156 const char *unpack_ok = "unpack ok\n";
1159 imsg_init(&ibuf, client->fd);
1162 istatus.reason_len = strlen(unpack_err->msg);
1164 istatus.reason_len = strlen(unpack_ok);
1166 len = sizeof(istatus) + istatus.reason_len;
1167 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1168 repo_write.pid, len);
1170 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1174 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1175 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1179 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1180 istatus.reason_len) == -1) {
1181 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1186 imsg_close(&ibuf, wbuf);
1188 err = gotd_imsg_flush(&ibuf);
1194 static const struct got_error *
1195 recv_packfile(int *have_packfile, struct imsg *imsg)
1197 const struct got_error *err = NULL, *unpack_err;
1198 struct repo_write_client *client = &repo_write_client;
1199 struct gotd_imsg_recv_packfile ireq;
1200 FILE *tempfiles[3] = { NULL, NULL, NULL };
1201 struct repo_tempfile {
1204 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1207 struct imsgbuf ibuf;
1208 struct got_ratelimit rl;
1209 struct got_pack *pack = NULL;
1210 off_t pack_filesize = 0;
1213 log_debug("packfile request received");
1216 got_ratelimit_init(&rl, 2, 0);
1218 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1219 if (datalen != sizeof(ireq))
1220 return got_error(GOT_ERR_PRIVSEP_LEN);
1221 memcpy(&ireq, imsg->data, sizeof(ireq));
1223 if (client->pack_pipe == -1 || client->packidx_fd == -1)
1224 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1226 imsg_init(&ibuf, client->fd);
1229 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1231 pack = &client->pack;
1232 memset(pack, 0, sizeof(*pack));
1233 pack->fd = imsg->fd;
1234 err = got_delta_cache_alloc(&pack->delta_cache);
1238 for (i = 0; i < nitems(repo_tempfiles); i++) {
1239 struct repo_tempfile *t = &repo_tempfiles[i];
1240 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
1245 for (i = 0; i < nitems(tempfiles); i++) {
1249 fd = dup(repo_tempfiles[i].fd);
1251 err = got_error_from_errno("dup");
1254 f = fdopen(fd, "w+");
1256 err = got_error_from_errno("fdopen");
1263 err = gotd_imsg_flush(&ibuf);
1267 log_debug("receiving pack data");
1268 unpack_err = recv_packdata(&pack_filesize, &nobj,
1269 client->pack_sha1, client->pack_pipe, pack->fd);
1270 if (ireq.report_status) {
1271 err = report_pack_status(unpack_err);
1273 /* Git clients hang up after sending the pack file. */
1274 if (err->code == GOT_ERR_EOF)
1283 log_debug("pack data received");
1286 * Clients which are creating new references only will
1287 * send us an empty pack file.
1290 pack_filesize == sizeof(struct got_packfile_hdr) &&
1291 client->nref_updates > 0 &&
1292 client->nref_updates == client->nref_new)
1296 * Clients which are deleting references only will send
1300 client->nref_del > 0 &&
1301 client->nref_updates == client->nref_del)
1305 * Clients which only move existing refs will send us an empty
1306 * pack file. All referenced objects must exist locally.
1309 pack_filesize == sizeof(struct got_packfile_hdr) &&
1310 client->nref_move > 0 &&
1311 client->nref_updates == client->nref_move)
1314 pack->filesize = pack_filesize;
1317 log_debug("begin indexing pack (%lld bytes in size)",
1318 (long long)pack->filesize);
1319 err = got_pack_index(pack, client->packidx_fd,
1320 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1321 pack_index_progress, NULL, &rl);
1324 log_debug("done indexing pack");
1326 if (fsync(client->packidx_fd) == -1) {
1327 err = got_error_from_errno("fsync");
1330 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1331 err = got_error_from_errno("lseek");
1333 if (close(client->pack_pipe) == -1 && err == NULL)
1334 err = got_error_from_errno("close");
1335 client->pack_pipe = -1;
1336 for (i = 0; i < nitems(repo_tempfiles); i++) {
1337 struct repo_tempfile *t = &repo_tempfiles[i];
1339 got_repo_temp_fds_put(t->idx, repo_write.repo);
1341 for (i = 0; i < nitems(tempfiles); i++) {
1342 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1343 err = got_error_from_errno("fclose");
1346 got_pack_close(pack);
1351 static const struct got_error *
1352 verify_packfile(void)
1354 const struct got_error *err = NULL, *close_err;
1355 struct repo_write_client *client = &repo_write_client;
1356 struct gotd_ref_update *ref_update;
1357 struct got_packidx *packidx = NULL;
1359 char *id_str = NULL;
1360 struct got_object *obj = NULL;
1361 struct got_pathlist_entry *pe;
1362 char hex[SHA1_DIGEST_STRING_LENGTH];
1364 if (STAILQ_EMPTY(&client->ref_updates)) {
1365 return got_error_msg(GOT_ERR_BAD_REQUEST,
1366 "cannot verify pack file without any ref-updates");
1369 if (client->pack.fd == -1) {
1370 return got_error_msg(GOT_ERR_BAD_REQUEST,
1371 "invalid pack file handle during pack verification");
1373 if (client->packidx_fd == -1) {
1374 return got_error_msg(GOT_ERR_BAD_REQUEST,
1375 "invalid pack index handle during pack verification");
1378 if (fstat(client->packidx_fd, &sb) == -1)
1379 return got_error_from_errno("pack index fstat");
1381 packidx = malloc(sizeof(*packidx));
1382 memset(packidx, 0, sizeof(*packidx));
1383 packidx->fd = client->packidx_fd;
1384 client->packidx_fd = -1;
1385 packidx->len = sb.st_size;
1387 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1391 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1392 if (ref_update->delete_ref)
1395 TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1396 err = protect_tag_namespace(pe->path, &client->pack,
1397 packidx, ref_update);
1403 * Objects which already exist in our repository need
1404 * not be present in the pack file.
1406 err = got_object_open(&obj, repo_write.repo,
1407 &ref_update->new_id);
1408 if (err && err->code != GOT_ERR_NO_OBJ)
1412 got_object_close(obj);
1415 int idx = got_packidx_get_object_idx(packidx,
1416 &ref_update->new_id);
1418 got_sha1_digest_to_str(ref_update->new_id.sha1,
1420 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1421 "object %s is missing from pack file",
1427 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1429 err = protect_branch_namespace(pe->path,
1430 &client->pack, packidx, ref_update);
1434 TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1435 err = protect_branch(pe->path, &client->pack,
1436 packidx, ref_update);
1443 close_err = got_packidx_close(packidx);
1444 if (close_err && err == NULL)
1448 got_object_close(obj);
1452 static const struct got_error *
1453 protect_refs_from_deletion(void)
1455 const struct got_error *err = NULL;
1456 struct repo_write_client *client = &repo_write_client;
1457 struct gotd_ref_update *ref_update;
1458 struct got_pathlist_entry *pe;
1459 const char *refname;
1461 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1462 if (!ref_update->delete_ref)
1465 refname = got_ref_get_name(ref_update->ref);
1467 TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1468 err = protect_ref_namespace(refname, pe->path);
1473 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1475 err = protect_ref_namespace(refname, pe->path);
1480 TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1481 if (strcmp(refname, pe->path) == 0) {
1482 return got_error_fmt(GOT_ERR_REF_PROTECTED,
1491 static const struct got_error *
1492 install_packfile(struct gotd_imsgev *iev)
1494 struct repo_write_client *client = &repo_write_client;
1495 struct gotd_imsg_packfile_install inst;
1498 memset(&inst, 0, sizeof(inst));
1499 inst.client_id = client->id;
1500 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1502 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1503 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1505 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1510 static const struct got_error *
1511 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1513 struct repo_write_client *client = &repo_write_client;
1514 struct gotd_imsg_ref_updates_start istart;
1517 memset(&istart, 0, sizeof(istart));
1518 istart.nref_updates = nref_updates;
1519 istart.client_id = client->id;
1521 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1522 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1524 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1530 static const struct got_error *
1531 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1533 struct repo_write_client *client = &repo_write_client;
1534 struct gotd_imsg_ref_update iref;
1535 const char *refname = got_ref_get_name(ref_update->ref);
1539 memset(&iref, 0, sizeof(iref));
1540 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1541 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1542 iref.ref_is_new = ref_update->ref_is_new;
1543 iref.delete_ref = ref_update->delete_ref;
1544 iref.client_id = client->id;
1545 iref.name_len = strlen(refname);
1547 len = sizeof(iref) + iref.name_len;
1548 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1549 repo_write.pid, len);
1551 return got_error_from_errno("imsg_create REF_UPDATE");
1553 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1554 return got_error_from_errno("imsg_add REF_UPDATE");
1555 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1556 return got_error_from_errno("imsg_add REF_UPDATE");
1559 imsg_close(&iev->ibuf, wbuf);
1561 gotd_imsg_event_add(iev);
1565 static const struct got_error *
1566 update_refs(struct gotd_imsgev *iev)
1568 const struct got_error *err = NULL;
1569 struct repo_write_client *client = &repo_write_client;
1570 struct gotd_ref_update *ref_update;
1572 err = send_ref_updates_start(client->nref_updates, iev);
1576 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1577 err = send_ref_update(ref_update, iev);
1585 static const struct got_error *
1586 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1588 struct repo_write_client *client = &repo_write_client;
1589 struct gotd_imsg_packfile_pipe ireq;
1592 log_debug("receiving pack pipe descriptor");
1595 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1597 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1598 if (datalen != sizeof(ireq))
1599 return got_error(GOT_ERR_PRIVSEP_LEN);
1600 memcpy(&ireq, imsg->data, sizeof(ireq));
1602 if (client->pack_pipe != -1)
1603 return got_error(GOT_ERR_PRIVSEP_MSG);
1605 client->pack_pipe = imsg->fd;
1609 static const struct got_error *
1610 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1612 struct repo_write_client *client = &repo_write_client;
1613 struct gotd_imsg_packidx_file ireq;
1616 log_debug("receiving pack index output file");
1619 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1621 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1622 if (datalen != sizeof(ireq))
1623 return got_error(GOT_ERR_PRIVSEP_LEN);
1624 memcpy(&ireq, imsg->data, sizeof(ireq));
1626 if (client->packidx_fd != -1)
1627 return got_error(GOT_ERR_PRIVSEP_MSG);
1629 client->packidx_fd = imsg->fd;
1634 repo_write_dispatch_session(int fd, short event, void *arg)
1636 const struct got_error *err = NULL;
1637 struct gotd_imsgev *iev = arg;
1638 struct imsgbuf *ibuf = &iev->ibuf;
1640 struct repo_write_client *client = &repo_write_client;
1642 int shut = 0, have_packfile = 0;
1644 if (event & EV_READ) {
1645 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1646 fatal("imsg_read error");
1647 if (n == 0) /* Connection closed. */
1651 if (event & EV_WRITE) {
1652 n = msgbuf_write(&ibuf->w);
1653 if (n == -1 && errno != EAGAIN)
1654 fatal("msgbuf_write");
1655 if (n == 0) /* Connection closed. */
1660 if ((n = imsg_get(ibuf, &imsg)) == -1)
1661 fatal("%s: imsg_get error", __func__);
1662 if (n == 0) /* No more messages. */
1665 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1667 err = got_error(GOT_ERR_PRIVSEP_MSG);
1671 switch (imsg.hdr.type) {
1672 case GOTD_IMSG_LIST_REFS_INTERNAL:
1673 err = list_refs(&imsg);
1675 log_warnx("ls-refs: %s", err->msg);
1677 case GOTD_IMSG_REF_UPDATE:
1678 err = recv_ref_update(&imsg);
1680 log_warnx("ref-update: %s", err->msg);
1682 case GOTD_IMSG_PACKFILE_PIPE:
1683 err = receive_pack_pipe(&imsg, iev);
1685 log_warnx("receiving pack pipe: %s", err->msg);
1689 case GOTD_IMSG_PACKIDX_FILE:
1690 err = receive_pack_idx(&imsg, iev);
1692 log_warnx("receiving pack index: %s",
1697 case GOTD_IMSG_RECV_PACKFILE:
1698 err = protect_refs_from_deletion();
1701 err = recv_packfile(&have_packfile, &imsg);
1703 log_warnx("receive packfile: %s", err->msg);
1706 if (have_packfile) {
1707 err = verify_packfile();
1709 log_warnx("verify packfile: %s",
1713 err = install_packfile(iev);
1715 log_warnx("install packfile: %s",
1720 err = update_refs(iev);
1722 log_warnx("update refs: %s", err->msg);
1726 log_debug("unexpected imsg %d", imsg.hdr.type);
1733 if (!shut && check_cancelled(NULL) == NULL) {
1735 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1736 client->id, err) == -1) {
1737 log_warnx("could not send error to parent: %s",
1740 gotd_imsg_event_add(iev);
1742 /* This pipe is dead. Remove its event handler */
1743 event_del(&iev->ev);
1744 event_loopexit(NULL);
1748 static const struct got_error *
1749 recv_connect(struct imsg *imsg)
1751 struct gotd_imsgev *iev = &repo_write.session_iev;
1754 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1756 return got_error(GOT_ERR_PRIVSEP_LEN);
1758 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1760 if (repo_write.session_fd != -1)
1761 return got_error(GOT_ERR_PRIVSEP_MSG);
1763 repo_write.session_fd = imsg->fd;
1765 imsg_init(&iev->ibuf, repo_write.session_fd);
1766 iev->handler = repo_write_dispatch_session;
1767 iev->events = EV_READ;
1768 iev->handler_arg = NULL;
1769 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1770 repo_write_dispatch_session, iev);
1771 gotd_imsg_event_add(iev);
1777 repo_write_dispatch(int fd, short event, void *arg)
1779 const struct got_error *err = NULL;
1780 struct gotd_imsgev *iev = arg;
1781 struct imsgbuf *ibuf = &iev->ibuf;
1785 struct repo_write_client *client = &repo_write_client;
1787 if (event & EV_READ) {
1788 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1789 fatal("imsg_read error");
1790 if (n == 0) /* Connection closed. */
1794 if (event & EV_WRITE) {
1795 n = msgbuf_write(&ibuf->w);
1796 if (n == -1 && errno != EAGAIN)
1797 fatal("msgbuf_write");
1798 if (n == 0) /* Connection closed. */
1802 while (err == NULL && check_cancelled(NULL) == NULL) {
1803 if ((n = imsg_get(ibuf, &imsg)) == -1)
1804 fatal("%s: imsg_get", __func__);
1805 if (n == 0) /* No more messages. */
1808 switch (imsg.hdr.type) {
1809 case GOTD_IMSG_CONNECT_REPO_CHILD:
1810 err = recv_connect(&imsg);
1813 log_debug("unexpected imsg %d", imsg.hdr.type);
1820 if (!shut && check_cancelled(NULL) == NULL) {
1822 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1823 client->id, err) == -1) {
1824 log_warnx("could not send error to parent: %s",
1827 gotd_imsg_event_add(iev);
1829 /* This pipe is dead. Remove its event handler */
1830 event_del(&iev->ev);
1831 event_loopexit(NULL);
1836 repo_write_main(const char *title, const char *repo_path,
1837 int *pack_fds, int *temp_fds,
1838 struct got_pathlist_head *protected_tag_namespaces,
1839 struct got_pathlist_head *protected_branch_namespaces,
1840 struct got_pathlist_head *protected_branches)
1842 const struct got_error *err = NULL;
1843 struct repo_write_client *client = &repo_write_client;
1844 struct gotd_imsgev iev;
1847 client->pack_pipe = -1;
1848 client->packidx_fd = -1;
1849 client->pack.fd = -1;
1851 repo_write.title = title;
1852 repo_write.pid = getpid();
1853 repo_write.pack_fds = pack_fds;
1854 repo_write.temp_fds = temp_fds;
1855 repo_write.session_fd = -1;
1856 repo_write.session_iev.ibuf.fd = -1;
1857 repo_write.protected_tag_namespaces = protected_tag_namespaces;
1858 repo_write.protected_branch_namespaces = protected_branch_namespaces;
1859 repo_write.protected_branches = protected_branches;
1861 STAILQ_INIT(&repo_write_client.ref_updates);
1863 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1866 if (!got_repo_is_bare(repo_write.repo)) {
1867 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1868 "bare git repository required");
1872 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1874 signal(SIGINT, catch_sigint);
1875 signal(SIGTERM, catch_sigterm);
1876 signal(SIGPIPE, SIG_IGN);
1877 signal(SIGHUP, SIG_IGN);
1879 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1880 iev.handler = repo_write_dispatch;
1881 iev.events = EV_READ;
1882 iev.handler_arg = NULL;
1883 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1884 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1885 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1886 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1893 log_warnx("%s: %s", title, err->msg);
1894 repo_write_shutdown();
1898 repo_write_shutdown(void)
1900 struct repo_write_client *client = &repo_write_client;
1901 struct gotd_ref_update *ref_update;
1903 log_debug("shutting down");
1905 while (!STAILQ_EMPTY(&client->ref_updates)) {
1906 ref_update = STAILQ_FIRST(&client->ref_updates);
1907 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1908 got_ref_close(ref_update->ref);
1912 got_pack_close(&client->pack);
1913 if (client->fd != -1)
1915 if (client->pack_pipe != -1)
1916 close(client->pack_pipe);
1917 if (client->packidx_fd != -1)
1918 close(client->packidx_fd);
1920 if (repo_write.repo)
1921 got_repo_close(repo_write.repo);
1922 got_repo_pack_fds_close(repo_write.pack_fds);
1923 got_repo_temp_fds_close(repo_write.temp_fds);
1924 if (repo_write.session_fd != -1)
1925 close(repo_write.session_fd);