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 <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"
44 #include "got_lib_delta.h"
45 #include "got_lib_delta_cache.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_ratelimit.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pack_index.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_poll.h"
54 #include "got_lib_sha1.h" /* XXX temp include for debugging */
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 } repo_write;
72 struct gotd_ref_update {
73 STAILQ_ENTRY(gotd_ref_update) entry;
74 struct got_reference *ref;
75 int ref_is_new;
76 struct got_object_id old_id;
77 struct got_object_id new_id;
78 };
79 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
81 struct repo_write_client {
82 STAILQ_ENTRY(repo_write_client) entry;
83 uint32_t id;
84 int fd;
85 int pack_pipe[2];
86 struct got_pack pack;
87 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
88 int packidx_fd;
89 struct gotd_ref_updates ref_updates;
90 int nref_updates;
91 };
92 STAILQ_HEAD(repo_write_clients, repo_write_client);
94 static struct repo_write_clients repo_write_clients[GOTD_CLIENT_TABLE_SIZE];
95 static SIPHASH_KEY clients_hash_key;
97 static uint64_t
98 client_hash(uint32_t client_id)
99 {
100 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
103 static void
104 add_client(struct repo_write_client *client, uint32_t client_id, int fd)
106 uint64_t slot;
108 client->id = client_id;
109 client->fd = fd;
110 client->pack_pipe[0] = -1;
111 client->pack_pipe[1] = -1;
112 client->packidx_fd = -1;
113 STAILQ_INIT(&client->ref_updates);
114 client->nref_updates = 0;
115 slot = client_hash(client->id) % nitems(repo_write_clients);
116 STAILQ_INSERT_HEAD(&repo_write_clients[slot], client, entry);
119 static struct repo_write_client *
120 find_client(uint32_t client_id)
122 uint64_t slot;
123 struct repo_write_client *c;
125 slot = client_hash(client_id) % nitems(repo_write_clients);
126 STAILQ_FOREACH(c, &repo_write_clients[slot], entry) {
127 if (c->id == client_id)
128 return c;
131 return NULL;
134 static volatile sig_atomic_t sigint_received;
135 static volatile sig_atomic_t sigterm_received;
137 static void
138 catch_sigint(int signo)
140 sigint_received = 1;
143 static void
144 catch_sigterm(int signo)
146 sigterm_received = 1;
149 static const struct got_error *
150 check_cancelled(void *arg)
152 if (sigint_received || sigterm_received)
153 return got_error(GOT_ERR_CANCELLED);
155 return NULL;
158 static const struct got_error *
159 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
160 struct imsgbuf *ibuf)
162 const struct got_error *err = NULL;
163 struct got_tag_object *tag;
164 size_t namelen, len;
165 char *peeled_refname = NULL;
166 struct got_object_id *id;
167 struct ibuf *wbuf;
169 err = got_object_tag_open(&tag, repo_write.repo, obj);
170 if (err)
171 return err;
173 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
174 err = got_error_from_errno("asprintf");
175 goto done;
178 id = got_object_tag_get_object_id(tag);
179 namelen = strlen(peeled_refname);
181 len = sizeof(struct gotd_imsg_ref) + namelen;
182 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
183 err = got_error(GOT_ERR_NO_SPACE);
184 goto done;
187 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
188 repo_write.pid, len);
189 if (wbuf == NULL) {
190 err = got_error_from_errno("imsg_create REF");
191 goto done;
194 /* Keep in sync with struct gotd_imsg_ref definition. */
195 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
196 err = got_error_from_errno("imsg_add REF");
197 goto done;
199 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
200 err = got_error_from_errno("imsg_add REF");
201 goto done;
203 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
204 err = got_error_from_errno("imsg_add REF");
205 goto done;
208 wbuf->fd = -1;
209 imsg_close(ibuf, wbuf);
210 done:
211 got_object_tag_close(tag);
212 return err;
215 static const struct got_error *
216 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
218 const struct got_error *err;
219 const char *refname = got_ref_get_name(ref);
220 size_t namelen;
221 struct got_object_id *id = NULL;
222 struct got_object *obj = NULL;
223 size_t len;
224 struct ibuf *wbuf;
226 namelen = strlen(refname);
228 len = sizeof(struct gotd_imsg_ref) + namelen;
229 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
230 return got_error(GOT_ERR_NO_SPACE);
232 err = got_ref_resolve(&id, repo_write.repo, ref);
233 if (err)
234 return err;
236 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
237 repo_write.pid, len);
238 if (wbuf == NULL) {
239 err = got_error_from_errno("imsg_create REF");
240 goto done;
243 /* Keep in sync with struct gotd_imsg_ref definition. */
244 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
245 return got_error_from_errno("imsg_add REF");
246 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
247 return got_error_from_errno("imsg_add REF");
248 if (imsg_add(wbuf, refname, namelen) == -1)
249 return got_error_from_errno("imsg_add REF");
251 wbuf->fd = -1;
252 imsg_close(ibuf, wbuf);
254 err = got_object_open(&obj, repo_write.repo, id);
255 if (err)
256 goto done;
257 if (obj->type == GOT_OBJ_TYPE_TAG)
258 err = send_peeled_tag_ref(ref, obj, ibuf);
259 done:
260 if (obj)
261 got_object_close(obj);
262 free(id);
263 return err;
266 static const struct got_error *
267 list_refs(struct repo_write_client **client, struct imsg *imsg)
269 const struct got_error *err;
270 struct got_reflist_head refs;
271 struct got_reflist_entry *re;
272 struct gotd_imsg_list_refs_internal ireq;
273 size_t datalen;
274 struct gotd_imsg_reflist irefs;
275 struct imsgbuf ibuf;
276 int client_fd = imsg->fd;
278 TAILQ_INIT(&refs);
280 if (client_fd == -1)
281 return got_error(GOT_ERR_PRIVSEP_NO_FD);
283 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
284 if (datalen != sizeof(ireq))
285 return got_error(GOT_ERR_PRIVSEP_LEN);
286 memcpy(&ireq, imsg->data, sizeof(ireq));
288 *client = find_client(ireq.client_id);
289 if (*client)
290 return got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
292 *client = calloc(1, sizeof(**client));
293 if (*client == NULL)
294 return got_error_from_errno("calloc");
295 add_client(*client, ireq.client_id, client_fd);
297 imsg_init(&ibuf, client_fd);
299 err = got_ref_list(&refs, repo_write.repo, "",
300 got_ref_cmp_by_name, NULL);
301 if (err)
302 return err;
304 memset(&irefs, 0, sizeof(irefs));
305 TAILQ_FOREACH(re, &refs, entry) {
306 struct got_object_id *id;
307 int obj_type;
309 if (got_ref_is_symbolic(re->ref))
310 continue;
312 irefs.nrefs++;
314 /* Account for a peeled tag refs. */
315 err = got_ref_resolve(&id, repo_write.repo, re->ref);
316 if (err)
317 goto done;
318 err = got_object_get_type(&obj_type, repo_write.repo, id);
319 free(id);
320 if (err)
321 goto done;
322 if (obj_type == GOT_OBJ_TYPE_TAG)
323 irefs.nrefs++;
326 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
327 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
328 err = got_error_from_errno("imsg_compose REFLIST");
329 goto done;
332 TAILQ_FOREACH(re, &refs, entry) {
333 if (got_ref_is_symbolic(re->ref))
334 continue;
335 err = send_ref(re->ref, &ibuf);
336 if (err)
337 goto done;
340 err = gotd_imsg_flush(&ibuf);
341 done:
342 got_ref_list_free(&refs);
343 imsg_clear(&ibuf);
344 return err;
347 static const struct got_error *
348 protect_ref_namespace(struct got_reference *ref, const char *namespace)
350 size_t len = strlen(namespace);
352 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
353 namespace[len -1] != '/') {
354 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
355 "reference namespace '%s'", namespace);
358 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
359 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
361 return NULL;
364 static const struct got_error *
365 recv_ref_update(struct repo_write_client **client, struct imsg *imsg)
367 const struct got_error *err = NULL;
368 struct gotd_imsg_ref_update iref;
369 size_t datalen;
370 char *refname = NULL;
371 struct got_reference *ref = NULL;
372 struct got_object_id *id = NULL;
373 struct imsgbuf ibuf;
374 struct gotd_ref_update *ref_update = NULL;
376 log_debug("ref-update received");
378 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
379 if (datalen < sizeof(iref))
380 return got_error(GOT_ERR_PRIVSEP_LEN);
381 memcpy(&iref, imsg->data, sizeof(iref));
382 if (datalen != sizeof(iref) + iref.name_len)
383 return got_error(GOT_ERR_PRIVSEP_LEN);
385 *client = find_client(iref.client_id);
386 if (*client == NULL)
387 return got_error(GOT_ERR_CLIENT_ID);
389 imsg_init(&ibuf, (*client)->fd);
391 refname = malloc(iref.name_len + 1);
392 if (refname == NULL)
393 return got_error_from_errno("malloc");
394 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
395 refname[iref.name_len] = '\0';
397 ref_update = calloc(1, sizeof(*ref_update));
398 if (ref_update == NULL) {
399 err = got_error_from_errno("malloc");
400 goto done;
403 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
404 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
406 err = got_ref_open(&ref, repo_write.repo, refname, 0);
407 if (err) {
408 if (err->code != GOT_ERR_NOT_REF)
409 goto done;
410 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
411 if (err)
412 goto done;
413 ref_update->ref_is_new = 1;
415 if (got_ref_is_symbolic(ref)) {
416 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
417 "'%s' is a symbolic reference and cannot "
418 "be updated", got_ref_get_name(ref));
419 goto done;
421 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
422 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
423 "%s: does not begin with 'refs/'",
424 got_ref_get_name(ref));
425 goto done;
428 err = protect_ref_namespace(ref, "refs/got/");
429 if (err)
430 goto done;
431 err = protect_ref_namespace(ref, "refs/remotes/");
432 if (err)
433 goto done;
435 if (!ref_update->ref_is_new) {
436 /*
437 * Ensure the client's idea of this update is still valid.
438 * At this point we can only return an error, to prevent
439 * the client from uploading a pack file which will likely
440 * have to be discarded.
441 */
442 err = got_ref_resolve(&id, repo_write.repo, ref);
443 if (err)
444 goto done;
446 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
447 err = got_error_fmt(GOT_ERR_REF_BUSY,
448 "%s has been modified by someone else "
449 "while transaction was in progress",
450 got_ref_get_name(ref));
451 goto done;
455 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
456 repo_write.pid);
458 ref_update->ref = ref;
459 STAILQ_INSERT_HEAD(&(*client)->ref_updates, ref_update, entry);
460 (*client)->nref_updates++;
461 ref = NULL;
462 ref_update = NULL;
463 done:
464 if (ref)
465 got_ref_close(ref);
466 free(ref_update);
467 free(refname);
468 free(id);
469 return err;
472 static const struct got_error *
473 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
474 uint32_t nobj_loose, uint32_t nobj_resolved)
476 int p_indexed = 0, p_resolved = 0;
477 int nobj_delta = nobj_total - nobj_loose;
479 if (nobj_total > 0)
480 p_indexed = (nobj_indexed * 100) / nobj_total;
482 if (nobj_delta > 0)
483 p_resolved = (nobj_resolved * 100) / nobj_delta;
485 if (p_resolved > 0) {
486 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
487 nobj_total, p_indexed, nobj_delta, p_resolved);
488 } else
489 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
491 return NULL;
494 static const struct got_error *
495 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
497 const struct got_error *err = NULL;
498 uint8_t readahead[65536];
499 size_t have, newlen;
501 err = got_poll_read_full(infd, &have,
502 readahead, sizeof(readahead), minsize);
503 if (err)
504 return err;
506 err = buf_append(&newlen, buf, readahead, have);
507 if (err)
508 return err;
509 return NULL;
512 static const struct got_error *
513 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
514 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
516 const struct got_error *err = NULL;
517 uint8_t t = 0;
518 uint64_t s = 0;
519 uint8_t sizebuf[8];
520 size_t i = 0;
521 off_t obj_offset = *outsize;
523 do {
524 /* We do not support size values which don't fit in 64 bit. */
525 if (i > 9)
526 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
527 "packfile offset %llu", obj_offset);
529 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
530 err = read_more_pack_stream(infd, buf,
531 sizeof(sizebuf[0]));
532 if (err)
533 return err;
536 sizebuf[i] = buf_getc(buf, *buf_pos);
537 *buf_pos += sizeof(sizebuf[i]);
539 if (i == 0) {
540 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
541 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
542 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
543 } else {
544 size_t shift = 4 + 7 * (i - 1);
545 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
546 shift);
548 i++;
549 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
551 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
552 if (err)
553 return err;
554 *outsize += i;
556 *type = t;
557 *size = s;
558 return NULL;
561 static const struct got_error *
562 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
563 SHA1_CTX *ctx)
565 const struct got_error *err = NULL;
566 size_t remain = buf_len(buf) - *buf_pos;
568 if (remain < SHA1_DIGEST_LENGTH) {
569 err = read_more_pack_stream(infd, buf,
570 SHA1_DIGEST_LENGTH - remain);
571 if (err)
572 return err;
575 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
576 SHA1_DIGEST_LENGTH, ctx);
577 if (err)
578 return err;
580 *buf_pos += SHA1_DIGEST_LENGTH;
581 return NULL;
584 static const struct got_error *
585 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
586 SHA1_CTX *ctx)
588 const struct got_error *err = NULL;
589 uint64_t o = 0;
590 uint8_t offbuf[8];
591 size_t i = 0;
592 off_t obj_offset = *outsize;
594 do {
595 /* We do not support offset values which don't fit in 64 bit. */
596 if (i > 8)
597 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
598 "packfile offset %llu", obj_offset);
600 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
601 err = read_more_pack_stream(infd, buf,
602 sizeof(offbuf[0]));
603 if (err)
604 return err;
607 offbuf[i] = buf_getc(buf, *buf_pos);
608 *buf_pos += sizeof(offbuf[i]);
610 if (i == 0)
611 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
612 else {
613 o++;
614 o <<= 7;
615 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
617 i++;
618 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
620 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
621 return got_error(GOT_ERR_PACK_OFFSET);
623 err = got_pack_hwrite(outfd, offbuf, i, ctx);
624 if (err)
625 return err;
627 *outsize += i;
628 return NULL;
631 static const struct got_error *
632 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
633 SHA1_CTX *ctx)
635 const struct got_error *err = NULL;
636 z_stream z;
637 int zret;
638 char voidbuf[1024];
639 size_t consumed_total = 0;
640 off_t zstream_offset = *outsize;
642 memset(&z, 0, sizeof(z));
644 z.zalloc = Z_NULL;
645 z.zfree = Z_NULL;
646 zret = inflateInit(&z);
647 if (zret != Z_OK) {
648 if (zret == Z_ERRNO)
649 return got_error_from_errno("inflateInit");
650 if (zret == Z_MEM_ERROR) {
651 errno = ENOMEM;
652 return got_error_from_errno("inflateInit");
654 return got_error_msg(GOT_ERR_DECOMPRESSION,
655 "inflateInit failed");
658 while (zret != Z_STREAM_END) {
659 size_t last_total_in, consumed;
661 /*
662 * Decompress into the void. Object data will be parsed
663 * later, when the pack file is indexed. For now, we just
664 * want to locate the end of the compressed stream.
665 */
666 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
667 last_total_in = z.total_in;
668 z.next_in = buf_get(buf) + *buf_pos;
669 z.avail_in = buf_len(buf) - *buf_pos;
670 z.next_out = voidbuf;
671 z.avail_out = sizeof(voidbuf);
673 zret = inflate(&z, Z_SYNC_FLUSH);
674 if (zret != Z_OK && zret != Z_BUF_ERROR &&
675 zret != Z_STREAM_END) {
676 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
677 "packfile offset %llu", zstream_offset);
678 goto done;
680 consumed = z.total_in - last_total_in;
682 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
683 consumed, ctx);
684 if (err)
685 goto done;
687 err = buf_discard(buf, *buf_pos + consumed);
688 if (err)
689 goto done;
690 *buf_pos = 0;
692 consumed_total += consumed;
695 if (zret != Z_STREAM_END) {
696 err = read_more_pack_stream(infd, buf, 1);
697 if (err)
698 goto done;
702 if (err == NULL)
703 *outsize += consumed_total;
704 done:
705 inflateEnd(&z);
706 return err;
709 static const struct got_error *
710 validate_object_type(int obj_type)
712 switch (obj_type) {
713 case GOT_OBJ_TYPE_BLOB:
714 case GOT_OBJ_TYPE_COMMIT:
715 case GOT_OBJ_TYPE_TREE:
716 case GOT_OBJ_TYPE_TAG:
717 case GOT_OBJ_TYPE_REF_DELTA:
718 case GOT_OBJ_TYPE_OFFSET_DELTA:
719 return NULL;
720 default:
721 break;
724 return got_error(GOT_ERR_OBJ_TYPE);
727 static const struct got_error *
728 recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
730 const struct got_error *err;
731 struct got_packfile_hdr hdr;
732 size_t have;
733 uint32_t nobj, nhave = 0;
734 SHA1_CTX ctx;
735 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
736 char hex[SHA1_DIGEST_STRING_LENGTH];
737 BUF *buf = NULL;
738 size_t buf_pos = 0, remain;
739 ssize_t w;
741 *outsize = 0;
742 SHA1Init(&ctx);
744 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
745 if (err)
746 return err;
747 if (have != sizeof(hdr))
748 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
749 *outsize += have;
751 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
752 return got_error_msg(GOT_ERR_BAD_PACKFILE,
753 "bad packfile signature");
754 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
755 return got_error_msg(GOT_ERR_BAD_PACKFILE,
756 "bad packfile version");
758 nobj = be32toh(hdr.nobjects);
759 if (nobj == 0)
760 return got_error_msg(GOT_ERR_BAD_PACKFILE,
761 "bad packfile with zero objects");
763 log_debug("expecting %d objects", nobj);
765 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
766 if (err)
767 return err;
769 err = buf_alloc(&buf, 65536);
770 if (err)
771 return err;
773 while (nhave != nobj) {
774 uint8_t obj_type;
775 uint64_t obj_size;
777 err = copy_object_type_and_size(&obj_type, &obj_size,
778 infd, outfd, outsize, buf, &buf_pos, &ctx);
779 if (err)
780 goto done;
782 err = validate_object_type(obj_type);
783 if (err)
784 goto done;
786 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
787 err = copy_ref_delta(infd, outfd, outsize,
788 buf, &buf_pos, &ctx);
789 if (err)
790 goto done;
791 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
792 err = copy_offset_delta(infd, outfd, outsize,
793 buf, &buf_pos, &ctx);
794 if (err)
795 goto done;
798 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
799 if (err)
800 goto done;
802 nhave++;
805 log_debug("received %u objects", nobj);
807 SHA1Final(expected_sha1, &ctx);
809 remain = buf_len(buf) - buf_pos;
810 if (remain < SHA1_DIGEST_LENGTH) {
811 err = read_more_pack_stream(infd, buf,
812 SHA1_DIGEST_LENGTH - remain);
813 if (err)
814 return err;
817 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
818 log_debug("expect SHA1: %s", hex);
819 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
820 log_debug("actual SHA1: %s", hex);
822 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
823 SHA1_DIGEST_LENGTH) != 0) {
824 err = got_error(GOT_ERR_PACKFILE_CSUM);
825 goto done;
828 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
830 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
831 if (w == -1) {
832 err = got_error_from_errno("write");
833 goto done;
835 if (w != SHA1_DIGEST_LENGTH) {
836 err = got_error(GOT_ERR_IO);
837 goto done;
840 *outsize += SHA1_DIGEST_LENGTH;
842 if (fsync(outfd) == -1) {
843 err = got_error_from_errno("fsync");
844 goto done;
846 if (lseek(outfd, 0L, SEEK_SET) == -1) {
847 err = got_error_from_errno("lseek");
848 goto done;
850 done:
851 buf_free(buf);
852 return err;
855 static const struct got_error *
856 report_pack_status(struct repo_write_client *client,
857 const struct got_error *unpack_err)
859 const struct got_error *err = NULL;
860 struct gotd_imsg_packfile_status istatus;
861 struct ibuf *wbuf;
862 struct imsgbuf ibuf;
863 const char *unpack_ok = "unpack ok\n";
864 size_t len;
866 imsg_init(&ibuf, client->fd);
868 if (unpack_err)
869 istatus.reason_len = strlen(unpack_err->msg);
870 else
871 istatus.reason_len = strlen(unpack_ok);
873 len = sizeof(istatus) + istatus.reason_len;
874 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
875 repo_write.pid, len);
876 if (wbuf == NULL) {
877 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
878 goto done;
881 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
882 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
883 goto done;
886 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
887 istatus.reason_len) == -1) {
888 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
889 goto done;
892 wbuf->fd = -1;
893 imsg_close(&ibuf, wbuf);
895 err = gotd_imsg_flush(&ibuf);
896 done:
897 imsg_clear(&ibuf);
898 return err;
901 static const struct got_error *
902 recv_packfile(struct repo_write_client **client, struct imsg *imsg)
904 const struct got_error *err = NULL, *unpack_err;
905 struct gotd_imsg_recv_packfile ireq;
906 FILE *tempfiles[3] = { NULL, NULL, NULL };
907 struct repo_tempfile {
908 int fd;
909 int idx;
910 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
911 int i;
912 size_t datalen;
913 struct imsgbuf ibuf;
914 struct got_ratelimit rl;
915 struct got_pack *pack = NULL;
916 off_t pack_filesize = 0;
918 log_debug("packfile request received");
920 got_ratelimit_init(&rl, 2, 0);
922 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
923 if (datalen != sizeof(ireq))
924 return got_error(GOT_ERR_PRIVSEP_LEN);
925 memcpy(&ireq, imsg->data, sizeof(ireq));
927 *client = find_client(ireq.client_id);
928 if (*client == NULL || STAILQ_EMPTY(&(*client)->ref_updates))
929 return got_error(GOT_ERR_CLIENT_ID);
931 if ((*client)->pack_pipe[0] == -1 ||
932 (*client)->pack_pipe[1] == -1 ||
933 (*client)->packidx_fd == -1)
934 return got_error(GOT_ERR_PRIVSEP_NO_FD);
936 imsg_init(&ibuf, (*client)->fd);
938 if (imsg->fd == -1)
939 return got_error(GOT_ERR_PRIVSEP_NO_FD);
941 pack = &(*client)->pack;
942 memset(pack, 0, sizeof(*pack));
943 pack->fd = imsg->fd;
944 err = got_delta_cache_alloc(&pack->delta_cache);
945 if (err)
946 return err;
948 for (i = 0; i < nitems(repo_tempfiles); i++) {
949 struct repo_tempfile *t = &repo_tempfiles[i];
950 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
951 if (err)
952 goto done;
955 for (i = 0; i < nitems(tempfiles); i++) {
956 int fd = dup(repo_tempfiles[i].fd);
957 FILE *f;
958 if (fd == -1) {
959 err = got_error_from_errno("dup");
960 goto done;
962 f = fdopen(fd, "w+");
963 if (f == NULL) {
964 err = got_error_from_errno("dup");
965 close(fd);
966 goto done;
968 tempfiles[i] = f;
971 /* Send pack file pipe to gotsh(1). */
972 if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
973 repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
974 (*client)->pack_pipe[1] = -1;
975 err = got_error_from_errno("imsg_compose ACK");
976 if (err)
977 goto done;
979 (*client)->pack_pipe[1] = -1;
980 err = gotd_imsg_flush(&ibuf);
981 if (err)
982 goto done;
984 log_debug("receiving pack data");
985 unpack_err = recv_packdata(&pack_filesize, (*client)->pack_sha1,
986 (*client)->pack_pipe[0], pack->fd);
987 if (ireq.report_status) {
988 err = report_pack_status(*client, unpack_err);
989 if (err) {
990 /* Git clients hang up after sending the pack file. */
991 if (err->code == GOT_ERR_EOF)
992 err = NULL;
995 if (unpack_err)
996 err = unpack_err;
997 if (err)
998 goto done;
1000 log_debug("pack data received");
1002 /* XXX size_t vs off_t, both should be off_t */
1003 if (pack_filesize >= SIZE_MAX) {
1004 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
1005 "pack file too large");
1006 goto done;
1008 pack->filesize = pack_filesize;
1010 log_debug("begin indexing pack (%zu bytes in size)", pack->filesize);
1011 err = got_pack_index(pack, (*client)->packidx_fd,
1012 tempfiles[0], tempfiles[1], tempfiles[2], (*client)->pack_sha1,
1013 pack_index_progress, NULL, &rl);
1014 if (err)
1015 goto done;
1016 log_debug("done indexing pack");
1018 if (fsync((*client)->packidx_fd) == -1) {
1019 err = got_error_from_errno("fsync");
1020 goto done;
1022 if (lseek((*client)->packidx_fd, 0L, SEEK_SET) == -1)
1023 err = got_error_from_errno("lseek");
1024 done:
1025 if (close((*client)->pack_pipe[0]) == -1 && err == NULL)
1026 err = got_error_from_errno("close");
1027 (*client)->pack_pipe[0] = -1;
1028 for (i = 0; i < nitems(repo_tempfiles); i++) {
1029 struct repo_tempfile *t = &repo_tempfiles[i];
1030 if (t->idx != -1)
1031 got_repo_temp_fds_put(t->idx, repo_write.repo);
1033 for (i = 0; i < nitems(tempfiles); i++) {
1034 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1035 err = got_error_from_errno("fclose");
1037 if (err)
1038 got_pack_close(pack);
1039 imsg_clear(&ibuf);
1040 return err;
1043 static const struct got_error *
1044 verify_packfile(struct repo_write_client *client)
1046 const struct got_error *err = NULL, *close_err;
1047 struct gotd_ref_update *ref_update;
1048 struct got_packidx *packidx = NULL;
1049 struct stat sb;
1050 char *id_str = NULL;
1051 int idx = -1;
1053 if (STAILQ_EMPTY(&client->ref_updates)) {
1054 return got_error_msg(GOT_ERR_BAD_REQUEST,
1055 "cannot verify pack file without any ref-updates");
1058 if (client->pack.fd == -1) {
1059 return got_error_msg(GOT_ERR_BAD_REQUEST,
1060 "invalid pack file handle during pack verification");
1062 if (client->packidx_fd == -1) {
1063 return got_error_msg(GOT_ERR_BAD_REQUEST,
1064 "invalid pack index handle during pack verification");
1067 if (fstat(client->packidx_fd, &sb) == -1)
1068 return got_error_from_errno("pack index fstat");
1070 packidx = malloc(sizeof(*packidx));
1071 memset(packidx, 0, sizeof(*packidx));
1072 packidx->fd = client->packidx_fd;
1073 client->packidx_fd = -1;
1074 packidx->len = sb.st_size;
1076 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1077 if (err)
1078 return err;
1080 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1081 err = got_object_id_str(&id_str, &ref_update->new_id);
1082 if (err)
1083 goto done;
1085 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1086 if (idx == -1) {
1087 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1088 "advertised object %s is missing from pack file",
1089 id_str);
1090 goto done;
1094 done:
1095 close_err = got_packidx_close(packidx);
1096 if (close_err && err == NULL)
1097 err = close_err;
1098 free(id_str);
1099 return err;
1102 static const struct got_error *
1103 install_packfile(struct repo_write_client *client, struct gotd_imsgev *iev)
1105 struct gotd_imsg_packfile_install inst;
1106 int ret;
1108 memset(&inst, 0, sizeof(inst));
1109 inst.client_id = client->id;
1110 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1112 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1113 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1114 if (ret == -1)
1115 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1117 return NULL;
1120 static const struct got_error *
1121 send_ref_updates_start(struct repo_write_client *client, int nref_updates,
1122 struct gotd_imsgev *iev)
1124 struct gotd_imsg_ref_updates_start istart;
1125 int ret;
1127 memset(&istart, 0, sizeof(istart));
1128 istart.nref_updates = nref_updates;
1129 istart.client_id = client->id;
1131 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1132 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1133 if (ret == -1)
1134 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1136 return NULL;
1140 static const struct got_error *
1141 send_ref_update(struct repo_write_client *client,
1142 struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1144 struct gotd_imsg_ref_update iref;
1145 const char *refname = got_ref_get_name(ref_update->ref);
1146 struct ibuf *wbuf;
1147 size_t len;
1149 memset(&iref, 0, sizeof(iref));
1150 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1151 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1152 iref.ref_is_new = ref_update->ref_is_new;
1153 iref.client_id = client->id;
1154 iref.name_len = strlen(refname);
1156 len = sizeof(iref) + iref.name_len;
1157 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1158 repo_write.pid, len);
1159 if (wbuf == NULL)
1160 return got_error_from_errno("imsg_create REF_UPDATE");
1162 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1163 return got_error_from_errno("imsg_add REF_UPDATE");
1164 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1165 return got_error_from_errno("imsg_add REF_UPDATE");
1167 wbuf->fd = -1;
1168 imsg_close(&iev->ibuf, wbuf);
1170 gotd_imsg_event_add(iev);
1171 return NULL;
1174 static const struct got_error *
1175 update_refs(struct repo_write_client *client, struct gotd_imsgev *iev)
1177 const struct got_error *err = NULL;
1178 struct gotd_ref_update *ref_update;
1180 err = send_ref_updates_start(client, client->nref_updates, iev);
1181 if (err)
1182 return err;
1184 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1185 err = send_ref_update(client, ref_update, iev);
1186 if (err)
1187 goto done;
1189 done:
1190 return err;
1193 static const struct got_error *
1194 recv_disconnect(struct imsg *imsg)
1196 const struct got_error *err = NULL;
1197 struct gotd_imsg_disconnect idisconnect;
1198 size_t datalen;
1199 int client_fd = -1, pipe0 = -1, pipe1 = - 1, idxfd = -1;
1200 struct repo_write_client *client = NULL;
1201 uint64_t slot;
1203 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1204 if (datalen != sizeof(idisconnect))
1205 return got_error(GOT_ERR_PRIVSEP_LEN);
1206 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1208 log_debug("client disconnecting");
1210 client = find_client(idisconnect.client_id);
1211 if (client == NULL)
1212 return got_error(GOT_ERR_CLIENT_ID);
1214 slot = client_hash(client->id) % nitems(repo_write_clients);
1215 STAILQ_REMOVE(&repo_write_clients[slot], client, repo_write_client,
1216 entry);
1217 while (!STAILQ_EMPTY(&client->ref_updates)) {
1218 struct gotd_ref_update *ref_update;
1219 ref_update = STAILQ_FIRST(&client->ref_updates);
1220 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1221 got_ref_close(ref_update->ref);
1222 free(ref_update);
1224 err = got_pack_close(&client->pack);
1225 client_fd = client->fd;
1226 pipe0 = client->pack_pipe[0];
1227 pipe1 = client->pack_pipe[1];
1228 idxfd = client->packidx_fd;
1229 free(client);
1230 if (client_fd != -1 && close(client_fd) == -1)
1231 err = got_error_from_errno("close");
1232 if (pipe0 != -1 && close(pipe0) == -1 && err == NULL)
1233 err = got_error_from_errno("close");
1234 if (pipe1 != -1 && close(pipe1) == -1 && err == NULL)
1235 err = got_error_from_errno("close");
1236 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1237 err = got_error_from_errno("close");
1238 return err;
1241 static const struct got_error *
1242 receive_pack_pipe(struct repo_write_client **client, struct imsg *imsg,
1243 struct gotd_imsgev *iev)
1245 struct gotd_imsg_packfile_pipe ireq;
1246 size_t datalen;
1248 log_debug("receving pack pipe descriptor");
1250 if (imsg->fd == -1)
1251 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1253 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1254 if (datalen != sizeof(ireq))
1255 return got_error(GOT_ERR_PRIVSEP_LEN);
1256 memcpy(&ireq, imsg->data, sizeof(ireq));
1258 *client = find_client(ireq.client_id);
1259 if (*client == NULL)
1260 return got_error(GOT_ERR_CLIENT_ID);
1261 if ((*client)->pack_pipe[1] != -1)
1262 return got_error(GOT_ERR_PRIVSEP_MSG);
1264 if ((*client)->pack_pipe[0] == -1)
1265 (*client)->pack_pipe[0] = imsg->fd;
1266 else
1267 (*client)->pack_pipe[1] = imsg->fd;
1269 return NULL;
1272 static const struct got_error *
1273 receive_pack_idx(struct repo_write_client **client, struct imsg *imsg,
1274 struct gotd_imsgev *iev)
1276 struct gotd_imsg_packidx_file ireq;
1277 size_t datalen;
1279 log_debug("receving pack index output file");
1281 if (imsg->fd == -1)
1282 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1284 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1285 if (datalen != sizeof(ireq))
1286 return got_error(GOT_ERR_PRIVSEP_LEN);
1287 memcpy(&ireq, imsg->data, sizeof(ireq));
1289 *client = find_client(ireq.client_id);
1290 if (*client == NULL)
1291 return got_error(GOT_ERR_CLIENT_ID);
1292 if ((*client)->packidx_fd != -1)
1293 return got_error(GOT_ERR_PRIVSEP_MSG);
1295 (*client)->packidx_fd = imsg->fd;
1296 return NULL;
1299 static void
1300 repo_write_dispatch(int fd, short event, void *arg)
1302 const struct got_error *err = NULL;
1303 struct gotd_imsgev *iev = arg;
1304 struct imsgbuf *ibuf = &iev->ibuf;
1305 struct imsg imsg;
1306 struct repo_write_client *client = NULL;
1307 ssize_t n;
1308 int shut = 0;
1310 if (event & EV_READ) {
1311 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1312 fatal("imsg_read error");
1313 if (n == 0) /* Connection closed. */
1314 shut = 1;
1317 if (event & EV_WRITE) {
1318 n = msgbuf_write(&ibuf->w);
1319 if (n == -1 && errno != EAGAIN)
1320 fatal("msgbuf_write");
1321 if (n == 0) /* Connection closed. */
1322 shut = 1;
1325 for (;;) {
1326 if ((n = imsg_get(ibuf, &imsg)) == -1)
1327 fatal("%s: imsg_get error", __func__);
1328 if (n == 0) /* No more messages. */
1329 break;
1331 switch (imsg.hdr.type) {
1332 case GOTD_IMSG_LIST_REFS_INTERNAL:
1333 err = list_refs(&client, &imsg);
1334 if (err)
1335 log_warnx("%s: ls-refs: %s", repo_write.title,
1336 err->msg);
1337 break;
1338 case GOTD_IMSG_REF_UPDATE:
1339 err = recv_ref_update(&client, &imsg);
1340 if (err)
1341 log_warnx("%s: ref-update: %s",
1342 repo_write.title, err->msg);
1343 break;
1344 case GOTD_IMSG_PACKFILE_PIPE:
1345 err = receive_pack_pipe(&client, &imsg, iev);
1346 if (err) {
1347 log_warnx("%s: receiving pack pipe: %s",
1348 repo_write.title, err->msg);
1349 break;
1351 break;
1352 case GOTD_IMSG_PACKIDX_FILE:
1353 err = receive_pack_idx(&client, &imsg, iev);
1354 if (err) {
1355 log_warnx("%s: receiving pack index: %s",
1356 repo_write.title, err->msg);
1357 break;
1359 break;
1360 case GOTD_IMSG_RECV_PACKFILE:
1361 err = recv_packfile(&client, &imsg);
1362 if (err) {
1363 log_warnx("%s: receive packfile: %s",
1364 repo_write.title, err->msg);
1365 break;
1367 err = verify_packfile(client);
1368 if (err) {
1369 log_warnx("%s: verify packfile: %s",
1370 repo_write.title, err->msg);
1371 break;
1373 err = install_packfile(client, iev);
1374 if (err) {
1375 log_warnx("%s: install packfile: %s",
1376 repo_write.title, err->msg);
1377 break;
1379 err = update_refs(client, iev);
1380 if (err) {
1381 log_warnx("%s: update refs: %s",
1382 repo_write.title, err->msg);
1384 break;
1385 case GOTD_IMSG_DISCONNECT:
1386 err = recv_disconnect(&imsg);
1387 if (err)
1388 log_warnx("%s: disconnect: %s",
1389 repo_write.title, err->msg);
1390 break;
1391 default:
1392 log_debug("%s: unexpected imsg %d", repo_write.title,
1393 imsg.hdr.type);
1394 break;
1397 imsg_free(&imsg);
1400 if (!shut && check_cancelled(NULL) == NULL) {
1401 if (err &&
1402 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1403 client ? client->id : 0, err) == -1) {
1404 log_warnx("could not send error to parent: %s",
1405 err->msg);
1407 gotd_imsg_event_add(iev);
1408 } else {
1409 /* This pipe is dead. Remove its event handler */
1410 event_del(&iev->ev);
1411 event_loopexit(NULL);
1415 void
1416 repo_write_main(const char *title, int *pack_fds, int *temp_fds)
1418 const struct got_error *err = NULL;
1419 struct gotd_imsgev iev;
1421 repo_write.title = title;
1422 repo_write.pid = getpid();
1423 repo_write.pack_fds = pack_fds;
1424 repo_write.temp_fds = temp_fds;
1426 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1429 * Open a repository in the root directory.
1430 * We are already in chroot at this point.
1432 err = got_repo_open(&repo_write.repo, "/", NULL, pack_fds);
1433 if (err)
1434 goto done;
1435 if (!got_repo_is_bare(repo_write.repo)) {
1436 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1437 "bare git repository required");
1438 goto done;
1441 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1443 signal(SIGINT, catch_sigint);
1444 signal(SIGTERM, catch_sigterm);
1445 signal(SIGPIPE, SIG_IGN);
1446 signal(SIGHUP, SIG_IGN);
1448 imsg_init(&iev.ibuf, GOTD_SOCK_FILENO);
1449 iev.handler = repo_write_dispatch;
1450 iev.events = EV_READ;
1451 iev.handler_arg = NULL;
1452 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1453 if (event_add(&iev.ev, NULL) == -1) {
1454 err = got_error_from_errno("event_add");
1455 goto done;
1458 event_dispatch();
1459 done:
1460 if (err)
1461 log_warnx("%s: %s", title, err->msg);
1462 repo_write_shutdown();
1465 void
1466 repo_write_shutdown(void)
1468 log_debug("%s: shutting down", repo_write.title);
1469 if (repo_write.repo)
1470 got_repo_close(repo_write.repo);
1471 got_repo_pack_fds_close(repo_write.pack_fds);
1472 got_repo_temp_fds_close(repo_write.pack_fds);
1473 exit(0);