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>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_object.h"
40 #include "got_reference.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_delta_cache.h"
45 #include "got_lib_hash.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"
56 #include "repo_write.h"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static struct repo_write {
65 struct got_repository *repo;
69 struct gotd_imsgev session_iev;
72 struct gotd_ref_update {
73 STAILQ_ENTRY(gotd_ref_update) entry;
74 struct got_reference *ref;
77 struct got_object_id old_id;
78 struct got_object_id new_id;
80 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
82 static struct repo_write_client {
87 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
89 struct gotd_ref_updates ref_updates;
95 static volatile sig_atomic_t sigint_received;
96 static volatile sig_atomic_t sigterm_received;
99 catch_sigint(int signo)
105 catch_sigterm(int signo)
107 sigterm_received = 1;
110 static const struct got_error *
111 check_cancelled(void *arg)
113 if (sigint_received || sigterm_received)
114 return got_error(GOT_ERR_CANCELLED);
119 static const struct got_error *
120 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
121 struct imsgbuf *ibuf)
123 const struct got_error *err = NULL;
124 struct got_tag_object *tag;
126 char *peeled_refname = NULL;
127 struct got_object_id *id;
130 err = got_object_tag_open(&tag, repo_write.repo, obj);
134 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
135 err = got_error_from_errno("asprintf");
139 id = got_object_tag_get_object_id(tag);
140 namelen = strlen(peeled_refname);
142 len = sizeof(struct gotd_imsg_ref) + namelen;
143 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
144 err = got_error(GOT_ERR_NO_SPACE);
148 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
149 repo_write.pid, len);
151 err = got_error_from_errno("imsg_create REF");
155 /* Keep in sync with struct gotd_imsg_ref definition. */
156 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
157 err = got_error_from_errno("imsg_add REF");
160 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
161 err = got_error_from_errno("imsg_add REF");
164 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
165 err = got_error_from_errno("imsg_add REF");
170 imsg_close(ibuf, wbuf);
172 got_object_tag_close(tag);
176 static const struct got_error *
177 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
179 const struct got_error *err;
180 const char *refname = got_ref_get_name(ref);
182 struct got_object_id *id = NULL;
183 struct got_object *obj = NULL;
187 namelen = strlen(refname);
189 len = sizeof(struct gotd_imsg_ref) + namelen;
190 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
191 return got_error(GOT_ERR_NO_SPACE);
193 err = got_ref_resolve(&id, repo_write.repo, ref);
197 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
198 repo_write.pid, len);
200 err = got_error_from_errno("imsg_create REF");
204 /* Keep in sync with struct gotd_imsg_ref definition. */
205 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
206 return got_error_from_errno("imsg_add REF");
207 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
208 return got_error_from_errno("imsg_add REF");
209 if (imsg_add(wbuf, refname, namelen) == -1)
210 return got_error_from_errno("imsg_add REF");
213 imsg_close(ibuf, wbuf);
215 err = got_object_open(&obj, repo_write.repo, id);
218 if (obj->type == GOT_OBJ_TYPE_TAG)
219 err = send_peeled_tag_ref(ref, obj, ibuf);
222 got_object_close(obj);
227 static const struct got_error *
228 list_refs(struct imsg *imsg)
230 const struct got_error *err;
231 struct repo_write_client *client = &repo_write_client;
232 struct got_reflist_head refs;
233 struct got_reflist_entry *re;
234 struct gotd_imsg_list_refs_internal ireq;
236 struct gotd_imsg_reflist irefs;
238 int client_fd = imsg->fd;
243 return got_error(GOT_ERR_PRIVSEP_NO_FD);
245 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
246 if (datalen != sizeof(ireq))
247 return got_error(GOT_ERR_PRIVSEP_LEN);
248 memcpy(&ireq, imsg->data, sizeof(ireq));
250 if (ireq.client_id == 0)
251 return got_error(GOT_ERR_CLIENT_ID);
252 if (client->id != 0) {
253 return got_error_msg(GOT_ERR_CLIENT_ID,
254 "duplicate list-refs request");
256 client->id = ireq.client_id;
257 client->fd = client_fd;
258 client->nref_updates = 0;
259 client->nref_del = 0;
260 client->nref_new = 0;
262 imsg_init(&ibuf, client_fd);
264 err = got_ref_list(&refs, repo_write.repo, "",
265 got_ref_cmp_by_name, NULL);
269 memset(&irefs, 0, sizeof(irefs));
270 TAILQ_FOREACH(re, &refs, entry) {
271 struct got_object_id *id;
274 if (got_ref_is_symbolic(re->ref))
279 /* Account for a peeled tag refs. */
280 err = got_ref_resolve(&id, repo_write.repo, re->ref);
283 err = got_object_get_type(&obj_type, repo_write.repo, id);
287 if (obj_type == GOT_OBJ_TYPE_TAG)
291 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
292 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
293 err = got_error_from_errno("imsg_compose REFLIST");
297 TAILQ_FOREACH(re, &refs, entry) {
298 if (got_ref_is_symbolic(re->ref))
300 err = send_ref(re->ref, &ibuf);
305 err = gotd_imsg_flush(&ibuf);
307 got_ref_list_free(&refs);
312 static const struct got_error *
313 protect_ref_namespace(struct got_reference *ref, const char *namespace)
315 size_t len = strlen(namespace);
317 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
318 namespace[len -1] != '/') {
319 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
320 "reference namespace '%s'", namespace);
323 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
324 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
329 static const struct got_error *
330 recv_ref_update(struct imsg *imsg)
332 static const char zero_id[SHA1_DIGEST_LENGTH];
333 const struct got_error *err = NULL;
334 struct repo_write_client *client = &repo_write_client;
335 struct gotd_imsg_ref_update iref;
337 char *refname = NULL;
338 struct got_reference *ref = NULL;
339 struct got_object_id *id = NULL;
341 struct gotd_ref_update *ref_update = NULL;
343 log_debug("ref-update received");
345 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
346 if (datalen < sizeof(iref))
347 return got_error(GOT_ERR_PRIVSEP_LEN);
348 memcpy(&iref, imsg->data, sizeof(iref));
349 if (datalen != sizeof(iref) + iref.name_len)
350 return got_error(GOT_ERR_PRIVSEP_LEN);
352 imsg_init(&ibuf, client->fd);
354 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
356 return got_error_from_errno("strndup");
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;
377 if (got_ref_is_symbolic(ref)) {
378 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
379 "'%s' is a symbolic reference and cannot "
380 "be updated", got_ref_get_name(ref));
383 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
384 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
385 "%s: does not begin with 'refs/'",
386 got_ref_get_name(ref));
390 err = protect_ref_namespace(ref, "refs/got/");
393 err = protect_ref_namespace(ref, "refs/remotes/");
397 if (!ref_update->ref_is_new) {
399 * Ensure the client's idea of this update is still valid.
400 * At this point we can only return an error, to prevent
401 * the client from uploading a pack file which will likely
402 * have to be discarded.
404 err = got_ref_resolve(&id, repo_write.repo, ref);
408 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
409 err = got_error_fmt(GOT_ERR_REF_BUSY,
410 "%s has been modified by someone else "
411 "while transaction was in progress",
412 got_ref_get_name(ref));
417 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
420 ref_update->ref = ref;
421 if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
422 ref_update->delete_ref = 1;
425 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
426 client->nref_updates++;
438 static const struct got_error *
439 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
440 uint32_t nobj_loose, uint32_t nobj_resolved)
442 int p_indexed = 0, p_resolved = 0;
443 int nobj_delta = nobj_total - nobj_loose;
446 p_indexed = (nobj_indexed * 100) / nobj_total;
449 p_resolved = (nobj_resolved * 100) / nobj_delta;
451 if (p_resolved > 0) {
452 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
453 nobj_total, p_indexed, nobj_delta, p_resolved);
455 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
460 static const struct got_error *
461 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
463 const struct got_error *err = NULL;
464 uint8_t readahead[65536];
467 err = got_poll_read_full(infd, &have,
468 readahead, sizeof(readahead), minsize);
472 err = buf_append(&newlen, buf, readahead, have);
478 static const struct got_error *
479 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
480 off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
482 const struct got_error *err = NULL;
487 off_t obj_offset = *outsize;
490 /* We do not support size values which don't fit in 64 bit. */
492 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
493 "packfile offset %lld", (long long)obj_offset);
495 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
496 err = read_more_pack_stream(infd, buf,
502 sizebuf[i] = buf_getc(buf, *buf_pos);
503 *buf_pos += sizeof(sizebuf[i]);
506 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
507 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
508 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
510 size_t shift = 4 + 7 * (i - 1);
511 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
515 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
517 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
527 static const struct got_error *
528 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
529 struct got_hash *ctx)
531 const struct got_error *err = NULL;
532 size_t remain = buf_len(buf) - *buf_pos;
534 if (remain < SHA1_DIGEST_LENGTH) {
535 err = read_more_pack_stream(infd, buf,
536 SHA1_DIGEST_LENGTH - remain);
541 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
542 SHA1_DIGEST_LENGTH, ctx);
546 *buf_pos += SHA1_DIGEST_LENGTH;
550 static const struct got_error *
551 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
552 struct got_hash *ctx)
554 const struct got_error *err = NULL;
558 off_t obj_offset = *outsize;
561 /* We do not support offset values which don't fit in 64 bit. */
563 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
564 "packfile offset %lld", (long long)obj_offset);
566 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
567 err = read_more_pack_stream(infd, buf,
573 offbuf[i] = buf_getc(buf, *buf_pos);
574 *buf_pos += sizeof(offbuf[i]);
577 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
581 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
584 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
586 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
587 return got_error(GOT_ERR_PACK_OFFSET);
589 err = got_pack_hwrite(outfd, offbuf, i, ctx);
597 static const struct got_error *
598 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
599 struct got_hash *ctx)
601 const struct got_error *err = NULL;
605 size_t consumed_total = 0;
606 off_t zstream_offset = *outsize;
608 memset(&z, 0, sizeof(z));
612 zret = inflateInit(&z);
615 return got_error_from_errno("inflateInit");
616 if (zret == Z_MEM_ERROR) {
618 return got_error_from_errno("inflateInit");
620 return got_error_msg(GOT_ERR_DECOMPRESSION,
621 "inflateInit failed");
624 while (zret != Z_STREAM_END) {
625 size_t last_total_in, consumed;
628 * Decompress into the void. Object data will be parsed
629 * later, when the pack file is indexed. For now, we just
630 * want to locate the end of the compressed stream.
632 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
633 last_total_in = z.total_in;
634 z.next_in = buf_get(buf) + *buf_pos;
635 z.avail_in = buf_len(buf) - *buf_pos;
636 z.next_out = voidbuf;
637 z.avail_out = sizeof(voidbuf);
639 zret = inflate(&z, Z_SYNC_FLUSH);
640 if (zret != Z_OK && zret != Z_BUF_ERROR &&
641 zret != Z_STREAM_END) {
642 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
643 "packfile offset %lld",
644 (long long)zstream_offset);
647 consumed = z.total_in - last_total_in;
649 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
654 err = buf_discard(buf, *buf_pos + consumed);
659 consumed_total += consumed;
662 if (zret != Z_STREAM_END) {
663 err = read_more_pack_stream(infd, buf, 1);
670 *outsize += consumed_total;
676 static const struct got_error *
677 validate_object_type(int obj_type)
680 case GOT_OBJ_TYPE_BLOB:
681 case GOT_OBJ_TYPE_COMMIT:
682 case GOT_OBJ_TYPE_TREE:
683 case GOT_OBJ_TYPE_TAG:
684 case GOT_OBJ_TYPE_REF_DELTA:
685 case GOT_OBJ_TYPE_OFFSET_DELTA:
691 return got_error(GOT_ERR_OBJ_TYPE);
694 static const struct got_error *
695 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
698 const struct got_error *err;
699 struct repo_write_client *client = &repo_write_client;
700 struct got_packfile_hdr hdr;
704 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
705 char hex[SHA1_DIGEST_STRING_LENGTH];
707 size_t buf_pos = 0, remain;
713 /* if only deleting references there's nothing to read */
714 if (client->nref_updates == client->nref_del)
717 got_hash_init(&ctx, GOT_HASH_SHA1);
719 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
722 if (have != sizeof(hdr))
723 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
726 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
727 return got_error_msg(GOT_ERR_BAD_PACKFILE,
728 "bad packfile signature");
729 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
730 return got_error_msg(GOT_ERR_BAD_PACKFILE,
731 "bad packfile version");
733 *nobj = be32toh(hdr.nobjects);
736 * Clients which are creating new references only
737 * will send us an empty pack file.
739 if (client->nref_updates > 0 &&
740 client->nref_updates == client->nref_new)
743 return got_error_msg(GOT_ERR_BAD_PACKFILE,
744 "bad packfile with zero objects");
747 log_debug("expecting %d objects", *nobj);
749 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
753 err = buf_alloc(&buf, 65536);
757 while (nhave != *nobj) {
761 err = copy_object_type_and_size(&obj_type, &obj_size,
762 infd, outfd, outsize, buf, &buf_pos, &ctx);
766 err = validate_object_type(obj_type);
770 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
771 err = copy_ref_delta(infd, outfd, outsize,
772 buf, &buf_pos, &ctx);
775 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
776 err = copy_offset_delta(infd, outfd, outsize,
777 buf, &buf_pos, &ctx);
782 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
789 log_debug("received %u objects", *nobj);
791 got_hash_final(&ctx, expected_sha1);
793 remain = buf_len(buf) - buf_pos;
794 if (remain < SHA1_DIGEST_LENGTH) {
795 err = read_more_pack_stream(infd, buf,
796 SHA1_DIGEST_LENGTH - remain);
801 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
802 log_debug("expect SHA1: %s", hex);
803 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
804 log_debug("actual SHA1: %s", hex);
806 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
807 SHA1_DIGEST_LENGTH) != 0) {
808 err = got_error(GOT_ERR_PACKFILE_CSUM);
812 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
814 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
816 err = got_error_from_errno("write");
819 if (w != SHA1_DIGEST_LENGTH) {
820 err = got_error(GOT_ERR_IO);
824 *outsize += SHA1_DIGEST_LENGTH;
826 if (fsync(outfd) == -1) {
827 err = got_error_from_errno("fsync");
830 if (lseek(outfd, 0L, SEEK_SET) == -1) {
831 err = got_error_from_errno("lseek");
839 static const struct got_error *
840 report_pack_status(const struct got_error *unpack_err)
842 const struct got_error *err = NULL;
843 struct repo_write_client *client = &repo_write_client;
844 struct gotd_imsg_packfile_status istatus;
847 const char *unpack_ok = "unpack ok\n";
850 imsg_init(&ibuf, client->fd);
853 istatus.reason_len = strlen(unpack_err->msg);
855 istatus.reason_len = strlen(unpack_ok);
857 len = sizeof(istatus) + istatus.reason_len;
858 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
859 repo_write.pid, len);
861 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
865 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
866 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
870 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
871 istatus.reason_len) == -1) {
872 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
877 imsg_close(&ibuf, wbuf);
879 err = gotd_imsg_flush(&ibuf);
885 static const struct got_error *
886 recv_packfile(int *have_packfile, struct imsg *imsg)
888 const struct got_error *err = NULL, *unpack_err;
889 struct repo_write_client *client = &repo_write_client;
890 struct gotd_imsg_recv_packfile ireq;
891 FILE *tempfiles[3] = { NULL, NULL, NULL };
892 struct repo_tempfile {
895 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
899 struct got_ratelimit rl;
900 struct got_pack *pack = NULL;
901 off_t pack_filesize = 0;
904 log_debug("packfile request received");
907 got_ratelimit_init(&rl, 2, 0);
909 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
910 if (datalen != sizeof(ireq))
911 return got_error(GOT_ERR_PRIVSEP_LEN);
912 memcpy(&ireq, imsg->data, sizeof(ireq));
914 if (client->pack_pipe == -1 || client->packidx_fd == -1)
915 return got_error(GOT_ERR_PRIVSEP_NO_FD);
917 imsg_init(&ibuf, client->fd);
920 return got_error(GOT_ERR_PRIVSEP_NO_FD);
922 pack = &client->pack;
923 memset(pack, 0, sizeof(*pack));
925 err = got_delta_cache_alloc(&pack->delta_cache);
929 for (i = 0; i < nitems(repo_tempfiles); i++) {
930 struct repo_tempfile *t = &repo_tempfiles[i];
931 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
936 for (i = 0; i < nitems(tempfiles); i++) {
940 fd = dup(repo_tempfiles[i].fd);
942 err = got_error_from_errno("dup");
945 f = fdopen(fd, "w+");
947 err = got_error_from_errno("fdopen");
954 /* Send pack file pipe to gotsh(1). */
955 if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
956 repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
957 (*client)->pack_pipe[1] = -1;
958 err = got_error_from_errno("imsg_compose ACK");
962 (*client)->pack_pipe[1] = -1;
963 err = gotd_imsg_flush(&ibuf);
967 log_debug("receiving pack data");
968 unpack_err = recv_packdata(&pack_filesize, &nobj,
969 client->pack_sha1, client->pack_pipe, pack->fd);
970 if (ireq.report_status) {
971 err = report_pack_status(unpack_err);
973 /* Git clients hang up after sending the pack file. */
974 if (err->code == GOT_ERR_EOF)
983 log_debug("pack data received");
986 * Clients which are creating new references only will
987 * send us an empty pack file.
990 pack_filesize == sizeof(struct got_packfile_hdr) &&
991 client->nref_updates > 0 &&
992 client->nref_updates == client->nref_new)
996 * Clients which are deleting references only will send
1000 client->nref_del > 0 &&
1001 client->nref_updates == client->nref_del)
1004 pack->filesize = pack_filesize;
1007 log_debug("begin indexing pack (%lld bytes in size)",
1008 (long long)pack->filesize);
1009 err = got_pack_index(pack, client->packidx_fd,
1010 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1011 pack_index_progress, NULL, &rl);
1014 log_debug("done indexing pack");
1016 if (fsync(client->packidx_fd) == -1) {
1017 err = got_error_from_errno("fsync");
1020 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1021 err = got_error_from_errno("lseek");
1023 if (close(client->pack_pipe) == -1 && err == NULL)
1024 err = got_error_from_errno("close");
1025 client->pack_pipe = -1;
1026 for (i = 0; i < nitems(repo_tempfiles); i++) {
1027 struct repo_tempfile *t = &repo_tempfiles[i];
1029 got_repo_temp_fds_put(t->idx, repo_write.repo);
1031 for (i = 0; i < nitems(tempfiles); i++) {
1032 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1033 err = got_error_from_errno("fclose");
1036 got_pack_close(pack);
1041 static const struct got_error *
1042 verify_packfile(void)
1044 const struct got_error *err = NULL, *close_err;
1045 struct repo_write_client *client = &repo_write_client;
1046 struct gotd_ref_update *ref_update;
1047 struct got_packidx *packidx = NULL;
1049 char *id_str = NULL;
1052 if (STAILQ_EMPTY(&client->ref_updates)) {
1053 return got_error_msg(GOT_ERR_BAD_REQUEST,
1054 "cannot verify pack file without any ref-updates");
1057 if (client->pack.fd == -1) {
1058 return got_error_msg(GOT_ERR_BAD_REQUEST,
1059 "invalid pack file handle during pack verification");
1061 if (client->packidx_fd == -1) {
1062 return got_error_msg(GOT_ERR_BAD_REQUEST,
1063 "invalid pack index handle during pack verification");
1066 if (fstat(client->packidx_fd, &sb) == -1)
1067 return got_error_from_errno("pack index fstat");
1069 packidx = malloc(sizeof(*packidx));
1070 memset(packidx, 0, sizeof(*packidx));
1071 packidx->fd = client->packidx_fd;
1072 client->packidx_fd = -1;
1073 packidx->len = sb.st_size;
1075 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1079 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1080 if (ref_update->delete_ref)
1083 err = got_object_id_str(&id_str, &ref_update->new_id);
1087 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1089 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1090 "advertised object %s is missing from pack file",
1097 close_err = got_packidx_close(packidx);
1098 if (close_err && err == NULL)
1104 static const struct got_error *
1105 install_packfile(struct gotd_imsgev *iev)
1107 struct repo_write_client *client = &repo_write_client;
1108 struct gotd_imsg_packfile_install inst;
1111 memset(&inst, 0, sizeof(inst));
1112 inst.client_id = client->id;
1113 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1115 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1116 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1118 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1123 static const struct got_error *
1124 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1126 struct repo_write_client *client = &repo_write_client;
1127 struct gotd_imsg_ref_updates_start istart;
1130 memset(&istart, 0, sizeof(istart));
1131 istart.nref_updates = nref_updates;
1132 istart.client_id = client->id;
1134 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1135 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1137 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1143 static const struct got_error *
1144 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1146 struct repo_write_client *client = &repo_write_client;
1147 struct gotd_imsg_ref_update iref;
1148 const char *refname = got_ref_get_name(ref_update->ref);
1152 memset(&iref, 0, sizeof(iref));
1153 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1154 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1155 iref.ref_is_new = ref_update->ref_is_new;
1156 iref.delete_ref = ref_update->delete_ref;
1157 iref.client_id = client->id;
1158 iref.name_len = strlen(refname);
1160 len = sizeof(iref) + iref.name_len;
1161 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1162 repo_write.pid, len);
1164 return got_error_from_errno("imsg_create REF_UPDATE");
1166 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1167 return got_error_from_errno("imsg_add REF_UPDATE");
1168 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1169 return got_error_from_errno("imsg_add REF_UPDATE");
1172 imsg_close(&iev->ibuf, wbuf);
1174 gotd_imsg_event_add(iev);
1178 static const struct got_error *
1179 update_refs(struct gotd_imsgev *iev)
1181 const struct got_error *err = NULL;
1182 struct repo_write_client *client = &repo_write_client;
1183 struct gotd_ref_update *ref_update;
1185 err = send_ref_updates_start(client->nref_updates, iev);
1189 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1190 err = send_ref_update(ref_update, iev);
1198 static const struct got_error *
1199 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1201 struct repo_write_client *client = &repo_write_client;
1202 struct gotd_imsg_packfile_pipe ireq;
1205 log_debug("receving pack pipe descriptor");
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->pack_pipe != -1)
1216 return got_error(GOT_ERR_PRIVSEP_MSG);
1218 client->pack_pipe = imsg->fd;
1222 static const struct got_error *
1223 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1225 struct repo_write_client *client = &repo_write_client;
1226 struct gotd_imsg_packidx_file ireq;
1229 log_debug("receving pack index output file");
1232 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1234 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1235 if (datalen != sizeof(ireq))
1236 return got_error(GOT_ERR_PRIVSEP_LEN);
1237 memcpy(&ireq, imsg->data, sizeof(ireq));
1239 if (client->packidx_fd != -1)
1240 return got_error(GOT_ERR_PRIVSEP_MSG);
1242 client->packidx_fd = imsg->fd;
1247 repo_write_dispatch_session(int fd, short event, void *arg)
1249 const struct got_error *err = NULL;
1250 struct gotd_imsgev *iev = arg;
1251 struct imsgbuf *ibuf = &iev->ibuf;
1253 struct repo_write_client *client = &repo_write_client;
1255 int shut = 0, have_packfile = 0;
1257 if (event & EV_READ) {
1258 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1259 fatal("imsg_read error");
1260 if (n == 0) /* Connection closed. */
1264 if (event & EV_WRITE) {
1265 n = msgbuf_write(&ibuf->w);
1266 if (n == -1 && errno != EAGAIN)
1267 fatal("msgbuf_write");
1268 if (n == 0) /* Connection closed. */
1273 if ((n = imsg_get(ibuf, &imsg)) == -1)
1274 fatal("%s: imsg_get error", __func__);
1275 if (n == 0) /* No more messages. */
1278 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1280 err = got_error(GOT_ERR_PRIVSEP_MSG);
1284 switch (imsg.hdr.type) {
1285 case GOTD_IMSG_LIST_REFS_INTERNAL:
1286 err = list_refs(&imsg);
1288 log_warnx("%s: ls-refs: %s", repo_write.title,
1291 case GOTD_IMSG_REF_UPDATE:
1292 err = recv_ref_update(&imsg);
1294 log_warnx("%s: ref-update: %s",
1295 repo_write.title, err->msg);
1297 case GOTD_IMSG_PACKFILE_PIPE:
1298 err = receive_pack_pipe(&imsg, iev);
1300 log_warnx("%s: receiving pack pipe: %s",
1301 repo_write.title, err->msg);
1305 case GOTD_IMSG_PACKIDX_FILE:
1306 err = receive_pack_idx(&imsg, iev);
1308 log_warnx("%s: receiving pack index: %s",
1309 repo_write.title, err->msg);
1313 case GOTD_IMSG_RECV_PACKFILE:
1314 err = recv_packfile(&have_packfile, &imsg);
1316 log_warnx("%s: receive packfile: %s",
1317 repo_write.title, err->msg);
1320 if (have_packfile) {
1321 err = verify_packfile();
1323 log_warnx("%s: verify packfile: %s",
1324 repo_write.title, err->msg);
1327 err = install_packfile(iev);
1329 log_warnx("%s: install packfile: %s",
1330 repo_write.title, err->msg);
1334 err = update_refs(iev);
1336 log_warnx("%s: update refs: %s",
1337 repo_write.title, err->msg);
1341 log_debug("%s: unexpected imsg %d", repo_write.title,
1349 if (!shut && check_cancelled(NULL) == NULL) {
1351 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1352 client->id, err) == -1) {
1353 log_warnx("could not send error to parent: %s",
1356 gotd_imsg_event_add(iev);
1358 /* This pipe is dead. Remove its event handler */
1359 event_del(&iev->ev);
1360 event_loopexit(NULL);
1364 static const struct got_error *
1365 recv_connect(struct imsg *imsg)
1367 struct gotd_imsgev *iev = &repo_write.session_iev;
1370 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1372 return got_error(GOT_ERR_PRIVSEP_LEN);
1374 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1376 if (repo_write.session_fd != -1)
1377 return got_error(GOT_ERR_PRIVSEP_MSG);
1379 repo_write.session_fd = imsg->fd;
1381 imsg_init(&iev->ibuf, repo_write.session_fd);
1382 iev->handler = repo_write_dispatch_session;
1383 iev->events = EV_READ;
1384 iev->handler_arg = NULL;
1385 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1386 repo_write_dispatch_session, iev);
1387 gotd_imsg_event_add(iev);
1393 repo_write_dispatch(int fd, short event, void *arg)
1395 const struct got_error *err = NULL;
1396 struct gotd_imsgev *iev = arg;
1397 struct imsgbuf *ibuf = &iev->ibuf;
1401 struct repo_write_client *client = &repo_write_client;
1403 if (event & EV_READ) {
1404 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1405 fatal("imsg_read error");
1406 if (n == 0) /* Connection closed. */
1410 if (event & EV_WRITE) {
1411 n = msgbuf_write(&ibuf->w);
1412 if (n == -1 && errno != EAGAIN)
1413 fatal("msgbuf_write");
1414 if (n == 0) /* Connection closed. */
1418 while (err == NULL && check_cancelled(NULL) == NULL) {
1419 if ((n = imsg_get(ibuf, &imsg)) == -1)
1420 fatal("%s: imsg_get", __func__);
1421 if (n == 0) /* No more messages. */
1424 switch (imsg.hdr.type) {
1425 case GOTD_IMSG_CONNECT_REPO_CHILD:
1426 err = recv_connect(&imsg);
1429 log_debug("%s: unexpected imsg %d", repo_write.title,
1437 if (!shut && check_cancelled(NULL) == NULL) {
1439 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1440 client->id, err) == -1) {
1441 log_warnx("could not send error to parent: %s",
1444 gotd_imsg_event_add(iev);
1446 /* This pipe is dead. Remove its event handler */
1447 event_del(&iev->ev);
1448 event_loopexit(NULL);
1453 repo_write_main(const char *title, const char *repo_path,
1454 int *pack_fds, int *temp_fds)
1456 const struct got_error *err = NULL;
1457 struct repo_write_client *client = &repo_write_client;
1458 struct gotd_imsgev iev;
1461 client->pack_pipe = -1;
1462 client->packidx_fd = -1;
1463 client->pack.fd = -1;
1465 repo_write.title = title;
1466 repo_write.pid = getpid();
1467 repo_write.pack_fds = pack_fds;
1468 repo_write.temp_fds = temp_fds;
1469 repo_write.session_fd = -1;
1470 repo_write.session_iev.ibuf.fd = -1;
1472 STAILQ_INIT(&repo_write_client.ref_updates);
1474 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1477 if (!got_repo_is_bare(repo_write.repo)) {
1478 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1479 "bare git repository required");
1483 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1485 signal(SIGINT, catch_sigint);
1486 signal(SIGTERM, catch_sigterm);
1487 signal(SIGPIPE, SIG_IGN);
1488 signal(SIGHUP, SIG_IGN);
1490 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1491 iev.handler = repo_write_dispatch;
1492 iev.events = EV_READ;
1493 iev.handler_arg = NULL;
1494 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1495 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1496 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1497 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1504 log_warnx("%s: %s", title, err->msg);
1505 repo_write_shutdown();
1509 repo_write_shutdown(void)
1511 struct repo_write_client *client = &repo_write_client;
1512 struct gotd_ref_update *ref_update;
1514 log_debug("%s: shutting down", repo_write.title);
1516 while (!STAILQ_EMPTY(&client->ref_updates)) {
1517 ref_update = STAILQ_FIRST(&client->ref_updates);
1518 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1519 got_ref_close(ref_update->ref);
1523 got_pack_close(&client->pack);
1524 if (client->fd != -1)
1526 if (client->pack_pipe != -1)
1527 close(client->pack_pipe);
1528 if (client->packidx_fd != -1)
1529 close(client->packidx_fd);
1531 if (repo_write.repo)
1532 got_repo_close(repo_write.repo);
1533 got_repo_pack_fds_close(repo_write.pack_fds);
1534 got_repo_temp_fds_close(repo_write.temp_fds);
1535 if (repo_write.session_fd != -1)
1536 close(repo_write.session_fd);