store ibuf used by got_repo_read_gitconfig() on the stack
[got-portable.git] / gotd / repo_write.c
blob50c0f8408c2cdb65e1f0a442c26f9f38c1be1cb1
1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
23 #include <ctype.h>
24 #include <event.h>
25 #include <errno.h>
26 #include <imsg.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <poll.h>
33 #include <unistd.h>
34 #include <zlib.h>
36 #include "buf.h"
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_path.h"
43 #include "got_diff.h"
44 #include "got_cancel.h"
45 #include "got_commit_graph.h"
46 #include "got_opentemp.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_delta_cache.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_ratelimit.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_pack_index.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_poll.h"
61 #include "log.h"
62 #include "gotd.h"
63 #include "repo_write.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 static struct repo_write {
70 pid_t pid;
71 const char *title;
72 struct got_repository *repo;
73 int *pack_fds;
74 int *temp_fds;
75 int session_fd;
76 struct gotd_imsgev session_iev;
77 struct got_pathlist_head *protected_tag_namespaces;
78 struct got_pathlist_head *protected_branch_namespaces;
79 struct got_pathlist_head *protected_branches;
80 struct {
81 FILE *f1;
82 FILE *f2;
83 int fd1;
84 int fd2;
85 } diff;
86 int refs_listed;
87 } repo_write;
89 struct gotd_ref_update {
90 STAILQ_ENTRY(gotd_ref_update) entry;
91 struct got_reference *ref;
92 int ref_is_new;
93 int delete_ref;
94 struct got_object_id old_id;
95 struct got_object_id new_id;
97 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
99 static struct repo_write_client {
100 uint32_t id;
101 int fd;
102 int pack_pipe;
103 struct got_pack pack;
104 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
105 int packidx_fd;
106 struct gotd_ref_updates ref_updates;
107 int nref_updates;
108 int nref_del;
109 int nref_new;
110 int nref_move;
111 } repo_write_client;
113 static volatile sig_atomic_t sigint_received;
114 static volatile sig_atomic_t sigterm_received;
116 static void
117 catch_sigint(int signo)
119 sigint_received = 1;
122 static void
123 catch_sigterm(int signo)
125 sigterm_received = 1;
128 static const struct got_error *
129 check_cancelled(void *arg)
131 if (sigint_received || sigterm_received)
132 return got_error(GOT_ERR_CANCELLED);
134 return NULL;
137 static const struct got_error *
138 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
139 struct imsgbuf *ibuf)
141 const struct got_error *err = NULL;
142 struct got_tag_object *tag;
143 size_t namelen, len;
144 char *peeled_refname = NULL;
145 struct got_object_id *id;
146 struct ibuf *wbuf;
148 err = got_object_tag_open(&tag, repo_write.repo, obj);
149 if (err)
150 return err;
152 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
153 err = got_error_from_errno("asprintf");
154 goto done;
157 id = got_object_tag_get_object_id(tag);
158 namelen = strlen(peeled_refname);
160 len = sizeof(struct gotd_imsg_ref) + namelen;
161 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
162 err = got_error(GOT_ERR_NO_SPACE);
163 goto done;
166 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
167 repo_write.pid, len);
168 if (wbuf == NULL) {
169 err = got_error_from_errno("imsg_create REF");
170 goto done;
173 /* Keep in sync with struct gotd_imsg_ref definition. */
174 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
175 err = got_error_from_errno("imsg_add REF");
176 goto done;
178 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
179 err = got_error_from_errno("imsg_add REF");
180 goto done;
182 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
183 err = got_error_from_errno("imsg_add REF");
184 goto done;
187 imsg_close(ibuf, wbuf);
188 done:
189 got_object_tag_close(tag);
190 return err;
193 static const struct got_error *
194 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
196 const struct got_error *err;
197 const char *refname = got_ref_get_name(ref);
198 size_t namelen;
199 struct got_object_id *id = NULL;
200 struct got_object *obj = NULL;
201 size_t len;
202 struct ibuf *wbuf;
204 namelen = strlen(refname);
206 len = sizeof(struct gotd_imsg_ref) + namelen;
207 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
208 return got_error(GOT_ERR_NO_SPACE);
210 err = got_ref_resolve(&id, repo_write.repo, ref);
211 if (err)
212 return err;
214 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
215 repo_write.pid, len);
216 if (wbuf == NULL) {
217 err = got_error_from_errno("imsg_create REF");
218 goto done;
221 /* Keep in sync with struct gotd_imsg_ref definition. */
222 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
223 return got_error_from_errno("imsg_add REF");
224 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
225 return got_error_from_errno("imsg_add REF");
226 if (imsg_add(wbuf, refname, namelen) == -1)
227 return got_error_from_errno("imsg_add REF");
229 imsg_close(ibuf, wbuf);
231 err = got_object_open(&obj, repo_write.repo, id);
232 if (err)
233 goto done;
234 if (obj->type == GOT_OBJ_TYPE_TAG)
235 err = send_peeled_tag_ref(ref, obj, ibuf);
236 done:
237 if (obj)
238 got_object_close(obj);
239 free(id);
240 return err;
243 static const struct got_error *
244 list_refs(struct imsg *imsg)
246 const struct got_error *err;
247 struct repo_write_client *client = &repo_write_client;
248 struct got_reflist_head refs;
249 struct got_reflist_entry *re;
250 size_t datalen;
251 struct gotd_imsg_reflist irefs;
252 struct imsgbuf ibuf;
254 TAILQ_INIT(&refs);
256 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
257 if (datalen != 0)
258 return got_error(GOT_ERR_PRIVSEP_LEN);
260 if (repo_write.refs_listed) {
261 return got_error_msg(GOT_ERR_CLIENT_ID,
262 "duplicate list-refs request");
264 repo_write.refs_listed = 1;
266 client->fd = imsg_get_fd(imsg);
267 if (client->fd == -1)
268 return got_error(GOT_ERR_PRIVSEP_NO_FD);
270 client->nref_updates = 0;
271 client->nref_del = 0;
272 client->nref_new = 0;
273 client->nref_move = 0;
275 if (imsgbuf_init(&ibuf, client->fd) == -1)
276 return got_error_from_errno("imsgbuf_init");
277 imsgbuf_allow_fdpass(&ibuf);
279 err = got_ref_list(&refs, repo_write.repo, "",
280 got_ref_cmp_by_name, NULL);
281 if (err)
282 return err;
284 memset(&irefs, 0, sizeof(irefs));
285 TAILQ_FOREACH(re, &refs, entry) {
286 struct got_object_id *id;
287 int obj_type;
289 if (got_ref_is_symbolic(re->ref))
290 continue;
292 irefs.nrefs++;
294 /* Account for a peeled tag refs. */
295 err = got_ref_resolve(&id, repo_write.repo, re->ref);
296 if (err)
297 goto done;
298 err = got_object_get_type(&obj_type, repo_write.repo, id);
299 free(id);
300 if (err)
301 goto done;
302 if (obj_type == GOT_OBJ_TYPE_TAG)
303 irefs.nrefs++;
306 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
307 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
308 err = got_error_from_errno("imsg_compose REFLIST");
309 goto done;
312 TAILQ_FOREACH(re, &refs, entry) {
313 if (got_ref_is_symbolic(re->ref))
314 continue;
315 err = send_ref(re->ref, &ibuf);
316 if (err)
317 goto done;
320 err = gotd_imsg_flush(&ibuf);
321 done:
322 got_ref_list_free(&refs);
323 imsgbuf_clear(&ibuf);
324 return err;
327 static const struct got_error *
328 validate_namespace(const char *namespace)
330 size_t len = strlen(namespace);
332 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
333 namespace[len -1] != '/') {
334 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
335 "reference namespace '%s'", namespace);
338 return NULL;
341 static const struct got_error *
342 protect_ref_namespace(const char *refname, const char *namespace)
344 const struct got_error *err;
346 err = validate_namespace(namespace);
347 if (err)
348 return err;
350 if (strncmp(namespace, refname, strlen(namespace)) == 0)
351 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
353 return NULL;
356 static const struct got_error *
357 verify_object_type(struct got_object_id *id, int expected_obj_type,
358 struct got_pack *pack, struct got_packidx *packidx)
360 const struct got_error *err;
361 char hex[SHA1_DIGEST_STRING_LENGTH];
362 struct got_object *obj;
363 int idx;
364 const char *typestr;
366 idx = got_packidx_get_object_idx(packidx, id);
367 if (idx == -1) {
368 got_object_id_hex(id, hex, sizeof(hex));
369 return got_error_fmt(GOT_ERR_BAD_PACKFILE,
370 "object %s is missing from pack file", hex);
373 err = got_object_open_from_packfile(&obj, id, pack, packidx,
374 idx, repo_write.repo);
375 if (err)
376 return err;
378 if (obj->type != expected_obj_type) {
379 got_object_id_hex(id, hex, sizeof(hex));
380 got_object_type_label(&typestr, expected_obj_type);
381 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
382 "%s is not pointing at a %s object", hex, typestr);
384 got_object_close(obj);
385 return err;
388 static const struct got_error *
389 protect_tag_namespace(const char *namespace, struct got_pack *pack,
390 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
392 const struct got_error *err;
394 err = validate_namespace(namespace);
395 if (err)
396 return err;
398 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
399 strlen(namespace)) != 0)
400 return NULL;
402 if (!ref_update->ref_is_new)
403 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
405 return verify_object_type(&ref_update->new_id, GOT_OBJ_TYPE_TAG,
406 pack, packidx);
409 static const struct got_error *
410 protect_require_yca(struct got_object_id *tip_id,
411 size_t max_commits_to_traverse, struct got_pack *pack,
412 struct got_packidx *packidx, struct got_reference *ref)
414 const struct got_error *err;
415 uint8_t *buf = NULL;
416 size_t len;
417 struct got_object_id *expected_yca_id = NULL;
418 struct got_object *obj = NULL;
419 struct got_commit_object *commit = NULL;
420 char hex[SHA1_DIGEST_STRING_LENGTH];
421 const struct got_object_id_queue *parent_ids;
422 struct got_object_id_queue ids;
423 struct got_object_qid *pid, *qid;
424 struct got_object_idset *traversed_set = NULL;
425 int found_yca = 0, obj_type;
427 STAILQ_INIT(&ids);
429 err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
430 if (err)
431 return err;
433 err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
434 if (err)
435 goto done;
437 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
438 got_object_id_hex(expected_yca_id, hex, sizeof(hex));
439 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
440 "%s is not pointing at a commit object", hex);
441 goto done;
444 traversed_set = got_object_idset_alloc();
445 if (traversed_set == NULL) {
446 err = got_error_from_errno("got_object_idset_alloc");
447 goto done;
450 err = got_object_qid_alloc(&qid, tip_id);
451 if (err)
452 goto done;
453 STAILQ_INSERT_TAIL(&ids, qid, entry);
454 while (!STAILQ_EMPTY(&ids)) {
455 err = check_cancelled(NULL);
456 if (err)
457 break;
459 qid = STAILQ_FIRST(&ids);
460 if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
461 found_yca = 1;
462 break;
465 if (got_object_idset_num_elements(traversed_set) >=
466 max_commits_to_traverse)
467 break;
469 if (got_object_idset_contains(traversed_set, &qid->id)) {
470 STAILQ_REMOVE_HEAD(&ids, entry);
471 got_object_qid_free(qid);
472 qid = NULL;
473 continue;
475 err = got_object_idset_add(traversed_set, &qid->id, NULL);
476 if (err)
477 goto done;
479 err = got_object_open(&obj, repo_write.repo, &qid->id);
480 if (err && err->code != GOT_ERR_NO_OBJ)
481 goto done;
482 err = NULL;
483 if (obj) {
484 err = got_object_commit_open(&commit, repo_write.repo,
485 obj);
486 if (err)
487 goto done;
488 } else {
489 int idx;
491 idx = got_packidx_get_object_idx(packidx, &qid->id);
492 if (idx == -1) {
493 got_object_id_hex(&qid->id, hex, sizeof(hex));
494 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
495 "object %s is missing from pack file", hex);
496 goto done;
499 err = got_object_open_from_packfile(&obj, &qid->id,
500 pack, packidx, idx, repo_write.repo);
501 if (err)
502 goto done;
504 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
505 got_object_id_hex(&qid->id, hex, sizeof(hex));
506 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
507 "%s is not pointing at a commit object",
508 hex);
509 goto done;
512 err = got_packfile_extract_object_to_mem(&buf, &len,
513 obj, pack);
514 if (err)
515 goto done;
517 err = got_object_parse_commit(&commit, buf, len,
518 GOT_HASH_SHA1);
519 if (err)
520 goto done;
522 free(buf);
523 buf = NULL;
526 got_object_close(obj);
527 obj = NULL;
529 STAILQ_REMOVE_HEAD(&ids, entry);
530 got_object_qid_free(qid);
531 qid = NULL;
533 if (got_object_commit_get_nparents(commit) == 0)
534 break;
536 parent_ids = got_object_commit_get_parent_ids(commit);
537 STAILQ_FOREACH(pid, parent_ids, entry) {
538 err = check_cancelled(NULL);
539 if (err)
540 goto done;
541 err = got_object_qid_alloc(&qid, &pid->id);
542 if (err)
543 goto done;
544 STAILQ_INSERT_TAIL(&ids, qid, entry);
545 qid = NULL;
547 got_object_commit_close(commit);
548 commit = NULL;
551 if (!found_yca) {
552 err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
553 got_ref_get_name(ref));
555 done:
556 got_object_idset_free(traversed_set);
557 got_object_id_queue_free(&ids);
558 free(buf);
559 if (obj)
560 got_object_close(obj);
561 if (commit)
562 got_object_commit_close(commit);
563 free(expected_yca_id);
564 return err;
567 static const struct got_error *
568 protect_branch_namespace(const char *namespace, struct got_pack *pack,
569 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
571 const struct got_error *err;
573 err = validate_namespace(namespace);
574 if (err)
575 return err;
577 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
578 strlen(namespace)) != 0)
579 return NULL;
581 if (ref_update->ref_is_new) {
582 return verify_object_type(&ref_update->new_id,
583 GOT_OBJ_TYPE_COMMIT, pack, packidx);
586 return protect_require_yca(&ref_update->new_id,
587 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
588 ref_update->ref);
591 static const struct got_error *
592 protect_branch(const char *refname, struct got_pack *pack,
593 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
595 if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
596 return NULL;
598 /* Always allow new branches to be created. */
599 if (ref_update->ref_is_new) {
600 return verify_object_type(&ref_update->new_id,
601 GOT_OBJ_TYPE_COMMIT, pack, packidx);
604 return protect_require_yca(&ref_update->new_id,
605 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
606 ref_update->ref);
609 static const struct got_error *
610 recv_ref_update(struct imsg *imsg)
612 static const char zero_id[SHA1_DIGEST_LENGTH];
613 const struct got_error *err = NULL;
614 struct repo_write_client *client = &repo_write_client;
615 struct gotd_imsg_ref_update iref;
616 size_t datalen;
617 char *refname = NULL;
618 struct got_reference *ref = NULL;
619 struct got_object_id *id = NULL;
620 struct imsgbuf ibuf;
621 struct gotd_ref_update *ref_update = NULL;
623 log_debug("ref-update received");
625 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
626 if (datalen < sizeof(iref))
627 return got_error(GOT_ERR_PRIVSEP_LEN);
628 memcpy(&iref, imsg->data, sizeof(iref));
629 if (datalen != sizeof(iref) + iref.name_len)
630 return got_error(GOT_ERR_PRIVSEP_LEN);
632 if (imsgbuf_init(&ibuf, client->fd))
633 return got_error_from_errno("imsgbuf_init");
634 imsgbuf_allow_fdpass(&ibuf);
636 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
637 if (refname == NULL) {
638 err = got_error_from_errno("strndup");
639 goto done;
642 ref_update = calloc(1, sizeof(*ref_update));
643 if (ref_update == NULL) {
644 err = got_error_from_errno("malloc");
645 goto done;
648 memcpy(ref_update->old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
649 memcpy(ref_update->new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
651 err = got_ref_open(&ref, repo_write.repo, refname, 0);
652 if (err) {
653 if (err->code != GOT_ERR_NOT_REF)
654 goto done;
655 if (memcmp(ref_update->new_id.hash,
656 zero_id, sizeof(zero_id)) == 0) {
657 err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
658 "%s", refname);
659 goto done;
661 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
662 if (err)
663 goto done;
664 ref_update->ref_is_new = 1;
665 client->nref_new++;
667 if (got_ref_is_symbolic(ref)) {
668 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
669 "'%s' is a symbolic reference and cannot "
670 "be updated", got_ref_get_name(ref));
671 goto done;
673 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
674 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
675 "%s: does not begin with 'refs/'",
676 got_ref_get_name(ref));
677 goto done;
680 err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
681 if (err)
682 goto done;
683 err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
684 if (err)
685 goto done;
687 if (!ref_update->ref_is_new) {
689 * Ensure the client's idea of this update is still valid.
690 * At this point we can only return an error, to prevent
691 * the client from uploading a pack file which will likely
692 * have to be discarded.
694 err = got_ref_resolve(&id, repo_write.repo, ref);
695 if (err)
696 goto done;
698 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
699 err = got_error_fmt(GOT_ERR_REF_BUSY,
700 "%s has been modified by someone else "
701 "while transaction was in progress",
702 got_ref_get_name(ref));
703 goto done;
707 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
708 repo_write.pid);
710 ref_update->ref = ref;
711 if (memcmp(ref_update->new_id.hash, zero_id, sizeof(zero_id)) == 0) {
712 ref_update->delete_ref = 1;
713 client->nref_del++;
715 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
716 client->nref_updates++;
717 ref = NULL;
718 ref_update = NULL;
719 done:
720 if (ref)
721 got_ref_close(ref);
722 free(ref_update);
723 free(refname);
724 free(id);
725 return err;
728 static const struct got_error *
729 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
730 uint32_t nobj_loose, uint32_t nobj_resolved)
732 int p_indexed = 0, p_resolved = 0;
733 int nobj_delta = nobj_total - nobj_loose;
735 if (nobj_total > 0)
736 p_indexed = (nobj_indexed * 100) / nobj_total;
738 if (nobj_delta > 0)
739 p_resolved = (nobj_resolved * 100) / nobj_delta;
741 if (p_resolved > 0) {
742 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
743 nobj_total, p_indexed, nobj_delta, p_resolved);
744 } else
745 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
747 return NULL;
750 static const struct got_error *
751 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
753 const struct got_error *err = NULL;
754 uint8_t readahead[65536];
755 size_t have, newlen;
757 err = got_poll_read_full(infd, &have,
758 readahead, sizeof(readahead), minsize);
759 if (err)
760 return err;
762 err = buf_append(&newlen, buf, readahead, have);
763 if (err)
764 return err;
765 return NULL;
768 static const struct got_error *
769 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
770 off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
772 const struct got_error *err = NULL;
773 uint8_t t = 0;
774 uint64_t s = 0;
775 uint8_t sizebuf[8];
776 size_t i = 0;
777 off_t obj_offset = *outsize;
779 do {
780 /* We do not support size values which don't fit in 64 bit. */
781 if (i > 9)
782 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
783 "packfile offset %lld", (long long)obj_offset);
785 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
786 err = read_more_pack_stream(infd, buf,
787 sizeof(sizebuf[0]));
788 if (err)
789 return err;
792 sizebuf[i] = buf_getc(buf, *buf_pos);
793 *buf_pos += sizeof(sizebuf[i]);
795 if (i == 0) {
796 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
797 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
798 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
799 } else {
800 size_t shift = 4 + 7 * (i - 1);
801 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
802 shift);
804 i++;
805 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
807 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
808 if (err)
809 return err;
810 *outsize += i;
812 *type = t;
813 *size = s;
814 return NULL;
817 static const struct got_error *
818 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
819 struct got_hash *ctx)
821 const struct got_error *err = NULL;
822 size_t remain = buf_len(buf) - *buf_pos;
824 if (remain < SHA1_DIGEST_LENGTH) {
825 err = read_more_pack_stream(infd, buf,
826 SHA1_DIGEST_LENGTH - remain);
827 if (err)
828 return err;
831 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
832 SHA1_DIGEST_LENGTH, ctx);
833 if (err)
834 return err;
836 *buf_pos += SHA1_DIGEST_LENGTH;
837 return NULL;
840 static const struct got_error *
841 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
842 struct got_hash *ctx)
844 const struct got_error *err = NULL;
845 uint64_t o = 0;
846 uint8_t offbuf[8];
847 size_t i = 0;
848 off_t obj_offset = *outsize;
850 do {
851 /* We do not support offset values which don't fit in 64 bit. */
852 if (i > 8)
853 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
854 "packfile offset %lld", (long long)obj_offset);
856 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
857 err = read_more_pack_stream(infd, buf,
858 sizeof(offbuf[0]));
859 if (err)
860 return err;
863 offbuf[i] = buf_getc(buf, *buf_pos);
864 *buf_pos += sizeof(offbuf[i]);
866 if (i == 0)
867 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
868 else {
869 o++;
870 o <<= 7;
871 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
873 i++;
874 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
876 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
877 return got_error(GOT_ERR_PACK_OFFSET);
879 err = got_pack_hwrite(outfd, offbuf, i, ctx);
880 if (err)
881 return err;
883 *outsize += i;
884 return NULL;
887 static const struct got_error *
888 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
889 struct got_hash *ctx)
891 const struct got_error *err = NULL;
892 z_stream z;
893 int zret;
894 char voidbuf[1024];
895 size_t consumed_total = 0;
896 off_t zstream_offset = *outsize;
898 memset(&z, 0, sizeof(z));
900 z.zalloc = Z_NULL;
901 z.zfree = Z_NULL;
902 zret = inflateInit(&z);
903 if (zret != Z_OK) {
904 if (zret == Z_ERRNO)
905 return got_error_from_errno("inflateInit");
906 if (zret == Z_MEM_ERROR) {
907 errno = ENOMEM;
908 return got_error_from_errno("inflateInit");
910 return got_error_msg(GOT_ERR_DECOMPRESSION,
911 "inflateInit failed");
914 while (zret != Z_STREAM_END) {
915 size_t last_total_in, consumed;
918 * Decompress into the void. Object data will be parsed
919 * later, when the pack file is indexed. For now, we just
920 * want to locate the end of the compressed stream.
922 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
923 last_total_in = z.total_in;
924 z.next_in = buf_get(buf) + *buf_pos;
925 z.avail_in = buf_len(buf) - *buf_pos;
926 z.next_out = voidbuf;
927 z.avail_out = sizeof(voidbuf);
929 zret = inflate(&z, Z_SYNC_FLUSH);
930 if (zret != Z_OK && zret != Z_BUF_ERROR &&
931 zret != Z_STREAM_END) {
932 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
933 "packfile offset %lld",
934 (long long)zstream_offset);
935 goto done;
937 consumed = z.total_in - last_total_in;
939 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
940 consumed, ctx);
941 if (err)
942 goto done;
944 err = buf_discard(buf, *buf_pos + consumed);
945 if (err)
946 goto done;
947 *buf_pos = 0;
949 consumed_total += consumed;
952 if (zret != Z_STREAM_END) {
953 err = read_more_pack_stream(infd, buf, 1);
954 if (err)
955 goto done;
959 if (err == NULL)
960 *outsize += consumed_total;
961 done:
962 inflateEnd(&z);
963 return err;
966 static const struct got_error *
967 validate_object_type(int obj_type)
969 switch (obj_type) {
970 case GOT_OBJ_TYPE_BLOB:
971 case GOT_OBJ_TYPE_COMMIT:
972 case GOT_OBJ_TYPE_TREE:
973 case GOT_OBJ_TYPE_TAG:
974 case GOT_OBJ_TYPE_REF_DELTA:
975 case GOT_OBJ_TYPE_OFFSET_DELTA:
976 return NULL;
977 default:
978 break;
981 return got_error(GOT_ERR_OBJ_TYPE);
984 static const struct got_error *
985 ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
987 const struct got_error *err = NULL;
988 struct gotd_ref_update *ref_update;
989 struct got_object *obj;
991 STAILQ_FOREACH(ref_update, ref_updates, entry) {
992 err = got_object_open(&obj, repo_write.repo,
993 &ref_update->new_id);
994 if (err)
995 return err;
996 got_object_close(obj);
999 return NULL;
1002 static const struct got_error *
1003 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
1004 int infd, int outfd)
1006 const struct got_error *err;
1007 struct repo_write_client *client = &repo_write_client;
1008 struct got_packfile_hdr hdr;
1009 size_t have;
1010 uint32_t nhave = 0;
1011 struct got_hash ctx;
1012 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1013 char hex[SHA1_DIGEST_STRING_LENGTH];
1014 BUF *buf = NULL;
1015 size_t buf_pos = 0, remain;
1016 ssize_t w;
1018 *outsize = 0;
1019 *nobj = 0;
1021 /* if only deleting references there's nothing to read */
1022 if (client->nref_updates == client->nref_del)
1023 return NULL;
1025 got_hash_init(&ctx, GOT_HASH_SHA1);
1027 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1028 if (err)
1029 return err;
1030 if (have != sizeof(hdr))
1031 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1032 *outsize += have;
1034 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1035 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1036 "bad packfile signature");
1037 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1038 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1039 "bad packfile version");
1041 *nobj = be32toh(hdr.nobjects);
1042 if (*nobj == 0) {
1044 * Clients which are creating new references only
1045 * will send us an empty pack file.
1047 if (client->nref_updates > 0 &&
1048 client->nref_updates == client->nref_new)
1049 return NULL;
1052 * Clients which only move existing refs will send us an empty
1053 * pack file. All referenced objects must exist locally.
1055 err = ensure_all_objects_exist_locally(&client->ref_updates);
1056 if (err) {
1057 if (err->code != GOT_ERR_NO_OBJ)
1058 return err;
1059 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1060 "bad packfile with zero objects");
1063 client->nref_move = client->nref_updates;
1064 return NULL;
1067 log_debug("expecting %d objects", *nobj);
1069 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1070 if (err)
1071 return err;
1073 err = buf_alloc(&buf, 65536);
1074 if (err)
1075 return err;
1077 while (nhave != *nobj) {
1078 uint8_t obj_type;
1079 uint64_t obj_size;
1081 err = copy_object_type_and_size(&obj_type, &obj_size,
1082 infd, outfd, outsize, buf, &buf_pos, &ctx);
1083 if (err)
1084 goto done;
1086 err = validate_object_type(obj_type);
1087 if (err)
1088 goto done;
1090 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1091 err = copy_ref_delta(infd, outfd, outsize,
1092 buf, &buf_pos, &ctx);
1093 if (err)
1094 goto done;
1095 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1096 err = copy_offset_delta(infd, outfd, outsize,
1097 buf, &buf_pos, &ctx);
1098 if (err)
1099 goto done;
1102 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1103 if (err)
1104 goto done;
1106 nhave++;
1109 log_debug("received %u objects", *nobj);
1111 got_hash_final(&ctx, expected_sha1);
1113 remain = buf_len(buf) - buf_pos;
1114 if (remain < SHA1_DIGEST_LENGTH) {
1115 err = read_more_pack_stream(infd, buf,
1116 SHA1_DIGEST_LENGTH - remain);
1117 if (err)
1118 return err;
1121 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1122 log_debug("expect SHA1: %s", hex);
1123 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1124 log_debug("actual SHA1: %s", hex);
1126 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1127 SHA1_DIGEST_LENGTH) != 0) {
1128 err = got_error(GOT_ERR_PACKFILE_CSUM);
1129 goto done;
1132 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1134 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1135 if (w == -1) {
1136 err = got_error_from_errno("write");
1137 goto done;
1139 if (w != SHA1_DIGEST_LENGTH) {
1140 err = got_error(GOT_ERR_IO);
1141 goto done;
1144 *outsize += SHA1_DIGEST_LENGTH;
1146 if (fsync(outfd) == -1) {
1147 err = got_error_from_errno("fsync");
1148 goto done;
1150 if (lseek(outfd, 0L, SEEK_SET) == -1) {
1151 err = got_error_from_errno("lseek");
1152 goto done;
1154 done:
1155 buf_free(buf);
1156 return err;
1159 static const struct got_error *
1160 report_pack_status(const struct got_error *unpack_err)
1162 const struct got_error *err = NULL;
1163 struct repo_write_client *client = &repo_write_client;
1164 struct gotd_imsg_packfile_status istatus;
1165 struct ibuf *wbuf;
1166 struct imsgbuf ibuf;
1167 const char *unpack_ok = "unpack ok\n";
1168 size_t len;
1170 if (imsgbuf_init(&ibuf, client->fd))
1171 return got_error_from_errno("imsgbuf_init");
1172 imsgbuf_allow_fdpass(&ibuf);
1174 if (unpack_err)
1175 istatus.reason_len = strlen(unpack_err->msg);
1176 else
1177 istatus.reason_len = strlen(unpack_ok);
1179 len = sizeof(istatus) + istatus.reason_len;
1180 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1181 repo_write.pid, len);
1182 if (wbuf == NULL) {
1183 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1184 goto done;
1187 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1188 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1189 goto done;
1192 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1193 istatus.reason_len) == -1) {
1194 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1195 goto done;
1198 imsg_close(&ibuf, wbuf);
1200 err = gotd_imsg_flush(&ibuf);
1201 done:
1202 imsgbuf_clear(&ibuf);
1203 return err;
1206 static const struct got_error *
1207 recv_packfile(int *have_packfile, struct imsg *imsg)
1209 const struct got_error *err = NULL, *unpack_err;
1210 struct repo_write_client *client = &repo_write_client;
1211 struct gotd_imsg_recv_packfile ireq;
1212 struct got_object_id id;
1213 FILE *tempfiles[3] = { NULL, NULL, NULL };
1214 struct repo_tempfile {
1215 int fd;
1216 int idx;
1217 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1218 int i;
1219 size_t datalen;
1220 struct got_ratelimit rl;
1221 struct got_pack *pack = NULL;
1222 off_t pack_filesize = 0;
1223 uint32_t nobj = 0;
1225 log_debug("packfile request received");
1227 *have_packfile = 0;
1228 got_ratelimit_init(&rl, 2, 0);
1230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1231 if (datalen != sizeof(ireq))
1232 return got_error(GOT_ERR_PRIVSEP_LEN);
1233 memcpy(&ireq, imsg->data, sizeof(ireq));
1235 if (client->pack_pipe == -1 || client->packidx_fd == -1)
1236 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1238 pack = &client->pack;
1239 memset(pack, 0, sizeof(*pack));
1240 pack->fd = imsg_get_fd(imsg);
1241 if (pack->fd == -1) {
1242 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1243 goto done;
1246 err = got_delta_cache_alloc(&pack->delta_cache);
1247 if (err)
1248 goto done;
1250 for (i = 0; i < nitems(repo_tempfiles); i++) {
1251 struct repo_tempfile *t = &repo_tempfiles[i];
1252 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
1253 if (err)
1254 goto done;
1257 for (i = 0; i < nitems(tempfiles); i++) {
1258 int fd;
1259 FILE *f;
1261 fd = dup(repo_tempfiles[i].fd);
1262 if (fd == -1) {
1263 err = got_error_from_errno("dup");
1264 goto done;
1266 f = fdopen(fd, "w+");
1267 if (f == NULL) {
1268 err = got_error_from_errno("fdopen");
1269 close(fd);
1270 goto done;
1272 tempfiles[i] = f;
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);
1280 if (err) {
1281 /* Git clients hang up after sending the pack file. */
1282 if (err->code == GOT_ERR_EOF)
1283 err = NULL;
1286 if (unpack_err)
1287 err = unpack_err;
1288 if (err)
1289 goto done;
1291 log_debug("pack data received");
1294 * Clients which are creating new references only will
1295 * send us an empty pack file.
1297 if (nobj == 0 &&
1298 pack_filesize == sizeof(struct got_packfile_hdr) &&
1299 client->nref_updates > 0 &&
1300 client->nref_updates == client->nref_new)
1301 goto done;
1304 * Clients which are deleting references only will send
1305 * no pack file.
1307 if (nobj == 0 &&
1308 client->nref_del > 0 &&
1309 client->nref_updates == client->nref_del)
1310 goto done;
1313 * Clients which only move existing refs will send us an empty
1314 * pack file. All referenced objects must exist locally.
1316 if (nobj == 0 &&
1317 pack_filesize == sizeof(struct got_packfile_hdr) &&
1318 client->nref_move > 0 &&
1319 client->nref_updates == client->nref_move)
1320 goto done;
1322 pack->filesize = pack_filesize;
1323 *have_packfile = 1;
1325 memset(&id, 0, sizeof(id));
1326 memcpy(&id.hash, client->pack_sha1, SHA1_DIGEST_LENGTH);
1327 id.algo = GOT_HASH_SHA1;
1329 log_debug("begin indexing pack (%lld bytes in size)",
1330 (long long)pack->filesize);
1331 err = got_pack_index(pack, client->packidx_fd,
1332 tempfiles[0], tempfiles[1], tempfiles[2], &id,
1333 pack_index_progress, NULL, &rl);
1334 if (err)
1335 goto done;
1336 log_debug("done indexing pack");
1338 if (fsync(client->packidx_fd) == -1) {
1339 err = got_error_from_errno("fsync");
1340 goto done;
1342 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1343 err = got_error_from_errno("lseek");
1344 done:
1345 if (close(client->pack_pipe) == -1 && err == NULL)
1346 err = got_error_from_errno("close");
1347 client->pack_pipe = -1;
1348 for (i = 0; i < nitems(repo_tempfiles); i++) {
1349 struct repo_tempfile *t = &repo_tempfiles[i];
1350 if (t->idx != -1)
1351 got_repo_temp_fds_put(t->idx, repo_write.repo);
1353 for (i = 0; i < nitems(tempfiles); i++) {
1354 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1355 err = got_error_from_errno("fclose");
1357 if (err)
1358 got_pack_close(pack);
1359 return err;
1362 static const struct got_error *
1363 verify_packfile(void)
1365 const struct got_error *err = NULL, *close_err;
1366 struct repo_write_client *client = &repo_write_client;
1367 struct gotd_ref_update *ref_update;
1368 struct got_packidx *packidx = NULL;
1369 struct stat sb;
1370 char *id_str = NULL;
1371 struct got_object *obj = NULL;
1372 struct got_pathlist_entry *pe;
1373 char hex[SHA1_DIGEST_STRING_LENGTH];
1375 if (STAILQ_EMPTY(&client->ref_updates)) {
1376 return got_error_msg(GOT_ERR_BAD_REQUEST,
1377 "cannot verify pack file without any ref-updates");
1380 if (client->pack.fd == -1) {
1381 return got_error_msg(GOT_ERR_BAD_REQUEST,
1382 "invalid pack file handle during pack verification");
1384 if (client->packidx_fd == -1) {
1385 return got_error_msg(GOT_ERR_BAD_REQUEST,
1386 "invalid pack index handle during pack verification");
1389 if (fstat(client->packidx_fd, &sb) == -1)
1390 return got_error_from_errno("pack index fstat");
1392 packidx = malloc(sizeof(*packidx));
1393 memset(packidx, 0, sizeof(*packidx));
1394 packidx->fd = client->packidx_fd;
1395 client->packidx_fd = -1;
1396 packidx->len = sb.st_size;
1398 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1399 if (err)
1400 return err;
1402 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1403 if (ref_update->delete_ref)
1404 continue;
1406 RB_FOREACH(pe, got_pathlist_head, repo_write.protected_tag_namespaces) {
1407 err = protect_tag_namespace(pe->path, &client->pack,
1408 packidx, ref_update);
1409 if (err)
1410 goto done;
1414 * Objects which already exist in our repository need
1415 * not be present in the pack file.
1417 err = got_object_open(&obj, repo_write.repo,
1418 &ref_update->new_id);
1419 if (err && err->code != GOT_ERR_NO_OBJ)
1420 goto done;
1421 err = NULL;
1422 if (obj) {
1423 got_object_close(obj);
1424 obj = NULL;
1425 } else {
1426 int idx = got_packidx_get_object_idx(packidx,
1427 &ref_update->new_id);
1428 if (idx == -1) {
1429 got_object_id_hex(&ref_update->new_id,
1430 hex, sizeof(hex));
1431 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1432 "object %s is missing from pack file",
1433 hex);
1434 goto done;
1438 RB_FOREACH(pe, got_pathlist_head, repo_write.protected_branch_namespaces)
1440 err = protect_branch_namespace(pe->path,
1441 &client->pack, packidx, ref_update);
1442 if (err)
1443 goto done;
1445 RB_FOREACH(pe, got_pathlist_head, repo_write.protected_branches)
1447 err = protect_branch(pe->path, &client->pack,
1448 packidx, ref_update);
1449 if (err)
1450 goto done;
1454 done:
1455 close_err = got_packidx_close(packidx);
1456 if (close_err && err == NULL)
1457 err = close_err;
1458 free(id_str);
1459 if (obj)
1460 got_object_close(obj);
1461 return err;
1464 static const struct got_error *
1465 protect_refs_from_deletion(void)
1467 const struct got_error *err = NULL;
1468 struct repo_write_client *client = &repo_write_client;
1469 struct gotd_ref_update *ref_update;
1470 struct got_pathlist_entry *pe;
1471 const char *refname;
1473 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1474 if (!ref_update->delete_ref)
1475 continue;
1477 refname = got_ref_get_name(ref_update->ref);
1479 RB_FOREACH(pe, got_pathlist_head,
1480 repo_write.protected_tag_namespaces) {
1481 err = protect_ref_namespace(refname, pe->path);
1482 if (err)
1483 return err;
1486 RB_FOREACH(pe, got_pathlist_head,
1487 repo_write.protected_branch_namespaces) {
1488 err = protect_ref_namespace(refname, pe->path);
1489 if (err)
1490 return err;
1493 RB_FOREACH(pe, got_pathlist_head, repo_write.protected_branches)
1495 if (strcmp(refname, pe->path) == 0) {
1496 return got_error_fmt(GOT_ERR_REF_PROTECTED,
1497 "%s", refname);
1502 return NULL;
1505 static const struct got_error *
1506 install_packfile(struct gotd_imsgev *iev)
1508 struct repo_write_client *client = &repo_write_client;
1509 struct gotd_imsg_packfile_install inst;
1510 int ret;
1512 memset(&inst, 0, sizeof(inst));
1513 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1515 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1516 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1517 if (ret == -1)
1518 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1520 return NULL;
1523 static const struct got_error *
1524 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1526 struct gotd_imsg_ref_updates_start istart;
1527 int ret;
1529 memset(&istart, 0, sizeof(istart));
1530 istart.nref_updates = nref_updates;
1532 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1533 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1534 if (ret == -1)
1535 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1537 return NULL;
1541 static const struct got_error *
1542 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1544 struct gotd_imsg_ref_update iref;
1545 const char *refname = got_ref_get_name(ref_update->ref);
1546 struct ibuf *wbuf;
1547 size_t len;
1549 memset(&iref, 0, sizeof(iref));
1550 memcpy(iref.old_id, ref_update->old_id.hash, SHA1_DIGEST_LENGTH);
1551 memcpy(iref.new_id, ref_update->new_id.hash, SHA1_DIGEST_LENGTH);
1552 iref.ref_is_new = ref_update->ref_is_new;
1553 iref.delete_ref = ref_update->delete_ref;
1554 iref.name_len = strlen(refname);
1556 len = sizeof(iref) + iref.name_len;
1557 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1558 repo_write.pid, len);
1559 if (wbuf == NULL)
1560 return got_error_from_errno("imsg_create REF_UPDATE");
1562 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1563 return got_error_from_errno("imsg_add REF_UPDATE");
1564 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1565 return got_error_from_errno("imsg_add REF_UPDATE");
1567 imsg_close(&iev->ibuf, wbuf);
1569 gotd_imsg_event_add(iev);
1570 return NULL;
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);
1581 if (err)
1582 return err;
1584 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1585 err = send_ref_update(ref_update, iev);
1586 if (err)
1587 goto done;
1589 done:
1590 return err;
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 size_t datalen;
1599 log_debug("receiving pack pipe descriptor");
1601 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1602 if (datalen != 0)
1603 return got_error(GOT_ERR_PRIVSEP_LEN);
1605 if (client->pack_pipe != -1)
1606 return got_error(GOT_ERR_PRIVSEP_MSG);
1608 client->pack_pipe = imsg_get_fd(imsg);
1609 if (client->pack_pipe == -1)
1610 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1612 return NULL;
1615 static const struct got_error *
1616 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1618 struct repo_write_client *client = &repo_write_client;
1619 size_t datalen;
1621 log_debug("receiving pack index output file");
1623 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1624 if (datalen != 0)
1625 return got_error(GOT_ERR_PRIVSEP_LEN);
1627 if (client->packidx_fd != -1)
1628 return got_error(GOT_ERR_PRIVSEP_MSG);
1630 client->packidx_fd = imsg_get_fd(imsg);
1631 if (client->packidx_fd == -1)
1632 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1634 return NULL;
1637 static const struct got_error *
1638 notify_removed_ref(const char *refname, struct got_object_id *id,
1639 struct gotd_imsgev *iev, int fd)
1641 const struct got_error *err;
1642 char *id_str;
1644 err = got_object_id_str(&id_str, id);
1645 if (err)
1646 return err;
1648 dprintf(fd, "Removed %s: %s\n", refname, id_str);
1649 free(id_str);
1650 return err;
1653 static const char *
1654 format_author(char *author)
1656 char *smallerthan;
1658 smallerthan = strchr(author, '<');
1659 if (smallerthan && smallerthan[1] != '\0')
1660 author = smallerthan + 1;
1661 author[strcspn(author, "@>")] = '\0';
1663 return author;
1666 static const struct got_error *
1667 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
1668 struct got_repository *repo, int fd)
1670 const struct got_error *err = NULL;
1671 char *id_str = NULL, *logmsg0 = NULL;
1672 char *s, *nl;
1673 char *committer = NULL, *author = NULL;
1674 time_t committer_time;
1676 err = got_object_id_str(&id_str, id);
1677 if (err)
1678 return err;
1680 committer_time = got_object_commit_get_committer_time(commit);
1682 err = got_object_commit_get_logmsg(&logmsg0, commit);
1683 if (err)
1684 goto done;
1686 s = logmsg0;
1687 while (isspace((unsigned char)s[0]))
1688 s++;
1690 nl = strchr(s, '\n');
1691 if (nl) {
1692 *nl = '\0';
1695 if (strcmp(got_object_commit_get_author(commit),
1696 got_object_commit_get_committer(commit)) != 0) {
1697 author = strdup(got_object_commit_get_author(commit));
1698 if (author == NULL) {
1699 err = got_error_from_errno("strdup");
1700 goto done;
1702 dprintf(fd, "%lld %.7s %.8s %s\n", (long long)committer_time,
1703 id_str, format_author(author), s);
1704 } else {
1705 committer = strdup(got_object_commit_get_committer(commit));
1706 dprintf(fd, "%lld %.7s %.8s %s\n", (long long)committer_time,
1707 id_str, format_author(committer), s);
1710 if (fsync(fd) == -1 && err == NULL)
1711 err = got_error_from_errno("fsync");
1712 done:
1713 free(id_str);
1714 free(logmsg0);
1715 free(committer);
1716 free(author);
1717 return err;
1720 static const struct got_error *
1721 print_diffstat(struct got_diffstat_cb_arg *dsa, int fd)
1723 struct got_pathlist_entry *pe;
1725 RB_FOREACH(pe, got_pathlist_head, dsa->paths) {
1726 struct got_diff_changed_path *cp = pe->data;
1727 int pad = dsa->max_path_len - pe->path_len + 1;
1729 dprintf(fd, " %c %s%*c | %*d+ %*d-\n", cp->status,
1730 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
1731 dsa->rm_cols + 1, cp->rm);
1733 dprintf(fd,
1734 "\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
1735 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
1736 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
1738 return NULL;
1741 static const struct got_error *
1742 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1743 struct got_repository *repo, struct got_pathlist_head *changed_paths,
1744 struct got_diffstat_cb_arg *diffstat, int fd)
1746 const struct got_error *err = NULL;
1747 char *id_str, *logmsg0, *logmsg, *line;
1748 time_t committer_time;
1749 const char *author, *committer;
1751 err = got_object_id_str(&id_str, id);
1752 if (err)
1753 return err;
1755 dprintf(fd, "commit %s\n", id_str);
1756 free(id_str);
1757 id_str = NULL;
1758 dprintf(fd, "from: %s\n", got_object_commit_get_author(commit));
1759 author = got_object_commit_get_author(commit);
1760 committer = got_object_commit_get_committer(commit);
1761 if (strcmp(author, committer) != 0)
1762 dprintf(fd, "via: %s\n", committer);
1763 committer_time = got_object_commit_get_committer_time(commit);
1764 dprintf(fd, "date: %lld\n", (long long)committer_time);
1765 if (got_object_commit_get_nparents(commit) > 1) {
1766 const struct got_object_id_queue *parent_ids;
1767 struct got_object_qid *qid;
1768 int n = 1;
1769 parent_ids = got_object_commit_get_parent_ids(commit);
1770 STAILQ_FOREACH(qid, parent_ids, entry) {
1771 err = got_object_id_str(&id_str, &qid->id);
1772 if (err)
1773 goto done;
1774 dprintf(fd, "parent %d: %s\n", n++, id_str);
1775 free(id_str);
1776 id_str = NULL;
1780 err = got_object_commit_get_logmsg(&logmsg0, commit);
1781 if (err)
1782 goto done;
1784 dprintf(fd, "messagelen: %zu\n", strlen(logmsg0));
1786 logmsg = logmsg0;
1787 do {
1788 line = strsep(&logmsg, "\n");
1789 if (line)
1790 dprintf(fd, " %s\n", line);
1791 } while (line);
1792 free(logmsg0);
1794 err = print_diffstat(diffstat, fd);
1795 if (err)
1796 goto done;
1798 if (fsync(fd) == -1 && err == NULL)
1799 err = got_error_from_errno("fsync");
1800 done:
1801 free(id_str);
1802 return err;
1805 static const struct got_error *
1806 get_changed_paths(struct got_pathlist_head *paths,
1807 struct got_commit_object *commit, struct got_repository *repo,
1808 struct got_diffstat_cb_arg *dsa)
1810 const struct got_error *err = NULL;
1811 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1812 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1813 struct got_object_qid *qid;
1814 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
1815 FILE *f1 = repo_write.diff.f1, *f2 = repo_write.diff.f2;
1816 int fd1 = repo_write.diff.fd1, fd2 = repo_write.diff.fd2;
1818 if (dsa)
1819 cb = got_diff_tree_compute_diffstat;
1821 err = got_opentemp_truncate(f1);
1822 if (err)
1823 return err;
1824 err = got_opentemp_truncate(f2);
1825 if (err)
1826 return err;
1827 err = got_opentemp_truncatefd(fd1);
1828 if (err)
1829 return err;
1830 err = got_opentemp_truncatefd(fd2);
1831 if (err)
1832 return err;
1834 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1835 if (qid != NULL) {
1836 struct got_commit_object *pcommit;
1837 err = got_object_open_as_commit(&pcommit, repo,
1838 &qid->id);
1839 if (err)
1840 return err;
1842 tree_id1 = got_object_id_dup(
1843 got_object_commit_get_tree_id(pcommit));
1844 if (tree_id1 == NULL) {
1845 got_object_commit_close(pcommit);
1846 return got_error_from_errno("got_object_id_dup");
1848 got_object_commit_close(pcommit);
1852 if (tree_id1) {
1853 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1854 if (err)
1855 goto done;
1858 tree_id2 = got_object_commit_get_tree_id(commit);
1859 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1860 if (err)
1861 goto done;
1863 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
1864 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
1865 done:
1866 if (tree1)
1867 got_object_tree_close(tree1);
1868 if (tree2)
1869 got_object_tree_close(tree2);
1870 free(tree_id1);
1871 return err;
1874 static const struct got_error *
1875 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
1876 struct got_repository *repo, int fd)
1878 const struct got_error *err;
1879 struct got_commit_graph *graph;
1880 struct got_object_id_queue reversed_commits;
1881 struct got_object_qid *qid;
1882 struct got_commit_object *commit = NULL;
1883 struct got_pathlist_head changed_paths;
1884 int ncommits = 0;
1885 const int shortlog_threshold = 50;
1887 STAILQ_INIT(&reversed_commits);
1888 RB_INIT(&changed_paths);
1890 /* XXX first-parent only for now */
1891 err = got_commit_graph_open(&graph, "/", 1);
1892 if (err)
1893 return err;
1894 err = got_commit_graph_bfsort(graph, root_id, repo,
1895 check_cancelled, NULL);
1896 if (err)
1897 goto done;
1898 for (;;) {
1899 struct got_object_id id;
1901 err = got_commit_graph_iter_next(&id, graph, repo,
1902 check_cancelled, NULL);
1903 if (err) {
1904 if (err->code == GOT_ERR_ITER_COMPLETED)
1905 err = NULL;
1906 break;
1909 err = got_object_open_as_commit(&commit, repo, &id);
1910 if (err)
1911 break;
1913 if (end_id && got_object_id_cmp(&id, end_id) == 0)
1914 break;
1916 err = got_object_qid_alloc(&qid, &id);
1917 if (err)
1918 break;
1920 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
1921 ncommits++;
1922 got_object_commit_close(commit);
1924 if (end_id == NULL)
1925 break;
1928 STAILQ_FOREACH(qid, &reversed_commits, entry) {
1929 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
1930 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
1932 err = got_object_open_as_commit(&commit, repo, &qid->id);
1933 if (err)
1934 break;
1936 if (ncommits > shortlog_threshold) {
1937 err = print_commit_oneline(commit, &qid->id,
1938 repo, fd);
1939 if (err)
1940 break;
1941 } else {
1942 err = get_changed_paths(&changed_paths, commit,
1943 repo, &dsa);
1944 if (err)
1945 break;
1946 err = print_commit(commit, &qid->id, repo,
1947 &changed_paths, &dsa, fd);
1949 got_object_commit_close(commit);
1950 commit = NULL;
1951 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1953 done:
1954 if (commit)
1955 got_object_commit_close(commit);
1956 while (!STAILQ_EMPTY(&reversed_commits)) {
1957 qid = STAILQ_FIRST(&reversed_commits);
1958 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
1959 got_object_qid_free(qid);
1961 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1962 got_commit_graph_close(graph);
1963 return err;
1966 static const struct got_error *
1967 print_tag(struct got_object_id *id,
1968 const char *refname, struct got_repository *repo, int fd)
1970 const struct got_error *err = NULL;
1971 struct got_tag_object *tag = NULL;
1972 const char *tagger = NULL;
1973 char *id_str = NULL, *tagmsg0 = NULL, *tagmsg, *line;
1974 time_t tagger_time;
1976 err = got_object_open_as_tag(&tag, repo, id);
1977 if (err)
1978 return err;
1980 tagger = got_object_tag_get_tagger(tag);
1981 tagger_time = got_object_tag_get_tagger_time(tag);
1982 err = got_object_id_str(&id_str,
1983 got_object_tag_get_object_id(tag));
1984 if (err)
1985 goto done;
1987 dprintf(fd, "tag %s\n", refname);
1988 dprintf(fd, "from: %s\n", tagger);
1989 dprintf(fd, "date: %lld\n", (long long)tagger_time);
1991 switch (got_object_tag_get_object_type(tag)) {
1992 case GOT_OBJ_TYPE_BLOB:
1993 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
1994 break;
1995 case GOT_OBJ_TYPE_TREE:
1996 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
1997 break;
1998 case GOT_OBJ_TYPE_COMMIT:
1999 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
2000 break;
2001 case GOT_OBJ_TYPE_TAG:
2002 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
2003 break;
2004 default:
2005 break;
2008 tagmsg0 = strdup(got_object_tag_get_message(tag));
2009 if (tagmsg0 == NULL) {
2010 err = got_error_from_errno("strdup");
2011 goto done;
2014 dprintf(fd, "messagelen: %zu\n", strlen(tagmsg0));
2016 tagmsg = tagmsg0;
2017 do {
2018 line = strsep(&tagmsg, "\n");
2019 if (line)
2020 dprintf(fd, " %s\n", line);
2021 } while (line);
2022 free(tagmsg0);
2023 done:
2024 if (tag)
2025 got_object_tag_close(tag);
2026 free(id_str);
2027 return err;
2030 static const struct got_error *
2031 notify_changed_ref(const char *refname, struct got_object_id *old_id,
2032 struct got_object_id *new_id, struct gotd_imsgev *iev, int fd)
2034 const struct got_error *err;
2035 int old_obj_type, new_obj_type;
2036 const char *label;
2037 char *new_id_str = NULL;
2039 err = got_object_get_type(&old_obj_type, repo_write.repo, old_id);
2040 if (err)
2041 return err;
2043 err = got_object_get_type(&new_obj_type, repo_write.repo, new_id);
2044 if (err)
2045 return err;
2047 switch (new_obj_type) {
2048 case GOT_OBJ_TYPE_COMMIT:
2049 err = print_commits(new_id,
2050 old_obj_type == GOT_OBJ_TYPE_COMMIT ? old_id : NULL,
2051 repo_write.repo, fd);
2052 break;
2053 case GOT_OBJ_TYPE_TAG:
2054 err = print_tag(new_id, refname, repo_write.repo, fd);
2055 break;
2056 default:
2057 err = got_object_type_label(&label, new_obj_type);
2058 if (err)
2059 goto done;
2060 err = got_object_id_str(&new_id_str, new_id);
2061 if (err)
2062 goto done;
2063 dprintf(fd, "%s: %s object %s\n", refname, label, new_id_str);
2064 break;
2066 done:
2067 free(new_id_str);
2068 return err;
2071 static const struct got_error *
2072 notify_created_ref(const char *refname, struct got_object_id *id,
2073 struct gotd_imsgev *iev, int fd)
2075 const struct got_error *err;
2076 int obj_type;
2078 err = got_object_get_type(&obj_type, repo_write.repo, id);
2079 if (err)
2080 return err;
2082 if (obj_type == GOT_OBJ_TYPE_TAG)
2083 return print_tag(id, refname, repo_write.repo, fd);
2085 return print_commits(id, NULL, repo_write.repo, fd);
2088 static const struct got_error *
2089 render_notification(struct imsg *imsg, struct gotd_imsgev *iev)
2091 const struct got_error *err = NULL;
2092 struct gotd_imsg_notification_content ireq;
2093 size_t datalen, len;
2094 char *refname = NULL;
2095 struct ibuf *wbuf;
2096 int fd = -1;
2098 fd = imsg_get_fd(imsg);
2099 if (fd == -1)
2100 return got_error(GOT_ERR_PRIVSEP_NO_FD);
2102 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2103 if (datalen < sizeof(ireq)) {
2104 err = got_error(GOT_ERR_PRIVSEP_LEN);
2105 goto done;
2108 memcpy(&ireq, imsg->data, sizeof(ireq));
2110 if (datalen != sizeof(ireq) + ireq.refname_len) {
2111 err = got_error(GOT_ERR_PRIVSEP_LEN);
2112 goto done;
2115 refname = strndup(imsg->data + sizeof(ireq), ireq.refname_len);
2116 if (refname == NULL) {
2117 err = got_error_from_errno("strndup");
2118 goto done;
2121 switch (ireq.action) {
2122 case GOTD_NOTIF_ACTION_CREATED:
2123 err = notify_created_ref(refname, &ireq.new_id, iev, fd);
2124 break;
2125 case GOTD_NOTIF_ACTION_REMOVED:
2126 err = notify_removed_ref(refname, &ireq.old_id, iev, fd);
2127 break;
2128 case GOTD_NOTIF_ACTION_CHANGED:
2129 err = notify_changed_ref(refname, &ireq.old_id, &ireq.new_id,
2130 iev, fd);
2131 break;
2133 if (err != NULL)
2134 goto done;
2136 if (fsync(fd) == -1) {
2137 err = got_error_from_errno("fsync");
2138 goto done;
2141 len = sizeof(ireq) + ireq.refname_len;
2142 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY, PROC_REPO_WRITE,
2143 repo_write.pid, len);
2144 if (wbuf == NULL) {
2145 err = got_error_from_errno("imsg_create REF");
2146 goto done;
2148 if (imsg_add(wbuf, &ireq, sizeof(ireq)) == -1) {
2149 err = got_error_from_errno("imsg_add NOTIFY");
2150 goto done;
2152 if (imsg_add(wbuf, refname, ireq.refname_len) == -1) {
2153 err = got_error_from_errno("imsg_add NOTIFY");
2154 goto done;
2157 imsg_close(&iev->ibuf, wbuf);
2158 gotd_imsg_event_add(iev);
2159 done:
2160 free(refname);
2161 if (fd != -1 && close(fd) == -1 && err == NULL)
2162 err = got_error_from_errno("close");
2163 return err;
2166 static void
2167 repo_write_dispatch_session(int fd, short event, void *arg)
2169 const struct got_error *err = NULL;
2170 struct gotd_imsgev *iev = arg;
2171 struct imsgbuf *ibuf = &iev->ibuf;
2172 struct imsg imsg;
2173 struct repo_write_client *client = &repo_write_client;
2174 ssize_t n;
2175 int shut = 0, have_packfile = 0;
2177 if (event & EV_READ) {
2178 if ((n = imsgbuf_read(ibuf)) == -1)
2179 fatal("imsgbuf_read error");
2180 if (n == 0) /* Connection closed. */
2181 shut = 1;
2184 if (event & EV_WRITE) {
2185 err = gotd_imsg_flush(ibuf);
2186 if (err)
2187 fatalx("%s", err->msg);
2190 for (;;) {
2191 if ((n = imsg_get(ibuf, &imsg)) == -1)
2192 fatal("%s: imsg_get error", __func__);
2193 if (n == 0) /* No more messages. */
2194 break;
2196 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
2197 !repo_write.refs_listed) {
2198 err = got_error(GOT_ERR_PRIVSEP_MSG);
2199 break;
2202 switch (imsg.hdr.type) {
2203 case GOTD_IMSG_LIST_REFS_INTERNAL:
2204 err = list_refs(&imsg);
2205 if (err)
2206 log_warnx("ls-refs: %s", err->msg);
2207 break;
2208 case GOTD_IMSG_REF_UPDATE:
2209 err = recv_ref_update(&imsg);
2210 if (err)
2211 log_warnx("ref-update: %s", err->msg);
2212 break;
2213 case GOTD_IMSG_PACKFILE_PIPE:
2214 err = receive_pack_pipe(&imsg, iev);
2215 if (err) {
2216 log_warnx("receiving pack pipe: %s", err->msg);
2217 break;
2219 break;
2220 case GOTD_IMSG_PACKIDX_FILE:
2221 err = receive_pack_idx(&imsg, iev);
2222 if (err) {
2223 log_warnx("receiving pack index: %s",
2224 err->msg);
2225 break;
2227 break;
2228 case GOTD_IMSG_RECV_PACKFILE:
2229 err = protect_refs_from_deletion();
2230 if (err)
2231 break;
2232 err = recv_packfile(&have_packfile, &imsg);
2233 if (err) {
2234 log_warnx("receive packfile: %s", err->msg);
2235 break;
2237 if (have_packfile) {
2238 err = verify_packfile();
2239 if (err) {
2240 log_warnx("verify packfile: %s",
2241 err->msg);
2242 break;
2244 err = install_packfile(iev);
2245 if (err) {
2246 log_warnx("install packfile: %s",
2247 err->msg);
2248 break;
2251 * Ensure we re-read the pack index list
2252 * upon next access.
2254 repo_write.repo->pack_path_mtime.tv_sec = 0;
2255 repo_write.repo->pack_path_mtime.tv_nsec = 0;
2257 err = update_refs(iev);
2258 if (err) {
2259 log_warnx("update refs: %s", err->msg);
2261 break;
2262 case GOTD_IMSG_NOTIFY:
2263 err = render_notification(&imsg, iev);
2264 if (err) {
2265 log_warnx("render notification: %s", err->msg);
2266 shut = 1;
2268 break;
2269 default:
2270 log_debug("unexpected imsg %d", imsg.hdr.type);
2271 break;
2274 imsg_free(&imsg);
2277 if (!shut && check_cancelled(NULL) == NULL) {
2278 if (err &&
2279 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2280 client->id, err) == -1) {
2281 log_warnx("could not send error to parent: %s",
2282 err->msg);
2284 gotd_imsg_event_add(iev);
2285 } else {
2286 /* This pipe is dead. Remove its event handler */
2287 event_del(&iev->ev);
2288 event_loopexit(NULL);
2292 static const struct got_error *
2293 recv_connect(struct imsg *imsg)
2295 struct gotd_imsgev *iev = &repo_write.session_iev;
2296 size_t datalen;
2298 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2299 if (datalen != 0)
2300 return got_error(GOT_ERR_PRIVSEP_LEN);
2302 if (repo_write.session_fd != -1)
2303 return got_error(GOT_ERR_PRIVSEP_MSG);
2305 repo_write.session_fd = imsg_get_fd(imsg);
2306 if (repo_write.session_fd == -1)
2307 return got_error(GOT_ERR_PRIVSEP_NO_FD);
2309 if (imsgbuf_init(&iev->ibuf, repo_write.session_fd) == -1)
2310 return got_error_from_errno("imsgbuf_init");
2311 imsgbuf_allow_fdpass(&iev->ibuf);
2312 iev->handler = repo_write_dispatch_session;
2313 iev->events = EV_READ;
2314 iev->handler_arg = NULL;
2315 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
2316 repo_write_dispatch_session, iev);
2317 gotd_imsg_event_add(iev);
2319 return NULL;
2322 static void
2323 repo_write_dispatch(int fd, short event, void *arg)
2325 const struct got_error *err = NULL;
2326 struct gotd_imsgev *iev = arg;
2327 struct imsgbuf *ibuf = &iev->ibuf;
2328 struct imsg imsg;
2329 ssize_t n;
2330 int shut = 0;
2331 struct repo_write_client *client = &repo_write_client;
2333 if (event & EV_READ) {
2334 if ((n = imsgbuf_read(ibuf)) == -1)
2335 fatal("imsgbuf_read error");
2336 if (n == 0) { /* Connection closed. */
2337 shut = 1;
2338 goto done;
2342 if (event & EV_WRITE) {
2343 err = gotd_imsg_flush(ibuf);
2344 if (err)
2345 fatalx("%s", err->msg);
2348 while (err == NULL) {
2349 err = check_cancelled(NULL);
2350 if (err)
2351 break;
2352 if ((n = imsg_get(ibuf, &imsg)) == -1)
2353 fatal("%s: imsg_get", __func__);
2354 if (n == 0) /* No more messages. */
2355 break;
2357 switch (imsg.hdr.type) {
2358 case GOTD_IMSG_CONNECT_REPO_CHILD:
2359 err = recv_connect(&imsg);
2360 break;
2361 default:
2362 log_debug("unexpected imsg %d", imsg.hdr.type);
2363 err = got_error(GOT_ERR_PRIVSEP_MSG);
2364 break;
2367 imsg_free(&imsg);
2370 if (err && gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2371 client->id, err) == -1)
2372 log_warnx("could not send error to parent: %s", err->msg);
2373 done:
2374 if (!shut) {
2375 gotd_imsg_event_add(iev);
2376 } else {
2377 /* This pipe is dead. Remove its event handler */
2378 event_del(&iev->ev);
2379 event_loopexit(NULL);
2383 void
2384 repo_write_main(const char *title, const char *repo_path,
2385 int *pack_fds, int *temp_fds,
2386 FILE *diff_f1, FILE *diff_f2, int diff_fd1, int diff_fd2,
2387 struct got_pathlist_head *protected_tag_namespaces,
2388 struct got_pathlist_head *protected_branch_namespaces,
2389 struct got_pathlist_head *protected_branches)
2391 const struct got_error *err = NULL;
2392 struct repo_write_client *client = &repo_write_client;
2393 struct gotd_imsgev iev;
2395 client->fd = -1;
2396 client->pack_pipe = -1;
2397 client->packidx_fd = -1;
2398 client->pack.fd = -1;
2400 repo_write.title = title;
2401 repo_write.pid = getpid();
2402 repo_write.pack_fds = pack_fds;
2403 repo_write.temp_fds = temp_fds;
2404 repo_write.session_fd = -1;
2405 repo_write.session_iev.ibuf.fd = -1;
2406 repo_write.protected_tag_namespaces = protected_tag_namespaces;
2407 repo_write.protected_branch_namespaces = protected_branch_namespaces;
2408 repo_write.protected_branches = protected_branches;
2409 repo_write.diff.f1 = diff_f1;
2410 repo_write.diff.f2 = diff_f2;
2411 repo_write.diff.fd1 = diff_fd1;
2412 repo_write.diff.fd2 = diff_fd2;
2414 STAILQ_INIT(&repo_write_client.ref_updates);
2416 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
2417 if (err)
2418 goto done;
2419 if (!got_repo_is_bare(repo_write.repo)) {
2420 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
2421 "bare git repository required");
2422 goto done;
2424 if (got_repo_get_object_format(repo_write.repo) != GOT_HASH_SHA1) {
2425 err = got_error_msg(GOT_ERR_NOT_IMPL,
2426 "sha256 object IDs unsupported in network protocol");
2427 goto done;
2430 got_repo_temp_fds_set(repo_write.repo, temp_fds);
2432 signal(SIGINT, catch_sigint);
2433 signal(SIGTERM, catch_sigterm);
2434 signal(SIGPIPE, SIG_IGN);
2435 signal(SIGHUP, SIG_IGN);
2437 if (imsgbuf_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE) == -1) {
2438 err = got_error_from_errno("imsgbuf_init");
2439 goto done;
2441 imsgbuf_allow_fdpass(&iev.ibuf);
2442 iev.handler = repo_write_dispatch;
2443 iev.events = EV_READ;
2444 iev.handler_arg = NULL;
2445 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
2446 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
2447 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
2448 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
2449 goto done;
2452 event_dispatch();
2453 done:
2454 if (fclose(diff_f1) == EOF && err == NULL)
2455 err = got_error_from_errno("fclose");
2456 if (fclose(diff_f2) == EOF && err == NULL)
2457 err = got_error_from_errno("fclose");
2458 if (close(diff_fd1) == -1 && err == NULL)
2459 err = got_error_from_errno("close");
2460 if (close(diff_fd2) == -1 && err == NULL)
2461 err = got_error_from_errno("close");
2462 if (err)
2463 log_warnx("%s: %s", title, err->msg);
2464 repo_write_shutdown();
2467 void
2468 repo_write_shutdown(void)
2470 struct repo_write_client *client = &repo_write_client;
2471 struct gotd_ref_update *ref_update;
2473 log_debug("%s: shutting down", repo_write.title);
2475 while (!STAILQ_EMPTY(&client->ref_updates)) {
2476 ref_update = STAILQ_FIRST(&client->ref_updates);
2477 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
2478 got_ref_close(ref_update->ref);
2479 free(ref_update);
2482 got_pack_close(&client->pack);
2483 if (client->fd != -1)
2484 close(client->fd);
2485 if (client->pack_pipe != -1)
2486 close(client->pack_pipe);
2487 if (client->packidx_fd != -1)
2488 close(client->packidx_fd);
2490 if (repo_write.repo)
2491 got_repo_close(repo_write.repo);
2492 got_repo_pack_fds_close(repo_write.pack_fds);
2493 got_repo_temp_fds_close(repo_write.temp_fds);
2494 if (repo_write.session_fd != -1)
2495 close(repo_write.session_fd);
2496 exit(0);