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 static struct repo_write_client {
82 uint32_t id;
83 int fd;
84 int pack_pipe[2];
85 struct got_pack pack;
86 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
87 int packidx_fd;
88 struct gotd_ref_updates ref_updates;
89 int nref_updates;
90 } repo_write_client;
92 static volatile sig_atomic_t sigint_received;
93 static volatile sig_atomic_t sigterm_received;
95 static void
96 catch_sigint(int signo)
97 {
98 sigint_received = 1;
99 }
101 static void
102 catch_sigterm(int signo)
104 sigterm_received = 1;
107 static const struct got_error *
108 check_cancelled(void *arg)
110 if (sigint_received || sigterm_received)
111 return got_error(GOT_ERR_CANCELLED);
113 return NULL;
116 static const struct got_error *
117 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
118 struct imsgbuf *ibuf)
120 const struct got_error *err = NULL;
121 struct got_tag_object *tag;
122 size_t namelen, len;
123 char *peeled_refname = NULL;
124 struct got_object_id *id;
125 struct ibuf *wbuf;
127 err = got_object_tag_open(&tag, repo_write.repo, obj);
128 if (err)
129 return err;
131 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
132 err = got_error_from_errno("asprintf");
133 goto done;
136 id = got_object_tag_get_object_id(tag);
137 namelen = strlen(peeled_refname);
139 len = sizeof(struct gotd_imsg_ref) + namelen;
140 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
141 err = got_error(GOT_ERR_NO_SPACE);
142 goto done;
145 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
146 repo_write.pid, len);
147 if (wbuf == NULL) {
148 err = got_error_from_errno("imsg_create REF");
149 goto done;
152 /* Keep in sync with struct gotd_imsg_ref definition. */
153 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
154 err = got_error_from_errno("imsg_add REF");
155 goto done;
157 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
158 err = got_error_from_errno("imsg_add REF");
159 goto done;
161 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
162 err = got_error_from_errno("imsg_add REF");
163 goto done;
166 wbuf->fd = -1;
167 imsg_close(ibuf, wbuf);
168 done:
169 got_object_tag_close(tag);
170 return err;
173 static const struct got_error *
174 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
176 const struct got_error *err;
177 const char *refname = got_ref_get_name(ref);
178 size_t namelen;
179 struct got_object_id *id = NULL;
180 struct got_object *obj = NULL;
181 size_t len;
182 struct ibuf *wbuf;
184 namelen = strlen(refname);
186 len = sizeof(struct gotd_imsg_ref) + namelen;
187 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
188 return got_error(GOT_ERR_NO_SPACE);
190 err = got_ref_resolve(&id, repo_write.repo, ref);
191 if (err)
192 return err;
194 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
195 repo_write.pid, len);
196 if (wbuf == NULL) {
197 err = got_error_from_errno("imsg_create REF");
198 goto done;
201 /* Keep in sync with struct gotd_imsg_ref definition. */
202 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
203 return got_error_from_errno("imsg_add REF");
204 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
205 return got_error_from_errno("imsg_add REF");
206 if (imsg_add(wbuf, refname, namelen) == -1)
207 return got_error_from_errno("imsg_add REF");
209 wbuf->fd = -1;
210 imsg_close(ibuf, wbuf);
212 err = got_object_open(&obj, repo_write.repo, id);
213 if (err)
214 goto done;
215 if (obj->type == GOT_OBJ_TYPE_TAG)
216 err = send_peeled_tag_ref(ref, obj, ibuf);
217 done:
218 if (obj)
219 got_object_close(obj);
220 free(id);
221 return err;
224 static const struct got_error *
225 list_refs(struct imsg *imsg)
227 const struct got_error *err;
228 struct repo_write_client *client = &repo_write_client;
229 struct got_reflist_head refs;
230 struct got_reflist_entry *re;
231 struct gotd_imsg_list_refs_internal ireq;
232 size_t datalen;
233 struct gotd_imsg_reflist irefs;
234 struct imsgbuf ibuf;
235 int client_fd = imsg->fd;
237 TAILQ_INIT(&refs);
239 if (client_fd == -1)
240 return got_error(GOT_ERR_PRIVSEP_NO_FD);
242 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
243 if (datalen != sizeof(ireq))
244 return got_error(GOT_ERR_PRIVSEP_LEN);
245 memcpy(&ireq, imsg->data, sizeof(ireq));
247 if (ireq.client_id == 0)
248 return got_error(GOT_ERR_CLIENT_ID);
249 if (client->id != 0) {
250 return got_error_msg(GOT_ERR_CLIENT_ID,
251 "duplicate list-refs request");
253 client->id = ireq.client_id;
254 client->fd = client_fd;
255 client->pack_pipe = -1;
256 client->packidx_fd = -1;
257 client->nref_updates = 0;
259 imsg_init(&ibuf, client_fd);
261 err = got_ref_list(&refs, repo_write.repo, "",
262 got_ref_cmp_by_name, NULL);
263 if (err)
264 return err;
266 memset(&irefs, 0, sizeof(irefs));
267 TAILQ_FOREACH(re, &refs, entry) {
268 struct got_object_id *id;
269 int obj_type;
271 if (got_ref_is_symbolic(re->ref))
272 continue;
274 irefs.nrefs++;
276 /* Account for a peeled tag refs. */
277 err = got_ref_resolve(&id, repo_write.repo, re->ref);
278 if (err)
279 goto done;
280 err = got_object_get_type(&obj_type, repo_write.repo, id);
281 free(id);
282 if (err)
283 goto done;
284 if (obj_type == GOT_OBJ_TYPE_TAG)
285 irefs.nrefs++;
288 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
289 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
290 err = got_error_from_errno("imsg_compose REFLIST");
291 goto done;
294 TAILQ_FOREACH(re, &refs, entry) {
295 if (got_ref_is_symbolic(re->ref))
296 continue;
297 err = send_ref(re->ref, &ibuf);
298 if (err)
299 goto done;
302 err = gotd_imsg_flush(&ibuf);
303 done:
304 got_ref_list_free(&refs);
305 imsg_clear(&ibuf);
306 return err;
309 static const struct got_error *
310 protect_ref_namespace(struct got_reference *ref, const char *namespace)
312 size_t len = strlen(namespace);
314 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
315 namespace[len -1] != '/') {
316 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
317 "reference namespace '%s'", namespace);
320 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
321 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
323 return NULL;
326 static const struct got_error *
327 recv_ref_update(struct imsg *imsg)
329 const struct got_error *err = NULL;
330 struct repo_write_client *client = &repo_write_client;
331 struct gotd_imsg_ref_update iref;
332 size_t datalen;
333 char *refname = NULL;
334 struct got_reference *ref = NULL;
335 struct got_object_id *id = NULL;
336 struct imsgbuf ibuf;
337 struct gotd_ref_update *ref_update = NULL;
339 log_debug("ref-update received");
341 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
342 if (datalen < sizeof(iref))
343 return got_error(GOT_ERR_PRIVSEP_LEN);
344 memcpy(&iref, imsg->data, sizeof(iref));
345 if (datalen != sizeof(iref) + iref.name_len)
346 return got_error(GOT_ERR_PRIVSEP_LEN);
348 imsg_init(&ibuf, client->fd);
350 refname = malloc(iref.name_len + 1);
351 if (refname == NULL)
352 return got_error_from_errno("malloc");
353 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
354 refname[iref.name_len] = '\0';
356 ref_update = calloc(1, sizeof(*ref_update));
357 if (ref_update == NULL) {
358 err = got_error_from_errno("malloc");
359 goto done;
362 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
363 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
365 err = got_ref_open(&ref, repo_write.repo, refname, 0);
366 if (err) {
367 if (err->code != GOT_ERR_NOT_REF)
368 goto done;
369 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
370 if (err)
371 goto done;
372 ref_update->ref_is_new = 1;
374 if (got_ref_is_symbolic(ref)) {
375 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
376 "'%s' is a symbolic reference and cannot "
377 "be updated", got_ref_get_name(ref));
378 goto done;
380 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
381 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
382 "%s: does not begin with 'refs/'",
383 got_ref_get_name(ref));
384 goto done;
387 err = protect_ref_namespace(ref, "refs/got/");
388 if (err)
389 goto done;
390 err = protect_ref_namespace(ref, "refs/remotes/");
391 if (err)
392 goto done;
394 if (!ref_update->ref_is_new) {
395 /*
396 * Ensure the client's idea of this update is still valid.
397 * At this point we can only return an error, to prevent
398 * the client from uploading a pack file which will likely
399 * have to be discarded.
400 */
401 err = got_ref_resolve(&id, repo_write.repo, ref);
402 if (err)
403 goto done;
405 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
406 err = got_error_fmt(GOT_ERR_REF_BUSY,
407 "%s has been modified by someone else "
408 "while transaction was in progress",
409 got_ref_get_name(ref));
410 goto done;
414 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
415 repo_write.pid);
417 ref_update->ref = ref;
418 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
419 client->nref_updates++;
420 ref = NULL;
421 ref_update = NULL;
422 done:
423 if (ref)
424 got_ref_close(ref);
425 free(ref_update);
426 free(refname);
427 free(id);
428 return err;
431 static const struct got_error *
432 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
433 uint32_t nobj_loose, uint32_t nobj_resolved)
435 int p_indexed = 0, p_resolved = 0;
436 int nobj_delta = nobj_total - nobj_loose;
438 if (nobj_total > 0)
439 p_indexed = (nobj_indexed * 100) / nobj_total;
441 if (nobj_delta > 0)
442 p_resolved = (nobj_resolved * 100) / nobj_delta;
444 if (p_resolved > 0) {
445 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
446 nobj_total, p_indexed, nobj_delta, p_resolved);
447 } else
448 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
450 return NULL;
453 static const struct got_error *
454 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
456 const struct got_error *err = NULL;
457 uint8_t readahead[65536];
458 size_t have, newlen;
460 err = got_poll_read_full(infd, &have,
461 readahead, sizeof(readahead), minsize);
462 if (err)
463 return err;
465 err = buf_append(&newlen, buf, readahead, have);
466 if (err)
467 return err;
468 return NULL;
471 static const struct got_error *
472 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
473 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
475 const struct got_error *err = NULL;
476 uint8_t t = 0;
477 uint64_t s = 0;
478 uint8_t sizebuf[8];
479 size_t i = 0;
480 off_t obj_offset = *outsize;
482 do {
483 /* We do not support size values which don't fit in 64 bit. */
484 if (i > 9)
485 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
486 "packfile offset %lld", (long long)obj_offset);
488 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
489 err = read_more_pack_stream(infd, buf,
490 sizeof(sizebuf[0]));
491 if (err)
492 return err;
495 sizebuf[i] = buf_getc(buf, *buf_pos);
496 *buf_pos += sizeof(sizebuf[i]);
498 if (i == 0) {
499 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
500 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
501 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
502 } else {
503 size_t shift = 4 + 7 * (i - 1);
504 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
505 shift);
507 i++;
508 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
510 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
511 if (err)
512 return err;
513 *outsize += i;
515 *type = t;
516 *size = s;
517 return NULL;
520 static const struct got_error *
521 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
522 SHA1_CTX *ctx)
524 const struct got_error *err = NULL;
525 size_t remain = buf_len(buf) - *buf_pos;
527 if (remain < SHA1_DIGEST_LENGTH) {
528 err = read_more_pack_stream(infd, buf,
529 SHA1_DIGEST_LENGTH - remain);
530 if (err)
531 return err;
534 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
535 SHA1_DIGEST_LENGTH, ctx);
536 if (err)
537 return err;
539 *buf_pos += SHA1_DIGEST_LENGTH;
540 return NULL;
543 static const struct got_error *
544 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
545 SHA1_CTX *ctx)
547 const struct got_error *err = NULL;
548 uint64_t o = 0;
549 uint8_t offbuf[8];
550 size_t i = 0;
551 off_t obj_offset = *outsize;
553 do {
554 /* We do not support offset values which don't fit in 64 bit. */
555 if (i > 8)
556 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
557 "packfile offset %lld", (long long)obj_offset);
559 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
560 err = read_more_pack_stream(infd, buf,
561 sizeof(offbuf[0]));
562 if (err)
563 return err;
566 offbuf[i] = buf_getc(buf, *buf_pos);
567 *buf_pos += sizeof(offbuf[i]);
569 if (i == 0)
570 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
571 else {
572 o++;
573 o <<= 7;
574 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
576 i++;
577 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
579 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
580 return got_error(GOT_ERR_PACK_OFFSET);
582 err = got_pack_hwrite(outfd, offbuf, i, ctx);
583 if (err)
584 return err;
586 *outsize += i;
587 return NULL;
590 static const struct got_error *
591 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
592 SHA1_CTX *ctx)
594 const struct got_error *err = NULL;
595 z_stream z;
596 int zret;
597 char voidbuf[1024];
598 size_t consumed_total = 0;
599 off_t zstream_offset = *outsize;
601 memset(&z, 0, sizeof(z));
603 z.zalloc = Z_NULL;
604 z.zfree = Z_NULL;
605 zret = inflateInit(&z);
606 if (zret != Z_OK) {
607 if (zret == Z_ERRNO)
608 return got_error_from_errno("inflateInit");
609 if (zret == Z_MEM_ERROR) {
610 errno = ENOMEM;
611 return got_error_from_errno("inflateInit");
613 return got_error_msg(GOT_ERR_DECOMPRESSION,
614 "inflateInit failed");
617 while (zret != Z_STREAM_END) {
618 size_t last_total_in, consumed;
620 /*
621 * Decompress into the void. Object data will be parsed
622 * later, when the pack file is indexed. For now, we just
623 * want to locate the end of the compressed stream.
624 */
625 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
626 last_total_in = z.total_in;
627 z.next_in = buf_get(buf) + *buf_pos;
628 z.avail_in = buf_len(buf) - *buf_pos;
629 z.next_out = voidbuf;
630 z.avail_out = sizeof(voidbuf);
632 zret = inflate(&z, Z_SYNC_FLUSH);
633 if (zret != Z_OK && zret != Z_BUF_ERROR &&
634 zret != Z_STREAM_END) {
635 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
636 "packfile offset %lld",
637 (long long)zstream_offset);
638 goto done;
640 consumed = z.total_in - last_total_in;
642 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
643 consumed, ctx);
644 if (err)
645 goto done;
647 err = buf_discard(buf, *buf_pos + consumed);
648 if (err)
649 goto done;
650 *buf_pos = 0;
652 consumed_total += consumed;
655 if (zret != Z_STREAM_END) {
656 err = read_more_pack_stream(infd, buf, 1);
657 if (err)
658 goto done;
662 if (err == NULL)
663 *outsize += consumed_total;
664 done:
665 inflateEnd(&z);
666 return err;
669 static const struct got_error *
670 validate_object_type(int obj_type)
672 switch (obj_type) {
673 case GOT_OBJ_TYPE_BLOB:
674 case GOT_OBJ_TYPE_COMMIT:
675 case GOT_OBJ_TYPE_TREE:
676 case GOT_OBJ_TYPE_TAG:
677 case GOT_OBJ_TYPE_REF_DELTA:
678 case GOT_OBJ_TYPE_OFFSET_DELTA:
679 return NULL;
680 default:
681 break;
684 return got_error(GOT_ERR_OBJ_TYPE);
687 static const struct got_error *
688 recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
690 const struct got_error *err;
691 struct got_packfile_hdr hdr;
692 size_t have;
693 uint32_t nobj, nhave = 0;
694 SHA1_CTX ctx;
695 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
696 char hex[SHA1_DIGEST_STRING_LENGTH];
697 BUF *buf = NULL;
698 size_t buf_pos = 0, remain;
699 ssize_t w;
701 *outsize = 0;
702 SHA1Init(&ctx);
704 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
705 if (err)
706 return err;
707 if (have != sizeof(hdr))
708 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
709 *outsize += have;
711 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
712 return got_error_msg(GOT_ERR_BAD_PACKFILE,
713 "bad packfile signature");
714 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
715 return got_error_msg(GOT_ERR_BAD_PACKFILE,
716 "bad packfile version");
718 nobj = be32toh(hdr.nobjects);
719 if (nobj == 0)
720 return got_error_msg(GOT_ERR_BAD_PACKFILE,
721 "bad packfile with zero objects");
723 log_debug("expecting %d objects", nobj);
725 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
726 if (err)
727 return err;
729 err = buf_alloc(&buf, 65536);
730 if (err)
731 return err;
733 while (nhave != nobj) {
734 uint8_t obj_type;
735 uint64_t obj_size;
737 err = copy_object_type_and_size(&obj_type, &obj_size,
738 infd, outfd, outsize, buf, &buf_pos, &ctx);
739 if (err)
740 goto done;
742 err = validate_object_type(obj_type);
743 if (err)
744 goto done;
746 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
747 err = copy_ref_delta(infd, outfd, outsize,
748 buf, &buf_pos, &ctx);
749 if (err)
750 goto done;
751 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
752 err = copy_offset_delta(infd, outfd, outsize,
753 buf, &buf_pos, &ctx);
754 if (err)
755 goto done;
758 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
759 if (err)
760 goto done;
762 nhave++;
765 log_debug("received %u objects", nobj);
767 SHA1Final(expected_sha1, &ctx);
769 remain = buf_len(buf) - buf_pos;
770 if (remain < SHA1_DIGEST_LENGTH) {
771 err = read_more_pack_stream(infd, buf,
772 SHA1_DIGEST_LENGTH - remain);
773 if (err)
774 return err;
777 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
778 log_debug("expect SHA1: %s", hex);
779 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
780 log_debug("actual SHA1: %s", hex);
782 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
783 SHA1_DIGEST_LENGTH) != 0) {
784 err = got_error(GOT_ERR_PACKFILE_CSUM);
785 goto done;
788 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
790 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
791 if (w == -1) {
792 err = got_error_from_errno("write");
793 goto done;
795 if (w != SHA1_DIGEST_LENGTH) {
796 err = got_error(GOT_ERR_IO);
797 goto done;
800 *outsize += SHA1_DIGEST_LENGTH;
802 if (fsync(outfd) == -1) {
803 err = got_error_from_errno("fsync");
804 goto done;
806 if (lseek(outfd, 0L, SEEK_SET) == -1) {
807 err = got_error_from_errno("lseek");
808 goto done;
810 done:
811 buf_free(buf);
812 return err;
815 static const struct got_error *
816 report_pack_status(const struct got_error *unpack_err)
818 const struct got_error *err = NULL;
819 struct repo_write_client *client = &repo_write_client;
820 struct gotd_imsg_packfile_status istatus;
821 struct ibuf *wbuf;
822 struct imsgbuf ibuf;
823 const char *unpack_ok = "unpack ok\n";
824 size_t len;
826 imsg_init(&ibuf, client->fd);
828 if (unpack_err)
829 istatus.reason_len = strlen(unpack_err->msg);
830 else
831 istatus.reason_len = strlen(unpack_ok);
833 len = sizeof(istatus) + istatus.reason_len;
834 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
835 repo_write.pid, len);
836 if (wbuf == NULL) {
837 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
838 goto done;
841 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
842 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
843 goto done;
846 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
847 istatus.reason_len) == -1) {
848 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
849 goto done;
852 wbuf->fd = -1;
853 imsg_close(&ibuf, wbuf);
855 err = gotd_imsg_flush(&ibuf);
856 done:
857 imsg_clear(&ibuf);
858 return err;
861 static const struct got_error *
862 recv_packfile(struct imsg *imsg)
864 const struct got_error *err = NULL, *unpack_err;
865 struct repo_write_client *client = &repo_write_client;
866 struct gotd_imsg_recv_packfile ireq;
867 FILE *tempfiles[3] = { NULL, NULL, NULL };
868 struct repo_tempfile {
869 int fd;
870 int idx;
871 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
872 int i;
873 size_t datalen;
874 struct imsgbuf ibuf;
875 struct got_ratelimit rl;
876 struct got_pack *pack = NULL;
877 off_t pack_filesize = 0;
879 log_debug("packfile request received");
881 got_ratelimit_init(&rl, 2, 0);
883 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
884 if (datalen != sizeof(ireq))
885 return got_error(GOT_ERR_PRIVSEP_LEN);
886 memcpy(&ireq, imsg->data, sizeof(ireq));
888 if (client->pack_pipe == -1 || client->packidx_fd == -1)
889 return got_error(GOT_ERR_PRIVSEP_NO_FD);
891 imsg_init(&ibuf, client->fd);
893 if (imsg->fd == -1)
894 return got_error(GOT_ERR_PRIVSEP_NO_FD);
896 pack = &client->pack;
897 memset(pack, 0, sizeof(*pack));
898 pack->fd = imsg->fd;
899 err = got_delta_cache_alloc(&pack->delta_cache);
900 if (err)
901 return err;
903 for (i = 0; i < nitems(repo_tempfiles); i++) {
904 struct repo_tempfile *t = &repo_tempfiles[i];
905 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
906 if (err)
907 goto done;
910 for (i = 0; i < nitems(tempfiles); i++) {
911 int fd = dup(repo_tempfiles[i].fd);
912 FILE *f;
913 if (fd == -1) {
914 err = got_error_from_errno("dup");
915 goto done;
917 f = fdopen(fd, "w+");
918 if (f == NULL) {
919 err = got_error_from_errno("dup");
920 close(fd);
921 goto done;
923 tempfiles[i] = f;
926 /* Send pack file pipe to gotsh(1). */
927 if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
928 repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
929 (*client)->pack_pipe[1] = -1;
930 err = got_error_from_errno("imsg_compose ACK");
931 if (err)
932 goto done;
934 (*client)->pack_pipe[1] = -1;
935 err = gotd_imsg_flush(&ibuf);
936 if (err)
937 goto done;
939 log_debug("receiving pack data");
940 unpack_err = recv_packdata(&pack_filesize, client->pack_sha1,
941 client->pack_pipe, pack->fd);
942 if (ireq.report_status) {
943 err = report_pack_status(unpack_err);
944 if (err) {
945 /* Git clients hang up after sending the pack file. */
946 if (err->code == GOT_ERR_EOF)
947 err = NULL;
950 if (unpack_err)
951 err = unpack_err;
952 if (err)
953 goto done;
955 log_debug("pack data received");
957 pack->filesize = pack_filesize;
959 log_debug("begin indexing pack (%lld bytes in size)",
960 (long long)pack->filesize);
961 err = got_pack_index(pack, client->packidx_fd,
962 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
963 pack_index_progress, NULL, &rl);
964 if (err)
965 goto done;
966 log_debug("done indexing pack");
968 if (fsync(client->packidx_fd) == -1) {
969 err = got_error_from_errno("fsync");
970 goto done;
972 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
973 err = got_error_from_errno("lseek");
974 done:
975 if (close(client->pack_pipe) == -1 && err == NULL)
976 err = got_error_from_errno("close");
977 client->pack_pipe = -1;
978 for (i = 0; i < nitems(repo_tempfiles); i++) {
979 struct repo_tempfile *t = &repo_tempfiles[i];
980 if (t->idx != -1)
981 got_repo_temp_fds_put(t->idx, repo_write.repo);
983 for (i = 0; i < nitems(tempfiles); i++) {
984 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
985 err = got_error_from_errno("fclose");
987 if (err)
988 got_pack_close(pack);
989 imsg_clear(&ibuf);
990 return err;
993 static const struct got_error *
994 verify_packfile(void)
996 const struct got_error *err = NULL, *close_err;
997 struct repo_write_client *client = &repo_write_client;
998 struct gotd_ref_update *ref_update;
999 struct got_packidx *packidx = NULL;
1000 struct stat sb;
1001 char *id_str = NULL;
1002 int idx = -1;
1004 if (STAILQ_EMPTY(&client->ref_updates)) {
1005 return got_error_msg(GOT_ERR_BAD_REQUEST,
1006 "cannot verify pack file without any ref-updates");
1009 if (client->pack.fd == -1) {
1010 return got_error_msg(GOT_ERR_BAD_REQUEST,
1011 "invalid pack file handle during pack verification");
1013 if (client->packidx_fd == -1) {
1014 return got_error_msg(GOT_ERR_BAD_REQUEST,
1015 "invalid pack index handle during pack verification");
1018 if (fstat(client->packidx_fd, &sb) == -1)
1019 return got_error_from_errno("pack index fstat");
1021 packidx = malloc(sizeof(*packidx));
1022 memset(packidx, 0, sizeof(*packidx));
1023 packidx->fd = client->packidx_fd;
1024 client->packidx_fd = -1;
1025 packidx->len = sb.st_size;
1027 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1028 if (err)
1029 return err;
1031 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1032 err = got_object_id_str(&id_str, &ref_update->new_id);
1033 if (err)
1034 goto done;
1036 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1037 if (idx == -1) {
1038 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1039 "advertised object %s is missing from pack file",
1040 id_str);
1041 goto done;
1045 done:
1046 close_err = got_packidx_close(packidx);
1047 if (close_err && err == NULL)
1048 err = close_err;
1049 free(id_str);
1050 return err;
1053 static const struct got_error *
1054 install_packfile(struct gotd_imsgev *iev)
1056 struct repo_write_client *client = &repo_write_client;
1057 struct gotd_imsg_packfile_install inst;
1058 int ret;
1060 memset(&inst, 0, sizeof(inst));
1061 inst.client_id = client->id;
1062 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1064 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1065 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1066 if (ret == -1)
1067 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1069 return NULL;
1072 static const struct got_error *
1073 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1075 struct repo_write_client *client = &repo_write_client;
1076 struct gotd_imsg_ref_updates_start istart;
1077 int ret;
1079 memset(&istart, 0, sizeof(istart));
1080 istart.nref_updates = nref_updates;
1081 istart.client_id = client->id;
1083 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1084 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1085 if (ret == -1)
1086 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1088 return NULL;
1092 static const struct got_error *
1093 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1095 struct repo_write_client *client = &repo_write_client;
1096 struct gotd_imsg_ref_update iref;
1097 const char *refname = got_ref_get_name(ref_update->ref);
1098 struct ibuf *wbuf;
1099 size_t len;
1101 memset(&iref, 0, sizeof(iref));
1102 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1103 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1104 iref.ref_is_new = ref_update->ref_is_new;
1105 iref.client_id = client->id;
1106 iref.name_len = strlen(refname);
1108 len = sizeof(iref) + iref.name_len;
1109 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1110 repo_write.pid, len);
1111 if (wbuf == NULL)
1112 return got_error_from_errno("imsg_create REF_UPDATE");
1114 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1115 return got_error_from_errno("imsg_add REF_UPDATE");
1116 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1117 return got_error_from_errno("imsg_add REF_UPDATE");
1119 wbuf->fd = -1;
1120 imsg_close(&iev->ibuf, wbuf);
1122 gotd_imsg_event_add(iev);
1123 return NULL;
1126 static const struct got_error *
1127 update_refs(struct gotd_imsgev *iev)
1129 const struct got_error *err = NULL;
1130 struct repo_write_client *client = &repo_write_client;
1131 struct gotd_ref_update *ref_update;
1133 err = send_ref_updates_start(client->nref_updates, iev);
1134 if (err)
1135 return err;
1137 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1138 err = send_ref_update(ref_update, iev);
1139 if (err)
1140 goto done;
1142 done:
1143 return err;
1146 static const struct got_error *
1147 recv_disconnect(struct imsg *imsg)
1149 const struct got_error *err = NULL;
1150 struct gotd_imsg_disconnect idisconnect;
1151 size_t datalen;
1152 int pack_pipe = -1, idxfd = -1;
1153 struct repo_write_client *client = &repo_write_client;
1155 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1156 if (datalen != sizeof(idisconnect))
1157 return got_error(GOT_ERR_PRIVSEP_LEN);
1158 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1160 log_debug("client disconnecting");
1162 while (!STAILQ_EMPTY(&client->ref_updates)) {
1163 struct gotd_ref_update *ref_update;
1164 ref_update = STAILQ_FIRST(&client->ref_updates);
1165 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1166 got_ref_close(ref_update->ref);
1167 free(ref_update);
1169 err = got_pack_close(&client->pack);
1170 if (client->fd != -1 && close(client->fd) == -1)
1171 err = got_error_from_errno("close");
1172 pack_pipe = client->pack_pipe;
1173 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1174 err = got_error_from_errno("close");
1175 idxfd = client->packidx_fd;
1176 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1177 err = got_error_from_errno("close");
1178 return err;
1181 static const struct got_error *
1182 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1184 struct repo_write_client *client = &repo_write_client;
1185 struct gotd_imsg_packfile_pipe ireq;
1186 size_t datalen;
1188 log_debug("receving pack pipe descriptor");
1190 if (imsg->fd == -1)
1191 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1193 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1194 if (datalen != sizeof(ireq))
1195 return got_error(GOT_ERR_PRIVSEP_LEN);
1196 memcpy(&ireq, imsg->data, sizeof(ireq));
1198 if (client->pack_pipe != -1)
1199 return got_error(GOT_ERR_PRIVSEP_MSG);
1201 client->pack_pipe = imsg->fd;
1202 return NULL;
1205 static const struct got_error *
1206 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1208 struct repo_write_client *client = &repo_write_client;
1209 struct gotd_imsg_packidx_file ireq;
1210 size_t datalen;
1212 log_debug("receving pack index output file");
1214 if (imsg->fd == -1)
1215 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1217 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1218 if (datalen != sizeof(ireq))
1219 return got_error(GOT_ERR_PRIVSEP_LEN);
1220 memcpy(&ireq, imsg->data, sizeof(ireq));
1222 if (client->packidx_fd != -1)
1223 return got_error(GOT_ERR_PRIVSEP_MSG);
1225 client->packidx_fd = imsg->fd;
1226 return NULL;
1229 static void
1230 repo_write_dispatch(int fd, short event, void *arg)
1232 const struct got_error *err = NULL;
1233 struct gotd_imsgev *iev = arg;
1234 struct imsgbuf *ibuf = &iev->ibuf;
1235 struct imsg imsg;
1236 struct repo_write_client *client = &repo_write_client;
1237 ssize_t n;
1238 int shut = 0;
1240 if (event & EV_READ) {
1241 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1242 fatal("imsg_read error");
1243 if (n == 0) /* Connection closed. */
1244 shut = 1;
1247 if (event & EV_WRITE) {
1248 n = msgbuf_write(&ibuf->w);
1249 if (n == -1 && errno != EAGAIN)
1250 fatal("msgbuf_write");
1251 if (n == 0) /* Connection closed. */
1252 shut = 1;
1255 for (;;) {
1256 if ((n = imsg_get(ibuf, &imsg)) == -1)
1257 fatal("%s: imsg_get error", __func__);
1258 if (n == 0) /* No more messages. */
1259 break;
1261 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1262 client->id == 0) {
1263 err = got_error(GOT_ERR_PRIVSEP_MSG);
1264 break;
1267 switch (imsg.hdr.type) {
1268 case GOTD_IMSG_LIST_REFS_INTERNAL:
1269 err = list_refs(&imsg);
1270 if (err)
1271 log_warnx("%s: ls-refs: %s", repo_write.title,
1272 err->msg);
1273 break;
1274 case GOTD_IMSG_REF_UPDATE:
1275 err = recv_ref_update(&imsg);
1276 if (err)
1277 log_warnx("%s: ref-update: %s",
1278 repo_write.title, err->msg);
1279 break;
1280 case GOTD_IMSG_PACKFILE_PIPE:
1281 err = receive_pack_pipe(&imsg, iev);
1282 if (err) {
1283 log_warnx("%s: receiving pack pipe: %s",
1284 repo_write.title, err->msg);
1285 break;
1287 break;
1288 case GOTD_IMSG_PACKIDX_FILE:
1289 err = receive_pack_idx(&imsg, iev);
1290 if (err) {
1291 log_warnx("%s: receiving pack index: %s",
1292 repo_write.title, err->msg);
1293 break;
1295 break;
1296 case GOTD_IMSG_RECV_PACKFILE:
1297 err = recv_packfile(&imsg);
1298 if (err) {
1299 log_warnx("%s: receive packfile: %s",
1300 repo_write.title, err->msg);
1301 break;
1303 err = verify_packfile();
1304 if (err) {
1305 log_warnx("%s: verify packfile: %s",
1306 repo_write.title, err->msg);
1307 break;
1309 err = install_packfile(iev);
1310 if (err) {
1311 log_warnx("%s: install packfile: %s",
1312 repo_write.title, err->msg);
1313 break;
1315 err = update_refs(iev);
1316 if (err) {
1317 log_warnx("%s: update refs: %s",
1318 repo_write.title, err->msg);
1320 break;
1321 case GOTD_IMSG_DISCONNECT:
1322 err = recv_disconnect(&imsg);
1323 if (err)
1324 log_warnx("%s: disconnect: %s",
1325 repo_write.title, err->msg);
1326 shut = 1;
1327 break;
1328 default:
1329 log_debug("%s: unexpected imsg %d", repo_write.title,
1330 imsg.hdr.type);
1331 break;
1334 imsg_free(&imsg);
1337 if (!shut && check_cancelled(NULL) == NULL) {
1338 if (err &&
1339 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1340 client->id, err) == -1) {
1341 log_warnx("could not send error to parent: %s",
1342 err->msg);
1344 gotd_imsg_event_add(iev);
1345 } else {
1346 /* This pipe is dead. Remove its event handler */
1347 event_del(&iev->ev);
1348 event_loopexit(NULL);
1352 void
1353 repo_write_main(const char *title, const char *repo_path,
1354 int *pack_fds, int *temp_fds)
1356 const struct got_error *err = NULL;
1357 struct gotd_imsgev iev;
1359 repo_write.title = title;
1360 repo_write.pid = getpid();
1361 repo_write.pack_fds = pack_fds;
1362 repo_write.temp_fds = temp_fds;
1364 STAILQ_INIT(&repo_write_client.ref_updates);
1366 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1367 if (err)
1368 goto done;
1369 if (!got_repo_is_bare(repo_write.repo)) {
1370 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1371 "bare git repository required");
1372 goto done;
1375 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1377 signal(SIGINT, catch_sigint);
1378 signal(SIGTERM, catch_sigterm);
1379 signal(SIGPIPE, SIG_IGN);
1380 signal(SIGHUP, SIG_IGN);
1382 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1383 iev.handler = repo_write_dispatch;
1384 iev.events = EV_READ;
1385 iev.handler_arg = NULL;
1386 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1387 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1388 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1389 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1390 goto done;
1393 event_dispatch();
1394 done:
1395 if (err)
1396 log_warnx("%s: %s", title, err->msg);
1397 repo_write_shutdown();
1400 void
1401 repo_write_shutdown(void)
1403 log_debug("%s: shutting down", repo_write.title);
1404 if (repo_write.repo)
1405 got_repo_close(repo_write.repo);
1406 got_repo_pack_fds_close(repo_write.pack_fds);
1407 got_repo_temp_fds_close(repo_write.temp_fds);
1408 exit(0);