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;
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 err = gotd_imsg_flush(&ibuf);
927 if (err)
928 goto done;
930 log_debug("receiving pack data");
931 unpack_err = recv_packdata(&pack_filesize, client->pack_sha1,
932 client->pack_pipe, pack->fd);
933 if (ireq.report_status) {
934 err = report_pack_status(unpack_err);
935 if (err) {
936 /* Git clients hang up after sending the pack file. */
937 if (err->code == GOT_ERR_EOF)
938 err = NULL;
941 if (unpack_err)
942 err = unpack_err;
943 if (err)
944 goto done;
946 log_debug("pack data received");
948 pack->filesize = pack_filesize;
950 log_debug("begin indexing pack (%lld bytes in size)",
951 (long long)pack->filesize);
952 err = got_pack_index(pack, client->packidx_fd,
953 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
954 pack_index_progress, NULL, &rl);
955 if (err)
956 goto done;
957 log_debug("done indexing pack");
959 if (fsync(client->packidx_fd) == -1) {
960 err = got_error_from_errno("fsync");
961 goto done;
963 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
964 err = got_error_from_errno("lseek");
965 done:
966 if (close(client->pack_pipe) == -1 && err == NULL)
967 err = got_error_from_errno("close");
968 client->pack_pipe = -1;
969 for (i = 0; i < nitems(repo_tempfiles); i++) {
970 struct repo_tempfile *t = &repo_tempfiles[i];
971 if (t->idx != -1)
972 got_repo_temp_fds_put(t->idx, repo_write.repo);
974 for (i = 0; i < nitems(tempfiles); i++) {
975 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
976 err = got_error_from_errno("fclose");
978 if (err)
979 got_pack_close(pack);
980 imsg_clear(&ibuf);
981 return err;
984 static const struct got_error *
985 verify_packfile(void)
987 const struct got_error *err = NULL, *close_err;
988 struct repo_write_client *client = &repo_write_client;
989 struct gotd_ref_update *ref_update;
990 struct got_packidx *packidx = NULL;
991 struct stat sb;
992 char *id_str = NULL;
993 int idx = -1;
995 if (STAILQ_EMPTY(&client->ref_updates)) {
996 return got_error_msg(GOT_ERR_BAD_REQUEST,
997 "cannot verify pack file without any ref-updates");
1000 if (client->pack.fd == -1) {
1001 return got_error_msg(GOT_ERR_BAD_REQUEST,
1002 "invalid pack file handle during pack verification");
1004 if (client->packidx_fd == -1) {
1005 return got_error_msg(GOT_ERR_BAD_REQUEST,
1006 "invalid pack index handle during pack verification");
1009 if (fstat(client->packidx_fd, &sb) == -1)
1010 return got_error_from_errno("pack index fstat");
1012 packidx = malloc(sizeof(*packidx));
1013 memset(packidx, 0, sizeof(*packidx));
1014 packidx->fd = client->packidx_fd;
1015 client->packidx_fd = -1;
1016 packidx->len = sb.st_size;
1018 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1019 if (err)
1020 return err;
1022 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1023 err = got_object_id_str(&id_str, &ref_update->new_id);
1024 if (err)
1025 goto done;
1027 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1028 if (idx == -1) {
1029 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1030 "advertised object %s is missing from pack file",
1031 id_str);
1032 goto done;
1036 done:
1037 close_err = got_packidx_close(packidx);
1038 if (close_err && err == NULL)
1039 err = close_err;
1040 free(id_str);
1041 return err;
1044 static const struct got_error *
1045 install_packfile(struct gotd_imsgev *iev)
1047 struct repo_write_client *client = &repo_write_client;
1048 struct gotd_imsg_packfile_install inst;
1049 int ret;
1051 memset(&inst, 0, sizeof(inst));
1052 inst.client_id = client->id;
1053 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1055 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1056 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1057 if (ret == -1)
1058 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1060 return NULL;
1063 static const struct got_error *
1064 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1066 struct repo_write_client *client = &repo_write_client;
1067 struct gotd_imsg_ref_updates_start istart;
1068 int ret;
1070 memset(&istart, 0, sizeof(istart));
1071 istart.nref_updates = nref_updates;
1072 istart.client_id = client->id;
1074 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1075 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1076 if (ret == -1)
1077 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1079 return NULL;
1083 static const struct got_error *
1084 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1086 struct repo_write_client *client = &repo_write_client;
1087 struct gotd_imsg_ref_update iref;
1088 const char *refname = got_ref_get_name(ref_update->ref);
1089 struct ibuf *wbuf;
1090 size_t len;
1092 memset(&iref, 0, sizeof(iref));
1093 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1094 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1095 iref.ref_is_new = ref_update->ref_is_new;
1096 iref.client_id = client->id;
1097 iref.name_len = strlen(refname);
1099 len = sizeof(iref) + iref.name_len;
1100 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1101 repo_write.pid, len);
1102 if (wbuf == NULL)
1103 return got_error_from_errno("imsg_create REF_UPDATE");
1105 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1106 return got_error_from_errno("imsg_add REF_UPDATE");
1107 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1108 return got_error_from_errno("imsg_add REF_UPDATE");
1110 wbuf->fd = -1;
1111 imsg_close(&iev->ibuf, wbuf);
1113 gotd_imsg_event_add(iev);
1114 return NULL;
1117 static const struct got_error *
1118 update_refs(struct gotd_imsgev *iev)
1120 const struct got_error *err = NULL;
1121 struct repo_write_client *client = &repo_write_client;
1122 struct gotd_ref_update *ref_update;
1124 err = send_ref_updates_start(client->nref_updates, iev);
1125 if (err)
1126 return err;
1128 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1129 err = send_ref_update(ref_update, iev);
1130 if (err)
1131 goto done;
1133 done:
1134 return err;
1137 static const struct got_error *
1138 recv_disconnect(struct imsg *imsg)
1140 const struct got_error *err = NULL;
1141 struct gotd_imsg_disconnect idisconnect;
1142 size_t datalen;
1143 int pack_pipe = -1, idxfd = -1;
1144 struct repo_write_client *client = &repo_write_client;
1146 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1147 if (datalen != sizeof(idisconnect))
1148 return got_error(GOT_ERR_PRIVSEP_LEN);
1149 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1151 log_debug("client disconnecting");
1153 while (!STAILQ_EMPTY(&client->ref_updates)) {
1154 struct gotd_ref_update *ref_update;
1155 ref_update = STAILQ_FIRST(&client->ref_updates);
1156 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1157 got_ref_close(ref_update->ref);
1158 free(ref_update);
1160 err = got_pack_close(&client->pack);
1161 if (client->fd != -1 && close(client->fd) == -1)
1162 err = got_error_from_errno("close");
1163 pack_pipe = client->pack_pipe;
1164 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1165 err = got_error_from_errno("close");
1166 idxfd = client->packidx_fd;
1167 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1168 err = got_error_from_errno("close");
1169 return err;
1172 static const struct got_error *
1173 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1175 struct repo_write_client *client = &repo_write_client;
1176 struct gotd_imsg_packfile_pipe ireq;
1177 size_t datalen;
1179 log_debug("receving pack pipe descriptor");
1181 if (imsg->fd == -1)
1182 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1184 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1185 if (datalen != sizeof(ireq))
1186 return got_error(GOT_ERR_PRIVSEP_LEN);
1187 memcpy(&ireq, imsg->data, sizeof(ireq));
1189 if (client->pack_pipe != -1)
1190 return got_error(GOT_ERR_PRIVSEP_MSG);
1192 client->pack_pipe = imsg->fd;
1193 return NULL;
1196 static const struct got_error *
1197 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1199 struct repo_write_client *client = &repo_write_client;
1200 struct gotd_imsg_packidx_file ireq;
1201 size_t datalen;
1203 log_debug("receving pack index output file");
1205 if (imsg->fd == -1)
1206 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1208 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1209 if (datalen != sizeof(ireq))
1210 return got_error(GOT_ERR_PRIVSEP_LEN);
1211 memcpy(&ireq, imsg->data, sizeof(ireq));
1213 if (client->packidx_fd != -1)
1214 return got_error(GOT_ERR_PRIVSEP_MSG);
1216 client->packidx_fd = imsg->fd;
1217 return NULL;
1220 static void
1221 repo_write_dispatch(int fd, short event, void *arg)
1223 const struct got_error *err = NULL;
1224 struct gotd_imsgev *iev = arg;
1225 struct imsgbuf *ibuf = &iev->ibuf;
1226 struct imsg imsg;
1227 struct repo_write_client *client = &repo_write_client;
1228 ssize_t n;
1229 int shut = 0;
1231 if (event & EV_READ) {
1232 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1233 fatal("imsg_read error");
1234 if (n == 0) /* Connection closed. */
1235 shut = 1;
1238 if (event & EV_WRITE) {
1239 n = msgbuf_write(&ibuf->w);
1240 if (n == -1 && errno != EAGAIN)
1241 fatal("msgbuf_write");
1242 if (n == 0) /* Connection closed. */
1243 shut = 1;
1246 for (;;) {
1247 if ((n = imsg_get(ibuf, &imsg)) == -1)
1248 fatal("%s: imsg_get error", __func__);
1249 if (n == 0) /* No more messages. */
1250 break;
1252 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1253 client->id == 0) {
1254 err = got_error(GOT_ERR_PRIVSEP_MSG);
1255 break;
1258 switch (imsg.hdr.type) {
1259 case GOTD_IMSG_LIST_REFS_INTERNAL:
1260 err = list_refs(&imsg);
1261 if (err)
1262 log_warnx("%s: ls-refs: %s", repo_write.title,
1263 err->msg);
1264 break;
1265 case GOTD_IMSG_REF_UPDATE:
1266 err = recv_ref_update(&imsg);
1267 if (err)
1268 log_warnx("%s: ref-update: %s",
1269 repo_write.title, err->msg);
1270 break;
1271 case GOTD_IMSG_PACKFILE_PIPE:
1272 err = receive_pack_pipe(&imsg, iev);
1273 if (err) {
1274 log_warnx("%s: receiving pack pipe: %s",
1275 repo_write.title, err->msg);
1276 break;
1278 break;
1279 case GOTD_IMSG_PACKIDX_FILE:
1280 err = receive_pack_idx(&imsg, iev);
1281 if (err) {
1282 log_warnx("%s: receiving pack index: %s",
1283 repo_write.title, err->msg);
1284 break;
1286 break;
1287 case GOTD_IMSG_RECV_PACKFILE:
1288 err = recv_packfile(&imsg);
1289 if (err) {
1290 log_warnx("%s: receive packfile: %s",
1291 repo_write.title, err->msg);
1292 break;
1294 err = verify_packfile();
1295 if (err) {
1296 log_warnx("%s: verify packfile: %s",
1297 repo_write.title, err->msg);
1298 break;
1300 err = install_packfile(iev);
1301 if (err) {
1302 log_warnx("%s: install packfile: %s",
1303 repo_write.title, err->msg);
1304 break;
1306 err = update_refs(iev);
1307 if (err) {
1308 log_warnx("%s: update refs: %s",
1309 repo_write.title, err->msg);
1311 break;
1312 case GOTD_IMSG_DISCONNECT:
1313 err = recv_disconnect(&imsg);
1314 if (err)
1315 log_warnx("%s: disconnect: %s",
1316 repo_write.title, err->msg);
1317 shut = 1;
1318 break;
1319 default:
1320 log_debug("%s: unexpected imsg %d", repo_write.title,
1321 imsg.hdr.type);
1322 break;
1325 imsg_free(&imsg);
1328 if (!shut && check_cancelled(NULL) == NULL) {
1329 if (err &&
1330 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1331 client->id, err) == -1) {
1332 log_warnx("could not send error to parent: %s",
1333 err->msg);
1335 gotd_imsg_event_add(iev);
1336 } else {
1337 /* This pipe is dead. Remove its event handler */
1338 event_del(&iev->ev);
1339 event_loopexit(NULL);
1343 void
1344 repo_write_main(const char *title, const char *repo_path,
1345 int *pack_fds, int *temp_fds)
1347 const struct got_error *err = NULL;
1348 struct gotd_imsgev iev;
1350 repo_write.title = title;
1351 repo_write.pid = getpid();
1352 repo_write.pack_fds = pack_fds;
1353 repo_write.temp_fds = temp_fds;
1355 STAILQ_INIT(&repo_write_client.ref_updates);
1357 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1358 if (err)
1359 goto done;
1360 if (!got_repo_is_bare(repo_write.repo)) {
1361 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1362 "bare git repository required");
1363 goto done;
1366 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1368 signal(SIGINT, catch_sigint);
1369 signal(SIGTERM, catch_sigterm);
1370 signal(SIGPIPE, SIG_IGN);
1371 signal(SIGHUP, SIG_IGN);
1373 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1374 iev.handler = repo_write_dispatch;
1375 iev.events = EV_READ;
1376 iev.handler_arg = NULL;
1377 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1378 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1379 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1380 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1381 goto done;
1384 event_dispatch();
1385 done:
1386 if (err)
1387 log_warnx("%s: %s", title, err->msg);
1388 repo_write_shutdown();
1391 void
1392 repo_write_shutdown(void)
1394 log_debug("%s: shutting down", repo_write.title);
1395 if (repo_write.repo)
1396 got_repo_close(repo_write.repo);
1397 got_repo_pack_fds_close(repo_write.pack_fds);
1398 got_repo_temp_fds_close(repo_write.temp_fds);
1399 exit(0);