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 <sys/queue.h>
19 #include <sys/types.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_object.h"
39 #include "got_reference.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_delta_cache.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_cache.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_ratelimit.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_pack_index.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_poll.h"
57 #include "repo_write.h"
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 static struct repo_write {
66 struct got_repository *repo;
70 struct gotd_imsgev session_iev;
71 struct got_pathlist_head *protected_tag_namespaces;
72 struct got_pathlist_head *protected_branch_namespaces;
73 struct got_pathlist_head *protected_branches;
76 struct gotd_ref_update {
77 STAILQ_ENTRY(gotd_ref_update) entry;
78 struct got_reference *ref;
81 struct got_object_id old_id;
82 struct got_object_id new_id;
84 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
86 static struct repo_write_client {
91 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
93 struct gotd_ref_updates ref_updates;
100 static volatile sig_atomic_t sigint_received;
101 static volatile sig_atomic_t sigterm_received;
104 catch_sigint(int signo)
110 catch_sigterm(int signo)
112 sigterm_received = 1;
115 static const struct got_error *
116 check_cancelled(void *arg)
118 if (sigint_received || sigterm_received)
119 return got_error(GOT_ERR_CANCELLED);
124 static const struct got_error *
125 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
126 struct imsgbuf *ibuf)
128 const struct got_error *err = NULL;
129 struct got_tag_object *tag;
131 char *peeled_refname = NULL;
132 struct got_object_id *id;
135 err = got_object_tag_open(&tag, repo_write.repo, obj);
139 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
140 err = got_error_from_errno("asprintf");
144 id = got_object_tag_get_object_id(tag);
145 namelen = strlen(peeled_refname);
147 len = sizeof(struct gotd_imsg_ref) + namelen;
148 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
149 err = got_error(GOT_ERR_NO_SPACE);
153 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
154 repo_write.pid, len);
156 err = got_error_from_errno("imsg_create REF");
160 /* Keep in sync with struct gotd_imsg_ref definition. */
161 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
162 err = got_error_from_errno("imsg_add REF");
165 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
166 err = got_error_from_errno("imsg_add REF");
169 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
170 err = got_error_from_errno("imsg_add REF");
175 imsg_close(ibuf, wbuf);
177 got_object_tag_close(tag);
181 static const struct got_error *
182 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
184 const struct got_error *err;
185 const char *refname = got_ref_get_name(ref);
187 struct got_object_id *id = NULL;
188 struct got_object *obj = NULL;
192 namelen = strlen(refname);
194 len = sizeof(struct gotd_imsg_ref) + namelen;
195 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
196 return got_error(GOT_ERR_NO_SPACE);
198 err = got_ref_resolve(&id, repo_write.repo, ref);
202 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
203 repo_write.pid, len);
205 err = got_error_from_errno("imsg_create REF");
209 /* Keep in sync with struct gotd_imsg_ref definition. */
210 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
211 return got_error_from_errno("imsg_add REF");
212 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
213 return got_error_from_errno("imsg_add REF");
214 if (imsg_add(wbuf, refname, namelen) == -1)
215 return got_error_from_errno("imsg_add REF");
218 imsg_close(ibuf, wbuf);
220 err = got_object_open(&obj, repo_write.repo, id);
223 if (obj->type == GOT_OBJ_TYPE_TAG)
224 err = send_peeled_tag_ref(ref, obj, ibuf);
227 got_object_close(obj);
232 static const struct got_error *
233 list_refs(struct imsg *imsg)
235 const struct got_error *err;
236 struct repo_write_client *client = &repo_write_client;
237 struct got_reflist_head refs;
238 struct got_reflist_entry *re;
239 struct gotd_imsg_list_refs_internal ireq;
241 struct gotd_imsg_reflist irefs;
243 int client_fd = imsg->fd;
248 return got_error(GOT_ERR_PRIVSEP_NO_FD);
250 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
251 if (datalen != sizeof(ireq))
252 return got_error(GOT_ERR_PRIVSEP_LEN);
253 memcpy(&ireq, imsg->data, sizeof(ireq));
255 if (ireq.client_id == 0)
256 return got_error(GOT_ERR_CLIENT_ID);
257 if (client->id != 0) {
258 return got_error_msg(GOT_ERR_CLIENT_ID,
259 "duplicate list-refs request");
261 client->id = ireq.client_id;
262 client->fd = client_fd;
263 client->nref_updates = 0;
264 client->nref_del = 0;
265 client->nref_new = 0;
266 client->nref_move = 0;
268 imsg_init(&ibuf, client_fd);
270 err = got_ref_list(&refs, repo_write.repo, "",
271 got_ref_cmp_by_name, NULL);
275 memset(&irefs, 0, sizeof(irefs));
276 TAILQ_FOREACH(re, &refs, entry) {
277 struct got_object_id *id;
280 if (got_ref_is_symbolic(re->ref))
285 /* Account for a peeled tag refs. */
286 err = got_ref_resolve(&id, repo_write.repo, re->ref);
289 err = got_object_get_type(&obj_type, repo_write.repo, id);
293 if (obj_type == GOT_OBJ_TYPE_TAG)
297 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
298 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
299 err = got_error_from_errno("imsg_compose REFLIST");
303 TAILQ_FOREACH(re, &refs, entry) {
304 if (got_ref_is_symbolic(re->ref))
306 err = send_ref(re->ref, &ibuf);
311 err = gotd_imsg_flush(&ibuf);
313 got_ref_list_free(&refs);
318 static const struct got_error *
319 validate_namespace(const char *namespace)
321 size_t len = strlen(namespace);
323 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
324 namespace[len -1] != '/') {
325 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
326 "reference namespace '%s'", namespace);
332 static const struct got_error *
333 protect_ref_namespace(const char *refname, const char *namespace)
335 const struct got_error *err;
337 err = validate_namespace(namespace);
341 if (strncmp(namespace, refname, strlen(namespace)) == 0)
342 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
347 static const struct got_error *
348 verify_object_type(struct got_object_id *id, int expected_obj_type,
349 struct got_pack *pack, struct got_packidx *packidx)
351 const struct got_error *err;
352 char hex[SHA1_DIGEST_STRING_LENGTH];
353 struct got_object *obj;
357 idx = got_packidx_get_object_idx(packidx, id);
359 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
360 return got_error_fmt(GOT_ERR_BAD_PACKFILE,
361 "object %s is missing from pack file", hex);
364 err = got_object_open_from_packfile(&obj, id, pack, packidx,
365 idx, repo_write.repo);
369 if (obj->type != expected_obj_type) {
370 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
371 got_object_type_label(&typestr, expected_obj_type);
372 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
373 "%s is not pointing at a %s object", hex, typestr);
375 got_object_close(obj);
379 static const struct got_error *
380 protect_tag_namespace(const char *namespace, struct got_pack *pack,
381 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
383 const struct got_error *err;
385 err = validate_namespace(namespace);
389 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
390 strlen(namespace)) != 0)
393 if (!ref_update->ref_is_new)
394 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
396 return verify_object_type(&ref_update->new_id, GOT_OBJ_TYPE_TAG,
400 static const struct got_error *
401 protect_require_yca(struct got_object_id *tip_id,
402 size_t max_commits_to_traverse, struct got_pack *pack,
403 struct got_packidx *packidx, struct got_reference *ref)
405 const struct got_error *err;
408 struct got_object_id *expected_yca_id = NULL;
409 struct got_object *obj = NULL;
410 struct got_commit_object *commit = NULL;
411 char hex[SHA1_DIGEST_STRING_LENGTH];
412 const struct got_object_id_queue *parent_ids;
413 struct got_object_id_queue ids;
414 struct got_object_qid *pid, *qid;
415 struct got_object_idset *traversed_set = NULL;
416 int found_yca = 0, obj_type;
420 err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
424 err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
428 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
429 got_sha1_digest_to_str(expected_yca_id->sha1, hex, sizeof(hex));
430 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
431 "%s is not pointing at a commit object", hex);
435 traversed_set = got_object_idset_alloc();
436 if (traversed_set == NULL) {
437 err = got_error_from_errno("got_object_idset_alloc");
441 err = got_object_qid_alloc(&qid, tip_id);
444 STAILQ_INSERT_TAIL(&ids, qid, entry);
445 while (!STAILQ_EMPTY(&ids)) {
446 err = check_cancelled(NULL);
450 qid = STAILQ_FIRST(&ids);
451 if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
456 if (got_object_idset_num_elements(traversed_set) >=
457 max_commits_to_traverse)
460 if (got_object_idset_contains(traversed_set, &qid->id)) {
461 STAILQ_REMOVE_HEAD(&ids, entry);
462 got_object_qid_free(qid);
466 err = got_object_idset_add(traversed_set, &qid->id, NULL);
470 err = got_object_open(&obj, repo_write.repo, &qid->id);
471 if (err && err->code != GOT_ERR_NO_OBJ)
475 err = got_object_commit_open(&commit, repo_write.repo,
482 idx = got_packidx_get_object_idx(packidx, &qid->id);
484 got_sha1_digest_to_str(qid->id.sha1,
486 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
487 "object %s is missing from pack file", hex);
491 err = got_object_open_from_packfile(&obj, &qid->id,
492 pack, packidx, idx, repo_write.repo);
496 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
497 got_sha1_digest_to_str(qid->id.sha1,
499 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
500 "%s is not pointing at a commit object",
505 err = got_packfile_extract_object_to_mem(&buf, &len,
510 err = got_object_parse_commit(&commit, buf, len);
518 got_object_close(obj);
521 STAILQ_REMOVE_HEAD(&ids, entry);
522 got_object_qid_free(qid);
525 if (got_object_commit_get_nparents(commit) == 0)
528 parent_ids = got_object_commit_get_parent_ids(commit);
529 STAILQ_FOREACH(pid, parent_ids, entry) {
530 err = check_cancelled(NULL);
533 err = got_object_qid_alloc(&qid, &pid->id);
536 STAILQ_INSERT_TAIL(&ids, qid, entry);
539 got_object_commit_close(commit);
544 err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
545 got_ref_get_name(ref));
548 got_object_idset_free(traversed_set);
549 got_object_id_queue_free(&ids);
552 got_object_close(obj);
554 got_object_commit_close(commit);
555 free(expected_yca_id);
559 static const struct got_error *
560 protect_branch_namespace(const char *namespace, struct got_pack *pack,
561 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
563 const struct got_error *err;
565 err = validate_namespace(namespace);
569 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
570 strlen(namespace)) != 0)
573 if (ref_update->ref_is_new) {
574 return verify_object_type(&ref_update->new_id,
575 GOT_OBJ_TYPE_COMMIT, pack, packidx);
578 return protect_require_yca(&ref_update->new_id,
579 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
583 static const struct got_error *
584 protect_branch(const char *refname, struct got_pack *pack,
585 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
587 if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
590 /* Always allow new branches to be created. */
591 if (ref_update->ref_is_new) {
592 return verify_object_type(&ref_update->new_id,
593 GOT_OBJ_TYPE_COMMIT, pack, packidx);
596 return protect_require_yca(&ref_update->new_id,
597 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
601 static const struct got_error *
602 recv_ref_update(struct imsg *imsg)
604 static const char zero_id[SHA1_DIGEST_LENGTH];
605 const struct got_error *err = NULL;
606 struct repo_write_client *client = &repo_write_client;
607 struct gotd_imsg_ref_update iref;
609 char *refname = NULL;
610 struct got_reference *ref = NULL;
611 struct got_object_id *id = NULL;
613 struct gotd_ref_update *ref_update = NULL;
615 log_debug("ref-update received");
617 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
618 if (datalen < sizeof(iref))
619 return got_error(GOT_ERR_PRIVSEP_LEN);
620 memcpy(&iref, imsg->data, sizeof(iref));
621 if (datalen != sizeof(iref) + iref.name_len)
622 return got_error(GOT_ERR_PRIVSEP_LEN);
624 imsg_init(&ibuf, client->fd);
626 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
628 return got_error_from_errno("strndup");
630 ref_update = calloc(1, sizeof(*ref_update));
631 if (ref_update == NULL) {
632 err = got_error_from_errno("malloc");
636 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
637 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
639 err = got_ref_open(&ref, repo_write.repo, refname, 0);
641 if (err->code != GOT_ERR_NOT_REF)
643 if (memcmp(ref_update->new_id.sha1,
644 zero_id, sizeof(zero_id)) == 0) {
645 err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
649 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
652 ref_update->ref_is_new = 1;
655 if (got_ref_is_symbolic(ref)) {
656 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
657 "'%s' is a symbolic reference and cannot "
658 "be updated", got_ref_get_name(ref));
661 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
662 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
663 "%s: does not begin with 'refs/'",
664 got_ref_get_name(ref));
668 err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
671 err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
675 if (!ref_update->ref_is_new) {
677 * Ensure the client's idea of this update is still valid.
678 * At this point we can only return an error, to prevent
679 * the client from uploading a pack file which will likely
680 * have to be discarded.
682 err = got_ref_resolve(&id, repo_write.repo, ref);
686 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
687 err = got_error_fmt(GOT_ERR_REF_BUSY,
688 "%s has been modified by someone else "
689 "while transaction was in progress",
690 got_ref_get_name(ref));
695 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
698 ref_update->ref = ref;
699 if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
700 ref_update->delete_ref = 1;
703 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
704 client->nref_updates++;
716 static const struct got_error *
717 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
718 uint32_t nobj_loose, uint32_t nobj_resolved)
720 int p_indexed = 0, p_resolved = 0;
721 int nobj_delta = nobj_total - nobj_loose;
724 p_indexed = (nobj_indexed * 100) / nobj_total;
727 p_resolved = (nobj_resolved * 100) / nobj_delta;
729 if (p_resolved > 0) {
730 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
731 nobj_total, p_indexed, nobj_delta, p_resolved);
733 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
738 static const struct got_error *
739 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
741 const struct got_error *err = NULL;
742 uint8_t readahead[65536];
745 err = got_poll_read_full(infd, &have,
746 readahead, sizeof(readahead), minsize);
750 err = buf_append(&newlen, buf, readahead, have);
756 static const struct got_error *
757 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
758 off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
760 const struct got_error *err = NULL;
765 off_t obj_offset = *outsize;
768 /* We do not support size values which don't fit in 64 bit. */
770 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
771 "packfile offset %lld", (long long)obj_offset);
773 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
774 err = read_more_pack_stream(infd, buf,
780 sizebuf[i] = buf_getc(buf, *buf_pos);
781 *buf_pos += sizeof(sizebuf[i]);
784 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
785 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
786 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
788 size_t shift = 4 + 7 * (i - 1);
789 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
793 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
795 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
805 static const struct got_error *
806 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
807 struct got_hash *ctx)
809 const struct got_error *err = NULL;
810 size_t remain = buf_len(buf) - *buf_pos;
812 if (remain < SHA1_DIGEST_LENGTH) {
813 err = read_more_pack_stream(infd, buf,
814 SHA1_DIGEST_LENGTH - remain);
819 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
820 SHA1_DIGEST_LENGTH, ctx);
824 *buf_pos += SHA1_DIGEST_LENGTH;
828 static const struct got_error *
829 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
830 struct got_hash *ctx)
832 const struct got_error *err = NULL;
836 off_t obj_offset = *outsize;
839 /* We do not support offset values which don't fit in 64 bit. */
841 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
842 "packfile offset %lld", (long long)obj_offset);
844 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
845 err = read_more_pack_stream(infd, buf,
851 offbuf[i] = buf_getc(buf, *buf_pos);
852 *buf_pos += sizeof(offbuf[i]);
855 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
859 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
862 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
864 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
865 return got_error(GOT_ERR_PACK_OFFSET);
867 err = got_pack_hwrite(outfd, offbuf, i, ctx);
875 static const struct got_error *
876 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
877 struct got_hash *ctx)
879 const struct got_error *err = NULL;
883 size_t consumed_total = 0;
884 off_t zstream_offset = *outsize;
886 memset(&z, 0, sizeof(z));
890 zret = inflateInit(&z);
893 return got_error_from_errno("inflateInit");
894 if (zret == Z_MEM_ERROR) {
896 return got_error_from_errno("inflateInit");
898 return got_error_msg(GOT_ERR_DECOMPRESSION,
899 "inflateInit failed");
902 while (zret != Z_STREAM_END) {
903 size_t last_total_in, consumed;
906 * Decompress into the void. Object data will be parsed
907 * later, when the pack file is indexed. For now, we just
908 * want to locate the end of the compressed stream.
910 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
911 last_total_in = z.total_in;
912 z.next_in = buf_get(buf) + *buf_pos;
913 z.avail_in = buf_len(buf) - *buf_pos;
914 z.next_out = voidbuf;
915 z.avail_out = sizeof(voidbuf);
917 zret = inflate(&z, Z_SYNC_FLUSH);
918 if (zret != Z_OK && zret != Z_BUF_ERROR &&
919 zret != Z_STREAM_END) {
920 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
921 "packfile offset %lld",
922 (long long)zstream_offset);
925 consumed = z.total_in - last_total_in;
927 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
932 err = buf_discard(buf, *buf_pos + consumed);
937 consumed_total += consumed;
940 if (zret != Z_STREAM_END) {
941 err = read_more_pack_stream(infd, buf, 1);
948 *outsize += consumed_total;
954 static const struct got_error *
955 validate_object_type(int obj_type)
958 case GOT_OBJ_TYPE_BLOB:
959 case GOT_OBJ_TYPE_COMMIT:
960 case GOT_OBJ_TYPE_TREE:
961 case GOT_OBJ_TYPE_TAG:
962 case GOT_OBJ_TYPE_REF_DELTA:
963 case GOT_OBJ_TYPE_OFFSET_DELTA:
969 return got_error(GOT_ERR_OBJ_TYPE);
972 static const struct got_error *
973 ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
975 const struct got_error *err = NULL;
976 struct gotd_ref_update *ref_update;
977 struct got_object *obj;
979 STAILQ_FOREACH(ref_update, ref_updates, entry) {
980 err = got_object_open(&obj, repo_write.repo,
981 &ref_update->new_id);
984 got_object_close(obj);
990 static const struct got_error *
991 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
994 const struct got_error *err;
995 struct repo_write_client *client = &repo_write_client;
996 struct got_packfile_hdr hdr;
1000 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1001 char hex[SHA1_DIGEST_STRING_LENGTH];
1003 size_t buf_pos = 0, remain;
1009 /* if only deleting references there's nothing to read */
1010 if (client->nref_updates == client->nref_del)
1013 got_hash_init(&ctx, GOT_HASH_SHA1);
1015 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1018 if (have != sizeof(hdr))
1019 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1022 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1023 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1024 "bad packfile signature");
1025 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1026 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1027 "bad packfile version");
1029 *nobj = be32toh(hdr.nobjects);
1032 * Clients which are creating new references only
1033 * will send us an empty pack file.
1035 if (client->nref_updates > 0 &&
1036 client->nref_updates == client->nref_new)
1040 * Clients which only move existing refs will send us an empty
1041 * pack file. All referenced objects must exist locally.
1043 err = ensure_all_objects_exist_locally(&client->ref_updates);
1045 if (err->code != GOT_ERR_NO_OBJ)
1047 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1048 "bad packfile with zero objects");
1051 client->nref_move = client->nref_updates;
1055 log_debug("expecting %d objects", *nobj);
1057 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1061 err = buf_alloc(&buf, 65536);
1065 while (nhave != *nobj) {
1069 err = copy_object_type_and_size(&obj_type, &obj_size,
1070 infd, outfd, outsize, buf, &buf_pos, &ctx);
1074 err = validate_object_type(obj_type);
1078 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1079 err = copy_ref_delta(infd, outfd, outsize,
1080 buf, &buf_pos, &ctx);
1083 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1084 err = copy_offset_delta(infd, outfd, outsize,
1085 buf, &buf_pos, &ctx);
1090 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1097 log_debug("received %u objects", *nobj);
1099 got_hash_final(&ctx, expected_sha1);
1101 remain = buf_len(buf) - buf_pos;
1102 if (remain < SHA1_DIGEST_LENGTH) {
1103 err = read_more_pack_stream(infd, buf,
1104 SHA1_DIGEST_LENGTH - remain);
1109 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1110 log_debug("expect SHA1: %s", hex);
1111 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1112 log_debug("actual SHA1: %s", hex);
1114 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1115 SHA1_DIGEST_LENGTH) != 0) {
1116 err = got_error(GOT_ERR_PACKFILE_CSUM);
1120 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1122 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1124 err = got_error_from_errno("write");
1127 if (w != SHA1_DIGEST_LENGTH) {
1128 err = got_error(GOT_ERR_IO);
1132 *outsize += SHA1_DIGEST_LENGTH;
1134 if (fsync(outfd) == -1) {
1135 err = got_error_from_errno("fsync");
1138 if (lseek(outfd, 0L, SEEK_SET) == -1) {
1139 err = got_error_from_errno("lseek");
1147 static const struct got_error *
1148 report_pack_status(const struct got_error *unpack_err)
1150 const struct got_error *err = NULL;
1151 struct repo_write_client *client = &repo_write_client;
1152 struct gotd_imsg_packfile_status istatus;
1154 struct imsgbuf ibuf;
1155 const char *unpack_ok = "unpack ok\n";
1158 imsg_init(&ibuf, client->fd);
1161 istatus.reason_len = strlen(unpack_err->msg);
1163 istatus.reason_len = strlen(unpack_ok);
1165 len = sizeof(istatus) + istatus.reason_len;
1166 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1167 repo_write.pid, len);
1169 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1173 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1174 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1178 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1179 istatus.reason_len) == -1) {
1180 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1185 imsg_close(&ibuf, wbuf);
1187 err = gotd_imsg_flush(&ibuf);
1193 static const struct got_error *
1194 recv_packfile(int *have_packfile, struct imsg *imsg)
1196 const struct got_error *err = NULL, *unpack_err;
1197 struct repo_write_client *client = &repo_write_client;
1198 struct gotd_imsg_recv_packfile ireq;
1199 FILE *tempfiles[3] = { NULL, NULL, NULL };
1200 struct repo_tempfile {
1203 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1206 struct imsgbuf ibuf;
1207 struct got_ratelimit rl;
1208 struct got_pack *pack = NULL;
1209 off_t pack_filesize = 0;
1212 log_debug("packfile request received");
1215 got_ratelimit_init(&rl, 2, 0);
1217 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1218 if (datalen != sizeof(ireq))
1219 return got_error(GOT_ERR_PRIVSEP_LEN);
1220 memcpy(&ireq, imsg->data, sizeof(ireq));
1222 if (client->pack_pipe == -1 || client->packidx_fd == -1)
1223 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1225 imsg_init(&ibuf, client->fd);
1228 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1230 pack = &client->pack;
1231 memset(pack, 0, sizeof(*pack));
1232 pack->fd = imsg->fd;
1233 err = got_delta_cache_alloc(&pack->delta_cache);
1237 for (i = 0; i < nitems(repo_tempfiles); i++) {
1238 struct repo_tempfile *t = &repo_tempfiles[i];
1239 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
1244 for (i = 0; i < nitems(tempfiles); i++) {
1248 fd = dup(repo_tempfiles[i].fd);
1250 err = got_error_from_errno("dup");
1253 f = fdopen(fd, "w+");
1255 err = got_error_from_errno("fdopen");
1262 /* Send pack file pipe to gotsh(1). */
1263 if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
1264 repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
1265 (*client)->pack_pipe[1] = -1;
1266 err = got_error_from_errno("imsg_compose ACK");
1270 (*client)->pack_pipe[1] = -1;
1271 err = gotd_imsg_flush(&ibuf);
1275 log_debug("receiving pack data");
1276 unpack_err = recv_packdata(&pack_filesize, &nobj,
1277 client->pack_sha1, client->pack_pipe, pack->fd);
1278 if (ireq.report_status) {
1279 err = report_pack_status(unpack_err);
1281 /* Git clients hang up after sending the pack file. */
1282 if (err->code == GOT_ERR_EOF)
1291 log_debug("pack data received");
1294 * Clients which are creating new references only will
1295 * send us an empty pack file.
1298 pack_filesize == sizeof(struct got_packfile_hdr) &&
1299 client->nref_updates > 0 &&
1300 client->nref_updates == client->nref_new)
1304 * Clients which are deleting references only will send
1308 client->nref_del > 0 &&
1309 client->nref_updates == client->nref_del)
1313 * Clients which only move existing refs will send us an empty
1314 * pack file. All referenced objects must exist locally.
1317 pack_filesize == sizeof(struct got_packfile_hdr) &&
1318 client->nref_move > 0 &&
1319 client->nref_updates == client->nref_move)
1322 pack->filesize = pack_filesize;
1325 log_debug("begin indexing pack (%lld bytes in size)",
1326 (long long)pack->filesize);
1327 err = got_pack_index(pack, client->packidx_fd,
1328 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1329 pack_index_progress, NULL, &rl);
1332 log_debug("done indexing pack");
1334 if (fsync(client->packidx_fd) == -1) {
1335 err = got_error_from_errno("fsync");
1338 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1339 err = got_error_from_errno("lseek");
1341 if (close(client->pack_pipe) == -1 && err == NULL)
1342 err = got_error_from_errno("close");
1343 client->pack_pipe = -1;
1344 for (i = 0; i < nitems(repo_tempfiles); i++) {
1345 struct repo_tempfile *t = &repo_tempfiles[i];
1347 got_repo_temp_fds_put(t->idx, repo_write.repo);
1349 for (i = 0; i < nitems(tempfiles); i++) {
1350 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1351 err = got_error_from_errno("fclose");
1354 got_pack_close(pack);
1359 static const struct got_error *
1360 verify_packfile(void)
1362 const struct got_error *err = NULL, *close_err;
1363 struct repo_write_client *client = &repo_write_client;
1364 struct gotd_ref_update *ref_update;
1365 struct got_packidx *packidx = NULL;
1367 char *id_str = NULL;
1368 struct got_object *obj = NULL;
1369 struct got_pathlist_entry *pe;
1370 char hex[SHA1_DIGEST_STRING_LENGTH];
1372 if (STAILQ_EMPTY(&client->ref_updates)) {
1373 return got_error_msg(GOT_ERR_BAD_REQUEST,
1374 "cannot verify pack file without any ref-updates");
1377 if (client->pack.fd == -1) {
1378 return got_error_msg(GOT_ERR_BAD_REQUEST,
1379 "invalid pack file handle during pack verification");
1381 if (client->packidx_fd == -1) {
1382 return got_error_msg(GOT_ERR_BAD_REQUEST,
1383 "invalid pack index handle during pack verification");
1386 if (fstat(client->packidx_fd, &sb) == -1)
1387 return got_error_from_errno("pack index fstat");
1389 packidx = malloc(sizeof(*packidx));
1390 memset(packidx, 0, sizeof(*packidx));
1391 packidx->fd = client->packidx_fd;
1392 client->packidx_fd = -1;
1393 packidx->len = sb.st_size;
1395 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1399 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1400 if (ref_update->delete_ref)
1403 TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1404 err = protect_tag_namespace(pe->path, &client->pack,
1405 packidx, ref_update);
1411 * Objects which already exist in our repository need
1412 * not be present in the pack file.
1414 err = got_object_open(&obj, repo_write.repo,
1415 &ref_update->new_id);
1416 if (err && err->code != GOT_ERR_NO_OBJ)
1420 got_object_close(obj);
1423 int idx = got_packidx_get_object_idx(packidx,
1424 &ref_update->new_id);
1426 got_sha1_digest_to_str(ref_update->new_id.sha1,
1428 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1429 "object %s is missing from pack file",
1435 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1437 err = protect_branch_namespace(pe->path,
1438 &client->pack, packidx, ref_update);
1442 TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1443 err = protect_branch(pe->path, &client->pack,
1444 packidx, ref_update);
1451 close_err = got_packidx_close(packidx);
1452 if (close_err && err == NULL)
1456 got_object_close(obj);
1460 static const struct got_error *
1461 protect_refs_from_deletion(void)
1463 const struct got_error *err = NULL;
1464 struct repo_write_client *client = &repo_write_client;
1465 struct gotd_ref_update *ref_update;
1466 struct got_pathlist_entry *pe;
1467 const char *refname;
1469 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1470 if (!ref_update->delete_ref)
1473 refname = got_ref_get_name(ref_update->ref);
1475 TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1476 err = protect_ref_namespace(refname, pe->path);
1481 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1483 err = protect_ref_namespace(refname, pe->path);
1488 TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1489 if (strcmp(refname, pe->path) == 0) {
1490 return got_error_fmt(GOT_ERR_REF_PROTECTED,
1499 static const struct got_error *
1500 install_packfile(struct gotd_imsgev *iev)
1502 struct repo_write_client *client = &repo_write_client;
1503 struct gotd_imsg_packfile_install inst;
1506 memset(&inst, 0, sizeof(inst));
1507 inst.client_id = client->id;
1508 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1510 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1511 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1513 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1518 static const struct got_error *
1519 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1521 struct repo_write_client *client = &repo_write_client;
1522 struct gotd_imsg_ref_updates_start istart;
1525 memset(&istart, 0, sizeof(istart));
1526 istart.nref_updates = nref_updates;
1527 istart.client_id = client->id;
1529 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1530 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1532 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1538 static const struct got_error *
1539 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1541 struct repo_write_client *client = &repo_write_client;
1542 struct gotd_imsg_ref_update iref;
1543 const char *refname = got_ref_get_name(ref_update->ref);
1547 memset(&iref, 0, sizeof(iref));
1548 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1549 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1550 iref.ref_is_new = ref_update->ref_is_new;
1551 iref.delete_ref = ref_update->delete_ref;
1552 iref.client_id = client->id;
1553 iref.name_len = strlen(refname);
1555 len = sizeof(iref) + iref.name_len;
1556 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1557 repo_write.pid, len);
1559 return got_error_from_errno("imsg_create REF_UPDATE");
1561 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1562 return got_error_from_errno("imsg_add REF_UPDATE");
1563 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1564 return got_error_from_errno("imsg_add REF_UPDATE");
1567 imsg_close(&iev->ibuf, wbuf);
1569 gotd_imsg_event_add(iev);
1573 static const struct got_error *
1574 update_refs(struct gotd_imsgev *iev)
1576 const struct got_error *err = NULL;
1577 struct repo_write_client *client = &repo_write_client;
1578 struct gotd_ref_update *ref_update;
1580 err = send_ref_updates_start(client->nref_updates, iev);
1584 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1585 err = send_ref_update(ref_update, iev);
1593 static const struct got_error *
1594 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1596 struct repo_write_client *client = &repo_write_client;
1597 struct gotd_imsg_packfile_pipe ireq;
1600 log_debug("receving pack pipe descriptor");
1603 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1605 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1606 if (datalen != sizeof(ireq))
1607 return got_error(GOT_ERR_PRIVSEP_LEN);
1608 memcpy(&ireq, imsg->data, sizeof(ireq));
1610 if (client->pack_pipe != -1)
1611 return got_error(GOT_ERR_PRIVSEP_MSG);
1613 client->pack_pipe = imsg->fd;
1617 static const struct got_error *
1618 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1620 struct repo_write_client *client = &repo_write_client;
1621 struct gotd_imsg_packidx_file ireq;
1624 log_debug("receving pack index output file");
1627 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1629 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1630 if (datalen != sizeof(ireq))
1631 return got_error(GOT_ERR_PRIVSEP_LEN);
1632 memcpy(&ireq, imsg->data, sizeof(ireq));
1634 if (client->packidx_fd != -1)
1635 return got_error(GOT_ERR_PRIVSEP_MSG);
1637 client->packidx_fd = imsg->fd;
1642 repo_write_dispatch_session(int fd, short event, void *arg)
1644 const struct got_error *err = NULL;
1645 struct gotd_imsgev *iev = arg;
1646 struct imsgbuf *ibuf = &iev->ibuf;
1648 struct repo_write_client *client = &repo_write_client;
1650 int shut = 0, have_packfile = 0;
1652 if (event & EV_READ) {
1653 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1654 fatal("imsg_read error");
1655 if (n == 0) /* Connection closed. */
1659 if (event & EV_WRITE) {
1660 n = msgbuf_write(&ibuf->w);
1661 if (n == -1 && errno != EAGAIN)
1662 fatal("msgbuf_write");
1663 if (n == 0) /* Connection closed. */
1668 if ((n = imsg_get(ibuf, &imsg)) == -1)
1669 fatal("%s: imsg_get error", __func__);
1670 if (n == 0) /* No more messages. */
1673 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1675 err = got_error(GOT_ERR_PRIVSEP_MSG);
1679 switch (imsg.hdr.type) {
1680 case GOTD_IMSG_LIST_REFS_INTERNAL:
1681 err = list_refs(&imsg);
1683 log_warnx("ls-refs: %s", err->msg);
1685 case GOTD_IMSG_REF_UPDATE:
1686 err = recv_ref_update(&imsg);
1688 log_warnx("ref-update: %s", err->msg);
1690 case GOTD_IMSG_PACKFILE_PIPE:
1691 err = receive_pack_pipe(&imsg, iev);
1693 log_warnx("receiving pack pipe: %s", err->msg);
1697 case GOTD_IMSG_PACKIDX_FILE:
1698 err = receive_pack_idx(&imsg, iev);
1700 log_warnx("receiving pack index: %s",
1705 case GOTD_IMSG_RECV_PACKFILE:
1706 err = protect_refs_from_deletion();
1709 err = recv_packfile(&have_packfile, &imsg);
1711 log_warnx("receive packfile: %s", err->msg);
1714 if (have_packfile) {
1715 err = verify_packfile();
1717 log_warnx("verify packfile: %s",
1721 err = install_packfile(iev);
1723 log_warnx("install packfile: %s",
1728 err = update_refs(iev);
1730 log_warnx("update refs: %s", err->msg);
1734 log_debug("unexpected imsg %d", imsg.hdr.type);
1741 if (!shut && check_cancelled(NULL) == NULL) {
1743 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1744 client->id, err) == -1) {
1745 log_warnx("could not send error to parent: %s",
1748 gotd_imsg_event_add(iev);
1750 /* This pipe is dead. Remove its event handler */
1751 event_del(&iev->ev);
1752 event_loopexit(NULL);
1756 static const struct got_error *
1757 recv_connect(struct imsg *imsg)
1759 struct gotd_imsgev *iev = &repo_write.session_iev;
1762 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1764 return got_error(GOT_ERR_PRIVSEP_LEN);
1766 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1768 if (repo_write.session_fd != -1)
1769 return got_error(GOT_ERR_PRIVSEP_MSG);
1771 repo_write.session_fd = imsg->fd;
1773 imsg_init(&iev->ibuf, repo_write.session_fd);
1774 iev->handler = repo_write_dispatch_session;
1775 iev->events = EV_READ;
1776 iev->handler_arg = NULL;
1777 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1778 repo_write_dispatch_session, iev);
1779 gotd_imsg_event_add(iev);
1785 repo_write_dispatch(int fd, short event, void *arg)
1787 const struct got_error *err = NULL;
1788 struct gotd_imsgev *iev = arg;
1789 struct imsgbuf *ibuf = &iev->ibuf;
1793 struct repo_write_client *client = &repo_write_client;
1795 if (event & EV_READ) {
1796 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1797 fatal("imsg_read error");
1798 if (n == 0) /* Connection closed. */
1802 if (event & EV_WRITE) {
1803 n = msgbuf_write(&ibuf->w);
1804 if (n == -1 && errno != EAGAIN)
1805 fatal("msgbuf_write");
1806 if (n == 0) /* Connection closed. */
1810 while (err == NULL && check_cancelled(NULL) == NULL) {
1811 if ((n = imsg_get(ibuf, &imsg)) == -1)
1812 fatal("%s: imsg_get", __func__);
1813 if (n == 0) /* No more messages. */
1816 switch (imsg.hdr.type) {
1817 case GOTD_IMSG_CONNECT_REPO_CHILD:
1818 err = recv_connect(&imsg);
1821 log_debug("unexpected imsg %d", imsg.hdr.type);
1828 if (!shut && check_cancelled(NULL) == NULL) {
1830 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1831 client->id, err) == -1) {
1832 log_warnx("could not send error to parent: %s",
1835 gotd_imsg_event_add(iev);
1837 /* This pipe is dead. Remove its event handler */
1838 event_del(&iev->ev);
1839 event_loopexit(NULL);
1844 repo_write_main(const char *title, const char *repo_path,
1845 int *pack_fds, int *temp_fds,
1846 struct got_pathlist_head *protected_tag_namespaces,
1847 struct got_pathlist_head *protected_branch_namespaces,
1848 struct got_pathlist_head *protected_branches)
1850 const struct got_error *err = NULL;
1851 struct repo_write_client *client = &repo_write_client;
1852 struct gotd_imsgev iev;
1855 client->pack_pipe = -1;
1856 client->packidx_fd = -1;
1857 client->pack.fd = -1;
1859 repo_write.title = title;
1860 repo_write.pid = getpid();
1861 repo_write.pack_fds = pack_fds;
1862 repo_write.temp_fds = temp_fds;
1863 repo_write.session_fd = -1;
1864 repo_write.session_iev.ibuf.fd = -1;
1865 repo_write.protected_tag_namespaces = protected_tag_namespaces;
1866 repo_write.protected_branch_namespaces = protected_branch_namespaces;
1867 repo_write.protected_branches = protected_branches;
1869 STAILQ_INIT(&repo_write_client.ref_updates);
1871 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1874 if (!got_repo_is_bare(repo_write.repo)) {
1875 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1876 "bare git repository required");
1880 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1882 signal(SIGINT, catch_sigint);
1883 signal(SIGTERM, catch_sigterm);
1884 signal(SIGPIPE, SIG_IGN);
1885 signal(SIGHUP, SIG_IGN);
1887 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1888 iev.handler = repo_write_dispatch;
1889 iev.events = EV_READ;
1890 iev.handler_arg = NULL;
1891 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1892 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1893 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1894 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1901 log_warnx("%s: %s", title, err->msg);
1902 repo_write_shutdown();
1906 repo_write_shutdown(void)
1908 struct repo_write_client *client = &repo_write_client;
1909 struct gotd_ref_update *ref_update;
1911 log_debug("shutting down");
1913 while (!STAILQ_EMPTY(&client->ref_updates)) {
1914 ref_update = STAILQ_FIRST(&client->ref_updates);
1915 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1916 got_ref_close(ref_update->ref);
1920 got_pack_close(&client->pack);
1921 if (client->fd != -1)
1923 if (client->pack_pipe != -1)
1924 close(client->pack_pipe);
1925 if (client->packidx_fd != -1)
1926 close(client->packidx_fd);
1928 if (repo_write.repo)
1929 got_repo_close(repo_write.repo);
1930 got_repo_pack_fds_close(repo_write.pack_fds);
1931 got_repo_temp_fds_close(repo_write.temp_fds);
1932 if (repo_write.session_fd != -1)
1933 close(repo_write.session_fd);