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 <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/tree.h>
20 #include <sys/types.h>
22 #include <event.h>
23 #include <errno.h>
24 #include <imsg.h>
25 #include <signal.h>
26 #include <siphash.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <poll.h>
32 #include <sha1.h>
33 #include <sha2.h>
34 #include <unistd.h>
35 #include <zlib.h>
37 #include "buf.h"
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_path.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_delta_cache.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_ratelimit.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_pack_index.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_poll.h"
55 #include "got_lib_hash.h" /* XXX temp include for debugging */
57 #include "log.h"
58 #include "gotd.h"
59 #include "repo_write.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 static struct repo_write {
66 pid_t pid;
67 const char *title;
68 struct got_repository *repo;
69 int *pack_fds;
70 int *temp_fds;
71 int session_fd;
72 struct gotd_imsgev session_iev;
73 } repo_write;
75 struct gotd_ref_update {
76 STAILQ_ENTRY(gotd_ref_update) entry;
77 struct got_reference *ref;
78 int ref_is_new;
79 int delete_ref;
80 struct got_object_id old_id;
81 struct got_object_id new_id;
82 };
83 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
85 static struct repo_write_client {
86 uint32_t id;
87 int fd;
88 int pack_pipe;
89 struct got_pack pack;
90 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
91 int packidx_fd;
92 struct gotd_ref_updates ref_updates;
93 int nref_updates;
94 int nref_del;
95 int nref_new;
96 } repo_write_client;
98 static volatile sig_atomic_t sigint_received;
99 static volatile sig_atomic_t sigterm_received;
101 static void
102 catch_sigint(int signo)
104 sigint_received = 1;
107 static void
108 catch_sigterm(int signo)
110 sigterm_received = 1;
113 static const struct got_error *
114 check_cancelled(void *arg)
116 if (sigint_received || sigterm_received)
117 return got_error(GOT_ERR_CANCELLED);
119 return NULL;
122 static const struct got_error *
123 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
124 struct imsgbuf *ibuf)
126 const struct got_error *err = NULL;
127 struct got_tag_object *tag;
128 size_t namelen, len;
129 char *peeled_refname = NULL;
130 struct got_object_id *id;
131 struct ibuf *wbuf;
133 err = got_object_tag_open(&tag, repo_write.repo, obj);
134 if (err)
135 return err;
137 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
138 err = got_error_from_errno("asprintf");
139 goto done;
142 id = got_object_tag_get_object_id(tag);
143 namelen = strlen(peeled_refname);
145 len = sizeof(struct gotd_imsg_ref) + namelen;
146 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
147 err = got_error(GOT_ERR_NO_SPACE);
148 goto done;
151 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
152 repo_write.pid, len);
153 if (wbuf == NULL) {
154 err = got_error_from_errno("imsg_create REF");
155 goto done;
158 /* Keep in sync with struct gotd_imsg_ref definition. */
159 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
160 err = got_error_from_errno("imsg_add REF");
161 goto done;
163 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
164 err = got_error_from_errno("imsg_add REF");
165 goto done;
167 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
168 err = got_error_from_errno("imsg_add REF");
169 goto done;
172 wbuf->fd = -1;
173 imsg_close(ibuf, wbuf);
174 done:
175 got_object_tag_close(tag);
176 return err;
179 static const struct got_error *
180 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
182 const struct got_error *err;
183 const char *refname = got_ref_get_name(ref);
184 size_t namelen;
185 struct got_object_id *id = NULL;
186 struct got_object *obj = NULL;
187 size_t len;
188 struct ibuf *wbuf;
190 namelen = strlen(refname);
192 len = sizeof(struct gotd_imsg_ref) + namelen;
193 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
194 return got_error(GOT_ERR_NO_SPACE);
196 err = got_ref_resolve(&id, repo_write.repo, ref);
197 if (err)
198 return err;
200 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
201 repo_write.pid, len);
202 if (wbuf == NULL) {
203 err = got_error_from_errno("imsg_create REF");
204 goto done;
207 /* Keep in sync with struct gotd_imsg_ref definition. */
208 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
209 return got_error_from_errno("imsg_add REF");
210 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
211 return got_error_from_errno("imsg_add REF");
212 if (imsg_add(wbuf, refname, namelen) == -1)
213 return got_error_from_errno("imsg_add REF");
215 wbuf->fd = -1;
216 imsg_close(ibuf, wbuf);
218 err = got_object_open(&obj, repo_write.repo, id);
219 if (err)
220 goto done;
221 if (obj->type == GOT_OBJ_TYPE_TAG)
222 err = send_peeled_tag_ref(ref, obj, ibuf);
223 done:
224 if (obj)
225 got_object_close(obj);
226 free(id);
227 return err;
230 static const struct got_error *
231 list_refs(struct imsg *imsg)
233 const struct got_error *err;
234 struct repo_write_client *client = &repo_write_client;
235 struct got_reflist_head refs;
236 struct got_reflist_entry *re;
237 struct gotd_imsg_list_refs_internal ireq;
238 size_t datalen;
239 struct gotd_imsg_reflist irefs;
240 struct imsgbuf ibuf;
241 int client_fd = imsg->fd;
243 TAILQ_INIT(&refs);
245 if (client_fd == -1)
246 return got_error(GOT_ERR_PRIVSEP_NO_FD);
248 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
249 if (datalen != sizeof(ireq))
250 return got_error(GOT_ERR_PRIVSEP_LEN);
251 memcpy(&ireq, imsg->data, sizeof(ireq));
253 if (ireq.client_id == 0)
254 return got_error(GOT_ERR_CLIENT_ID);
255 if (client->id != 0) {
256 return got_error_msg(GOT_ERR_CLIENT_ID,
257 "duplicate list-refs request");
259 client->id = ireq.client_id;
260 client->fd = client_fd;
261 client->nref_updates = 0;
262 client->nref_del = 0;
263 client->nref_new = 0;
265 imsg_init(&ibuf, client_fd);
267 err = got_ref_list(&refs, repo_write.repo, "",
268 got_ref_cmp_by_name, NULL);
269 if (err)
270 return err;
272 memset(&irefs, 0, sizeof(irefs));
273 TAILQ_FOREACH(re, &refs, entry) {
274 struct got_object_id *id;
275 int obj_type;
277 if (got_ref_is_symbolic(re->ref))
278 continue;
280 irefs.nrefs++;
282 /* Account for a peeled tag refs. */
283 err = got_ref_resolve(&id, repo_write.repo, re->ref);
284 if (err)
285 goto done;
286 err = got_object_get_type(&obj_type, repo_write.repo, id);
287 free(id);
288 if (err)
289 goto done;
290 if (obj_type == GOT_OBJ_TYPE_TAG)
291 irefs.nrefs++;
294 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
295 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
296 err = got_error_from_errno("imsg_compose REFLIST");
297 goto done;
300 TAILQ_FOREACH(re, &refs, entry) {
301 if (got_ref_is_symbolic(re->ref))
302 continue;
303 err = send_ref(re->ref, &ibuf);
304 if (err)
305 goto done;
308 err = gotd_imsg_flush(&ibuf);
309 done:
310 got_ref_list_free(&refs);
311 imsg_clear(&ibuf);
312 return err;
315 static const struct got_error *
316 protect_ref_namespace(struct got_reference *ref, const char *namespace)
318 size_t len = strlen(namespace);
320 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
321 namespace[len -1] != '/') {
322 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
323 "reference namespace '%s'", namespace);
326 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
327 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
329 return NULL;
332 static const struct got_error *
333 recv_ref_update(struct imsg *imsg)
335 static const char zero_id[SHA1_DIGEST_LENGTH];
336 const struct got_error *err = NULL;
337 struct repo_write_client *client = &repo_write_client;
338 struct gotd_imsg_ref_update iref;
339 size_t datalen;
340 char *refname = NULL;
341 struct got_reference *ref = NULL;
342 struct got_object_id *id = NULL;
343 struct imsgbuf ibuf;
344 struct gotd_ref_update *ref_update = NULL;
346 log_debug("ref-update received");
348 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
349 if (datalen < sizeof(iref))
350 return got_error(GOT_ERR_PRIVSEP_LEN);
351 memcpy(&iref, imsg->data, sizeof(iref));
352 if (datalen != sizeof(iref) + iref.name_len)
353 return got_error(GOT_ERR_PRIVSEP_LEN);
355 imsg_init(&ibuf, client->fd);
357 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
358 if (refname == NULL)
359 return got_error_from_errno("strndup");
361 ref_update = calloc(1, sizeof(*ref_update));
362 if (ref_update == NULL) {
363 err = got_error_from_errno("malloc");
364 goto done;
367 memcpy(ref_update->old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
368 memcpy(ref_update->new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
370 err = got_ref_open(&ref, repo_write.repo, refname, 0);
371 if (err) {
372 if (err->code != GOT_ERR_NOT_REF)
373 goto done;
374 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
375 if (err)
376 goto done;
377 ref_update->ref_is_new = 1;
378 client->nref_new++;
380 if (got_ref_is_symbolic(ref)) {
381 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
382 "'%s' is a symbolic reference and cannot "
383 "be updated", got_ref_get_name(ref));
384 goto done;
386 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
387 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
388 "%s: does not begin with 'refs/'",
389 got_ref_get_name(ref));
390 goto done;
393 err = protect_ref_namespace(ref, "refs/got/");
394 if (err)
395 goto done;
396 err = protect_ref_namespace(ref, "refs/remotes/");
397 if (err)
398 goto done;
400 if (!ref_update->ref_is_new) {
401 /*
402 * Ensure the client's idea of this update is still valid.
403 * At this point we can only return an error, to prevent
404 * the client from uploading a pack file which will likely
405 * have to be discarded.
406 */
407 err = got_ref_resolve(&id, repo_write.repo, ref);
408 if (err)
409 goto done;
411 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
412 err = got_error_fmt(GOT_ERR_REF_BUSY,
413 "%s has been modified by someone else "
414 "while transaction was in progress",
415 got_ref_get_name(ref));
416 goto done;
420 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
421 repo_write.pid);
423 ref_update->ref = ref;
424 if (memcmp(ref_update->new_id.hash, zero_id, sizeof(zero_id)) == 0) {
425 ref_update->delete_ref = 1;
426 client->nref_del++;
428 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
429 client->nref_updates++;
430 ref = NULL;
431 ref_update = NULL;
432 done:
433 if (ref)
434 got_ref_close(ref);
435 free(ref_update);
436 free(refname);
437 free(id);
438 return err;
441 static const struct got_error *
442 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
443 uint32_t nobj_loose, uint32_t nobj_resolved)
445 int p_indexed = 0, p_resolved = 0;
446 int nobj_delta = nobj_total - nobj_loose;
448 if (nobj_total > 0)
449 p_indexed = (nobj_indexed * 100) / nobj_total;
451 if (nobj_delta > 0)
452 p_resolved = (nobj_resolved * 100) / nobj_delta;
454 if (p_resolved > 0) {
455 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
456 nobj_total, p_indexed, nobj_delta, p_resolved);
457 } else
458 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
460 return NULL;
463 static const struct got_error *
464 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
466 const struct got_error *err = NULL;
467 uint8_t readahead[65536];
468 size_t have, newlen;
470 err = got_poll_read_full(infd, &have,
471 readahead, sizeof(readahead), minsize);
472 if (err)
473 return err;
475 err = buf_append(&newlen, buf, readahead, have);
476 if (err)
477 return err;
478 return NULL;
481 static const struct got_error *
482 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
483 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
485 const struct got_error *err = NULL;
486 uint8_t t = 0;
487 uint64_t s = 0;
488 uint8_t sizebuf[8];
489 size_t i = 0;
490 off_t obj_offset = *outsize;
492 do {
493 /* We do not support size values which don't fit in 64 bit. */
494 if (i > 9)
495 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
496 "packfile offset %lld", (long long)obj_offset);
498 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
499 err = read_more_pack_stream(infd, buf,
500 sizeof(sizebuf[0]));
501 if (err)
502 return err;
505 sizebuf[i] = buf_getc(buf, *buf_pos);
506 *buf_pos += sizeof(sizebuf[i]);
508 if (i == 0) {
509 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
510 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
511 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
512 } else {
513 size_t shift = 4 + 7 * (i - 1);
514 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
515 shift);
517 i++;
518 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
520 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
521 if (err)
522 return err;
523 *outsize += i;
525 *type = t;
526 *size = s;
527 return NULL;
530 static const struct got_error *
531 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
532 SHA1_CTX *ctx)
534 const struct got_error *err = NULL;
535 size_t remain = buf_len(buf) - *buf_pos;
537 if (remain < SHA1_DIGEST_LENGTH) {
538 err = read_more_pack_stream(infd, buf,
539 SHA1_DIGEST_LENGTH - remain);
540 if (err)
541 return err;
544 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
545 SHA1_DIGEST_LENGTH, ctx);
546 if (err)
547 return err;
549 *buf_pos += SHA1_DIGEST_LENGTH;
550 return NULL;
553 static const struct got_error *
554 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
555 SHA1_CTX *ctx)
557 const struct got_error *err = NULL;
558 uint64_t o = 0;
559 uint8_t offbuf[8];
560 size_t i = 0;
561 off_t obj_offset = *outsize;
563 do {
564 /* We do not support offset values which don't fit in 64 bit. */
565 if (i > 8)
566 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
567 "packfile offset %lld", (long long)obj_offset);
569 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
570 err = read_more_pack_stream(infd, buf,
571 sizeof(offbuf[0]));
572 if (err)
573 return err;
576 offbuf[i] = buf_getc(buf, *buf_pos);
577 *buf_pos += sizeof(offbuf[i]);
579 if (i == 0)
580 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
581 else {
582 o++;
583 o <<= 7;
584 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
586 i++;
587 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
589 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
590 return got_error(GOT_ERR_PACK_OFFSET);
592 err = got_pack_hwrite(outfd, offbuf, i, ctx);
593 if (err)
594 return err;
596 *outsize += i;
597 return NULL;
600 static const struct got_error *
601 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
602 SHA1_CTX *ctx)
604 const struct got_error *err = NULL;
605 z_stream z;
606 int zret;
607 char voidbuf[1024];
608 size_t consumed_total = 0;
609 off_t zstream_offset = *outsize;
611 memset(&z, 0, sizeof(z));
613 z.zalloc = Z_NULL;
614 z.zfree = Z_NULL;
615 zret = inflateInit(&z);
616 if (zret != Z_OK) {
617 if (zret == Z_ERRNO)
618 return got_error_from_errno("inflateInit");
619 if (zret == Z_MEM_ERROR) {
620 errno = ENOMEM;
621 return got_error_from_errno("inflateInit");
623 return got_error_msg(GOT_ERR_DECOMPRESSION,
624 "inflateInit failed");
627 while (zret != Z_STREAM_END) {
628 size_t last_total_in, consumed;
630 /*
631 * Decompress into the void. Object data will be parsed
632 * later, when the pack file is indexed. For now, we just
633 * want to locate the end of the compressed stream.
634 */
635 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
636 last_total_in = z.total_in;
637 z.next_in = buf_get(buf) + *buf_pos;
638 z.avail_in = buf_len(buf) - *buf_pos;
639 z.next_out = voidbuf;
640 z.avail_out = sizeof(voidbuf);
642 zret = inflate(&z, Z_SYNC_FLUSH);
643 if (zret != Z_OK && zret != Z_BUF_ERROR &&
644 zret != Z_STREAM_END) {
645 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
646 "packfile offset %lld",
647 (long long)zstream_offset);
648 goto done;
650 consumed = z.total_in - last_total_in;
652 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
653 consumed, ctx);
654 if (err)
655 goto done;
657 err = buf_discard(buf, *buf_pos + consumed);
658 if (err)
659 goto done;
660 *buf_pos = 0;
662 consumed_total += consumed;
665 if (zret != Z_STREAM_END) {
666 err = read_more_pack_stream(infd, buf, 1);
667 if (err)
668 goto done;
672 if (err == NULL)
673 *outsize += consumed_total;
674 done:
675 inflateEnd(&z);
676 return err;
679 static const struct got_error *
680 validate_object_type(int obj_type)
682 switch (obj_type) {
683 case GOT_OBJ_TYPE_BLOB:
684 case GOT_OBJ_TYPE_COMMIT:
685 case GOT_OBJ_TYPE_TREE:
686 case GOT_OBJ_TYPE_TAG:
687 case GOT_OBJ_TYPE_REF_DELTA:
688 case GOT_OBJ_TYPE_OFFSET_DELTA:
689 return NULL;
690 default:
691 break;
694 return got_error(GOT_ERR_OBJ_TYPE);
697 static const struct got_error *
698 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
699 int infd, int outfd)
701 const struct got_error *err;
702 struct repo_write_client *client = &repo_write_client;
703 struct got_packfile_hdr hdr;
704 size_t have;
705 uint32_t nhave = 0;
706 SHA1_CTX ctx;
707 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
708 char hex[SHA1_DIGEST_STRING_LENGTH];
709 BUF *buf = NULL;
710 size_t buf_pos = 0, remain;
711 ssize_t w;
713 *outsize = 0;
714 *nobj = 0;
716 /* if only deleting references there's nothing to read */
717 if (client->nref_updates == client->nref_del)
718 return NULL;
720 SHA1Init(&ctx);
722 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
723 if (err)
724 return err;
725 if (have != sizeof(hdr))
726 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
727 *outsize += have;
729 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
730 return got_error_msg(GOT_ERR_BAD_PACKFILE,
731 "bad packfile signature");
732 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
733 return got_error_msg(GOT_ERR_BAD_PACKFILE,
734 "bad packfile version");
736 *nobj = be32toh(hdr.nobjects);
737 if (*nobj == 0) {
738 /*
739 * Clients which are creating new references only
740 * will send us an empty pack file.
741 */
742 if (client->nref_updates > 0 &&
743 client->nref_updates == client->nref_new)
744 return NULL;
746 return got_error_msg(GOT_ERR_BAD_PACKFILE,
747 "bad packfile with zero objects");
750 log_debug("expecting %d objects", *nobj);
752 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
753 if (err)
754 return err;
756 err = buf_alloc(&buf, 65536);
757 if (err)
758 return err;
760 while (nhave != *nobj) {
761 uint8_t obj_type;
762 uint64_t obj_size;
764 err = copy_object_type_and_size(&obj_type, &obj_size,
765 infd, outfd, outsize, buf, &buf_pos, &ctx);
766 if (err)
767 goto done;
769 err = validate_object_type(obj_type);
770 if (err)
771 goto done;
773 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
774 err = copy_ref_delta(infd, outfd, outsize,
775 buf, &buf_pos, &ctx);
776 if (err)
777 goto done;
778 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
779 err = copy_offset_delta(infd, outfd, outsize,
780 buf, &buf_pos, &ctx);
781 if (err)
782 goto done;
785 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
786 if (err)
787 goto done;
789 nhave++;
792 log_debug("received %u objects", *nobj);
794 SHA1Final(expected_sha1, &ctx);
796 remain = buf_len(buf) - buf_pos;
797 if (remain < SHA1_DIGEST_LENGTH) {
798 err = read_more_pack_stream(infd, buf,
799 SHA1_DIGEST_LENGTH - remain);
800 if (err)
801 return err;
804 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
805 log_debug("expect SHA1: %s", hex);
806 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
807 log_debug("actual SHA1: %s", hex);
809 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
810 SHA1_DIGEST_LENGTH) != 0) {
811 err = got_error(GOT_ERR_PACKFILE_CSUM);
812 goto done;
815 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
817 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
818 if (w == -1) {
819 err = got_error_from_errno("write");
820 goto done;
822 if (w != SHA1_DIGEST_LENGTH) {
823 err = got_error(GOT_ERR_IO);
824 goto done;
827 *outsize += SHA1_DIGEST_LENGTH;
829 if (fsync(outfd) == -1) {
830 err = got_error_from_errno("fsync");
831 goto done;
833 if (lseek(outfd, 0L, SEEK_SET) == -1) {
834 err = got_error_from_errno("lseek");
835 goto done;
837 done:
838 buf_free(buf);
839 return err;
842 static const struct got_error *
843 report_pack_status(const struct got_error *unpack_err)
845 const struct got_error *err = NULL;
846 struct repo_write_client *client = &repo_write_client;
847 struct gotd_imsg_packfile_status istatus;
848 struct ibuf *wbuf;
849 struct imsgbuf ibuf;
850 const char *unpack_ok = "unpack ok\n";
851 size_t len;
853 imsg_init(&ibuf, client->fd);
855 if (unpack_err)
856 istatus.reason_len = strlen(unpack_err->msg);
857 else
858 istatus.reason_len = strlen(unpack_ok);
860 len = sizeof(istatus) + istatus.reason_len;
861 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
862 repo_write.pid, len);
863 if (wbuf == NULL) {
864 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
865 goto done;
868 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
869 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
870 goto done;
873 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
874 istatus.reason_len) == -1) {
875 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
876 goto done;
879 wbuf->fd = -1;
880 imsg_close(&ibuf, wbuf);
882 err = gotd_imsg_flush(&ibuf);
883 done:
884 imsg_clear(&ibuf);
885 return err;
888 static const struct got_error *
889 recv_packfile(int *have_packfile, struct imsg *imsg)
891 const struct got_error *err = NULL, *unpack_err;
892 struct repo_write_client *client = &repo_write_client;
893 struct gotd_imsg_recv_packfile ireq;
894 FILE *tempfiles[3] = { NULL, NULL, NULL };
895 struct repo_tempfile {
896 int fd;
897 int idx;
898 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
899 int i;
900 size_t datalen;
901 struct imsgbuf ibuf;
902 struct got_ratelimit rl;
903 struct got_pack *pack = NULL;
904 off_t pack_filesize = 0;
905 uint32_t nobj = 0;
907 log_debug("packfile request received");
909 *have_packfile = 0;
910 got_ratelimit_init(&rl, 2, 0);
912 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
913 if (datalen != sizeof(ireq))
914 return got_error(GOT_ERR_PRIVSEP_LEN);
915 memcpy(&ireq, imsg->data, sizeof(ireq));
917 if (client->pack_pipe == -1 || client->packidx_fd == -1)
918 return got_error(GOT_ERR_PRIVSEP_NO_FD);
920 imsg_init(&ibuf, client->fd);
922 if (imsg->fd == -1)
923 return got_error(GOT_ERR_PRIVSEP_NO_FD);
925 pack = &client->pack;
926 memset(pack, 0, sizeof(*pack));
927 pack->fd = imsg->fd;
928 err = got_delta_cache_alloc(&pack->delta_cache);
929 if (err)
930 return err;
932 for (i = 0; i < nitems(repo_tempfiles); i++) {
933 struct repo_tempfile *t = &repo_tempfiles[i];
934 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
935 if (err)
936 goto done;
939 for (i = 0; i < nitems(tempfiles); i++) {
940 int fd;
941 FILE *f;
943 fd = dup(repo_tempfiles[i].fd);
944 if (fd == -1) {
945 err = got_error_from_errno("dup");
946 goto done;
948 f = fdopen(fd, "w+");
949 if (f == NULL) {
950 err = got_error_from_errno("fdopen");
951 close(fd);
952 goto done;
954 tempfiles[i] = f;
957 err = gotd_imsg_flush(&ibuf);
958 if (err)
959 goto done;
961 log_debug("receiving pack data");
962 unpack_err = recv_packdata(&pack_filesize, &nobj,
963 client->pack_sha1, client->pack_pipe, pack->fd);
964 if (ireq.report_status) {
965 err = report_pack_status(unpack_err);
966 if (err) {
967 /* Git clients hang up after sending the pack file. */
968 if (err->code == GOT_ERR_EOF)
969 err = NULL;
972 if (unpack_err)
973 err = unpack_err;
974 if (err)
975 goto done;
977 log_debug("pack data received");
979 /*
980 * Clients which are creating new references only will
981 * send us an empty pack file.
982 */
983 if (nobj == 0 &&
984 pack_filesize == sizeof(struct got_packfile_hdr) &&
985 client->nref_updates > 0 &&
986 client->nref_updates == client->nref_new)
987 goto done;
989 /*
990 * Clients which are deleting references only will send
991 * no pack file.
992 */
993 if (nobj == 0 &&
994 client->nref_del > 0 &&
995 client->nref_updates == client->nref_del)
996 goto done;
998 pack->filesize = pack_filesize;
999 *have_packfile = 1;
1001 log_debug("begin indexing pack (%lld bytes in size)",
1002 (long long)pack->filesize);
1003 err = got_pack_index(pack, client->packidx_fd,
1004 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1005 pack_index_progress, NULL, &rl);
1006 if (err)
1007 goto done;
1008 log_debug("done indexing pack");
1010 if (fsync(client->packidx_fd) == -1) {
1011 err = got_error_from_errno("fsync");
1012 goto done;
1014 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1015 err = got_error_from_errno("lseek");
1016 done:
1017 if (close(client->pack_pipe) == -1 && err == NULL)
1018 err = got_error_from_errno("close");
1019 client->pack_pipe = -1;
1020 for (i = 0; i < nitems(repo_tempfiles); i++) {
1021 struct repo_tempfile *t = &repo_tempfiles[i];
1022 if (t->idx != -1)
1023 got_repo_temp_fds_put(t->idx, repo_write.repo);
1025 for (i = 0; i < nitems(tempfiles); i++) {
1026 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1027 err = got_error_from_errno("fclose");
1029 if (err)
1030 got_pack_close(pack);
1031 imsg_clear(&ibuf);
1032 return err;
1035 static const struct got_error *
1036 verify_packfile(void)
1038 const struct got_error *err = NULL, *close_err;
1039 struct repo_write_client *client = &repo_write_client;
1040 struct gotd_ref_update *ref_update;
1041 struct got_packidx *packidx = NULL;
1042 struct stat sb;
1043 char *id_str = NULL;
1044 int idx = -1;
1046 if (STAILQ_EMPTY(&client->ref_updates)) {
1047 return got_error_msg(GOT_ERR_BAD_REQUEST,
1048 "cannot verify pack file without any ref-updates");
1051 if (client->pack.fd == -1) {
1052 return got_error_msg(GOT_ERR_BAD_REQUEST,
1053 "invalid pack file handle during pack verification");
1055 if (client->packidx_fd == -1) {
1056 return got_error_msg(GOT_ERR_BAD_REQUEST,
1057 "invalid pack index handle during pack verification");
1060 if (fstat(client->packidx_fd, &sb) == -1)
1061 return got_error_from_errno("pack index fstat");
1063 packidx = malloc(sizeof(*packidx));
1064 memset(packidx, 0, sizeof(*packidx));
1065 packidx->fd = client->packidx_fd;
1066 client->packidx_fd = -1;
1067 packidx->len = sb.st_size;
1069 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1070 if (err)
1071 return err;
1073 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1074 if (ref_update->delete_ref)
1075 continue;
1077 err = got_object_id_str(&id_str, &ref_update->new_id);
1078 if (err)
1079 goto done;
1081 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1082 if (idx == -1) {
1083 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1084 "advertised object %s is missing from pack file",
1085 id_str);
1086 goto done;
1090 done:
1091 close_err = got_packidx_close(packidx);
1092 if (close_err && err == NULL)
1093 err = close_err;
1094 free(id_str);
1095 return err;
1098 static const struct got_error *
1099 install_packfile(struct gotd_imsgev *iev)
1101 struct repo_write_client *client = &repo_write_client;
1102 struct gotd_imsg_packfile_install inst;
1103 int ret;
1105 memset(&inst, 0, sizeof(inst));
1106 inst.client_id = client->id;
1107 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1109 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1110 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1111 if (ret == -1)
1112 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1114 return NULL;
1117 static const struct got_error *
1118 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1120 struct repo_write_client *client = &repo_write_client;
1121 struct gotd_imsg_ref_updates_start istart;
1122 int ret;
1124 memset(&istart, 0, sizeof(istart));
1125 istart.nref_updates = nref_updates;
1126 istart.client_id = client->id;
1128 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1129 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1130 if (ret == -1)
1131 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1133 return NULL;
1137 static const struct got_error *
1138 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1140 struct repo_write_client *client = &repo_write_client;
1141 struct gotd_imsg_ref_update iref;
1142 const char *refname = got_ref_get_name(ref_update->ref);
1143 struct ibuf *wbuf;
1144 size_t len;
1146 memset(&iref, 0, sizeof(iref));
1147 memcpy(iref.old_id, ref_update->old_id.hash, SHA1_DIGEST_LENGTH);
1148 memcpy(iref.new_id, ref_update->new_id.hash, SHA1_DIGEST_LENGTH);
1149 iref.ref_is_new = ref_update->ref_is_new;
1150 iref.delete_ref = ref_update->delete_ref;
1151 iref.client_id = client->id;
1152 iref.name_len = strlen(refname);
1154 len = sizeof(iref) + iref.name_len;
1155 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1156 repo_write.pid, len);
1157 if (wbuf == NULL)
1158 return got_error_from_errno("imsg_create REF_UPDATE");
1160 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1161 return got_error_from_errno("imsg_add REF_UPDATE");
1162 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1163 return got_error_from_errno("imsg_add REF_UPDATE");
1165 wbuf->fd = -1;
1166 imsg_close(&iev->ibuf, wbuf);
1168 gotd_imsg_event_add(iev);
1169 return NULL;
1172 static const struct got_error *
1173 update_refs(struct gotd_imsgev *iev)
1175 const struct got_error *err = NULL;
1176 struct repo_write_client *client = &repo_write_client;
1177 struct gotd_ref_update *ref_update;
1179 err = send_ref_updates_start(client->nref_updates, iev);
1180 if (err)
1181 return err;
1183 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1184 err = send_ref_update(ref_update, iev);
1185 if (err)
1186 goto done;
1188 done:
1189 return err;
1192 static const struct got_error *
1193 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1195 struct repo_write_client *client = &repo_write_client;
1196 struct gotd_imsg_packfile_pipe ireq;
1197 size_t datalen;
1199 log_debug("receving pack pipe descriptor");
1201 if (imsg->fd == -1)
1202 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1204 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1205 if (datalen != sizeof(ireq))
1206 return got_error(GOT_ERR_PRIVSEP_LEN);
1207 memcpy(&ireq, imsg->data, sizeof(ireq));
1209 if (client->pack_pipe != -1)
1210 return got_error(GOT_ERR_PRIVSEP_MSG);
1212 client->pack_pipe = imsg->fd;
1213 return NULL;
1216 static const struct got_error *
1217 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1219 struct repo_write_client *client = &repo_write_client;
1220 struct gotd_imsg_packidx_file ireq;
1221 size_t datalen;
1223 log_debug("receving pack index output file");
1225 if (imsg->fd == -1)
1226 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1228 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1229 if (datalen != sizeof(ireq))
1230 return got_error(GOT_ERR_PRIVSEP_LEN);
1231 memcpy(&ireq, imsg->data, sizeof(ireq));
1233 if (client->packidx_fd != -1)
1234 return got_error(GOT_ERR_PRIVSEP_MSG);
1236 client->packidx_fd = imsg->fd;
1237 return NULL;
1240 static void
1241 repo_write_dispatch_session(int fd, short event, void *arg)
1243 const struct got_error *err = NULL;
1244 struct gotd_imsgev *iev = arg;
1245 struct imsgbuf *ibuf = &iev->ibuf;
1246 struct imsg imsg;
1247 struct repo_write_client *client = &repo_write_client;
1248 ssize_t n;
1249 int shut = 0, have_packfile = 0;
1251 if (event & EV_READ) {
1252 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1253 fatal("imsg_read error");
1254 if (n == 0) /* Connection closed. */
1255 shut = 1;
1258 if (event & EV_WRITE) {
1259 n = msgbuf_write(&ibuf->w);
1260 if (n == -1 && errno != EAGAIN)
1261 fatal("msgbuf_write");
1262 if (n == 0) /* Connection closed. */
1263 shut = 1;
1266 for (;;) {
1267 if ((n = imsg_get(ibuf, &imsg)) == -1)
1268 fatal("%s: imsg_get error", __func__);
1269 if (n == 0) /* No more messages. */
1270 break;
1272 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1273 client->id == 0) {
1274 err = got_error(GOT_ERR_PRIVSEP_MSG);
1275 break;
1278 switch (imsg.hdr.type) {
1279 case GOTD_IMSG_LIST_REFS_INTERNAL:
1280 err = list_refs(&imsg);
1281 if (err)
1282 log_warnx("%s: ls-refs: %s", repo_write.title,
1283 err->msg);
1284 break;
1285 case GOTD_IMSG_REF_UPDATE:
1286 err = recv_ref_update(&imsg);
1287 if (err)
1288 log_warnx("%s: ref-update: %s",
1289 repo_write.title, err->msg);
1290 break;
1291 case GOTD_IMSG_PACKFILE_PIPE:
1292 err = receive_pack_pipe(&imsg, iev);
1293 if (err) {
1294 log_warnx("%s: receiving pack pipe: %s",
1295 repo_write.title, err->msg);
1296 break;
1298 break;
1299 case GOTD_IMSG_PACKIDX_FILE:
1300 err = receive_pack_idx(&imsg, iev);
1301 if (err) {
1302 log_warnx("%s: receiving pack index: %s",
1303 repo_write.title, err->msg);
1304 break;
1306 break;
1307 case GOTD_IMSG_RECV_PACKFILE:
1308 err = recv_packfile(&have_packfile, &imsg);
1309 if (err) {
1310 log_warnx("%s: receive packfile: %s",
1311 repo_write.title, err->msg);
1312 break;
1314 if (have_packfile) {
1315 err = verify_packfile();
1316 if (err) {
1317 log_warnx("%s: verify packfile: %s",
1318 repo_write.title, err->msg);
1319 break;
1321 err = install_packfile(iev);
1322 if (err) {
1323 log_warnx("%s: install packfile: %s",
1324 repo_write.title, err->msg);
1325 break;
1328 err = update_refs(iev);
1329 if (err) {
1330 log_warnx("%s: update refs: %s",
1331 repo_write.title, err->msg);
1333 break;
1334 default:
1335 log_debug("%s: unexpected imsg %d", repo_write.title,
1336 imsg.hdr.type);
1337 break;
1340 imsg_free(&imsg);
1343 if (!shut && check_cancelled(NULL) == NULL) {
1344 if (err &&
1345 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1346 client->id, err) == -1) {
1347 log_warnx("could not send error to parent: %s",
1348 err->msg);
1350 gotd_imsg_event_add(iev);
1351 } else {
1352 /* This pipe is dead. Remove its event handler */
1353 event_del(&iev->ev);
1354 event_loopexit(NULL);
1358 static const struct got_error *
1359 recv_connect(struct imsg *imsg)
1361 struct gotd_imsgev *iev = &repo_write.session_iev;
1362 size_t datalen;
1364 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1365 if (datalen != 0)
1366 return got_error(GOT_ERR_PRIVSEP_LEN);
1367 if (imsg->fd == -1)
1368 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1370 if (repo_write.session_fd != -1)
1371 return got_error(GOT_ERR_PRIVSEP_MSG);
1373 repo_write.session_fd = imsg->fd;
1375 imsg_init(&iev->ibuf, repo_write.session_fd);
1376 iev->handler = repo_write_dispatch_session;
1377 iev->events = EV_READ;
1378 iev->handler_arg = NULL;
1379 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1380 repo_write_dispatch_session, iev);
1381 gotd_imsg_event_add(iev);
1383 return NULL;
1386 static void
1387 repo_write_dispatch(int fd, short event, void *arg)
1389 const struct got_error *err = NULL;
1390 struct gotd_imsgev *iev = arg;
1391 struct imsgbuf *ibuf = &iev->ibuf;
1392 struct imsg imsg;
1393 ssize_t n;
1394 int shut = 0;
1395 struct repo_write_client *client = &repo_write_client;
1397 if (event & EV_READ) {
1398 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1399 fatal("imsg_read error");
1400 if (n == 0) /* Connection closed. */
1401 shut = 1;
1404 if (event & EV_WRITE) {
1405 n = msgbuf_write(&ibuf->w);
1406 if (n == -1 && errno != EAGAIN)
1407 fatal("msgbuf_write");
1408 if (n == 0) /* Connection closed. */
1409 shut = 1;
1412 while (err == NULL && check_cancelled(NULL) == NULL) {
1413 if ((n = imsg_get(ibuf, &imsg)) == -1)
1414 fatal("%s: imsg_get", __func__);
1415 if (n == 0) /* No more messages. */
1416 break;
1418 switch (imsg.hdr.type) {
1419 case GOTD_IMSG_CONNECT_REPO_CHILD:
1420 err = recv_connect(&imsg);
1421 break;
1422 default:
1423 log_debug("%s: unexpected imsg %d", repo_write.title,
1424 imsg.hdr.type);
1425 break;
1428 imsg_free(&imsg);
1431 if (!shut && check_cancelled(NULL) == NULL) {
1432 if (err &&
1433 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1434 client->id, err) == -1) {
1435 log_warnx("could not send error to parent: %s",
1436 err->msg);
1438 gotd_imsg_event_add(iev);
1439 } else {
1440 /* This pipe is dead. Remove its event handler */
1441 event_del(&iev->ev);
1442 event_loopexit(NULL);
1446 void
1447 repo_write_main(const char *title, const char *repo_path,
1448 int *pack_fds, int *temp_fds)
1450 const struct got_error *err = NULL;
1451 struct repo_write_client *client = &repo_write_client;
1452 struct gotd_imsgev iev;
1454 client->fd = -1;
1455 client->pack_pipe = -1;
1456 client->packidx_fd = -1;
1457 client->pack.fd = -1;
1459 repo_write.title = title;
1460 repo_write.pid = getpid();
1461 repo_write.pack_fds = pack_fds;
1462 repo_write.temp_fds = temp_fds;
1463 repo_write.session_fd = -1;
1464 repo_write.session_iev.ibuf.fd = -1;
1466 STAILQ_INIT(&repo_write_client.ref_updates);
1468 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1469 if (err)
1470 goto done;
1471 if (!got_repo_is_bare(repo_write.repo)) {
1472 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1473 "bare git repository required");
1474 goto done;
1477 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1479 signal(SIGINT, catch_sigint);
1480 signal(SIGTERM, catch_sigterm);
1481 signal(SIGPIPE, SIG_IGN);
1482 signal(SIGHUP, SIG_IGN);
1484 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1485 iev.handler = repo_write_dispatch;
1486 iev.events = EV_READ;
1487 iev.handler_arg = NULL;
1488 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1489 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1490 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1491 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1492 goto done;
1495 event_dispatch();
1496 done:
1497 if (err)
1498 log_warnx("%s: %s", title, err->msg);
1499 repo_write_shutdown();
1502 void
1503 repo_write_shutdown(void)
1505 struct repo_write_client *client = &repo_write_client;
1506 struct gotd_ref_update *ref_update;
1508 log_debug("%s: shutting down", repo_write.title);
1510 while (!STAILQ_EMPTY(&client->ref_updates)) {
1511 ref_update = STAILQ_FIRST(&client->ref_updates);
1512 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1513 got_ref_close(ref_update->ref);
1514 free(ref_update);
1517 got_pack_close(&client->pack);
1518 if (client->fd != -1)
1519 close(client->fd);
1520 if (client->pack_pipe != -1)
1521 close(client->pack_pipe);
1522 if (client->packidx_fd != -1)
1523 close(client->packidx_fd);
1525 if (repo_write.repo)
1526 got_repo_close(repo_write.repo);
1527 got_repo_pack_fds_close(repo_write.pack_fds);
1528 got_repo_temp_fds_close(repo_write.temp_fds);
1529 if (repo_write.session_fd != -1)
1530 close(repo_write.session_fd);
1531 exit(0);