2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
20 #include <sys/types.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_object.h"
41 #include "got_reference.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_delta_cache.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_ratelimit.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pack_index.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_poll.h"
54 #include "got_lib_sha1.h" /* XXX temp include for debugging */
58 #include "repo_write.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static struct repo_write {
67 struct got_repository *repo;
71 struct gotd_imsgev session_iev;
74 struct gotd_ref_update {
75 STAILQ_ENTRY(gotd_ref_update) entry;
76 struct got_reference *ref;
78 struct got_object_id old_id;
79 struct got_object_id new_id;
81 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
83 static struct repo_write_client {
88 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
90 struct gotd_ref_updates ref_updates;
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->pack_pipe = -1;
259 client->packidx_fd = -1;
260 client->nref_updates = 0;
261 client->nref_new = 0;
263 imsg_init(&ibuf, client_fd);
265 err = got_ref_list(&refs, repo_write.repo, "",
266 got_ref_cmp_by_name, NULL);
270 memset(&irefs, 0, sizeof(irefs));
271 TAILQ_FOREACH(re, &refs, entry) {
272 struct got_object_id *id;
275 if (got_ref_is_symbolic(re->ref))
280 /* Account for a peeled tag refs. */
281 err = got_ref_resolve(&id, repo_write.repo, re->ref);
284 err = got_object_get_type(&obj_type, repo_write.repo, id);
288 if (obj_type == GOT_OBJ_TYPE_TAG)
292 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
293 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
294 err = got_error_from_errno("imsg_compose REFLIST");
298 TAILQ_FOREACH(re, &refs, entry) {
299 if (got_ref_is_symbolic(re->ref))
301 err = send_ref(re->ref, &ibuf);
306 err = gotd_imsg_flush(&ibuf);
308 got_ref_list_free(&refs);
313 static const struct got_error *
314 protect_ref_namespace(struct got_reference *ref, const char *namespace)
316 size_t len = strlen(namespace);
318 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
319 namespace[len -1] != '/') {
320 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
321 "reference namespace '%s'", namespace);
324 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
325 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
330 static const struct got_error *
331 recv_ref_update(struct imsg *imsg)
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 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
422 client->nref_updates++;
434 static const struct got_error *
435 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
436 uint32_t nobj_loose, uint32_t nobj_resolved)
438 int p_indexed = 0, p_resolved = 0;
439 int nobj_delta = nobj_total - nobj_loose;
442 p_indexed = (nobj_indexed * 100) / nobj_total;
445 p_resolved = (nobj_resolved * 100) / nobj_delta;
447 if (p_resolved > 0) {
448 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
449 nobj_total, p_indexed, nobj_delta, p_resolved);
451 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
456 static const struct got_error *
457 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
459 const struct got_error *err = NULL;
460 uint8_t readahead[65536];
463 err = got_poll_read_full(infd, &have,
464 readahead, sizeof(readahead), minsize);
468 err = buf_append(&newlen, buf, readahead, have);
474 static const struct got_error *
475 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
476 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
478 const struct got_error *err = NULL;
483 off_t obj_offset = *outsize;
486 /* We do not support size values which don't fit in 64 bit. */
488 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
489 "packfile offset %lld", (long long)obj_offset);
491 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
492 err = read_more_pack_stream(infd, buf,
498 sizebuf[i] = buf_getc(buf, *buf_pos);
499 *buf_pos += sizeof(sizebuf[i]);
502 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
503 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
504 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
506 size_t shift = 4 + 7 * (i - 1);
507 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
511 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
513 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
523 static const struct got_error *
524 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
527 const struct got_error *err = NULL;
528 size_t remain = buf_len(buf) - *buf_pos;
530 if (remain < SHA1_DIGEST_LENGTH) {
531 err = read_more_pack_stream(infd, buf,
532 SHA1_DIGEST_LENGTH - remain);
537 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
538 SHA1_DIGEST_LENGTH, ctx);
542 *buf_pos += SHA1_DIGEST_LENGTH;
546 static const struct got_error *
547 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
550 const struct got_error *err = NULL;
554 off_t obj_offset = *outsize;
557 /* We do not support offset values which don't fit in 64 bit. */
559 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
560 "packfile offset %lld", (long long)obj_offset);
562 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
563 err = read_more_pack_stream(infd, buf,
569 offbuf[i] = buf_getc(buf, *buf_pos);
570 *buf_pos += sizeof(offbuf[i]);
573 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
577 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
580 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
582 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
583 return got_error(GOT_ERR_PACK_OFFSET);
585 err = got_pack_hwrite(outfd, offbuf, i, ctx);
593 static const struct got_error *
594 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
597 const struct got_error *err = NULL;
601 size_t consumed_total = 0;
602 off_t zstream_offset = *outsize;
604 memset(&z, 0, sizeof(z));
608 zret = inflateInit(&z);
611 return got_error_from_errno("inflateInit");
612 if (zret == Z_MEM_ERROR) {
614 return got_error_from_errno("inflateInit");
616 return got_error_msg(GOT_ERR_DECOMPRESSION,
617 "inflateInit failed");
620 while (zret != Z_STREAM_END) {
621 size_t last_total_in, consumed;
624 * Decompress into the void. Object data will be parsed
625 * later, when the pack file is indexed. For now, we just
626 * want to locate the end of the compressed stream.
628 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
629 last_total_in = z.total_in;
630 z.next_in = buf_get(buf) + *buf_pos;
631 z.avail_in = buf_len(buf) - *buf_pos;
632 z.next_out = voidbuf;
633 z.avail_out = sizeof(voidbuf);
635 zret = inflate(&z, Z_SYNC_FLUSH);
636 if (zret != Z_OK && zret != Z_BUF_ERROR &&
637 zret != Z_STREAM_END) {
638 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
639 "packfile offset %lld",
640 (long long)zstream_offset);
643 consumed = z.total_in - last_total_in;
645 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
650 err = buf_discard(buf, *buf_pos + consumed);
655 consumed_total += consumed;
658 if (zret != Z_STREAM_END) {
659 err = read_more_pack_stream(infd, buf, 1);
666 *outsize += consumed_total;
672 static const struct got_error *
673 validate_object_type(int obj_type)
676 case GOT_OBJ_TYPE_BLOB:
677 case GOT_OBJ_TYPE_COMMIT:
678 case GOT_OBJ_TYPE_TREE:
679 case GOT_OBJ_TYPE_TAG:
680 case GOT_OBJ_TYPE_REF_DELTA:
681 case GOT_OBJ_TYPE_OFFSET_DELTA:
687 return got_error(GOT_ERR_OBJ_TYPE);
690 static const struct got_error *
691 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
694 const struct got_error *err;
695 struct repo_write_client *client = &repo_write_client;
696 struct got_packfile_hdr hdr;
700 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
701 char hex[SHA1_DIGEST_STRING_LENGTH];
703 size_t buf_pos = 0, remain;
710 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
713 if (have != sizeof(hdr))
714 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
717 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
718 return got_error_msg(GOT_ERR_BAD_PACKFILE,
719 "bad packfile signature");
720 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
721 return got_error_msg(GOT_ERR_BAD_PACKFILE,
722 "bad packfile version");
724 *nobj = be32toh(hdr.nobjects);
727 * Clients which are creating new references only
728 * will send us an empty pack file.
730 if (client->nref_updates > 0 &&
731 client->nref_updates == client->nref_new)
734 return got_error_msg(GOT_ERR_BAD_PACKFILE,
735 "bad packfile with zero objects");
738 log_debug("expecting %d objects", *nobj);
740 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
744 err = buf_alloc(&buf, 65536);
748 while (nhave != *nobj) {
752 err = copy_object_type_and_size(&obj_type, &obj_size,
753 infd, outfd, outsize, buf, &buf_pos, &ctx);
757 err = validate_object_type(obj_type);
761 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
762 err = copy_ref_delta(infd, outfd, outsize,
763 buf, &buf_pos, &ctx);
766 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
767 err = copy_offset_delta(infd, outfd, outsize,
768 buf, &buf_pos, &ctx);
773 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
780 log_debug("received %u objects", *nobj);
782 SHA1Final(expected_sha1, &ctx);
784 remain = buf_len(buf) - buf_pos;
785 if (remain < SHA1_DIGEST_LENGTH) {
786 err = read_more_pack_stream(infd, buf,
787 SHA1_DIGEST_LENGTH - remain);
792 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
793 log_debug("expect SHA1: %s", hex);
794 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
795 log_debug("actual SHA1: %s", hex);
797 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
798 SHA1_DIGEST_LENGTH) != 0) {
799 err = got_error(GOT_ERR_PACKFILE_CSUM);
803 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
805 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
807 err = got_error_from_errno("write");
810 if (w != SHA1_DIGEST_LENGTH) {
811 err = got_error(GOT_ERR_IO);
815 *outsize += SHA1_DIGEST_LENGTH;
817 if (fsync(outfd) == -1) {
818 err = got_error_from_errno("fsync");
821 if (lseek(outfd, 0L, SEEK_SET) == -1) {
822 err = got_error_from_errno("lseek");
830 static const struct got_error *
831 report_pack_status(const struct got_error *unpack_err)
833 const struct got_error *err = NULL;
834 struct repo_write_client *client = &repo_write_client;
835 struct gotd_imsg_packfile_status istatus;
838 const char *unpack_ok = "unpack ok\n";
841 imsg_init(&ibuf, client->fd);
844 istatus.reason_len = strlen(unpack_err->msg);
846 istatus.reason_len = strlen(unpack_ok);
848 len = sizeof(istatus) + istatus.reason_len;
849 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
850 repo_write.pid, len);
852 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
856 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
857 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
861 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
862 istatus.reason_len) == -1) {
863 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
868 imsg_close(&ibuf, wbuf);
870 err = gotd_imsg_flush(&ibuf);
876 static const struct got_error *
877 recv_packfile(int *have_packfile, struct imsg *imsg)
879 const struct got_error *err = NULL, *unpack_err;
880 struct repo_write_client *client = &repo_write_client;
881 struct gotd_imsg_recv_packfile ireq;
882 FILE *tempfiles[3] = { NULL, NULL, NULL };
883 struct repo_tempfile {
886 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
890 struct got_ratelimit rl;
891 struct got_pack *pack = NULL;
892 off_t pack_filesize = 0;
895 log_debug("packfile request received");
898 got_ratelimit_init(&rl, 2, 0);
900 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
901 if (datalen != sizeof(ireq))
902 return got_error(GOT_ERR_PRIVSEP_LEN);
903 memcpy(&ireq, imsg->data, sizeof(ireq));
905 if (client->pack_pipe == -1 || client->packidx_fd == -1)
906 return got_error(GOT_ERR_PRIVSEP_NO_FD);
908 imsg_init(&ibuf, client->fd);
911 return got_error(GOT_ERR_PRIVSEP_NO_FD);
913 pack = &client->pack;
914 memset(pack, 0, sizeof(*pack));
916 err = got_delta_cache_alloc(&pack->delta_cache);
920 for (i = 0; i < nitems(repo_tempfiles); i++) {
921 struct repo_tempfile *t = &repo_tempfiles[i];
922 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
927 for (i = 0; i < nitems(tempfiles); i++) {
928 int fd = dup(repo_tempfiles[i].fd);
931 err = got_error_from_errno("dup");
934 f = fdopen(fd, "w+");
936 err = got_error_from_errno("dup");
943 /* Send pack file pipe to gotsh(1). */
944 if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
945 repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
946 (*client)->pack_pipe[1] = -1;
947 err = got_error_from_errno("imsg_compose ACK");
951 (*client)->pack_pipe[1] = -1;
952 err = gotd_imsg_flush(&ibuf);
956 log_debug("receiving pack data");
957 unpack_err = recv_packdata(&pack_filesize, &nobj,
958 client->pack_sha1, client->pack_pipe, pack->fd);
959 if (ireq.report_status) {
960 err = report_pack_status(unpack_err);
962 /* Git clients hang up after sending the pack file. */
963 if (err->code == GOT_ERR_EOF)
972 log_debug("pack data received");
975 * Clients which are creating new references only will
976 * send us an empty pack file.
979 pack_filesize == sizeof(struct got_packfile_hdr) &&
980 client->nref_updates > 0 &&
981 client->nref_updates == client->nref_new)
984 pack->filesize = pack_filesize;
987 log_debug("begin indexing pack (%lld bytes in size)",
988 (long long)pack->filesize);
989 err = got_pack_index(pack, client->packidx_fd,
990 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
991 pack_index_progress, NULL, &rl);
994 log_debug("done indexing pack");
996 if (fsync(client->packidx_fd) == -1) {
997 err = got_error_from_errno("fsync");
1000 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1001 err = got_error_from_errno("lseek");
1003 if (close(client->pack_pipe) == -1 && err == NULL)
1004 err = got_error_from_errno("close");
1005 client->pack_pipe = -1;
1006 for (i = 0; i < nitems(repo_tempfiles); i++) {
1007 struct repo_tempfile *t = &repo_tempfiles[i];
1009 got_repo_temp_fds_put(t->idx, repo_write.repo);
1011 for (i = 0; i < nitems(tempfiles); i++) {
1012 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1013 err = got_error_from_errno("fclose");
1016 got_pack_close(pack);
1021 static const struct got_error *
1022 verify_packfile(void)
1024 const struct got_error *err = NULL, *close_err;
1025 struct repo_write_client *client = &repo_write_client;
1026 struct gotd_ref_update *ref_update;
1027 struct got_packidx *packidx = NULL;
1029 char *id_str = NULL;
1032 if (STAILQ_EMPTY(&client->ref_updates)) {
1033 return got_error_msg(GOT_ERR_BAD_REQUEST,
1034 "cannot verify pack file without any ref-updates");
1037 if (client->pack.fd == -1) {
1038 return got_error_msg(GOT_ERR_BAD_REQUEST,
1039 "invalid pack file handle during pack verification");
1041 if (client->packidx_fd == -1) {
1042 return got_error_msg(GOT_ERR_BAD_REQUEST,
1043 "invalid pack index handle during pack verification");
1046 if (fstat(client->packidx_fd, &sb) == -1)
1047 return got_error_from_errno("pack index fstat");
1049 packidx = malloc(sizeof(*packidx));
1050 memset(packidx, 0, sizeof(*packidx));
1051 packidx->fd = client->packidx_fd;
1052 client->packidx_fd = -1;
1053 packidx->len = sb.st_size;
1055 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1059 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1060 err = got_object_id_str(&id_str, &ref_update->new_id);
1064 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1066 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1067 "advertised object %s is missing from pack file",
1074 close_err = got_packidx_close(packidx);
1075 if (close_err && err == NULL)
1081 static const struct got_error *
1082 install_packfile(struct gotd_imsgev *iev)
1084 struct repo_write_client *client = &repo_write_client;
1085 struct gotd_imsg_packfile_install inst;
1088 memset(&inst, 0, sizeof(inst));
1089 inst.client_id = client->id;
1090 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1092 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1093 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1095 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1100 static const struct got_error *
1101 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1103 struct repo_write_client *client = &repo_write_client;
1104 struct gotd_imsg_ref_updates_start istart;
1107 memset(&istart, 0, sizeof(istart));
1108 istart.nref_updates = nref_updates;
1109 istart.client_id = client->id;
1111 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1112 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1114 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1120 static const struct got_error *
1121 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1123 struct repo_write_client *client = &repo_write_client;
1124 struct gotd_imsg_ref_update iref;
1125 const char *refname = got_ref_get_name(ref_update->ref);
1129 memset(&iref, 0, sizeof(iref));
1130 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1131 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1132 iref.ref_is_new = ref_update->ref_is_new;
1133 iref.client_id = client->id;
1134 iref.name_len = strlen(refname);
1136 len = sizeof(iref) + iref.name_len;
1137 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1138 repo_write.pid, len);
1140 return got_error_from_errno("imsg_create REF_UPDATE");
1142 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1143 return got_error_from_errno("imsg_add REF_UPDATE");
1144 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1145 return got_error_from_errno("imsg_add REF_UPDATE");
1148 imsg_close(&iev->ibuf, wbuf);
1150 gotd_imsg_event_add(iev);
1154 static const struct got_error *
1155 update_refs(struct gotd_imsgev *iev)
1157 const struct got_error *err = NULL;
1158 struct repo_write_client *client = &repo_write_client;
1159 struct gotd_ref_update *ref_update;
1161 err = send_ref_updates_start(client->nref_updates, iev);
1165 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1166 err = send_ref_update(ref_update, iev);
1174 static const struct got_error *
1175 recv_disconnect(struct imsg *imsg)
1177 const struct got_error *err = NULL;
1178 struct gotd_imsg_disconnect idisconnect;
1180 int pack_pipe = -1, idxfd = -1;
1181 struct repo_write_client *client = &repo_write_client;
1183 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1184 if (datalen != sizeof(idisconnect))
1185 return got_error(GOT_ERR_PRIVSEP_LEN);
1186 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1188 log_debug("client disconnecting");
1190 while (!STAILQ_EMPTY(&client->ref_updates)) {
1191 struct gotd_ref_update *ref_update;
1192 ref_update = STAILQ_FIRST(&client->ref_updates);
1193 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1194 got_ref_close(ref_update->ref);
1197 err = got_pack_close(&client->pack);
1198 if (client->fd != -1 && close(client->fd) == -1)
1199 err = got_error_from_errno("close");
1200 pack_pipe = client->pack_pipe;
1201 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1202 err = got_error_from_errno("close");
1203 idxfd = client->packidx_fd;
1204 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1205 err = got_error_from_errno("close");
1209 static const struct got_error *
1210 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1212 struct repo_write_client *client = &repo_write_client;
1213 struct gotd_imsg_packfile_pipe ireq;
1216 log_debug("receving pack pipe descriptor");
1219 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1221 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1222 if (datalen != sizeof(ireq))
1223 return got_error(GOT_ERR_PRIVSEP_LEN);
1224 memcpy(&ireq, imsg->data, sizeof(ireq));
1226 if (client->pack_pipe != -1)
1227 return got_error(GOT_ERR_PRIVSEP_MSG);
1229 client->pack_pipe = imsg->fd;
1233 static const struct got_error *
1234 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1236 struct repo_write_client *client = &repo_write_client;
1237 struct gotd_imsg_packidx_file ireq;
1240 log_debug("receving pack index output file");
1243 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1245 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1246 if (datalen != sizeof(ireq))
1247 return got_error(GOT_ERR_PRIVSEP_LEN);
1248 memcpy(&ireq, imsg->data, sizeof(ireq));
1250 if (client->packidx_fd != -1)
1251 return got_error(GOT_ERR_PRIVSEP_MSG);
1253 client->packidx_fd = imsg->fd;
1258 repo_write_dispatch_session(int fd, short event, void *arg)
1260 const struct got_error *err = NULL;
1261 struct gotd_imsgev *iev = arg;
1262 struct imsgbuf *ibuf = &iev->ibuf;
1264 struct repo_write_client *client = &repo_write_client;
1266 int shut = 0, have_packfile = 0;
1268 if (event & EV_READ) {
1269 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1270 fatal("imsg_read error");
1271 if (n == 0) /* Connection closed. */
1275 if (event & EV_WRITE) {
1276 n = msgbuf_write(&ibuf->w);
1277 if (n == -1 && errno != EAGAIN)
1278 fatal("msgbuf_write");
1279 if (n == 0) /* Connection closed. */
1284 if ((n = imsg_get(ibuf, &imsg)) == -1)
1285 fatal("%s: imsg_get error", __func__);
1286 if (n == 0) /* No more messages. */
1289 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1291 err = got_error(GOT_ERR_PRIVSEP_MSG);
1295 switch (imsg.hdr.type) {
1296 case GOTD_IMSG_LIST_REFS_INTERNAL:
1297 err = list_refs(&imsg);
1299 log_warnx("%s: ls-refs: %s", repo_write.title,
1302 case GOTD_IMSG_REF_UPDATE:
1303 err = recv_ref_update(&imsg);
1305 log_warnx("%s: ref-update: %s",
1306 repo_write.title, err->msg);
1308 case GOTD_IMSG_PACKFILE_PIPE:
1309 err = receive_pack_pipe(&imsg, iev);
1311 log_warnx("%s: receiving pack pipe: %s",
1312 repo_write.title, err->msg);
1316 case GOTD_IMSG_PACKIDX_FILE:
1317 err = receive_pack_idx(&imsg, iev);
1319 log_warnx("%s: receiving pack index: %s",
1320 repo_write.title, err->msg);
1324 case GOTD_IMSG_RECV_PACKFILE:
1325 err = recv_packfile(&have_packfile, &imsg);
1327 log_warnx("%s: receive packfile: %s",
1328 repo_write.title, err->msg);
1331 if (have_packfile) {
1332 err = verify_packfile();
1334 log_warnx("%s: verify packfile: %s",
1335 repo_write.title, err->msg);
1338 err = install_packfile(iev);
1340 log_warnx("%s: install packfile: %s",
1341 repo_write.title, err->msg);
1345 err = update_refs(iev);
1347 log_warnx("%s: update refs: %s",
1348 repo_write.title, err->msg);
1351 case GOTD_IMSG_DISCONNECT:
1352 err = recv_disconnect(&imsg);
1354 log_warnx("%s: disconnect: %s",
1355 repo_write.title, err->msg);
1359 log_debug("%s: unexpected imsg %d", repo_write.title,
1367 if (!shut && check_cancelled(NULL) == NULL) {
1369 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1370 client->id, err) == -1) {
1371 log_warnx("could not send error to parent: %s",
1374 gotd_imsg_event_add(iev);
1376 /* This pipe is dead. Remove its event handler */
1377 event_del(&iev->ev);
1378 event_loopexit(NULL);
1382 static const struct got_error *
1383 recv_connect(struct imsg *imsg)
1385 struct gotd_imsgev *iev = &repo_write.session_iev;
1388 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1390 return got_error(GOT_ERR_PRIVSEP_LEN);
1392 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1394 if (repo_write.session_fd != -1)
1395 return got_error(GOT_ERR_PRIVSEP_MSG);
1397 repo_write.session_fd = imsg->fd;
1399 imsg_init(&iev->ibuf, repo_write.session_fd);
1400 iev->handler = repo_write_dispatch_session;
1401 iev->events = EV_READ;
1402 iev->handler_arg = NULL;
1403 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1404 repo_write_dispatch_session, iev);
1405 gotd_imsg_event_add(iev);
1411 repo_write_dispatch(int fd, short event, void *arg)
1413 const struct got_error *err = NULL;
1414 struct gotd_imsgev *iev = arg;
1415 struct imsgbuf *ibuf = &iev->ibuf;
1419 struct repo_write_client *client = &repo_write_client;
1421 if (event & EV_READ) {
1422 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1423 fatal("imsg_read error");
1424 if (n == 0) /* Connection closed. */
1428 if (event & EV_WRITE) {
1429 n = msgbuf_write(&ibuf->w);
1430 if (n == -1 && errno != EAGAIN)
1431 fatal("msgbuf_write");
1432 if (n == 0) /* Connection closed. */
1436 while (err == NULL && check_cancelled(NULL) == NULL) {
1437 if ((n = imsg_get(ibuf, &imsg)) == -1)
1438 fatal("%s: imsg_get", __func__);
1439 if (n == 0) /* No more messages. */
1442 switch (imsg.hdr.type) {
1443 case GOTD_IMSG_CONNECT_REPO_CHILD:
1444 err = recv_connect(&imsg);
1447 log_debug("%s: unexpected imsg %d", repo_write.title,
1455 if (!shut && check_cancelled(NULL) == NULL) {
1457 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1458 client->id, err) == -1) {
1459 log_warnx("could not send error to parent: %s",
1462 gotd_imsg_event_add(iev);
1464 /* This pipe is dead. Remove its event handler */
1465 event_del(&iev->ev);
1466 event_loopexit(NULL);
1471 repo_write_main(const char *title, const char *repo_path,
1472 int *pack_fds, int *temp_fds)
1474 const struct got_error *err = NULL;
1475 struct gotd_imsgev iev;
1477 repo_write.title = title;
1478 repo_write.pid = getpid();
1479 repo_write.pack_fds = pack_fds;
1480 repo_write.temp_fds = temp_fds;
1481 repo_write.session_fd = -1;
1482 repo_write.session_iev.ibuf.fd = -1;
1484 STAILQ_INIT(&repo_write_client.ref_updates);
1486 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1489 if (!got_repo_is_bare(repo_write.repo)) {
1490 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1491 "bare git repository required");
1495 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1497 signal(SIGINT, catch_sigint);
1498 signal(SIGTERM, catch_sigterm);
1499 signal(SIGPIPE, SIG_IGN);
1500 signal(SIGHUP, SIG_IGN);
1502 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1503 iev.handler = repo_write_dispatch;
1504 iev.events = EV_READ;
1505 iev.handler_arg = NULL;
1506 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1507 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1508 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1509 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1516 log_warnx("%s: %s", title, err->msg);
1517 repo_write_shutdown();
1521 repo_write_shutdown(void)
1523 log_debug("%s: shutting down", repo_write.title);
1524 if (repo_write.repo)
1525 got_repo_close(repo_write.repo);
1526 got_repo_pack_fds_close(repo_write.pack_fds);
1527 got_repo_temp_fds_close(repo_write.temp_fds);
1528 if (repo_write.session_fd != -1)
1529 close(repo_write.session_fd);