2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
20 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_object.h"
42 #include "got_reference.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_delta_cache.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_ratelimit.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_pack_index.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_poll.h"
55 #include "got_lib_hash.h" /* XXX temp include for debugging */
59 #include "repo_write.h"
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 static struct repo_write {
68 struct got_repository *repo;
72 struct gotd_imsgev session_iev;
75 struct gotd_ref_update {
76 STAILQ_ENTRY(gotd_ref_update) entry;
77 struct got_reference *ref;
80 struct got_object_id old_id;
81 struct got_object_id new_id;
83 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
85 static struct repo_write_client {
90 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
92 struct gotd_ref_updates ref_updates;
98 static volatile sig_atomic_t sigint_received;
99 static volatile sig_atomic_t sigterm_received;
102 catch_sigint(int signo)
108 catch_sigterm(int signo)
110 sigterm_received = 1;
113 static const struct got_error *
114 check_cancelled(void *arg)
116 if (sigint_received || sigterm_received)
117 return got_error(GOT_ERR_CANCELLED);
122 static const struct got_error *
123 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
124 struct imsgbuf *ibuf)
126 const struct got_error *err = NULL;
127 struct got_tag_object *tag;
129 char *peeled_refname = NULL;
130 struct got_object_id *id;
133 err = got_object_tag_open(&tag, repo_write.repo, obj);
137 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
138 err = got_error_from_errno("asprintf");
142 id = got_object_tag_get_object_id(tag);
143 namelen = strlen(peeled_refname);
145 len = sizeof(struct gotd_imsg_ref) + namelen;
146 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
147 err = got_error(GOT_ERR_NO_SPACE);
151 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
152 repo_write.pid, len);
154 err = got_error_from_errno("imsg_create REF");
158 /* Keep in sync with struct gotd_imsg_ref definition. */
159 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
160 err = got_error_from_errno("imsg_add REF");
163 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
164 err = got_error_from_errno("imsg_add REF");
167 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
168 err = got_error_from_errno("imsg_add REF");
173 imsg_close(ibuf, wbuf);
175 got_object_tag_close(tag);
179 static const struct got_error *
180 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
182 const struct got_error *err;
183 const char *refname = got_ref_get_name(ref);
185 struct got_object_id *id = NULL;
186 struct got_object *obj = NULL;
190 namelen = strlen(refname);
192 len = sizeof(struct gotd_imsg_ref) + namelen;
193 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
194 return got_error(GOT_ERR_NO_SPACE);
196 err = got_ref_resolve(&id, repo_write.repo, ref);
200 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
201 repo_write.pid, len);
203 err = got_error_from_errno("imsg_create REF");
207 /* Keep in sync with struct gotd_imsg_ref definition. */
208 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
209 return got_error_from_errno("imsg_add REF");
210 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
211 return got_error_from_errno("imsg_add REF");
212 if (imsg_add(wbuf, refname, namelen) == -1)
213 return got_error_from_errno("imsg_add REF");
216 imsg_close(ibuf, wbuf);
218 err = got_object_open(&obj, repo_write.repo, id);
221 if (obj->type == GOT_OBJ_TYPE_TAG)
222 err = send_peeled_tag_ref(ref, obj, ibuf);
225 got_object_close(obj);
230 static const struct got_error *
231 list_refs(struct imsg *imsg)
233 const struct got_error *err;
234 struct repo_write_client *client = &repo_write_client;
235 struct got_reflist_head refs;
236 struct got_reflist_entry *re;
237 struct gotd_imsg_list_refs_internal ireq;
239 struct gotd_imsg_reflist irefs;
241 int client_fd = imsg->fd;
246 return got_error(GOT_ERR_PRIVSEP_NO_FD);
248 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
249 if (datalen != sizeof(ireq))
250 return got_error(GOT_ERR_PRIVSEP_LEN);
251 memcpy(&ireq, imsg->data, sizeof(ireq));
253 if (ireq.client_id == 0)
254 return got_error(GOT_ERR_CLIENT_ID);
255 if (client->id != 0) {
256 return got_error_msg(GOT_ERR_CLIENT_ID,
257 "duplicate list-refs request");
259 client->id = ireq.client_id;
260 client->fd = client_fd;
261 client->nref_updates = 0;
262 client->nref_del = 0;
263 client->nref_new = 0;
265 imsg_init(&ibuf, client_fd);
267 err = got_ref_list(&refs, repo_write.repo, "",
268 got_ref_cmp_by_name, NULL);
272 memset(&irefs, 0, sizeof(irefs));
273 TAILQ_FOREACH(re, &refs, entry) {
274 struct got_object_id *id;
277 if (got_ref_is_symbolic(re->ref))
282 /* Account for a peeled tag refs. */
283 err = got_ref_resolve(&id, repo_write.repo, re->ref);
286 err = got_object_get_type(&obj_type, repo_write.repo, id);
290 if (obj_type == GOT_OBJ_TYPE_TAG)
294 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
295 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
296 err = got_error_from_errno("imsg_compose REFLIST");
300 TAILQ_FOREACH(re, &refs, entry) {
301 if (got_ref_is_symbolic(re->ref))
303 err = send_ref(re->ref, &ibuf);
308 err = gotd_imsg_flush(&ibuf);
310 got_ref_list_free(&refs);
315 static const struct got_error *
316 protect_ref_namespace(struct got_reference *ref, const char *namespace)
318 size_t len = strlen(namespace);
320 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
321 namespace[len -1] != '/') {
322 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
323 "reference namespace '%s'", namespace);
326 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
327 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
332 static const struct got_error *
333 recv_ref_update(struct imsg *imsg)
335 static const char zero_id[SHA1_DIGEST_LENGTH];
336 const struct got_error *err = NULL;
337 struct repo_write_client *client = &repo_write_client;
338 struct gotd_imsg_ref_update iref;
340 char *refname = NULL;
341 struct got_reference *ref = NULL;
342 struct got_object_id *id = NULL;
344 struct gotd_ref_update *ref_update = NULL;
346 log_debug("ref-update received");
348 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
349 if (datalen < sizeof(iref))
350 return got_error(GOT_ERR_PRIVSEP_LEN);
351 memcpy(&iref, imsg->data, sizeof(iref));
352 if (datalen != sizeof(iref) + iref.name_len)
353 return got_error(GOT_ERR_PRIVSEP_LEN);
355 imsg_init(&ibuf, client->fd);
357 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
359 return got_error_from_errno("strndup");
361 ref_update = calloc(1, sizeof(*ref_update));
362 if (ref_update == NULL) {
363 err = got_error_from_errno("malloc");
367 memcpy(ref_update->old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
368 memcpy(ref_update->new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
370 err = got_ref_open(&ref, repo_write.repo, refname, 0);
372 if (err->code != GOT_ERR_NOT_REF)
374 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
377 ref_update->ref_is_new = 1;
380 if (got_ref_is_symbolic(ref)) {
381 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
382 "'%s' is a symbolic reference and cannot "
383 "be updated", got_ref_get_name(ref));
386 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
387 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
388 "%s: does not begin with 'refs/'",
389 got_ref_get_name(ref));
393 err = protect_ref_namespace(ref, "refs/got/");
396 err = protect_ref_namespace(ref, "refs/remotes/");
400 if (!ref_update->ref_is_new) {
402 * Ensure the client's idea of this update is still valid.
403 * At this point we can only return an error, to prevent
404 * the client from uploading a pack file which will likely
405 * have to be discarded.
407 err = got_ref_resolve(&id, repo_write.repo, ref);
411 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
412 err = got_error_fmt(GOT_ERR_REF_BUSY,
413 "%s has been modified by someone else "
414 "while transaction was in progress",
415 got_ref_get_name(ref));
420 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
423 ref_update->ref = ref;
424 if (memcmp(ref_update->new_id.hash, zero_id, sizeof(zero_id)) == 0) {
425 ref_update->delete_ref = 1;
428 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
429 client->nref_updates++;
441 static const struct got_error *
442 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
443 uint32_t nobj_loose, uint32_t nobj_resolved)
445 int p_indexed = 0, p_resolved = 0;
446 int nobj_delta = nobj_total - nobj_loose;
449 p_indexed = (nobj_indexed * 100) / nobj_total;
452 p_resolved = (nobj_resolved * 100) / nobj_delta;
454 if (p_resolved > 0) {
455 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
456 nobj_total, p_indexed, nobj_delta, p_resolved);
458 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
463 static const struct got_error *
464 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
466 const struct got_error *err = NULL;
467 uint8_t readahead[65536];
470 err = got_poll_read_full(infd, &have,
471 readahead, sizeof(readahead), minsize);
475 err = buf_append(&newlen, buf, readahead, have);
481 static const struct got_error *
482 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
483 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
485 const struct got_error *err = NULL;
490 off_t obj_offset = *outsize;
493 /* We do not support size values which don't fit in 64 bit. */
495 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
496 "packfile offset %lld", (long long)obj_offset);
498 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
499 err = read_more_pack_stream(infd, buf,
505 sizebuf[i] = buf_getc(buf, *buf_pos);
506 *buf_pos += sizeof(sizebuf[i]);
509 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
510 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
511 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
513 size_t shift = 4 + 7 * (i - 1);
514 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
518 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
520 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
530 static const struct got_error *
531 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
534 const struct got_error *err = NULL;
535 size_t remain = buf_len(buf) - *buf_pos;
537 if (remain < SHA1_DIGEST_LENGTH) {
538 err = read_more_pack_stream(infd, buf,
539 SHA1_DIGEST_LENGTH - remain);
544 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
545 SHA1_DIGEST_LENGTH, ctx);
549 *buf_pos += SHA1_DIGEST_LENGTH;
553 static const struct got_error *
554 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
557 const struct got_error *err = NULL;
561 off_t obj_offset = *outsize;
564 /* We do not support offset values which don't fit in 64 bit. */
566 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
567 "packfile offset %lld", (long long)obj_offset);
569 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
570 err = read_more_pack_stream(infd, buf,
576 offbuf[i] = buf_getc(buf, *buf_pos);
577 *buf_pos += sizeof(offbuf[i]);
580 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
584 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
587 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
589 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
590 return got_error(GOT_ERR_PACK_OFFSET);
592 err = got_pack_hwrite(outfd, offbuf, i, ctx);
600 static const struct got_error *
601 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
604 const struct got_error *err = NULL;
608 size_t consumed_total = 0;
609 off_t zstream_offset = *outsize;
611 memset(&z, 0, sizeof(z));
615 zret = inflateInit(&z);
618 return got_error_from_errno("inflateInit");
619 if (zret == Z_MEM_ERROR) {
621 return got_error_from_errno("inflateInit");
623 return got_error_msg(GOT_ERR_DECOMPRESSION,
624 "inflateInit failed");
627 while (zret != Z_STREAM_END) {
628 size_t last_total_in, consumed;
631 * Decompress into the void. Object data will be parsed
632 * later, when the pack file is indexed. For now, we just
633 * want to locate the end of the compressed stream.
635 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
636 last_total_in = z.total_in;
637 z.next_in = buf_get(buf) + *buf_pos;
638 z.avail_in = buf_len(buf) - *buf_pos;
639 z.next_out = voidbuf;
640 z.avail_out = sizeof(voidbuf);
642 zret = inflate(&z, Z_SYNC_FLUSH);
643 if (zret != Z_OK && zret != Z_BUF_ERROR &&
644 zret != Z_STREAM_END) {
645 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
646 "packfile offset %lld",
647 (long long)zstream_offset);
650 consumed = z.total_in - last_total_in;
652 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
657 err = buf_discard(buf, *buf_pos + consumed);
662 consumed_total += consumed;
665 if (zret != Z_STREAM_END) {
666 err = read_more_pack_stream(infd, buf, 1);
673 *outsize += consumed_total;
679 static const struct got_error *
680 validate_object_type(int obj_type)
683 case GOT_OBJ_TYPE_BLOB:
684 case GOT_OBJ_TYPE_COMMIT:
685 case GOT_OBJ_TYPE_TREE:
686 case GOT_OBJ_TYPE_TAG:
687 case GOT_OBJ_TYPE_REF_DELTA:
688 case GOT_OBJ_TYPE_OFFSET_DELTA:
694 return got_error(GOT_ERR_OBJ_TYPE);
697 static const struct got_error *
698 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
701 const struct got_error *err;
702 struct repo_write_client *client = &repo_write_client;
703 struct got_packfile_hdr hdr;
707 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
708 char hex[SHA1_DIGEST_STRING_LENGTH];
710 size_t buf_pos = 0, remain;
716 /* if only deleting references there's nothing to read */
717 if (client->nref_updates == client->nref_del)
722 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
725 if (have != sizeof(hdr))
726 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
729 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
730 return got_error_msg(GOT_ERR_BAD_PACKFILE,
731 "bad packfile signature");
732 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
733 return got_error_msg(GOT_ERR_BAD_PACKFILE,
734 "bad packfile version");
736 *nobj = be32toh(hdr.nobjects);
739 * Clients which are creating new references only
740 * will send us an empty pack file.
742 if (client->nref_updates > 0 &&
743 client->nref_updates == client->nref_new)
746 return got_error_msg(GOT_ERR_BAD_PACKFILE,
747 "bad packfile with zero objects");
750 log_debug("expecting %d objects", *nobj);
752 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
756 err = buf_alloc(&buf, 65536);
760 while (nhave != *nobj) {
764 err = copy_object_type_and_size(&obj_type, &obj_size,
765 infd, outfd, outsize, buf, &buf_pos, &ctx);
769 err = validate_object_type(obj_type);
773 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
774 err = copy_ref_delta(infd, outfd, outsize,
775 buf, &buf_pos, &ctx);
778 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
779 err = copy_offset_delta(infd, outfd, outsize,
780 buf, &buf_pos, &ctx);
785 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
792 log_debug("received %u objects", *nobj);
794 SHA1Final(expected_sha1, &ctx);
796 remain = buf_len(buf) - buf_pos;
797 if (remain < SHA1_DIGEST_LENGTH) {
798 err = read_more_pack_stream(infd, buf,
799 SHA1_DIGEST_LENGTH - remain);
804 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
805 log_debug("expect SHA1: %s", hex);
806 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
807 log_debug("actual SHA1: %s", hex);
809 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
810 SHA1_DIGEST_LENGTH) != 0) {
811 err = got_error(GOT_ERR_PACKFILE_CSUM);
815 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
817 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
819 err = got_error_from_errno("write");
822 if (w != SHA1_DIGEST_LENGTH) {
823 err = got_error(GOT_ERR_IO);
827 *outsize += SHA1_DIGEST_LENGTH;
829 if (fsync(outfd) == -1) {
830 err = got_error_from_errno("fsync");
833 if (lseek(outfd, 0L, SEEK_SET) == -1) {
834 err = got_error_from_errno("lseek");
842 static const struct got_error *
843 report_pack_status(const struct got_error *unpack_err)
845 const struct got_error *err = NULL;
846 struct repo_write_client *client = &repo_write_client;
847 struct gotd_imsg_packfile_status istatus;
850 const char *unpack_ok = "unpack ok\n";
853 imsg_init(&ibuf, client->fd);
856 istatus.reason_len = strlen(unpack_err->msg);
858 istatus.reason_len = strlen(unpack_ok);
860 len = sizeof(istatus) + istatus.reason_len;
861 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
862 repo_write.pid, len);
864 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
868 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
869 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
873 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
874 istatus.reason_len) == -1) {
875 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
880 imsg_close(&ibuf, wbuf);
882 err = gotd_imsg_flush(&ibuf);
888 static const struct got_error *
889 recv_packfile(int *have_packfile, struct imsg *imsg)
891 const struct got_error *err = NULL, *unpack_err;
892 struct repo_write_client *client = &repo_write_client;
893 struct gotd_imsg_recv_packfile ireq;
894 FILE *tempfiles[3] = { NULL, NULL, NULL };
895 struct repo_tempfile {
898 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
902 struct got_ratelimit rl;
903 struct got_pack *pack = NULL;
904 off_t pack_filesize = 0;
907 log_debug("packfile request received");
910 got_ratelimit_init(&rl, 2, 0);
912 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
913 if (datalen != sizeof(ireq))
914 return got_error(GOT_ERR_PRIVSEP_LEN);
915 memcpy(&ireq, imsg->data, sizeof(ireq));
917 if (client->pack_pipe == -1 || client->packidx_fd == -1)
918 return got_error(GOT_ERR_PRIVSEP_NO_FD);
920 imsg_init(&ibuf, client->fd);
923 return got_error(GOT_ERR_PRIVSEP_NO_FD);
925 pack = &client->pack;
926 memset(pack, 0, sizeof(*pack));
928 err = got_delta_cache_alloc(&pack->delta_cache);
932 for (i = 0; i < nitems(repo_tempfiles); i++) {
933 struct repo_tempfile *t = &repo_tempfiles[i];
934 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
939 for (i = 0; i < nitems(tempfiles); i++) {
943 fd = dup(repo_tempfiles[i].fd);
945 err = got_error_from_errno("dup");
948 f = fdopen(fd, "w+");
950 err = got_error_from_errno("fdopen");
957 err = gotd_imsg_flush(&ibuf);
961 log_debug("receiving pack data");
962 unpack_err = recv_packdata(&pack_filesize, &nobj,
963 client->pack_sha1, client->pack_pipe, pack->fd);
964 if (ireq.report_status) {
965 err = report_pack_status(unpack_err);
967 /* Git clients hang up after sending the pack file. */
968 if (err->code == GOT_ERR_EOF)
977 log_debug("pack data received");
980 * Clients which are creating new references only will
981 * send us an empty pack file.
984 pack_filesize == sizeof(struct got_packfile_hdr) &&
985 client->nref_updates > 0 &&
986 client->nref_updates == client->nref_new)
990 * Clients which are deleting references only will send
994 client->nref_del > 0 &&
995 client->nref_updates == client->nref_del)
998 pack->filesize = pack_filesize;
1001 log_debug("begin indexing pack (%lld bytes in size)",
1002 (long long)pack->filesize);
1003 err = got_pack_index(pack, client->packidx_fd,
1004 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1005 pack_index_progress, NULL, &rl);
1008 log_debug("done indexing pack");
1010 if (fsync(client->packidx_fd) == -1) {
1011 err = got_error_from_errno("fsync");
1014 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1015 err = got_error_from_errno("lseek");
1017 if (close(client->pack_pipe) == -1 && err == NULL)
1018 err = got_error_from_errno("close");
1019 client->pack_pipe = -1;
1020 for (i = 0; i < nitems(repo_tempfiles); i++) {
1021 struct repo_tempfile *t = &repo_tempfiles[i];
1023 got_repo_temp_fds_put(t->idx, repo_write.repo);
1025 for (i = 0; i < nitems(tempfiles); i++) {
1026 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1027 err = got_error_from_errno("fclose");
1030 got_pack_close(pack);
1035 static const struct got_error *
1036 verify_packfile(void)
1038 const struct got_error *err = NULL, *close_err;
1039 struct repo_write_client *client = &repo_write_client;
1040 struct gotd_ref_update *ref_update;
1041 struct got_packidx *packidx = NULL;
1043 char *id_str = NULL;
1046 if (STAILQ_EMPTY(&client->ref_updates)) {
1047 return got_error_msg(GOT_ERR_BAD_REQUEST,
1048 "cannot verify pack file without any ref-updates");
1051 if (client->pack.fd == -1) {
1052 return got_error_msg(GOT_ERR_BAD_REQUEST,
1053 "invalid pack file handle during pack verification");
1055 if (client->packidx_fd == -1) {
1056 return got_error_msg(GOT_ERR_BAD_REQUEST,
1057 "invalid pack index handle during pack verification");
1060 if (fstat(client->packidx_fd, &sb) == -1)
1061 return got_error_from_errno("pack index fstat");
1063 packidx = malloc(sizeof(*packidx));
1064 memset(packidx, 0, sizeof(*packidx));
1065 packidx->fd = client->packidx_fd;
1066 client->packidx_fd = -1;
1067 packidx->len = sb.st_size;
1069 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1073 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1074 if (ref_update->delete_ref)
1077 err = got_object_id_str(&id_str, &ref_update->new_id);
1081 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1083 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1084 "advertised object %s is missing from pack file",
1091 close_err = got_packidx_close(packidx);
1092 if (close_err && err == NULL)
1098 static const struct got_error *
1099 install_packfile(struct gotd_imsgev *iev)
1101 struct repo_write_client *client = &repo_write_client;
1102 struct gotd_imsg_packfile_install inst;
1105 memset(&inst, 0, sizeof(inst));
1106 inst.client_id = client->id;
1107 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1109 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1110 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1112 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1117 static const struct got_error *
1118 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1120 struct repo_write_client *client = &repo_write_client;
1121 struct gotd_imsg_ref_updates_start istart;
1124 memset(&istart, 0, sizeof(istart));
1125 istart.nref_updates = nref_updates;
1126 istart.client_id = client->id;
1128 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1129 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1131 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1137 static const struct got_error *
1138 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1140 struct repo_write_client *client = &repo_write_client;
1141 struct gotd_imsg_ref_update iref;
1142 const char *refname = got_ref_get_name(ref_update->ref);
1146 memset(&iref, 0, sizeof(iref));
1147 memcpy(iref.old_id, ref_update->old_id.hash, SHA1_DIGEST_LENGTH);
1148 memcpy(iref.new_id, ref_update->new_id.hash, SHA1_DIGEST_LENGTH);
1149 iref.ref_is_new = ref_update->ref_is_new;
1150 iref.delete_ref = ref_update->delete_ref;
1151 iref.client_id = client->id;
1152 iref.name_len = strlen(refname);
1154 len = sizeof(iref) + iref.name_len;
1155 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1156 repo_write.pid, len);
1158 return got_error_from_errno("imsg_create REF_UPDATE");
1160 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1161 return got_error_from_errno("imsg_add REF_UPDATE");
1162 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1163 return got_error_from_errno("imsg_add REF_UPDATE");
1166 imsg_close(&iev->ibuf, wbuf);
1168 gotd_imsg_event_add(iev);
1172 static const struct got_error *
1173 update_refs(struct gotd_imsgev *iev)
1175 const struct got_error *err = NULL;
1176 struct repo_write_client *client = &repo_write_client;
1177 struct gotd_ref_update *ref_update;
1179 err = send_ref_updates_start(client->nref_updates, iev);
1183 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1184 err = send_ref_update(ref_update, iev);
1192 static const struct got_error *
1193 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1195 struct repo_write_client *client = &repo_write_client;
1196 struct gotd_imsg_packfile_pipe ireq;
1199 log_debug("receving pack pipe descriptor");
1202 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1204 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1205 if (datalen != sizeof(ireq))
1206 return got_error(GOT_ERR_PRIVSEP_LEN);
1207 memcpy(&ireq, imsg->data, sizeof(ireq));
1209 if (client->pack_pipe != -1)
1210 return got_error(GOT_ERR_PRIVSEP_MSG);
1212 client->pack_pipe = imsg->fd;
1216 static const struct got_error *
1217 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1219 struct repo_write_client *client = &repo_write_client;
1220 struct gotd_imsg_packidx_file ireq;
1223 log_debug("receving pack index output file");
1226 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1228 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1229 if (datalen != sizeof(ireq))
1230 return got_error(GOT_ERR_PRIVSEP_LEN);
1231 memcpy(&ireq, imsg->data, sizeof(ireq));
1233 if (client->packidx_fd != -1)
1234 return got_error(GOT_ERR_PRIVSEP_MSG);
1236 client->packidx_fd = imsg->fd;
1241 repo_write_dispatch_session(int fd, short event, void *arg)
1243 const struct got_error *err = NULL;
1244 struct gotd_imsgev *iev = arg;
1245 struct imsgbuf *ibuf = &iev->ibuf;
1247 struct repo_write_client *client = &repo_write_client;
1249 int shut = 0, have_packfile = 0;
1251 if (event & EV_READ) {
1252 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1253 fatal("imsg_read error");
1254 if (n == 0) /* Connection closed. */
1258 if (event & EV_WRITE) {
1259 n = msgbuf_write(&ibuf->w);
1260 if (n == -1 && errno != EAGAIN)
1261 fatal("msgbuf_write");
1262 if (n == 0) /* Connection closed. */
1267 if ((n = imsg_get(ibuf, &imsg)) == -1)
1268 fatal("%s: imsg_get error", __func__);
1269 if (n == 0) /* No more messages. */
1272 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1274 err = got_error(GOT_ERR_PRIVSEP_MSG);
1278 switch (imsg.hdr.type) {
1279 case GOTD_IMSG_LIST_REFS_INTERNAL:
1280 err = list_refs(&imsg);
1282 log_warnx("%s: ls-refs: %s", repo_write.title,
1285 case GOTD_IMSG_REF_UPDATE:
1286 err = recv_ref_update(&imsg);
1288 log_warnx("%s: ref-update: %s",
1289 repo_write.title, err->msg);
1291 case GOTD_IMSG_PACKFILE_PIPE:
1292 err = receive_pack_pipe(&imsg, iev);
1294 log_warnx("%s: receiving pack pipe: %s",
1295 repo_write.title, err->msg);
1299 case GOTD_IMSG_PACKIDX_FILE:
1300 err = receive_pack_idx(&imsg, iev);
1302 log_warnx("%s: receiving pack index: %s",
1303 repo_write.title, err->msg);
1307 case GOTD_IMSG_RECV_PACKFILE:
1308 err = recv_packfile(&have_packfile, &imsg);
1310 log_warnx("%s: receive packfile: %s",
1311 repo_write.title, err->msg);
1314 if (have_packfile) {
1315 err = verify_packfile();
1317 log_warnx("%s: verify packfile: %s",
1318 repo_write.title, err->msg);
1321 err = install_packfile(iev);
1323 log_warnx("%s: install packfile: %s",
1324 repo_write.title, err->msg);
1328 err = update_refs(iev);
1330 log_warnx("%s: update refs: %s",
1331 repo_write.title, err->msg);
1335 log_debug("%s: unexpected imsg %d", repo_write.title,
1343 if (!shut && check_cancelled(NULL) == NULL) {
1345 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1346 client->id, err) == -1) {
1347 log_warnx("could not send error to parent: %s",
1350 gotd_imsg_event_add(iev);
1352 /* This pipe is dead. Remove its event handler */
1353 event_del(&iev->ev);
1354 event_loopexit(NULL);
1358 static const struct got_error *
1359 recv_connect(struct imsg *imsg)
1361 struct gotd_imsgev *iev = &repo_write.session_iev;
1364 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1366 return got_error(GOT_ERR_PRIVSEP_LEN);
1368 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1370 if (repo_write.session_fd != -1)
1371 return got_error(GOT_ERR_PRIVSEP_MSG);
1373 repo_write.session_fd = imsg->fd;
1375 imsg_init(&iev->ibuf, repo_write.session_fd);
1376 iev->handler = repo_write_dispatch_session;
1377 iev->events = EV_READ;
1378 iev->handler_arg = NULL;
1379 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1380 repo_write_dispatch_session, iev);
1381 gotd_imsg_event_add(iev);
1387 repo_write_dispatch(int fd, short event, void *arg)
1389 const struct got_error *err = NULL;
1390 struct gotd_imsgev *iev = arg;
1391 struct imsgbuf *ibuf = &iev->ibuf;
1395 struct repo_write_client *client = &repo_write_client;
1397 if (event & EV_READ) {
1398 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1399 fatal("imsg_read error");
1400 if (n == 0) /* Connection closed. */
1404 if (event & EV_WRITE) {
1405 n = msgbuf_write(&ibuf->w);
1406 if (n == -1 && errno != EAGAIN)
1407 fatal("msgbuf_write");
1408 if (n == 0) /* Connection closed. */
1412 while (err == NULL && check_cancelled(NULL) == NULL) {
1413 if ((n = imsg_get(ibuf, &imsg)) == -1)
1414 fatal("%s: imsg_get", __func__);
1415 if (n == 0) /* No more messages. */
1418 switch (imsg.hdr.type) {
1419 case GOTD_IMSG_CONNECT_REPO_CHILD:
1420 err = recv_connect(&imsg);
1423 log_debug("%s: unexpected imsg %d", repo_write.title,
1431 if (!shut && check_cancelled(NULL) == NULL) {
1433 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1434 client->id, err) == -1) {
1435 log_warnx("could not send error to parent: %s",
1438 gotd_imsg_event_add(iev);
1440 /* This pipe is dead. Remove its event handler */
1441 event_del(&iev->ev);
1442 event_loopexit(NULL);
1447 repo_write_main(const char *title, const char *repo_path,
1448 int *pack_fds, int *temp_fds)
1450 const struct got_error *err = NULL;
1451 struct repo_write_client *client = &repo_write_client;
1452 struct gotd_imsgev iev;
1455 client->pack_pipe = -1;
1456 client->packidx_fd = -1;
1457 client->pack.fd = -1;
1459 repo_write.title = title;
1460 repo_write.pid = getpid();
1461 repo_write.pack_fds = pack_fds;
1462 repo_write.temp_fds = temp_fds;
1463 repo_write.session_fd = -1;
1464 repo_write.session_iev.ibuf.fd = -1;
1466 STAILQ_INIT(&repo_write_client.ref_updates);
1468 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1471 if (!got_repo_is_bare(repo_write.repo)) {
1472 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1473 "bare git repository required");
1477 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1479 signal(SIGINT, catch_sigint);
1480 signal(SIGTERM, catch_sigterm);
1481 signal(SIGPIPE, SIG_IGN);
1482 signal(SIGHUP, SIG_IGN);
1484 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1485 iev.handler = repo_write_dispatch;
1486 iev.events = EV_READ;
1487 iev.handler_arg = NULL;
1488 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1489 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1490 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1491 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1498 log_warnx("%s: %s", title, err->msg);
1499 repo_write_shutdown();
1503 repo_write_shutdown(void)
1505 struct repo_write_client *client = &repo_write_client;
1506 struct gotd_ref_update *ref_update;
1508 log_debug("%s: shutting down", repo_write.title);
1510 while (!STAILQ_EMPTY(&client->ref_updates)) {
1511 ref_update = STAILQ_FIRST(&client->ref_updates);
1512 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1513 got_ref_close(ref_update->ref);
1517 got_pack_close(&client->pack);
1518 if (client->fd != -1)
1520 if (client->pack_pipe != -1)
1521 close(client->pack_pipe);
1522 if (client->packidx_fd != -1)
1523 close(client->packidx_fd);
1525 if (repo_write.repo)
1526 got_repo_close(repo_write.repo);
1527 got_repo_pack_fds_close(repo_write.pack_fds);
1528 got_repo_temp_fds_close(repo_write.temp_fds);
1529 if (repo_write.session_fd != -1)
1530 close(repo_write.session_fd);