2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
20 #include <sys/types.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_object.h"
41 #include "got_reference.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_delta_cache.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_ratelimit.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pack_index.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_poll.h"
54 #include "got_lib_sha1.h" /* XXX temp include for debugging */
58 #include "repo_write.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static struct repo_write {
67 struct got_repository *repo;
71 struct gotd_imsgev session_iev;
74 struct gotd_ref_update {
75 STAILQ_ENTRY(gotd_ref_update) entry;
76 struct got_reference *ref;
79 struct got_object_id old_id;
80 struct got_object_id new_id;
82 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
84 static struct repo_write_client {
89 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
91 struct gotd_ref_updates ref_updates;
97 static volatile sig_atomic_t sigint_received;
98 static volatile sig_atomic_t sigterm_received;
101 catch_sigint(int signo)
107 catch_sigterm(int signo)
109 sigterm_received = 1;
112 static const struct got_error *
113 check_cancelled(void *arg)
115 if (sigint_received || sigterm_received)
116 return got_error(GOT_ERR_CANCELLED);
121 static const struct got_error *
122 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
123 struct imsgbuf *ibuf)
125 const struct got_error *err = NULL;
126 struct got_tag_object *tag;
128 char *peeled_refname = NULL;
129 struct got_object_id *id;
132 err = got_object_tag_open(&tag, repo_write.repo, obj);
136 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
137 err = got_error_from_errno("asprintf");
141 id = got_object_tag_get_object_id(tag);
142 namelen = strlen(peeled_refname);
144 len = sizeof(struct gotd_imsg_ref) + namelen;
145 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
146 err = got_error(GOT_ERR_NO_SPACE);
150 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
151 repo_write.pid, len);
153 err = got_error_from_errno("imsg_create REF");
157 /* Keep in sync with struct gotd_imsg_ref definition. */
158 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
159 err = got_error_from_errno("imsg_add REF");
162 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
163 err = got_error_from_errno("imsg_add REF");
166 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
167 err = got_error_from_errno("imsg_add REF");
172 imsg_close(ibuf, wbuf);
174 got_object_tag_close(tag);
178 static const struct got_error *
179 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
181 const struct got_error *err;
182 const char *refname = got_ref_get_name(ref);
184 struct got_object_id *id = NULL;
185 struct got_object *obj = NULL;
189 namelen = strlen(refname);
191 len = sizeof(struct gotd_imsg_ref) + namelen;
192 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
193 return got_error(GOT_ERR_NO_SPACE);
195 err = got_ref_resolve(&id, repo_write.repo, ref);
199 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
200 repo_write.pid, len);
202 err = got_error_from_errno("imsg_create REF");
206 /* Keep in sync with struct gotd_imsg_ref definition. */
207 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
208 return got_error_from_errno("imsg_add REF");
209 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
210 return got_error_from_errno("imsg_add REF");
211 if (imsg_add(wbuf, refname, namelen) == -1)
212 return got_error_from_errno("imsg_add REF");
215 imsg_close(ibuf, wbuf);
217 err = got_object_open(&obj, repo_write.repo, id);
220 if (obj->type == GOT_OBJ_TYPE_TAG)
221 err = send_peeled_tag_ref(ref, obj, ibuf);
224 got_object_close(obj);
229 static const struct got_error *
230 list_refs(struct imsg *imsg)
232 const struct got_error *err;
233 struct repo_write_client *client = &repo_write_client;
234 struct got_reflist_head refs;
235 struct got_reflist_entry *re;
236 struct gotd_imsg_list_refs_internal ireq;
238 struct gotd_imsg_reflist irefs;
240 int client_fd = imsg->fd;
245 return got_error(GOT_ERR_PRIVSEP_NO_FD);
247 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
248 if (datalen != sizeof(ireq))
249 return got_error(GOT_ERR_PRIVSEP_LEN);
250 memcpy(&ireq, imsg->data, sizeof(ireq));
252 if (ireq.client_id == 0)
253 return got_error(GOT_ERR_CLIENT_ID);
254 if (client->id != 0) {
255 return got_error_msg(GOT_ERR_CLIENT_ID,
256 "duplicate list-refs request");
258 client->id = ireq.client_id;
259 client->fd = client_fd;
260 client->pack_pipe = -1;
261 client->packidx_fd = -1;
262 client->nref_updates = 0;
263 client->nref_del = 0;
264 client->nref_new = 0;
266 imsg_init(&ibuf, client_fd);
268 err = got_ref_list(&refs, repo_write.repo, "",
269 got_ref_cmp_by_name, NULL);
273 memset(&irefs, 0, sizeof(irefs));
274 TAILQ_FOREACH(re, &refs, entry) {
275 struct got_object_id *id;
278 if (got_ref_is_symbolic(re->ref))
283 /* Account for a peeled tag refs. */
284 err = got_ref_resolve(&id, repo_write.repo, re->ref);
287 err = got_object_get_type(&obj_type, repo_write.repo, id);
291 if (obj_type == GOT_OBJ_TYPE_TAG)
295 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
296 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
297 err = got_error_from_errno("imsg_compose REFLIST");
301 TAILQ_FOREACH(re, &refs, entry) {
302 if (got_ref_is_symbolic(re->ref))
304 err = send_ref(re->ref, &ibuf);
309 err = gotd_imsg_flush(&ibuf);
311 got_ref_list_free(&refs);
316 static const struct got_error *
317 protect_ref_namespace(struct got_reference *ref, const char *namespace)
319 size_t len = strlen(namespace);
321 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
322 namespace[len -1] != '/') {
323 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
324 "reference namespace '%s'", namespace);
327 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
328 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
333 static const struct got_error *
334 recv_ref_update(struct imsg *imsg)
336 static const char zero_id[SHA1_DIGEST_LENGTH];
337 const struct got_error *err = NULL;
338 struct repo_write_client *client = &repo_write_client;
339 struct gotd_imsg_ref_update iref;
341 char *refname = NULL;
342 struct got_reference *ref = NULL;
343 struct got_object_id *id = NULL;
345 struct gotd_ref_update *ref_update = NULL;
347 log_debug("ref-update received");
349 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
350 if (datalen < sizeof(iref))
351 return got_error(GOT_ERR_PRIVSEP_LEN);
352 memcpy(&iref, imsg->data, sizeof(iref));
353 if (datalen != sizeof(iref) + iref.name_len)
354 return got_error(GOT_ERR_PRIVSEP_LEN);
356 imsg_init(&ibuf, client->fd);
358 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
360 return got_error_from_errno("strndup");
362 ref_update = calloc(1, sizeof(*ref_update));
363 if (ref_update == NULL) {
364 err = got_error_from_errno("malloc");
368 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
369 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
371 err = got_ref_open(&ref, repo_write.repo, refname, 0);
373 if (err->code != GOT_ERR_NOT_REF)
375 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
378 ref_update->ref_is_new = 1;
381 if (got_ref_is_symbolic(ref)) {
382 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
383 "'%s' is a symbolic reference and cannot "
384 "be updated", got_ref_get_name(ref));
387 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
388 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
389 "%s: does not begin with 'refs/'",
390 got_ref_get_name(ref));
394 err = protect_ref_namespace(ref, "refs/got/");
397 err = protect_ref_namespace(ref, "refs/remotes/");
401 if (!ref_update->ref_is_new) {
403 * Ensure the client's idea of this update is still valid.
404 * At this point we can only return an error, to prevent
405 * the client from uploading a pack file which will likely
406 * have to be discarded.
408 err = got_ref_resolve(&id, repo_write.repo, ref);
412 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
413 err = got_error_fmt(GOT_ERR_REF_BUSY,
414 "%s has been modified by someone else "
415 "while transaction was in progress",
416 got_ref_get_name(ref));
421 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
424 ref_update->ref = ref;
425 if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
426 ref_update->delete_ref = 1;
429 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
430 client->nref_updates++;
442 static const struct got_error *
443 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
444 uint32_t nobj_loose, uint32_t nobj_resolved)
446 int p_indexed = 0, p_resolved = 0;
447 int nobj_delta = nobj_total - nobj_loose;
450 p_indexed = (nobj_indexed * 100) / nobj_total;
453 p_resolved = (nobj_resolved * 100) / nobj_delta;
455 if (p_resolved > 0) {
456 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
457 nobj_total, p_indexed, nobj_delta, p_resolved);
459 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
464 static const struct got_error *
465 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
467 const struct got_error *err = NULL;
468 uint8_t readahead[65536];
471 err = got_poll_read_full(infd, &have,
472 readahead, sizeof(readahead), minsize);
476 err = buf_append(&newlen, buf, readahead, have);
482 static const struct got_error *
483 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
484 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
486 const struct got_error *err = NULL;
491 off_t obj_offset = *outsize;
494 /* We do not support size values which don't fit in 64 bit. */
496 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
497 "packfile offset %lld", (long long)obj_offset);
499 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
500 err = read_more_pack_stream(infd, buf,
506 sizebuf[i] = buf_getc(buf, *buf_pos);
507 *buf_pos += sizeof(sizebuf[i]);
510 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
511 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
512 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
514 size_t shift = 4 + 7 * (i - 1);
515 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
519 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
521 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
531 static const struct got_error *
532 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
535 const struct got_error *err = NULL;
536 size_t remain = buf_len(buf) - *buf_pos;
538 if (remain < SHA1_DIGEST_LENGTH) {
539 err = read_more_pack_stream(infd, buf,
540 SHA1_DIGEST_LENGTH - remain);
545 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
546 SHA1_DIGEST_LENGTH, ctx);
550 *buf_pos += SHA1_DIGEST_LENGTH;
554 static const struct got_error *
555 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
558 const struct got_error *err = NULL;
562 off_t obj_offset = *outsize;
565 /* We do not support offset values which don't fit in 64 bit. */
567 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
568 "packfile offset %lld", (long long)obj_offset);
570 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
571 err = read_more_pack_stream(infd, buf,
577 offbuf[i] = buf_getc(buf, *buf_pos);
578 *buf_pos += sizeof(offbuf[i]);
581 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
585 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
588 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
590 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
591 return got_error(GOT_ERR_PACK_OFFSET);
593 err = got_pack_hwrite(outfd, offbuf, i, ctx);
601 static const struct got_error *
602 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
605 const struct got_error *err = NULL;
609 size_t consumed_total = 0;
610 off_t zstream_offset = *outsize;
612 memset(&z, 0, sizeof(z));
616 zret = inflateInit(&z);
619 return got_error_from_errno("inflateInit");
620 if (zret == Z_MEM_ERROR) {
622 return got_error_from_errno("inflateInit");
624 return got_error_msg(GOT_ERR_DECOMPRESSION,
625 "inflateInit failed");
628 while (zret != Z_STREAM_END) {
629 size_t last_total_in, consumed;
632 * Decompress into the void. Object data will be parsed
633 * later, when the pack file is indexed. For now, we just
634 * want to locate the end of the compressed stream.
636 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
637 last_total_in = z.total_in;
638 z.next_in = buf_get(buf) + *buf_pos;
639 z.avail_in = buf_len(buf) - *buf_pos;
640 z.next_out = voidbuf;
641 z.avail_out = sizeof(voidbuf);
643 zret = inflate(&z, Z_SYNC_FLUSH);
644 if (zret != Z_OK && zret != Z_BUF_ERROR &&
645 zret != Z_STREAM_END) {
646 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
647 "packfile offset %lld",
648 (long long)zstream_offset);
651 consumed = z.total_in - last_total_in;
653 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
658 err = buf_discard(buf, *buf_pos + consumed);
663 consumed_total += consumed;
666 if (zret != Z_STREAM_END) {
667 err = read_more_pack_stream(infd, buf, 1);
674 *outsize += consumed_total;
680 static const struct got_error *
681 validate_object_type(int obj_type)
684 case GOT_OBJ_TYPE_BLOB:
685 case GOT_OBJ_TYPE_COMMIT:
686 case GOT_OBJ_TYPE_TREE:
687 case GOT_OBJ_TYPE_TAG:
688 case GOT_OBJ_TYPE_REF_DELTA:
689 case GOT_OBJ_TYPE_OFFSET_DELTA:
695 return got_error(GOT_ERR_OBJ_TYPE);
698 static const struct got_error *
699 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
702 const struct got_error *err;
703 struct repo_write_client *client = &repo_write_client;
704 struct got_packfile_hdr hdr;
708 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
709 char hex[SHA1_DIGEST_STRING_LENGTH];
711 size_t buf_pos = 0, remain;
717 /* if only deleting references there's nothing to read */
718 if (client->nref_updates == client->nref_del)
723 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
726 if (have != sizeof(hdr))
727 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
730 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
731 return got_error_msg(GOT_ERR_BAD_PACKFILE,
732 "bad packfile signature");
733 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
734 return got_error_msg(GOT_ERR_BAD_PACKFILE,
735 "bad packfile version");
737 *nobj = be32toh(hdr.nobjects);
740 * Clients which are creating new references only
741 * will send us an empty pack file.
743 if (client->nref_updates > 0 &&
744 client->nref_updates == client->nref_new)
747 return got_error_msg(GOT_ERR_BAD_PACKFILE,
748 "bad packfile with zero objects");
751 log_debug("expecting %d objects", *nobj);
753 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
757 err = buf_alloc(&buf, 65536);
761 while (nhave != *nobj) {
765 err = copy_object_type_and_size(&obj_type, &obj_size,
766 infd, outfd, outsize, buf, &buf_pos, &ctx);
770 err = validate_object_type(obj_type);
774 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
775 err = copy_ref_delta(infd, outfd, outsize,
776 buf, &buf_pos, &ctx);
779 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
780 err = copy_offset_delta(infd, outfd, outsize,
781 buf, &buf_pos, &ctx);
786 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
793 log_debug("received %u objects", *nobj);
795 SHA1Final(expected_sha1, &ctx);
797 remain = buf_len(buf) - buf_pos;
798 if (remain < SHA1_DIGEST_LENGTH) {
799 err = read_more_pack_stream(infd, buf,
800 SHA1_DIGEST_LENGTH - remain);
805 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
806 log_debug("expect SHA1: %s", hex);
807 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
808 log_debug("actual SHA1: %s", hex);
810 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
811 SHA1_DIGEST_LENGTH) != 0) {
812 err = got_error(GOT_ERR_PACKFILE_CSUM);
816 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
818 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
820 err = got_error_from_errno("write");
823 if (w != SHA1_DIGEST_LENGTH) {
824 err = got_error(GOT_ERR_IO);
828 *outsize += SHA1_DIGEST_LENGTH;
830 if (fsync(outfd) == -1) {
831 err = got_error_from_errno("fsync");
834 if (lseek(outfd, 0L, SEEK_SET) == -1) {
835 err = got_error_from_errno("lseek");
843 static const struct got_error *
844 report_pack_status(const struct got_error *unpack_err)
846 const struct got_error *err = NULL;
847 struct repo_write_client *client = &repo_write_client;
848 struct gotd_imsg_packfile_status istatus;
851 const char *unpack_ok = "unpack ok\n";
854 imsg_init(&ibuf, client->fd);
857 istatus.reason_len = strlen(unpack_err->msg);
859 istatus.reason_len = strlen(unpack_ok);
861 len = sizeof(istatus) + istatus.reason_len;
862 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
863 repo_write.pid, len);
865 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
869 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
870 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
874 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
875 istatus.reason_len) == -1) {
876 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
881 imsg_close(&ibuf, wbuf);
883 err = gotd_imsg_flush(&ibuf);
889 static const struct got_error *
890 recv_packfile(int *have_packfile, struct imsg *imsg)
892 const struct got_error *err = NULL, *unpack_err;
893 struct repo_write_client *client = &repo_write_client;
894 struct gotd_imsg_recv_packfile ireq;
895 FILE *tempfiles[3] = { NULL, NULL, NULL };
896 struct repo_tempfile {
899 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
903 struct got_ratelimit rl;
904 struct got_pack *pack = NULL;
905 off_t pack_filesize = 0;
908 log_debug("packfile request received");
911 got_ratelimit_init(&rl, 2, 0);
913 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
914 if (datalen != sizeof(ireq))
915 return got_error(GOT_ERR_PRIVSEP_LEN);
916 memcpy(&ireq, imsg->data, sizeof(ireq));
918 if (client->pack_pipe == -1 || client->packidx_fd == -1)
919 return got_error(GOT_ERR_PRIVSEP_NO_FD);
921 imsg_init(&ibuf, client->fd);
924 return got_error(GOT_ERR_PRIVSEP_NO_FD);
926 pack = &client->pack;
927 memset(pack, 0, sizeof(*pack));
929 err = got_delta_cache_alloc(&pack->delta_cache);
933 for (i = 0; i < nitems(repo_tempfiles); i++) {
934 struct repo_tempfile *t = &repo_tempfiles[i];
935 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
940 for (i = 0; i < nitems(tempfiles); i++) {
944 fd = dup(repo_tempfiles[i].fd);
946 err = got_error_from_errno("dup");
949 f = fdopen(fd, "w+");
951 err = got_error_from_errno("fdopen");
958 err = gotd_imsg_flush(&ibuf);
962 log_debug("receiving pack data");
963 unpack_err = recv_packdata(&pack_filesize, &nobj,
964 client->pack_sha1, client->pack_pipe, pack->fd);
965 if (ireq.report_status) {
966 err = report_pack_status(unpack_err);
968 /* Git clients hang up after sending the pack file. */
969 if (err->code == GOT_ERR_EOF)
978 log_debug("pack data received");
981 * Clients which are creating new references only will
982 * send us an empty pack file.
985 pack_filesize == sizeof(struct got_packfile_hdr) &&
986 client->nref_updates > 0 &&
987 client->nref_updates == client->nref_new)
991 * Clients which are deleting references only will send
995 client->nref_del > 0 &&
996 client->nref_updates == client->nref_del)
999 pack->filesize = pack_filesize;
1002 log_debug("begin indexing pack (%lld bytes in size)",
1003 (long long)pack->filesize);
1004 err = got_pack_index(pack, client->packidx_fd,
1005 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1006 pack_index_progress, NULL, &rl);
1009 log_debug("done indexing pack");
1011 if (fsync(client->packidx_fd) == -1) {
1012 err = got_error_from_errno("fsync");
1015 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1016 err = got_error_from_errno("lseek");
1018 if (close(client->pack_pipe) == -1 && err == NULL)
1019 err = got_error_from_errno("close");
1020 client->pack_pipe = -1;
1021 for (i = 0; i < nitems(repo_tempfiles); i++) {
1022 struct repo_tempfile *t = &repo_tempfiles[i];
1024 got_repo_temp_fds_put(t->idx, repo_write.repo);
1026 for (i = 0; i < nitems(tempfiles); i++) {
1027 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1028 err = got_error_from_errno("fclose");
1031 got_pack_close(pack);
1036 static const struct got_error *
1037 verify_packfile(void)
1039 const struct got_error *err = NULL, *close_err;
1040 struct repo_write_client *client = &repo_write_client;
1041 struct gotd_ref_update *ref_update;
1042 struct got_packidx *packidx = NULL;
1044 char *id_str = NULL;
1047 if (STAILQ_EMPTY(&client->ref_updates)) {
1048 return got_error_msg(GOT_ERR_BAD_REQUEST,
1049 "cannot verify pack file without any ref-updates");
1052 if (client->pack.fd == -1) {
1053 return got_error_msg(GOT_ERR_BAD_REQUEST,
1054 "invalid pack file handle during pack verification");
1056 if (client->packidx_fd == -1) {
1057 return got_error_msg(GOT_ERR_BAD_REQUEST,
1058 "invalid pack index handle during pack verification");
1061 if (fstat(client->packidx_fd, &sb) == -1)
1062 return got_error_from_errno("pack index fstat");
1064 packidx = malloc(sizeof(*packidx));
1065 memset(packidx, 0, sizeof(*packidx));
1066 packidx->fd = client->packidx_fd;
1067 client->packidx_fd = -1;
1068 packidx->len = sb.st_size;
1070 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1074 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1075 if (ref_update->delete_ref)
1078 err = got_object_id_str(&id_str, &ref_update->new_id);
1082 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1084 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1085 "advertised object %s is missing from pack file",
1092 close_err = got_packidx_close(packidx);
1093 if (close_err && err == NULL)
1099 static const struct got_error *
1100 install_packfile(struct gotd_imsgev *iev)
1102 struct repo_write_client *client = &repo_write_client;
1103 struct gotd_imsg_packfile_install inst;
1106 memset(&inst, 0, sizeof(inst));
1107 inst.client_id = client->id;
1108 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1110 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1111 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1113 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1118 static const struct got_error *
1119 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1121 struct repo_write_client *client = &repo_write_client;
1122 struct gotd_imsg_ref_updates_start istart;
1125 memset(&istart, 0, sizeof(istart));
1126 istart.nref_updates = nref_updates;
1127 istart.client_id = client->id;
1129 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1130 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1132 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1138 static const struct got_error *
1139 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1141 struct repo_write_client *client = &repo_write_client;
1142 struct gotd_imsg_ref_update iref;
1143 const char *refname = got_ref_get_name(ref_update->ref);
1147 memset(&iref, 0, sizeof(iref));
1148 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1149 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1150 iref.ref_is_new = ref_update->ref_is_new;
1151 iref.delete_ref = ref_update->delete_ref;
1152 iref.client_id = client->id;
1153 iref.name_len = strlen(refname);
1155 len = sizeof(iref) + iref.name_len;
1156 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1157 repo_write.pid, len);
1159 return got_error_from_errno("imsg_create REF_UPDATE");
1161 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1162 return got_error_from_errno("imsg_add REF_UPDATE");
1163 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1164 return got_error_from_errno("imsg_add REF_UPDATE");
1167 imsg_close(&iev->ibuf, wbuf);
1169 gotd_imsg_event_add(iev);
1173 static const struct got_error *
1174 update_refs(struct gotd_imsgev *iev)
1176 const struct got_error *err = NULL;
1177 struct repo_write_client *client = &repo_write_client;
1178 struct gotd_ref_update *ref_update;
1180 err = send_ref_updates_start(client->nref_updates, iev);
1184 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1185 err = send_ref_update(ref_update, iev);
1193 static const struct got_error *
1194 recv_disconnect(struct imsg *imsg)
1196 const struct got_error *err = NULL;
1197 struct gotd_imsg_disconnect idisconnect;
1199 int pack_pipe = -1, idxfd = -1;
1200 struct repo_write_client *client = &repo_write_client;
1202 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1203 if (datalen != sizeof(idisconnect))
1204 return got_error(GOT_ERR_PRIVSEP_LEN);
1205 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1207 log_debug("client disconnecting");
1209 while (!STAILQ_EMPTY(&client->ref_updates)) {
1210 struct gotd_ref_update *ref_update;
1211 ref_update = STAILQ_FIRST(&client->ref_updates);
1212 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1213 got_ref_close(ref_update->ref);
1216 err = got_pack_close(&client->pack);
1217 if (client->fd != -1 && close(client->fd) == -1)
1218 err = got_error_from_errno("close");
1219 pack_pipe = client->pack_pipe;
1220 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1221 err = got_error_from_errno("close");
1222 idxfd = client->packidx_fd;
1223 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1224 err = got_error_from_errno("close");
1228 static const struct got_error *
1229 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1231 struct repo_write_client *client = &repo_write_client;
1232 struct gotd_imsg_packfile_pipe ireq;
1235 log_debug("receving pack pipe descriptor");
1238 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1240 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1241 if (datalen != sizeof(ireq))
1242 return got_error(GOT_ERR_PRIVSEP_LEN);
1243 memcpy(&ireq, imsg->data, sizeof(ireq));
1245 if (client->pack_pipe != -1)
1246 return got_error(GOT_ERR_PRIVSEP_MSG);
1248 client->pack_pipe = imsg->fd;
1252 static const struct got_error *
1253 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1255 struct repo_write_client *client = &repo_write_client;
1256 struct gotd_imsg_packidx_file ireq;
1259 log_debug("receving pack index output file");
1262 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1264 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1265 if (datalen != sizeof(ireq))
1266 return got_error(GOT_ERR_PRIVSEP_LEN);
1267 memcpy(&ireq, imsg->data, sizeof(ireq));
1269 if (client->packidx_fd != -1)
1270 return got_error(GOT_ERR_PRIVSEP_MSG);
1272 client->packidx_fd = imsg->fd;
1277 repo_write_dispatch_session(int fd, short event, void *arg)
1279 const struct got_error *err = NULL;
1280 struct gotd_imsgev *iev = arg;
1281 struct imsgbuf *ibuf = &iev->ibuf;
1283 struct repo_write_client *client = &repo_write_client;
1285 int shut = 0, have_packfile = 0;
1287 if (event & EV_READ) {
1288 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1289 fatal("imsg_read error");
1290 if (n == 0) /* Connection closed. */
1294 if (event & EV_WRITE) {
1295 n = msgbuf_write(&ibuf->w);
1296 if (n == -1 && errno != EAGAIN)
1297 fatal("msgbuf_write");
1298 if (n == 0) /* Connection closed. */
1303 if ((n = imsg_get(ibuf, &imsg)) == -1)
1304 fatal("%s: imsg_get error", __func__);
1305 if (n == 0) /* No more messages. */
1308 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1310 err = got_error(GOT_ERR_PRIVSEP_MSG);
1314 switch (imsg.hdr.type) {
1315 case GOTD_IMSG_LIST_REFS_INTERNAL:
1316 err = list_refs(&imsg);
1318 log_warnx("%s: ls-refs: %s", repo_write.title,
1321 case GOTD_IMSG_REF_UPDATE:
1322 err = recv_ref_update(&imsg);
1324 log_warnx("%s: ref-update: %s",
1325 repo_write.title, err->msg);
1327 case GOTD_IMSG_PACKFILE_PIPE:
1328 err = receive_pack_pipe(&imsg, iev);
1330 log_warnx("%s: receiving pack pipe: %s",
1331 repo_write.title, err->msg);
1335 case GOTD_IMSG_PACKIDX_FILE:
1336 err = receive_pack_idx(&imsg, iev);
1338 log_warnx("%s: receiving pack index: %s",
1339 repo_write.title, err->msg);
1343 case GOTD_IMSG_RECV_PACKFILE:
1344 err = recv_packfile(&have_packfile, &imsg);
1346 log_warnx("%s: receive packfile: %s",
1347 repo_write.title, err->msg);
1350 if (have_packfile) {
1351 err = verify_packfile();
1353 log_warnx("%s: verify packfile: %s",
1354 repo_write.title, err->msg);
1357 err = install_packfile(iev);
1359 log_warnx("%s: install packfile: %s",
1360 repo_write.title, err->msg);
1364 err = update_refs(iev);
1366 log_warnx("%s: update refs: %s",
1367 repo_write.title, err->msg);
1370 case GOTD_IMSG_DISCONNECT:
1371 err = recv_disconnect(&imsg);
1373 log_warnx("%s: disconnect: %s",
1374 repo_write.title, err->msg);
1378 log_debug("%s: unexpected imsg %d", repo_write.title,
1386 if (!shut && check_cancelled(NULL) == NULL) {
1388 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1389 client->id, err) == -1) {
1390 log_warnx("could not send error to parent: %s",
1393 gotd_imsg_event_add(iev);
1395 /* This pipe is dead. Remove its event handler */
1396 event_del(&iev->ev);
1397 event_loopexit(NULL);
1401 static const struct got_error *
1402 recv_connect(struct imsg *imsg)
1404 struct gotd_imsgev *iev = &repo_write.session_iev;
1407 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1409 return got_error(GOT_ERR_PRIVSEP_LEN);
1411 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1413 if (repo_write.session_fd != -1)
1414 return got_error(GOT_ERR_PRIVSEP_MSG);
1416 repo_write.session_fd = imsg->fd;
1418 imsg_init(&iev->ibuf, repo_write.session_fd);
1419 iev->handler = repo_write_dispatch_session;
1420 iev->events = EV_READ;
1421 iev->handler_arg = NULL;
1422 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1423 repo_write_dispatch_session, iev);
1424 gotd_imsg_event_add(iev);
1430 repo_write_dispatch(int fd, short event, void *arg)
1432 const struct got_error *err = NULL;
1433 struct gotd_imsgev *iev = arg;
1434 struct imsgbuf *ibuf = &iev->ibuf;
1438 struct repo_write_client *client = &repo_write_client;
1440 if (event & EV_READ) {
1441 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1442 fatal("imsg_read error");
1443 if (n == 0) /* Connection closed. */
1447 if (event & EV_WRITE) {
1448 n = msgbuf_write(&ibuf->w);
1449 if (n == -1 && errno != EAGAIN)
1450 fatal("msgbuf_write");
1451 if (n == 0) /* Connection closed. */
1455 while (err == NULL && check_cancelled(NULL) == NULL) {
1456 if ((n = imsg_get(ibuf, &imsg)) == -1)
1457 fatal("%s: imsg_get", __func__);
1458 if (n == 0) /* No more messages. */
1461 switch (imsg.hdr.type) {
1462 case GOTD_IMSG_CONNECT_REPO_CHILD:
1463 err = recv_connect(&imsg);
1466 log_debug("%s: unexpected imsg %d", repo_write.title,
1474 if (!shut && check_cancelled(NULL) == NULL) {
1476 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1477 client->id, err) == -1) {
1478 log_warnx("could not send error to parent: %s",
1481 gotd_imsg_event_add(iev);
1483 /* This pipe is dead. Remove its event handler */
1484 event_del(&iev->ev);
1485 event_loopexit(NULL);
1490 repo_write_main(const char *title, const char *repo_path,
1491 int *pack_fds, int *temp_fds)
1493 const struct got_error *err = NULL;
1494 struct gotd_imsgev iev;
1496 repo_write.title = title;
1497 repo_write.pid = getpid();
1498 repo_write.pack_fds = pack_fds;
1499 repo_write.temp_fds = temp_fds;
1500 repo_write.session_fd = -1;
1501 repo_write.session_iev.ibuf.fd = -1;
1503 STAILQ_INIT(&repo_write_client.ref_updates);
1505 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1508 if (!got_repo_is_bare(repo_write.repo)) {
1509 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1510 "bare git repository required");
1514 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1516 signal(SIGINT, catch_sigint);
1517 signal(SIGTERM, catch_sigterm);
1518 signal(SIGPIPE, SIG_IGN);
1519 signal(SIGHUP, SIG_IGN);
1521 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1522 iev.handler = repo_write_dispatch;
1523 iev.events = EV_READ;
1524 iev.handler_arg = NULL;
1525 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1526 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1527 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1528 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1535 log_warnx("%s: %s", title, err->msg);
1536 repo_write_shutdown();
1540 repo_write_shutdown(void)
1542 log_debug("%s: shutting down", repo_write.title);
1543 if (repo_write.repo)
1544 got_repo_close(repo_write.repo);
1545 got_repo_pack_fds_close(repo_write.pack_fds);
1546 got_repo_temp_fds_close(repo_write.temp_fds);
1547 if (repo_write.session_fd != -1)
1548 close(repo_write.session_fd);