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_hash.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_ratelimit.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_pack_index.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_poll.h"
56 #include "log.h"
57 #include "gotd.h"
58 #include "repo_write.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 static struct repo_write {
65 pid_t pid;
66 const char *title;
67 struct got_repository *repo;
68 int *pack_fds;
69 int *temp_fds;
70 int session_fd;
71 struct gotd_imsgev session_iev;
72 } repo_write;
74 struct gotd_ref_update {
75 STAILQ_ENTRY(gotd_ref_update) entry;
76 struct got_reference *ref;
77 int ref_is_new;
78 int delete_ref;
79 struct got_object_id old_id;
80 struct got_object_id new_id;
81 };
82 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
84 static struct repo_write_client {
85 uint32_t id;
86 int fd;
87 int pack_pipe;
88 struct got_pack pack;
89 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
90 int packidx_fd;
91 struct gotd_ref_updates ref_updates;
92 int nref_updates;
93 int nref_del;
94 int nref_new;
95 } repo_write_client;
97 static volatile sig_atomic_t sigint_received;
98 static volatile sig_atomic_t sigterm_received;
100 static void
101 catch_sigint(int signo)
103 sigint_received = 1;
106 static void
107 catch_sigterm(int signo)
109 sigterm_received = 1;
112 static const struct got_error *
113 check_cancelled(void *arg)
115 if (sigint_received || sigterm_received)
116 return got_error(GOT_ERR_CANCELLED);
118 return NULL;
121 static const struct got_error *
122 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
123 struct imsgbuf *ibuf)
125 const struct got_error *err = NULL;
126 struct got_tag_object *tag;
127 size_t namelen, len;
128 char *peeled_refname = NULL;
129 struct got_object_id *id;
130 struct ibuf *wbuf;
132 err = got_object_tag_open(&tag, repo_write.repo, obj);
133 if (err)
134 return err;
136 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
137 err = got_error_from_errno("asprintf");
138 goto done;
141 id = got_object_tag_get_object_id(tag);
142 namelen = strlen(peeled_refname);
144 len = sizeof(struct gotd_imsg_ref) + namelen;
145 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
146 err = got_error(GOT_ERR_NO_SPACE);
147 goto done;
150 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
151 repo_write.pid, len);
152 if (wbuf == NULL) {
153 err = got_error_from_errno("imsg_create REF");
154 goto done;
157 /* Keep in sync with struct gotd_imsg_ref definition. */
158 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
159 err = got_error_from_errno("imsg_add REF");
160 goto done;
162 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
163 err = got_error_from_errno("imsg_add REF");
164 goto done;
166 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
167 err = got_error_from_errno("imsg_add REF");
168 goto done;
171 wbuf->fd = -1;
172 imsg_close(ibuf, wbuf);
173 done:
174 got_object_tag_close(tag);
175 return err;
178 static const struct got_error *
179 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
181 const struct got_error *err;
182 const char *refname = got_ref_get_name(ref);
183 size_t namelen;
184 struct got_object_id *id = NULL;
185 struct got_object *obj = NULL;
186 size_t len;
187 struct ibuf *wbuf;
189 namelen = strlen(refname);
191 len = sizeof(struct gotd_imsg_ref) + namelen;
192 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
193 return got_error(GOT_ERR_NO_SPACE);
195 err = got_ref_resolve(&id, repo_write.repo, ref);
196 if (err)
197 return err;
199 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
200 repo_write.pid, len);
201 if (wbuf == NULL) {
202 err = got_error_from_errno("imsg_create REF");
203 goto done;
206 /* Keep in sync with struct gotd_imsg_ref definition. */
207 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
208 return got_error_from_errno("imsg_add REF");
209 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
210 return got_error_from_errno("imsg_add REF");
211 if (imsg_add(wbuf, refname, namelen) == -1)
212 return got_error_from_errno("imsg_add REF");
214 wbuf->fd = -1;
215 imsg_close(ibuf, wbuf);
217 err = got_object_open(&obj, repo_write.repo, id);
218 if (err)
219 goto done;
220 if (obj->type == GOT_OBJ_TYPE_TAG)
221 err = send_peeled_tag_ref(ref, obj, ibuf);
222 done:
223 if (obj)
224 got_object_close(obj);
225 free(id);
226 return err;
229 static const struct got_error *
230 list_refs(struct imsg *imsg)
232 const struct got_error *err;
233 struct repo_write_client *client = &repo_write_client;
234 struct got_reflist_head refs;
235 struct got_reflist_entry *re;
236 struct gotd_imsg_list_refs_internal ireq;
237 size_t datalen;
238 struct gotd_imsg_reflist irefs;
239 struct imsgbuf ibuf;
240 int client_fd = imsg->fd;
242 TAILQ_INIT(&refs);
244 if (client_fd == -1)
245 return got_error(GOT_ERR_PRIVSEP_NO_FD);
247 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
248 if (datalen != sizeof(ireq))
249 return got_error(GOT_ERR_PRIVSEP_LEN);
250 memcpy(&ireq, imsg->data, sizeof(ireq));
252 if (ireq.client_id == 0)
253 return got_error(GOT_ERR_CLIENT_ID);
254 if (client->id != 0) {
255 return got_error_msg(GOT_ERR_CLIENT_ID,
256 "duplicate list-refs request");
258 client->id = ireq.client_id;
259 client->fd = client_fd;
260 client->nref_updates = 0;
261 client->nref_del = 0;
262 client->nref_new = 0;
264 imsg_init(&ibuf, client_fd);
266 err = got_ref_list(&refs, repo_write.repo, "",
267 got_ref_cmp_by_name, NULL);
268 if (err)
269 return err;
271 memset(&irefs, 0, sizeof(irefs));
272 TAILQ_FOREACH(re, &refs, entry) {
273 struct got_object_id *id;
274 int obj_type;
276 if (got_ref_is_symbolic(re->ref))
277 continue;
279 irefs.nrefs++;
281 /* Account for a peeled tag refs. */
282 err = got_ref_resolve(&id, repo_write.repo, re->ref);
283 if (err)
284 goto done;
285 err = got_object_get_type(&obj_type, repo_write.repo, id);
286 free(id);
287 if (err)
288 goto done;
289 if (obj_type == GOT_OBJ_TYPE_TAG)
290 irefs.nrefs++;
293 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
294 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
295 err = got_error_from_errno("imsg_compose REFLIST");
296 goto done;
299 TAILQ_FOREACH(re, &refs, entry) {
300 if (got_ref_is_symbolic(re->ref))
301 continue;
302 err = send_ref(re->ref, &ibuf);
303 if (err)
304 goto done;
307 err = gotd_imsg_flush(&ibuf);
308 done:
309 got_ref_list_free(&refs);
310 imsg_clear(&ibuf);
311 return err;
314 static const struct got_error *
315 protect_ref_namespace(struct got_reference *ref, const char *namespace)
317 size_t len = strlen(namespace);
319 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
320 namespace[len -1] != '/') {
321 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
322 "reference namespace '%s'", namespace);
325 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
326 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
328 return NULL;
331 static const struct got_error *
332 recv_ref_update(struct imsg *imsg)
334 static const char zero_id[SHA1_DIGEST_LENGTH];
335 const struct got_error *err = NULL;
336 struct repo_write_client *client = &repo_write_client;
337 struct gotd_imsg_ref_update iref;
338 size_t datalen;
339 char *refname = NULL;
340 struct got_reference *ref = NULL;
341 struct got_object_id *id = NULL;
342 struct imsgbuf ibuf;
343 struct gotd_ref_update *ref_update = NULL;
345 log_debug("ref-update received");
347 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
348 if (datalen < sizeof(iref))
349 return got_error(GOT_ERR_PRIVSEP_LEN);
350 memcpy(&iref, imsg->data, sizeof(iref));
351 if (datalen != sizeof(iref) + iref.name_len)
352 return got_error(GOT_ERR_PRIVSEP_LEN);
354 imsg_init(&ibuf, client->fd);
356 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
357 if (refname == NULL)
358 return got_error_from_errno("strndup");
360 ref_update = calloc(1, sizeof(*ref_update));
361 if (ref_update == NULL) {
362 err = got_error_from_errno("malloc");
363 goto done;
366 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
367 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
369 err = got_ref_open(&ref, repo_write.repo, refname, 0);
370 if (err) {
371 if (err->code != GOT_ERR_NOT_REF)
372 goto done;
373 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
374 if (err)
375 goto done;
376 ref_update->ref_is_new = 1;
377 client->nref_new++;
379 if (got_ref_is_symbolic(ref)) {
380 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
381 "'%s' is a symbolic reference and cannot "
382 "be updated", got_ref_get_name(ref));
383 goto done;
385 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
386 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
387 "%s: does not begin with 'refs/'",
388 got_ref_get_name(ref));
389 goto done;
392 err = protect_ref_namespace(ref, "refs/got/");
393 if (err)
394 goto done;
395 err = protect_ref_namespace(ref, "refs/remotes/");
396 if (err)
397 goto done;
399 if (!ref_update->ref_is_new) {
400 /*
401 * Ensure the client's idea of this update is still valid.
402 * At this point we can only return an error, to prevent
403 * the client from uploading a pack file which will likely
404 * have to be discarded.
405 */
406 err = got_ref_resolve(&id, repo_write.repo, ref);
407 if (err)
408 goto done;
410 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
411 err = got_error_fmt(GOT_ERR_REF_BUSY,
412 "%s has been modified by someone else "
413 "while transaction was in progress",
414 got_ref_get_name(ref));
415 goto done;
419 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
420 repo_write.pid);
422 ref_update->ref = ref;
423 if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
424 ref_update->delete_ref = 1;
425 client->nref_del++;
427 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
428 client->nref_updates++;
429 ref = NULL;
430 ref_update = NULL;
431 done:
432 if (ref)
433 got_ref_close(ref);
434 free(ref_update);
435 free(refname);
436 free(id);
437 return err;
440 static const struct got_error *
441 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
442 uint32_t nobj_loose, uint32_t nobj_resolved)
444 int p_indexed = 0, p_resolved = 0;
445 int nobj_delta = nobj_total - nobj_loose;
447 if (nobj_total > 0)
448 p_indexed = (nobj_indexed * 100) / nobj_total;
450 if (nobj_delta > 0)
451 p_resolved = (nobj_resolved * 100) / nobj_delta;
453 if (p_resolved > 0) {
454 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
455 nobj_total, p_indexed, nobj_delta, p_resolved);
456 } else
457 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
459 return NULL;
462 static const struct got_error *
463 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
465 const struct got_error *err = NULL;
466 uint8_t readahead[65536];
467 size_t have, newlen;
469 err = got_poll_read_full(infd, &have,
470 readahead, sizeof(readahead), minsize);
471 if (err)
472 return err;
474 err = buf_append(&newlen, buf, readahead, have);
475 if (err)
476 return err;
477 return NULL;
480 static const struct got_error *
481 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
482 off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
484 const struct got_error *err = NULL;
485 uint8_t t = 0;
486 uint64_t s = 0;
487 uint8_t sizebuf[8];
488 size_t i = 0;
489 off_t obj_offset = *outsize;
491 do {
492 /* We do not support size values which don't fit in 64 bit. */
493 if (i > 9)
494 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
495 "packfile offset %lld", (long long)obj_offset);
497 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
498 err = read_more_pack_stream(infd, buf,
499 sizeof(sizebuf[0]));
500 if (err)
501 return err;
504 sizebuf[i] = buf_getc(buf, *buf_pos);
505 *buf_pos += sizeof(sizebuf[i]);
507 if (i == 0) {
508 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
509 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
510 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
511 } else {
512 size_t shift = 4 + 7 * (i - 1);
513 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
514 shift);
516 i++;
517 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
519 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
520 if (err)
521 return err;
522 *outsize += i;
524 *type = t;
525 *size = s;
526 return NULL;
529 static const struct got_error *
530 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
531 struct got_hash *ctx)
533 const struct got_error *err = NULL;
534 size_t remain = buf_len(buf) - *buf_pos;
536 if (remain < SHA1_DIGEST_LENGTH) {
537 err = read_more_pack_stream(infd, buf,
538 SHA1_DIGEST_LENGTH - remain);
539 if (err)
540 return err;
543 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
544 SHA1_DIGEST_LENGTH, ctx);
545 if (err)
546 return err;
548 *buf_pos += SHA1_DIGEST_LENGTH;
549 return NULL;
552 static const struct got_error *
553 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
554 struct got_hash *ctx)
556 const struct got_error *err = NULL;
557 uint64_t o = 0;
558 uint8_t offbuf[8];
559 size_t i = 0;
560 off_t obj_offset = *outsize;
562 do {
563 /* We do not support offset values which don't fit in 64 bit. */
564 if (i > 8)
565 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
566 "packfile offset %lld", (long long)obj_offset);
568 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
569 err = read_more_pack_stream(infd, buf,
570 sizeof(offbuf[0]));
571 if (err)
572 return err;
575 offbuf[i] = buf_getc(buf, *buf_pos);
576 *buf_pos += sizeof(offbuf[i]);
578 if (i == 0)
579 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
580 else {
581 o++;
582 o <<= 7;
583 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
585 i++;
586 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
588 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
589 return got_error(GOT_ERR_PACK_OFFSET);
591 err = got_pack_hwrite(outfd, offbuf, i, ctx);
592 if (err)
593 return err;
595 *outsize += i;
596 return NULL;
599 static const struct got_error *
600 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
601 struct got_hash *ctx)
603 const struct got_error *err = NULL;
604 z_stream z;
605 int zret;
606 char voidbuf[1024];
607 size_t consumed_total = 0;
608 off_t zstream_offset = *outsize;
610 memset(&z, 0, sizeof(z));
612 z.zalloc = Z_NULL;
613 z.zfree = Z_NULL;
614 zret = inflateInit(&z);
615 if (zret != Z_OK) {
616 if (zret == Z_ERRNO)
617 return got_error_from_errno("inflateInit");
618 if (zret == Z_MEM_ERROR) {
619 errno = ENOMEM;
620 return got_error_from_errno("inflateInit");
622 return got_error_msg(GOT_ERR_DECOMPRESSION,
623 "inflateInit failed");
626 while (zret != Z_STREAM_END) {
627 size_t last_total_in, consumed;
629 /*
630 * Decompress into the void. Object data will be parsed
631 * later, when the pack file is indexed. For now, we just
632 * want to locate the end of the compressed stream.
633 */
634 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
635 last_total_in = z.total_in;
636 z.next_in = buf_get(buf) + *buf_pos;
637 z.avail_in = buf_len(buf) - *buf_pos;
638 z.next_out = voidbuf;
639 z.avail_out = sizeof(voidbuf);
641 zret = inflate(&z, Z_SYNC_FLUSH);
642 if (zret != Z_OK && zret != Z_BUF_ERROR &&
643 zret != Z_STREAM_END) {
644 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
645 "packfile offset %lld",
646 (long long)zstream_offset);
647 goto done;
649 consumed = z.total_in - last_total_in;
651 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
652 consumed, ctx);
653 if (err)
654 goto done;
656 err = buf_discard(buf, *buf_pos + consumed);
657 if (err)
658 goto done;
659 *buf_pos = 0;
661 consumed_total += consumed;
664 if (zret != Z_STREAM_END) {
665 err = read_more_pack_stream(infd, buf, 1);
666 if (err)
667 goto done;
671 if (err == NULL)
672 *outsize += consumed_total;
673 done:
674 inflateEnd(&z);
675 return err;
678 static const struct got_error *
679 validate_object_type(int obj_type)
681 switch (obj_type) {
682 case GOT_OBJ_TYPE_BLOB:
683 case GOT_OBJ_TYPE_COMMIT:
684 case GOT_OBJ_TYPE_TREE:
685 case GOT_OBJ_TYPE_TAG:
686 case GOT_OBJ_TYPE_REF_DELTA:
687 case GOT_OBJ_TYPE_OFFSET_DELTA:
688 return NULL;
689 default:
690 break;
693 return got_error(GOT_ERR_OBJ_TYPE);
696 static const struct got_error *
697 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
698 int infd, int outfd)
700 const struct got_error *err;
701 struct repo_write_client *client = &repo_write_client;
702 struct got_packfile_hdr hdr;
703 size_t have;
704 uint32_t nhave = 0;
705 struct got_hash ctx;
706 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
707 char hex[SHA1_DIGEST_STRING_LENGTH];
708 BUF *buf = NULL;
709 size_t buf_pos = 0, remain;
710 ssize_t w;
712 *outsize = 0;
713 *nobj = 0;
715 /* if only deleting references there's nothing to read */
716 if (client->nref_updates == client->nref_del)
717 return NULL;
719 got_hash_init(&ctx, GOT_HASH_SHA1);
721 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
722 if (err)
723 return err;
724 if (have != sizeof(hdr))
725 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
726 *outsize += have;
728 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
729 return got_error_msg(GOT_ERR_BAD_PACKFILE,
730 "bad packfile signature");
731 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
732 return got_error_msg(GOT_ERR_BAD_PACKFILE,
733 "bad packfile version");
735 *nobj = be32toh(hdr.nobjects);
736 if (*nobj == 0) {
737 /*
738 * Clients which are creating new references only
739 * will send us an empty pack file.
740 */
741 if (client->nref_updates > 0 &&
742 client->nref_updates == client->nref_new)
743 return NULL;
745 return got_error_msg(GOT_ERR_BAD_PACKFILE,
746 "bad packfile with zero objects");
749 log_debug("expecting %d objects", *nobj);
751 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
752 if (err)
753 return err;
755 err = buf_alloc(&buf, 65536);
756 if (err)
757 return err;
759 while (nhave != *nobj) {
760 uint8_t obj_type;
761 uint64_t obj_size;
763 err = copy_object_type_and_size(&obj_type, &obj_size,
764 infd, outfd, outsize, buf, &buf_pos, &ctx);
765 if (err)
766 goto done;
768 err = validate_object_type(obj_type);
769 if (err)
770 goto done;
772 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
773 err = copy_ref_delta(infd, outfd, outsize,
774 buf, &buf_pos, &ctx);
775 if (err)
776 goto done;
777 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
778 err = copy_offset_delta(infd, outfd, outsize,
779 buf, &buf_pos, &ctx);
780 if (err)
781 goto done;
784 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
785 if (err)
786 goto done;
788 nhave++;
791 log_debug("received %u objects", *nobj);
793 got_hash_final(&ctx, expected_sha1);
795 remain = buf_len(buf) - buf_pos;
796 if (remain < SHA1_DIGEST_LENGTH) {
797 err = read_more_pack_stream(infd, buf,
798 SHA1_DIGEST_LENGTH - remain);
799 if (err)
800 return err;
803 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
804 log_debug("expect SHA1: %s", hex);
805 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
806 log_debug("actual SHA1: %s", hex);
808 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
809 SHA1_DIGEST_LENGTH) != 0) {
810 err = got_error(GOT_ERR_PACKFILE_CSUM);
811 goto done;
814 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
816 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
817 if (w == -1) {
818 err = got_error_from_errno("write");
819 goto done;
821 if (w != SHA1_DIGEST_LENGTH) {
822 err = got_error(GOT_ERR_IO);
823 goto done;
826 *outsize += SHA1_DIGEST_LENGTH;
828 if (fsync(outfd) == -1) {
829 err = got_error_from_errno("fsync");
830 goto done;
832 if (lseek(outfd, 0L, SEEK_SET) == -1) {
833 err = got_error_from_errno("lseek");
834 goto done;
836 done:
837 buf_free(buf);
838 return err;
841 static const struct got_error *
842 report_pack_status(const struct got_error *unpack_err)
844 const struct got_error *err = NULL;
845 struct repo_write_client *client = &repo_write_client;
846 struct gotd_imsg_packfile_status istatus;
847 struct ibuf *wbuf;
848 struct imsgbuf ibuf;
849 const char *unpack_ok = "unpack ok\n";
850 size_t len;
852 imsg_init(&ibuf, client->fd);
854 if (unpack_err)
855 istatus.reason_len = strlen(unpack_err->msg);
856 else
857 istatus.reason_len = strlen(unpack_ok);
859 len = sizeof(istatus) + istatus.reason_len;
860 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
861 repo_write.pid, len);
862 if (wbuf == NULL) {
863 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
864 goto done;
867 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
868 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
869 goto done;
872 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
873 istatus.reason_len) == -1) {
874 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
875 goto done;
878 wbuf->fd = -1;
879 imsg_close(&ibuf, wbuf);
881 err = gotd_imsg_flush(&ibuf);
882 done:
883 imsg_clear(&ibuf);
884 return err;
887 static const struct got_error *
888 recv_packfile(int *have_packfile, struct imsg *imsg)
890 const struct got_error *err = NULL, *unpack_err;
891 struct repo_write_client *client = &repo_write_client;
892 struct gotd_imsg_recv_packfile ireq;
893 FILE *tempfiles[3] = { NULL, NULL, NULL };
894 struct repo_tempfile {
895 int fd;
896 int idx;
897 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
898 int i;
899 size_t datalen;
900 struct imsgbuf ibuf;
901 struct got_ratelimit rl;
902 struct got_pack *pack = NULL;
903 off_t pack_filesize = 0;
904 uint32_t nobj = 0;
906 log_debug("packfile request received");
908 *have_packfile = 0;
909 got_ratelimit_init(&rl, 2, 0);
911 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
912 if (datalen != sizeof(ireq))
913 return got_error(GOT_ERR_PRIVSEP_LEN);
914 memcpy(&ireq, imsg->data, sizeof(ireq));
916 if (client->pack_pipe == -1 || client->packidx_fd == -1)
917 return got_error(GOT_ERR_PRIVSEP_NO_FD);
919 imsg_init(&ibuf, client->fd);
921 if (imsg->fd == -1)
922 return got_error(GOT_ERR_PRIVSEP_NO_FD);
924 pack = &client->pack;
925 memset(pack, 0, sizeof(*pack));
926 pack->fd = imsg->fd;
927 err = got_delta_cache_alloc(&pack->delta_cache);
928 if (err)
929 return err;
931 for (i = 0; i < nitems(repo_tempfiles); i++) {
932 struct repo_tempfile *t = &repo_tempfiles[i];
933 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
934 if (err)
935 goto done;
938 for (i = 0; i < nitems(tempfiles); i++) {
939 int fd;
940 FILE *f;
942 fd = dup(repo_tempfiles[i].fd);
943 if (fd == -1) {
944 err = got_error_from_errno("dup");
945 goto done;
947 f = fdopen(fd, "w+");
948 if (f == NULL) {
949 err = got_error_from_errno("fdopen");
950 close(fd);
951 goto done;
953 tempfiles[i] = f;
956 err = gotd_imsg_flush(&ibuf);
957 if (err)
958 goto done;
960 log_debug("receiving pack data");
961 unpack_err = recv_packdata(&pack_filesize, &nobj,
962 client->pack_sha1, client->pack_pipe, pack->fd);
963 if (ireq.report_status) {
964 err = report_pack_status(unpack_err);
965 if (err) {
966 /* Git clients hang up after sending the pack file. */
967 if (err->code == GOT_ERR_EOF)
968 err = NULL;
971 if (unpack_err)
972 err = unpack_err;
973 if (err)
974 goto done;
976 log_debug("pack data received");
978 /*
979 * Clients which are creating new references only will
980 * send us an empty pack file.
981 */
982 if (nobj == 0 &&
983 pack_filesize == sizeof(struct got_packfile_hdr) &&
984 client->nref_updates > 0 &&
985 client->nref_updates == client->nref_new)
986 goto done;
988 /*
989 * Clients which are deleting references only will send
990 * no pack file.
991 */
992 if (nobj == 0 &&
993 client->nref_del > 0 &&
994 client->nref_updates == client->nref_del)
995 goto done;
997 pack->filesize = pack_filesize;
998 *have_packfile = 1;
1000 log_debug("begin indexing pack (%lld bytes in size)",
1001 (long long)pack->filesize);
1002 err = got_pack_index(pack, client->packidx_fd,
1003 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1004 pack_index_progress, NULL, &rl);
1005 if (err)
1006 goto done;
1007 log_debug("done indexing pack");
1009 if (fsync(client->packidx_fd) == -1) {
1010 err = got_error_from_errno("fsync");
1011 goto done;
1013 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1014 err = got_error_from_errno("lseek");
1015 done:
1016 if (close(client->pack_pipe) == -1 && err == NULL)
1017 err = got_error_from_errno("close");
1018 client->pack_pipe = -1;
1019 for (i = 0; i < nitems(repo_tempfiles); i++) {
1020 struct repo_tempfile *t = &repo_tempfiles[i];
1021 if (t->idx != -1)
1022 got_repo_temp_fds_put(t->idx, repo_write.repo);
1024 for (i = 0; i < nitems(tempfiles); i++) {
1025 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1026 err = got_error_from_errno("fclose");
1028 if (err)
1029 got_pack_close(pack);
1030 imsg_clear(&ibuf);
1031 return err;
1034 static const struct got_error *
1035 verify_packfile(void)
1037 const struct got_error *err = NULL, *close_err;
1038 struct repo_write_client *client = &repo_write_client;
1039 struct gotd_ref_update *ref_update;
1040 struct got_packidx *packidx = NULL;
1041 struct stat sb;
1042 char *id_str = NULL;
1043 int idx = -1;
1045 if (STAILQ_EMPTY(&client->ref_updates)) {
1046 return got_error_msg(GOT_ERR_BAD_REQUEST,
1047 "cannot verify pack file without any ref-updates");
1050 if (client->pack.fd == -1) {
1051 return got_error_msg(GOT_ERR_BAD_REQUEST,
1052 "invalid pack file handle during pack verification");
1054 if (client->packidx_fd == -1) {
1055 return got_error_msg(GOT_ERR_BAD_REQUEST,
1056 "invalid pack index handle during pack verification");
1059 if (fstat(client->packidx_fd, &sb) == -1)
1060 return got_error_from_errno("pack index fstat");
1062 packidx = malloc(sizeof(*packidx));
1063 memset(packidx, 0, sizeof(*packidx));
1064 packidx->fd = client->packidx_fd;
1065 client->packidx_fd = -1;
1066 packidx->len = sb.st_size;
1068 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1069 if (err)
1070 return err;
1072 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1073 if (ref_update->delete_ref)
1074 continue;
1076 err = got_object_id_str(&id_str, &ref_update->new_id);
1077 if (err)
1078 goto done;
1080 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1081 if (idx == -1) {
1082 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1083 "advertised object %s is missing from pack file",
1084 id_str);
1085 goto done;
1089 done:
1090 close_err = got_packidx_close(packidx);
1091 if (close_err && err == NULL)
1092 err = close_err;
1093 free(id_str);
1094 return err;
1097 static const struct got_error *
1098 install_packfile(struct gotd_imsgev *iev)
1100 struct repo_write_client *client = &repo_write_client;
1101 struct gotd_imsg_packfile_install inst;
1102 int ret;
1104 memset(&inst, 0, sizeof(inst));
1105 inst.client_id = client->id;
1106 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1108 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1109 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1110 if (ret == -1)
1111 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1113 return NULL;
1116 static const struct got_error *
1117 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1119 struct repo_write_client *client = &repo_write_client;
1120 struct gotd_imsg_ref_updates_start istart;
1121 int ret;
1123 memset(&istart, 0, sizeof(istart));
1124 istart.nref_updates = nref_updates;
1125 istart.client_id = client->id;
1127 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1128 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1129 if (ret == -1)
1130 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1132 return NULL;
1136 static const struct got_error *
1137 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1139 struct repo_write_client *client = &repo_write_client;
1140 struct gotd_imsg_ref_update iref;
1141 const char *refname = got_ref_get_name(ref_update->ref);
1142 struct ibuf *wbuf;
1143 size_t len;
1145 memset(&iref, 0, sizeof(iref));
1146 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1147 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1148 iref.ref_is_new = ref_update->ref_is_new;
1149 iref.delete_ref = ref_update->delete_ref;
1150 iref.client_id = client->id;
1151 iref.name_len = strlen(refname);
1153 len = sizeof(iref) + iref.name_len;
1154 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1155 repo_write.pid, len);
1156 if (wbuf == NULL)
1157 return got_error_from_errno("imsg_create REF_UPDATE");
1159 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1160 return got_error_from_errno("imsg_add REF_UPDATE");
1161 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1162 return got_error_from_errno("imsg_add REF_UPDATE");
1164 wbuf->fd = -1;
1165 imsg_close(&iev->ibuf, wbuf);
1167 gotd_imsg_event_add(iev);
1168 return NULL;
1171 static const struct got_error *
1172 update_refs(struct gotd_imsgev *iev)
1174 const struct got_error *err = NULL;
1175 struct repo_write_client *client = &repo_write_client;
1176 struct gotd_ref_update *ref_update;
1178 err = send_ref_updates_start(client->nref_updates, iev);
1179 if (err)
1180 return err;
1182 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1183 err = send_ref_update(ref_update, iev);
1184 if (err)
1185 goto done;
1187 done:
1188 return err;
1191 static const struct got_error *
1192 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1194 struct repo_write_client *client = &repo_write_client;
1195 struct gotd_imsg_packfile_pipe ireq;
1196 size_t datalen;
1198 log_debug("receving pack pipe descriptor");
1200 if (imsg->fd == -1)
1201 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1203 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1204 if (datalen != sizeof(ireq))
1205 return got_error(GOT_ERR_PRIVSEP_LEN);
1206 memcpy(&ireq, imsg->data, sizeof(ireq));
1208 if (client->pack_pipe != -1)
1209 return got_error(GOT_ERR_PRIVSEP_MSG);
1211 client->pack_pipe = imsg->fd;
1212 return NULL;
1215 static const struct got_error *
1216 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1218 struct repo_write_client *client = &repo_write_client;
1219 struct gotd_imsg_packidx_file ireq;
1220 size_t datalen;
1222 log_debug("receving pack index output file");
1224 if (imsg->fd == -1)
1225 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1227 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1228 if (datalen != sizeof(ireq))
1229 return got_error(GOT_ERR_PRIVSEP_LEN);
1230 memcpy(&ireq, imsg->data, sizeof(ireq));
1232 if (client->packidx_fd != -1)
1233 return got_error(GOT_ERR_PRIVSEP_MSG);
1235 client->packidx_fd = imsg->fd;
1236 return NULL;
1239 static void
1240 repo_write_dispatch_session(int fd, short event, void *arg)
1242 const struct got_error *err = NULL;
1243 struct gotd_imsgev *iev = arg;
1244 struct imsgbuf *ibuf = &iev->ibuf;
1245 struct imsg imsg;
1246 struct repo_write_client *client = &repo_write_client;
1247 ssize_t n;
1248 int shut = 0, have_packfile = 0;
1250 if (event & EV_READ) {
1251 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1252 fatal("imsg_read error");
1253 if (n == 0) /* Connection closed. */
1254 shut = 1;
1257 if (event & EV_WRITE) {
1258 n = msgbuf_write(&ibuf->w);
1259 if (n == -1 && errno != EAGAIN)
1260 fatal("msgbuf_write");
1261 if (n == 0) /* Connection closed. */
1262 shut = 1;
1265 for (;;) {
1266 if ((n = imsg_get(ibuf, &imsg)) == -1)
1267 fatal("%s: imsg_get error", __func__);
1268 if (n == 0) /* No more messages. */
1269 break;
1271 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1272 client->id == 0) {
1273 err = got_error(GOT_ERR_PRIVSEP_MSG);
1274 break;
1277 switch (imsg.hdr.type) {
1278 case GOTD_IMSG_LIST_REFS_INTERNAL:
1279 err = list_refs(&imsg);
1280 if (err)
1281 log_warnx("ls-refs: %s", err->msg);
1282 break;
1283 case GOTD_IMSG_REF_UPDATE:
1284 err = recv_ref_update(&imsg);
1285 if (err)
1286 log_warnx("ref-update: %s", err->msg);
1287 break;
1288 case GOTD_IMSG_PACKFILE_PIPE:
1289 err = receive_pack_pipe(&imsg, iev);
1290 if (err) {
1291 log_warnx("receiving pack pipe: %s", err->msg);
1292 break;
1294 break;
1295 case GOTD_IMSG_PACKIDX_FILE:
1296 err = receive_pack_idx(&imsg, iev);
1297 if (err) {
1298 log_warnx("receiving pack index: %s",
1299 err->msg);
1300 break;
1302 break;
1303 case GOTD_IMSG_RECV_PACKFILE:
1304 err = recv_packfile(&have_packfile, &imsg);
1305 if (err) {
1306 log_warnx("receive packfile: %s", err->msg);
1307 break;
1309 if (have_packfile) {
1310 err = verify_packfile();
1311 if (err) {
1312 log_warnx("verify packfile: %s",
1313 err->msg);
1314 break;
1316 err = install_packfile(iev);
1317 if (err) {
1318 log_warnx("install packfile: %s",
1319 err->msg);
1320 break;
1323 err = update_refs(iev);
1324 if (err) {
1325 log_warnx("update refs: %s", err->msg);
1327 break;
1328 default:
1329 log_debug("unexpected imsg %d", imsg.hdr.type);
1330 break;
1333 imsg_free(&imsg);
1336 if (!shut && check_cancelled(NULL) == NULL) {
1337 if (err &&
1338 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1339 client->id, err) == -1) {
1340 log_warnx("could not send error to parent: %s",
1341 err->msg);
1343 gotd_imsg_event_add(iev);
1344 } else {
1345 /* This pipe is dead. Remove its event handler */
1346 event_del(&iev->ev);
1347 event_loopexit(NULL);
1351 static const struct got_error *
1352 recv_connect(struct imsg *imsg)
1354 struct gotd_imsgev *iev = &repo_write.session_iev;
1355 size_t datalen;
1357 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1358 if (datalen != 0)
1359 return got_error(GOT_ERR_PRIVSEP_LEN);
1360 if (imsg->fd == -1)
1361 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1363 if (repo_write.session_fd != -1)
1364 return got_error(GOT_ERR_PRIVSEP_MSG);
1366 repo_write.session_fd = imsg->fd;
1368 imsg_init(&iev->ibuf, repo_write.session_fd);
1369 iev->handler = repo_write_dispatch_session;
1370 iev->events = EV_READ;
1371 iev->handler_arg = NULL;
1372 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1373 repo_write_dispatch_session, iev);
1374 gotd_imsg_event_add(iev);
1376 return NULL;
1379 static void
1380 repo_write_dispatch(int fd, short event, void *arg)
1382 const struct got_error *err = NULL;
1383 struct gotd_imsgev *iev = arg;
1384 struct imsgbuf *ibuf = &iev->ibuf;
1385 struct imsg imsg;
1386 ssize_t n;
1387 int shut = 0;
1388 struct repo_write_client *client = &repo_write_client;
1390 if (event & EV_READ) {
1391 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1392 fatal("imsg_read error");
1393 if (n == 0) /* Connection closed. */
1394 shut = 1;
1397 if (event & EV_WRITE) {
1398 n = msgbuf_write(&ibuf->w);
1399 if (n == -1 && errno != EAGAIN)
1400 fatal("msgbuf_write");
1401 if (n == 0) /* Connection closed. */
1402 shut = 1;
1405 while (err == NULL && check_cancelled(NULL) == NULL) {
1406 if ((n = imsg_get(ibuf, &imsg)) == -1)
1407 fatal("%s: imsg_get", __func__);
1408 if (n == 0) /* No more messages. */
1409 break;
1411 switch (imsg.hdr.type) {
1412 case GOTD_IMSG_CONNECT_REPO_CHILD:
1413 err = recv_connect(&imsg);
1414 break;
1415 default:
1416 log_debug("unexpected imsg %d", imsg.hdr.type);
1417 break;
1420 imsg_free(&imsg);
1423 if (!shut && check_cancelled(NULL) == NULL) {
1424 if (err &&
1425 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1426 client->id, err) == -1) {
1427 log_warnx("could not send error to parent: %s",
1428 err->msg);
1430 gotd_imsg_event_add(iev);
1431 } else {
1432 /* This pipe is dead. Remove its event handler */
1433 event_del(&iev->ev);
1434 event_loopexit(NULL);
1438 void
1439 repo_write_main(const char *title, const char *repo_path,
1440 int *pack_fds, int *temp_fds)
1442 const struct got_error *err = NULL;
1443 struct repo_write_client *client = &repo_write_client;
1444 struct gotd_imsgev iev;
1446 client->fd = -1;
1447 client->pack_pipe = -1;
1448 client->packidx_fd = -1;
1449 client->pack.fd = -1;
1451 repo_write.title = title;
1452 repo_write.pid = getpid();
1453 repo_write.pack_fds = pack_fds;
1454 repo_write.temp_fds = temp_fds;
1455 repo_write.session_fd = -1;
1456 repo_write.session_iev.ibuf.fd = -1;
1458 STAILQ_INIT(&repo_write_client.ref_updates);
1460 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1461 if (err)
1462 goto done;
1463 if (!got_repo_is_bare(repo_write.repo)) {
1464 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1465 "bare git repository required");
1466 goto done;
1469 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1471 signal(SIGINT, catch_sigint);
1472 signal(SIGTERM, catch_sigterm);
1473 signal(SIGPIPE, SIG_IGN);
1474 signal(SIGHUP, SIG_IGN);
1476 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1477 iev.handler = repo_write_dispatch;
1478 iev.events = EV_READ;
1479 iev.handler_arg = NULL;
1480 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1481 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1482 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1483 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1484 goto done;
1487 event_dispatch();
1488 done:
1489 if (err)
1490 log_warnx("%s: %s", title, err->msg);
1491 repo_write_shutdown();
1494 void
1495 repo_write_shutdown(void)
1497 struct repo_write_client *client = &repo_write_client;
1498 struct gotd_ref_update *ref_update;
1500 log_debug("shutting down");
1502 while (!STAILQ_EMPTY(&client->ref_updates)) {
1503 ref_update = STAILQ_FIRST(&client->ref_updates);
1504 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1505 got_ref_close(ref_update->ref);
1506 free(ref_update);
1509 got_pack_close(&client->pack);
1510 if (client->fd != -1)
1511 close(client->fd);
1512 if (client->pack_pipe != -1)
1513 close(client->pack_pipe);
1514 if (client->packidx_fd != -1)
1515 close(client->packidx_fd);
1517 if (repo_write.repo)
1518 got_repo_close(repo_write.repo);
1519 got_repo_pack_fds_close(repo_write.pack_fds);
1520 got_repo_temp_fds_close(repo_write.temp_fds);
1521 if (repo_write.session_fd != -1)
1522 close(repo_write.session_fd);
1523 exit(0);