Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
23 #include <event.h>
24 #include <errno.h>
25 #include <imsg.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <poll.h>
32 #include <unistd.h>
33 #include <zlib.h>
35 #include "buf.h"
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_path.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"
56 #include "log.h"
57 #include "gotd.h"
58 #include "repo_write.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 static struct repo_write {
65 pid_t pid;
66 const char *title;
67 struct got_repository *repo;
68 int *pack_fds;
69 int *temp_fds;
70 int session_fd;
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;
75 } repo_write;
77 struct gotd_ref_update {
78 STAILQ_ENTRY(gotd_ref_update) entry;
79 struct got_reference *ref;
80 int ref_is_new;
81 int delete_ref;
82 struct got_object_id old_id;
83 struct got_object_id new_id;
84 };
85 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
87 static struct repo_write_client {
88 uint32_t id;
89 int fd;
90 int pack_pipe;
91 struct got_pack pack;
92 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
93 int packidx_fd;
94 struct gotd_ref_updates ref_updates;
95 int nref_updates;
96 int nref_del;
97 int nref_new;
98 int nref_move;
99 } repo_write_client;
101 static volatile sig_atomic_t sigint_received;
102 static volatile sig_atomic_t sigterm_received;
104 static void
105 catch_sigint(int signo)
107 sigint_received = 1;
110 static void
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);
122 return NULL;
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;
131 size_t namelen, len;
132 char *peeled_refname = NULL;
133 struct got_object_id *id;
134 struct ibuf *wbuf;
136 err = got_object_tag_open(&tag, repo_write.repo, obj);
137 if (err)
138 return err;
140 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
141 err = got_error_from_errno("asprintf");
142 goto done;
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);
151 goto done;
154 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
155 repo_write.pid, len);
156 if (wbuf == NULL) {
157 err = got_error_from_errno("imsg_create REF");
158 goto done;
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");
164 goto done;
166 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
167 err = got_error_from_errno("imsg_add REF");
168 goto done;
170 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
171 err = got_error_from_errno("imsg_add REF");
172 goto done;
175 wbuf->fd = -1;
176 imsg_close(ibuf, wbuf);
177 done:
178 got_object_tag_close(tag);
179 return err;
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);
187 size_t namelen;
188 struct got_object_id *id = NULL;
189 struct got_object *obj = NULL;
190 size_t len;
191 struct ibuf *wbuf;
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);
200 if (err)
201 return err;
203 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
204 repo_write.pid, len);
205 if (wbuf == NULL) {
206 err = got_error_from_errno("imsg_create REF");
207 goto done;
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");
218 wbuf->fd = -1;
219 imsg_close(ibuf, wbuf);
221 err = got_object_open(&obj, repo_write.repo, id);
222 if (err)
223 goto done;
224 if (obj->type == GOT_OBJ_TYPE_TAG)
225 err = send_peeled_tag_ref(ref, obj, ibuf);
226 done:
227 if (obj)
228 got_object_close(obj);
229 free(id);
230 return err;
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;
241 size_t datalen;
242 struct gotd_imsg_reflist irefs;
243 struct imsgbuf ibuf;
244 int client_fd = imsg->fd;
246 TAILQ_INIT(&refs);
248 if (client_fd == -1)
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);
273 if (err)
274 return err;
276 memset(&irefs, 0, sizeof(irefs));
277 TAILQ_FOREACH(re, &refs, entry) {
278 struct got_object_id *id;
279 int obj_type;
281 if (got_ref_is_symbolic(re->ref))
282 continue;
284 irefs.nrefs++;
286 /* Account for a peeled tag refs. */
287 err = got_ref_resolve(&id, repo_write.repo, re->ref);
288 if (err)
289 goto done;
290 err = got_object_get_type(&obj_type, repo_write.repo, id);
291 free(id);
292 if (err)
293 goto done;
294 if (obj_type == GOT_OBJ_TYPE_TAG)
295 irefs.nrefs++;
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");
301 goto done;
304 TAILQ_FOREACH(re, &refs, entry) {
305 if (got_ref_is_symbolic(re->ref))
306 continue;
307 err = send_ref(re->ref, &ibuf);
308 if (err)
309 goto done;
312 err = gotd_imsg_flush(&ibuf);
313 done:
314 got_ref_list_free(&refs);
315 imsg_clear(&ibuf);
316 return err;
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);
330 return NULL;
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);
339 if (err)
340 return err;
342 if (strncmp(namespace, refname, strlen(namespace)) == 0)
343 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
345 return NULL;
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;
355 int idx;
356 const char *typestr;
358 idx = got_packidx_get_object_idx(packidx, id);
359 if (idx == -1) {
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);
367 if (err)
368 return err;
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);
377 return err;
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);
387 if (err)
388 return err;
390 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
391 strlen(namespace)) != 0)
392 return NULL;
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,
398 pack, packidx);
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;
407 uint8_t *buf = NULL;
408 size_t len;
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;
419 STAILQ_INIT(&ids);
421 err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
422 if (err)
423 return err;
425 err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
426 if (err)
427 goto done;
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);
433 goto done;
436 traversed_set = got_object_idset_alloc();
437 if (traversed_set == NULL) {
438 err = got_error_from_errno("got_object_idset_alloc");
439 goto done;
442 err = got_object_qid_alloc(&qid, tip_id);
443 if (err)
444 goto done;
445 STAILQ_INSERT_TAIL(&ids, qid, entry);
446 while (!STAILQ_EMPTY(&ids)) {
447 err = check_cancelled(NULL);
448 if (err)
449 break;
451 qid = STAILQ_FIRST(&ids);
452 if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
453 found_yca = 1;
454 break;
457 if (got_object_idset_num_elements(traversed_set) >=
458 max_commits_to_traverse)
459 break;
461 if (got_object_idset_contains(traversed_set, &qid->id)) {
462 STAILQ_REMOVE_HEAD(&ids, entry);
463 got_object_qid_free(qid);
464 qid = NULL;
465 continue;
467 err = got_object_idset_add(traversed_set, &qid->id, NULL);
468 if (err)
469 goto done;
471 err = got_object_open(&obj, repo_write.repo, &qid->id);
472 if (err && err->code != GOT_ERR_NO_OBJ)
473 goto done;
474 err = NULL;
475 if (obj) {
476 err = got_object_commit_open(&commit, repo_write.repo,
477 obj);
478 if (err)
479 goto done;
480 } else {
481 int idx;
483 idx = got_packidx_get_object_idx(packidx, &qid->id);
484 if (idx == -1) {
485 got_sha1_digest_to_str(qid->id.sha1,
486 hex, sizeof(hex));
487 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
488 "object %s is missing from pack file", hex);
489 goto done;
492 err = got_object_open_from_packfile(&obj, &qid->id,
493 pack, packidx, idx, repo_write.repo);
494 if (err)
495 goto done;
497 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
498 got_sha1_digest_to_str(qid->id.sha1,
499 hex, sizeof(hex));
500 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
501 "%s is not pointing at a commit object",
502 hex);
503 goto done;
506 err = got_packfile_extract_object_to_mem(&buf, &len,
507 obj, pack);
508 if (err)
509 goto done;
511 err = got_object_parse_commit(&commit, buf, len);
512 if (err)
513 goto done;
515 free(buf);
516 buf = NULL;
519 got_object_close(obj);
520 obj = NULL;
522 STAILQ_REMOVE_HEAD(&ids, entry);
523 got_object_qid_free(qid);
524 qid = NULL;
526 if (got_object_commit_get_nparents(commit) == 0)
527 break;
529 parent_ids = got_object_commit_get_parent_ids(commit);
530 STAILQ_FOREACH(pid, parent_ids, entry) {
531 err = check_cancelled(NULL);
532 if (err)
533 goto done;
534 err = got_object_qid_alloc(&qid, &pid->id);
535 if (err)
536 goto done;
537 STAILQ_INSERT_TAIL(&ids, qid, entry);
538 qid = NULL;
540 got_object_commit_close(commit);
541 commit = NULL;
544 if (!found_yca) {
545 err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
546 got_ref_get_name(ref));
548 done:
549 got_object_idset_free(traversed_set);
550 got_object_id_queue_free(&ids);
551 free(buf);
552 if (obj)
553 got_object_close(obj);
554 if (commit)
555 got_object_commit_close(commit);
556 free(expected_yca_id);
557 return err;
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);
567 if (err)
568 return err;
570 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
571 strlen(namespace)) != 0)
572 return NULL;
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,
581 ref_update->ref);
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)
589 return NULL;
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,
599 ref_update->ref);
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;
609 size_t datalen;
610 char *refname = NULL;
611 struct got_reference *ref = NULL;
612 struct got_object_id *id = NULL;
613 struct imsgbuf ibuf;
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);
628 if (refname == NULL)
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");
634 goto done;
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);
641 if (err) {
642 if (err->code != GOT_ERR_NOT_REF)
643 goto done;
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,
647 "%s", refname);
648 goto done;
650 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
651 if (err)
652 goto done;
653 ref_update->ref_is_new = 1;
654 client->nref_new++;
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));
660 goto done;
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));
666 goto done;
669 err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
670 if (err)
671 goto done;
672 err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
673 if (err)
674 goto done;
676 if (!ref_update->ref_is_new) {
677 /*
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.
682 */
683 err = got_ref_resolve(&id, repo_write.repo, ref);
684 if (err)
685 goto done;
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));
692 goto done;
696 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
697 repo_write.pid);
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;
702 client->nref_del++;
704 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
705 client->nref_updates++;
706 ref = NULL;
707 ref_update = NULL;
708 done:
709 if (ref)
710 got_ref_close(ref);
711 free(ref_update);
712 free(refname);
713 free(id);
714 return err;
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;
724 if (nobj_total > 0)
725 p_indexed = (nobj_indexed * 100) / nobj_total;
727 if (nobj_delta > 0)
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);
733 } else
734 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
736 return NULL;
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];
744 size_t have, newlen;
746 err = got_poll_read_full(infd, &have,
747 readahead, sizeof(readahead), minsize);
748 if (err)
749 return err;
751 err = buf_append(&newlen, buf, readahead, have);
752 if (err)
753 return err;
754 return NULL;
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;
762 uint8_t t = 0;
763 uint64_t s = 0;
764 uint8_t sizebuf[8];
765 size_t i = 0;
766 off_t obj_offset = *outsize;
768 do {
769 /* We do not support size values which don't fit in 64 bit. */
770 if (i > 9)
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,
776 sizeof(sizebuf[0]));
777 if (err)
778 return err;
781 sizebuf[i] = buf_getc(buf, *buf_pos);
782 *buf_pos += sizeof(sizebuf[i]);
784 if (i == 0) {
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);
788 } else {
789 size_t shift = 4 + 7 * (i - 1);
790 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
791 shift);
793 i++;
794 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
796 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
797 if (err)
798 return err;
799 *outsize += i;
801 *type = t;
802 *size = s;
803 return NULL;
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);
816 if (err)
817 return err;
820 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
821 SHA1_DIGEST_LENGTH, ctx);
822 if (err)
823 return err;
825 *buf_pos += SHA1_DIGEST_LENGTH;
826 return NULL;
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;
834 uint64_t o = 0;
835 uint8_t offbuf[8];
836 size_t i = 0;
837 off_t obj_offset = *outsize;
839 do {
840 /* We do not support offset values which don't fit in 64 bit. */
841 if (i > 8)
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,
847 sizeof(offbuf[0]));
848 if (err)
849 return err;
852 offbuf[i] = buf_getc(buf, *buf_pos);
853 *buf_pos += sizeof(offbuf[i]);
855 if (i == 0)
856 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
857 else {
858 o++;
859 o <<= 7;
860 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
862 i++;
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);
869 if (err)
870 return err;
872 *outsize += i;
873 return NULL;
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;
881 z_stream z;
882 int zret;
883 char voidbuf[1024];
884 size_t consumed_total = 0;
885 off_t zstream_offset = *outsize;
887 memset(&z, 0, sizeof(z));
889 z.zalloc = Z_NULL;
890 z.zfree = Z_NULL;
891 zret = inflateInit(&z);
892 if (zret != Z_OK) {
893 if (zret == Z_ERRNO)
894 return got_error_from_errno("inflateInit");
895 if (zret == Z_MEM_ERROR) {
896 errno = ENOMEM;
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;
906 /*
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.
910 */
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);
924 goto done;
926 consumed = z.total_in - last_total_in;
928 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
929 consumed, ctx);
930 if (err)
931 goto done;
933 err = buf_discard(buf, *buf_pos + consumed);
934 if (err)
935 goto done;
936 *buf_pos = 0;
938 consumed_total += consumed;
941 if (zret != Z_STREAM_END) {
942 err = read_more_pack_stream(infd, buf, 1);
943 if (err)
944 goto done;
948 if (err == NULL)
949 *outsize += consumed_total;
950 done:
951 inflateEnd(&z);
952 return err;
955 static const struct got_error *
956 validate_object_type(int obj_type)
958 switch (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:
965 return NULL;
966 default:
967 break;
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);
983 if (err)
984 return err;
985 got_object_close(obj);
988 return NULL;
991 static const struct got_error *
992 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
993 int infd, int outfd)
995 const struct got_error *err;
996 struct repo_write_client *client = &repo_write_client;
997 struct got_packfile_hdr hdr;
998 size_t have;
999 uint32_t nhave = 0;
1000 struct got_hash ctx;
1001 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1002 char hex[SHA1_DIGEST_STRING_LENGTH];
1003 BUF *buf = NULL;
1004 size_t buf_pos = 0, remain;
1005 ssize_t w;
1007 *outsize = 0;
1008 *nobj = 0;
1010 /* if only deleting references there's nothing to read */
1011 if (client->nref_updates == client->nref_del)
1012 return NULL;
1014 got_hash_init(&ctx, GOT_HASH_SHA1);
1016 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1017 if (err)
1018 return err;
1019 if (have != sizeof(hdr))
1020 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1021 *outsize += have;
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);
1031 if (*nobj == 0) {
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)
1038 return NULL;
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);
1045 if (err) {
1046 if (err->code != GOT_ERR_NO_OBJ)
1047 return err;
1048 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1049 "bad packfile with zero objects");
1052 client->nref_move = client->nref_updates;
1053 return NULL;
1056 log_debug("expecting %d objects", *nobj);
1058 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1059 if (err)
1060 return err;
1062 err = buf_alloc(&buf, 65536);
1063 if (err)
1064 return err;
1066 while (nhave != *nobj) {
1067 uint8_t obj_type;
1068 uint64_t obj_size;
1070 err = copy_object_type_and_size(&obj_type, &obj_size,
1071 infd, outfd, outsize, buf, &buf_pos, &ctx);
1072 if (err)
1073 goto done;
1075 err = validate_object_type(obj_type);
1076 if (err)
1077 goto done;
1079 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1080 err = copy_ref_delta(infd, outfd, outsize,
1081 buf, &buf_pos, &ctx);
1082 if (err)
1083 goto done;
1084 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1085 err = copy_offset_delta(infd, outfd, outsize,
1086 buf, &buf_pos, &ctx);
1087 if (err)
1088 goto done;
1091 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1092 if (err)
1093 goto done;
1095 nhave++;
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);
1106 if (err)
1107 return err;
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);
1118 goto done;
1121 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1123 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1124 if (w == -1) {
1125 err = got_error_from_errno("write");
1126 goto done;
1128 if (w != SHA1_DIGEST_LENGTH) {
1129 err = got_error(GOT_ERR_IO);
1130 goto done;
1133 *outsize += SHA1_DIGEST_LENGTH;
1135 if (fsync(outfd) == -1) {
1136 err = got_error_from_errno("fsync");
1137 goto done;
1139 if (lseek(outfd, 0L, SEEK_SET) == -1) {
1140 err = got_error_from_errno("lseek");
1141 goto done;
1143 done:
1144 buf_free(buf);
1145 return err;
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;
1154 struct ibuf *wbuf;
1155 struct imsgbuf ibuf;
1156 const char *unpack_ok = "unpack ok\n";
1157 size_t len;
1159 imsg_init(&ibuf, client->fd);
1161 if (unpack_err)
1162 istatus.reason_len = strlen(unpack_err->msg);
1163 else
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);
1169 if (wbuf == NULL) {
1170 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1171 goto done;
1174 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1175 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1176 goto done;
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");
1182 goto done;
1185 wbuf->fd = -1;
1186 imsg_close(&ibuf, wbuf);
1188 err = gotd_imsg_flush(&ibuf);
1189 done:
1190 imsg_clear(&ibuf);
1191 return err;
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 {
1202 int fd;
1203 int idx;
1204 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1205 int i;
1206 size_t datalen;
1207 struct imsgbuf ibuf;
1208 struct got_ratelimit rl;
1209 struct got_pack *pack = NULL;
1210 off_t pack_filesize = 0;
1211 uint32_t nobj = 0;
1213 log_debug("packfile request received");
1215 *have_packfile = 0;
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);
1228 if (imsg->fd == -1)
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);
1235 if (err)
1236 return err;
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);
1241 if (err)
1242 goto done;
1245 for (i = 0; i < nitems(tempfiles); i++) {
1246 int fd;
1247 FILE *f;
1249 fd = dup(repo_tempfiles[i].fd);
1250 if (fd == -1) {
1251 err = got_error_from_errno("dup");
1252 goto done;
1254 f = fdopen(fd, "w+");
1255 if (f == NULL) {
1256 err = got_error_from_errno("fdopen");
1257 close(fd);
1258 goto done;
1260 tempfiles[i] = f;
1263 err = gotd_imsg_flush(&ibuf);
1264 if (err)
1265 goto done;
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);
1272 if (err) {
1273 /* Git clients hang up after sending the pack file. */
1274 if (err->code == GOT_ERR_EOF)
1275 err = NULL;
1278 if (unpack_err)
1279 err = unpack_err;
1280 if (err)
1281 goto done;
1283 log_debug("pack data received");
1286 * Clients which are creating new references only will
1287 * send us an empty pack file.
1289 if (nobj == 0 &&
1290 pack_filesize == sizeof(struct got_packfile_hdr) &&
1291 client->nref_updates > 0 &&
1292 client->nref_updates == client->nref_new)
1293 goto done;
1296 * Clients which are deleting references only will send
1297 * no pack file.
1299 if (nobj == 0 &&
1300 client->nref_del > 0 &&
1301 client->nref_updates == client->nref_del)
1302 goto done;
1305 * Clients which only move existing refs will send us an empty
1306 * pack file. All referenced objects must exist locally.
1308 if (nobj == 0 &&
1309 pack_filesize == sizeof(struct got_packfile_hdr) &&
1310 client->nref_move > 0 &&
1311 client->nref_updates == client->nref_move)
1312 goto done;
1314 pack->filesize = pack_filesize;
1315 *have_packfile = 1;
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);
1322 if (err)
1323 goto done;
1324 log_debug("done indexing pack");
1326 if (fsync(client->packidx_fd) == -1) {
1327 err = got_error_from_errno("fsync");
1328 goto done;
1330 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1331 err = got_error_from_errno("lseek");
1332 done:
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];
1338 if (t->idx != -1)
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");
1345 if (err)
1346 got_pack_close(pack);
1347 imsg_clear(&ibuf);
1348 return err;
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;
1358 struct stat sb;
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);
1388 if (err)
1389 return err;
1391 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1392 if (ref_update->delete_ref)
1393 continue;
1395 TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1396 err = protect_tag_namespace(pe->path, &client->pack,
1397 packidx, ref_update);
1398 if (err)
1399 goto done;
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)
1409 goto done;
1410 err = NULL;
1411 if (obj) {
1412 got_object_close(obj);
1413 obj = NULL;
1414 } else {
1415 int idx = got_packidx_get_object_idx(packidx,
1416 &ref_update->new_id);
1417 if (idx == -1) {
1418 got_sha1_digest_to_str(ref_update->new_id.sha1,
1419 hex, sizeof(hex));
1420 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1421 "object %s is missing from pack file",
1422 hex);
1423 goto done;
1427 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1428 entry) {
1429 err = protect_branch_namespace(pe->path,
1430 &client->pack, packidx, ref_update);
1431 if (err)
1432 goto done;
1434 TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1435 err = protect_branch(pe->path, &client->pack,
1436 packidx, ref_update);
1437 if (err)
1438 goto done;
1442 done:
1443 close_err = got_packidx_close(packidx);
1444 if (close_err && err == NULL)
1445 err = close_err;
1446 free(id_str);
1447 if (obj)
1448 got_object_close(obj);
1449 return err;
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)
1463 continue;
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);
1469 if (err)
1470 return err;
1473 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1474 entry) {
1475 err = protect_ref_namespace(refname, pe->path);
1476 if (err)
1477 return err;
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,
1483 "%s", refname);
1488 return NULL;
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;
1496 int ret;
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));
1504 if (ret == -1)
1505 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1507 return NULL;
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;
1515 int ret;
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));
1523 if (ret == -1)
1524 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1526 return NULL;
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);
1536 struct ibuf *wbuf;
1537 size_t len;
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);
1550 if (wbuf == NULL)
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");
1558 wbuf->fd = -1;
1559 imsg_close(&iev->ibuf, wbuf);
1561 gotd_imsg_event_add(iev);
1562 return NULL;
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);
1573 if (err)
1574 return err;
1576 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1577 err = send_ref_update(ref_update, iev);
1578 if (err)
1579 goto done;
1581 done:
1582 return err;
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;
1590 size_t datalen;
1592 log_debug("receiving pack pipe descriptor");
1594 if (imsg->fd == -1)
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;
1606 return NULL;
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;
1614 size_t datalen;
1616 log_debug("receiving pack index output file");
1618 if (imsg->fd == -1)
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;
1630 return NULL;
1633 static void
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;
1639 struct imsg imsg;
1640 struct repo_write_client *client = &repo_write_client;
1641 ssize_t n;
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. */
1648 shut = 1;
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. */
1656 shut = 1;
1659 for (;;) {
1660 if ((n = imsg_get(ibuf, &imsg)) == -1)
1661 fatal("%s: imsg_get error", __func__);
1662 if (n == 0) /* No more messages. */
1663 break;
1665 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1666 client->id == 0) {
1667 err = got_error(GOT_ERR_PRIVSEP_MSG);
1668 break;
1671 switch (imsg.hdr.type) {
1672 case GOTD_IMSG_LIST_REFS_INTERNAL:
1673 err = list_refs(&imsg);
1674 if (err)
1675 log_warnx("ls-refs: %s", err->msg);
1676 break;
1677 case GOTD_IMSG_REF_UPDATE:
1678 err = recv_ref_update(&imsg);
1679 if (err)
1680 log_warnx("ref-update: %s", err->msg);
1681 break;
1682 case GOTD_IMSG_PACKFILE_PIPE:
1683 err = receive_pack_pipe(&imsg, iev);
1684 if (err) {
1685 log_warnx("receiving pack pipe: %s", err->msg);
1686 break;
1688 break;
1689 case GOTD_IMSG_PACKIDX_FILE:
1690 err = receive_pack_idx(&imsg, iev);
1691 if (err) {
1692 log_warnx("receiving pack index: %s",
1693 err->msg);
1694 break;
1696 break;
1697 case GOTD_IMSG_RECV_PACKFILE:
1698 err = protect_refs_from_deletion();
1699 if (err)
1700 break;
1701 err = recv_packfile(&have_packfile, &imsg);
1702 if (err) {
1703 log_warnx("receive packfile: %s", err->msg);
1704 break;
1706 if (have_packfile) {
1707 err = verify_packfile();
1708 if (err) {
1709 log_warnx("verify packfile: %s",
1710 err->msg);
1711 break;
1713 err = install_packfile(iev);
1714 if (err) {
1715 log_warnx("install packfile: %s",
1716 err->msg);
1717 break;
1720 err = update_refs(iev);
1721 if (err) {
1722 log_warnx("update refs: %s", err->msg);
1724 break;
1725 default:
1726 log_debug("unexpected imsg %d", imsg.hdr.type);
1727 break;
1730 imsg_free(&imsg);
1733 if (!shut && check_cancelled(NULL) == NULL) {
1734 if (err &&
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",
1738 err->msg);
1740 gotd_imsg_event_add(iev);
1741 } else {
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;
1752 size_t datalen;
1754 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1755 if (datalen != 0)
1756 return got_error(GOT_ERR_PRIVSEP_LEN);
1757 if (imsg->fd == -1)
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);
1773 return NULL;
1776 static void
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;
1782 struct imsg imsg;
1783 ssize_t n;
1784 int shut = 0;
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. */
1791 shut = 1;
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. */
1799 shut = 1;
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. */
1806 break;
1808 switch (imsg.hdr.type) {
1809 case GOTD_IMSG_CONNECT_REPO_CHILD:
1810 err = recv_connect(&imsg);
1811 break;
1812 default:
1813 log_debug("unexpected imsg %d", imsg.hdr.type);
1814 break;
1817 imsg_free(&imsg);
1820 if (!shut && check_cancelled(NULL) == NULL) {
1821 if (err &&
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",
1825 err->msg);
1827 gotd_imsg_event_add(iev);
1828 } else {
1829 /* This pipe is dead. Remove its event handler */
1830 event_del(&iev->ev);
1831 event_loopexit(NULL);
1835 void
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;
1846 client->fd = -1;
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);
1864 if (err)
1865 goto done;
1866 if (!got_repo_is_bare(repo_write.repo)) {
1867 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1868 "bare git repository required");
1869 goto done;
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");
1887 goto done;
1890 event_dispatch();
1891 done:
1892 if (err)
1893 log_warnx("%s: %s", title, err->msg);
1894 repo_write_shutdown();
1897 void
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);
1909 free(ref_update);
1912 got_pack_close(&client->pack);
1913 if (client->fd != -1)
1914 close(client->fd);
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);
1926 exit(0);