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 err = gotd_imsg_flush(&ibuf);
932 log_debug("receiving pack data");
933 unpack_err = recv_packdata(&pack_filesize, client->pack_sha1,
934 client->pack_pipe, pack->fd);
935 if (ireq.report_status) {
936 err = report_pack_status(unpack_err);
938 /* Git clients hang up after sending the pack file. */
939 if (err->code == GOT_ERR_EOF)
948 log_debug("pack data received");
950 pack->filesize = pack_filesize;
952 log_debug("begin indexing pack (%lld bytes in size)",
953 (long long)pack->filesize);
954 err = got_pack_index(pack, client->packidx_fd,
955 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
956 pack_index_progress, NULL, &rl);
959 log_debug("done indexing pack");
961 if (fsync(client->packidx_fd) == -1) {
962 err = got_error_from_errno("fsync");
965 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
966 err = got_error_from_errno("lseek");
968 if (close(client->pack_pipe) == -1 && err == NULL)
969 err = got_error_from_errno("close");
970 client->pack_pipe = -1;
971 for (i = 0; i < nitems(repo_tempfiles); i++) {
972 struct repo_tempfile *t = &repo_tempfiles[i];
974 got_repo_temp_fds_put(t->idx, repo_write.repo);
976 for (i = 0; i < nitems(tempfiles); i++) {
977 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
978 err = got_error_from_errno("fclose");
981 got_pack_close(pack);
986 static const struct got_error *
987 verify_packfile(void)
989 const struct got_error *err = NULL, *close_err;
990 struct repo_write_client *client = &repo_write_client;
991 struct gotd_ref_update *ref_update;
992 struct got_packidx *packidx = NULL;
997 if (STAILQ_EMPTY(&client->ref_updates)) {
998 return got_error_msg(GOT_ERR_BAD_REQUEST,
999 "cannot verify pack file without any ref-updates");
1002 if (client->pack.fd == -1) {
1003 return got_error_msg(GOT_ERR_BAD_REQUEST,
1004 "invalid pack file handle during pack verification");
1006 if (client->packidx_fd == -1) {
1007 return got_error_msg(GOT_ERR_BAD_REQUEST,
1008 "invalid pack index handle during pack verification");
1011 if (fstat(client->packidx_fd, &sb) == -1)
1012 return got_error_from_errno("pack index fstat");
1014 packidx = malloc(sizeof(*packidx));
1015 memset(packidx, 0, sizeof(*packidx));
1016 packidx->fd = client->packidx_fd;
1017 client->packidx_fd = -1;
1018 packidx->len = sb.st_size;
1020 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1024 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1025 err = got_object_id_str(&id_str, &ref_update->new_id);
1029 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1031 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1032 "advertised object %s is missing from pack file",
1039 close_err = got_packidx_close(packidx);
1040 if (close_err && err == NULL)
1046 static const struct got_error *
1047 install_packfile(struct gotd_imsgev *iev)
1049 struct repo_write_client *client = &repo_write_client;
1050 struct gotd_imsg_packfile_install inst;
1053 memset(&inst, 0, sizeof(inst));
1054 inst.client_id = client->id;
1055 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1057 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1058 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1060 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1065 static const struct got_error *
1066 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1068 struct repo_write_client *client = &repo_write_client;
1069 struct gotd_imsg_ref_updates_start istart;
1072 memset(&istart, 0, sizeof(istart));
1073 istart.nref_updates = nref_updates;
1074 istart.client_id = client->id;
1076 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1077 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1079 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1085 static const struct got_error *
1086 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1088 struct repo_write_client *client = &repo_write_client;
1089 struct gotd_imsg_ref_update iref;
1090 const char *refname = got_ref_get_name(ref_update->ref);
1094 memset(&iref, 0, sizeof(iref));
1095 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1096 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1097 iref.ref_is_new = ref_update->ref_is_new;
1098 iref.client_id = client->id;
1099 iref.name_len = strlen(refname);
1101 len = sizeof(iref) + iref.name_len;
1102 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1103 repo_write.pid, len);
1105 return got_error_from_errno("imsg_create REF_UPDATE");
1107 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1108 return got_error_from_errno("imsg_add REF_UPDATE");
1109 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1110 return got_error_from_errno("imsg_add REF_UPDATE");
1113 imsg_close(&iev->ibuf, wbuf);
1115 gotd_imsg_event_add(iev);
1119 static const struct got_error *
1120 update_refs(struct gotd_imsgev *iev)
1122 const struct got_error *err = NULL;
1123 struct repo_write_client *client = &repo_write_client;
1124 struct gotd_ref_update *ref_update;
1126 err = send_ref_updates_start(client->nref_updates, iev);
1130 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1131 err = send_ref_update(ref_update, iev);
1139 static const struct got_error *
1140 recv_disconnect(struct imsg *imsg)
1142 const struct got_error *err = NULL;
1143 struct gotd_imsg_disconnect idisconnect;
1145 int pack_pipe = -1, idxfd = -1;
1146 struct repo_write_client *client = &repo_write_client;
1148 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1149 if (datalen != sizeof(idisconnect))
1150 return got_error(GOT_ERR_PRIVSEP_LEN);
1151 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1153 log_debug("client disconnecting");
1155 while (!STAILQ_EMPTY(&client->ref_updates)) {
1156 struct gotd_ref_update *ref_update;
1157 ref_update = STAILQ_FIRST(&client->ref_updates);
1158 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1159 got_ref_close(ref_update->ref);
1162 err = got_pack_close(&client->pack);
1163 if (client->fd != -1 && close(client->fd) == -1)
1164 err = got_error_from_errno("close");
1165 pack_pipe = client->pack_pipe;
1166 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1167 err = got_error_from_errno("close");
1168 idxfd = client->packidx_fd;
1169 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1170 err = got_error_from_errno("close");
1174 static const struct got_error *
1175 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1177 struct repo_write_client *client = &repo_write_client;
1178 struct gotd_imsg_packfile_pipe ireq;
1181 log_debug("receving pack pipe descriptor");
1184 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1186 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1187 if (datalen != sizeof(ireq))
1188 return got_error(GOT_ERR_PRIVSEP_LEN);
1189 memcpy(&ireq, imsg->data, sizeof(ireq));
1191 if (client->pack_pipe != -1)
1192 return got_error(GOT_ERR_PRIVSEP_MSG);
1194 client->pack_pipe = imsg->fd;
1198 static const struct got_error *
1199 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1201 struct repo_write_client *client = &repo_write_client;
1202 struct gotd_imsg_packidx_file ireq;
1205 log_debug("receving pack index output file");
1208 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1210 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1211 if (datalen != sizeof(ireq))
1212 return got_error(GOT_ERR_PRIVSEP_LEN);
1213 memcpy(&ireq, imsg->data, sizeof(ireq));
1215 if (client->packidx_fd != -1)
1216 return got_error(GOT_ERR_PRIVSEP_MSG);
1218 client->packidx_fd = imsg->fd;
1223 repo_write_dispatch_session(int fd, short event, void *arg)
1225 const struct got_error *err = NULL;
1226 struct gotd_imsgev *iev = arg;
1227 struct imsgbuf *ibuf = &iev->ibuf;
1229 struct repo_write_client *client = &repo_write_client;
1233 if (event & EV_READ) {
1234 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1235 fatal("imsg_read error");
1236 if (n == 0) /* Connection closed. */
1240 if (event & EV_WRITE) {
1241 n = msgbuf_write(&ibuf->w);
1242 if (n == -1 && errno != EAGAIN)
1243 fatal("msgbuf_write");
1244 if (n == 0) /* Connection closed. */
1249 if ((n = imsg_get(ibuf, &imsg)) == -1)
1250 fatal("%s: imsg_get error", __func__);
1251 if (n == 0) /* No more messages. */
1254 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1256 err = got_error(GOT_ERR_PRIVSEP_MSG);
1260 switch (imsg.hdr.type) {
1261 case GOTD_IMSG_LIST_REFS_INTERNAL:
1262 err = list_refs(&imsg);
1264 log_warnx("%s: ls-refs: %s", repo_write.title,
1267 case GOTD_IMSG_REF_UPDATE:
1268 err = recv_ref_update(&imsg);
1270 log_warnx("%s: ref-update: %s",
1271 repo_write.title, err->msg);
1273 case GOTD_IMSG_PACKFILE_PIPE:
1274 err = receive_pack_pipe(&imsg, iev);
1276 log_warnx("%s: receiving pack pipe: %s",
1277 repo_write.title, err->msg);
1281 case GOTD_IMSG_PACKIDX_FILE:
1282 err = receive_pack_idx(&imsg, iev);
1284 log_warnx("%s: receiving pack index: %s",
1285 repo_write.title, err->msg);
1289 case GOTD_IMSG_RECV_PACKFILE:
1290 err = recv_packfile(&imsg);
1292 log_warnx("%s: receive packfile: %s",
1293 repo_write.title, err->msg);
1296 err = verify_packfile();
1298 log_warnx("%s: verify packfile: %s",
1299 repo_write.title, err->msg);
1302 err = install_packfile(iev);
1304 log_warnx("%s: install packfile: %s",
1305 repo_write.title, err->msg);
1308 err = update_refs(iev);
1310 log_warnx("%s: update refs: %s",
1311 repo_write.title, err->msg);
1314 case GOTD_IMSG_DISCONNECT:
1315 err = recv_disconnect(&imsg);
1317 log_warnx("%s: disconnect: %s",
1318 repo_write.title, err->msg);
1322 log_debug("%s: unexpected imsg %d", repo_write.title,
1330 if (!shut && check_cancelled(NULL) == NULL) {
1332 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1333 client->id, err) == -1) {
1334 log_warnx("could not send error to parent: %s",
1337 gotd_imsg_event_add(iev);
1339 /* This pipe is dead. Remove its event handler */
1340 event_del(&iev->ev);
1341 event_loopexit(NULL);
1345 static const struct got_error *
1346 recv_connect(struct imsg *imsg)
1348 struct gotd_imsgev *iev = &repo_write.session_iev;
1351 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1353 return got_error(GOT_ERR_PRIVSEP_LEN);
1355 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1357 if (repo_write.session_fd != -1)
1358 return got_error(GOT_ERR_PRIVSEP_MSG);
1360 repo_write.session_fd = imsg->fd;
1362 imsg_init(&iev->ibuf, repo_write.session_fd);
1363 iev->handler = repo_write_dispatch_session;
1364 iev->events = EV_READ;
1365 iev->handler_arg = NULL;
1366 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1367 repo_write_dispatch_session, iev);
1368 gotd_imsg_event_add(iev);
1374 repo_write_dispatch(int fd, short event, void *arg)
1376 const struct got_error *err = NULL;
1377 struct gotd_imsgev *iev = arg;
1378 struct imsgbuf *ibuf = &iev->ibuf;
1382 struct repo_write_client *client = &repo_write_client;
1384 if (event & EV_READ) {
1385 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1386 fatal("imsg_read error");
1387 if (n == 0) /* Connection closed. */
1391 if (event & EV_WRITE) {
1392 n = msgbuf_write(&ibuf->w);
1393 if (n == -1 && errno != EAGAIN)
1394 fatal("msgbuf_write");
1395 if (n == 0) /* Connection closed. */
1399 while (err == NULL && check_cancelled(NULL) == NULL) {
1400 if ((n = imsg_get(ibuf, &imsg)) == -1)
1401 fatal("%s: imsg_get", __func__);
1402 if (n == 0) /* No more messages. */
1405 switch (imsg.hdr.type) {
1406 case GOTD_IMSG_CONNECT_REPO_CHILD:
1407 err = recv_connect(&imsg);
1410 log_debug("%s: unexpected imsg %d", repo_write.title,
1418 if (!shut && check_cancelled(NULL) == NULL) {
1420 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1421 client->id, err) == -1) {
1422 log_warnx("could not send error to parent: %s",
1425 gotd_imsg_event_add(iev);
1427 /* This pipe is dead. Remove its event handler */
1428 event_del(&iev->ev);
1429 event_loopexit(NULL);
1434 repo_write_main(const char *title, const char *repo_path,
1435 int *pack_fds, int *temp_fds)
1437 const struct got_error *err = NULL;
1438 struct gotd_imsgev iev;
1440 repo_write.title = title;
1441 repo_write.pid = getpid();
1442 repo_write.pack_fds = pack_fds;
1443 repo_write.temp_fds = temp_fds;
1444 repo_write.session_fd = -1;
1445 repo_write.session_iev.ibuf.fd = -1;
1447 STAILQ_INIT(&repo_write_client.ref_updates);
1449 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1452 if (!got_repo_is_bare(repo_write.repo)) {
1453 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1454 "bare git repository required");
1458 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1460 signal(SIGINT, catch_sigint);
1461 signal(SIGTERM, catch_sigterm);
1462 signal(SIGPIPE, SIG_IGN);
1463 signal(SIGHUP, SIG_IGN);
1465 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1466 iev.handler = repo_write_dispatch;
1467 iev.events = EV_READ;
1468 iev.handler_arg = NULL;
1469 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1470 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1471 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1472 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1479 log_warnx("%s: %s", title, err->msg);
1480 repo_write_shutdown();
1484 repo_write_shutdown(void)
1486 log_debug("%s: shutting down", repo_write.title);
1487 if (repo_write.repo)
1488 got_repo_close(repo_write.repo);
1489 got_repo_pack_fds_close(repo_write.pack_fds);
1490 got_repo_temp_fds_close(repo_write.temp_fds);
1491 if (repo_write.session_fd != -1)
1492 close(repo_write.session_fd);