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>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_object.h"
41 #include "got_reference.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 */
58 #include "repo_write.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static struct repo_write {
67 struct got_repository *repo;
71 struct gotd_imsgev session_iev;
74 struct gotd_ref_update {
75 STAILQ_ENTRY(gotd_ref_update) entry;
76 struct got_reference *ref;
78 struct got_object_id old_id;
79 struct got_object_id new_id;
81 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
83 static struct repo_write_client {
88 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
90 struct gotd_ref_updates ref_updates;
94 static volatile sig_atomic_t sigint_received;
95 static volatile sig_atomic_t sigterm_received;
98 catch_sigint(int signo)
104 catch_sigterm(int signo)
106 sigterm_received = 1;
109 static const struct got_error *
110 check_cancelled(void *arg)
112 if (sigint_received || sigterm_received)
113 return got_error(GOT_ERR_CANCELLED);
118 static const struct got_error *
119 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
120 struct imsgbuf *ibuf)
122 const struct got_error *err = NULL;
123 struct got_tag_object *tag;
125 char *peeled_refname = NULL;
126 struct got_object_id *id;
129 err = got_object_tag_open(&tag, repo_write.repo, obj);
133 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
134 err = got_error_from_errno("asprintf");
138 id = got_object_tag_get_object_id(tag);
139 namelen = strlen(peeled_refname);
141 len = sizeof(struct gotd_imsg_ref) + namelen;
142 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
143 err = got_error(GOT_ERR_NO_SPACE);
147 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
148 repo_write.pid, len);
150 err = got_error_from_errno("imsg_create REF");
154 /* Keep in sync with struct gotd_imsg_ref definition. */
155 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
156 err = got_error_from_errno("imsg_add REF");
159 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
160 err = got_error_from_errno("imsg_add REF");
163 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
164 err = got_error_from_errno("imsg_add REF");
169 imsg_close(ibuf, wbuf);
171 got_object_tag_close(tag);
175 static const struct got_error *
176 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
178 const struct got_error *err;
179 const char *refname = got_ref_get_name(ref);
181 struct got_object_id *id = NULL;
182 struct got_object *obj = NULL;
186 namelen = strlen(refname);
188 len = sizeof(struct gotd_imsg_ref) + namelen;
189 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 return got_error(GOT_ERR_NO_SPACE);
192 err = got_ref_resolve(&id, repo_write.repo, ref);
196 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
197 repo_write.pid, len);
199 err = got_error_from_errno("imsg_create REF");
203 /* Keep in sync with struct gotd_imsg_ref definition. */
204 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
205 return got_error_from_errno("imsg_add REF");
206 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
207 return got_error_from_errno("imsg_add REF");
208 if (imsg_add(wbuf, refname, namelen) == -1)
209 return got_error_from_errno("imsg_add REF");
212 imsg_close(ibuf, wbuf);
214 err = got_object_open(&obj, repo_write.repo, id);
217 if (obj->type == GOT_OBJ_TYPE_TAG)
218 err = send_peeled_tag_ref(ref, obj, ibuf);
221 got_object_close(obj);
226 static const struct got_error *
227 list_refs(struct imsg *imsg)
229 const struct got_error *err;
230 struct repo_write_client *client = &repo_write_client;
231 struct got_reflist_head refs;
232 struct got_reflist_entry *re;
233 struct gotd_imsg_list_refs_internal ireq;
235 struct gotd_imsg_reflist irefs;
237 int client_fd = imsg->fd;
242 return got_error(GOT_ERR_PRIVSEP_NO_FD);
244 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
245 if (datalen != sizeof(ireq))
246 return got_error(GOT_ERR_PRIVSEP_LEN);
247 memcpy(&ireq, imsg->data, sizeof(ireq));
249 if (ireq.client_id == 0)
250 return got_error(GOT_ERR_CLIENT_ID);
251 if (client->id != 0) {
252 return got_error_msg(GOT_ERR_CLIENT_ID,
253 "duplicate list-refs request");
255 client->id = ireq.client_id;
256 client->fd = client_fd;
257 client->pack_pipe = -1;
258 client->packidx_fd = -1;
259 client->nref_updates = 0;
261 imsg_init(&ibuf, client_fd);
263 err = got_ref_list(&refs, repo_write.repo, "",
264 got_ref_cmp_by_name, NULL);
268 memset(&irefs, 0, sizeof(irefs));
269 TAILQ_FOREACH(re, &refs, entry) {
270 struct got_object_id *id;
273 if (got_ref_is_symbolic(re->ref))
278 /* Account for a peeled tag refs. */
279 err = got_ref_resolve(&id, repo_write.repo, re->ref);
282 err = got_object_get_type(&obj_type, repo_write.repo, id);
286 if (obj_type == GOT_OBJ_TYPE_TAG)
290 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
291 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
292 err = got_error_from_errno("imsg_compose REFLIST");
296 TAILQ_FOREACH(re, &refs, entry) {
297 if (got_ref_is_symbolic(re->ref))
299 err = send_ref(re->ref, &ibuf);
304 err = gotd_imsg_flush(&ibuf);
306 got_ref_list_free(&refs);
311 static const struct got_error *
312 protect_ref_namespace(struct got_reference *ref, const char *namespace)
314 size_t len = strlen(namespace);
316 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
317 namespace[len -1] != '/') {
318 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
319 "reference namespace '%s'", namespace);
322 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
323 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
328 static const struct got_error *
329 recv_ref_update(struct imsg *imsg)
331 const struct got_error *err = NULL;
332 struct repo_write_client *client = &repo_write_client;
333 struct gotd_imsg_ref_update iref;
335 char *refname = NULL;
336 struct got_reference *ref = NULL;
337 struct got_object_id *id = NULL;
339 struct gotd_ref_update *ref_update = NULL;
341 log_debug("ref-update received");
343 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
344 if (datalen < sizeof(iref))
345 return got_error(GOT_ERR_PRIVSEP_LEN);
346 memcpy(&iref, imsg->data, sizeof(iref));
347 if (datalen != sizeof(iref) + iref.name_len)
348 return got_error(GOT_ERR_PRIVSEP_LEN);
350 imsg_init(&ibuf, client->fd);
352 refname = malloc(iref.name_len + 1);
354 return got_error_from_errno("malloc");
355 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
356 refname[iref.name_len] = '\0';
358 ref_update = calloc(1, sizeof(*ref_update));
359 if (ref_update == NULL) {
360 err = got_error_from_errno("malloc");
364 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
365 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
367 err = got_ref_open(&ref, repo_write.repo, refname, 0);
369 if (err->code != GOT_ERR_NOT_REF)
371 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
374 ref_update->ref_is_new = 1;
376 if (got_ref_is_symbolic(ref)) {
377 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
378 "'%s' is a symbolic reference and cannot "
379 "be updated", got_ref_get_name(ref));
382 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
383 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
384 "%s: does not begin with 'refs/'",
385 got_ref_get_name(ref));
389 err = protect_ref_namespace(ref, "refs/got/");
392 err = protect_ref_namespace(ref, "refs/remotes/");
396 if (!ref_update->ref_is_new) {
398 * Ensure the client's idea of this update is still valid.
399 * At this point we can only return an error, to prevent
400 * the client from uploading a pack file which will likely
401 * have to be discarded.
403 err = got_ref_resolve(&id, repo_write.repo, ref);
407 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
408 err = got_error_fmt(GOT_ERR_REF_BUSY,
409 "%s has been modified by someone else "
410 "while transaction was in progress",
411 got_ref_get_name(ref));
416 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
419 ref_update->ref = ref;
420 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
421 client->nref_updates++;
433 static const struct got_error *
434 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
435 uint32_t nobj_loose, uint32_t nobj_resolved)
437 int p_indexed = 0, p_resolved = 0;
438 int nobj_delta = nobj_total - nobj_loose;
441 p_indexed = (nobj_indexed * 100) / nobj_total;
444 p_resolved = (nobj_resolved * 100) / nobj_delta;
446 if (p_resolved > 0) {
447 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
448 nobj_total, p_indexed, nobj_delta, p_resolved);
450 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
455 static const struct got_error *
456 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
458 const struct got_error *err = NULL;
459 uint8_t readahead[65536];
462 err = got_poll_read_full(infd, &have,
463 readahead, sizeof(readahead), minsize);
467 err = buf_append(&newlen, buf, readahead, have);
473 static const struct got_error *
474 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
475 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
477 const struct got_error *err = NULL;
482 off_t obj_offset = *outsize;
485 /* We do not support size values which don't fit in 64 bit. */
487 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
488 "packfile offset %lld", (long long)obj_offset);
490 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
491 err = read_more_pack_stream(infd, buf,
497 sizebuf[i] = buf_getc(buf, *buf_pos);
498 *buf_pos += sizeof(sizebuf[i]);
501 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
502 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
503 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
505 size_t shift = 4 + 7 * (i - 1);
506 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
510 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
512 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
522 static const struct got_error *
523 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
526 const struct got_error *err = NULL;
527 size_t remain = buf_len(buf) - *buf_pos;
529 if (remain < SHA1_DIGEST_LENGTH) {
530 err = read_more_pack_stream(infd, buf,
531 SHA1_DIGEST_LENGTH - remain);
536 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
537 SHA1_DIGEST_LENGTH, ctx);
541 *buf_pos += SHA1_DIGEST_LENGTH;
545 static const struct got_error *
546 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
549 const struct got_error *err = NULL;
553 off_t obj_offset = *outsize;
556 /* We do not support offset values which don't fit in 64 bit. */
558 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
559 "packfile offset %lld", (long long)obj_offset);
561 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
562 err = read_more_pack_stream(infd, buf,
568 offbuf[i] = buf_getc(buf, *buf_pos);
569 *buf_pos += sizeof(offbuf[i]);
572 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
576 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
579 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
581 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
582 return got_error(GOT_ERR_PACK_OFFSET);
584 err = got_pack_hwrite(outfd, offbuf, i, ctx);
592 static const struct got_error *
593 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
596 const struct got_error *err = NULL;
600 size_t consumed_total = 0;
601 off_t zstream_offset = *outsize;
603 memset(&z, 0, sizeof(z));
607 zret = inflateInit(&z);
610 return got_error_from_errno("inflateInit");
611 if (zret == Z_MEM_ERROR) {
613 return got_error_from_errno("inflateInit");
615 return got_error_msg(GOT_ERR_DECOMPRESSION,
616 "inflateInit failed");
619 while (zret != Z_STREAM_END) {
620 size_t last_total_in, consumed;
623 * Decompress into the void. Object data will be parsed
624 * later, when the pack file is indexed. For now, we just
625 * want to locate the end of the compressed stream.
627 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
628 last_total_in = z.total_in;
629 z.next_in = buf_get(buf) + *buf_pos;
630 z.avail_in = buf_len(buf) - *buf_pos;
631 z.next_out = voidbuf;
632 z.avail_out = sizeof(voidbuf);
634 zret = inflate(&z, Z_SYNC_FLUSH);
635 if (zret != Z_OK && zret != Z_BUF_ERROR &&
636 zret != Z_STREAM_END) {
637 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
638 "packfile offset %lld",
639 (long long)zstream_offset);
642 consumed = z.total_in - last_total_in;
644 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
649 err = buf_discard(buf, *buf_pos + consumed);
654 consumed_total += consumed;
657 if (zret != Z_STREAM_END) {
658 err = read_more_pack_stream(infd, buf, 1);
665 *outsize += consumed_total;
671 static const struct got_error *
672 validate_object_type(int obj_type)
675 case GOT_OBJ_TYPE_BLOB:
676 case GOT_OBJ_TYPE_COMMIT:
677 case GOT_OBJ_TYPE_TREE:
678 case GOT_OBJ_TYPE_TAG:
679 case GOT_OBJ_TYPE_REF_DELTA:
680 case GOT_OBJ_TYPE_OFFSET_DELTA:
686 return got_error(GOT_ERR_OBJ_TYPE);
689 static const struct got_error *
690 recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
692 const struct got_error *err;
693 struct got_packfile_hdr hdr;
695 uint32_t nobj, nhave = 0;
697 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
698 char hex[SHA1_DIGEST_STRING_LENGTH];
700 size_t buf_pos = 0, remain;
706 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
709 if (have != sizeof(hdr))
710 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
713 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
714 return got_error_msg(GOT_ERR_BAD_PACKFILE,
715 "bad packfile signature");
716 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
717 return got_error_msg(GOT_ERR_BAD_PACKFILE,
718 "bad packfile version");
720 nobj = be32toh(hdr.nobjects);
722 return got_error_msg(GOT_ERR_BAD_PACKFILE,
723 "bad packfile with zero objects");
725 log_debug("expecting %d objects", nobj);
727 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
731 err = buf_alloc(&buf, 65536);
735 while (nhave != nobj) {
739 err = copy_object_type_and_size(&obj_type, &obj_size,
740 infd, outfd, outsize, buf, &buf_pos, &ctx);
744 err = validate_object_type(obj_type);
748 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
749 err = copy_ref_delta(infd, outfd, outsize,
750 buf, &buf_pos, &ctx);
753 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
754 err = copy_offset_delta(infd, outfd, outsize,
755 buf, &buf_pos, &ctx);
760 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
767 log_debug("received %u objects", nobj);
769 SHA1Final(expected_sha1, &ctx);
771 remain = buf_len(buf) - buf_pos;
772 if (remain < SHA1_DIGEST_LENGTH) {
773 err = read_more_pack_stream(infd, buf,
774 SHA1_DIGEST_LENGTH - remain);
779 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
780 log_debug("expect SHA1: %s", hex);
781 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
782 log_debug("actual SHA1: %s", hex);
784 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
785 SHA1_DIGEST_LENGTH) != 0) {
786 err = got_error(GOT_ERR_PACKFILE_CSUM);
790 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
792 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
794 err = got_error_from_errno("write");
797 if (w != SHA1_DIGEST_LENGTH) {
798 err = got_error(GOT_ERR_IO);
802 *outsize += SHA1_DIGEST_LENGTH;
804 if (fsync(outfd) == -1) {
805 err = got_error_from_errno("fsync");
808 if (lseek(outfd, 0L, SEEK_SET) == -1) {
809 err = got_error_from_errno("lseek");
817 static const struct got_error *
818 report_pack_status(const struct got_error *unpack_err)
820 const struct got_error *err = NULL;
821 struct repo_write_client *client = &repo_write_client;
822 struct gotd_imsg_packfile_status istatus;
825 const char *unpack_ok = "unpack ok\n";
828 imsg_init(&ibuf, client->fd);
831 istatus.reason_len = strlen(unpack_err->msg);
833 istatus.reason_len = strlen(unpack_ok);
835 len = sizeof(istatus) + istatus.reason_len;
836 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
837 repo_write.pid, len);
839 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
843 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
844 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
848 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
849 istatus.reason_len) == -1) {
850 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
855 imsg_close(&ibuf, wbuf);
857 err = gotd_imsg_flush(&ibuf);
863 static const struct got_error *
864 recv_packfile(struct imsg *imsg)
866 const struct got_error *err = NULL, *unpack_err;
867 struct repo_write_client *client = &repo_write_client;
868 struct gotd_imsg_recv_packfile ireq;
869 FILE *tempfiles[3] = { NULL, NULL, NULL };
870 struct repo_tempfile {
873 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
877 struct got_ratelimit rl;
878 struct got_pack *pack = NULL;
879 off_t pack_filesize = 0;
881 log_debug("packfile request received");
883 got_ratelimit_init(&rl, 2, 0);
885 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
886 if (datalen != sizeof(ireq))
887 return got_error(GOT_ERR_PRIVSEP_LEN);
888 memcpy(&ireq, imsg->data, sizeof(ireq));
890 if (client->pack_pipe == -1 || client->packidx_fd == -1)
891 return got_error(GOT_ERR_PRIVSEP_NO_FD);
893 imsg_init(&ibuf, client->fd);
896 return got_error(GOT_ERR_PRIVSEP_NO_FD);
898 pack = &client->pack;
899 memset(pack, 0, sizeof(*pack));
901 err = got_delta_cache_alloc(&pack->delta_cache);
905 for (i = 0; i < nitems(repo_tempfiles); i++) {
906 struct repo_tempfile *t = &repo_tempfiles[i];
907 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
912 for (i = 0; i < nitems(tempfiles); i++) {
913 int fd = dup(repo_tempfiles[i].fd);
916 err = got_error_from_errno("dup");
919 f = fdopen(fd, "w+");
921 err = got_error_from_errno("dup");
928 /* Send pack file pipe to gotsh(1). */
929 if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
930 repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
931 (*client)->pack_pipe[1] = -1;
932 err = got_error_from_errno("imsg_compose ACK");
936 (*client)->pack_pipe[1] = -1;
937 err = gotd_imsg_flush(&ibuf);
941 log_debug("receiving pack data");
942 unpack_err = recv_packdata(&pack_filesize, client->pack_sha1,
943 client->pack_pipe, pack->fd);
944 if (ireq.report_status) {
945 err = report_pack_status(unpack_err);
947 /* Git clients hang up after sending the pack file. */
948 if (err->code == GOT_ERR_EOF)
957 log_debug("pack data received");
959 pack->filesize = pack_filesize;
961 log_debug("begin indexing pack (%lld bytes in size)",
962 (long long)pack->filesize);
963 err = got_pack_index(pack, client->packidx_fd,
964 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
965 pack_index_progress, NULL, &rl);
968 log_debug("done indexing pack");
970 if (fsync(client->packidx_fd) == -1) {
971 err = got_error_from_errno("fsync");
974 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
975 err = got_error_from_errno("lseek");
977 if (close(client->pack_pipe) == -1 && err == NULL)
978 err = got_error_from_errno("close");
979 client->pack_pipe = -1;
980 for (i = 0; i < nitems(repo_tempfiles); i++) {
981 struct repo_tempfile *t = &repo_tempfiles[i];
983 got_repo_temp_fds_put(t->idx, repo_write.repo);
985 for (i = 0; i < nitems(tempfiles); i++) {
986 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
987 err = got_error_from_errno("fclose");
990 got_pack_close(pack);
995 static const struct got_error *
996 verify_packfile(void)
998 const struct got_error *err = NULL, *close_err;
999 struct repo_write_client *client = &repo_write_client;
1000 struct gotd_ref_update *ref_update;
1001 struct got_packidx *packidx = NULL;
1003 char *id_str = NULL;
1006 if (STAILQ_EMPTY(&client->ref_updates)) {
1007 return got_error_msg(GOT_ERR_BAD_REQUEST,
1008 "cannot verify pack file without any ref-updates");
1011 if (client->pack.fd == -1) {
1012 return got_error_msg(GOT_ERR_BAD_REQUEST,
1013 "invalid pack file handle during pack verification");
1015 if (client->packidx_fd == -1) {
1016 return got_error_msg(GOT_ERR_BAD_REQUEST,
1017 "invalid pack index handle during pack verification");
1020 if (fstat(client->packidx_fd, &sb) == -1)
1021 return got_error_from_errno("pack index fstat");
1023 packidx = malloc(sizeof(*packidx));
1024 memset(packidx, 0, sizeof(*packidx));
1025 packidx->fd = client->packidx_fd;
1026 client->packidx_fd = -1;
1027 packidx->len = sb.st_size;
1029 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1033 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1034 err = got_object_id_str(&id_str, &ref_update->new_id);
1038 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1040 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1041 "advertised object %s is missing from pack file",
1048 close_err = got_packidx_close(packidx);
1049 if (close_err && err == NULL)
1055 static const struct got_error *
1056 install_packfile(struct gotd_imsgev *iev)
1058 struct repo_write_client *client = &repo_write_client;
1059 struct gotd_imsg_packfile_install inst;
1062 memset(&inst, 0, sizeof(inst));
1063 inst.client_id = client->id;
1064 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1066 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1067 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1069 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1074 static const struct got_error *
1075 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1077 struct repo_write_client *client = &repo_write_client;
1078 struct gotd_imsg_ref_updates_start istart;
1081 memset(&istart, 0, sizeof(istart));
1082 istart.nref_updates = nref_updates;
1083 istart.client_id = client->id;
1085 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1086 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1088 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1094 static const struct got_error *
1095 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1097 struct repo_write_client *client = &repo_write_client;
1098 struct gotd_imsg_ref_update iref;
1099 const char *refname = got_ref_get_name(ref_update->ref);
1103 memset(&iref, 0, sizeof(iref));
1104 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1105 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1106 iref.ref_is_new = ref_update->ref_is_new;
1107 iref.client_id = client->id;
1108 iref.name_len = strlen(refname);
1110 len = sizeof(iref) + iref.name_len;
1111 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1112 repo_write.pid, len);
1114 return got_error_from_errno("imsg_create REF_UPDATE");
1116 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1117 return got_error_from_errno("imsg_add REF_UPDATE");
1118 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1119 return got_error_from_errno("imsg_add REF_UPDATE");
1122 imsg_close(&iev->ibuf, wbuf);
1124 gotd_imsg_event_add(iev);
1128 static const struct got_error *
1129 update_refs(struct gotd_imsgev *iev)
1131 const struct got_error *err = NULL;
1132 struct repo_write_client *client = &repo_write_client;
1133 struct gotd_ref_update *ref_update;
1135 err = send_ref_updates_start(client->nref_updates, iev);
1139 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1140 err = send_ref_update(ref_update, iev);
1148 static const struct got_error *
1149 recv_disconnect(struct imsg *imsg)
1151 const struct got_error *err = NULL;
1152 struct gotd_imsg_disconnect idisconnect;
1154 int pack_pipe = -1, idxfd = -1;
1155 struct repo_write_client *client = &repo_write_client;
1157 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1158 if (datalen != sizeof(idisconnect))
1159 return got_error(GOT_ERR_PRIVSEP_LEN);
1160 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1162 log_debug("client disconnecting");
1164 while (!STAILQ_EMPTY(&client->ref_updates)) {
1165 struct gotd_ref_update *ref_update;
1166 ref_update = STAILQ_FIRST(&client->ref_updates);
1167 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1168 got_ref_close(ref_update->ref);
1171 err = got_pack_close(&client->pack);
1172 if (client->fd != -1 && close(client->fd) == -1)
1173 err = got_error_from_errno("close");
1174 pack_pipe = client->pack_pipe;
1175 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1176 err = got_error_from_errno("close");
1177 idxfd = client->packidx_fd;
1178 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1179 err = got_error_from_errno("close");
1183 static const struct got_error *
1184 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1186 struct repo_write_client *client = &repo_write_client;
1187 struct gotd_imsg_packfile_pipe ireq;
1190 log_debug("receving pack pipe descriptor");
1193 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1195 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1196 if (datalen != sizeof(ireq))
1197 return got_error(GOT_ERR_PRIVSEP_LEN);
1198 memcpy(&ireq, imsg->data, sizeof(ireq));
1200 if (client->pack_pipe != -1)
1201 return got_error(GOT_ERR_PRIVSEP_MSG);
1203 client->pack_pipe = imsg->fd;
1207 static const struct got_error *
1208 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1210 struct repo_write_client *client = &repo_write_client;
1211 struct gotd_imsg_packidx_file ireq;
1214 log_debug("receving pack index output file");
1217 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1219 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1220 if (datalen != sizeof(ireq))
1221 return got_error(GOT_ERR_PRIVSEP_LEN);
1222 memcpy(&ireq, imsg->data, sizeof(ireq));
1224 if (client->packidx_fd != -1)
1225 return got_error(GOT_ERR_PRIVSEP_MSG);
1227 client->packidx_fd = imsg->fd;
1232 repo_write_dispatch_session(int fd, short event, void *arg)
1234 const struct got_error *err = NULL;
1235 struct gotd_imsgev *iev = arg;
1236 struct imsgbuf *ibuf = &iev->ibuf;
1238 struct repo_write_client *client = &repo_write_client;
1242 if (event & EV_READ) {
1243 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1244 fatal("imsg_read error");
1245 if (n == 0) /* Connection closed. */
1249 if (event & EV_WRITE) {
1250 n = msgbuf_write(&ibuf->w);
1251 if (n == -1 && errno != EAGAIN)
1252 fatal("msgbuf_write");
1253 if (n == 0) /* Connection closed. */
1258 if ((n = imsg_get(ibuf, &imsg)) == -1)
1259 fatal("%s: imsg_get error", __func__);
1260 if (n == 0) /* No more messages. */
1263 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1265 err = got_error(GOT_ERR_PRIVSEP_MSG);
1269 switch (imsg.hdr.type) {
1270 case GOTD_IMSG_LIST_REFS_INTERNAL:
1271 err = list_refs(&imsg);
1273 log_warnx("%s: ls-refs: %s", repo_write.title,
1276 case GOTD_IMSG_REF_UPDATE:
1277 err = recv_ref_update(&imsg);
1279 log_warnx("%s: ref-update: %s",
1280 repo_write.title, err->msg);
1282 case GOTD_IMSG_PACKFILE_PIPE:
1283 err = receive_pack_pipe(&imsg, iev);
1285 log_warnx("%s: receiving pack pipe: %s",
1286 repo_write.title, err->msg);
1290 case GOTD_IMSG_PACKIDX_FILE:
1291 err = receive_pack_idx(&imsg, iev);
1293 log_warnx("%s: receiving pack index: %s",
1294 repo_write.title, err->msg);
1298 case GOTD_IMSG_RECV_PACKFILE:
1299 err = recv_packfile(&imsg);
1301 log_warnx("%s: receive packfile: %s",
1302 repo_write.title, err->msg);
1305 err = verify_packfile();
1307 log_warnx("%s: verify packfile: %s",
1308 repo_write.title, err->msg);
1311 err = install_packfile(iev);
1313 log_warnx("%s: install packfile: %s",
1314 repo_write.title, err->msg);
1317 err = update_refs(iev);
1319 log_warnx("%s: update refs: %s",
1320 repo_write.title, err->msg);
1323 case GOTD_IMSG_DISCONNECT:
1324 err = recv_disconnect(&imsg);
1326 log_warnx("%s: disconnect: %s",
1327 repo_write.title, err->msg);
1331 log_debug("%s: unexpected imsg %d", repo_write.title,
1339 if (!shut && check_cancelled(NULL) == NULL) {
1341 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1342 client->id, err) == -1) {
1343 log_warnx("could not send error to parent: %s",
1346 gotd_imsg_event_add(iev);
1348 /* This pipe is dead. Remove its event handler */
1349 event_del(&iev->ev);
1350 event_loopexit(NULL);
1354 static const struct got_error *
1355 recv_connect(struct imsg *imsg)
1357 struct gotd_imsgev *iev = &repo_write.session_iev;
1360 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1362 return got_error(GOT_ERR_PRIVSEP_LEN);
1364 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1366 if (repo_write.session_fd != -1)
1367 return got_error(GOT_ERR_PRIVSEP_MSG);
1369 repo_write.session_fd = imsg->fd;
1371 imsg_init(&iev->ibuf, repo_write.session_fd);
1372 iev->handler = repo_write_dispatch_session;
1373 iev->events = EV_READ;
1374 iev->handler_arg = NULL;
1375 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1376 repo_write_dispatch_session, iev);
1377 gotd_imsg_event_add(iev);
1383 repo_write_dispatch(int fd, short event, void *arg)
1385 const struct got_error *err = NULL;
1386 struct gotd_imsgev *iev = arg;
1387 struct imsgbuf *ibuf = &iev->ibuf;
1391 struct repo_write_client *client = &repo_write_client;
1393 if (event & EV_READ) {
1394 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1395 fatal("imsg_read error");
1396 if (n == 0) /* Connection closed. */
1400 if (event & EV_WRITE) {
1401 n = msgbuf_write(&ibuf->w);
1402 if (n == -1 && errno != EAGAIN)
1403 fatal("msgbuf_write");
1404 if (n == 0) /* Connection closed. */
1408 while (err == NULL && check_cancelled(NULL) == NULL) {
1409 if ((n = imsg_get(ibuf, &imsg)) == -1)
1410 fatal("%s: imsg_get", __func__);
1411 if (n == 0) /* No more messages. */
1414 switch (imsg.hdr.type) {
1415 case GOTD_IMSG_CONNECT_REPO_CHILD:
1416 err = recv_connect(&imsg);
1419 log_debug("%s: unexpected imsg %d", repo_write.title,
1427 if (!shut && check_cancelled(NULL) == NULL) {
1429 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1430 client->id, err) == -1) {
1431 log_warnx("could not send error to parent: %s",
1434 gotd_imsg_event_add(iev);
1436 /* This pipe is dead. Remove its event handler */
1437 event_del(&iev->ev);
1438 event_loopexit(NULL);
1443 repo_write_main(const char *title, const char *repo_path,
1444 int *pack_fds, int *temp_fds)
1446 const struct got_error *err = NULL;
1447 struct gotd_imsgev iev;
1449 repo_write.title = title;
1450 repo_write.pid = getpid();
1451 repo_write.pack_fds = pack_fds;
1452 repo_write.temp_fds = temp_fds;
1453 repo_write.session_fd = -1;
1454 repo_write.session_iev.ibuf.fd = -1;
1456 STAILQ_INIT(&repo_write_client.ref_updates);
1458 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1461 if (!got_repo_is_bare(repo_write.repo)) {
1462 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1463 "bare git repository required");
1467 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1469 signal(SIGINT, catch_sigint);
1470 signal(SIGTERM, catch_sigterm);
1471 signal(SIGPIPE, SIG_IGN);
1472 signal(SIGHUP, SIG_IGN);
1474 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1475 iev.handler = repo_write_dispatch;
1476 iev.events = EV_READ;
1477 iev.handler_arg = NULL;
1478 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1479 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1480 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1481 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1488 log_warnx("%s: %s", title, err->msg);
1489 repo_write_shutdown();
1493 repo_write_shutdown(void)
1495 log_debug("%s: shutting down", repo_write.title);
1496 if (repo_write.repo)
1497 got_repo_close(repo_write.repo);
1498 got_repo_pack_fds_close(repo_write.pack_fds);
1499 got_repo_temp_fds_close(repo_write.temp_fds);
1500 if (repo_write.session_fd != -1)
1501 close(repo_write.session_fd);