2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
20 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_object.h"
42 #include "got_reference.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_delta_cache.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_ratelimit.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_pack_index.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_poll.h"
55 #include "got_lib_hash.h" /* XXX temp include for debugging */
59 #include "repo_write.h"
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 static struct repo_write {
68 struct got_repository *repo;
72 struct gotd_imsgev session_iev;
75 struct gotd_ref_update {
76 STAILQ_ENTRY(gotd_ref_update) entry;
77 struct got_reference *ref;
80 struct got_object_id old_id;
81 struct got_object_id new_id;
83 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
85 static struct repo_write_client {
90 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
92 struct gotd_ref_updates ref_updates;
98 static volatile sig_atomic_t sigint_received;
99 static volatile sig_atomic_t sigterm_received;
102 catch_sigint(int signo)
108 catch_sigterm(int signo)
110 sigterm_received = 1;
113 static const struct got_error *
114 check_cancelled(void *arg)
116 if (sigint_received || sigterm_received)
117 return got_error(GOT_ERR_CANCELLED);
122 static const struct got_error *
123 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
124 struct imsgbuf *ibuf)
126 const struct got_error *err = NULL;
127 struct got_tag_object *tag;
129 char *peeled_refname = NULL;
130 struct got_object_id *id;
133 err = got_object_tag_open(&tag, repo_write.repo, obj);
137 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
138 err = got_error_from_errno("asprintf");
142 id = got_object_tag_get_object_id(tag);
143 namelen = strlen(peeled_refname);
145 len = sizeof(struct gotd_imsg_ref) + namelen;
146 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
147 err = got_error(GOT_ERR_NO_SPACE);
151 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
152 repo_write.pid, len);
154 err = got_error_from_errno("imsg_create REF");
158 /* Keep in sync with struct gotd_imsg_ref definition. */
159 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
160 err = got_error_from_errno("imsg_add REF");
163 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
164 err = got_error_from_errno("imsg_add REF");
167 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
168 err = got_error_from_errno("imsg_add REF");
173 imsg_close(ibuf, wbuf);
175 got_object_tag_close(tag);
179 static const struct got_error *
180 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
182 const struct got_error *err;
183 const char *refname = got_ref_get_name(ref);
185 struct got_object_id *id = NULL;
186 struct got_object *obj = NULL;
190 namelen = strlen(refname);
192 len = sizeof(struct gotd_imsg_ref) + namelen;
193 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
194 return got_error(GOT_ERR_NO_SPACE);
196 err = got_ref_resolve(&id, repo_write.repo, ref);
200 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
201 repo_write.pid, len);
203 err = got_error_from_errno("imsg_create REF");
207 /* Keep in sync with struct gotd_imsg_ref definition. */
208 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
209 return got_error_from_errno("imsg_add REF");
210 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
211 return got_error_from_errno("imsg_add REF");
212 if (imsg_add(wbuf, refname, namelen) == -1)
213 return got_error_from_errno("imsg_add REF");
216 imsg_close(ibuf, wbuf);
218 err = got_object_open(&obj, repo_write.repo, id);
221 if (obj->type == GOT_OBJ_TYPE_TAG)
222 err = send_peeled_tag_ref(ref, obj, ibuf);
225 got_object_close(obj);
230 static const struct got_error *
231 list_refs(struct imsg *imsg)
233 const struct got_error *err;
234 struct repo_write_client *client = &repo_write_client;
235 struct got_reflist_head refs;
236 struct got_reflist_entry *re;
237 struct gotd_imsg_list_refs_internal ireq;
239 struct gotd_imsg_reflist irefs;
241 int client_fd = imsg->fd;
246 return got_error(GOT_ERR_PRIVSEP_NO_FD);
248 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
249 if (datalen != sizeof(ireq))
250 return got_error(GOT_ERR_PRIVSEP_LEN);
251 memcpy(&ireq, imsg->data, sizeof(ireq));
253 if (ireq.client_id == 0)
254 return got_error(GOT_ERR_CLIENT_ID);
255 if (client->id != 0) {
256 return got_error_msg(GOT_ERR_CLIENT_ID,
257 "duplicate list-refs request");
259 client->id = ireq.client_id;
260 client->fd = client_fd;
261 client->pack_pipe = -1;
262 client->packidx_fd = -1;
263 client->nref_updates = 0;
264 client->nref_del = 0;
265 client->nref_new = 0;
267 imsg_init(&ibuf, client_fd);
269 err = got_ref_list(&refs, repo_write.repo, "",
270 got_ref_cmp_by_name, NULL);
274 memset(&irefs, 0, sizeof(irefs));
275 TAILQ_FOREACH(re, &refs, entry) {
276 struct got_object_id *id;
279 if (got_ref_is_symbolic(re->ref))
284 /* Account for a peeled tag refs. */
285 err = got_ref_resolve(&id, repo_write.repo, re->ref);
288 err = got_object_get_type(&obj_type, repo_write.repo, id);
292 if (obj_type == GOT_OBJ_TYPE_TAG)
296 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
297 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
298 err = got_error_from_errno("imsg_compose REFLIST");
302 TAILQ_FOREACH(re, &refs, entry) {
303 if (got_ref_is_symbolic(re->ref))
305 err = send_ref(re->ref, &ibuf);
310 err = gotd_imsg_flush(&ibuf);
312 got_ref_list_free(&refs);
317 static const struct got_error *
318 protect_ref_namespace(struct got_reference *ref, const char *namespace)
320 size_t len = strlen(namespace);
322 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
323 namespace[len -1] != '/') {
324 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
325 "reference namespace '%s'", namespace);
328 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
329 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
334 static const struct got_error *
335 recv_ref_update(struct imsg *imsg)
337 static const char zero_id[SHA1_DIGEST_LENGTH];
338 const struct got_error *err = NULL;
339 struct repo_write_client *client = &repo_write_client;
340 struct gotd_imsg_ref_update iref;
342 char *refname = NULL;
343 struct got_reference *ref = NULL;
344 struct got_object_id *id = NULL;
346 struct gotd_ref_update *ref_update = NULL;
348 log_debug("ref-update received");
350 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
351 if (datalen < sizeof(iref))
352 return got_error(GOT_ERR_PRIVSEP_LEN);
353 memcpy(&iref, imsg->data, sizeof(iref));
354 if (datalen != sizeof(iref) + iref.name_len)
355 return got_error(GOT_ERR_PRIVSEP_LEN);
357 imsg_init(&ibuf, client->fd);
359 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
361 return got_error_from_errno("strndup");
363 ref_update = calloc(1, sizeof(*ref_update));
364 if (ref_update == NULL) {
365 err = got_error_from_errno("malloc");
369 memcpy(ref_update->old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
370 memcpy(ref_update->new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
372 err = got_ref_open(&ref, repo_write.repo, refname, 0);
374 if (err->code != GOT_ERR_NOT_REF)
376 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
379 ref_update->ref_is_new = 1;
382 if (got_ref_is_symbolic(ref)) {
383 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
384 "'%s' is a symbolic reference and cannot "
385 "be updated", got_ref_get_name(ref));
388 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
389 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
390 "%s: does not begin with 'refs/'",
391 got_ref_get_name(ref));
395 err = protect_ref_namespace(ref, "refs/got/");
398 err = protect_ref_namespace(ref, "refs/remotes/");
402 if (!ref_update->ref_is_new) {
404 * Ensure the client's idea of this update is still valid.
405 * At this point we can only return an error, to prevent
406 * the client from uploading a pack file which will likely
407 * have to be discarded.
409 err = got_ref_resolve(&id, repo_write.repo, ref);
413 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
414 err = got_error_fmt(GOT_ERR_REF_BUSY,
415 "%s has been modified by someone else "
416 "while transaction was in progress",
417 got_ref_get_name(ref));
422 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
425 ref_update->ref = ref;
426 if (memcmp(ref_update->new_id.hash, zero_id, sizeof(zero_id)) == 0) {
427 ref_update->delete_ref = 1;
430 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
431 client->nref_updates++;
443 static const struct got_error *
444 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
445 uint32_t nobj_loose, uint32_t nobj_resolved)
447 int p_indexed = 0, p_resolved = 0;
448 int nobj_delta = nobj_total - nobj_loose;
451 p_indexed = (nobj_indexed * 100) / nobj_total;
454 p_resolved = (nobj_resolved * 100) / nobj_delta;
456 if (p_resolved > 0) {
457 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
458 nobj_total, p_indexed, nobj_delta, p_resolved);
460 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
465 static const struct got_error *
466 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
468 const struct got_error *err = NULL;
469 uint8_t readahead[65536];
472 err = got_poll_read_full(infd, &have,
473 readahead, sizeof(readahead), minsize);
477 err = buf_append(&newlen, buf, readahead, have);
483 static const struct got_error *
484 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
485 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
487 const struct got_error *err = NULL;
492 off_t obj_offset = *outsize;
495 /* We do not support size values which don't fit in 64 bit. */
497 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
498 "packfile offset %lld", (long long)obj_offset);
500 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
501 err = read_more_pack_stream(infd, buf,
507 sizebuf[i] = buf_getc(buf, *buf_pos);
508 *buf_pos += sizeof(sizebuf[i]);
511 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
512 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
513 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
515 size_t shift = 4 + 7 * (i - 1);
516 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
520 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
522 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
532 static const struct got_error *
533 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
536 const struct got_error *err = NULL;
537 size_t remain = buf_len(buf) - *buf_pos;
539 if (remain < SHA1_DIGEST_LENGTH) {
540 err = read_more_pack_stream(infd, buf,
541 SHA1_DIGEST_LENGTH - remain);
546 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
547 SHA1_DIGEST_LENGTH, ctx);
551 *buf_pos += SHA1_DIGEST_LENGTH;
555 static const struct got_error *
556 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
559 const struct got_error *err = NULL;
563 off_t obj_offset = *outsize;
566 /* We do not support offset values which don't fit in 64 bit. */
568 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
569 "packfile offset %lld", (long long)obj_offset);
571 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
572 err = read_more_pack_stream(infd, buf,
578 offbuf[i] = buf_getc(buf, *buf_pos);
579 *buf_pos += sizeof(offbuf[i]);
582 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
586 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
589 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
591 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
592 return got_error(GOT_ERR_PACK_OFFSET);
594 err = got_pack_hwrite(outfd, offbuf, i, ctx);
602 static const struct got_error *
603 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
606 const struct got_error *err = NULL;
610 size_t consumed_total = 0;
611 off_t zstream_offset = *outsize;
613 memset(&z, 0, sizeof(z));
617 zret = inflateInit(&z);
620 return got_error_from_errno("inflateInit");
621 if (zret == Z_MEM_ERROR) {
623 return got_error_from_errno("inflateInit");
625 return got_error_msg(GOT_ERR_DECOMPRESSION,
626 "inflateInit failed");
629 while (zret != Z_STREAM_END) {
630 size_t last_total_in, consumed;
633 * Decompress into the void. Object data will be parsed
634 * later, when the pack file is indexed. For now, we just
635 * want to locate the end of the compressed stream.
637 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
638 last_total_in = z.total_in;
639 z.next_in = buf_get(buf) + *buf_pos;
640 z.avail_in = buf_len(buf) - *buf_pos;
641 z.next_out = voidbuf;
642 z.avail_out = sizeof(voidbuf);
644 zret = inflate(&z, Z_SYNC_FLUSH);
645 if (zret != Z_OK && zret != Z_BUF_ERROR &&
646 zret != Z_STREAM_END) {
647 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
648 "packfile offset %lld",
649 (long long)zstream_offset);
652 consumed = z.total_in - last_total_in;
654 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
659 err = buf_discard(buf, *buf_pos + consumed);
664 consumed_total += consumed;
667 if (zret != Z_STREAM_END) {
668 err = read_more_pack_stream(infd, buf, 1);
675 *outsize += consumed_total;
681 static const struct got_error *
682 validate_object_type(int obj_type)
685 case GOT_OBJ_TYPE_BLOB:
686 case GOT_OBJ_TYPE_COMMIT:
687 case GOT_OBJ_TYPE_TREE:
688 case GOT_OBJ_TYPE_TAG:
689 case GOT_OBJ_TYPE_REF_DELTA:
690 case GOT_OBJ_TYPE_OFFSET_DELTA:
696 return got_error(GOT_ERR_OBJ_TYPE);
699 static const struct got_error *
700 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *hash,
703 const struct got_error *err;
704 struct repo_write_client *client = &repo_write_client;
705 struct got_packfile_hdr hdr;
709 uint8_t expected_hash[SHA1_DIGEST_LENGTH];
710 char hex[SHA1_DIGEST_STRING_LENGTH];
712 size_t buf_pos = 0, remain;
718 /* if only deleting references there's nothing to read */
719 if (client->nref_updates == client->nref_del)
724 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
727 if (have != sizeof(hdr))
728 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
731 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
732 return got_error_msg(GOT_ERR_BAD_PACKFILE,
733 "bad packfile signature");
734 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
735 return got_error_msg(GOT_ERR_BAD_PACKFILE,
736 "bad packfile version");
738 *nobj = be32toh(hdr.nobjects);
741 * Clients which are creating new references only
742 * will send us an empty pack file.
744 if (client->nref_updates > 0 &&
745 client->nref_updates == client->nref_new)
748 return got_error_msg(GOT_ERR_BAD_PACKFILE,
749 "bad packfile with zero objects");
752 log_debug("expecting %d objects", *nobj);
754 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
758 err = buf_alloc(&buf, 65536);
762 while (nhave != *nobj) {
766 err = copy_object_type_and_size(&obj_type, &obj_size,
767 infd, outfd, outsize, buf, &buf_pos, &ctx);
771 err = validate_object_type(obj_type);
775 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
776 err = copy_ref_delta(infd, outfd, outsize,
777 buf, &buf_pos, &ctx);
780 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
781 err = copy_offset_delta(infd, outfd, outsize,
782 buf, &buf_pos, &ctx);
787 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
794 log_debug("received %u objects", *nobj);
796 SHA1Final(expected_hash, &ctx);
798 remain = buf_len(buf) - buf_pos;
799 if (remain < SHA1_DIGEST_LENGTH) {
800 err = read_more_pack_stream(infd, buf,
801 SHA1_DIGEST_LENGTH - remain);
806 got_sha1_digest_to_str(expected_hash, hex, sizeof(hex));
807 log_debug("expect SHA1: %s", hex);
808 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
809 log_debug("actual SHA1: %s", hex);
811 if (memcmp(buf_get(buf) + buf_pos, expected_hash,
812 SHA1_DIGEST_LENGTH) != 0) {
813 err = got_error(GOT_ERR_PACKFILE_CSUM);
817 memcpy(hash, expected_hash, SHA1_DIGEST_LENGTH);
819 w = write(outfd, expected_hash, SHA1_DIGEST_LENGTH);
821 err = got_error_from_errno("write");
824 if (w != SHA1_DIGEST_LENGTH) {
825 err = got_error(GOT_ERR_IO);
829 *outsize += SHA1_DIGEST_LENGTH;
831 if (fsync(outfd) == -1) {
832 err = got_error_from_errno("fsync");
835 if (lseek(outfd, 0L, SEEK_SET) == -1) {
836 err = got_error_from_errno("lseek");
844 static const struct got_error *
845 report_pack_status(const struct got_error *unpack_err)
847 const struct got_error *err = NULL;
848 struct repo_write_client *client = &repo_write_client;
849 struct gotd_imsg_packfile_status istatus;
852 const char *unpack_ok = "unpack ok\n";
855 imsg_init(&ibuf, client->fd);
858 istatus.reason_len = strlen(unpack_err->msg);
860 istatus.reason_len = strlen(unpack_ok);
862 len = sizeof(istatus) + istatus.reason_len;
863 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
864 repo_write.pid, len);
866 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
870 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
871 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
875 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
876 istatus.reason_len) == -1) {
877 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
882 imsg_close(&ibuf, wbuf);
884 err = gotd_imsg_flush(&ibuf);
890 static const struct got_error *
891 recv_packfile(int *have_packfile, struct imsg *imsg)
893 const struct got_error *err = NULL, *unpack_err;
894 struct repo_write_client *client = &repo_write_client;
895 struct gotd_imsg_recv_packfile ireq;
896 FILE *tempfiles[3] = { NULL, NULL, NULL };
897 struct repo_tempfile {
900 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
904 struct got_ratelimit rl;
905 struct got_pack *pack = NULL;
906 off_t pack_filesize = 0;
909 log_debug("packfile request received");
912 got_ratelimit_init(&rl, 2, 0);
914 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
915 if (datalen != sizeof(ireq))
916 return got_error(GOT_ERR_PRIVSEP_LEN);
917 memcpy(&ireq, imsg->data, sizeof(ireq));
919 if (client->pack_pipe == -1 || client->packidx_fd == -1)
920 return got_error(GOT_ERR_PRIVSEP_NO_FD);
922 imsg_init(&ibuf, client->fd);
925 return got_error(GOT_ERR_PRIVSEP_NO_FD);
927 pack = &client->pack;
928 memset(pack, 0, sizeof(*pack));
930 err = got_delta_cache_alloc(&pack->delta_cache);
934 for (i = 0; i < nitems(repo_tempfiles); i++) {
935 struct repo_tempfile *t = &repo_tempfiles[i];
936 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
941 for (i = 0; i < nitems(tempfiles); i++) {
945 fd = dup(repo_tempfiles[i].fd);
947 err = got_error_from_errno("dup");
950 f = fdopen(fd, "w+");
952 err = got_error_from_errno("fdopen");
959 err = gotd_imsg_flush(&ibuf);
963 log_debug("receiving pack data");
964 unpack_err = recv_packdata(&pack_filesize, &nobj,
965 client->pack_hash, client->pack_pipe, pack->fd);
966 if (ireq.report_status) {
967 err = report_pack_status(unpack_err);
969 /* Git clients hang up after sending the pack file. */
970 if (err->code == GOT_ERR_EOF)
979 log_debug("pack data received");
982 * Clients which are creating new references only will
983 * send us an empty pack file.
986 pack_filesize == sizeof(struct got_packfile_hdr) &&
987 client->nref_updates > 0 &&
988 client->nref_updates == client->nref_new)
992 * Clients which are deleting references only will send
996 client->nref_del > 0 &&
997 client->nref_updates == client->nref_del)
1000 pack->filesize = pack_filesize;
1003 log_debug("begin indexing pack (%lld bytes in size)",
1004 (long long)pack->filesize);
1005 err = got_pack_index(pack, client->packidx_fd,
1006 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_hash,
1007 pack_index_progress, NULL, &rl);
1010 log_debug("done indexing pack");
1012 if (fsync(client->packidx_fd) == -1) {
1013 err = got_error_from_errno("fsync");
1016 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1017 err = got_error_from_errno("lseek");
1019 if (close(client->pack_pipe) == -1 && err == NULL)
1020 err = got_error_from_errno("close");
1021 client->pack_pipe = -1;
1022 for (i = 0; i < nitems(repo_tempfiles); i++) {
1023 struct repo_tempfile *t = &repo_tempfiles[i];
1025 got_repo_temp_fds_put(t->idx, repo_write.repo);
1027 for (i = 0; i < nitems(tempfiles); i++) {
1028 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1029 err = got_error_from_errno("fclose");
1032 got_pack_close(pack);
1037 static const struct got_error *
1038 verify_packfile(void)
1040 const struct got_error *err = NULL, *close_err;
1041 struct repo_write_client *client = &repo_write_client;
1042 struct gotd_ref_update *ref_update;
1043 struct got_packidx *packidx = NULL;
1045 char *id_str = NULL;
1048 if (STAILQ_EMPTY(&client->ref_updates)) {
1049 return got_error_msg(GOT_ERR_BAD_REQUEST,
1050 "cannot verify pack file without any ref-updates");
1053 if (client->pack.fd == -1) {
1054 return got_error_msg(GOT_ERR_BAD_REQUEST,
1055 "invalid pack file handle during pack verification");
1057 if (client->packidx_fd == -1) {
1058 return got_error_msg(GOT_ERR_BAD_REQUEST,
1059 "invalid pack index handle during pack verification");
1062 if (fstat(client->packidx_fd, &sb) == -1)
1063 return got_error_from_errno("pack index fstat");
1065 packidx = malloc(sizeof(*packidx));
1066 memset(packidx, 0, sizeof(*packidx));
1067 packidx->fd = client->packidx_fd;
1068 client->packidx_fd = -1;
1069 packidx->len = sb.st_size;
1071 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1075 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1076 if (ref_update->delete_ref)
1079 err = got_object_id_str(&id_str, &ref_update->new_id);
1083 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1085 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1086 "advertised object %s is missing from pack file",
1093 close_err = got_packidx_close(packidx);
1094 if (close_err && err == NULL)
1100 static const struct got_error *
1101 install_packfile(struct gotd_imsgev *iev)
1103 struct repo_write_client *client = &repo_write_client;
1104 struct gotd_imsg_packfile_install inst;
1107 memset(&inst, 0, sizeof(inst));
1108 inst.client_id = client->id;
1109 memcpy(inst.pack_hash, client->pack_hash, SHA1_DIGEST_LENGTH);
1111 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1112 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1114 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1119 static const struct got_error *
1120 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1122 struct repo_write_client *client = &repo_write_client;
1123 struct gotd_imsg_ref_updates_start istart;
1126 memset(&istart, 0, sizeof(istart));
1127 istart.nref_updates = nref_updates;
1128 istart.client_id = client->id;
1130 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1131 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1133 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1139 static const struct got_error *
1140 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1142 struct repo_write_client *client = &repo_write_client;
1143 struct gotd_imsg_ref_update iref;
1144 const char *refname = got_ref_get_name(ref_update->ref);
1148 memset(&iref, 0, sizeof(iref));
1149 memcpy(iref.old_id, ref_update->old_id.hash, SHA1_DIGEST_LENGTH);
1150 memcpy(iref.new_id, ref_update->new_id.hash, SHA1_DIGEST_LENGTH);
1151 iref.ref_is_new = ref_update->ref_is_new;
1152 iref.delete_ref = ref_update->delete_ref;
1153 iref.client_id = client->id;
1154 iref.name_len = strlen(refname);
1156 len = sizeof(iref) + iref.name_len;
1157 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1158 repo_write.pid, len);
1160 return got_error_from_errno("imsg_create REF_UPDATE");
1162 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1163 return got_error_from_errno("imsg_add REF_UPDATE");
1164 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1165 return got_error_from_errno("imsg_add REF_UPDATE");
1168 imsg_close(&iev->ibuf, wbuf);
1170 gotd_imsg_event_add(iev);
1174 static const struct got_error *
1175 update_refs(struct gotd_imsgev *iev)
1177 const struct got_error *err = NULL;
1178 struct repo_write_client *client = &repo_write_client;
1179 struct gotd_ref_update *ref_update;
1181 err = send_ref_updates_start(client->nref_updates, iev);
1185 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1186 err = send_ref_update(ref_update, iev);
1194 static const struct got_error *
1195 recv_disconnect(struct imsg *imsg)
1197 const struct got_error *err = NULL;
1198 struct gotd_imsg_disconnect idisconnect;
1200 int pack_pipe = -1, idxfd = -1;
1201 struct repo_write_client *client = &repo_write_client;
1203 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1204 if (datalen != sizeof(idisconnect))
1205 return got_error(GOT_ERR_PRIVSEP_LEN);
1206 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1208 log_debug("client disconnecting");
1210 while (!STAILQ_EMPTY(&client->ref_updates)) {
1211 struct gotd_ref_update *ref_update;
1212 ref_update = STAILQ_FIRST(&client->ref_updates);
1213 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1214 got_ref_close(ref_update->ref);
1217 err = got_pack_close(&client->pack);
1218 if (client->fd != -1 && close(client->fd) == -1)
1219 err = got_error_from_errno("close");
1220 pack_pipe = client->pack_pipe;
1221 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1222 err = got_error_from_errno("close");
1223 idxfd = client->packidx_fd;
1224 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1225 err = got_error_from_errno("close");
1229 static const struct got_error *
1230 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1232 struct repo_write_client *client = &repo_write_client;
1233 struct gotd_imsg_packfile_pipe ireq;
1236 log_debug("receving pack pipe descriptor");
1239 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1241 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1242 if (datalen != sizeof(ireq))
1243 return got_error(GOT_ERR_PRIVSEP_LEN);
1244 memcpy(&ireq, imsg->data, sizeof(ireq));
1246 if (client->pack_pipe != -1)
1247 return got_error(GOT_ERR_PRIVSEP_MSG);
1249 client->pack_pipe = imsg->fd;
1253 static const struct got_error *
1254 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1256 struct repo_write_client *client = &repo_write_client;
1257 struct gotd_imsg_packidx_file ireq;
1260 log_debug("receving pack index output file");
1263 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1265 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1266 if (datalen != sizeof(ireq))
1267 return got_error(GOT_ERR_PRIVSEP_LEN);
1268 memcpy(&ireq, imsg->data, sizeof(ireq));
1270 if (client->packidx_fd != -1)
1271 return got_error(GOT_ERR_PRIVSEP_MSG);
1273 client->packidx_fd = imsg->fd;
1278 repo_write_dispatch_session(int fd, short event, void *arg)
1280 const struct got_error *err = NULL;
1281 struct gotd_imsgev *iev = arg;
1282 struct imsgbuf *ibuf = &iev->ibuf;
1284 struct repo_write_client *client = &repo_write_client;
1286 int shut = 0, have_packfile = 0;
1288 if (event & EV_READ) {
1289 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1290 fatal("imsg_read error");
1291 if (n == 0) /* Connection closed. */
1295 if (event & EV_WRITE) {
1296 n = msgbuf_write(&ibuf->w);
1297 if (n == -1 && errno != EAGAIN)
1298 fatal("msgbuf_write");
1299 if (n == 0) /* Connection closed. */
1304 if ((n = imsg_get(ibuf, &imsg)) == -1)
1305 fatal("%s: imsg_get error", __func__);
1306 if (n == 0) /* No more messages. */
1309 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1311 err = got_error(GOT_ERR_PRIVSEP_MSG);
1315 switch (imsg.hdr.type) {
1316 case GOTD_IMSG_LIST_REFS_INTERNAL:
1317 err = list_refs(&imsg);
1319 log_warnx("%s: ls-refs: %s", repo_write.title,
1322 case GOTD_IMSG_REF_UPDATE:
1323 err = recv_ref_update(&imsg);
1325 log_warnx("%s: ref-update: %s",
1326 repo_write.title, err->msg);
1328 case GOTD_IMSG_PACKFILE_PIPE:
1329 err = receive_pack_pipe(&imsg, iev);
1331 log_warnx("%s: receiving pack pipe: %s",
1332 repo_write.title, err->msg);
1336 case GOTD_IMSG_PACKIDX_FILE:
1337 err = receive_pack_idx(&imsg, iev);
1339 log_warnx("%s: receiving pack index: %s",
1340 repo_write.title, err->msg);
1344 case GOTD_IMSG_RECV_PACKFILE:
1345 err = recv_packfile(&have_packfile, &imsg);
1347 log_warnx("%s: receive packfile: %s",
1348 repo_write.title, err->msg);
1351 if (have_packfile) {
1352 err = verify_packfile();
1354 log_warnx("%s: verify packfile: %s",
1355 repo_write.title, err->msg);
1358 err = install_packfile(iev);
1360 log_warnx("%s: install packfile: %s",
1361 repo_write.title, err->msg);
1365 err = update_refs(iev);
1367 log_warnx("%s: update refs: %s",
1368 repo_write.title, err->msg);
1371 case GOTD_IMSG_DISCONNECT:
1372 err = recv_disconnect(&imsg);
1374 log_warnx("%s: disconnect: %s",
1375 repo_write.title, err->msg);
1379 log_debug("%s: unexpected imsg %d", repo_write.title,
1387 if (!shut && check_cancelled(NULL) == NULL) {
1389 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1390 client->id, err) == -1) {
1391 log_warnx("could not send error to parent: %s",
1394 gotd_imsg_event_add(iev);
1396 /* This pipe is dead. Remove its event handler */
1397 event_del(&iev->ev);
1398 event_loopexit(NULL);
1402 static const struct got_error *
1403 recv_connect(struct imsg *imsg)
1405 struct gotd_imsgev *iev = &repo_write.session_iev;
1408 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1410 return got_error(GOT_ERR_PRIVSEP_LEN);
1412 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1414 if (repo_write.session_fd != -1)
1415 return got_error(GOT_ERR_PRIVSEP_MSG);
1417 repo_write.session_fd = imsg->fd;
1419 imsg_init(&iev->ibuf, repo_write.session_fd);
1420 iev->handler = repo_write_dispatch_session;
1421 iev->events = EV_READ;
1422 iev->handler_arg = NULL;
1423 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1424 repo_write_dispatch_session, iev);
1425 gotd_imsg_event_add(iev);
1431 repo_write_dispatch(int fd, short event, void *arg)
1433 const struct got_error *err = NULL;
1434 struct gotd_imsgev *iev = arg;
1435 struct imsgbuf *ibuf = &iev->ibuf;
1439 struct repo_write_client *client = &repo_write_client;
1441 if (event & EV_READ) {
1442 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1443 fatal("imsg_read error");
1444 if (n == 0) /* Connection closed. */
1448 if (event & EV_WRITE) {
1449 n = msgbuf_write(&ibuf->w);
1450 if (n == -1 && errno != EAGAIN)
1451 fatal("msgbuf_write");
1452 if (n == 0) /* Connection closed. */
1456 while (err == NULL && check_cancelled(NULL) == NULL) {
1457 if ((n = imsg_get(ibuf, &imsg)) == -1)
1458 fatal("%s: imsg_get", __func__);
1459 if (n == 0) /* No more messages. */
1462 switch (imsg.hdr.type) {
1463 case GOTD_IMSG_CONNECT_REPO_CHILD:
1464 err = recv_connect(&imsg);
1467 log_debug("%s: unexpected imsg %d", repo_write.title,
1475 if (!shut && check_cancelled(NULL) == NULL) {
1477 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1478 client->id, err) == -1) {
1479 log_warnx("could not send error to parent: %s",
1482 gotd_imsg_event_add(iev);
1484 /* This pipe is dead. Remove its event handler */
1485 event_del(&iev->ev);
1486 event_loopexit(NULL);
1491 repo_write_main(const char *title, const char *repo_path,
1492 int *pack_fds, int *temp_fds)
1494 const struct got_error *err = NULL;
1495 struct gotd_imsgev iev;
1497 repo_write.title = title;
1498 repo_write.pid = getpid();
1499 repo_write.pack_fds = pack_fds;
1500 repo_write.temp_fds = temp_fds;
1501 repo_write.session_fd = -1;
1502 repo_write.session_iev.ibuf.fd = -1;
1504 STAILQ_INIT(&repo_write_client.ref_updates);
1506 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1509 if (!got_repo_is_bare(repo_write.repo)) {
1510 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1511 "bare git repository required");
1515 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1517 signal(SIGINT, catch_sigint);
1518 signal(SIGTERM, catch_sigterm);
1519 signal(SIGPIPE, SIG_IGN);
1520 signal(SIGHUP, SIG_IGN);
1522 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1523 iev.handler = repo_write_dispatch;
1524 iev.events = EV_READ;
1525 iev.handler_arg = NULL;
1526 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1527 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1528 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1529 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1536 log_warnx("%s: %s", title, err->msg);
1537 repo_write_shutdown();
1541 repo_write_shutdown(void)
1543 log_debug("%s: shutting down", repo_write.title);
1544 if (repo_write.repo)
1545 got_repo_close(repo_write.repo);
1546 got_repo_pack_fds_close(repo_write.pack_fds);
1547 got_repo_temp_fds_close(repo_write.temp_fds);
1548 if (repo_write.session_fd != -1)
1549 close(repo_write.session_fd);