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 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->pack_pipe = -1;
261 client->packidx_fd = -1;
262 client->nref_updates = 0;
263 client->nref_del = 0;
264 client->nref_new = 0;
266 imsg_init(&ibuf, client_fd);
268 err = got_ref_list(&refs, repo_write.repo, "",
269 got_ref_cmp_by_name, NULL);
270 if (err)
271 return err;
273 memset(&irefs, 0, sizeof(irefs));
274 TAILQ_FOREACH(re, &refs, entry) {
275 struct got_object_id *id;
276 int obj_type;
278 if (got_ref_is_symbolic(re->ref))
279 continue;
281 irefs.nrefs++;
283 /* Account for a peeled tag refs. */
284 err = got_ref_resolve(&id, repo_write.repo, re->ref);
285 if (err)
286 goto done;
287 err = got_object_get_type(&obj_type, repo_write.repo, id);
288 free(id);
289 if (err)
290 goto done;
291 if (obj_type == GOT_OBJ_TYPE_TAG)
292 irefs.nrefs++;
295 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
296 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
297 err = got_error_from_errno("imsg_compose REFLIST");
298 goto done;
301 TAILQ_FOREACH(re, &refs, entry) {
302 if (got_ref_is_symbolic(re->ref))
303 continue;
304 err = send_ref(re->ref, &ibuf);
305 if (err)
306 goto done;
309 err = gotd_imsg_flush(&ibuf);
310 done:
311 got_ref_list_free(&refs);
312 imsg_clear(&ibuf);
313 return err;
316 static const struct got_error *
317 protect_ref_namespace(struct got_reference *ref, const char *namespace)
319 size_t len = strlen(namespace);
321 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
322 namespace[len -1] != '/') {
323 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
324 "reference namespace '%s'", namespace);
327 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
328 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
330 return NULL;
333 static const struct got_error *
334 recv_ref_update(struct imsg *imsg)
336 static const char zero_id[SHA1_DIGEST_LENGTH];
337 const struct got_error *err = NULL;
338 struct repo_write_client *client = &repo_write_client;
339 struct gotd_imsg_ref_update iref;
340 size_t datalen;
341 char *refname = NULL;
342 struct got_reference *ref = NULL;
343 struct got_object_id *id = NULL;
344 struct imsgbuf ibuf;
345 struct gotd_ref_update *ref_update = NULL;
347 log_debug("ref-update received");
349 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
350 if (datalen < sizeof(iref))
351 return got_error(GOT_ERR_PRIVSEP_LEN);
352 memcpy(&iref, imsg->data, sizeof(iref));
353 if (datalen != sizeof(iref) + iref.name_len)
354 return got_error(GOT_ERR_PRIVSEP_LEN);
356 imsg_init(&ibuf, client->fd);
358 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
359 if (refname == NULL)
360 return got_error_from_errno("strndup");
362 ref_update = calloc(1, sizeof(*ref_update));
363 if (ref_update == NULL) {
364 err = got_error_from_errno("malloc");
365 goto done;
368 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
369 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
371 err = got_ref_open(&ref, repo_write.repo, refname, 0);
372 if (err) {
373 if (err->code != GOT_ERR_NOT_REF)
374 goto done;
375 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
376 if (err)
377 goto done;
378 ref_update->ref_is_new = 1;
379 client->nref_new++;
381 if (got_ref_is_symbolic(ref)) {
382 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
383 "'%s' is a symbolic reference and cannot "
384 "be updated", got_ref_get_name(ref));
385 goto done;
387 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
388 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
389 "%s: does not begin with 'refs/'",
390 got_ref_get_name(ref));
391 goto done;
394 err = protect_ref_namespace(ref, "refs/got/");
395 if (err)
396 goto done;
397 err = protect_ref_namespace(ref, "refs/remotes/");
398 if (err)
399 goto done;
401 if (!ref_update->ref_is_new) {
402 /*
403 * Ensure the client's idea of this update is still valid.
404 * At this point we can only return an error, to prevent
405 * the client from uploading a pack file which will likely
406 * have to be discarded.
407 */
408 err = got_ref_resolve(&id, repo_write.repo, ref);
409 if (err)
410 goto done;
412 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
413 err = got_error_fmt(GOT_ERR_REF_BUSY,
414 "%s has been modified by someone else "
415 "while transaction was in progress",
416 got_ref_get_name(ref));
417 goto done;
421 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
422 repo_write.pid);
424 ref_update->ref = ref;
425 if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
426 ref_update->delete_ref = 1;
427 client->nref_del++;
429 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
430 client->nref_updates++;
431 ref = NULL;
432 ref_update = NULL;
433 done:
434 if (ref)
435 got_ref_close(ref);
436 free(ref_update);
437 free(refname);
438 free(id);
439 return err;
442 static const struct got_error *
443 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
444 uint32_t nobj_loose, uint32_t nobj_resolved)
446 int p_indexed = 0, p_resolved = 0;
447 int nobj_delta = nobj_total - nobj_loose;
449 if (nobj_total > 0)
450 p_indexed = (nobj_indexed * 100) / nobj_total;
452 if (nobj_delta > 0)
453 p_resolved = (nobj_resolved * 100) / nobj_delta;
455 if (p_resolved > 0) {
456 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
457 nobj_total, p_indexed, nobj_delta, p_resolved);
458 } else
459 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
461 return NULL;
464 static const struct got_error *
465 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
467 const struct got_error *err = NULL;
468 uint8_t readahead[65536];
469 size_t have, newlen;
471 err = got_poll_read_full(infd, &have,
472 readahead, sizeof(readahead), minsize);
473 if (err)
474 return err;
476 err = buf_append(&newlen, buf, readahead, have);
477 if (err)
478 return err;
479 return NULL;
482 static const struct got_error *
483 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
484 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
486 const struct got_error *err = NULL;
487 uint8_t t = 0;
488 uint64_t s = 0;
489 uint8_t sizebuf[8];
490 size_t i = 0;
491 off_t obj_offset = *outsize;
493 do {
494 /* We do not support size values which don't fit in 64 bit. */
495 if (i > 9)
496 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
497 "packfile offset %lld", (long long)obj_offset);
499 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
500 err = read_more_pack_stream(infd, buf,
501 sizeof(sizebuf[0]));
502 if (err)
503 return err;
506 sizebuf[i] = buf_getc(buf, *buf_pos);
507 *buf_pos += sizeof(sizebuf[i]);
509 if (i == 0) {
510 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
511 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
512 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
513 } else {
514 size_t shift = 4 + 7 * (i - 1);
515 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
516 shift);
518 i++;
519 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
521 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
522 if (err)
523 return err;
524 *outsize += i;
526 *type = t;
527 *size = s;
528 return NULL;
531 static const struct got_error *
532 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
533 SHA1_CTX *ctx)
535 const struct got_error *err = NULL;
536 size_t remain = buf_len(buf) - *buf_pos;
538 if (remain < SHA1_DIGEST_LENGTH) {
539 err = read_more_pack_stream(infd, buf,
540 SHA1_DIGEST_LENGTH - remain);
541 if (err)
542 return err;
545 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
546 SHA1_DIGEST_LENGTH, ctx);
547 if (err)
548 return err;
550 *buf_pos += SHA1_DIGEST_LENGTH;
551 return NULL;
554 static const struct got_error *
555 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
556 SHA1_CTX *ctx)
558 const struct got_error *err = NULL;
559 uint64_t o = 0;
560 uint8_t offbuf[8];
561 size_t i = 0;
562 off_t obj_offset = *outsize;
564 do {
565 /* We do not support offset values which don't fit in 64 bit. */
566 if (i > 8)
567 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
568 "packfile offset %lld", (long long)obj_offset);
570 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
571 err = read_more_pack_stream(infd, buf,
572 sizeof(offbuf[0]));
573 if (err)
574 return err;
577 offbuf[i] = buf_getc(buf, *buf_pos);
578 *buf_pos += sizeof(offbuf[i]);
580 if (i == 0)
581 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
582 else {
583 o++;
584 o <<= 7;
585 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
587 i++;
588 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
590 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
591 return got_error(GOT_ERR_PACK_OFFSET);
593 err = got_pack_hwrite(outfd, offbuf, i, ctx);
594 if (err)
595 return err;
597 *outsize += i;
598 return NULL;
601 static const struct got_error *
602 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
603 SHA1_CTX *ctx)
605 const struct got_error *err = NULL;
606 z_stream z;
607 int zret;
608 char voidbuf[1024];
609 size_t consumed_total = 0;
610 off_t zstream_offset = *outsize;
612 memset(&z, 0, sizeof(z));
614 z.zalloc = Z_NULL;
615 z.zfree = Z_NULL;
616 zret = inflateInit(&z);
617 if (zret != Z_OK) {
618 if (zret == Z_ERRNO)
619 return got_error_from_errno("inflateInit");
620 if (zret == Z_MEM_ERROR) {
621 errno = ENOMEM;
622 return got_error_from_errno("inflateInit");
624 return got_error_msg(GOT_ERR_DECOMPRESSION,
625 "inflateInit failed");
628 while (zret != Z_STREAM_END) {
629 size_t last_total_in, consumed;
631 /*
632 * Decompress into the void. Object data will be parsed
633 * later, when the pack file is indexed. For now, we just
634 * want to locate the end of the compressed stream.
635 */
636 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
637 last_total_in = z.total_in;
638 z.next_in = buf_get(buf) + *buf_pos;
639 z.avail_in = buf_len(buf) - *buf_pos;
640 z.next_out = voidbuf;
641 z.avail_out = sizeof(voidbuf);
643 zret = inflate(&z, Z_SYNC_FLUSH);
644 if (zret != Z_OK && zret != Z_BUF_ERROR &&
645 zret != Z_STREAM_END) {
646 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
647 "packfile offset %lld",
648 (long long)zstream_offset);
649 goto done;
651 consumed = z.total_in - last_total_in;
653 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
654 consumed, ctx);
655 if (err)
656 goto done;
658 err = buf_discard(buf, *buf_pos + consumed);
659 if (err)
660 goto done;
661 *buf_pos = 0;
663 consumed_total += consumed;
666 if (zret != Z_STREAM_END) {
667 err = read_more_pack_stream(infd, buf, 1);
668 if (err)
669 goto done;
673 if (err == NULL)
674 *outsize += consumed_total;
675 done:
676 inflateEnd(&z);
677 return err;
680 static const struct got_error *
681 validate_object_type(int obj_type)
683 switch (obj_type) {
684 case GOT_OBJ_TYPE_BLOB:
685 case GOT_OBJ_TYPE_COMMIT:
686 case GOT_OBJ_TYPE_TREE:
687 case GOT_OBJ_TYPE_TAG:
688 case GOT_OBJ_TYPE_REF_DELTA:
689 case GOT_OBJ_TYPE_OFFSET_DELTA:
690 return NULL;
691 default:
692 break;
695 return got_error(GOT_ERR_OBJ_TYPE);
698 static const struct got_error *
699 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
700 int infd, int outfd)
702 const struct got_error *err;
703 struct repo_write_client *client = &repo_write_client;
704 struct got_packfile_hdr hdr;
705 size_t have;
706 uint32_t nhave = 0;
707 SHA1_CTX ctx;
708 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
709 char hex[SHA1_DIGEST_STRING_LENGTH];
710 BUF *buf = NULL;
711 size_t buf_pos = 0, remain;
712 ssize_t w;
714 *outsize = 0;
715 *nobj = 0;
717 /* if only deleting references there's nothing to read */
718 if (client->nref_updates == client->nref_del)
719 return NULL;
721 SHA1Init(&ctx);
723 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
724 if (err)
725 return err;
726 if (have != sizeof(hdr))
727 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
728 *outsize += have;
730 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
731 return got_error_msg(GOT_ERR_BAD_PACKFILE,
732 "bad packfile signature");
733 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
734 return got_error_msg(GOT_ERR_BAD_PACKFILE,
735 "bad packfile version");
737 *nobj = be32toh(hdr.nobjects);
738 if (*nobj == 0) {
739 /*
740 * Clients which are creating new references only
741 * will send us an empty pack file.
742 */
743 if (client->nref_updates > 0 &&
744 client->nref_updates == client->nref_new)
745 return NULL;
747 return got_error_msg(GOT_ERR_BAD_PACKFILE,
748 "bad packfile with zero objects");
751 log_debug("expecting %d objects", *nobj);
753 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
754 if (err)
755 return err;
757 err = buf_alloc(&buf, 65536);
758 if (err)
759 return err;
761 while (nhave != *nobj) {
762 uint8_t obj_type;
763 uint64_t obj_size;
765 err = copy_object_type_and_size(&obj_type, &obj_size,
766 infd, outfd, outsize, buf, &buf_pos, &ctx);
767 if (err)
768 goto done;
770 err = validate_object_type(obj_type);
771 if (err)
772 goto done;
774 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
775 err = copy_ref_delta(infd, outfd, outsize,
776 buf, &buf_pos, &ctx);
777 if (err)
778 goto done;
779 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
780 err = copy_offset_delta(infd, outfd, outsize,
781 buf, &buf_pos, &ctx);
782 if (err)
783 goto done;
786 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
787 if (err)
788 goto done;
790 nhave++;
793 log_debug("received %u objects", *nobj);
795 SHA1Final(expected_sha1, &ctx);
797 remain = buf_len(buf) - buf_pos;
798 if (remain < SHA1_DIGEST_LENGTH) {
799 err = read_more_pack_stream(infd, buf,
800 SHA1_DIGEST_LENGTH - remain);
801 if (err)
802 return err;
805 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
806 log_debug("expect SHA1: %s", hex);
807 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
808 log_debug("actual SHA1: %s", hex);
810 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
811 SHA1_DIGEST_LENGTH) != 0) {
812 err = got_error(GOT_ERR_PACKFILE_CSUM);
813 goto done;
816 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
818 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
819 if (w == -1) {
820 err = got_error_from_errno("write");
821 goto done;
823 if (w != SHA1_DIGEST_LENGTH) {
824 err = got_error(GOT_ERR_IO);
825 goto done;
828 *outsize += SHA1_DIGEST_LENGTH;
830 if (fsync(outfd) == -1) {
831 err = got_error_from_errno("fsync");
832 goto done;
834 if (lseek(outfd, 0L, SEEK_SET) == -1) {
835 err = got_error_from_errno("lseek");
836 goto done;
838 done:
839 buf_free(buf);
840 return err;
843 static const struct got_error *
844 report_pack_status(const struct got_error *unpack_err)
846 const struct got_error *err = NULL;
847 struct repo_write_client *client = &repo_write_client;
848 struct gotd_imsg_packfile_status istatus;
849 struct ibuf *wbuf;
850 struct imsgbuf ibuf;
851 const char *unpack_ok = "unpack ok\n";
852 size_t len;
854 imsg_init(&ibuf, client->fd);
856 if (unpack_err)
857 istatus.reason_len = strlen(unpack_err->msg);
858 else
859 istatus.reason_len = strlen(unpack_ok);
861 len = sizeof(istatus) + istatus.reason_len;
862 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
863 repo_write.pid, len);
864 if (wbuf == NULL) {
865 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
866 goto done;
869 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
870 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
871 goto done;
874 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
875 istatus.reason_len) == -1) {
876 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
877 goto done;
880 wbuf->fd = -1;
881 imsg_close(&ibuf, wbuf);
883 err = gotd_imsg_flush(&ibuf);
884 done:
885 imsg_clear(&ibuf);
886 return err;
889 static const struct got_error *
890 recv_packfile(int *have_packfile, struct imsg *imsg)
892 const struct got_error *err = NULL, *unpack_err;
893 struct repo_write_client *client = &repo_write_client;
894 struct gotd_imsg_recv_packfile ireq;
895 FILE *tempfiles[3] = { NULL, NULL, NULL };
896 struct repo_tempfile {
897 int fd;
898 int idx;
899 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
900 int i;
901 size_t datalen;
902 struct imsgbuf ibuf;
903 struct got_ratelimit rl;
904 struct got_pack *pack = NULL;
905 off_t pack_filesize = 0;
906 uint32_t nobj = 0;
908 log_debug("packfile request received");
910 *have_packfile = 0;
911 got_ratelimit_init(&rl, 2, 0);
913 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
914 if (datalen != sizeof(ireq))
915 return got_error(GOT_ERR_PRIVSEP_LEN);
916 memcpy(&ireq, imsg->data, sizeof(ireq));
918 if (client->pack_pipe == -1 || client->packidx_fd == -1)
919 return got_error(GOT_ERR_PRIVSEP_NO_FD);
921 imsg_init(&ibuf, client->fd);
923 if (imsg->fd == -1)
924 return got_error(GOT_ERR_PRIVSEP_NO_FD);
926 pack = &client->pack;
927 memset(pack, 0, sizeof(*pack));
928 pack->fd = imsg->fd;
929 err = got_delta_cache_alloc(&pack->delta_cache);
930 if (err)
931 return err;
933 for (i = 0; i < nitems(repo_tempfiles); i++) {
934 struct repo_tempfile *t = &repo_tempfiles[i];
935 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
936 if (err)
937 goto done;
940 for (i = 0; i < nitems(tempfiles); i++) {
941 int fd;
942 FILE *f;
944 fd = dup(repo_tempfiles[i].fd);
945 if (fd == -1) {
946 err = got_error_from_errno("dup");
947 goto done;
949 f = fdopen(fd, "w+");
950 if (f == NULL) {
951 err = got_error_from_errno("fdopen");
952 close(fd);
953 goto done;
955 tempfiles[i] = f;
958 err = gotd_imsg_flush(&ibuf);
959 if (err)
960 goto done;
962 log_debug("receiving pack data");
963 unpack_err = recv_packdata(&pack_filesize, &nobj,
964 client->pack_sha1, client->pack_pipe, pack->fd);
965 if (ireq.report_status) {
966 err = report_pack_status(unpack_err);
967 if (err) {
968 /* Git clients hang up after sending the pack file. */
969 if (err->code == GOT_ERR_EOF)
970 err = NULL;
973 if (unpack_err)
974 err = unpack_err;
975 if (err)
976 goto done;
978 log_debug("pack data received");
980 /*
981 * Clients which are creating new references only will
982 * send us an empty pack file.
983 */
984 if (nobj == 0 &&
985 pack_filesize == sizeof(struct got_packfile_hdr) &&
986 client->nref_updates > 0 &&
987 client->nref_updates == client->nref_new)
988 goto done;
990 /*
991 * Clients which are deleting references only will send
992 * no pack file.
993 */
994 if (nobj == 0 &&
995 client->nref_del > 0 &&
996 client->nref_updates == client->nref_del)
997 goto done;
999 pack->filesize = pack_filesize;
1000 *have_packfile = 1;
1002 log_debug("begin indexing pack (%lld bytes in size)",
1003 (long long)pack->filesize);
1004 err = got_pack_index(pack, client->packidx_fd,
1005 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1006 pack_index_progress, NULL, &rl);
1007 if (err)
1008 goto done;
1009 log_debug("done indexing pack");
1011 if (fsync(client->packidx_fd) == -1) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1015 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1016 err = got_error_from_errno("lseek");
1017 done:
1018 if (close(client->pack_pipe) == -1 && err == NULL)
1019 err = got_error_from_errno("close");
1020 client->pack_pipe = -1;
1021 for (i = 0; i < nitems(repo_tempfiles); i++) {
1022 struct repo_tempfile *t = &repo_tempfiles[i];
1023 if (t->idx != -1)
1024 got_repo_temp_fds_put(t->idx, repo_write.repo);
1026 for (i = 0; i < nitems(tempfiles); i++) {
1027 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1028 err = got_error_from_errno("fclose");
1030 if (err)
1031 got_pack_close(pack);
1032 imsg_clear(&ibuf);
1033 return err;
1036 static const struct got_error *
1037 verify_packfile(void)
1039 const struct got_error *err = NULL, *close_err;
1040 struct repo_write_client *client = &repo_write_client;
1041 struct gotd_ref_update *ref_update;
1042 struct got_packidx *packidx = NULL;
1043 struct stat sb;
1044 char *id_str = NULL;
1045 int idx = -1;
1047 if (STAILQ_EMPTY(&client->ref_updates)) {
1048 return got_error_msg(GOT_ERR_BAD_REQUEST,
1049 "cannot verify pack file without any ref-updates");
1052 if (client->pack.fd == -1) {
1053 return got_error_msg(GOT_ERR_BAD_REQUEST,
1054 "invalid pack file handle during pack verification");
1056 if (client->packidx_fd == -1) {
1057 return got_error_msg(GOT_ERR_BAD_REQUEST,
1058 "invalid pack index handle during pack verification");
1061 if (fstat(client->packidx_fd, &sb) == -1)
1062 return got_error_from_errno("pack index fstat");
1064 packidx = malloc(sizeof(*packidx));
1065 memset(packidx, 0, sizeof(*packidx));
1066 packidx->fd = client->packidx_fd;
1067 client->packidx_fd = -1;
1068 packidx->len = sb.st_size;
1070 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1071 if (err)
1072 return err;
1074 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1075 if (ref_update->delete_ref)
1076 continue;
1078 err = got_object_id_str(&id_str, &ref_update->new_id);
1079 if (err)
1080 goto done;
1082 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1083 if (idx == -1) {
1084 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1085 "advertised object %s is missing from pack file",
1086 id_str);
1087 goto done;
1091 done:
1092 close_err = got_packidx_close(packidx);
1093 if (close_err && err == NULL)
1094 err = close_err;
1095 free(id_str);
1096 return err;
1099 static const struct got_error *
1100 install_packfile(struct gotd_imsgev *iev)
1102 struct repo_write_client *client = &repo_write_client;
1103 struct gotd_imsg_packfile_install inst;
1104 int ret;
1106 memset(&inst, 0, sizeof(inst));
1107 inst.client_id = client->id;
1108 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1110 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1111 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1112 if (ret == -1)
1113 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1115 return NULL;
1118 static const struct got_error *
1119 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1121 struct repo_write_client *client = &repo_write_client;
1122 struct gotd_imsg_ref_updates_start istart;
1123 int ret;
1125 memset(&istart, 0, sizeof(istart));
1126 istart.nref_updates = nref_updates;
1127 istart.client_id = client->id;
1129 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1130 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1131 if (ret == -1)
1132 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1134 return NULL;
1138 static const struct got_error *
1139 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1141 struct repo_write_client *client = &repo_write_client;
1142 struct gotd_imsg_ref_update iref;
1143 const char *refname = got_ref_get_name(ref_update->ref);
1144 struct ibuf *wbuf;
1145 size_t len;
1147 memset(&iref, 0, sizeof(iref));
1148 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1149 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1150 iref.ref_is_new = ref_update->ref_is_new;
1151 iref.delete_ref = ref_update->delete_ref;
1152 iref.client_id = client->id;
1153 iref.name_len = strlen(refname);
1155 len = sizeof(iref) + iref.name_len;
1156 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1157 repo_write.pid, len);
1158 if (wbuf == NULL)
1159 return got_error_from_errno("imsg_create REF_UPDATE");
1161 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1162 return got_error_from_errno("imsg_add REF_UPDATE");
1163 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1164 return got_error_from_errno("imsg_add REF_UPDATE");
1166 wbuf->fd = -1;
1167 imsg_close(&iev->ibuf, wbuf);
1169 gotd_imsg_event_add(iev);
1170 return NULL;
1173 static const struct got_error *
1174 update_refs(struct gotd_imsgev *iev)
1176 const struct got_error *err = NULL;
1177 struct repo_write_client *client = &repo_write_client;
1178 struct gotd_ref_update *ref_update;
1180 err = send_ref_updates_start(client->nref_updates, iev);
1181 if (err)
1182 return err;
1184 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1185 err = send_ref_update(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 pack_pipe = -1, idxfd = -1;
1200 struct repo_write_client *client = &repo_write_client;
1202 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1203 if (datalen != sizeof(idisconnect))
1204 return got_error(GOT_ERR_PRIVSEP_LEN);
1205 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1207 log_debug("client disconnecting");
1209 while (!STAILQ_EMPTY(&client->ref_updates)) {
1210 struct gotd_ref_update *ref_update;
1211 ref_update = STAILQ_FIRST(&client->ref_updates);
1212 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1213 got_ref_close(ref_update->ref);
1214 free(ref_update);
1216 err = got_pack_close(&client->pack);
1217 if (client->fd != -1 && close(client->fd) == -1)
1218 err = got_error_from_errno("close");
1219 pack_pipe = client->pack_pipe;
1220 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1221 err = got_error_from_errno("close");
1222 idxfd = client->packidx_fd;
1223 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1224 err = got_error_from_errno("close");
1225 return err;
1228 static const struct got_error *
1229 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1231 struct repo_write_client *client = &repo_write_client;
1232 struct gotd_imsg_packfile_pipe ireq;
1233 size_t datalen;
1235 log_debug("receving pack pipe descriptor");
1237 if (imsg->fd == -1)
1238 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1240 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1241 if (datalen != sizeof(ireq))
1242 return got_error(GOT_ERR_PRIVSEP_LEN);
1243 memcpy(&ireq, imsg->data, sizeof(ireq));
1245 if (client->pack_pipe != -1)
1246 return got_error(GOT_ERR_PRIVSEP_MSG);
1248 client->pack_pipe = imsg->fd;
1249 return NULL;
1252 static const struct got_error *
1253 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1255 struct repo_write_client *client = &repo_write_client;
1256 struct gotd_imsg_packidx_file ireq;
1257 size_t datalen;
1259 log_debug("receving pack index output file");
1261 if (imsg->fd == -1)
1262 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1264 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1265 if (datalen != sizeof(ireq))
1266 return got_error(GOT_ERR_PRIVSEP_LEN);
1267 memcpy(&ireq, imsg->data, sizeof(ireq));
1269 if (client->packidx_fd != -1)
1270 return got_error(GOT_ERR_PRIVSEP_MSG);
1272 client->packidx_fd = imsg->fd;
1273 return NULL;
1276 static void
1277 repo_write_dispatch_session(int fd, short event, void *arg)
1279 const struct got_error *err = NULL;
1280 struct gotd_imsgev *iev = arg;
1281 struct imsgbuf *ibuf = &iev->ibuf;
1282 struct imsg imsg;
1283 struct repo_write_client *client = &repo_write_client;
1284 ssize_t n;
1285 int shut = 0, have_packfile = 0;
1287 if (event & EV_READ) {
1288 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1289 fatal("imsg_read error");
1290 if (n == 0) /* Connection closed. */
1291 shut = 1;
1294 if (event & EV_WRITE) {
1295 n = msgbuf_write(&ibuf->w);
1296 if (n == -1 && errno != EAGAIN)
1297 fatal("msgbuf_write");
1298 if (n == 0) /* Connection closed. */
1299 shut = 1;
1302 for (;;) {
1303 if ((n = imsg_get(ibuf, &imsg)) == -1)
1304 fatal("%s: imsg_get error", __func__);
1305 if (n == 0) /* No more messages. */
1306 break;
1308 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1309 client->id == 0) {
1310 err = got_error(GOT_ERR_PRIVSEP_MSG);
1311 break;
1314 switch (imsg.hdr.type) {
1315 case GOTD_IMSG_LIST_REFS_INTERNAL:
1316 err = list_refs(&imsg);
1317 if (err)
1318 log_warnx("%s: ls-refs: %s", repo_write.title,
1319 err->msg);
1320 break;
1321 case GOTD_IMSG_REF_UPDATE:
1322 err = recv_ref_update(&imsg);
1323 if (err)
1324 log_warnx("%s: ref-update: %s",
1325 repo_write.title, err->msg);
1326 break;
1327 case GOTD_IMSG_PACKFILE_PIPE:
1328 err = receive_pack_pipe(&imsg, iev);
1329 if (err) {
1330 log_warnx("%s: receiving pack pipe: %s",
1331 repo_write.title, err->msg);
1332 break;
1334 break;
1335 case GOTD_IMSG_PACKIDX_FILE:
1336 err = receive_pack_idx(&imsg, iev);
1337 if (err) {
1338 log_warnx("%s: receiving pack index: %s",
1339 repo_write.title, err->msg);
1340 break;
1342 break;
1343 case GOTD_IMSG_RECV_PACKFILE:
1344 err = recv_packfile(&have_packfile, &imsg);
1345 if (err) {
1346 log_warnx("%s: receive packfile: %s",
1347 repo_write.title, err->msg);
1348 break;
1350 if (have_packfile) {
1351 err = verify_packfile();
1352 if (err) {
1353 log_warnx("%s: verify packfile: %s",
1354 repo_write.title, err->msg);
1355 break;
1357 err = install_packfile(iev);
1358 if (err) {
1359 log_warnx("%s: install packfile: %s",
1360 repo_write.title, err->msg);
1361 break;
1364 err = update_refs(iev);
1365 if (err) {
1366 log_warnx("%s: update refs: %s",
1367 repo_write.title, err->msg);
1369 break;
1370 case GOTD_IMSG_DISCONNECT:
1371 err = recv_disconnect(&imsg);
1372 if (err)
1373 log_warnx("%s: disconnect: %s",
1374 repo_write.title, err->msg);
1375 shut = 1;
1376 break;
1377 default:
1378 log_debug("%s: unexpected imsg %d", repo_write.title,
1379 imsg.hdr.type);
1380 break;
1383 imsg_free(&imsg);
1386 if (!shut && check_cancelled(NULL) == NULL) {
1387 if (err &&
1388 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1389 client->id, err) == -1) {
1390 log_warnx("could not send error to parent: %s",
1391 err->msg);
1393 gotd_imsg_event_add(iev);
1394 } else {
1395 /* This pipe is dead. Remove its event handler */
1396 event_del(&iev->ev);
1397 event_loopexit(NULL);
1401 static const struct got_error *
1402 recv_connect(struct imsg *imsg)
1404 struct gotd_imsgev *iev = &repo_write.session_iev;
1405 size_t datalen;
1407 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1408 if (datalen != 0)
1409 return got_error(GOT_ERR_PRIVSEP_LEN);
1410 if (imsg->fd == -1)
1411 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1413 if (repo_write.session_fd != -1)
1414 return got_error(GOT_ERR_PRIVSEP_MSG);
1416 repo_write.session_fd = imsg->fd;
1418 imsg_init(&iev->ibuf, repo_write.session_fd);
1419 iev->handler = repo_write_dispatch_session;
1420 iev->events = EV_READ;
1421 iev->handler_arg = NULL;
1422 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1423 repo_write_dispatch_session, iev);
1424 gotd_imsg_event_add(iev);
1426 return NULL;
1429 static void
1430 repo_write_dispatch(int fd, short event, void *arg)
1432 const struct got_error *err = NULL;
1433 struct gotd_imsgev *iev = arg;
1434 struct imsgbuf *ibuf = &iev->ibuf;
1435 struct imsg imsg;
1436 ssize_t n;
1437 int shut = 0;
1438 struct repo_write_client *client = &repo_write_client;
1440 if (event & EV_READ) {
1441 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1442 fatal("imsg_read error");
1443 if (n == 0) /* Connection closed. */
1444 shut = 1;
1447 if (event & EV_WRITE) {
1448 n = msgbuf_write(&ibuf->w);
1449 if (n == -1 && errno != EAGAIN)
1450 fatal("msgbuf_write");
1451 if (n == 0) /* Connection closed. */
1452 shut = 1;
1455 while (err == NULL && check_cancelled(NULL) == NULL) {
1456 if ((n = imsg_get(ibuf, &imsg)) == -1)
1457 fatal("%s: imsg_get", __func__);
1458 if (n == 0) /* No more messages. */
1459 break;
1461 switch (imsg.hdr.type) {
1462 case GOTD_IMSG_CONNECT_REPO_CHILD:
1463 err = recv_connect(&imsg);
1464 break;
1465 default:
1466 log_debug("%s: unexpected imsg %d", repo_write.title,
1467 imsg.hdr.type);
1468 break;
1471 imsg_free(&imsg);
1474 if (!shut && check_cancelled(NULL) == NULL) {
1475 if (err &&
1476 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1477 client->id, err) == -1) {
1478 log_warnx("could not send error to parent: %s",
1479 err->msg);
1481 gotd_imsg_event_add(iev);
1482 } else {
1483 /* This pipe is dead. Remove its event handler */
1484 event_del(&iev->ev);
1485 event_loopexit(NULL);
1489 void
1490 repo_write_main(const char *title, const char *repo_path,
1491 int *pack_fds, int *temp_fds)
1493 const struct got_error *err = NULL;
1494 struct gotd_imsgev iev;
1496 repo_write.title = title;
1497 repo_write.pid = getpid();
1498 repo_write.pack_fds = pack_fds;
1499 repo_write.temp_fds = temp_fds;
1500 repo_write.session_fd = -1;
1501 repo_write.session_iev.ibuf.fd = -1;
1503 STAILQ_INIT(&repo_write_client.ref_updates);
1505 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1506 if (err)
1507 goto done;
1508 if (!got_repo_is_bare(repo_write.repo)) {
1509 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1510 "bare git repository required");
1511 goto done;
1514 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1516 signal(SIGINT, catch_sigint);
1517 signal(SIGTERM, catch_sigterm);
1518 signal(SIGPIPE, SIG_IGN);
1519 signal(SIGHUP, SIG_IGN);
1521 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1522 iev.handler = repo_write_dispatch;
1523 iev.events = EV_READ;
1524 iev.handler_arg = NULL;
1525 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1526 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1527 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1528 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1529 goto done;
1532 event_dispatch();
1533 done:
1534 if (err)
1535 log_warnx("%s: %s", title, err->msg);
1536 repo_write_shutdown();
1539 void
1540 repo_write_shutdown(void)
1542 log_debug("%s: shutting down", repo_write.title);
1543 if (repo_write.repo)
1544 got_repo_close(repo_write.repo);
1545 got_repo_pack_fds_close(repo_write.pack_fds);
1546 got_repo_temp_fds_close(repo_write.temp_fds);
1547 if (repo_write.session_fd != -1)
1548 close(repo_write.session_fd);
1549 exit(0);