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;
72 struct gotd_ref_update {
73 STAILQ_ENTRY(gotd_ref_update) entry;
74 struct got_reference *ref;
76 struct got_object_id old_id;
77 struct got_object_id new_id;
79 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
81 static struct repo_write_client {
86 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
88 struct gotd_ref_updates ref_updates;
92 static volatile sig_atomic_t sigint_received;
93 static volatile sig_atomic_t sigterm_received;
96 catch_sigint(int signo)
102 catch_sigterm(int signo)
104 sigterm_received = 1;
107 static const struct got_error *
108 check_cancelled(void *arg)
110 if (sigint_received || sigterm_received)
111 return got_error(GOT_ERR_CANCELLED);
116 static const struct got_error *
117 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
118 struct imsgbuf *ibuf)
120 const struct got_error *err = NULL;
121 struct got_tag_object *tag;
123 char *peeled_refname = NULL;
124 struct got_object_id *id;
127 err = got_object_tag_open(&tag, repo_write.repo, obj);
131 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
132 err = got_error_from_errno("asprintf");
136 id = got_object_tag_get_object_id(tag);
137 namelen = strlen(peeled_refname);
139 len = sizeof(struct gotd_imsg_ref) + namelen;
140 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
141 err = got_error(GOT_ERR_NO_SPACE);
145 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
146 repo_write.pid, len);
148 err = got_error_from_errno("imsg_create REF");
152 /* Keep in sync with struct gotd_imsg_ref definition. */
153 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
154 err = got_error_from_errno("imsg_add REF");
157 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
158 err = got_error_from_errno("imsg_add REF");
161 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
162 err = got_error_from_errno("imsg_add REF");
167 imsg_close(ibuf, wbuf);
169 got_object_tag_close(tag);
173 static const struct got_error *
174 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
176 const struct got_error *err;
177 const char *refname = got_ref_get_name(ref);
179 struct got_object_id *id = NULL;
180 struct got_object *obj = NULL;
184 namelen = strlen(refname);
186 len = sizeof(struct gotd_imsg_ref) + namelen;
187 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
188 return got_error(GOT_ERR_NO_SPACE);
190 err = got_ref_resolve(&id, repo_write.repo, ref);
194 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
195 repo_write.pid, len);
197 err = got_error_from_errno("imsg_create REF");
201 /* Keep in sync with struct gotd_imsg_ref definition. */
202 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
203 return got_error_from_errno("imsg_add REF");
204 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
205 return got_error_from_errno("imsg_add REF");
206 if (imsg_add(wbuf, refname, namelen) == -1)
207 return got_error_from_errno("imsg_add REF");
210 imsg_close(ibuf, wbuf);
212 err = got_object_open(&obj, repo_write.repo, id);
215 if (obj->type == GOT_OBJ_TYPE_TAG)
216 err = send_peeled_tag_ref(ref, obj, ibuf);
219 got_object_close(obj);
224 static const struct got_error *
225 list_refs(struct imsg *imsg)
227 const struct got_error *err;
228 struct repo_write_client *client = &repo_write_client;
229 struct got_reflist_head refs;
230 struct got_reflist_entry *re;
231 struct gotd_imsg_list_refs_internal ireq;
233 struct gotd_imsg_reflist irefs;
235 int client_fd = imsg->fd;
240 return got_error(GOT_ERR_PRIVSEP_NO_FD);
242 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
243 if (datalen != sizeof(ireq))
244 return got_error(GOT_ERR_PRIVSEP_LEN);
245 memcpy(&ireq, imsg->data, sizeof(ireq));
247 if (ireq.client_id == 0)
248 return got_error(GOT_ERR_CLIENT_ID);
249 if (client->id != 0) {
250 return got_error_msg(GOT_ERR_CLIENT_ID,
251 "duplicate list-refs request");
253 client->id = ireq.client_id;
254 client->fd = client_fd;
255 client->pack_pipe = -1;
256 client->packidx_fd = -1;
257 client->nref_updates = 0;
259 imsg_init(&ibuf, client_fd);
261 err = got_ref_list(&refs, repo_write.repo, "",
262 got_ref_cmp_by_name, NULL);
266 memset(&irefs, 0, sizeof(irefs));
267 TAILQ_FOREACH(re, &refs, entry) {
268 struct got_object_id *id;
271 if (got_ref_is_symbolic(re->ref))
276 /* Account for a peeled tag refs. */
277 err = got_ref_resolve(&id, repo_write.repo, re->ref);
280 err = got_object_get_type(&obj_type, repo_write.repo, id);
284 if (obj_type == GOT_OBJ_TYPE_TAG)
288 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
289 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
290 err = got_error_from_errno("imsg_compose REFLIST");
294 TAILQ_FOREACH(re, &refs, entry) {
295 if (got_ref_is_symbolic(re->ref))
297 err = send_ref(re->ref, &ibuf);
302 err = gotd_imsg_flush(&ibuf);
304 got_ref_list_free(&refs);
309 static const struct got_error *
310 protect_ref_namespace(struct got_reference *ref, const char *namespace)
312 size_t len = strlen(namespace);
314 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
315 namespace[len -1] != '/') {
316 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
317 "reference namespace '%s'", namespace);
320 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
321 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
326 static const struct got_error *
327 recv_ref_update(struct imsg *imsg)
329 const struct got_error *err = NULL;
330 struct repo_write_client *client = &repo_write_client;
331 struct gotd_imsg_ref_update iref;
333 char *refname = NULL;
334 struct got_reference *ref = NULL;
335 struct got_object_id *id = NULL;
337 struct gotd_ref_update *ref_update = NULL;
339 log_debug("ref-update received");
341 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
342 if (datalen < sizeof(iref))
343 return got_error(GOT_ERR_PRIVSEP_LEN);
344 memcpy(&iref, imsg->data, sizeof(iref));
345 if (datalen != sizeof(iref) + iref.name_len)
346 return got_error(GOT_ERR_PRIVSEP_LEN);
348 imsg_init(&ibuf, client->fd);
350 refname = malloc(iref.name_len + 1);
352 return got_error_from_errno("malloc");
353 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
354 refname[iref.name_len] = '\0';
356 ref_update = calloc(1, sizeof(*ref_update));
357 if (ref_update == NULL) {
358 err = got_error_from_errno("malloc");
362 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
363 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
365 err = got_ref_open(&ref, repo_write.repo, refname, 0);
367 if (err->code != GOT_ERR_NOT_REF)
369 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
372 ref_update->ref_is_new = 1;
374 if (got_ref_is_symbolic(ref)) {
375 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
376 "'%s' is a symbolic reference and cannot "
377 "be updated", got_ref_get_name(ref));
380 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
381 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
382 "%s: does not begin with 'refs/'",
383 got_ref_get_name(ref));
387 err = protect_ref_namespace(ref, "refs/got/");
390 err = protect_ref_namespace(ref, "refs/remotes/");
394 if (!ref_update->ref_is_new) {
396 * Ensure the client's idea of this update is still valid.
397 * At this point we can only return an error, to prevent
398 * the client from uploading a pack file which will likely
399 * have to be discarded.
401 err = got_ref_resolve(&id, repo_write.repo, ref);
405 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
406 err = got_error_fmt(GOT_ERR_REF_BUSY,
407 "%s has been modified by someone else "
408 "while transaction was in progress",
409 got_ref_get_name(ref));
414 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
417 ref_update->ref = ref;
418 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
419 client->nref_updates++;
431 static const struct got_error *
432 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
433 uint32_t nobj_loose, uint32_t nobj_resolved)
435 int p_indexed = 0, p_resolved = 0;
436 int nobj_delta = nobj_total - nobj_loose;
439 p_indexed = (nobj_indexed * 100) / nobj_total;
442 p_resolved = (nobj_resolved * 100) / nobj_delta;
444 if (p_resolved > 0) {
445 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
446 nobj_total, p_indexed, nobj_delta, p_resolved);
448 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
453 static const struct got_error *
454 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
456 const struct got_error *err = NULL;
457 uint8_t readahead[65536];
460 err = got_poll_read_full(infd, &have,
461 readahead, sizeof(readahead), minsize);
465 err = buf_append(&newlen, buf, readahead, have);
471 static const struct got_error *
472 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
473 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
475 const struct got_error *err = NULL;
480 off_t obj_offset = *outsize;
483 /* We do not support size values which don't fit in 64 bit. */
485 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
486 "packfile offset %lld", (long long)obj_offset);
488 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
489 err = read_more_pack_stream(infd, buf,
495 sizebuf[i] = buf_getc(buf, *buf_pos);
496 *buf_pos += sizeof(sizebuf[i]);
499 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
500 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
501 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
503 size_t shift = 4 + 7 * (i - 1);
504 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
508 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
510 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
520 static const struct got_error *
521 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
524 const struct got_error *err = NULL;
525 size_t remain = buf_len(buf) - *buf_pos;
527 if (remain < SHA1_DIGEST_LENGTH) {
528 err = read_more_pack_stream(infd, buf,
529 SHA1_DIGEST_LENGTH - remain);
534 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
535 SHA1_DIGEST_LENGTH, ctx);
539 *buf_pos += SHA1_DIGEST_LENGTH;
543 static const struct got_error *
544 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
547 const struct got_error *err = NULL;
551 off_t obj_offset = *outsize;
554 /* We do not support offset values which don't fit in 64 bit. */
556 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
557 "packfile offset %lld", (long long)obj_offset);
559 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
560 err = read_more_pack_stream(infd, buf,
566 offbuf[i] = buf_getc(buf, *buf_pos);
567 *buf_pos += sizeof(offbuf[i]);
570 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
574 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
577 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
579 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
580 return got_error(GOT_ERR_PACK_OFFSET);
582 err = got_pack_hwrite(outfd, offbuf, i, ctx);
590 static const struct got_error *
591 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
594 const struct got_error *err = NULL;
598 size_t consumed_total = 0;
599 off_t zstream_offset = *outsize;
601 memset(&z, 0, sizeof(z));
605 zret = inflateInit(&z);
608 return got_error_from_errno("inflateInit");
609 if (zret == Z_MEM_ERROR) {
611 return got_error_from_errno("inflateInit");
613 return got_error_msg(GOT_ERR_DECOMPRESSION,
614 "inflateInit failed");
617 while (zret != Z_STREAM_END) {
618 size_t last_total_in, consumed;
621 * Decompress into the void. Object data will be parsed
622 * later, when the pack file is indexed. For now, we just
623 * want to locate the end of the compressed stream.
625 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
626 last_total_in = z.total_in;
627 z.next_in = buf_get(buf) + *buf_pos;
628 z.avail_in = buf_len(buf) - *buf_pos;
629 z.next_out = voidbuf;
630 z.avail_out = sizeof(voidbuf);
632 zret = inflate(&z, Z_SYNC_FLUSH);
633 if (zret != Z_OK && zret != Z_BUF_ERROR &&
634 zret != Z_STREAM_END) {
635 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
636 "packfile offset %lld",
637 (long long)zstream_offset);
640 consumed = z.total_in - last_total_in;
642 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
647 err = buf_discard(buf, *buf_pos + consumed);
652 consumed_total += consumed;
655 if (zret != Z_STREAM_END) {
656 err = read_more_pack_stream(infd, buf, 1);
663 *outsize += consumed_total;
669 static const struct got_error *
670 validate_object_type(int obj_type)
673 case GOT_OBJ_TYPE_BLOB:
674 case GOT_OBJ_TYPE_COMMIT:
675 case GOT_OBJ_TYPE_TREE:
676 case GOT_OBJ_TYPE_TAG:
677 case GOT_OBJ_TYPE_REF_DELTA:
678 case GOT_OBJ_TYPE_OFFSET_DELTA:
684 return got_error(GOT_ERR_OBJ_TYPE);
687 static const struct got_error *
688 recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
690 const struct got_error *err;
691 struct got_packfile_hdr hdr;
693 uint32_t nobj, nhave = 0;
695 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
696 char hex[SHA1_DIGEST_STRING_LENGTH];
698 size_t buf_pos = 0, remain;
704 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
707 if (have != sizeof(hdr))
708 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
711 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
712 return got_error_msg(GOT_ERR_BAD_PACKFILE,
713 "bad packfile signature");
714 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
715 return got_error_msg(GOT_ERR_BAD_PACKFILE,
716 "bad packfile version");
718 nobj = be32toh(hdr.nobjects);
720 return got_error_msg(GOT_ERR_BAD_PACKFILE,
721 "bad packfile with zero objects");
723 log_debug("expecting %d objects", nobj);
725 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
729 err = buf_alloc(&buf, 65536);
733 while (nhave != nobj) {
737 err = copy_object_type_and_size(&obj_type, &obj_size,
738 infd, outfd, outsize, buf, &buf_pos, &ctx);
742 err = validate_object_type(obj_type);
746 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
747 err = copy_ref_delta(infd, outfd, outsize,
748 buf, &buf_pos, &ctx);
751 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
752 err = copy_offset_delta(infd, outfd, outsize,
753 buf, &buf_pos, &ctx);
758 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
765 log_debug("received %u objects", nobj);
767 SHA1Final(expected_sha1, &ctx);
769 remain = buf_len(buf) - buf_pos;
770 if (remain < SHA1_DIGEST_LENGTH) {
771 err = read_more_pack_stream(infd, buf,
772 SHA1_DIGEST_LENGTH - remain);
777 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
778 log_debug("expect SHA1: %s", hex);
779 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
780 log_debug("actual SHA1: %s", hex);
782 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
783 SHA1_DIGEST_LENGTH) != 0) {
784 err = got_error(GOT_ERR_PACKFILE_CSUM);
788 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
790 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
792 err = got_error_from_errno("write");
795 if (w != SHA1_DIGEST_LENGTH) {
796 err = got_error(GOT_ERR_IO);
800 *outsize += SHA1_DIGEST_LENGTH;
802 if (fsync(outfd) == -1) {
803 err = got_error_from_errno("fsync");
806 if (lseek(outfd, 0L, SEEK_SET) == -1) {
807 err = got_error_from_errno("lseek");
815 static const struct got_error *
816 report_pack_status(const struct got_error *unpack_err)
818 const struct got_error *err = NULL;
819 struct repo_write_client *client = &repo_write_client;
820 struct gotd_imsg_packfile_status istatus;
823 const char *unpack_ok = "unpack ok\n";
826 imsg_init(&ibuf, client->fd);
829 istatus.reason_len = strlen(unpack_err->msg);
831 istatus.reason_len = strlen(unpack_ok);
833 len = sizeof(istatus) + istatus.reason_len;
834 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
835 repo_write.pid, len);
837 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
841 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
842 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
846 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
847 istatus.reason_len) == -1) {
848 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
853 imsg_close(&ibuf, wbuf);
855 err = gotd_imsg_flush(&ibuf);
861 static const struct got_error *
862 recv_packfile(struct imsg *imsg)
864 const struct got_error *err = NULL, *unpack_err;
865 struct repo_write_client *client = &repo_write_client;
866 struct gotd_imsg_recv_packfile ireq;
867 FILE *tempfiles[3] = { NULL, NULL, NULL };
868 struct repo_tempfile {
871 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
875 struct got_ratelimit rl;
876 struct got_pack *pack = NULL;
877 off_t pack_filesize = 0;
879 log_debug("packfile request received");
881 got_ratelimit_init(&rl, 2, 0);
883 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
884 if (datalen != sizeof(ireq))
885 return got_error(GOT_ERR_PRIVSEP_LEN);
886 memcpy(&ireq, imsg->data, sizeof(ireq));
888 if (client->pack_pipe == -1 || client->packidx_fd == -1)
889 return got_error(GOT_ERR_PRIVSEP_NO_FD);
891 imsg_init(&ibuf, client->fd);
894 return got_error(GOT_ERR_PRIVSEP_NO_FD);
896 pack = &client->pack;
897 memset(pack, 0, sizeof(*pack));
899 err = got_delta_cache_alloc(&pack->delta_cache);
903 for (i = 0; i < nitems(repo_tempfiles); i++) {
904 struct repo_tempfile *t = &repo_tempfiles[i];
905 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
910 for (i = 0; i < nitems(tempfiles); i++) {
911 int fd = dup(repo_tempfiles[i].fd);
914 err = got_error_from_errno("dup");
917 f = fdopen(fd, "w+");
919 err = got_error_from_errno("dup");
926 err = gotd_imsg_flush(&ibuf);
930 log_debug("receiving pack data");
931 unpack_err = recv_packdata(&pack_filesize, client->pack_sha1,
932 client->pack_pipe, pack->fd);
933 if (ireq.report_status) {
934 err = report_pack_status(unpack_err);
936 /* Git clients hang up after sending the pack file. */
937 if (err->code == GOT_ERR_EOF)
946 log_debug("pack data received");
948 pack->filesize = pack_filesize;
950 log_debug("begin indexing pack (%lld bytes in size)",
951 (long long)pack->filesize);
952 err = got_pack_index(pack, client->packidx_fd,
953 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
954 pack_index_progress, NULL, &rl);
957 log_debug("done indexing pack");
959 if (fsync(client->packidx_fd) == -1) {
960 err = got_error_from_errno("fsync");
963 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
964 err = got_error_from_errno("lseek");
966 if (close(client->pack_pipe) == -1 && err == NULL)
967 err = got_error_from_errno("close");
968 client->pack_pipe = -1;
969 for (i = 0; i < nitems(repo_tempfiles); i++) {
970 struct repo_tempfile *t = &repo_tempfiles[i];
972 got_repo_temp_fds_put(t->idx, repo_write.repo);
974 for (i = 0; i < nitems(tempfiles); i++) {
975 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
976 err = got_error_from_errno("fclose");
979 got_pack_close(pack);
984 static const struct got_error *
985 verify_packfile(void)
987 const struct got_error *err = NULL, *close_err;
988 struct repo_write_client *client = &repo_write_client;
989 struct gotd_ref_update *ref_update;
990 struct got_packidx *packidx = NULL;
995 if (STAILQ_EMPTY(&client->ref_updates)) {
996 return got_error_msg(GOT_ERR_BAD_REQUEST,
997 "cannot verify pack file without any ref-updates");
1000 if (client->pack.fd == -1) {
1001 return got_error_msg(GOT_ERR_BAD_REQUEST,
1002 "invalid pack file handle during pack verification");
1004 if (client->packidx_fd == -1) {
1005 return got_error_msg(GOT_ERR_BAD_REQUEST,
1006 "invalid pack index handle during pack verification");
1009 if (fstat(client->packidx_fd, &sb) == -1)
1010 return got_error_from_errno("pack index fstat");
1012 packidx = malloc(sizeof(*packidx));
1013 memset(packidx, 0, sizeof(*packidx));
1014 packidx->fd = client->packidx_fd;
1015 client->packidx_fd = -1;
1016 packidx->len = sb.st_size;
1018 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1022 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1023 err = got_object_id_str(&id_str, &ref_update->new_id);
1027 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1029 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1030 "advertised object %s is missing from pack file",
1037 close_err = got_packidx_close(packidx);
1038 if (close_err && err == NULL)
1044 static const struct got_error *
1045 install_packfile(struct gotd_imsgev *iev)
1047 struct repo_write_client *client = &repo_write_client;
1048 struct gotd_imsg_packfile_install inst;
1051 memset(&inst, 0, sizeof(inst));
1052 inst.client_id = client->id;
1053 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1055 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1056 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1058 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1063 static const struct got_error *
1064 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1066 struct repo_write_client *client = &repo_write_client;
1067 struct gotd_imsg_ref_updates_start istart;
1070 memset(&istart, 0, sizeof(istart));
1071 istart.nref_updates = nref_updates;
1072 istart.client_id = client->id;
1074 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1075 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1077 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1083 static const struct got_error *
1084 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1086 struct repo_write_client *client = &repo_write_client;
1087 struct gotd_imsg_ref_update iref;
1088 const char *refname = got_ref_get_name(ref_update->ref);
1092 memset(&iref, 0, sizeof(iref));
1093 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1094 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1095 iref.ref_is_new = ref_update->ref_is_new;
1096 iref.client_id = client->id;
1097 iref.name_len = strlen(refname);
1099 len = sizeof(iref) + iref.name_len;
1100 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1101 repo_write.pid, len);
1103 return got_error_from_errno("imsg_create REF_UPDATE");
1105 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1106 return got_error_from_errno("imsg_add REF_UPDATE");
1107 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1108 return got_error_from_errno("imsg_add REF_UPDATE");
1111 imsg_close(&iev->ibuf, wbuf);
1113 gotd_imsg_event_add(iev);
1117 static const struct got_error *
1118 update_refs(struct gotd_imsgev *iev)
1120 const struct got_error *err = NULL;
1121 struct repo_write_client *client = &repo_write_client;
1122 struct gotd_ref_update *ref_update;
1124 err = send_ref_updates_start(client->nref_updates, iev);
1128 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1129 err = send_ref_update(ref_update, iev);
1137 static const struct got_error *
1138 recv_disconnect(struct imsg *imsg)
1140 const struct got_error *err = NULL;
1141 struct gotd_imsg_disconnect idisconnect;
1143 int pack_pipe = -1, idxfd = -1;
1144 struct repo_write_client *client = &repo_write_client;
1146 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1147 if (datalen != sizeof(idisconnect))
1148 return got_error(GOT_ERR_PRIVSEP_LEN);
1149 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1151 log_debug("client disconnecting");
1153 while (!STAILQ_EMPTY(&client->ref_updates)) {
1154 struct gotd_ref_update *ref_update;
1155 ref_update = STAILQ_FIRST(&client->ref_updates);
1156 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1157 got_ref_close(ref_update->ref);
1160 err = got_pack_close(&client->pack);
1161 if (client->fd != -1 && close(client->fd) == -1)
1162 err = got_error_from_errno("close");
1163 pack_pipe = client->pack_pipe;
1164 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1165 err = got_error_from_errno("close");
1166 idxfd = client->packidx_fd;
1167 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1168 err = got_error_from_errno("close");
1172 static const struct got_error *
1173 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1175 struct repo_write_client *client = &repo_write_client;
1176 struct gotd_imsg_packfile_pipe ireq;
1179 log_debug("receving pack pipe descriptor");
1182 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1184 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1185 if (datalen != sizeof(ireq))
1186 return got_error(GOT_ERR_PRIVSEP_LEN);
1187 memcpy(&ireq, imsg->data, sizeof(ireq));
1189 if (client->pack_pipe != -1)
1190 return got_error(GOT_ERR_PRIVSEP_MSG);
1192 client->pack_pipe = imsg->fd;
1196 static const struct got_error *
1197 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1199 struct repo_write_client *client = &repo_write_client;
1200 struct gotd_imsg_packidx_file ireq;
1203 log_debug("receving pack index output file");
1206 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1208 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1209 if (datalen != sizeof(ireq))
1210 return got_error(GOT_ERR_PRIVSEP_LEN);
1211 memcpy(&ireq, imsg->data, sizeof(ireq));
1213 if (client->packidx_fd != -1)
1214 return got_error(GOT_ERR_PRIVSEP_MSG);
1216 client->packidx_fd = imsg->fd;
1221 repo_write_dispatch(int fd, short event, void *arg)
1223 const struct got_error *err = NULL;
1224 struct gotd_imsgev *iev = arg;
1225 struct imsgbuf *ibuf = &iev->ibuf;
1227 struct repo_write_client *client = &repo_write_client;
1231 if (event & EV_READ) {
1232 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1233 fatal("imsg_read error");
1234 if (n == 0) /* Connection closed. */
1238 if (event & EV_WRITE) {
1239 n = msgbuf_write(&ibuf->w);
1240 if (n == -1 && errno != EAGAIN)
1241 fatal("msgbuf_write");
1242 if (n == 0) /* Connection closed. */
1247 if ((n = imsg_get(ibuf, &imsg)) == -1)
1248 fatal("%s: imsg_get error", __func__);
1249 if (n == 0) /* No more messages. */
1252 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1254 err = got_error(GOT_ERR_PRIVSEP_MSG);
1258 switch (imsg.hdr.type) {
1259 case GOTD_IMSG_LIST_REFS_INTERNAL:
1260 err = list_refs(&imsg);
1262 log_warnx("%s: ls-refs: %s", repo_write.title,
1265 case GOTD_IMSG_REF_UPDATE:
1266 err = recv_ref_update(&imsg);
1268 log_warnx("%s: ref-update: %s",
1269 repo_write.title, err->msg);
1271 case GOTD_IMSG_PACKFILE_PIPE:
1272 err = receive_pack_pipe(&imsg, iev);
1274 log_warnx("%s: receiving pack pipe: %s",
1275 repo_write.title, err->msg);
1279 case GOTD_IMSG_PACKIDX_FILE:
1280 err = receive_pack_idx(&imsg, iev);
1282 log_warnx("%s: receiving pack index: %s",
1283 repo_write.title, err->msg);
1287 case GOTD_IMSG_RECV_PACKFILE:
1288 err = recv_packfile(&imsg);
1290 log_warnx("%s: receive packfile: %s",
1291 repo_write.title, err->msg);
1294 err = verify_packfile();
1296 log_warnx("%s: verify packfile: %s",
1297 repo_write.title, err->msg);
1300 err = install_packfile(iev);
1302 log_warnx("%s: install packfile: %s",
1303 repo_write.title, err->msg);
1306 err = update_refs(iev);
1308 log_warnx("%s: update refs: %s",
1309 repo_write.title, err->msg);
1312 case GOTD_IMSG_DISCONNECT:
1313 err = recv_disconnect(&imsg);
1315 log_warnx("%s: disconnect: %s",
1316 repo_write.title, err->msg);
1320 log_debug("%s: unexpected imsg %d", repo_write.title,
1328 if (!shut && check_cancelled(NULL) == NULL) {
1330 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1331 client->id, err) == -1) {
1332 log_warnx("could not send error to parent: %s",
1335 gotd_imsg_event_add(iev);
1337 /* This pipe is dead. Remove its event handler */
1338 event_del(&iev->ev);
1339 event_loopexit(NULL);
1344 repo_write_main(const char *title, const char *repo_path,
1345 int *pack_fds, int *temp_fds)
1347 const struct got_error *err = NULL;
1348 struct gotd_imsgev iev;
1350 repo_write.title = title;
1351 repo_write.pid = getpid();
1352 repo_write.pack_fds = pack_fds;
1353 repo_write.temp_fds = temp_fds;
1355 STAILQ_INIT(&repo_write_client.ref_updates);
1357 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1360 if (!got_repo_is_bare(repo_write.repo)) {
1361 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1362 "bare git repository required");
1366 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1368 signal(SIGINT, catch_sigint);
1369 signal(SIGTERM, catch_sigterm);
1370 signal(SIGPIPE, SIG_IGN);
1371 signal(SIGHUP, SIG_IGN);
1373 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1374 iev.handler = repo_write_dispatch;
1375 iev.events = EV_READ;
1376 iev.handler_arg = NULL;
1377 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1378 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1379 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1380 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1387 log_warnx("%s: %s", title, err->msg);
1388 repo_write_shutdown();
1392 repo_write_shutdown(void)
1394 log_debug("%s: shutting down", repo_write.title);
1395 if (repo_write.repo)
1396 got_repo_close(repo_write.repo);
1397 got_repo_pack_fds_close(repo_write.pack_fds);
1398 got_repo_temp_fds_close(repo_write.temp_fds);