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>
40 #include "got_error.h"
41 #include "got_repository.h"
42 #include "got_object.h"
43 #include "got_reference.h"
46 #include "got_cancel.h"
47 #include "got_commit_graph.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_delta_cache.h"
52 #include "got_lib_hash.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_object_idset.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_ratelimit.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_pack_index.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_poll.h"
65 #include "repo_write.h"
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 static struct repo_write {
74 struct got_repository *repo;
78 struct gotd_imsgev session_iev;
79 struct got_pathlist_head *protected_tag_namespaces;
80 struct got_pathlist_head *protected_branch_namespaces;
81 struct got_pathlist_head *protected_branches;
91 struct gotd_ref_update {
92 STAILQ_ENTRY(gotd_ref_update) entry;
93 struct got_reference *ref;
96 struct got_object_id old_id;
97 struct got_object_id new_id;
99 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
101 static struct repo_write_client {
105 struct got_pack pack;
106 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
108 struct gotd_ref_updates ref_updates;
115 static volatile sig_atomic_t sigint_received;
116 static volatile sig_atomic_t sigterm_received;
119 catch_sigint(int signo)
125 catch_sigterm(int signo)
127 sigterm_received = 1;
130 static const struct got_error *
131 check_cancelled(void *arg)
133 if (sigint_received || sigterm_received)
134 return got_error(GOT_ERR_CANCELLED);
139 static const struct got_error *
140 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
141 struct imsgbuf *ibuf)
143 const struct got_error *err = NULL;
144 struct got_tag_object *tag;
146 char *peeled_refname = NULL;
147 struct got_object_id *id;
150 err = got_object_tag_open(&tag, repo_write.repo, obj);
154 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
155 err = got_error_from_errno("asprintf");
159 id = got_object_tag_get_object_id(tag);
160 namelen = strlen(peeled_refname);
162 len = sizeof(struct gotd_imsg_ref) + namelen;
163 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
164 err = got_error(GOT_ERR_NO_SPACE);
168 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
169 repo_write.pid, len);
171 err = got_error_from_errno("imsg_create REF");
175 /* Keep in sync with struct gotd_imsg_ref definition. */
176 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
177 err = got_error_from_errno("imsg_add REF");
180 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
181 err = got_error_from_errno("imsg_add REF");
184 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
185 err = got_error_from_errno("imsg_add REF");
189 imsg_close(ibuf, wbuf);
191 got_object_tag_close(tag);
195 static const struct got_error *
196 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
198 const struct got_error *err;
199 const char *refname = got_ref_get_name(ref);
201 struct got_object_id *id = NULL;
202 struct got_object *obj = NULL;
206 namelen = strlen(refname);
208 len = sizeof(struct gotd_imsg_ref) + namelen;
209 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
210 return got_error(GOT_ERR_NO_SPACE);
212 err = got_ref_resolve(&id, repo_write.repo, ref);
216 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
217 repo_write.pid, len);
219 err = got_error_from_errno("imsg_create REF");
223 /* Keep in sync with struct gotd_imsg_ref definition. */
224 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
225 return got_error_from_errno("imsg_add REF");
226 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
227 return got_error_from_errno("imsg_add REF");
228 if (imsg_add(wbuf, refname, namelen) == -1)
229 return got_error_from_errno("imsg_add REF");
231 imsg_close(ibuf, wbuf);
233 err = got_object_open(&obj, repo_write.repo, id);
236 if (obj->type == GOT_OBJ_TYPE_TAG)
237 err = send_peeled_tag_ref(ref, obj, ibuf);
240 got_object_close(obj);
245 static const struct got_error *
246 list_refs(struct imsg *imsg)
248 const struct got_error *err;
249 struct repo_write_client *client = &repo_write_client;
250 struct got_reflist_head refs;
251 struct got_reflist_entry *re;
253 struct gotd_imsg_reflist irefs;
259 client_fd = imsg_get_fd(imsg);
261 return got_error(GOT_ERR_PRIVSEP_NO_FD);
263 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
265 return got_error(GOT_ERR_PRIVSEP_LEN);
267 if (repo_write.refs_listed) {
268 return got_error_msg(GOT_ERR_CLIENT_ID,
269 "duplicate list-refs request");
271 repo_write.refs_listed = 1;
273 client->fd = client_fd;
274 client->nref_updates = 0;
275 client->nref_del = 0;
276 client->nref_new = 0;
277 client->nref_move = 0;
279 imsg_init(&ibuf, client_fd);
281 err = got_ref_list(&refs, repo_write.repo, "",
282 got_ref_cmp_by_name, NULL);
286 memset(&irefs, 0, sizeof(irefs));
287 TAILQ_FOREACH(re, &refs, entry) {
288 struct got_object_id *id;
291 if (got_ref_is_symbolic(re->ref))
296 /* Account for a peeled tag refs. */
297 err = got_ref_resolve(&id, repo_write.repo, re->ref);
300 err = got_object_get_type(&obj_type, repo_write.repo, id);
304 if (obj_type == GOT_OBJ_TYPE_TAG)
308 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
309 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
310 err = got_error_from_errno("imsg_compose REFLIST");
314 TAILQ_FOREACH(re, &refs, entry) {
315 if (got_ref_is_symbolic(re->ref))
317 err = send_ref(re->ref, &ibuf);
322 err = gotd_imsg_flush(&ibuf);
324 got_ref_list_free(&refs);
329 static const struct got_error *
330 validate_namespace(const char *namespace)
332 size_t len = strlen(namespace);
334 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
335 namespace[len -1] != '/') {
336 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
337 "reference namespace '%s'", namespace);
343 static const struct got_error *
344 protect_ref_namespace(const char *refname, const char *namespace)
346 const struct got_error *err;
348 err = validate_namespace(namespace);
352 if (strncmp(namespace, refname, strlen(namespace)) == 0)
353 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
358 static const struct got_error *
359 verify_object_type(struct got_object_id *id, int expected_obj_type,
360 struct got_pack *pack, struct got_packidx *packidx)
362 const struct got_error *err;
363 char hex[SHA1_DIGEST_STRING_LENGTH];
364 struct got_object *obj;
368 idx = got_packidx_get_object_idx(packidx, id);
370 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
371 return got_error_fmt(GOT_ERR_BAD_PACKFILE,
372 "object %s is missing from pack file", hex);
375 err = got_object_open_from_packfile(&obj, id, pack, packidx,
376 idx, repo_write.repo);
380 if (obj->type != expected_obj_type) {
381 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
382 got_object_type_label(&typestr, expected_obj_type);
383 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
384 "%s is not pointing at a %s object", hex, typestr);
386 got_object_close(obj);
390 static const struct got_error *
391 protect_tag_namespace(const char *namespace, struct got_pack *pack,
392 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
394 const struct got_error *err;
396 err = validate_namespace(namespace);
400 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
401 strlen(namespace)) != 0)
404 if (!ref_update->ref_is_new)
405 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
407 return verify_object_type(&ref_update->new_id, GOT_OBJ_TYPE_TAG,
411 static const struct got_error *
412 protect_require_yca(struct got_object_id *tip_id,
413 size_t max_commits_to_traverse, struct got_pack *pack,
414 struct got_packidx *packidx, struct got_reference *ref)
416 const struct got_error *err;
419 struct got_object_id *expected_yca_id = NULL;
420 struct got_object *obj = NULL;
421 struct got_commit_object *commit = NULL;
422 char hex[SHA1_DIGEST_STRING_LENGTH];
423 const struct got_object_id_queue *parent_ids;
424 struct got_object_id_queue ids;
425 struct got_object_qid *pid, *qid;
426 struct got_object_idset *traversed_set = NULL;
427 int found_yca = 0, obj_type;
431 err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
435 err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
439 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
440 got_sha1_digest_to_str(expected_yca_id->sha1, hex, sizeof(hex));
441 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
442 "%s is not pointing at a commit object", hex);
446 traversed_set = got_object_idset_alloc();
447 if (traversed_set == NULL) {
448 err = got_error_from_errno("got_object_idset_alloc");
452 err = got_object_qid_alloc(&qid, tip_id);
455 STAILQ_INSERT_TAIL(&ids, qid, entry);
456 while (!STAILQ_EMPTY(&ids)) {
457 err = check_cancelled(NULL);
461 qid = STAILQ_FIRST(&ids);
462 if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
467 if (got_object_idset_num_elements(traversed_set) >=
468 max_commits_to_traverse)
471 if (got_object_idset_contains(traversed_set, &qid->id)) {
472 STAILQ_REMOVE_HEAD(&ids, entry);
473 got_object_qid_free(qid);
477 err = got_object_idset_add(traversed_set, &qid->id, NULL);
481 err = got_object_open(&obj, repo_write.repo, &qid->id);
482 if (err && err->code != GOT_ERR_NO_OBJ)
486 err = got_object_commit_open(&commit, repo_write.repo,
493 idx = got_packidx_get_object_idx(packidx, &qid->id);
495 got_sha1_digest_to_str(qid->id.sha1,
497 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
498 "object %s is missing from pack file", hex);
502 err = got_object_open_from_packfile(&obj, &qid->id,
503 pack, packidx, idx, repo_write.repo);
507 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
508 got_sha1_digest_to_str(qid->id.sha1,
510 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
511 "%s is not pointing at a commit object",
516 err = got_packfile_extract_object_to_mem(&buf, &len,
521 err = got_object_parse_commit(&commit, buf, len);
529 got_object_close(obj);
532 STAILQ_REMOVE_HEAD(&ids, entry);
533 got_object_qid_free(qid);
536 if (got_object_commit_get_nparents(commit) == 0)
539 parent_ids = got_object_commit_get_parent_ids(commit);
540 STAILQ_FOREACH(pid, parent_ids, entry) {
541 err = check_cancelled(NULL);
544 err = got_object_qid_alloc(&qid, &pid->id);
547 STAILQ_INSERT_TAIL(&ids, qid, entry);
550 got_object_commit_close(commit);
555 err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
556 got_ref_get_name(ref));
559 got_object_idset_free(traversed_set);
560 got_object_id_queue_free(&ids);
563 got_object_close(obj);
565 got_object_commit_close(commit);
566 free(expected_yca_id);
570 static const struct got_error *
571 protect_branch_namespace(const char *namespace, struct got_pack *pack,
572 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
574 const struct got_error *err;
576 err = validate_namespace(namespace);
580 if (strncmp(namespace, got_ref_get_name(ref_update->ref),
581 strlen(namespace)) != 0)
584 if (ref_update->ref_is_new) {
585 return verify_object_type(&ref_update->new_id,
586 GOT_OBJ_TYPE_COMMIT, pack, packidx);
589 return protect_require_yca(&ref_update->new_id,
590 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
594 static const struct got_error *
595 protect_branch(const char *refname, struct got_pack *pack,
596 struct got_packidx *packidx, struct gotd_ref_update *ref_update)
598 if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
601 /* Always allow new branches to be created. */
602 if (ref_update->ref_is_new) {
603 return verify_object_type(&ref_update->new_id,
604 GOT_OBJ_TYPE_COMMIT, pack, packidx);
607 return protect_require_yca(&ref_update->new_id,
608 be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
612 static const struct got_error *
613 recv_ref_update(struct imsg *imsg)
615 static const char zero_id[SHA1_DIGEST_LENGTH];
616 const struct got_error *err = NULL;
617 struct repo_write_client *client = &repo_write_client;
618 struct gotd_imsg_ref_update iref;
620 char *refname = NULL;
621 struct got_reference *ref = NULL;
622 struct got_object_id *id = NULL;
624 struct gotd_ref_update *ref_update = NULL;
626 log_debug("ref-update received");
628 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
629 if (datalen < sizeof(iref))
630 return got_error(GOT_ERR_PRIVSEP_LEN);
631 memcpy(&iref, imsg->data, sizeof(iref));
632 if (datalen != sizeof(iref) + iref.name_len)
633 return got_error(GOT_ERR_PRIVSEP_LEN);
635 imsg_init(&ibuf, client->fd);
637 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
639 return got_error_from_errno("strndup");
641 ref_update = calloc(1, sizeof(*ref_update));
642 if (ref_update == NULL) {
643 err = got_error_from_errno("malloc");
647 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
648 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
650 err = got_ref_open(&ref, repo_write.repo, refname, 0);
652 if (err->code != GOT_ERR_NOT_REF)
654 if (memcmp(ref_update->new_id.sha1,
655 zero_id, sizeof(zero_id)) == 0) {
656 err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
660 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
663 ref_update->ref_is_new = 1;
666 if (got_ref_is_symbolic(ref)) {
667 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
668 "'%s' is a symbolic reference and cannot "
669 "be updated", got_ref_get_name(ref));
672 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
673 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
674 "%s: does not begin with 'refs/'",
675 got_ref_get_name(ref));
679 err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
682 err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
686 if (!ref_update->ref_is_new) {
688 * Ensure the client's idea of this update is still valid.
689 * At this point we can only return an error, to prevent
690 * the client from uploading a pack file which will likely
691 * have to be discarded.
693 err = got_ref_resolve(&id, repo_write.repo, ref);
697 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
698 err = got_error_fmt(GOT_ERR_REF_BUSY,
699 "%s has been modified by someone else "
700 "while transaction was in progress",
701 got_ref_get_name(ref));
706 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
709 ref_update->ref = ref;
710 if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
711 ref_update->delete_ref = 1;
714 STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
715 client->nref_updates++;
727 static const struct got_error *
728 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
729 uint32_t nobj_loose, uint32_t nobj_resolved)
731 int p_indexed = 0, p_resolved = 0;
732 int nobj_delta = nobj_total - nobj_loose;
735 p_indexed = (nobj_indexed * 100) / nobj_total;
738 p_resolved = (nobj_resolved * 100) / nobj_delta;
740 if (p_resolved > 0) {
741 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
742 nobj_total, p_indexed, nobj_delta, p_resolved);
744 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
749 static const struct got_error *
750 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
752 const struct got_error *err = NULL;
753 uint8_t readahead[65536];
756 err = got_poll_read_full(infd, &have,
757 readahead, sizeof(readahead), minsize);
761 err = buf_append(&newlen, buf, readahead, have);
767 static const struct got_error *
768 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
769 off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
771 const struct got_error *err = NULL;
776 off_t obj_offset = *outsize;
779 /* We do not support size values which don't fit in 64 bit. */
781 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
782 "packfile offset %lld", (long long)obj_offset);
784 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
785 err = read_more_pack_stream(infd, buf,
791 sizebuf[i] = buf_getc(buf, *buf_pos);
792 *buf_pos += sizeof(sizebuf[i]);
795 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
796 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
797 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
799 size_t shift = 4 + 7 * (i - 1);
800 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
804 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
806 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
816 static const struct got_error *
817 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
818 struct got_hash *ctx)
820 const struct got_error *err = NULL;
821 size_t remain = buf_len(buf) - *buf_pos;
823 if (remain < SHA1_DIGEST_LENGTH) {
824 err = read_more_pack_stream(infd, buf,
825 SHA1_DIGEST_LENGTH - remain);
830 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
831 SHA1_DIGEST_LENGTH, ctx);
835 *buf_pos += SHA1_DIGEST_LENGTH;
839 static const struct got_error *
840 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
841 struct got_hash *ctx)
843 const struct got_error *err = NULL;
847 off_t obj_offset = *outsize;
850 /* We do not support offset values which don't fit in 64 bit. */
852 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
853 "packfile offset %lld", (long long)obj_offset);
855 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
856 err = read_more_pack_stream(infd, buf,
862 offbuf[i] = buf_getc(buf, *buf_pos);
863 *buf_pos += sizeof(offbuf[i]);
866 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
870 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
873 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
875 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
876 return got_error(GOT_ERR_PACK_OFFSET);
878 err = got_pack_hwrite(outfd, offbuf, i, ctx);
886 static const struct got_error *
887 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
888 struct got_hash *ctx)
890 const struct got_error *err = NULL;
894 size_t consumed_total = 0;
895 off_t zstream_offset = *outsize;
897 memset(&z, 0, sizeof(z));
901 zret = inflateInit(&z);
904 return got_error_from_errno("inflateInit");
905 if (zret == Z_MEM_ERROR) {
907 return got_error_from_errno("inflateInit");
909 return got_error_msg(GOT_ERR_DECOMPRESSION,
910 "inflateInit failed");
913 while (zret != Z_STREAM_END) {
914 size_t last_total_in, consumed;
917 * Decompress into the void. Object data will be parsed
918 * later, when the pack file is indexed. For now, we just
919 * want to locate the end of the compressed stream.
921 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
922 last_total_in = z.total_in;
923 z.next_in = buf_get(buf) + *buf_pos;
924 z.avail_in = buf_len(buf) - *buf_pos;
925 z.next_out = voidbuf;
926 z.avail_out = sizeof(voidbuf);
928 zret = inflate(&z, Z_SYNC_FLUSH);
929 if (zret != Z_OK && zret != Z_BUF_ERROR &&
930 zret != Z_STREAM_END) {
931 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
932 "packfile offset %lld",
933 (long long)zstream_offset);
936 consumed = z.total_in - last_total_in;
938 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
943 err = buf_discard(buf, *buf_pos + consumed);
948 consumed_total += consumed;
951 if (zret != Z_STREAM_END) {
952 err = read_more_pack_stream(infd, buf, 1);
959 *outsize += consumed_total;
965 static const struct got_error *
966 validate_object_type(int obj_type)
969 case GOT_OBJ_TYPE_BLOB:
970 case GOT_OBJ_TYPE_COMMIT:
971 case GOT_OBJ_TYPE_TREE:
972 case GOT_OBJ_TYPE_TAG:
973 case GOT_OBJ_TYPE_REF_DELTA:
974 case GOT_OBJ_TYPE_OFFSET_DELTA:
980 return got_error(GOT_ERR_OBJ_TYPE);
983 static const struct got_error *
984 ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
986 const struct got_error *err = NULL;
987 struct gotd_ref_update *ref_update;
988 struct got_object *obj;
990 STAILQ_FOREACH(ref_update, ref_updates, entry) {
991 err = got_object_open(&obj, repo_write.repo,
992 &ref_update->new_id);
995 got_object_close(obj);
1001 static const struct got_error *
1002 recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
1003 int infd, int outfd)
1005 const struct got_error *err;
1006 struct repo_write_client *client = &repo_write_client;
1007 struct got_packfile_hdr hdr;
1010 struct got_hash ctx;
1011 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1012 char hex[SHA1_DIGEST_STRING_LENGTH];
1014 size_t buf_pos = 0, remain;
1020 /* if only deleting references there's nothing to read */
1021 if (client->nref_updates == client->nref_del)
1024 got_hash_init(&ctx, GOT_HASH_SHA1);
1026 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1029 if (have != sizeof(hdr))
1030 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1033 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1034 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1035 "bad packfile signature");
1036 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1037 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1038 "bad packfile version");
1040 *nobj = be32toh(hdr.nobjects);
1043 * Clients which are creating new references only
1044 * will send us an empty pack file.
1046 if (client->nref_updates > 0 &&
1047 client->nref_updates == client->nref_new)
1051 * Clients which only move existing refs will send us an empty
1052 * pack file. All referenced objects must exist locally.
1054 err = ensure_all_objects_exist_locally(&client->ref_updates);
1056 if (err->code != GOT_ERR_NO_OBJ)
1058 return got_error_msg(GOT_ERR_BAD_PACKFILE,
1059 "bad packfile with zero objects");
1062 client->nref_move = client->nref_updates;
1066 log_debug("expecting %d objects", *nobj);
1068 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1072 err = buf_alloc(&buf, 65536);
1076 while (nhave != *nobj) {
1080 err = copy_object_type_and_size(&obj_type, &obj_size,
1081 infd, outfd, outsize, buf, &buf_pos, &ctx);
1085 err = validate_object_type(obj_type);
1089 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1090 err = copy_ref_delta(infd, outfd, outsize,
1091 buf, &buf_pos, &ctx);
1094 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1095 err = copy_offset_delta(infd, outfd, outsize,
1096 buf, &buf_pos, &ctx);
1101 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1108 log_debug("received %u objects", *nobj);
1110 got_hash_final(&ctx, expected_sha1);
1112 remain = buf_len(buf) - buf_pos;
1113 if (remain < SHA1_DIGEST_LENGTH) {
1114 err = read_more_pack_stream(infd, buf,
1115 SHA1_DIGEST_LENGTH - remain);
1120 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1121 log_debug("expect SHA1: %s", hex);
1122 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1123 log_debug("actual SHA1: %s", hex);
1125 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1126 SHA1_DIGEST_LENGTH) != 0) {
1127 err = got_error(GOT_ERR_PACKFILE_CSUM);
1131 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1133 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1135 err = got_error_from_errno("write");
1138 if (w != SHA1_DIGEST_LENGTH) {
1139 err = got_error(GOT_ERR_IO);
1143 *outsize += SHA1_DIGEST_LENGTH;
1145 if (fsync(outfd) == -1) {
1146 err = got_error_from_errno("fsync");
1149 if (lseek(outfd, 0L, SEEK_SET) == -1) {
1150 err = got_error_from_errno("lseek");
1158 static const struct got_error *
1159 report_pack_status(const struct got_error *unpack_err)
1161 const struct got_error *err = NULL;
1162 struct repo_write_client *client = &repo_write_client;
1163 struct gotd_imsg_packfile_status istatus;
1165 struct imsgbuf ibuf;
1166 const char *unpack_ok = "unpack ok\n";
1169 imsg_init(&ibuf, client->fd);
1172 istatus.reason_len = strlen(unpack_err->msg);
1174 istatus.reason_len = strlen(unpack_ok);
1176 len = sizeof(istatus) + istatus.reason_len;
1177 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1178 repo_write.pid, len);
1180 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1184 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1185 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1189 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1190 istatus.reason_len) == -1) {
1191 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1195 imsg_close(&ibuf, wbuf);
1197 err = gotd_imsg_flush(&ibuf);
1203 static const struct got_error *
1204 recv_packfile(int *have_packfile, struct imsg *imsg)
1206 const struct got_error *err = NULL, *unpack_err;
1207 struct repo_write_client *client = &repo_write_client;
1208 struct gotd_imsg_recv_packfile ireq;
1209 FILE *tempfiles[3] = { NULL, NULL, NULL };
1210 struct repo_tempfile {
1213 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1216 struct imsgbuf ibuf;
1217 struct got_ratelimit rl;
1218 struct got_pack *pack = NULL;
1219 off_t pack_filesize = 0;
1222 log_debug("packfile request received");
1225 got_ratelimit_init(&rl, 2, 0);
1227 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1228 if (datalen != sizeof(ireq))
1229 return got_error(GOT_ERR_PRIVSEP_LEN);
1230 memcpy(&ireq, imsg->data, sizeof(ireq));
1232 if (client->pack_pipe == -1 || client->packidx_fd == -1)
1233 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1235 imsg_init(&ibuf, client->fd);
1237 pack = &client->pack;
1238 memset(pack, 0, sizeof(*pack));
1239 pack->fd = imsg_get_fd(imsg);
1241 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1243 err = got_delta_cache_alloc(&pack->delta_cache);
1247 for (i = 0; i < nitems(repo_tempfiles); i++) {
1248 struct repo_tempfile *t = &repo_tempfiles[i];
1249 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
1254 for (i = 0; i < nitems(tempfiles); i++) {
1258 fd = dup(repo_tempfiles[i].fd);
1260 err = got_error_from_errno("dup");
1263 f = fdopen(fd, "w+");
1265 err = got_error_from_errno("fdopen");
1272 err = gotd_imsg_flush(&ibuf);
1276 log_debug("receiving pack data");
1277 unpack_err = recv_packdata(&pack_filesize, &nobj,
1278 client->pack_sha1, client->pack_pipe, pack->fd);
1279 if (ireq.report_status) {
1280 err = report_pack_status(unpack_err);
1282 /* Git clients hang up after sending the pack file. */
1283 if (err->code == GOT_ERR_EOF)
1292 log_debug("pack data received");
1295 * Clients which are creating new references only will
1296 * send us an empty pack file.
1299 pack_filesize == sizeof(struct got_packfile_hdr) &&
1300 client->nref_updates > 0 &&
1301 client->nref_updates == client->nref_new)
1305 * Clients which are deleting references only will send
1309 client->nref_del > 0 &&
1310 client->nref_updates == client->nref_del)
1314 * Clients which only move existing refs will send us an empty
1315 * pack file. All referenced objects must exist locally.
1318 pack_filesize == sizeof(struct got_packfile_hdr) &&
1319 client->nref_move > 0 &&
1320 client->nref_updates == client->nref_move)
1323 pack->filesize = pack_filesize;
1326 log_debug("begin indexing pack (%lld bytes in size)",
1327 (long long)pack->filesize);
1328 err = got_pack_index(pack, client->packidx_fd,
1329 tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1330 pack_index_progress, NULL, &rl);
1333 log_debug("done indexing pack");
1335 if (fsync(client->packidx_fd) == -1) {
1336 err = got_error_from_errno("fsync");
1339 if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1340 err = got_error_from_errno("lseek");
1342 if (close(client->pack_pipe) == -1 && err == NULL)
1343 err = got_error_from_errno("close");
1344 client->pack_pipe = -1;
1345 for (i = 0; i < nitems(repo_tempfiles); i++) {
1346 struct repo_tempfile *t = &repo_tempfiles[i];
1348 got_repo_temp_fds_put(t->idx, repo_write.repo);
1350 for (i = 0; i < nitems(tempfiles); i++) {
1351 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1352 err = got_error_from_errno("fclose");
1355 got_pack_close(pack);
1360 static const struct got_error *
1361 verify_packfile(void)
1363 const struct got_error *err = NULL, *close_err;
1364 struct repo_write_client *client = &repo_write_client;
1365 struct gotd_ref_update *ref_update;
1366 struct got_packidx *packidx = NULL;
1368 char *id_str = NULL;
1369 struct got_object *obj = NULL;
1370 struct got_pathlist_entry *pe;
1371 char hex[SHA1_DIGEST_STRING_LENGTH];
1373 if (STAILQ_EMPTY(&client->ref_updates)) {
1374 return got_error_msg(GOT_ERR_BAD_REQUEST,
1375 "cannot verify pack file without any ref-updates");
1378 if (client->pack.fd == -1) {
1379 return got_error_msg(GOT_ERR_BAD_REQUEST,
1380 "invalid pack file handle during pack verification");
1382 if (client->packidx_fd == -1) {
1383 return got_error_msg(GOT_ERR_BAD_REQUEST,
1384 "invalid pack index handle during pack verification");
1387 if (fstat(client->packidx_fd, &sb) == -1)
1388 return got_error_from_errno("pack index fstat");
1390 packidx = malloc(sizeof(*packidx));
1391 memset(packidx, 0, sizeof(*packidx));
1392 packidx->fd = client->packidx_fd;
1393 client->packidx_fd = -1;
1394 packidx->len = sb.st_size;
1396 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1400 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1401 if (ref_update->delete_ref)
1404 TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1405 err = protect_tag_namespace(pe->path, &client->pack,
1406 packidx, ref_update);
1412 * Objects which already exist in our repository need
1413 * not be present in the pack file.
1415 err = got_object_open(&obj, repo_write.repo,
1416 &ref_update->new_id);
1417 if (err && err->code != GOT_ERR_NO_OBJ)
1421 got_object_close(obj);
1424 int idx = got_packidx_get_object_idx(packidx,
1425 &ref_update->new_id);
1427 got_sha1_digest_to_str(ref_update->new_id.sha1,
1429 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1430 "object %s is missing from pack file",
1436 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1438 err = protect_branch_namespace(pe->path,
1439 &client->pack, packidx, ref_update);
1443 TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1444 err = protect_branch(pe->path, &client->pack,
1445 packidx, ref_update);
1452 close_err = got_packidx_close(packidx);
1453 if (close_err && err == NULL)
1457 got_object_close(obj);
1461 static const struct got_error *
1462 protect_refs_from_deletion(void)
1464 const struct got_error *err = NULL;
1465 struct repo_write_client *client = &repo_write_client;
1466 struct gotd_ref_update *ref_update;
1467 struct got_pathlist_entry *pe;
1468 const char *refname;
1470 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1471 if (!ref_update->delete_ref)
1474 refname = got_ref_get_name(ref_update->ref);
1476 TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1477 err = protect_ref_namespace(refname, pe->path);
1482 TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1484 err = protect_ref_namespace(refname, pe->path);
1489 TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1490 if (strcmp(refname, pe->path) == 0) {
1491 return got_error_fmt(GOT_ERR_REF_PROTECTED,
1500 static const struct got_error *
1501 install_packfile(struct gotd_imsgev *iev)
1503 struct repo_write_client *client = &repo_write_client;
1504 struct gotd_imsg_packfile_install inst;
1507 memset(&inst, 0, sizeof(inst));
1508 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1510 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1511 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1513 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1518 static const struct got_error *
1519 send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1521 struct gotd_imsg_ref_updates_start istart;
1524 memset(&istart, 0, sizeof(istart));
1525 istart.nref_updates = nref_updates;
1527 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1528 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1530 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1536 static const struct got_error *
1537 send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1539 struct gotd_imsg_ref_update iref;
1540 const char *refname = got_ref_get_name(ref_update->ref);
1544 memset(&iref, 0, sizeof(iref));
1545 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1546 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1547 iref.ref_is_new = ref_update->ref_is_new;
1548 iref.delete_ref = ref_update->delete_ref;
1549 iref.name_len = strlen(refname);
1551 len = sizeof(iref) + iref.name_len;
1552 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1553 repo_write.pid, len);
1555 return got_error_from_errno("imsg_create REF_UPDATE");
1557 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1558 return got_error_from_errno("imsg_add REF_UPDATE");
1559 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1560 return got_error_from_errno("imsg_add REF_UPDATE");
1562 imsg_close(&iev->ibuf, wbuf);
1564 gotd_imsg_event_add(iev);
1568 static const struct got_error *
1569 update_refs(struct gotd_imsgev *iev)
1571 const struct got_error *err = NULL;
1572 struct repo_write_client *client = &repo_write_client;
1573 struct gotd_ref_update *ref_update;
1575 err = send_ref_updates_start(client->nref_updates, iev);
1579 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1580 err = send_ref_update(ref_update, iev);
1588 static const struct got_error *
1589 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1591 struct repo_write_client *client = &repo_write_client;
1594 log_debug("receiving pack pipe descriptor");
1596 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1598 return got_error(GOT_ERR_PRIVSEP_LEN);
1600 if (client->pack_pipe != -1)
1601 return got_error(GOT_ERR_PRIVSEP_MSG);
1603 client->pack_pipe = imsg_get_fd(imsg);
1604 if (client->pack_pipe == -1)
1605 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1610 static const struct got_error *
1611 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1613 struct repo_write_client *client = &repo_write_client;
1616 log_debug("receiving pack index output file");
1618 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1620 return got_error(GOT_ERR_PRIVSEP_LEN);
1622 if (client->packidx_fd != -1)
1623 return got_error(GOT_ERR_PRIVSEP_MSG);
1625 client->packidx_fd = imsg_get_fd(imsg);
1626 if (client->packidx_fd == -1)
1627 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1633 get_datestr(time_t *time, char *datebuf)
1635 struct tm mytm, *tm;
1638 tm = gmtime_r(time, &mytm);
1641 s = asctime_r(tm, datebuf);
1644 p = strchr(s, '\n');
1650 static const struct got_error *
1651 notify_removed_ref(const char *refname, uint8_t *sha1,
1652 struct gotd_imsgev *iev, int fd)
1654 const struct got_error *err;
1655 struct got_object_id id;
1658 memset(&id, 0, sizeof(id));
1659 memcpy(id.sha1, sha1, sizeof(id.sha1));
1661 err = got_object_id_str(&id_str, &id);
1665 dprintf(fd, "Removed %s: %s\n", refname, id_str);
1671 format_author(char *author)
1675 smallerthan = strchr(author, '<');
1676 if (smallerthan && smallerthan[1] != '\0')
1677 author = smallerthan + 1;
1678 author[strcspn(author, "@>")] = '\0';
1683 static const struct got_error *
1684 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
1685 struct got_repository *repo, int fd)
1687 const struct got_error *err = NULL;
1688 char *id_str = NULL, *logmsg0 = NULL;
1690 char *committer = NULL, *author = NULL;
1691 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1693 time_t committer_time;
1695 err = got_object_id_str(&id_str, id);
1699 committer_time = got_object_commit_get_committer_time(commit);
1700 if (gmtime_r(&committer_time, &tm) == NULL) {
1701 err = got_error_from_errno("gmtime_r");
1704 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
1705 err = got_error(GOT_ERR_NO_SPACE);
1709 err = got_object_commit_get_logmsg(&logmsg0, commit);
1714 while (isspace((unsigned char)s[0]))
1717 nl = strchr(s, '\n');
1722 if (strcmp(got_object_commit_get_author(commit),
1723 got_object_commit_get_committer(commit)) != 0) {
1724 author = strdup(got_object_commit_get_author(commit));
1725 if (author == NULL) {
1726 err = got_error_from_errno("strdup");
1729 dprintf(fd, "%s%.7s %.8s %s\n", datebuf, id_str,
1730 format_author(author), s);
1732 committer = strdup(got_object_commit_get_committer(commit));
1733 if (committer == NULL) {
1734 err = got_error_from_errno("strdup");
1737 dprintf(fd, "%s%.7s %.8s %s\n", datebuf, id_str,
1738 format_author(committer), s);
1741 if (fsync(fd) == -1 && err == NULL)
1742 err = got_error_from_errno("fsync");
1751 static const struct got_error *
1752 print_diffstat(struct got_diffstat_cb_arg *dsa, int fd)
1754 struct got_pathlist_entry *pe;
1756 TAILQ_FOREACH(pe, dsa->paths, entry) {
1757 struct got_diff_changed_path *cp = pe->data;
1758 int pad = dsa->max_path_len - pe->path_len + 1;
1760 dprintf(fd, " %c %s%*c | %*d+ %*d-\n", cp->status,
1761 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
1762 dsa->rm_cols + 1, cp->rm);
1765 "\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
1766 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
1767 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
1772 static const struct got_error *
1773 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1774 struct got_repository *repo, struct got_pathlist_head *changed_paths,
1775 struct got_diffstat_cb_arg *diffstat, int fd)
1777 const struct got_error *err = NULL;
1778 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1780 time_t committer_time;
1781 const char *author, *committer;
1783 err = got_object_id_str(&id_str, id);
1787 dprintf(fd, "commit %s\n", id_str);
1790 dprintf(fd, "from: %s\n", got_object_commit_get_author(commit));
1791 author = got_object_commit_get_author(commit);
1792 committer = got_object_commit_get_committer(commit);
1793 if (strcmp(author, committer) != 0)
1794 dprintf(fd, "via: %s\n", committer);
1795 committer_time = got_object_commit_get_committer_time(commit);
1796 datestr = get_datestr(&committer_time, datebuf);
1798 dprintf(fd, "date: %s UTC\n", datestr);
1799 if (got_object_commit_get_nparents(commit) > 1) {
1800 const struct got_object_id_queue *parent_ids;
1801 struct got_object_qid *qid;
1803 parent_ids = got_object_commit_get_parent_ids(commit);
1804 STAILQ_FOREACH(qid, parent_ids, entry) {
1805 err = got_object_id_str(&id_str, &qid->id);
1808 dprintf(fd, "parent %d: %s\n", n++, id_str);
1814 err = got_object_commit_get_logmsg(&logmsg0, commit);
1820 line = strsep(&logmsg, "\n");
1822 dprintf(fd, " %s\n", line);
1826 err = print_diffstat(diffstat, fd);
1830 if (fsync(fd) == -1 && err == NULL)
1831 err = got_error_from_errno("fsync");
1837 static const struct got_error *
1838 get_changed_paths(struct got_pathlist_head *paths,
1839 struct got_commit_object *commit, struct got_repository *repo,
1840 struct got_diffstat_cb_arg *dsa)
1842 const struct got_error *err = NULL;
1843 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1844 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1845 struct got_object_qid *qid;
1846 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
1847 FILE *f1 = repo_write.diff.f1, *f2 = repo_write.diff.f2;
1848 int fd1 = repo_write.diff.fd1, fd2 = repo_write.diff.fd2;
1851 cb = got_diff_tree_compute_diffstat;
1853 err = got_opentemp_truncate(f1);
1856 err = got_opentemp_truncate(f2);
1859 err = got_opentemp_truncatefd(fd1);
1862 err = got_opentemp_truncatefd(fd2);
1866 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1868 struct got_commit_object *pcommit;
1869 err = got_object_open_as_commit(&pcommit, repo,
1874 tree_id1 = got_object_id_dup(
1875 got_object_commit_get_tree_id(pcommit));
1876 if (tree_id1 == NULL) {
1877 got_object_commit_close(pcommit);
1878 return got_error_from_errno("got_object_id_dup");
1880 got_object_commit_close(pcommit);
1885 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1890 tree_id2 = got_object_commit_get_tree_id(commit);
1891 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1895 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
1896 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
1899 got_object_tree_close(tree1);
1901 got_object_tree_close(tree2);
1906 static const struct got_error *
1907 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
1908 struct got_repository *repo, int fd)
1910 const struct got_error *err;
1911 struct got_commit_graph *graph;
1912 struct got_object_id_queue reversed_commits;
1913 struct got_object_qid *qid;
1914 struct got_commit_object *commit = NULL;
1915 struct got_pathlist_head changed_paths;
1917 const int shortlog_threshold = 50;
1919 STAILQ_INIT(&reversed_commits);
1920 TAILQ_INIT(&changed_paths);
1922 /* XXX first-parent only for now */
1923 err = got_commit_graph_open(&graph, "/", 1);
1926 err = got_commit_graph_iter_start(graph, root_id, repo,
1927 check_cancelled, NULL);
1931 struct got_object_id id;
1933 err = got_commit_graph_iter_next(&id, graph, repo,
1934 check_cancelled, NULL);
1936 if (err->code == GOT_ERR_ITER_COMPLETED)
1941 err = got_object_open_as_commit(&commit, repo, &id);
1945 if (end_id && got_object_id_cmp(&id, end_id) == 0)
1948 err = got_object_qid_alloc(&qid, &id);
1952 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
1954 got_object_commit_close(commit);
1960 STAILQ_FOREACH(qid, &reversed_commits, entry) {
1961 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
1962 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
1964 err = got_object_open_as_commit(&commit, repo, &qid->id);
1968 if (ncommits > shortlog_threshold) {
1969 err = print_commit_oneline(commit, &qid->id,
1974 err = get_changed_paths(&changed_paths, commit,
1978 err = print_commit(commit, &qid->id, repo,
1979 &changed_paths, &dsa, fd);
1981 got_object_commit_close(commit);
1983 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1987 got_object_commit_close(commit);
1988 while (!STAILQ_EMPTY(&reversed_commits)) {
1989 qid = STAILQ_FIRST(&reversed_commits);
1990 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
1991 got_object_qid_free(qid);
1993 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1994 got_commit_graph_close(graph);
1998 static const struct got_error *
1999 print_tag(struct got_object_id *id,
2000 const char *refname, struct got_repository *repo, int fd)
2002 const struct got_error *err = NULL;
2003 struct got_tag_object *tag = NULL;
2004 const char *tagger = NULL;
2005 char *id_str = NULL, *tagmsg0 = NULL, *tagmsg, *line, *datestr;
2009 err = got_object_open_as_tag(&tag, repo, id);
2013 tagger = got_object_tag_get_tagger(tag);
2014 tagger_time = got_object_tag_get_tagger_time(tag);
2015 err = got_object_id_str(&id_str,
2016 got_object_tag_get_object_id(tag));
2020 dprintf(fd, "tag %s\n", refname);
2021 dprintf(fd, "from: %s\n", tagger);
2022 datestr = get_datestr(&tagger_time, datebuf);
2024 dprintf(fd, "date: %s UTC\n", datestr);
2026 switch (got_object_tag_get_object_type(tag)) {
2027 case GOT_OBJ_TYPE_BLOB:
2028 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
2030 case GOT_OBJ_TYPE_TREE:
2031 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
2033 case GOT_OBJ_TYPE_COMMIT:
2034 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
2036 case GOT_OBJ_TYPE_TAG:
2037 dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
2043 tagmsg0 = strdup(got_object_tag_get_message(tag));
2044 if (tagmsg0 == NULL) {
2045 err = got_error_from_errno("strdup");
2050 line = strsep(&tagmsg, "\n");
2052 dprintf(fd, " %s\n", line);
2057 got_object_tag_close(tag);
2062 static const struct got_error *
2063 notify_changed_ref(const char *refname, uint8_t *old_sha1,
2064 uint8_t *new_sha1, struct gotd_imsgev *iev, int fd)
2066 const struct got_error *err;
2067 struct got_object_id old_id, new_id;
2068 int old_obj_type, new_obj_type;
2070 char *new_id_str = NULL;
2072 memset(&old_id, 0, sizeof(old_id));
2073 memcpy(old_id.sha1, old_sha1, sizeof(old_id.sha1));
2074 memset(&new_id, 0, sizeof(new_id));
2075 memcpy(new_id.sha1, new_sha1, sizeof(new_id.sha1));
2077 err = got_object_get_type(&old_obj_type, repo_write.repo, &old_id);
2081 err = got_object_get_type(&new_obj_type, repo_write.repo, &new_id);
2085 switch (new_obj_type) {
2086 case GOT_OBJ_TYPE_COMMIT:
2087 err = print_commits(&new_id,
2088 old_obj_type == GOT_OBJ_TYPE_COMMIT ? &old_id : NULL,
2089 repo_write.repo, fd);
2091 case GOT_OBJ_TYPE_TAG:
2092 err = print_tag(&new_id, refname, repo_write.repo, fd);
2095 err = got_object_type_label(&label, new_obj_type);
2098 err = got_object_id_str(&new_id_str, &new_id);
2101 dprintf(fd, "%s: %s object %s\n", refname, label, new_id_str);
2109 static const struct got_error *
2110 notify_created_ref(const char *refname, uint8_t *sha1,
2111 struct gotd_imsgev *iev, int fd)
2113 const struct got_error *err;
2114 struct got_object_id id;
2117 memset(&id, 0, sizeof(id));
2118 memcpy(id.sha1, sha1, sizeof(id.sha1));
2120 err = got_object_get_type(&obj_type, repo_write.repo, &id);
2124 if (obj_type == GOT_OBJ_TYPE_TAG)
2125 return print_tag(&id, refname, repo_write.repo, fd);
2127 return print_commits(&id, NULL, repo_write.repo, fd);
2130 static const struct got_error *
2131 render_notification(struct imsg *imsg, struct gotd_imsgev *iev)
2133 const struct got_error *err = NULL;
2134 struct gotd_imsg_notification_content ireq;
2135 size_t datalen, len;
2140 fd = imsg_get_fd(imsg);
2142 return got_error(GOT_ERR_PRIVSEP_NO_FD);
2144 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2145 if (datalen < sizeof(ireq))
2146 return got_error(GOT_ERR_PRIVSEP_LEN);
2148 memcpy(&ireq, imsg->data, sizeof(ireq));
2150 if (datalen != sizeof(ireq) + ireq.refname_len)
2151 return got_error(GOT_ERR_PRIVSEP_LEN);
2153 refname = strndup(imsg->data + sizeof(ireq), ireq.refname_len);
2154 if (refname == NULL)
2155 return got_error_from_errno("strndup");
2157 switch (ireq.action) {
2158 case GOTD_NOTIF_ACTION_CREATED:
2159 err = notify_created_ref(refname, ireq.new_id, iev, fd);
2161 case GOTD_NOTIF_ACTION_REMOVED:
2162 err = notify_removed_ref(refname, ireq.old_id, iev, fd);
2164 case GOTD_NOTIF_ACTION_CHANGED:
2165 err = notify_changed_ref(refname, ireq.old_id, ireq.new_id,
2170 if (fsync(fd) == -1) {
2171 err = got_error_from_errno("fsync");
2175 len = sizeof(ireq) + ireq.refname_len;
2176 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY, PROC_REPO_WRITE,
2177 repo_write.pid, len);
2179 err = got_error_from_errno("imsg_create REF");
2182 if (imsg_add(wbuf, &ireq, sizeof(ireq)) == -1) {
2183 err = got_error_from_errno("imsg_add NOTIFY");
2186 if (imsg_add(wbuf, refname, ireq.refname_len) == -1) {
2187 err = got_error_from_errno("imsg_add NOTIFY");
2191 imsg_close(&iev->ibuf, wbuf);
2192 gotd_imsg_event_add(iev);
2195 if (close(fd) == -1 && err == NULL)
2196 err = got_error_from_errno("close");
2201 repo_write_dispatch_session(int fd, short event, void *arg)
2203 const struct got_error *err = NULL;
2204 struct gotd_imsgev *iev = arg;
2205 struct imsgbuf *ibuf = &iev->ibuf;
2207 struct repo_write_client *client = &repo_write_client;
2209 int shut = 0, have_packfile = 0;
2211 if (event & EV_READ) {
2212 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2213 fatal("imsg_read error");
2214 if (n == 0) /* Connection closed. */
2218 if (event & EV_WRITE) {
2219 n = msgbuf_write(&ibuf->w);
2220 if (n == -1 && errno != EAGAIN)
2221 fatal("msgbuf_write");
2222 if (n == 0) /* Connection closed. */
2227 if ((n = imsg_get(ibuf, &imsg)) == -1)
2228 fatal("%s: imsg_get error", __func__);
2229 if (n == 0) /* No more messages. */
2232 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
2233 !repo_write.refs_listed) {
2234 err = got_error(GOT_ERR_PRIVSEP_MSG);
2238 switch (imsg.hdr.type) {
2239 case GOTD_IMSG_LIST_REFS_INTERNAL:
2240 err = list_refs(&imsg);
2242 log_warnx("ls-refs: %s", err->msg);
2244 case GOTD_IMSG_REF_UPDATE:
2245 err = recv_ref_update(&imsg);
2247 log_warnx("ref-update: %s", err->msg);
2249 case GOTD_IMSG_PACKFILE_PIPE:
2250 err = receive_pack_pipe(&imsg, iev);
2252 log_warnx("receiving pack pipe: %s", err->msg);
2256 case GOTD_IMSG_PACKIDX_FILE:
2257 err = receive_pack_idx(&imsg, iev);
2259 log_warnx("receiving pack index: %s",
2264 case GOTD_IMSG_RECV_PACKFILE:
2265 err = protect_refs_from_deletion();
2268 err = recv_packfile(&have_packfile, &imsg);
2270 log_warnx("receive packfile: %s", err->msg);
2273 if (have_packfile) {
2274 err = verify_packfile();
2276 log_warnx("verify packfile: %s",
2280 err = install_packfile(iev);
2282 log_warnx("install packfile: %s",
2287 * Ensure we re-read the pack index list
2290 repo_write.repo->pack_path_mtime.tv_sec = 0;
2291 repo_write.repo->pack_path_mtime.tv_nsec = 0;
2293 err = update_refs(iev);
2295 log_warnx("update refs: %s", err->msg);
2298 case GOTD_IMSG_NOTIFY:
2299 err = render_notification(&imsg, iev);
2301 log_warnx("render notification: %s", err->msg);
2306 log_debug("unexpected imsg %d", imsg.hdr.type);
2313 if (!shut && check_cancelled(NULL) == NULL) {
2315 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2316 client->id, err) == -1) {
2317 log_warnx("could not send error to parent: %s",
2320 gotd_imsg_event_add(iev);
2322 /* This pipe is dead. Remove its event handler */
2323 event_del(&iev->ev);
2324 event_loopexit(NULL);
2328 static const struct got_error *
2329 recv_connect(struct imsg *imsg)
2331 struct gotd_imsgev *iev = &repo_write.session_iev;
2334 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2336 return got_error(GOT_ERR_PRIVSEP_LEN);
2338 if (repo_write.session_fd != -1)
2339 return got_error(GOT_ERR_PRIVSEP_MSG);
2341 repo_write.session_fd = imsg_get_fd(imsg);
2342 if (repo_write.session_fd == -1)
2343 return got_error(GOT_ERR_PRIVSEP_NO_FD);
2345 imsg_init(&iev->ibuf, repo_write.session_fd);
2346 iev->handler = repo_write_dispatch_session;
2347 iev->events = EV_READ;
2348 iev->handler_arg = NULL;
2349 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
2350 repo_write_dispatch_session, iev);
2351 gotd_imsg_event_add(iev);
2357 repo_write_dispatch(int fd, short event, void *arg)
2359 const struct got_error *err = NULL;
2360 struct gotd_imsgev *iev = arg;
2361 struct imsgbuf *ibuf = &iev->ibuf;
2365 struct repo_write_client *client = &repo_write_client;
2367 if (event & EV_READ) {
2368 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2369 fatal("imsg_read error");
2370 if (n == 0) /* Connection closed. */
2374 if (event & EV_WRITE) {
2375 n = msgbuf_write(&ibuf->w);
2376 if (n == -1 && errno != EAGAIN)
2377 fatal("msgbuf_write");
2378 if (n == 0) /* Connection closed. */
2382 while (err == NULL && check_cancelled(NULL) == NULL) {
2383 if ((n = imsg_get(ibuf, &imsg)) == -1)
2384 fatal("%s: imsg_get", __func__);
2385 if (n == 0) /* No more messages. */
2388 switch (imsg.hdr.type) {
2389 case GOTD_IMSG_CONNECT_REPO_CHILD:
2390 err = recv_connect(&imsg);
2393 log_debug("unexpected imsg %d", imsg.hdr.type);
2400 if (!shut && check_cancelled(NULL) == NULL) {
2402 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2403 client->id, err) == -1) {
2404 log_warnx("could not send error to parent: %s",
2407 gotd_imsg_event_add(iev);
2409 /* This pipe is dead. Remove its event handler */
2410 event_del(&iev->ev);
2411 event_loopexit(NULL);
2416 repo_write_main(const char *title, const char *repo_path,
2417 int *pack_fds, int *temp_fds,
2418 FILE *diff_f1, FILE *diff_f2, int diff_fd1, int diff_fd2,
2419 struct got_pathlist_head *protected_tag_namespaces,
2420 struct got_pathlist_head *protected_branch_namespaces,
2421 struct got_pathlist_head *protected_branches)
2423 const struct got_error *err = NULL;
2424 struct repo_write_client *client = &repo_write_client;
2425 struct gotd_imsgev iev;
2428 client->pack_pipe = -1;
2429 client->packidx_fd = -1;
2430 client->pack.fd = -1;
2432 repo_write.title = title;
2433 repo_write.pid = getpid();
2434 repo_write.pack_fds = pack_fds;
2435 repo_write.temp_fds = temp_fds;
2436 repo_write.session_fd = -1;
2437 repo_write.session_iev.ibuf.fd = -1;
2438 repo_write.protected_tag_namespaces = protected_tag_namespaces;
2439 repo_write.protected_branch_namespaces = protected_branch_namespaces;
2440 repo_write.protected_branches = protected_branches;
2441 repo_write.diff.f1 = diff_f1;
2442 repo_write.diff.f2 = diff_f2;
2443 repo_write.diff.fd1 = diff_fd1;
2444 repo_write.diff.fd2 = diff_fd2;
2446 STAILQ_INIT(&repo_write_client.ref_updates);
2448 err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
2451 if (!got_repo_is_bare(repo_write.repo)) {
2452 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
2453 "bare git repository required");
2457 got_repo_temp_fds_set(repo_write.repo, temp_fds);
2459 signal(SIGINT, catch_sigint);
2460 signal(SIGTERM, catch_sigterm);
2461 signal(SIGPIPE, SIG_IGN);
2462 signal(SIGHUP, SIG_IGN);
2464 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
2465 iev.handler = repo_write_dispatch;
2466 iev.events = EV_READ;
2467 iev.handler_arg = NULL;
2468 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
2469 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
2470 PROC_REPO_WRITE, -1, NULL, 0) == -1) {
2471 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
2477 if (fclose(diff_f1) == EOF && err == NULL)
2478 err = got_error_from_errno("fclose");
2479 if (fclose(diff_f2) == EOF && err == NULL)
2480 err = got_error_from_errno("fclose");
2481 if (close(diff_fd1) == -1 && err == NULL)
2482 err = got_error_from_errno("close");
2483 if (close(diff_fd2) == -1 && err == NULL)
2484 err = got_error_from_errno("close");
2486 log_warnx("%s: %s", title, err->msg);
2487 repo_write_shutdown();
2491 repo_write_shutdown(void)
2493 struct repo_write_client *client = &repo_write_client;
2494 struct gotd_ref_update *ref_update;
2496 log_debug("%s: shutting down", repo_write.title);
2498 while (!STAILQ_EMPTY(&client->ref_updates)) {
2499 ref_update = STAILQ_FIRST(&client->ref_updates);
2500 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
2501 got_ref_close(ref_update->ref);
2505 got_pack_close(&client->pack);
2506 if (client->fd != -1)
2508 if (client->pack_pipe != -1)
2509 close(client->pack_pipe);
2510 if (client->packidx_fd != -1)
2511 close(client->packidx_fd);
2513 if (repo_write.repo)
2514 got_repo_close(repo_write.repo);
2515 got_repo_pack_fds_close(repo_write.pack_fds);
2516 got_repo_temp_fds_close(repo_write.temp_fds);
2517 if (repo_write.session_fd != -1)
2518 close(repo_write.session_fd);