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 = dup(repo_tempfiles[i].fd);
942 FILE *f;
943 if (fd == -1) {
944 err = got_error_from_errno("dup");
945 goto done;
947 f = fdopen(fd, "w+");
948 if (f == NULL) {
949 err = got_error_from_errno("dup");
950 close(fd);
951 goto done;
953 tempfiles[i] = f;
956 err = gotd_imsg_flush(&ibuf);
957 if (err)
958 goto done;
960 log_debug("receiving pack data");
961 unpack_err = recv_packdata(&pack_filesize, &nobj,
962 client->pack_sha1, client->pack_pipe, pack->fd);
963 if (ireq.report_status) {
964 err = report_pack_status(unpack_err);
965 if (err) {
966 /* Git clients hang up after sending the pack file. */
967 if (err->code == GOT_ERR_EOF)
968 err = NULL;
971 if (unpack_err)
972 err = unpack_err;
973 if (err)
974 goto done;
976 log_debug("pack data received");
978 /*
979 * Clients which are creating new references only will
980 * send us an empty pack file.
981 */
982 if (nobj == 0 &&
983 pack_filesize == sizeof(struct got_packfile_hdr) &&
984 client->nref_updates > 0 &&
985 client->nref_updates == client->nref_new)
986 goto done;
988 /*
989 * Clients which are deleting references only will send
990 * no pack file.
991 */
992 if (nobj == 0 &&
993 client->nref_del > 0 &&
994 client->nref_updates == client->nref_del)
995 goto done;
997 pack->filesize = pack_filesize;
998 *have_packfile = 1;
1000 log_debug("begin indexing pack (%lld bytes in size)",
1001 (long long)pack->filesize);
1002 err = got_pack_index(pack, client->packidx_fd,
1003 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1004 pack_index_progress, NULL, &rl);
1005 if (err)
1006 goto done;
1007 log_debug("done indexing pack");
1009 if (fsync(client->packidx_fd) == -1) {
1010 err = got_error_from_errno("fsync");
1011 goto done;
1013 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1014 err = got_error_from_errno("lseek");
1015 done:
1016 if (close(client->pack_pipe) == -1 && err == NULL)
1017 err = got_error_from_errno("close");
1018 client->pack_pipe = -1;
1019 for (i = 0; i < nitems(repo_tempfiles); i++) {
1020 struct repo_tempfile *t = &repo_tempfiles[i];
1021 if (t->idx != -1)
1022 got_repo_temp_fds_put(t->idx, repo_write.repo);
1024 for (i = 0; i < nitems(tempfiles); i++) {
1025 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1026 err = got_error_from_errno("fclose");
1028 if (err)
1029 got_pack_close(pack);
1030 imsg_clear(&ibuf);
1031 return err;
1034 static const struct got_error *
1035 verify_packfile(void)
1037 const struct got_error *err = NULL, *close_err;
1038 struct repo_write_client *client = &repo_write_client;
1039 struct gotd_ref_update *ref_update;
1040 struct got_packidx *packidx = NULL;
1041 struct stat sb;
1042 char *id_str = NULL;
1043 int idx = -1;
1045 if (STAILQ_EMPTY(&client->ref_updates)) {
1046 return got_error_msg(GOT_ERR_BAD_REQUEST,
1047 "cannot verify pack file without any ref-updates");
1050 if (client->pack.fd == -1) {
1051 return got_error_msg(GOT_ERR_BAD_REQUEST,
1052 "invalid pack file handle during pack verification");
1054 if (client->packidx_fd == -1) {
1055 return got_error_msg(GOT_ERR_BAD_REQUEST,
1056 "invalid pack index handle during pack verification");
1059 if (fstat(client->packidx_fd, &sb) == -1)
1060 return got_error_from_errno("pack index fstat");
1062 packidx = malloc(sizeof(*packidx));
1063 memset(packidx, 0, sizeof(*packidx));
1064 packidx->fd = client->packidx_fd;
1065 client->packidx_fd = -1;
1066 packidx->len = sb.st_size;
1068 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1069 if (err)
1070 return err;
1072 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1073 if (ref_update->delete_ref)
1074 continue;
1076 err = got_object_id_str(&id_str, &ref_update->new_id);
1077 if (err)
1078 goto done;
1080 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1081 if (idx == -1) {
1082 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1083 "advertised object %s is missing from pack file",
1084 id_str);
1085 goto done;
1089 done:
1090 close_err = got_packidx_close(packidx);
1091 if (close_err && err == NULL)
1092 err = close_err;
1093 free(id_str);
1094 return err;
1097 static const struct got_error *
1098 install_packfile(struct gotd_imsgev *iev)
1100 struct repo_write_client *client = &repo_write_client;
1101 struct gotd_imsg_packfile_install inst;
1102 int ret;
1104 memset(&inst, 0, sizeof(inst));
1105 inst.client_id = client->id;
1106 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1108 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1109 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1110 if (ret == -1)
1111 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1113 return NULL;
1116 static const struct got_error *
1117 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1119 struct repo_write_client *client = &repo_write_client;
1120 struct gotd_imsg_ref_updates_start istart;
1121 int ret;
1123 memset(&istart, 0, sizeof(istart));
1124 istart.nref_updates = nref_updates;
1125 istart.client_id = client->id;
1127 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1128 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1129 if (ret == -1)
1130 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1132 return NULL;
1136 static const struct got_error *
1137 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1139 struct repo_write_client *client = &repo_write_client;
1140 struct gotd_imsg_ref_update iref;
1141 const char *refname = got_ref_get_name(ref_update->ref);
1142 struct ibuf *wbuf;
1143 size_t len;
1145 memset(&iref, 0, sizeof(iref));
1146 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1147 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1148 iref.ref_is_new = ref_update->ref_is_new;
1149 iref.delete_ref = ref_update->delete_ref;
1150 iref.client_id = client->id;
1151 iref.name_len = strlen(refname);
1153 len = sizeof(iref) + iref.name_len;
1154 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1155 repo_write.pid, len);
1156 if (wbuf == NULL)
1157 return got_error_from_errno("imsg_create REF_UPDATE");
1159 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1160 return got_error_from_errno("imsg_add REF_UPDATE");
1161 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1162 return got_error_from_errno("imsg_add REF_UPDATE");
1164 wbuf->fd = -1;
1165 imsg_close(&iev->ibuf, wbuf);
1167 gotd_imsg_event_add(iev);
1168 return NULL;
1171 static const struct got_error *
1172 update_refs(struct gotd_imsgev *iev)
1174 const struct got_error *err = NULL;
1175 struct repo_write_client *client = &repo_write_client;
1176 struct gotd_ref_update *ref_update;
1178 err = send_ref_updates_start(client->nref_updates, iev);
1179 if (err)
1180 return err;
1182 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1183 err = send_ref_update(ref_update, iev);
1184 if (err)
1185 goto done;
1187 done:
1188 return err;
1191 static const struct got_error *
1192 recv_disconnect(struct imsg *imsg)
1194 const struct got_error *err = NULL;
1195 struct gotd_imsg_disconnect idisconnect;
1196 size_t datalen;
1197 int pack_pipe = -1, idxfd = -1;
1198 struct repo_write_client *client = &repo_write_client;
1200 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1201 if (datalen != sizeof(idisconnect))
1202 return got_error(GOT_ERR_PRIVSEP_LEN);
1203 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1205 log_debug("client disconnecting");
1207 while (!STAILQ_EMPTY(&client->ref_updates)) {
1208 struct gotd_ref_update *ref_update;
1209 ref_update = STAILQ_FIRST(&client->ref_updates);
1210 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1211 got_ref_close(ref_update->ref);
1212 free(ref_update);
1214 err = got_pack_close(&client->pack);
1215 if (client->fd != -1 && close(client->fd) == -1)
1216 err = got_error_from_errno("close");
1217 pack_pipe = client->pack_pipe;
1218 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1219 err = got_error_from_errno("close");
1220 idxfd = client->packidx_fd;
1221 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1222 err = got_error_from_errno("close");
1223 return err;
1226 static const struct got_error *
1227 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1229 struct repo_write_client *client = &repo_write_client;
1230 struct gotd_imsg_packfile_pipe ireq;
1231 size_t datalen;
1233 log_debug("receving pack pipe descriptor");
1235 if (imsg->fd == -1)
1236 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1238 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1239 if (datalen != sizeof(ireq))
1240 return got_error(GOT_ERR_PRIVSEP_LEN);
1241 memcpy(&ireq, imsg->data, sizeof(ireq));
1243 if (client->pack_pipe != -1)
1244 return got_error(GOT_ERR_PRIVSEP_MSG);
1246 client->pack_pipe = imsg->fd;
1247 return NULL;
1250 static const struct got_error *
1251 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1253 struct repo_write_client *client = &repo_write_client;
1254 struct gotd_imsg_packidx_file ireq;
1255 size_t datalen;
1257 log_debug("receving pack index output file");
1259 if (imsg->fd == -1)
1260 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1262 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1263 if (datalen != sizeof(ireq))
1264 return got_error(GOT_ERR_PRIVSEP_LEN);
1265 memcpy(&ireq, imsg->data, sizeof(ireq));
1267 if (client->packidx_fd != -1)
1268 return got_error(GOT_ERR_PRIVSEP_MSG);
1270 client->packidx_fd = imsg->fd;
1271 return NULL;
1274 static void
1275 repo_write_dispatch_session(int fd, short event, void *arg)
1277 const struct got_error *err = NULL;
1278 struct gotd_imsgev *iev = arg;
1279 struct imsgbuf *ibuf = &iev->ibuf;
1280 struct imsg imsg;
1281 struct repo_write_client *client = &repo_write_client;
1282 ssize_t n;
1283 int shut = 0, have_packfile = 0;
1285 if (event & EV_READ) {
1286 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1287 fatal("imsg_read error");
1288 if (n == 0) /* Connection closed. */
1289 shut = 1;
1292 if (event & EV_WRITE) {
1293 n = msgbuf_write(&ibuf->w);
1294 if (n == -1 && errno != EAGAIN)
1295 fatal("msgbuf_write");
1296 if (n == 0) /* Connection closed. */
1297 shut = 1;
1300 for (;;) {
1301 if ((n = imsg_get(ibuf, &imsg)) == -1)
1302 fatal("%s: imsg_get error", __func__);
1303 if (n == 0) /* No more messages. */
1304 break;
1306 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1307 client->id == 0) {
1308 err = got_error(GOT_ERR_PRIVSEP_MSG);
1309 break;
1312 switch (imsg.hdr.type) {
1313 case GOTD_IMSG_LIST_REFS_INTERNAL:
1314 err = list_refs(&imsg);
1315 if (err)
1316 log_warnx("%s: ls-refs: %s", repo_write.title,
1317 err->msg);
1318 break;
1319 case GOTD_IMSG_REF_UPDATE:
1320 err = recv_ref_update(&imsg);
1321 if (err)
1322 log_warnx("%s: ref-update: %s",
1323 repo_write.title, err->msg);
1324 break;
1325 case GOTD_IMSG_PACKFILE_PIPE:
1326 err = receive_pack_pipe(&imsg, iev);
1327 if (err) {
1328 log_warnx("%s: receiving pack pipe: %s",
1329 repo_write.title, err->msg);
1330 break;
1332 break;
1333 case GOTD_IMSG_PACKIDX_FILE:
1334 err = receive_pack_idx(&imsg, iev);
1335 if (err) {
1336 log_warnx("%s: receiving pack index: %s",
1337 repo_write.title, err->msg);
1338 break;
1340 break;
1341 case GOTD_IMSG_RECV_PACKFILE:
1342 err = recv_packfile(&have_packfile, &imsg);
1343 if (err) {
1344 log_warnx("%s: receive packfile: %s",
1345 repo_write.title, err->msg);
1346 break;
1348 if (have_packfile) {
1349 err = verify_packfile();
1350 if (err) {
1351 log_warnx("%s: verify packfile: %s",
1352 repo_write.title, err->msg);
1353 break;
1355 err = install_packfile(iev);
1356 if (err) {
1357 log_warnx("%s: install packfile: %s",
1358 repo_write.title, err->msg);
1359 break;
1362 err = update_refs(iev);
1363 if (err) {
1364 log_warnx("%s: update refs: %s",
1365 repo_write.title, err->msg);
1367 break;
1368 case GOTD_IMSG_DISCONNECT:
1369 err = recv_disconnect(&imsg);
1370 if (err)
1371 log_warnx("%s: disconnect: %s",
1372 repo_write.title, err->msg);
1373 shut = 1;
1374 break;
1375 default:
1376 log_debug("%s: unexpected imsg %d", repo_write.title,
1377 imsg.hdr.type);
1378 break;
1381 imsg_free(&imsg);
1384 if (!shut && check_cancelled(NULL) == NULL) {
1385 if (err &&
1386 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1387 client->id, err) == -1) {
1388 log_warnx("could not send error to parent: %s",
1389 err->msg);
1391 gotd_imsg_event_add(iev);
1392 } else {
1393 /* This pipe is dead. Remove its event handler */
1394 event_del(&iev->ev);
1395 event_loopexit(NULL);
1399 static const struct got_error *
1400 recv_connect(struct imsg *imsg)
1402 struct gotd_imsgev *iev = &repo_write.session_iev;
1403 size_t datalen;
1405 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1406 if (datalen != 0)
1407 return got_error(GOT_ERR_PRIVSEP_LEN);
1408 if (imsg->fd == -1)
1409 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1411 if (repo_write.session_fd != -1)
1412 return got_error(GOT_ERR_PRIVSEP_MSG);
1414 repo_write.session_fd = imsg->fd;
1416 imsg_init(&iev->ibuf, repo_write.session_fd);
1417 iev->handler = repo_write_dispatch_session;
1418 iev->events = EV_READ;
1419 iev->handler_arg = NULL;
1420 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1421 repo_write_dispatch_session, iev);
1422 gotd_imsg_event_add(iev);
1424 return NULL;
1427 static void
1428 repo_write_dispatch(int fd, short event, void *arg)
1430 const struct got_error *err = NULL;
1431 struct gotd_imsgev *iev = arg;
1432 struct imsgbuf *ibuf = &iev->ibuf;
1433 struct imsg imsg;
1434 ssize_t n;
1435 int shut = 0;
1436 struct repo_write_client *client = &repo_write_client;
1438 if (event & EV_READ) {
1439 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1440 fatal("imsg_read error");
1441 if (n == 0) /* Connection closed. */
1442 shut = 1;
1445 if (event & EV_WRITE) {
1446 n = msgbuf_write(&ibuf->w);
1447 if (n == -1 && errno != EAGAIN)
1448 fatal("msgbuf_write");
1449 if (n == 0) /* Connection closed. */
1450 shut = 1;
1453 while (err == NULL && check_cancelled(NULL) == NULL) {
1454 if ((n = imsg_get(ibuf, &imsg)) == -1)
1455 fatal("%s: imsg_get", __func__);
1456 if (n == 0) /* No more messages. */
1457 break;
1459 switch (imsg.hdr.type) {
1460 case GOTD_IMSG_CONNECT_REPO_CHILD:
1461 err = recv_connect(&imsg);
1462 break;
1463 default:
1464 log_debug("%s: unexpected imsg %d", repo_write.title,
1465 imsg.hdr.type);
1466 break;
1469 imsg_free(&imsg);
1472 if (!shut && check_cancelled(NULL) == NULL) {
1473 if (err &&
1474 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1475 client->id, err) == -1) {
1476 log_warnx("could not send error to parent: %s",
1477 err->msg);
1479 gotd_imsg_event_add(iev);
1480 } else {
1481 /* This pipe is dead. Remove its event handler */
1482 event_del(&iev->ev);
1483 event_loopexit(NULL);
1487 void
1488 repo_write_main(const char *title, const char *repo_path,
1489 int *pack_fds, int *temp_fds)
1491 const struct got_error *err = NULL;
1492 struct gotd_imsgev iev;
1494 repo_write.title = title;
1495 repo_write.pid = getpid();
1496 repo_write.pack_fds = pack_fds;
1497 repo_write.temp_fds = temp_fds;
1498 repo_write.session_fd = -1;
1499 repo_write.session_iev.ibuf.fd = -1;
1501 STAILQ_INIT(&repo_write_client.ref_updates);
1503 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1504 if (err)
1505 goto done;
1506 if (!got_repo_is_bare(repo_write.repo)) {
1507 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1508 "bare git repository required");
1509 goto done;
1512 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1514 signal(SIGINT, catch_sigint);
1515 signal(SIGTERM, catch_sigterm);
1516 signal(SIGPIPE, SIG_IGN);
1517 signal(SIGHUP, SIG_IGN);
1519 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1520 iev.handler = repo_write_dispatch;
1521 iev.events = EV_READ;
1522 iev.handler_arg = NULL;
1523 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1524 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1525 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1526 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1527 goto done;
1530 event_dispatch();
1531 done:
1532 if (err)
1533 log_warnx("%s: %s", title, err->msg);
1534 repo_write_shutdown();
1537 void
1538 repo_write_shutdown(void)
1540 log_debug("%s: shutting down", repo_write.title);
1541 if (repo_write.repo)
1542 got_repo_close(repo_write.repo);
1543 got_repo_pack_fds_close(repo_write.pack_fds);
1544 got_repo_temp_fds_close(repo_write.temp_fds);
1545 if (repo_write.session_fd != -1)
1546 close(repo_write.session_fd);
1547 exit(0);