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 %lld", (long long)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 %lld", (long long)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 %lld",
678 (long long)zstream_offset);
679 goto done;
681 consumed = z.total_in - last_total_in;
683 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
684 consumed, ctx);
685 if (err)
686 goto done;
688 err = buf_discard(buf, *buf_pos + consumed);
689 if (err)
690 goto done;
691 *buf_pos = 0;
693 consumed_total += consumed;
696 if (zret != Z_STREAM_END) {
697 err = read_more_pack_stream(infd, buf, 1);
698 if (err)
699 goto done;
703 if (err == NULL)
704 *outsize += consumed_total;
705 done:
706 inflateEnd(&z);
707 return err;
710 static const struct got_error *
711 validate_object_type(int obj_type)
713 switch (obj_type) {
714 case GOT_OBJ_TYPE_BLOB:
715 case GOT_OBJ_TYPE_COMMIT:
716 case GOT_OBJ_TYPE_TREE:
717 case GOT_OBJ_TYPE_TAG:
718 case GOT_OBJ_TYPE_REF_DELTA:
719 case GOT_OBJ_TYPE_OFFSET_DELTA:
720 return NULL;
721 default:
722 break;
725 return got_error(GOT_ERR_OBJ_TYPE);
728 static const struct got_error *
729 recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
731 const struct got_error *err;
732 struct got_packfile_hdr hdr;
733 size_t have;
734 uint32_t nobj, nhave = 0;
735 SHA1_CTX ctx;
736 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
737 char hex[SHA1_DIGEST_STRING_LENGTH];
738 BUF *buf = NULL;
739 size_t buf_pos = 0, remain;
740 ssize_t w;
742 *outsize = 0;
743 SHA1Init(&ctx);
745 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
746 if (err)
747 return err;
748 if (have != sizeof(hdr))
749 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
750 *outsize += have;
752 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
753 return got_error_msg(GOT_ERR_BAD_PACKFILE,
754 "bad packfile signature");
755 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
756 return got_error_msg(GOT_ERR_BAD_PACKFILE,
757 "bad packfile version");
759 nobj = be32toh(hdr.nobjects);
760 if (nobj == 0)
761 return got_error_msg(GOT_ERR_BAD_PACKFILE,
762 "bad packfile with zero objects");
764 log_debug("expecting %d objects", nobj);
766 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
767 if (err)
768 return err;
770 err = buf_alloc(&buf, 65536);
771 if (err)
772 return err;
774 while (nhave != nobj) {
775 uint8_t obj_type;
776 uint64_t obj_size;
778 err = copy_object_type_and_size(&obj_type, &obj_size,
779 infd, outfd, outsize, buf, &buf_pos, &ctx);
780 if (err)
781 goto done;
783 err = validate_object_type(obj_type);
784 if (err)
785 goto done;
787 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
788 err = copy_ref_delta(infd, outfd, outsize,
789 buf, &buf_pos, &ctx);
790 if (err)
791 goto done;
792 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
793 err = copy_offset_delta(infd, outfd, outsize,
794 buf, &buf_pos, &ctx);
795 if (err)
796 goto done;
799 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
800 if (err)
801 goto done;
803 nhave++;
806 log_debug("received %u objects", nobj);
808 SHA1Final(expected_sha1, &ctx);
810 remain = buf_len(buf) - buf_pos;
811 if (remain < SHA1_DIGEST_LENGTH) {
812 err = read_more_pack_stream(infd, buf,
813 SHA1_DIGEST_LENGTH - remain);
814 if (err)
815 return err;
818 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
819 log_debug("expect SHA1: %s", hex);
820 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
821 log_debug("actual SHA1: %s", hex);
823 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
824 SHA1_DIGEST_LENGTH) != 0) {
825 err = got_error(GOT_ERR_PACKFILE_CSUM);
826 goto done;
829 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
831 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
832 if (w == -1) {
833 err = got_error_from_errno("write");
834 goto done;
836 if (w != SHA1_DIGEST_LENGTH) {
837 err = got_error(GOT_ERR_IO);
838 goto done;
841 *outsize += SHA1_DIGEST_LENGTH;
843 if (fsync(outfd) == -1) {
844 err = got_error_from_errno("fsync");
845 goto done;
847 if (lseek(outfd, 0L, SEEK_SET) == -1) {
848 err = got_error_from_errno("lseek");
849 goto done;
851 done:
852 buf_free(buf);
853 return err;
856 static const struct got_error *
857 report_pack_status(struct repo_write_client *client,
858 const struct got_error *unpack_err)
860 const struct got_error *err = NULL;
861 struct gotd_imsg_packfile_status istatus;
862 struct ibuf *wbuf;
863 struct imsgbuf ibuf;
864 const char *unpack_ok = "unpack ok\n";
865 size_t len;
867 imsg_init(&ibuf, client->fd);
869 if (unpack_err)
870 istatus.reason_len = strlen(unpack_err->msg);
871 else
872 istatus.reason_len = strlen(unpack_ok);
874 len = sizeof(istatus) + istatus.reason_len;
875 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
876 repo_write.pid, len);
877 if (wbuf == NULL) {
878 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
879 goto done;
882 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
883 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
884 goto done;
887 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
888 istatus.reason_len) == -1) {
889 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
890 goto done;
893 wbuf->fd = -1;
894 imsg_close(&ibuf, wbuf);
896 err = gotd_imsg_flush(&ibuf);
897 done:
898 imsg_clear(&ibuf);
899 return err;
902 static const struct got_error *
903 recv_packfile(struct repo_write_client **client, struct imsg *imsg)
905 const struct got_error *err = NULL, *unpack_err;
906 struct gotd_imsg_recv_packfile ireq;
907 FILE *tempfiles[3] = { NULL, NULL, NULL };
908 struct repo_tempfile {
909 int fd;
910 int idx;
911 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
912 int i;
913 size_t datalen;
914 struct imsgbuf ibuf;
915 struct got_ratelimit rl;
916 struct got_pack *pack = NULL;
917 off_t pack_filesize = 0;
919 log_debug("packfile request received");
921 got_ratelimit_init(&rl, 2, 0);
923 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
924 if (datalen != sizeof(ireq))
925 return got_error(GOT_ERR_PRIVSEP_LEN);
926 memcpy(&ireq, imsg->data, sizeof(ireq));
928 *client = find_client(ireq.client_id);
929 if (*client == NULL || STAILQ_EMPTY(&(*client)->ref_updates))
930 return got_error(GOT_ERR_CLIENT_ID);
932 if ((*client)->pack_pipe[0] == -1 ||
933 (*client)->pack_pipe[1] == -1 ||
934 (*client)->packidx_fd == -1)
935 return got_error(GOT_ERR_PRIVSEP_NO_FD);
937 imsg_init(&ibuf, (*client)->fd);
939 if (imsg->fd == -1)
940 return got_error(GOT_ERR_PRIVSEP_NO_FD);
942 pack = &(*client)->pack;
943 memset(pack, 0, sizeof(*pack));
944 pack->fd = imsg->fd;
945 err = got_delta_cache_alloc(&pack->delta_cache);
946 if (err)
947 return err;
949 for (i = 0; i < nitems(repo_tempfiles); i++) {
950 struct repo_tempfile *t = &repo_tempfiles[i];
951 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
952 if (err)
953 goto done;
956 for (i = 0; i < nitems(tempfiles); i++) {
957 int fd = dup(repo_tempfiles[i].fd);
958 FILE *f;
959 if (fd == -1) {
960 err = got_error_from_errno("dup");
961 goto done;
963 f = fdopen(fd, "w+");
964 if (f == NULL) {
965 err = got_error_from_errno("dup");
966 close(fd);
967 goto done;
969 tempfiles[i] = f;
972 /* Send pack file pipe to gotsh(1). */
973 if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
974 repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
975 (*client)->pack_pipe[1] = -1;
976 err = got_error_from_errno("imsg_compose ACK");
977 if (err)
978 goto done;
980 (*client)->pack_pipe[1] = -1;
981 err = gotd_imsg_flush(&ibuf);
982 if (err)
983 goto done;
985 log_debug("receiving pack data");
986 unpack_err = recv_packdata(&pack_filesize, (*client)->pack_sha1,
987 (*client)->pack_pipe[0], pack->fd);
988 if (ireq.report_status) {
989 err = report_pack_status(*client, unpack_err);
990 if (err) {
991 /* Git clients hang up after sending the pack file. */
992 if (err->code == GOT_ERR_EOF)
993 err = NULL;
996 if (unpack_err)
997 err = unpack_err;
998 if (err)
999 goto done;
1001 log_debug("pack data received");
1003 /* XXX size_t vs off_t, both should be off_t */
1004 if (pack_filesize >= SIZE_MAX) {
1005 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
1006 "pack file too large");
1007 goto done;
1009 pack->filesize = pack_filesize;
1011 log_debug("begin indexing pack (%zu bytes in size)", pack->filesize);
1012 err = got_pack_index(pack, (*client)->packidx_fd,
1013 tempfiles[0], tempfiles[1], tempfiles[2], (*client)->pack_sha1,
1014 pack_index_progress, NULL, &rl);
1015 if (err)
1016 goto done;
1017 log_debug("done indexing pack");
1019 if (fsync((*client)->packidx_fd) == -1) {
1020 err = got_error_from_errno("fsync");
1021 goto done;
1023 if (lseek((*client)->packidx_fd, 0L, SEEK_SET) == -1)
1024 err = got_error_from_errno("lseek");
1025 done:
1026 if (close((*client)->pack_pipe[0]) == -1 && err == NULL)
1027 err = got_error_from_errno("close");
1028 (*client)->pack_pipe[0] = -1;
1029 for (i = 0; i < nitems(repo_tempfiles); i++) {
1030 struct repo_tempfile *t = &repo_tempfiles[i];
1031 if (t->idx != -1)
1032 got_repo_temp_fds_put(t->idx, repo_write.repo);
1034 for (i = 0; i < nitems(tempfiles); i++) {
1035 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1036 err = got_error_from_errno("fclose");
1038 if (err)
1039 got_pack_close(pack);
1040 imsg_clear(&ibuf);
1041 return err;
1044 static const struct got_error *
1045 verify_packfile(struct repo_write_client *client)
1047 const struct got_error *err = NULL, *close_err;
1048 struct gotd_ref_update *ref_update;
1049 struct got_packidx *packidx = NULL;
1050 struct stat sb;
1051 char *id_str = NULL;
1052 int idx = -1;
1054 if (STAILQ_EMPTY(&client->ref_updates)) {
1055 return got_error_msg(GOT_ERR_BAD_REQUEST,
1056 "cannot verify pack file without any ref-updates");
1059 if (client->pack.fd == -1) {
1060 return got_error_msg(GOT_ERR_BAD_REQUEST,
1061 "invalid pack file handle during pack verification");
1063 if (client->packidx_fd == -1) {
1064 return got_error_msg(GOT_ERR_BAD_REQUEST,
1065 "invalid pack index handle during pack verification");
1068 if (fstat(client->packidx_fd, &sb) == -1)
1069 return got_error_from_errno("pack index fstat");
1071 packidx = malloc(sizeof(*packidx));
1072 memset(packidx, 0, sizeof(*packidx));
1073 packidx->fd = client->packidx_fd;
1074 client->packidx_fd = -1;
1075 packidx->len = sb.st_size;
1077 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1078 if (err)
1079 return err;
1081 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1082 err = got_object_id_str(&id_str, &ref_update->new_id);
1083 if (err)
1084 goto done;
1086 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1087 if (idx == -1) {
1088 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1089 "advertised object %s is missing from pack file",
1090 id_str);
1091 goto done;
1095 done:
1096 close_err = got_packidx_close(packidx);
1097 if (close_err && err == NULL)
1098 err = close_err;
1099 free(id_str);
1100 return err;
1103 static const struct got_error *
1104 install_packfile(struct repo_write_client *client, struct gotd_imsgev *iev)
1106 struct gotd_imsg_packfile_install inst;
1107 int ret;
1109 memset(&inst, 0, sizeof(inst));
1110 inst.client_id = client->id;
1111 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1113 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1114 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1115 if (ret == -1)
1116 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1118 return NULL;
1121 static const struct got_error *
1122 send_ref_updates_start(struct repo_write_client *client, int nref_updates,
1123 struct gotd_imsgev *iev)
1125 struct gotd_imsg_ref_updates_start istart;
1126 int ret;
1128 memset(&istart, 0, sizeof(istart));
1129 istart.nref_updates = nref_updates;
1130 istart.client_id = client->id;
1132 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1133 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1134 if (ret == -1)
1135 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1137 return NULL;
1141 static const struct got_error *
1142 send_ref_update(struct repo_write_client *client,
1143 struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1145 struct gotd_imsg_ref_update iref;
1146 const char *refname = got_ref_get_name(ref_update->ref);
1147 struct ibuf *wbuf;
1148 size_t len;
1150 memset(&iref, 0, sizeof(iref));
1151 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1152 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1153 iref.ref_is_new = ref_update->ref_is_new;
1154 iref.client_id = client->id;
1155 iref.name_len = strlen(refname);
1157 len = sizeof(iref) + iref.name_len;
1158 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1159 repo_write.pid, len);
1160 if (wbuf == NULL)
1161 return got_error_from_errno("imsg_create REF_UPDATE");
1163 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1164 return got_error_from_errno("imsg_add REF_UPDATE");
1165 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1166 return got_error_from_errno("imsg_add REF_UPDATE");
1168 wbuf->fd = -1;
1169 imsg_close(&iev->ibuf, wbuf);
1171 gotd_imsg_event_add(iev);
1172 return NULL;
1175 static const struct got_error *
1176 update_refs(struct repo_write_client *client, struct gotd_imsgev *iev)
1178 const struct got_error *err = NULL;
1179 struct gotd_ref_update *ref_update;
1181 err = send_ref_updates_start(client, client->nref_updates, iev);
1182 if (err)
1183 return err;
1185 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1186 err = send_ref_update(client, ref_update, iev);
1187 if (err)
1188 goto done;
1190 done:
1191 return err;
1194 static const struct got_error *
1195 recv_disconnect(struct imsg *imsg)
1197 const struct got_error *err = NULL;
1198 struct gotd_imsg_disconnect idisconnect;
1199 size_t datalen;
1200 int client_fd = -1, pipe0 = -1, pipe1 = - 1, idxfd = -1;
1201 struct repo_write_client *client = NULL;
1202 uint64_t slot;
1204 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1205 if (datalen != sizeof(idisconnect))
1206 return got_error(GOT_ERR_PRIVSEP_LEN);
1207 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1209 log_debug("client disconnecting");
1211 client = find_client(idisconnect.client_id);
1212 if (client == NULL)
1213 return got_error(GOT_ERR_CLIENT_ID);
1215 slot = client_hash(client->id) % nitems(repo_write_clients);
1216 STAILQ_REMOVE(&repo_write_clients[slot], client, repo_write_client,
1217 entry);
1218 while (!STAILQ_EMPTY(&client->ref_updates)) {
1219 struct gotd_ref_update *ref_update;
1220 ref_update = STAILQ_FIRST(&client->ref_updates);
1221 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1222 got_ref_close(ref_update->ref);
1223 free(ref_update);
1225 err = got_pack_close(&client->pack);
1226 client_fd = client->fd;
1227 pipe0 = client->pack_pipe[0];
1228 pipe1 = client->pack_pipe[1];
1229 idxfd = client->packidx_fd;
1230 free(client);
1231 if (client_fd != -1 && close(client_fd) == -1)
1232 err = got_error_from_errno("close");
1233 if (pipe0 != -1 && close(pipe0) == -1 && err == NULL)
1234 err = got_error_from_errno("close");
1235 if (pipe1 != -1 && close(pipe1) == -1 && err == NULL)
1236 err = got_error_from_errno("close");
1237 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1238 err = got_error_from_errno("close");
1239 return err;
1242 static const struct got_error *
1243 receive_pack_pipe(struct repo_write_client **client, struct imsg *imsg,
1244 struct gotd_imsgev *iev)
1246 struct gotd_imsg_packfile_pipe ireq;
1247 size_t datalen;
1249 log_debug("receving pack pipe descriptor");
1251 if (imsg->fd == -1)
1252 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1254 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1255 if (datalen != sizeof(ireq))
1256 return got_error(GOT_ERR_PRIVSEP_LEN);
1257 memcpy(&ireq, imsg->data, sizeof(ireq));
1259 *client = find_client(ireq.client_id);
1260 if (*client == NULL)
1261 return got_error(GOT_ERR_CLIENT_ID);
1262 if ((*client)->pack_pipe[1] != -1)
1263 return got_error(GOT_ERR_PRIVSEP_MSG);
1265 if ((*client)->pack_pipe[0] == -1)
1266 (*client)->pack_pipe[0] = imsg->fd;
1267 else
1268 (*client)->pack_pipe[1] = imsg->fd;
1270 return NULL;
1273 static const struct got_error *
1274 receive_pack_idx(struct repo_write_client **client, struct imsg *imsg,
1275 struct gotd_imsgev *iev)
1277 struct gotd_imsg_packidx_file ireq;
1278 size_t datalen;
1280 log_debug("receving pack index output file");
1282 if (imsg->fd == -1)
1283 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1285 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1286 if (datalen != sizeof(ireq))
1287 return got_error(GOT_ERR_PRIVSEP_LEN);
1288 memcpy(&ireq, imsg->data, sizeof(ireq));
1290 *client = find_client(ireq.client_id);
1291 if (*client == NULL)
1292 return got_error(GOT_ERR_CLIENT_ID);
1293 if ((*client)->packidx_fd != -1)
1294 return got_error(GOT_ERR_PRIVSEP_MSG);
1296 (*client)->packidx_fd = imsg->fd;
1297 return NULL;
1300 static void
1301 repo_write_dispatch(int fd, short event, void *arg)
1303 const struct got_error *err = NULL;
1304 struct gotd_imsgev *iev = arg;
1305 struct imsgbuf *ibuf = &iev->ibuf;
1306 struct imsg imsg;
1307 struct repo_write_client *client = NULL;
1308 ssize_t n;
1309 int shut = 0;
1311 if (event & EV_READ) {
1312 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1313 fatal("imsg_read error");
1314 if (n == 0) /* Connection closed. */
1315 shut = 1;
1318 if (event & EV_WRITE) {
1319 n = msgbuf_write(&ibuf->w);
1320 if (n == -1 && errno != EAGAIN)
1321 fatal("msgbuf_write");
1322 if (n == 0) /* Connection closed. */
1323 shut = 1;
1326 for (;;) {
1327 if ((n = imsg_get(ibuf, &imsg)) == -1)
1328 fatal("%s: imsg_get error", __func__);
1329 if (n == 0) /* No more messages. */
1330 break;
1332 switch (imsg.hdr.type) {
1333 case GOTD_IMSG_LIST_REFS_INTERNAL:
1334 err = list_refs(&client, &imsg);
1335 if (err)
1336 log_warnx("%s: ls-refs: %s", repo_write.title,
1337 err->msg);
1338 break;
1339 case GOTD_IMSG_REF_UPDATE:
1340 err = recv_ref_update(&client, &imsg);
1341 if (err)
1342 log_warnx("%s: ref-update: %s",
1343 repo_write.title, err->msg);
1344 break;
1345 case GOTD_IMSG_PACKFILE_PIPE:
1346 err = receive_pack_pipe(&client, &imsg, iev);
1347 if (err) {
1348 log_warnx("%s: receiving pack pipe: %s",
1349 repo_write.title, err->msg);
1350 break;
1352 break;
1353 case GOTD_IMSG_PACKIDX_FILE:
1354 err = receive_pack_idx(&client, &imsg, iev);
1355 if (err) {
1356 log_warnx("%s: receiving pack index: %s",
1357 repo_write.title, err->msg);
1358 break;
1360 break;
1361 case GOTD_IMSG_RECV_PACKFILE:
1362 err = recv_packfile(&client, &imsg);
1363 if (err) {
1364 log_warnx("%s: receive packfile: %s",
1365 repo_write.title, err->msg);
1366 break;
1368 err = verify_packfile(client);
1369 if (err) {
1370 log_warnx("%s: verify packfile: %s",
1371 repo_write.title, err->msg);
1372 break;
1374 err = install_packfile(client, iev);
1375 if (err) {
1376 log_warnx("%s: install packfile: %s",
1377 repo_write.title, err->msg);
1378 break;
1380 err = update_refs(client, iev);
1381 if (err) {
1382 log_warnx("%s: update refs: %s",
1383 repo_write.title, err->msg);
1385 break;
1386 case GOTD_IMSG_DISCONNECT:
1387 err = recv_disconnect(&imsg);
1388 if (err)
1389 log_warnx("%s: disconnect: %s",
1390 repo_write.title, err->msg);
1391 break;
1392 default:
1393 log_debug("%s: unexpected imsg %d", repo_write.title,
1394 imsg.hdr.type);
1395 break;
1398 imsg_free(&imsg);
1401 if (!shut && check_cancelled(NULL) == NULL) {
1402 if (err &&
1403 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1404 client ? client->id : 0, err) == -1) {
1405 log_warnx("could not send error to parent: %s",
1406 err->msg);
1408 gotd_imsg_event_add(iev);
1409 } else {
1410 /* This pipe is dead. Remove its event handler */
1411 event_del(&iev->ev);
1412 event_loopexit(NULL);
1416 void
1417 repo_write_main(const char *title, int *pack_fds, int *temp_fds)
1419 const struct got_error *err = NULL;
1420 struct gotd_imsgev iev;
1422 repo_write.title = title;
1423 repo_write.pid = getpid();
1424 repo_write.pack_fds = pack_fds;
1425 repo_write.temp_fds = temp_fds;
1427 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1430 * Open a repository in the root directory.
1431 * We are already in chroot at this point.
1433 err = got_repo_open(&repo_write.repo, "/", NULL, pack_fds);
1434 if (err)
1435 goto done;
1436 if (!got_repo_is_bare(repo_write.repo)) {
1437 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1438 "bare git repository required");
1439 goto done;
1442 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1444 signal(SIGINT, catch_sigint);
1445 signal(SIGTERM, catch_sigterm);
1446 signal(SIGPIPE, SIG_IGN);
1447 signal(SIGHUP, SIG_IGN);
1449 imsg_init(&iev.ibuf, GOTD_SOCK_FILENO);
1450 iev.handler = repo_write_dispatch;
1451 iev.events = EV_READ;
1452 iev.handler_arg = NULL;
1453 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1454 if (event_add(&iev.ev, NULL) == -1) {
1455 err = got_error_from_errno("event_add");
1456 goto done;
1459 event_dispatch();
1460 done:
1461 if (err)
1462 log_warnx("%s: %s", title, err->msg);
1463 repo_write_shutdown();
1466 void
1467 repo_write_shutdown(void)
1469 log_debug("%s: shutting down", repo_write.title);
1470 if (repo_write.repo)
1471 got_repo_close(repo_write.repo);
1472 got_repo_pack_fds_close(repo_write.pack_fds);
1473 got_repo_temp_fds_close(repo_write.pack_fds);
1474 exit(0);