Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
7 3efd8e31 2022-10-23 thomas *
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
19 3efd8e31 2022-10-23 thomas #include <sys/types.h>
20 3efd8e31 2022-10-23 thomas
21 3efd8e31 2022-10-23 thomas #include <event.h>
22 3efd8e31 2022-10-23 thomas #include <errno.h>
23 3efd8e31 2022-10-23 thomas #include <imsg.h>
24 3efd8e31 2022-10-23 thomas #include <signal.h>
25 3efd8e31 2022-10-23 thomas #include <siphash.h>
26 3efd8e31 2022-10-23 thomas #include <stdio.h>
27 3efd8e31 2022-10-23 thomas #include <stdlib.h>
28 3efd8e31 2022-10-23 thomas #include <string.h>
29 3efd8e31 2022-10-23 thomas #include <limits.h>
30 3efd8e31 2022-10-23 thomas #include <poll.h>
31 3efd8e31 2022-10-23 thomas #include <unistd.h>
32 3efd8e31 2022-10-23 thomas #include <zlib.h>
33 3efd8e31 2022-10-23 thomas
34 3efd8e31 2022-10-23 thomas #include "buf.h"
35 3efd8e31 2022-10-23 thomas
36 3efd8e31 2022-10-23 thomas #include "got_error.h"
37 3efd8e31 2022-10-23 thomas #include "got_repository.h"
38 3efd8e31 2022-10-23 thomas #include "got_object.h"
39 3efd8e31 2022-10-23 thomas #include "got_reference.h"
40 3efd8e31 2022-10-23 thomas #include "got_path.h"
41 3efd8e31 2022-10-23 thomas
42 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
43 3efd8e31 2022-10-23 thomas #include "got_lib_delta_cache.h"
44 b16893ba 2023-02-24 thomas #include "got_lib_hash.h"
45 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
46 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
47 6d7eb4f7 2023-04-04 thomas #include "got_lib_object_idset.h"
48 6d7eb4f7 2023-04-04 thomas #include "got_lib_object_parse.h"
49 3efd8e31 2022-10-23 thomas #include "got_lib_ratelimit.h"
50 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
51 3efd8e31 2022-10-23 thomas #include "got_lib_pack_index.h"
52 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
53 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
54 3efd8e31 2022-10-23 thomas
55 3efd8e31 2022-10-23 thomas #include "log.h"
56 3efd8e31 2022-10-23 thomas #include "gotd.h"
57 3efd8e31 2022-10-23 thomas #include "repo_write.h"
58 3efd8e31 2022-10-23 thomas
59 3efd8e31 2022-10-23 thomas #ifndef nitems
60 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 3efd8e31 2022-10-23 thomas #endif
62 3efd8e31 2022-10-23 thomas
63 3efd8e31 2022-10-23 thomas static struct repo_write {
64 3efd8e31 2022-10-23 thomas pid_t pid;
65 3efd8e31 2022-10-23 thomas const char *title;
66 3efd8e31 2022-10-23 thomas struct got_repository *repo;
67 3efd8e31 2022-10-23 thomas int *pack_fds;
68 3efd8e31 2022-10-23 thomas int *temp_fds;
69 62ee7d94 2023-01-10 thomas int session_fd;
70 62ee7d94 2023-01-10 thomas struct gotd_imsgev session_iev;
71 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *protected_tag_namespaces;
72 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *protected_branch_namespaces;
73 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *protected_branches;
74 3efd8e31 2022-10-23 thomas } repo_write;
75 3efd8e31 2022-10-23 thomas
76 3efd8e31 2022-10-23 thomas struct gotd_ref_update {
77 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(gotd_ref_update) entry;
78 3efd8e31 2022-10-23 thomas struct got_reference *ref;
79 3efd8e31 2022-10-23 thomas int ref_is_new;
80 49563dfb 2023-01-28 thomas int delete_ref;
81 3efd8e31 2022-10-23 thomas struct got_object_id old_id;
82 3efd8e31 2022-10-23 thomas struct got_object_id new_id;
83 3efd8e31 2022-10-23 thomas };
84 3efd8e31 2022-10-23 thomas STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
85 3efd8e31 2022-10-23 thomas
86 9148c8a7 2023-01-02 thomas static struct repo_write_client {
87 3efd8e31 2022-10-23 thomas uint32_t id;
88 3efd8e31 2022-10-23 thomas int fd;
89 3efd8e31 2022-10-23 thomas int pack_pipe[2];
90 3efd8e31 2022-10-23 thomas struct got_pack pack;
91 3efd8e31 2022-10-23 thomas uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
92 3efd8e31 2022-10-23 thomas int packidx_fd;
93 3efd8e31 2022-10-23 thomas struct gotd_ref_updates ref_updates;
94 3efd8e31 2022-10-23 thomas int nref_updates;
95 49563dfb 2023-01-28 thomas int nref_del;
96 d98779cd 2023-01-19 thomas int nref_new;
97 f9542c24 2023-04-14 thomas int nref_move;
98 9148c8a7 2023-01-02 thomas } repo_write_client;
99 3efd8e31 2022-10-23 thomas
100 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigint_received;
101 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigterm_received;
102 3efd8e31 2022-10-23 thomas
103 3efd8e31 2022-10-23 thomas static void
104 3efd8e31 2022-10-23 thomas catch_sigint(int signo)
105 3efd8e31 2022-10-23 thomas {
106 3efd8e31 2022-10-23 thomas sigint_received = 1;
107 3efd8e31 2022-10-23 thomas }
108 3efd8e31 2022-10-23 thomas
109 3efd8e31 2022-10-23 thomas static void
110 3efd8e31 2022-10-23 thomas catch_sigterm(int signo)
111 3efd8e31 2022-10-23 thomas {
112 3efd8e31 2022-10-23 thomas sigterm_received = 1;
113 3efd8e31 2022-10-23 thomas }
114 3efd8e31 2022-10-23 thomas
115 3efd8e31 2022-10-23 thomas static const struct got_error *
116 3efd8e31 2022-10-23 thomas check_cancelled(void *arg)
117 3efd8e31 2022-10-23 thomas {
118 3efd8e31 2022-10-23 thomas if (sigint_received || sigterm_received)
119 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CANCELLED);
120 3efd8e31 2022-10-23 thomas
121 3efd8e31 2022-10-23 thomas return NULL;
122 3efd8e31 2022-10-23 thomas }
123 3efd8e31 2022-10-23 thomas
124 3efd8e31 2022-10-23 thomas static const struct got_error *
125 3efd8e31 2022-10-23 thomas send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
126 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf)
127 3efd8e31 2022-10-23 thomas {
128 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
129 3efd8e31 2022-10-23 thomas struct got_tag_object *tag;
130 3efd8e31 2022-10-23 thomas size_t namelen, len;
131 3efd8e31 2022-10-23 thomas char *peeled_refname = NULL;
132 3efd8e31 2022-10-23 thomas struct got_object_id *id;
133 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
134 3efd8e31 2022-10-23 thomas
135 3efd8e31 2022-10-23 thomas err = got_object_tag_open(&tag, repo_write.repo, obj);
136 3efd8e31 2022-10-23 thomas if (err)
137 3efd8e31 2022-10-23 thomas return err;
138 3efd8e31 2022-10-23 thomas
139 3efd8e31 2022-10-23 thomas if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
140 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
141 3efd8e31 2022-10-23 thomas goto done;
142 3efd8e31 2022-10-23 thomas }
143 3efd8e31 2022-10-23 thomas
144 3efd8e31 2022-10-23 thomas id = got_object_tag_get_object_id(tag);
145 3efd8e31 2022-10-23 thomas namelen = strlen(peeled_refname);
146 3efd8e31 2022-10-23 thomas
147 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
148 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
149 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
150 3efd8e31 2022-10-23 thomas goto done;
151 3efd8e31 2022-10-23 thomas }
152 3efd8e31 2022-10-23 thomas
153 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
154 3efd8e31 2022-10-23 thomas repo_write.pid, len);
155 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
156 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
157 3efd8e31 2022-10-23 thomas goto done;
158 3efd8e31 2022-10-23 thomas }
159 3efd8e31 2022-10-23 thomas
160 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
161 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
162 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
163 3efd8e31 2022-10-23 thomas goto done;
164 3efd8e31 2022-10-23 thomas }
165 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
166 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
167 3efd8e31 2022-10-23 thomas goto done;
168 3efd8e31 2022-10-23 thomas }
169 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
170 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
171 3efd8e31 2022-10-23 thomas goto done;
172 3efd8e31 2022-10-23 thomas }
173 3efd8e31 2022-10-23 thomas
174 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
175 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
176 3efd8e31 2022-10-23 thomas done:
177 3efd8e31 2022-10-23 thomas got_object_tag_close(tag);
178 3efd8e31 2022-10-23 thomas return err;
179 3efd8e31 2022-10-23 thomas }
180 3efd8e31 2022-10-23 thomas
181 3efd8e31 2022-10-23 thomas static const struct got_error *
182 3efd8e31 2022-10-23 thomas send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
183 3efd8e31 2022-10-23 thomas {
184 3efd8e31 2022-10-23 thomas const struct got_error *err;
185 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref);
186 3efd8e31 2022-10-23 thomas size_t namelen;
187 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
188 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
189 3efd8e31 2022-10-23 thomas size_t len;
190 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
191 3efd8e31 2022-10-23 thomas
192 3efd8e31 2022-10-23 thomas namelen = strlen(refname);
193 3efd8e31 2022-10-23 thomas
194 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
195 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
196 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
197 3efd8e31 2022-10-23 thomas
198 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, ref);
199 3efd8e31 2022-10-23 thomas if (err)
200 3efd8e31 2022-10-23 thomas return err;
201 3efd8e31 2022-10-23 thomas
202 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
203 3efd8e31 2022-10-23 thomas repo_write.pid, len);
204 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
205 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
206 3efd8e31 2022-10-23 thomas goto done;
207 3efd8e31 2022-10-23 thomas }
208 3efd8e31 2022-10-23 thomas
209 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
210 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
211 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
212 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
213 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
214 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, namelen) == -1)
215 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
216 3efd8e31 2022-10-23 thomas
217 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
218 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
219 3efd8e31 2022-10-23 thomas
220 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo_write.repo, id);
221 3efd8e31 2022-10-23 thomas if (err)
222 3efd8e31 2022-10-23 thomas goto done;
223 3efd8e31 2022-10-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG)
224 3efd8e31 2022-10-23 thomas err = send_peeled_tag_ref(ref, obj, ibuf);
225 3efd8e31 2022-10-23 thomas done:
226 3efd8e31 2022-10-23 thomas if (obj)
227 3efd8e31 2022-10-23 thomas got_object_close(obj);
228 3efd8e31 2022-10-23 thomas free(id);
229 3efd8e31 2022-10-23 thomas return err;
230 3efd8e31 2022-10-23 thomas }
231 3efd8e31 2022-10-23 thomas
232 3efd8e31 2022-10-23 thomas static const struct got_error *
233 9148c8a7 2023-01-02 thomas list_refs(struct imsg *imsg)
234 3efd8e31 2022-10-23 thomas {
235 3efd8e31 2022-10-23 thomas const struct got_error *err;
236 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
237 3efd8e31 2022-10-23 thomas struct got_reflist_head refs;
238 3efd8e31 2022-10-23 thomas struct got_reflist_entry *re;
239 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs_internal ireq;
240 3efd8e31 2022-10-23 thomas size_t datalen;
241 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist irefs;
242 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
243 3efd8e31 2022-10-23 thomas int client_fd = imsg->fd;
244 3efd8e31 2022-10-23 thomas
245 3efd8e31 2022-10-23 thomas TAILQ_INIT(&refs);
246 3efd8e31 2022-10-23 thomas
247 3efd8e31 2022-10-23 thomas if (client_fd == -1)
248 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
249 3efd8e31 2022-10-23 thomas
250 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
251 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
252 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
253 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
254 3efd8e31 2022-10-23 thomas
255 9148c8a7 2023-01-02 thomas if (ireq.client_id == 0)
256 9148c8a7 2023-01-02 thomas return got_error(GOT_ERR_CLIENT_ID);
257 9148c8a7 2023-01-02 thomas if (client->id != 0) {
258 9148c8a7 2023-01-02 thomas return got_error_msg(GOT_ERR_CLIENT_ID,
259 9148c8a7 2023-01-02 thomas "duplicate list-refs request");
260 9148c8a7 2023-01-02 thomas }
261 9148c8a7 2023-01-02 thomas client->id = ireq.client_id;
262 9148c8a7 2023-01-02 thomas client->fd = client_fd;
263 9148c8a7 2023-01-02 thomas client->nref_updates = 0;
264 49563dfb 2023-01-28 thomas client->nref_del = 0;
265 d98779cd 2023-01-19 thomas client->nref_new = 0;
266 f9542c24 2023-04-14 thomas client->nref_move = 0;
267 3efd8e31 2022-10-23 thomas
268 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client_fd);
269 3efd8e31 2022-10-23 thomas
270 3efd8e31 2022-10-23 thomas err = got_ref_list(&refs, repo_write.repo, "",
271 3efd8e31 2022-10-23 thomas got_ref_cmp_by_name, NULL);
272 3efd8e31 2022-10-23 thomas if (err)
273 3efd8e31 2022-10-23 thomas return err;
274 3efd8e31 2022-10-23 thomas
275 3efd8e31 2022-10-23 thomas memset(&irefs, 0, sizeof(irefs));
276 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
277 3efd8e31 2022-10-23 thomas struct got_object_id *id;
278 3efd8e31 2022-10-23 thomas int obj_type;
279 3efd8e31 2022-10-23 thomas
280 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
281 3efd8e31 2022-10-23 thomas continue;
282 3efd8e31 2022-10-23 thomas
283 3efd8e31 2022-10-23 thomas irefs.nrefs++;
284 3efd8e31 2022-10-23 thomas
285 3efd8e31 2022-10-23 thomas /* Account for a peeled tag refs. */
286 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, re->ref);
287 3efd8e31 2022-10-23 thomas if (err)
288 3efd8e31 2022-10-23 thomas goto done;
289 8652e561 2023-01-14 thomas err = got_object_get_type(&obj_type, repo_write.repo, id);
290 3efd8e31 2022-10-23 thomas free(id);
291 3efd8e31 2022-10-23 thomas if (err)
292 3efd8e31 2022-10-23 thomas goto done;
293 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_TAG)
294 3efd8e31 2022-10-23 thomas irefs.nrefs++;
295 3efd8e31 2022-10-23 thomas }
296 3efd8e31 2022-10-23 thomas
297 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
298 3efd8e31 2022-10-23 thomas repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
299 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose REFLIST");
300 3efd8e31 2022-10-23 thomas goto done;
301 3efd8e31 2022-10-23 thomas }
302 3efd8e31 2022-10-23 thomas
303 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
304 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
305 3efd8e31 2022-10-23 thomas continue;
306 3efd8e31 2022-10-23 thomas err = send_ref(re->ref, &ibuf);
307 3efd8e31 2022-10-23 thomas if (err)
308 3efd8e31 2022-10-23 thomas goto done;
309 3efd8e31 2022-10-23 thomas }
310 3efd8e31 2022-10-23 thomas
311 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
312 3efd8e31 2022-10-23 thomas done:
313 3efd8e31 2022-10-23 thomas got_ref_list_free(&refs);
314 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
315 3efd8e31 2022-10-23 thomas return err;
316 3efd8e31 2022-10-23 thomas }
317 3efd8e31 2022-10-23 thomas
318 3efd8e31 2022-10-23 thomas static const struct got_error *
319 6d7eb4f7 2023-04-04 thomas validate_namespace(const char *namespace)
320 3efd8e31 2022-10-23 thomas {
321 3efd8e31 2022-10-23 thomas size_t len = strlen(namespace);
322 3efd8e31 2022-10-23 thomas
323 3efd8e31 2022-10-23 thomas if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
324 3efd8e31 2022-10-23 thomas namespace[len -1] != '/') {
325 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_REF_NAME,
326 3efd8e31 2022-10-23 thomas "reference namespace '%s'", namespace);
327 3efd8e31 2022-10-23 thomas }
328 3efd8e31 2022-10-23 thomas
329 6d7eb4f7 2023-04-04 thomas return NULL;
330 6d7eb4f7 2023-04-04 thomas }
331 6d7eb4f7 2023-04-04 thomas
332 6d7eb4f7 2023-04-04 thomas static const struct got_error *
333 6d7eb4f7 2023-04-04 thomas protect_ref_namespace(const char *refname, const char *namespace)
334 6d7eb4f7 2023-04-04 thomas {
335 6d7eb4f7 2023-04-04 thomas const struct got_error *err;
336 6d7eb4f7 2023-04-04 thomas
337 6d7eb4f7 2023-04-04 thomas err = validate_namespace(namespace);
338 6d7eb4f7 2023-04-04 thomas if (err)
339 6d7eb4f7 2023-04-04 thomas return err;
340 6d7eb4f7 2023-04-04 thomas
341 6d7eb4f7 2023-04-04 thomas if (strncmp(namespace, refname, strlen(namespace)) == 0)
342 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
343 3efd8e31 2022-10-23 thomas
344 3efd8e31 2022-10-23 thomas return NULL;
345 3efd8e31 2022-10-23 thomas }
346 3efd8e31 2022-10-23 thomas
347 3efd8e31 2022-10-23 thomas static const struct got_error *
348 6d7eb4f7 2023-04-04 thomas verify_object_type(struct got_object_id *id, int expected_obj_type,
349 6d7eb4f7 2023-04-04 thomas struct got_pack *pack, struct got_packidx *packidx)
350 6d7eb4f7 2023-04-04 thomas {
351 6d7eb4f7 2023-04-04 thomas const struct got_error *err;
352 6d7eb4f7 2023-04-04 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
353 6d7eb4f7 2023-04-04 thomas struct got_object *obj;
354 6d7eb4f7 2023-04-04 thomas int idx;
355 6d7eb4f7 2023-04-04 thomas const char *typestr;
356 6d7eb4f7 2023-04-04 thomas
357 6d7eb4f7 2023-04-04 thomas idx = got_packidx_get_object_idx(packidx, id);
358 6d7eb4f7 2023-04-04 thomas if (idx == -1) {
359 6d7eb4f7 2023-04-04 thomas got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
360 6d7eb4f7 2023-04-04 thomas return got_error_fmt(GOT_ERR_BAD_PACKFILE,
361 6d7eb4f7 2023-04-04 thomas "object %s is missing from pack file", hex);
362 6d7eb4f7 2023-04-04 thomas }
363 6d7eb4f7 2023-04-04 thomas
364 6d7eb4f7 2023-04-04 thomas err = got_object_open_from_packfile(&obj, id, pack, packidx,
365 6d7eb4f7 2023-04-04 thomas idx, repo_write.repo);
366 6d7eb4f7 2023-04-04 thomas if (err)
367 6d7eb4f7 2023-04-04 thomas return err;
368 6d7eb4f7 2023-04-04 thomas
369 6d7eb4f7 2023-04-04 thomas if (obj->type != expected_obj_type) {
370 6d7eb4f7 2023-04-04 thomas got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
371 6d7eb4f7 2023-04-04 thomas got_object_type_label(&typestr, expected_obj_type);
372 6d7eb4f7 2023-04-04 thomas err = got_error_fmt(GOT_ERR_OBJ_TYPE,
373 6d7eb4f7 2023-04-04 thomas "%s is not pointing at a %s object", hex, typestr);
374 6d7eb4f7 2023-04-04 thomas }
375 6d7eb4f7 2023-04-04 thomas got_object_close(obj);
376 6d7eb4f7 2023-04-04 thomas return err;
377 6d7eb4f7 2023-04-04 thomas }
378 6d7eb4f7 2023-04-04 thomas
379 6d7eb4f7 2023-04-04 thomas static const struct got_error *
380 6d7eb4f7 2023-04-04 thomas protect_tag_namespace(const char *namespace, struct got_pack *pack,
381 6d7eb4f7 2023-04-04 thomas struct got_packidx *packidx, struct gotd_ref_update *ref_update)
382 6d7eb4f7 2023-04-04 thomas {
383 6d7eb4f7 2023-04-04 thomas const struct got_error *err;
384 6d7eb4f7 2023-04-04 thomas
385 6d7eb4f7 2023-04-04 thomas err = validate_namespace(namespace);
386 6d7eb4f7 2023-04-04 thomas if (err)
387 6d7eb4f7 2023-04-04 thomas return err;
388 6d7eb4f7 2023-04-04 thomas
389 6d7eb4f7 2023-04-04 thomas if (strncmp(namespace, got_ref_get_name(ref_update->ref),
390 6d7eb4f7 2023-04-04 thomas strlen(namespace)) != 0)
391 6d7eb4f7 2023-04-04 thomas return NULL;
392 6d7eb4f7 2023-04-04 thomas
393 6d7eb4f7 2023-04-04 thomas if (!ref_update->ref_is_new)
394 6d7eb4f7 2023-04-04 thomas return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
395 6d7eb4f7 2023-04-04 thomas
396 6d7eb4f7 2023-04-04 thomas return verify_object_type(&ref_update->new_id, GOT_OBJ_TYPE_TAG,
397 6d7eb4f7 2023-04-04 thomas pack, packidx);
398 6d7eb4f7 2023-04-04 thomas }
399 6d7eb4f7 2023-04-04 thomas
400 6d7eb4f7 2023-04-04 thomas static const struct got_error *
401 6d7eb4f7 2023-04-04 thomas protect_require_yca(struct got_object_id *tip_id,
402 6d7eb4f7 2023-04-04 thomas size_t max_commits_to_traverse, struct got_pack *pack,
403 6d7eb4f7 2023-04-04 thomas struct got_packidx *packidx, struct got_reference *ref)
404 6d7eb4f7 2023-04-04 thomas {
405 6d7eb4f7 2023-04-04 thomas const struct got_error *err;
406 6d7eb4f7 2023-04-04 thomas uint8_t *buf = NULL;
407 6d7eb4f7 2023-04-04 thomas size_t len;
408 6d7eb4f7 2023-04-04 thomas struct got_object_id *expected_yca_id = NULL;
409 6d7eb4f7 2023-04-04 thomas struct got_object *obj = NULL;
410 6d7eb4f7 2023-04-04 thomas struct got_commit_object *commit = NULL;
411 6d7eb4f7 2023-04-04 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
412 6d7eb4f7 2023-04-04 thomas const struct got_object_id_queue *parent_ids;
413 6d7eb4f7 2023-04-04 thomas struct got_object_id_queue ids;
414 6d7eb4f7 2023-04-04 thomas struct got_object_qid *pid, *qid;
415 6d7eb4f7 2023-04-04 thomas struct got_object_idset *traversed_set = NULL;
416 6d7eb4f7 2023-04-04 thomas int found_yca = 0, obj_type;
417 6d7eb4f7 2023-04-04 thomas
418 6d7eb4f7 2023-04-04 thomas STAILQ_INIT(&ids);
419 6d7eb4f7 2023-04-04 thomas
420 6d7eb4f7 2023-04-04 thomas err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
421 6d7eb4f7 2023-04-04 thomas if (err)
422 6d7eb4f7 2023-04-04 thomas return err;
423 6d7eb4f7 2023-04-04 thomas
424 6d7eb4f7 2023-04-04 thomas err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
425 6d7eb4f7 2023-04-04 thomas if (err)
426 6d7eb4f7 2023-04-04 thomas goto done;
427 6d7eb4f7 2023-04-04 thomas
428 6d7eb4f7 2023-04-04 thomas if (obj_type != GOT_OBJ_TYPE_COMMIT) {
429 6d7eb4f7 2023-04-04 thomas got_sha1_digest_to_str(expected_yca_id->sha1, hex, sizeof(hex));
430 6d7eb4f7 2023-04-04 thomas err = got_error_fmt(GOT_ERR_OBJ_TYPE,
431 6d7eb4f7 2023-04-04 thomas "%s is not pointing at a commit object", hex);
432 6d7eb4f7 2023-04-04 thomas goto done;
433 6d7eb4f7 2023-04-04 thomas }
434 6d7eb4f7 2023-04-04 thomas
435 6d7eb4f7 2023-04-04 thomas traversed_set = got_object_idset_alloc();
436 6d7eb4f7 2023-04-04 thomas if (traversed_set == NULL) {
437 6d7eb4f7 2023-04-04 thomas err = got_error_from_errno("got_object_idset_alloc");
438 6d7eb4f7 2023-04-04 thomas goto done;
439 6d7eb4f7 2023-04-04 thomas }
440 6d7eb4f7 2023-04-04 thomas
441 6d7eb4f7 2023-04-04 thomas err = got_object_qid_alloc(&qid, tip_id);
442 6d7eb4f7 2023-04-04 thomas if (err)
443 6d7eb4f7 2023-04-04 thomas goto done;
444 6d7eb4f7 2023-04-04 thomas STAILQ_INSERT_TAIL(&ids, qid, entry);
445 6d7eb4f7 2023-04-04 thomas while (!STAILQ_EMPTY(&ids)) {
446 6d7eb4f7 2023-04-04 thomas err = check_cancelled(NULL);
447 6d7eb4f7 2023-04-04 thomas if (err)
448 6d7eb4f7 2023-04-04 thomas break;
449 6d7eb4f7 2023-04-04 thomas
450 6d7eb4f7 2023-04-04 thomas qid = STAILQ_FIRST(&ids);
451 6d7eb4f7 2023-04-04 thomas if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
452 6d7eb4f7 2023-04-04 thomas found_yca = 1;
453 6d7eb4f7 2023-04-04 thomas break;
454 6d7eb4f7 2023-04-04 thomas }
455 6d7eb4f7 2023-04-04 thomas
456 6d7eb4f7 2023-04-04 thomas if (got_object_idset_num_elements(traversed_set) >=
457 6d7eb4f7 2023-04-04 thomas max_commits_to_traverse)
458 6d7eb4f7 2023-04-04 thomas break;
459 6d7eb4f7 2023-04-04 thomas
460 6d7eb4f7 2023-04-04 thomas if (got_object_idset_contains(traversed_set, &qid->id)) {
461 6d7eb4f7 2023-04-04 thomas STAILQ_REMOVE_HEAD(&ids, entry);
462 6d7eb4f7 2023-04-04 thomas got_object_qid_free(qid);
463 6d7eb4f7 2023-04-04 thomas qid = NULL;
464 6d7eb4f7 2023-04-04 thomas continue;
465 6d7eb4f7 2023-04-04 thomas }
466 6d7eb4f7 2023-04-04 thomas err = got_object_idset_add(traversed_set, &qid->id, NULL);
467 6d7eb4f7 2023-04-04 thomas if (err)
468 6d7eb4f7 2023-04-04 thomas goto done;
469 6d7eb4f7 2023-04-04 thomas
470 6d7eb4f7 2023-04-04 thomas err = got_object_open(&obj, repo_write.repo, &qid->id);
471 6d7eb4f7 2023-04-04 thomas if (err && err->code != GOT_ERR_NO_OBJ)
472 6d7eb4f7 2023-04-04 thomas goto done;
473 6d7eb4f7 2023-04-04 thomas err = NULL;
474 6d7eb4f7 2023-04-04 thomas if (obj) {
475 6d7eb4f7 2023-04-04 thomas err = got_object_commit_open(&commit, repo_write.repo,
476 6d7eb4f7 2023-04-04 thomas obj);
477 6d7eb4f7 2023-04-04 thomas if (err)
478 6d7eb4f7 2023-04-04 thomas goto done;
479 6d7eb4f7 2023-04-04 thomas } else {
480 6d7eb4f7 2023-04-04 thomas int idx;
481 6d7eb4f7 2023-04-04 thomas
482 6d7eb4f7 2023-04-04 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
483 6d7eb4f7 2023-04-04 thomas if (idx == -1) {
484 6d7eb4f7 2023-04-04 thomas got_sha1_digest_to_str(qid->id.sha1,
485 6d7eb4f7 2023-04-04 thomas hex, sizeof(hex));
486 6d7eb4f7 2023-04-04 thomas err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
487 6d7eb4f7 2023-04-04 thomas "object %s is missing from pack file", hex);
488 6d7eb4f7 2023-04-04 thomas goto done;
489 6d7eb4f7 2023-04-04 thomas }
490 6d7eb4f7 2023-04-04 thomas
491 6d7eb4f7 2023-04-04 thomas err = got_object_open_from_packfile(&obj, &qid->id,
492 6d7eb4f7 2023-04-04 thomas pack, packidx, idx, repo_write.repo);
493 6d7eb4f7 2023-04-04 thomas if (err)
494 6d7eb4f7 2023-04-04 thomas goto done;
495 6d7eb4f7 2023-04-04 thomas
496 6d7eb4f7 2023-04-04 thomas if (obj->type != GOT_OBJ_TYPE_COMMIT) {
497 6d7eb4f7 2023-04-04 thomas got_sha1_digest_to_str(qid->id.sha1,
498 6d7eb4f7 2023-04-04 thomas hex, sizeof(hex));
499 6d7eb4f7 2023-04-04 thomas err = got_error_fmt(GOT_ERR_OBJ_TYPE,
500 6d7eb4f7 2023-04-04 thomas "%s is not pointing at a commit object",
501 6d7eb4f7 2023-04-04 thomas hex);
502 6d7eb4f7 2023-04-04 thomas goto done;
503 6d7eb4f7 2023-04-04 thomas }
504 6d7eb4f7 2023-04-04 thomas
505 6d7eb4f7 2023-04-04 thomas err = got_packfile_extract_object_to_mem(&buf, &len,
506 6d7eb4f7 2023-04-04 thomas obj, pack);
507 6d7eb4f7 2023-04-04 thomas if (err)
508 6d7eb4f7 2023-04-04 thomas goto done;
509 6d7eb4f7 2023-04-04 thomas
510 6d7eb4f7 2023-04-04 thomas err = got_object_parse_commit(&commit, buf, len);
511 6d7eb4f7 2023-04-04 thomas if (err)
512 6d7eb4f7 2023-04-04 thomas goto done;
513 6d7eb4f7 2023-04-04 thomas
514 6d7eb4f7 2023-04-04 thomas free(buf);
515 6d7eb4f7 2023-04-04 thomas buf = NULL;
516 6d7eb4f7 2023-04-04 thomas }
517 6d7eb4f7 2023-04-04 thomas
518 6d7eb4f7 2023-04-04 thomas got_object_close(obj);
519 6d7eb4f7 2023-04-04 thomas obj = NULL;
520 6d7eb4f7 2023-04-04 thomas
521 6d7eb4f7 2023-04-04 thomas STAILQ_REMOVE_HEAD(&ids, entry);
522 6d7eb4f7 2023-04-04 thomas got_object_qid_free(qid);
523 6d7eb4f7 2023-04-04 thomas qid = NULL;
524 6d7eb4f7 2023-04-04 thomas
525 6d7eb4f7 2023-04-04 thomas if (got_object_commit_get_nparents(commit) == 0)
526 6d7eb4f7 2023-04-04 thomas break;
527 6d7eb4f7 2023-04-04 thomas
528 6d7eb4f7 2023-04-04 thomas parent_ids = got_object_commit_get_parent_ids(commit);
529 6d7eb4f7 2023-04-04 thomas STAILQ_FOREACH(pid, parent_ids, entry) {
530 6d7eb4f7 2023-04-04 thomas err = check_cancelled(NULL);
531 6d7eb4f7 2023-04-04 thomas if (err)
532 6d7eb4f7 2023-04-04 thomas goto done;
533 6d7eb4f7 2023-04-04 thomas err = got_object_qid_alloc(&qid, &pid->id);
534 6d7eb4f7 2023-04-04 thomas if (err)
535 6d7eb4f7 2023-04-04 thomas goto done;
536 6d7eb4f7 2023-04-04 thomas STAILQ_INSERT_TAIL(&ids, qid, entry);
537 6d7eb4f7 2023-04-04 thomas qid = NULL;
538 6d7eb4f7 2023-04-04 thomas }
539 6d7eb4f7 2023-04-04 thomas got_object_commit_close(commit);
540 6d7eb4f7 2023-04-04 thomas commit = NULL;
541 6d7eb4f7 2023-04-04 thomas }
542 6d7eb4f7 2023-04-04 thomas
543 6d7eb4f7 2023-04-04 thomas if (!found_yca) {
544 6d7eb4f7 2023-04-04 thomas err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
545 6d7eb4f7 2023-04-04 thomas got_ref_get_name(ref));
546 6d7eb4f7 2023-04-04 thomas }
547 6d7eb4f7 2023-04-04 thomas done:
548 6d7eb4f7 2023-04-04 thomas got_object_idset_free(traversed_set);
549 6d7eb4f7 2023-04-04 thomas got_object_id_queue_free(&ids);
550 6d7eb4f7 2023-04-04 thomas free(buf);
551 6d7eb4f7 2023-04-04 thomas if (obj)
552 6d7eb4f7 2023-04-04 thomas got_object_close(obj);
553 6d7eb4f7 2023-04-04 thomas if (commit)
554 6d7eb4f7 2023-04-04 thomas got_object_commit_close(commit);
555 6d7eb4f7 2023-04-04 thomas free(expected_yca_id);
556 6d7eb4f7 2023-04-04 thomas return err;
557 6d7eb4f7 2023-04-04 thomas }
558 6d7eb4f7 2023-04-04 thomas
559 6d7eb4f7 2023-04-04 thomas static const struct got_error *
560 6d7eb4f7 2023-04-04 thomas protect_branch_namespace(const char *namespace, struct got_pack *pack,
561 6d7eb4f7 2023-04-04 thomas struct got_packidx *packidx, struct gotd_ref_update *ref_update)
562 6d7eb4f7 2023-04-04 thomas {
563 6d7eb4f7 2023-04-04 thomas const struct got_error *err;
564 6d7eb4f7 2023-04-04 thomas
565 6d7eb4f7 2023-04-04 thomas err = validate_namespace(namespace);
566 6d7eb4f7 2023-04-04 thomas if (err)
567 6d7eb4f7 2023-04-04 thomas return err;
568 6d7eb4f7 2023-04-04 thomas
569 6d7eb4f7 2023-04-04 thomas if (strncmp(namespace, got_ref_get_name(ref_update->ref),
570 6d7eb4f7 2023-04-04 thomas strlen(namespace)) != 0)
571 6d7eb4f7 2023-04-04 thomas return NULL;
572 6d7eb4f7 2023-04-04 thomas
573 6d7eb4f7 2023-04-04 thomas if (ref_update->ref_is_new) {
574 6d7eb4f7 2023-04-04 thomas return verify_object_type(&ref_update->new_id,
575 6d7eb4f7 2023-04-04 thomas GOT_OBJ_TYPE_COMMIT, pack, packidx);
576 6d7eb4f7 2023-04-04 thomas }
577 6d7eb4f7 2023-04-04 thomas
578 6d7eb4f7 2023-04-04 thomas return protect_require_yca(&ref_update->new_id,
579 6d7eb4f7 2023-04-04 thomas be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
580 6d7eb4f7 2023-04-04 thomas ref_update->ref);
581 6d7eb4f7 2023-04-04 thomas }
582 6d7eb4f7 2023-04-04 thomas
583 6d7eb4f7 2023-04-04 thomas static const struct got_error *
584 6d7eb4f7 2023-04-04 thomas protect_branch(const char *refname, struct got_pack *pack,
585 6d7eb4f7 2023-04-04 thomas struct got_packidx *packidx, struct gotd_ref_update *ref_update)
586 6d7eb4f7 2023-04-04 thomas {
587 6d7eb4f7 2023-04-04 thomas if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
588 6d7eb4f7 2023-04-04 thomas return NULL;
589 6d7eb4f7 2023-04-04 thomas
590 6d7eb4f7 2023-04-04 thomas /* Always allow new branches to be created. */
591 6d7eb4f7 2023-04-04 thomas if (ref_update->ref_is_new) {
592 6d7eb4f7 2023-04-04 thomas return verify_object_type(&ref_update->new_id,
593 6d7eb4f7 2023-04-04 thomas GOT_OBJ_TYPE_COMMIT, pack, packidx);
594 6d7eb4f7 2023-04-04 thomas }
595 6d7eb4f7 2023-04-04 thomas
596 6d7eb4f7 2023-04-04 thomas return protect_require_yca(&ref_update->new_id,
597 6d7eb4f7 2023-04-04 thomas be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
598 6d7eb4f7 2023-04-04 thomas ref_update->ref);
599 6d7eb4f7 2023-04-04 thomas }
600 6d7eb4f7 2023-04-04 thomas
601 6d7eb4f7 2023-04-04 thomas static const struct got_error *
602 9148c8a7 2023-01-02 thomas recv_ref_update(struct imsg *imsg)
603 3efd8e31 2022-10-23 thomas {
604 49563dfb 2023-01-28 thomas static const char zero_id[SHA1_DIGEST_LENGTH];
605 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
606 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
607 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
608 3efd8e31 2022-10-23 thomas size_t datalen;
609 3efd8e31 2022-10-23 thomas char *refname = NULL;
610 3efd8e31 2022-10-23 thomas struct got_reference *ref = NULL;
611 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
612 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
613 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update = NULL;
614 3efd8e31 2022-10-23 thomas
615 3efd8e31 2022-10-23 thomas log_debug("ref-update received");
616 3efd8e31 2022-10-23 thomas
617 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
618 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref))
619 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
620 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg->data, sizeof(iref));
621 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iref) + iref.name_len)
622 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
623 3efd8e31 2022-10-23 thomas
624 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
625 3efd8e31 2022-10-23 thomas
626 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iref), iref.name_len);
627 3efd8e31 2022-10-23 thomas if (refname == NULL)
628 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
629 3efd8e31 2022-10-23 thomas
630 3efd8e31 2022-10-23 thomas ref_update = calloc(1, sizeof(*ref_update));
631 3efd8e31 2022-10-23 thomas if (ref_update == NULL) {
632 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
633 3efd8e31 2022-10-23 thomas goto done;
634 3efd8e31 2022-10-23 thomas }
635 3efd8e31 2022-10-23 thomas
636 3efd8e31 2022-10-23 thomas memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
637 3efd8e31 2022-10-23 thomas memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
638 3efd8e31 2022-10-23 thomas
639 3efd8e31 2022-10-23 thomas err = got_ref_open(&ref, repo_write.repo, refname, 0);
640 3efd8e31 2022-10-23 thomas if (err) {
641 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_NOT_REF)
642 3efd8e31 2022-10-23 thomas goto done;
643 6d7eb4f7 2023-04-04 thomas if (memcmp(ref_update->new_id.sha1,
644 6d7eb4f7 2023-04-04 thomas zero_id, sizeof(zero_id)) == 0) {
645 6d7eb4f7 2023-04-04 thomas err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
646 6d7eb4f7 2023-04-04 thomas "%s", refname);
647 6d7eb4f7 2023-04-04 thomas goto done;
648 6d7eb4f7 2023-04-04 thomas }
649 3efd8e31 2022-10-23 thomas err = got_ref_alloc(&ref, refname, &ref_update->new_id);
650 3efd8e31 2022-10-23 thomas if (err)
651 3efd8e31 2022-10-23 thomas goto done;
652 3efd8e31 2022-10-23 thomas ref_update->ref_is_new = 1;
653 d98779cd 2023-01-19 thomas client->nref_new++;
654 3efd8e31 2022-10-23 thomas }
655 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(ref)) {
656 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
657 3efd8e31 2022-10-23 thomas "'%s' is a symbolic reference and cannot "
658 3efd8e31 2022-10-23 thomas "be updated", got_ref_get_name(ref));
659 3efd8e31 2022-10-23 thomas goto done;
660 3efd8e31 2022-10-23 thomas }
661 3efd8e31 2022-10-23 thomas if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
662 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
663 3efd8e31 2022-10-23 thomas "%s: does not begin with 'refs/'",
664 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
665 3efd8e31 2022-10-23 thomas goto done;
666 3efd8e31 2022-10-23 thomas }
667 3efd8e31 2022-10-23 thomas
668 6d7eb4f7 2023-04-04 thomas err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
669 3efd8e31 2022-10-23 thomas if (err)
670 3efd8e31 2022-10-23 thomas goto done;
671 6d7eb4f7 2023-04-04 thomas err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
672 3efd8e31 2022-10-23 thomas if (err)
673 3efd8e31 2022-10-23 thomas goto done;
674 3efd8e31 2022-10-23 thomas
675 3efd8e31 2022-10-23 thomas if (!ref_update->ref_is_new) {
676 3efd8e31 2022-10-23 thomas /*
677 3efd8e31 2022-10-23 thomas * Ensure the client's idea of this update is still valid.
678 3efd8e31 2022-10-23 thomas * At this point we can only return an error, to prevent
679 3efd8e31 2022-10-23 thomas * the client from uploading a pack file which will likely
680 3efd8e31 2022-10-23 thomas * have to be discarded.
681 3efd8e31 2022-10-23 thomas */
682 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, ref);
683 3efd8e31 2022-10-23 thomas if (err)
684 3efd8e31 2022-10-23 thomas goto done;
685 3efd8e31 2022-10-23 thomas
686 3efd8e31 2022-10-23 thomas if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
687 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
688 3efd8e31 2022-10-23 thomas "%s has been modified by someone else "
689 3efd8e31 2022-10-23 thomas "while transaction was in progress",
690 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
691 3efd8e31 2022-10-23 thomas goto done;
692 3efd8e31 2022-10-23 thomas }
693 3efd8e31 2022-10-23 thomas }
694 3efd8e31 2022-10-23 thomas
695 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
696 3efd8e31 2022-10-23 thomas repo_write.pid);
697 3efd8e31 2022-10-23 thomas
698 3efd8e31 2022-10-23 thomas ref_update->ref = ref;
699 49563dfb 2023-01-28 thomas if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
700 49563dfb 2023-01-28 thomas ref_update->delete_ref = 1;
701 49563dfb 2023-01-28 thomas client->nref_del++;
702 49563dfb 2023-01-28 thomas }
703 9148c8a7 2023-01-02 thomas STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
704 9148c8a7 2023-01-02 thomas client->nref_updates++;
705 3efd8e31 2022-10-23 thomas ref = NULL;
706 3efd8e31 2022-10-23 thomas ref_update = NULL;
707 3efd8e31 2022-10-23 thomas done:
708 3efd8e31 2022-10-23 thomas if (ref)
709 3efd8e31 2022-10-23 thomas got_ref_close(ref);
710 3efd8e31 2022-10-23 thomas free(ref_update);
711 3efd8e31 2022-10-23 thomas free(refname);
712 3efd8e31 2022-10-23 thomas free(id);
713 3efd8e31 2022-10-23 thomas return err;
714 3efd8e31 2022-10-23 thomas }
715 3efd8e31 2022-10-23 thomas
716 3efd8e31 2022-10-23 thomas static const struct got_error *
717 3efd8e31 2022-10-23 thomas pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
718 3efd8e31 2022-10-23 thomas uint32_t nobj_loose, uint32_t nobj_resolved)
719 3efd8e31 2022-10-23 thomas {
720 3efd8e31 2022-10-23 thomas int p_indexed = 0, p_resolved = 0;
721 3efd8e31 2022-10-23 thomas int nobj_delta = nobj_total - nobj_loose;
722 3efd8e31 2022-10-23 thomas
723 3efd8e31 2022-10-23 thomas if (nobj_total > 0)
724 3efd8e31 2022-10-23 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
725 3efd8e31 2022-10-23 thomas
726 3efd8e31 2022-10-23 thomas if (nobj_delta > 0)
727 3efd8e31 2022-10-23 thomas p_resolved = (nobj_resolved * 100) / nobj_delta;
728 3efd8e31 2022-10-23 thomas
729 3efd8e31 2022-10-23 thomas if (p_resolved > 0) {
730 3efd8e31 2022-10-23 thomas log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
731 3efd8e31 2022-10-23 thomas nobj_total, p_indexed, nobj_delta, p_resolved);
732 3efd8e31 2022-10-23 thomas } else
733 3efd8e31 2022-10-23 thomas log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
734 3efd8e31 2022-10-23 thomas
735 3efd8e31 2022-10-23 thomas return NULL;
736 3efd8e31 2022-10-23 thomas }
737 3efd8e31 2022-10-23 thomas
738 3efd8e31 2022-10-23 thomas static const struct got_error *
739 3efd8e31 2022-10-23 thomas read_more_pack_stream(int infd, BUF *buf, size_t minsize)
740 3efd8e31 2022-10-23 thomas {
741 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
742 3efd8e31 2022-10-23 thomas uint8_t readahead[65536];
743 3efd8e31 2022-10-23 thomas size_t have, newlen;
744 3efd8e31 2022-10-23 thomas
745 3efd8e31 2022-10-23 thomas err = got_poll_read_full(infd, &have,
746 3efd8e31 2022-10-23 thomas readahead, sizeof(readahead), minsize);
747 3efd8e31 2022-10-23 thomas if (err)
748 3efd8e31 2022-10-23 thomas return err;
749 3efd8e31 2022-10-23 thomas
750 3efd8e31 2022-10-23 thomas err = buf_append(&newlen, buf, readahead, have);
751 3efd8e31 2022-10-23 thomas if (err)
752 3efd8e31 2022-10-23 thomas return err;
753 8652e561 2023-01-14 thomas return NULL;
754 3efd8e31 2022-10-23 thomas }
755 3efd8e31 2022-10-23 thomas
756 3efd8e31 2022-10-23 thomas static const struct got_error *
757 3efd8e31 2022-10-23 thomas copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
758 b16893ba 2023-02-24 thomas off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
759 3efd8e31 2022-10-23 thomas {
760 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
761 3efd8e31 2022-10-23 thomas uint8_t t = 0;
762 3efd8e31 2022-10-23 thomas uint64_t s = 0;
763 3efd8e31 2022-10-23 thomas uint8_t sizebuf[8];
764 3efd8e31 2022-10-23 thomas size_t i = 0;
765 3efd8e31 2022-10-23 thomas off_t obj_offset = *outsize;
766 3efd8e31 2022-10-23 thomas
767 3efd8e31 2022-10-23 thomas do {
768 3efd8e31 2022-10-23 thomas /* We do not support size values which don't fit in 64 bit. */
769 3efd8e31 2022-10-23 thomas if (i > 9)
770 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
771 fe3b5495 2022-10-25 thomas "packfile offset %lld", (long long)obj_offset);
772 3efd8e31 2022-10-23 thomas
773 3efd8e31 2022-10-23 thomas if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
774 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
775 3efd8e31 2022-10-23 thomas sizeof(sizebuf[0]));
776 3efd8e31 2022-10-23 thomas if (err)
777 3efd8e31 2022-10-23 thomas return err;
778 3efd8e31 2022-10-23 thomas }
779 3efd8e31 2022-10-23 thomas
780 3efd8e31 2022-10-23 thomas sizebuf[i] = buf_getc(buf, *buf_pos);
781 3efd8e31 2022-10-23 thomas *buf_pos += sizeof(sizebuf[i]);
782 3efd8e31 2022-10-23 thomas
783 3efd8e31 2022-10-23 thomas if (i == 0) {
784 3efd8e31 2022-10-23 thomas t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
785 3efd8e31 2022-10-23 thomas GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
786 3efd8e31 2022-10-23 thomas s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
787 3efd8e31 2022-10-23 thomas } else {
788 3efd8e31 2022-10-23 thomas size_t shift = 4 + 7 * (i - 1);
789 3efd8e31 2022-10-23 thomas s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
790 3efd8e31 2022-10-23 thomas shift);
791 3efd8e31 2022-10-23 thomas }
792 3efd8e31 2022-10-23 thomas i++;
793 3efd8e31 2022-10-23 thomas } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
794 3efd8e31 2022-10-23 thomas
795 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, sizebuf, i, ctx);
796 3efd8e31 2022-10-23 thomas if (err)
797 3efd8e31 2022-10-23 thomas return err;
798 3efd8e31 2022-10-23 thomas *outsize += i;
799 3efd8e31 2022-10-23 thomas
800 3efd8e31 2022-10-23 thomas *type = t;
801 3efd8e31 2022-10-23 thomas *size = s;
802 3efd8e31 2022-10-23 thomas return NULL;
803 3efd8e31 2022-10-23 thomas }
804 3efd8e31 2022-10-23 thomas
805 3efd8e31 2022-10-23 thomas static const struct got_error *
806 3efd8e31 2022-10-23 thomas copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
807 b16893ba 2023-02-24 thomas struct got_hash *ctx)
808 3efd8e31 2022-10-23 thomas {
809 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
810 3efd8e31 2022-10-23 thomas size_t remain = buf_len(buf) - *buf_pos;
811 3efd8e31 2022-10-23 thomas
812 3efd8e31 2022-10-23 thomas if (remain < SHA1_DIGEST_LENGTH) {
813 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
814 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH - remain);
815 3efd8e31 2022-10-23 thomas if (err)
816 3efd8e31 2022-10-23 thomas return err;
817 3efd8e31 2022-10-23 thomas }
818 3efd8e31 2022-10-23 thomas
819 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
820 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH, ctx);
821 3efd8e31 2022-10-23 thomas if (err)
822 3efd8e31 2022-10-23 thomas return err;
823 3efd8e31 2022-10-23 thomas
824 3efd8e31 2022-10-23 thomas *buf_pos += SHA1_DIGEST_LENGTH;
825 3efd8e31 2022-10-23 thomas return NULL;
826 3efd8e31 2022-10-23 thomas }
827 3efd8e31 2022-10-23 thomas
828 3efd8e31 2022-10-23 thomas static const struct got_error *
829 3efd8e31 2022-10-23 thomas copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
830 b16893ba 2023-02-24 thomas struct got_hash *ctx)
831 3efd8e31 2022-10-23 thomas {
832 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
833 3efd8e31 2022-10-23 thomas uint64_t o = 0;
834 3efd8e31 2022-10-23 thomas uint8_t offbuf[8];
835 3efd8e31 2022-10-23 thomas size_t i = 0;
836 3efd8e31 2022-10-23 thomas off_t obj_offset = *outsize;
837 3efd8e31 2022-10-23 thomas
838 3efd8e31 2022-10-23 thomas do {
839 3efd8e31 2022-10-23 thomas /* We do not support offset values which don't fit in 64 bit. */
840 3efd8e31 2022-10-23 thomas if (i > 8)
841 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
842 fe3b5495 2022-10-25 thomas "packfile offset %lld", (long long)obj_offset);
843 3efd8e31 2022-10-23 thomas
844 3efd8e31 2022-10-23 thomas if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
845 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
846 3efd8e31 2022-10-23 thomas sizeof(offbuf[0]));
847 3efd8e31 2022-10-23 thomas if (err)
848 3efd8e31 2022-10-23 thomas return err;
849 3efd8e31 2022-10-23 thomas }
850 3efd8e31 2022-10-23 thomas
851 3efd8e31 2022-10-23 thomas offbuf[i] = buf_getc(buf, *buf_pos);
852 3efd8e31 2022-10-23 thomas *buf_pos += sizeof(offbuf[i]);
853 3efd8e31 2022-10-23 thomas
854 3efd8e31 2022-10-23 thomas if (i == 0)
855 3efd8e31 2022-10-23 thomas o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
856 3efd8e31 2022-10-23 thomas else {
857 3efd8e31 2022-10-23 thomas o++;
858 3efd8e31 2022-10-23 thomas o <<= 7;
859 3efd8e31 2022-10-23 thomas o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
860 3efd8e31 2022-10-23 thomas }
861 3efd8e31 2022-10-23 thomas i++;
862 3efd8e31 2022-10-23 thomas } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
863 3efd8e31 2022-10-23 thomas
864 3efd8e31 2022-10-23 thomas if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
865 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PACK_OFFSET);
866 3efd8e31 2022-10-23 thomas
867 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, offbuf, i, ctx);
868 3efd8e31 2022-10-23 thomas if (err)
869 3efd8e31 2022-10-23 thomas return err;
870 3efd8e31 2022-10-23 thomas
871 3efd8e31 2022-10-23 thomas *outsize += i;
872 3efd8e31 2022-10-23 thomas return NULL;
873 3efd8e31 2022-10-23 thomas }
874 3efd8e31 2022-10-23 thomas
875 3efd8e31 2022-10-23 thomas static const struct got_error *
876 3efd8e31 2022-10-23 thomas copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
877 b16893ba 2023-02-24 thomas struct got_hash *ctx)
878 3efd8e31 2022-10-23 thomas {
879 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
880 3efd8e31 2022-10-23 thomas z_stream z;
881 3efd8e31 2022-10-23 thomas int zret;
882 3efd8e31 2022-10-23 thomas char voidbuf[1024];
883 3efd8e31 2022-10-23 thomas size_t consumed_total = 0;
884 3efd8e31 2022-10-23 thomas off_t zstream_offset = *outsize;
885 3efd8e31 2022-10-23 thomas
886 3efd8e31 2022-10-23 thomas memset(&z, 0, sizeof(z));
887 3efd8e31 2022-10-23 thomas
888 3efd8e31 2022-10-23 thomas z.zalloc = Z_NULL;
889 3efd8e31 2022-10-23 thomas z.zfree = Z_NULL;
890 3efd8e31 2022-10-23 thomas zret = inflateInit(&z);
891 3efd8e31 2022-10-23 thomas if (zret != Z_OK) {
892 3efd8e31 2022-10-23 thomas if (zret == Z_ERRNO)
893 3efd8e31 2022-10-23 thomas return got_error_from_errno("inflateInit");
894 3efd8e31 2022-10-23 thomas if (zret == Z_MEM_ERROR) {
895 3efd8e31 2022-10-23 thomas errno = ENOMEM;
896 3efd8e31 2022-10-23 thomas return got_error_from_errno("inflateInit");
897 3efd8e31 2022-10-23 thomas }
898 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_DECOMPRESSION,
899 3efd8e31 2022-10-23 thomas "inflateInit failed");
900 3efd8e31 2022-10-23 thomas }
901 3efd8e31 2022-10-23 thomas
902 3efd8e31 2022-10-23 thomas while (zret != Z_STREAM_END) {
903 3efd8e31 2022-10-23 thomas size_t last_total_in, consumed;
904 3efd8e31 2022-10-23 thomas
905 3efd8e31 2022-10-23 thomas /*
906 3efd8e31 2022-10-23 thomas * Decompress into the void. Object data will be parsed
907 3efd8e31 2022-10-23 thomas * later, when the pack file is indexed. For now, we just
908 3efd8e31 2022-10-23 thomas * want to locate the end of the compressed stream.
909 3efd8e31 2022-10-23 thomas */
910 3efd8e31 2022-10-23 thomas while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
911 3efd8e31 2022-10-23 thomas last_total_in = z.total_in;
912 3efd8e31 2022-10-23 thomas z.next_in = buf_get(buf) + *buf_pos;
913 3efd8e31 2022-10-23 thomas z.avail_in = buf_len(buf) - *buf_pos;
914 3efd8e31 2022-10-23 thomas z.next_out = voidbuf;
915 3efd8e31 2022-10-23 thomas z.avail_out = sizeof(voidbuf);
916 3efd8e31 2022-10-23 thomas
917 3efd8e31 2022-10-23 thomas zret = inflate(&z, Z_SYNC_FLUSH);
918 3efd8e31 2022-10-23 thomas if (zret != Z_OK && zret != Z_BUF_ERROR &&
919 3efd8e31 2022-10-23 thomas zret != Z_STREAM_END) {
920 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_DECOMPRESSION,
921 fe3b5495 2022-10-25 thomas "packfile offset %lld",
922 fe3b5495 2022-10-25 thomas (long long)zstream_offset);
923 3efd8e31 2022-10-23 thomas goto done;
924 3efd8e31 2022-10-23 thomas }
925 3efd8e31 2022-10-23 thomas consumed = z.total_in - last_total_in;
926 3efd8e31 2022-10-23 thomas
927 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
928 3efd8e31 2022-10-23 thomas consumed, ctx);
929 3efd8e31 2022-10-23 thomas if (err)
930 3efd8e31 2022-10-23 thomas goto done;
931 3efd8e31 2022-10-23 thomas
932 3efd8e31 2022-10-23 thomas err = buf_discard(buf, *buf_pos + consumed);
933 3efd8e31 2022-10-23 thomas if (err)
934 3efd8e31 2022-10-23 thomas goto done;
935 3efd8e31 2022-10-23 thomas *buf_pos = 0;
936 3efd8e31 2022-10-23 thomas
937 3efd8e31 2022-10-23 thomas consumed_total += consumed;
938 3efd8e31 2022-10-23 thomas }
939 3efd8e31 2022-10-23 thomas
940 3efd8e31 2022-10-23 thomas if (zret != Z_STREAM_END) {
941 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf, 1);
942 3efd8e31 2022-10-23 thomas if (err)
943 3efd8e31 2022-10-23 thomas goto done;
944 3efd8e31 2022-10-23 thomas }
945 3efd8e31 2022-10-23 thomas }
946 3efd8e31 2022-10-23 thomas
947 3efd8e31 2022-10-23 thomas if (err == NULL)
948 3efd8e31 2022-10-23 thomas *outsize += consumed_total;
949 3efd8e31 2022-10-23 thomas done:
950 3efd8e31 2022-10-23 thomas inflateEnd(&z);
951 3efd8e31 2022-10-23 thomas return err;
952 3efd8e31 2022-10-23 thomas }
953 3efd8e31 2022-10-23 thomas
954 3efd8e31 2022-10-23 thomas static const struct got_error *
955 3efd8e31 2022-10-23 thomas validate_object_type(int obj_type)
956 3efd8e31 2022-10-23 thomas {
957 3efd8e31 2022-10-23 thomas switch (obj_type) {
958 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_BLOB:
959 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_COMMIT:
960 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_TREE:
961 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_TAG:
962 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_REF_DELTA:
963 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_OFFSET_DELTA:
964 3efd8e31 2022-10-23 thomas return NULL;
965 3efd8e31 2022-10-23 thomas default:
966 3efd8e31 2022-10-23 thomas break;
967 3efd8e31 2022-10-23 thomas }
968 3efd8e31 2022-10-23 thomas
969 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
970 3efd8e31 2022-10-23 thomas }
971 3efd8e31 2022-10-23 thomas
972 3efd8e31 2022-10-23 thomas static const struct got_error *
973 f9542c24 2023-04-14 thomas ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
974 f9542c24 2023-04-14 thomas {
975 f9542c24 2023-04-14 thomas const struct got_error *err = NULL;
976 f9542c24 2023-04-14 thomas struct gotd_ref_update *ref_update;
977 f9542c24 2023-04-14 thomas struct got_object *obj;
978 f9542c24 2023-04-14 thomas
979 f9542c24 2023-04-14 thomas STAILQ_FOREACH(ref_update, ref_updates, entry) {
980 f9542c24 2023-04-14 thomas err = got_object_open(&obj, repo_write.repo,
981 f9542c24 2023-04-14 thomas &ref_update->new_id);
982 f9542c24 2023-04-14 thomas if (err)
983 f9542c24 2023-04-14 thomas return err;
984 f9542c24 2023-04-14 thomas got_object_close(obj);
985 f9542c24 2023-04-14 thomas }
986 f9542c24 2023-04-14 thomas
987 f9542c24 2023-04-14 thomas return NULL;
988 f9542c24 2023-04-14 thomas }
989 f9542c24 2023-04-14 thomas
990 f9542c24 2023-04-14 thomas static const struct got_error *
991 d98779cd 2023-01-19 thomas recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
992 d98779cd 2023-01-19 thomas int infd, int outfd)
993 3efd8e31 2022-10-23 thomas {
994 3efd8e31 2022-10-23 thomas const struct got_error *err;
995 d98779cd 2023-01-19 thomas struct repo_write_client *client = &repo_write_client;
996 3efd8e31 2022-10-23 thomas struct got_packfile_hdr hdr;
997 3efd8e31 2022-10-23 thomas size_t have;
998 d98779cd 2023-01-19 thomas uint32_t nhave = 0;
999 b16893ba 2023-02-24 thomas struct got_hash ctx;
1000 3efd8e31 2022-10-23 thomas uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1001 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
1002 3efd8e31 2022-10-23 thomas BUF *buf = NULL;
1003 3efd8e31 2022-10-23 thomas size_t buf_pos = 0, remain;
1004 3efd8e31 2022-10-23 thomas ssize_t w;
1005 3efd8e31 2022-10-23 thomas
1006 3efd8e31 2022-10-23 thomas *outsize = 0;
1007 d98779cd 2023-01-19 thomas *nobj = 0;
1008 49563dfb 2023-01-28 thomas
1009 49563dfb 2023-01-28 thomas /* if only deleting references there's nothing to read */
1010 49563dfb 2023-01-28 thomas if (client->nref_updates == client->nref_del)
1011 49563dfb 2023-01-28 thomas return NULL;
1012 49563dfb 2023-01-28 thomas
1013 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
1014 3efd8e31 2022-10-23 thomas
1015 3efd8e31 2022-10-23 thomas err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1016 3efd8e31 2022-10-23 thomas if (err)
1017 3efd8e31 2022-10-23 thomas return err;
1018 3efd8e31 2022-10-23 thomas if (have != sizeof(hdr))
1019 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1020 3efd8e31 2022-10-23 thomas *outsize += have;
1021 3efd8e31 2022-10-23 thomas
1022 3efd8e31 2022-10-23 thomas if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1023 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
1024 3efd8e31 2022-10-23 thomas "bad packfile signature");
1025 3efd8e31 2022-10-23 thomas if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1026 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
1027 3efd8e31 2022-10-23 thomas "bad packfile version");
1028 3efd8e31 2022-10-23 thomas
1029 d98779cd 2023-01-19 thomas *nobj = be32toh(hdr.nobjects);
1030 d98779cd 2023-01-19 thomas if (*nobj == 0) {
1031 d98779cd 2023-01-19 thomas /*
1032 d98779cd 2023-01-19 thomas * Clients which are creating new references only
1033 d98779cd 2023-01-19 thomas * will send us an empty pack file.
1034 d98779cd 2023-01-19 thomas */
1035 d98779cd 2023-01-19 thomas if (client->nref_updates > 0 &&
1036 d98779cd 2023-01-19 thomas client->nref_updates == client->nref_new)
1037 d98779cd 2023-01-19 thomas return NULL;
1038 d98779cd 2023-01-19 thomas
1039 f9542c24 2023-04-14 thomas /*
1040 f9542c24 2023-04-14 thomas * Clients which only move existing refs will send us an empty
1041 f9542c24 2023-04-14 thomas * pack file. All referenced objects must exist locally.
1042 f9542c24 2023-04-14 thomas */
1043 f9542c24 2023-04-14 thomas err = ensure_all_objects_exist_locally(&client->ref_updates);
1044 f9542c24 2023-04-14 thomas if (err) {
1045 f9542c24 2023-04-14 thomas if (err->code != GOT_ERR_NO_OBJ)
1046 f9542c24 2023-04-14 thomas return err;
1047 f9542c24 2023-04-14 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
1048 f9542c24 2023-04-14 thomas "bad packfile with zero objects");
1049 f9542c24 2023-04-14 thomas }
1050 f9542c24 2023-04-14 thomas
1051 f9542c24 2023-04-14 thomas client->nref_move = client->nref_updates;
1052 f9542c24 2023-04-14 thomas return NULL;
1053 d98779cd 2023-01-19 thomas }
1054 3efd8e31 2022-10-23 thomas
1055 d98779cd 2023-01-19 thomas log_debug("expecting %d objects", *nobj);
1056 3efd8e31 2022-10-23 thomas
1057 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1058 3efd8e31 2022-10-23 thomas if (err)
1059 3efd8e31 2022-10-23 thomas return err;
1060 3efd8e31 2022-10-23 thomas
1061 3efd8e31 2022-10-23 thomas err = buf_alloc(&buf, 65536);
1062 3efd8e31 2022-10-23 thomas if (err)
1063 3efd8e31 2022-10-23 thomas return err;
1064 3efd8e31 2022-10-23 thomas
1065 d98779cd 2023-01-19 thomas while (nhave != *nobj) {
1066 3efd8e31 2022-10-23 thomas uint8_t obj_type;
1067 3efd8e31 2022-10-23 thomas uint64_t obj_size;
1068 3efd8e31 2022-10-23 thomas
1069 3efd8e31 2022-10-23 thomas err = copy_object_type_and_size(&obj_type, &obj_size,
1070 3efd8e31 2022-10-23 thomas infd, outfd, outsize, buf, &buf_pos, &ctx);
1071 3efd8e31 2022-10-23 thomas if (err)
1072 3efd8e31 2022-10-23 thomas goto done;
1073 3efd8e31 2022-10-23 thomas
1074 3efd8e31 2022-10-23 thomas err = validate_object_type(obj_type);
1075 3efd8e31 2022-10-23 thomas if (err)
1076 3efd8e31 2022-10-23 thomas goto done;
1077 3efd8e31 2022-10-23 thomas
1078 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1079 3efd8e31 2022-10-23 thomas err = copy_ref_delta(infd, outfd, outsize,
1080 3efd8e31 2022-10-23 thomas buf, &buf_pos, &ctx);
1081 3efd8e31 2022-10-23 thomas if (err)
1082 3efd8e31 2022-10-23 thomas goto done;
1083 3efd8e31 2022-10-23 thomas } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1084 3efd8e31 2022-10-23 thomas err = copy_offset_delta(infd, outfd, outsize,
1085 3efd8e31 2022-10-23 thomas buf, &buf_pos, &ctx);
1086 3efd8e31 2022-10-23 thomas if (err)
1087 3efd8e31 2022-10-23 thomas goto done;
1088 3efd8e31 2022-10-23 thomas }
1089 3efd8e31 2022-10-23 thomas
1090 3efd8e31 2022-10-23 thomas err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1091 3efd8e31 2022-10-23 thomas if (err)
1092 3efd8e31 2022-10-23 thomas goto done;
1093 3efd8e31 2022-10-23 thomas
1094 3efd8e31 2022-10-23 thomas nhave++;
1095 3efd8e31 2022-10-23 thomas }
1096 3efd8e31 2022-10-23 thomas
1097 d98779cd 2023-01-19 thomas log_debug("received %u objects", *nobj);
1098 3efd8e31 2022-10-23 thomas
1099 b16893ba 2023-02-24 thomas got_hash_final(&ctx, expected_sha1);
1100 3efd8e31 2022-10-23 thomas
1101 3efd8e31 2022-10-23 thomas remain = buf_len(buf) - buf_pos;
1102 3efd8e31 2022-10-23 thomas if (remain < SHA1_DIGEST_LENGTH) {
1103 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
1104 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH - remain);
1105 3efd8e31 2022-10-23 thomas if (err)
1106 3efd8e31 2022-10-23 thomas return err;
1107 3efd8e31 2022-10-23 thomas }
1108 3efd8e31 2022-10-23 thomas
1109 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1110 3efd8e31 2022-10-23 thomas log_debug("expect SHA1: %s", hex);
1111 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1112 3efd8e31 2022-10-23 thomas log_debug("actual SHA1: %s", hex);
1113 3efd8e31 2022-10-23 thomas
1114 3efd8e31 2022-10-23 thomas if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1115 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH) != 0) {
1116 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PACKFILE_CSUM);
1117 3efd8e31 2022-10-23 thomas goto done;
1118 3efd8e31 2022-10-23 thomas }
1119 3efd8e31 2022-10-23 thomas
1120 3efd8e31 2022-10-23 thomas memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1121 3efd8e31 2022-10-23 thomas
1122 3efd8e31 2022-10-23 thomas w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1123 3efd8e31 2022-10-23 thomas if (w == -1) {
1124 3efd8e31 2022-10-23 thomas err = got_error_from_errno("write");
1125 3efd8e31 2022-10-23 thomas goto done;
1126 3efd8e31 2022-10-23 thomas }
1127 3efd8e31 2022-10-23 thomas if (w != SHA1_DIGEST_LENGTH) {
1128 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_IO);
1129 3efd8e31 2022-10-23 thomas goto done;
1130 3efd8e31 2022-10-23 thomas }
1131 3efd8e31 2022-10-23 thomas
1132 3efd8e31 2022-10-23 thomas *outsize += SHA1_DIGEST_LENGTH;
1133 3efd8e31 2022-10-23 thomas
1134 3efd8e31 2022-10-23 thomas if (fsync(outfd) == -1) {
1135 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fsync");
1136 3efd8e31 2022-10-23 thomas goto done;
1137 3efd8e31 2022-10-23 thomas }
1138 3efd8e31 2022-10-23 thomas if (lseek(outfd, 0L, SEEK_SET) == -1) {
1139 3efd8e31 2022-10-23 thomas err = got_error_from_errno("lseek");
1140 3efd8e31 2022-10-23 thomas goto done;
1141 3efd8e31 2022-10-23 thomas }
1142 3efd8e31 2022-10-23 thomas done:
1143 3efd8e31 2022-10-23 thomas buf_free(buf);
1144 3efd8e31 2022-10-23 thomas return err;
1145 3efd8e31 2022-10-23 thomas }
1146 3efd8e31 2022-10-23 thomas
1147 3efd8e31 2022-10-23 thomas static const struct got_error *
1148 9148c8a7 2023-01-02 thomas report_pack_status(const struct got_error *unpack_err)
1149 3efd8e31 2022-10-23 thomas {
1150 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1151 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1152 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_status istatus;
1153 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1154 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
1155 3efd8e31 2022-10-23 thomas const char *unpack_ok = "unpack ok\n";
1156 3efd8e31 2022-10-23 thomas size_t len;
1157 8652e561 2023-01-14 thomas
1158 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
1159 3efd8e31 2022-10-23 thomas
1160 3efd8e31 2022-10-23 thomas if (unpack_err)
1161 3efd8e31 2022-10-23 thomas istatus.reason_len = strlen(unpack_err->msg);
1162 3efd8e31 2022-10-23 thomas else
1163 3efd8e31 2022-10-23 thomas istatus.reason_len = strlen(unpack_ok);
1164 3efd8e31 2022-10-23 thomas
1165 3efd8e31 2022-10-23 thomas len = sizeof(istatus) + istatus.reason_len;
1166 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1167 3efd8e31 2022-10-23 thomas repo_write.pid, len);
1168 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
1169 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1170 3efd8e31 2022-10-23 thomas goto done;
1171 3efd8e31 2022-10-23 thomas }
1172 3efd8e31 2022-10-23 thomas
1173 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1174 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1175 3efd8e31 2022-10-23 thomas goto done;
1176 3efd8e31 2022-10-23 thomas }
1177 3efd8e31 2022-10-23 thomas
1178 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1179 3efd8e31 2022-10-23 thomas istatus.reason_len) == -1) {
1180 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1181 3efd8e31 2022-10-23 thomas goto done;
1182 3efd8e31 2022-10-23 thomas }
1183 3efd8e31 2022-10-23 thomas
1184 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1185 3efd8e31 2022-10-23 thomas imsg_close(&ibuf, wbuf);
1186 3efd8e31 2022-10-23 thomas
1187 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
1188 3efd8e31 2022-10-23 thomas done:
1189 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
1190 3efd8e31 2022-10-23 thomas return err;
1191 3efd8e31 2022-10-23 thomas }
1192 3efd8e31 2022-10-23 thomas
1193 3efd8e31 2022-10-23 thomas static const struct got_error *
1194 d98779cd 2023-01-19 thomas recv_packfile(int *have_packfile, struct imsg *imsg)
1195 3efd8e31 2022-10-23 thomas {
1196 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL, *unpack_err;
1197 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1198 3efd8e31 2022-10-23 thomas struct gotd_imsg_recv_packfile ireq;
1199 3efd8e31 2022-10-23 thomas FILE *tempfiles[3] = { NULL, NULL, NULL };
1200 3efd8e31 2022-10-23 thomas struct repo_tempfile {
1201 3efd8e31 2022-10-23 thomas int fd;
1202 3efd8e31 2022-10-23 thomas int idx;
1203 3efd8e31 2022-10-23 thomas } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1204 3efd8e31 2022-10-23 thomas int i;
1205 3efd8e31 2022-10-23 thomas size_t datalen;
1206 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
1207 3efd8e31 2022-10-23 thomas struct got_ratelimit rl;
1208 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
1209 3efd8e31 2022-10-23 thomas off_t pack_filesize = 0;
1210 d98779cd 2023-01-19 thomas uint32_t nobj = 0;
1211 3efd8e31 2022-10-23 thomas
1212 3efd8e31 2022-10-23 thomas log_debug("packfile request received");
1213 3efd8e31 2022-10-23 thomas
1214 d98779cd 2023-01-19 thomas *have_packfile = 0;
1215 3efd8e31 2022-10-23 thomas got_ratelimit_init(&rl, 2, 0);
1216 3efd8e31 2022-10-23 thomas
1217 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1218 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
1219 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1220 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1221 3efd8e31 2022-10-23 thomas
1222 9148c8a7 2023-01-02 thomas if (client->pack_pipe == -1 || client->packidx_fd == -1)
1223 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1224 3efd8e31 2022-10-23 thomas
1225 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
1226 3efd8e31 2022-10-23 thomas
1227 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1228 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1229 3efd8e31 2022-10-23 thomas
1230 9148c8a7 2023-01-02 thomas pack = &client->pack;
1231 3efd8e31 2022-10-23 thomas memset(pack, 0, sizeof(*pack));
1232 3efd8e31 2022-10-23 thomas pack->fd = imsg->fd;
1233 3efd8e31 2022-10-23 thomas err = got_delta_cache_alloc(&pack->delta_cache);
1234 3efd8e31 2022-10-23 thomas if (err)
1235 3efd8e31 2022-10-23 thomas return err;
1236 3efd8e31 2022-10-23 thomas
1237 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo_tempfiles); i++) {
1238 3efd8e31 2022-10-23 thomas struct repo_tempfile *t = &repo_tempfiles[i];
1239 3efd8e31 2022-10-23 thomas err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
1240 3efd8e31 2022-10-23 thomas if (err)
1241 3efd8e31 2022-10-23 thomas goto done;
1242 3efd8e31 2022-10-23 thomas }
1243 3efd8e31 2022-10-23 thomas
1244 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(tempfiles); i++) {
1245 da2c57e4 2023-02-03 thomas int fd;
1246 3efd8e31 2022-10-23 thomas FILE *f;
1247 da2c57e4 2023-02-03 thomas
1248 da2c57e4 2023-02-03 thomas fd = dup(repo_tempfiles[i].fd);
1249 3efd8e31 2022-10-23 thomas if (fd == -1) {
1250 3efd8e31 2022-10-23 thomas err = got_error_from_errno("dup");
1251 3efd8e31 2022-10-23 thomas goto done;
1252 3efd8e31 2022-10-23 thomas }
1253 3efd8e31 2022-10-23 thomas f = fdopen(fd, "w+");
1254 3efd8e31 2022-10-23 thomas if (f == NULL) {
1255 da2c57e4 2023-02-03 thomas err = got_error_from_errno("fdopen");
1256 3efd8e31 2022-10-23 thomas close(fd);
1257 3efd8e31 2022-10-23 thomas goto done;
1258 3efd8e31 2022-10-23 thomas }
1259 3efd8e31 2022-10-23 thomas tempfiles[i] = f;
1260 3efd8e31 2022-10-23 thomas }
1261 3efd8e31 2022-10-23 thomas
1262 3efd8e31 2022-10-23 thomas /* Send pack file pipe to gotsh(1). */
1263 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
1264 3efd8e31 2022-10-23 thomas repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
1265 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] = -1;
1266 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose ACK");
1267 3efd8e31 2022-10-23 thomas if (err)
1268 3efd8e31 2022-10-23 thomas goto done;
1269 3efd8e31 2022-10-23 thomas }
1270 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] = -1;
1271 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
1272 3efd8e31 2022-10-23 thomas if (err)
1273 3efd8e31 2022-10-23 thomas goto done;
1274 3efd8e31 2022-10-23 thomas
1275 3efd8e31 2022-10-23 thomas log_debug("receiving pack data");
1276 d98779cd 2023-01-19 thomas unpack_err = recv_packdata(&pack_filesize, &nobj,
1277 d98779cd 2023-01-19 thomas client->pack_sha1, client->pack_pipe, pack->fd);
1278 3efd8e31 2022-10-23 thomas if (ireq.report_status) {
1279 9148c8a7 2023-01-02 thomas err = report_pack_status(unpack_err);
1280 3efd8e31 2022-10-23 thomas if (err) {
1281 3efd8e31 2022-10-23 thomas /* Git clients hang up after sending the pack file. */
1282 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF)
1283 3efd8e31 2022-10-23 thomas err = NULL;
1284 3efd8e31 2022-10-23 thomas }
1285 3efd8e31 2022-10-23 thomas }
1286 3efd8e31 2022-10-23 thomas if (unpack_err)
1287 3efd8e31 2022-10-23 thomas err = unpack_err;
1288 3efd8e31 2022-10-23 thomas if (err)
1289 3efd8e31 2022-10-23 thomas goto done;
1290 3efd8e31 2022-10-23 thomas
1291 3efd8e31 2022-10-23 thomas log_debug("pack data received");
1292 d98779cd 2023-01-19 thomas
1293 d98779cd 2023-01-19 thomas /*
1294 d98779cd 2023-01-19 thomas * Clients which are creating new references only will
1295 d98779cd 2023-01-19 thomas * send us an empty pack file.
1296 d98779cd 2023-01-19 thomas */
1297 d98779cd 2023-01-19 thomas if (nobj == 0 &&
1298 d98779cd 2023-01-19 thomas pack_filesize == sizeof(struct got_packfile_hdr) &&
1299 d98779cd 2023-01-19 thomas client->nref_updates > 0 &&
1300 d98779cd 2023-01-19 thomas client->nref_updates == client->nref_new)
1301 d98779cd 2023-01-19 thomas goto done;
1302 3efd8e31 2022-10-23 thomas
1303 49563dfb 2023-01-28 thomas /*
1304 49563dfb 2023-01-28 thomas * Clients which are deleting references only will send
1305 49563dfb 2023-01-28 thomas * no pack file.
1306 49563dfb 2023-01-28 thomas */
1307 49563dfb 2023-01-28 thomas if (nobj == 0 &&
1308 49563dfb 2023-01-28 thomas client->nref_del > 0 &&
1309 49563dfb 2023-01-28 thomas client->nref_updates == client->nref_del)
1310 f9542c24 2023-04-14 thomas goto done;
1311 f9542c24 2023-04-14 thomas
1312 f9542c24 2023-04-14 thomas /*
1313 f9542c24 2023-04-14 thomas * Clients which only move existing refs will send us an empty
1314 f9542c24 2023-04-14 thomas * pack file. All referenced objects must exist locally.
1315 f9542c24 2023-04-14 thomas */
1316 f9542c24 2023-04-14 thomas if (nobj == 0 &&
1317 f9542c24 2023-04-14 thomas pack_filesize == sizeof(struct got_packfile_hdr) &&
1318 f9542c24 2023-04-14 thomas client->nref_move > 0 &&
1319 f9542c24 2023-04-14 thomas client->nref_updates == client->nref_move)
1320 49563dfb 2023-01-28 thomas goto done;
1321 49563dfb 2023-01-28 thomas
1322 3efd8e31 2022-10-23 thomas pack->filesize = pack_filesize;
1323 d98779cd 2023-01-19 thomas *have_packfile = 1;
1324 3efd8e31 2022-10-23 thomas
1325 66e6097f 2022-10-27 thomas log_debug("begin indexing pack (%lld bytes in size)",
1326 66e6097f 2022-10-27 thomas (long long)pack->filesize);
1327 9148c8a7 2023-01-02 thomas err = got_pack_index(pack, client->packidx_fd,
1328 9148c8a7 2023-01-02 thomas tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1329 3efd8e31 2022-10-23 thomas pack_index_progress, NULL, &rl);
1330 3efd8e31 2022-10-23 thomas if (err)
1331 3efd8e31 2022-10-23 thomas goto done;
1332 3efd8e31 2022-10-23 thomas log_debug("done indexing pack");
1333 3efd8e31 2022-10-23 thomas
1334 9148c8a7 2023-01-02 thomas if (fsync(client->packidx_fd) == -1) {
1335 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fsync");
1336 3efd8e31 2022-10-23 thomas goto done;
1337 3efd8e31 2022-10-23 thomas }
1338 9148c8a7 2023-01-02 thomas if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1339 3efd8e31 2022-10-23 thomas err = got_error_from_errno("lseek");
1340 3efd8e31 2022-10-23 thomas done:
1341 9148c8a7 2023-01-02 thomas if (close(client->pack_pipe) == -1 && err == NULL)
1342 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1343 9148c8a7 2023-01-02 thomas client->pack_pipe = -1;
1344 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo_tempfiles); i++) {
1345 3efd8e31 2022-10-23 thomas struct repo_tempfile *t = &repo_tempfiles[i];
1346 3efd8e31 2022-10-23 thomas if (t->idx != -1)
1347 3efd8e31 2022-10-23 thomas got_repo_temp_fds_put(t->idx, repo_write.repo);
1348 3efd8e31 2022-10-23 thomas }
1349 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(tempfiles); i++) {
1350 3efd8e31 2022-10-23 thomas if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1351 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
1352 3efd8e31 2022-10-23 thomas }
1353 3efd8e31 2022-10-23 thomas if (err)
1354 3efd8e31 2022-10-23 thomas got_pack_close(pack);
1355 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
1356 3efd8e31 2022-10-23 thomas return err;
1357 3efd8e31 2022-10-23 thomas }
1358 3efd8e31 2022-10-23 thomas
1359 3efd8e31 2022-10-23 thomas static const struct got_error *
1360 9148c8a7 2023-01-02 thomas verify_packfile(void)
1361 3efd8e31 2022-10-23 thomas {
1362 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL, *close_err;
1363 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1364 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1365 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
1366 3efd8e31 2022-10-23 thomas struct stat sb;
1367 3efd8e31 2022-10-23 thomas char *id_str = NULL;
1368 6d7eb4f7 2023-04-04 thomas struct got_object *obj = NULL;
1369 6d7eb4f7 2023-04-04 thomas struct got_pathlist_entry *pe;
1370 6d7eb4f7 2023-04-04 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
1371 3efd8e31 2022-10-23 thomas
1372 3efd8e31 2022-10-23 thomas if (STAILQ_EMPTY(&client->ref_updates)) {
1373 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1374 3efd8e31 2022-10-23 thomas "cannot verify pack file without any ref-updates");
1375 3efd8e31 2022-10-23 thomas }
1376 3efd8e31 2022-10-23 thomas
1377 3efd8e31 2022-10-23 thomas if (client->pack.fd == -1) {
1378 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1379 3efd8e31 2022-10-23 thomas "invalid pack file handle during pack verification");
1380 3efd8e31 2022-10-23 thomas }
1381 3efd8e31 2022-10-23 thomas if (client->packidx_fd == -1) {
1382 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1383 3efd8e31 2022-10-23 thomas "invalid pack index handle during pack verification");
1384 3efd8e31 2022-10-23 thomas }
1385 3efd8e31 2022-10-23 thomas
1386 3efd8e31 2022-10-23 thomas if (fstat(client->packidx_fd, &sb) == -1)
1387 3efd8e31 2022-10-23 thomas return got_error_from_errno("pack index fstat");
1388 3efd8e31 2022-10-23 thomas
1389 3efd8e31 2022-10-23 thomas packidx = malloc(sizeof(*packidx));
1390 3efd8e31 2022-10-23 thomas memset(packidx, 0, sizeof(*packidx));
1391 3efd8e31 2022-10-23 thomas packidx->fd = client->packidx_fd;
1392 3efd8e31 2022-10-23 thomas client->packidx_fd = -1;
1393 3efd8e31 2022-10-23 thomas packidx->len = sb.st_size;
1394 8652e561 2023-01-14 thomas
1395 3efd8e31 2022-10-23 thomas err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1396 3efd8e31 2022-10-23 thomas if (err)
1397 3efd8e31 2022-10-23 thomas return err;
1398 3efd8e31 2022-10-23 thomas
1399 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1400 49563dfb 2023-01-28 thomas if (ref_update->delete_ref)
1401 49563dfb 2023-01-28 thomas continue;
1402 49563dfb 2023-01-28 thomas
1403 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1404 6d7eb4f7 2023-04-04 thomas err = protect_tag_namespace(pe->path, &client->pack,
1405 6d7eb4f7 2023-04-04 thomas packidx, ref_update);
1406 6d7eb4f7 2023-04-04 thomas if (err)
1407 6d7eb4f7 2023-04-04 thomas goto done;
1408 6d7eb4f7 2023-04-04 thomas }
1409 3efd8e31 2022-10-23 thomas
1410 6d7eb4f7 2023-04-04 thomas /*
1411 6d7eb4f7 2023-04-04 thomas * Objects which already exist in our repository need
1412 6d7eb4f7 2023-04-04 thomas * not be present in the pack file.
1413 6d7eb4f7 2023-04-04 thomas */
1414 6d7eb4f7 2023-04-04 thomas err = got_object_open(&obj, repo_write.repo,
1415 6d7eb4f7 2023-04-04 thomas &ref_update->new_id);
1416 6d7eb4f7 2023-04-04 thomas if (err && err->code != GOT_ERR_NO_OBJ)
1417 3efd8e31 2022-10-23 thomas goto done;
1418 6d7eb4f7 2023-04-04 thomas err = NULL;
1419 6d7eb4f7 2023-04-04 thomas if (obj) {
1420 6d7eb4f7 2023-04-04 thomas got_object_close(obj);
1421 6d7eb4f7 2023-04-04 thomas obj = NULL;
1422 6d7eb4f7 2023-04-04 thomas } else {
1423 6d7eb4f7 2023-04-04 thomas int idx = got_packidx_get_object_idx(packidx,
1424 6d7eb4f7 2023-04-04 thomas &ref_update->new_id);
1425 6d7eb4f7 2023-04-04 thomas if (idx == -1) {
1426 6d7eb4f7 2023-04-04 thomas got_sha1_digest_to_str(ref_update->new_id.sha1,
1427 6d7eb4f7 2023-04-04 thomas hex, sizeof(hex));
1428 6d7eb4f7 2023-04-04 thomas err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1429 6d7eb4f7 2023-04-04 thomas "object %s is missing from pack file",
1430 6d7eb4f7 2023-04-04 thomas hex);
1431 6d7eb4f7 2023-04-04 thomas goto done;
1432 6d7eb4f7 2023-04-04 thomas }
1433 3efd8e31 2022-10-23 thomas }
1434 6d7eb4f7 2023-04-04 thomas
1435 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1436 6d7eb4f7 2023-04-04 thomas entry) {
1437 6d7eb4f7 2023-04-04 thomas err = protect_branch_namespace(pe->path,
1438 6d7eb4f7 2023-04-04 thomas &client->pack, packidx, ref_update);
1439 6d7eb4f7 2023-04-04 thomas if (err)
1440 6d7eb4f7 2023-04-04 thomas goto done;
1441 6d7eb4f7 2023-04-04 thomas }
1442 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1443 6d7eb4f7 2023-04-04 thomas err = protect_branch(pe->path, &client->pack,
1444 6d7eb4f7 2023-04-04 thomas packidx, ref_update);
1445 6d7eb4f7 2023-04-04 thomas if (err)
1446 6d7eb4f7 2023-04-04 thomas goto done;
1447 6d7eb4f7 2023-04-04 thomas }
1448 3efd8e31 2022-10-23 thomas }
1449 3efd8e31 2022-10-23 thomas
1450 8652e561 2023-01-14 thomas done:
1451 3efd8e31 2022-10-23 thomas close_err = got_packidx_close(packidx);
1452 3efd8e31 2022-10-23 thomas if (close_err && err == NULL)
1453 3efd8e31 2022-10-23 thomas err = close_err;
1454 3efd8e31 2022-10-23 thomas free(id_str);
1455 6d7eb4f7 2023-04-04 thomas if (obj)
1456 6d7eb4f7 2023-04-04 thomas got_object_close(obj);
1457 3efd8e31 2022-10-23 thomas return err;
1458 3efd8e31 2022-10-23 thomas }
1459 3efd8e31 2022-10-23 thomas
1460 3efd8e31 2022-10-23 thomas static const struct got_error *
1461 6d7eb4f7 2023-04-04 thomas protect_refs_from_deletion(void)
1462 6d7eb4f7 2023-04-04 thomas {
1463 6d7eb4f7 2023-04-04 thomas const struct got_error *err = NULL;
1464 6d7eb4f7 2023-04-04 thomas struct repo_write_client *client = &repo_write_client;
1465 6d7eb4f7 2023-04-04 thomas struct gotd_ref_update *ref_update;
1466 6d7eb4f7 2023-04-04 thomas struct got_pathlist_entry *pe;
1467 6d7eb4f7 2023-04-04 thomas const char *refname;
1468 6d7eb4f7 2023-04-04 thomas
1469 6d7eb4f7 2023-04-04 thomas STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1470 6d7eb4f7 2023-04-04 thomas if (!ref_update->delete_ref)
1471 6d7eb4f7 2023-04-04 thomas continue;
1472 6d7eb4f7 2023-04-04 thomas
1473 6d7eb4f7 2023-04-04 thomas refname = got_ref_get_name(ref_update->ref);
1474 6d7eb4f7 2023-04-04 thomas
1475 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1476 6d7eb4f7 2023-04-04 thomas err = protect_ref_namespace(refname, pe->path);
1477 6d7eb4f7 2023-04-04 thomas if (err)
1478 6d7eb4f7 2023-04-04 thomas return err;
1479 6d7eb4f7 2023-04-04 thomas }
1480 6d7eb4f7 2023-04-04 thomas
1481 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1482 6d7eb4f7 2023-04-04 thomas entry) {
1483 6d7eb4f7 2023-04-04 thomas err = protect_ref_namespace(refname, pe->path);
1484 6d7eb4f7 2023-04-04 thomas if (err)
1485 6d7eb4f7 2023-04-04 thomas return err;
1486 6d7eb4f7 2023-04-04 thomas }
1487 6d7eb4f7 2023-04-04 thomas
1488 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1489 6d7eb4f7 2023-04-04 thomas if (strcmp(refname, pe->path) == 0) {
1490 6d7eb4f7 2023-04-04 thomas return got_error_fmt(GOT_ERR_REF_PROTECTED,
1491 6d7eb4f7 2023-04-04 thomas "%s", refname);
1492 6d7eb4f7 2023-04-04 thomas }
1493 6d7eb4f7 2023-04-04 thomas }
1494 6d7eb4f7 2023-04-04 thomas }
1495 6d7eb4f7 2023-04-04 thomas
1496 6d7eb4f7 2023-04-04 thomas return NULL;
1497 6d7eb4f7 2023-04-04 thomas }
1498 6d7eb4f7 2023-04-04 thomas
1499 6d7eb4f7 2023-04-04 thomas static const struct got_error *
1500 9148c8a7 2023-01-02 thomas install_packfile(struct gotd_imsgev *iev)
1501 3efd8e31 2022-10-23 thomas {
1502 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1503 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_install inst;
1504 3efd8e31 2022-10-23 thomas int ret;
1505 3efd8e31 2022-10-23 thomas
1506 3efd8e31 2022-10-23 thomas memset(&inst, 0, sizeof(inst));
1507 3efd8e31 2022-10-23 thomas inst.client_id = client->id;
1508 3efd8e31 2022-10-23 thomas memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1509 3efd8e31 2022-10-23 thomas
1510 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1511 3efd8e31 2022-10-23 thomas PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1512 3efd8e31 2022-10-23 thomas if (ret == -1)
1513 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1514 3efd8e31 2022-10-23 thomas
1515 3efd8e31 2022-10-23 thomas return NULL;
1516 3efd8e31 2022-10-23 thomas }
1517 3efd8e31 2022-10-23 thomas
1518 3efd8e31 2022-10-23 thomas static const struct got_error *
1519 9148c8a7 2023-01-02 thomas send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1520 3efd8e31 2022-10-23 thomas {
1521 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1522 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_updates_start istart;
1523 3efd8e31 2022-10-23 thomas int ret;
1524 3efd8e31 2022-10-23 thomas
1525 3efd8e31 2022-10-23 thomas memset(&istart, 0, sizeof(istart));
1526 3efd8e31 2022-10-23 thomas istart.nref_updates = nref_updates;
1527 3efd8e31 2022-10-23 thomas istart.client_id = client->id;
1528 3efd8e31 2022-10-23 thomas
1529 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1530 3efd8e31 2022-10-23 thomas PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1531 3efd8e31 2022-10-23 thomas if (ret == -1)
1532 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose REF_UPDATES_START");
1533 3efd8e31 2022-10-23 thomas
1534 3efd8e31 2022-10-23 thomas return NULL;
1535 3efd8e31 2022-10-23 thomas }
1536 3efd8e31 2022-10-23 thomas
1537 3efd8e31 2022-10-23 thomas
1538 3efd8e31 2022-10-23 thomas static const struct got_error *
1539 9148c8a7 2023-01-02 thomas send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1540 3efd8e31 2022-10-23 thomas {
1541 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1542 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1543 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref_update->ref);
1544 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1545 3efd8e31 2022-10-23 thomas size_t len;
1546 3efd8e31 2022-10-23 thomas
1547 3efd8e31 2022-10-23 thomas memset(&iref, 0, sizeof(iref));
1548 3efd8e31 2022-10-23 thomas memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1549 3efd8e31 2022-10-23 thomas memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1550 3efd8e31 2022-10-23 thomas iref.ref_is_new = ref_update->ref_is_new;
1551 49563dfb 2023-01-28 thomas iref.delete_ref = ref_update->delete_ref;
1552 3efd8e31 2022-10-23 thomas iref.client_id = client->id;
1553 3efd8e31 2022-10-23 thomas iref.name_len = strlen(refname);
1554 3efd8e31 2022-10-23 thomas
1555 3efd8e31 2022-10-23 thomas len = sizeof(iref) + iref.name_len;
1556 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1557 3efd8e31 2022-10-23 thomas repo_write.pid, len);
1558 3efd8e31 2022-10-23 thomas if (wbuf == NULL)
1559 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_create REF_UPDATE");
1560 3efd8e31 2022-10-23 thomas
1561 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1562 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1563 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, iref.name_len) == -1)
1564 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1565 3efd8e31 2022-10-23 thomas
1566 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1567 3efd8e31 2022-10-23 thomas imsg_close(&iev->ibuf, wbuf);
1568 3efd8e31 2022-10-23 thomas
1569 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
1570 3efd8e31 2022-10-23 thomas return NULL;
1571 3efd8e31 2022-10-23 thomas }
1572 3efd8e31 2022-10-23 thomas
1573 3efd8e31 2022-10-23 thomas static const struct got_error *
1574 9148c8a7 2023-01-02 thomas update_refs(struct gotd_imsgev *iev)
1575 3efd8e31 2022-10-23 thomas {
1576 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1577 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1578 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1579 3efd8e31 2022-10-23 thomas
1580 9148c8a7 2023-01-02 thomas err = send_ref_updates_start(client->nref_updates, iev);
1581 3efd8e31 2022-10-23 thomas if (err)
1582 3efd8e31 2022-10-23 thomas return err;
1583 3efd8e31 2022-10-23 thomas
1584 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1585 9148c8a7 2023-01-02 thomas err = send_ref_update(ref_update, iev);
1586 3efd8e31 2022-10-23 thomas if (err)
1587 3efd8e31 2022-10-23 thomas goto done;
1588 3efd8e31 2022-10-23 thomas }
1589 3efd8e31 2022-10-23 thomas done:
1590 3efd8e31 2022-10-23 thomas return err;
1591 3efd8e31 2022-10-23 thomas }
1592 3efd8e31 2022-10-23 thomas
1593 3efd8e31 2022-10-23 thomas static const struct got_error *
1594 9148c8a7 2023-01-02 thomas receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1595 3efd8e31 2022-10-23 thomas {
1596 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1597 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ireq;
1598 3efd8e31 2022-10-23 thomas size_t datalen;
1599 3efd8e31 2022-10-23 thomas
1600 ff553ea7 2023-04-22 thomas log_debug("receiving pack pipe descriptor");
1601 3efd8e31 2022-10-23 thomas
1602 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1603 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1604 3efd8e31 2022-10-23 thomas
1605 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1606 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
1607 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1608 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1609 3efd8e31 2022-10-23 thomas
1610 9148c8a7 2023-01-02 thomas if (client->pack_pipe != -1)
1611 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1612 3efd8e31 2022-10-23 thomas
1613 9148c8a7 2023-01-02 thomas client->pack_pipe = imsg->fd;
1614 3efd8e31 2022-10-23 thomas return NULL;
1615 3efd8e31 2022-10-23 thomas }
1616 3efd8e31 2022-10-23 thomas
1617 3efd8e31 2022-10-23 thomas static const struct got_error *
1618 9148c8a7 2023-01-02 thomas receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1619 3efd8e31 2022-10-23 thomas {
1620 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1621 3efd8e31 2022-10-23 thomas struct gotd_imsg_packidx_file ireq;
1622 3efd8e31 2022-10-23 thomas size_t datalen;
1623 3efd8e31 2022-10-23 thomas
1624 ff553ea7 2023-04-22 thomas log_debug("receiving pack index output file");
1625 3efd8e31 2022-10-23 thomas
1626 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1627 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1628 3efd8e31 2022-10-23 thomas
1629 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1630 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
1631 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1632 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1633 3efd8e31 2022-10-23 thomas
1634 9148c8a7 2023-01-02 thomas if (client->packidx_fd != -1)
1635 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1636 3efd8e31 2022-10-23 thomas
1637 9148c8a7 2023-01-02 thomas client->packidx_fd = imsg->fd;
1638 3efd8e31 2022-10-23 thomas return NULL;
1639 3efd8e31 2022-10-23 thomas }
1640 3efd8e31 2022-10-23 thomas
1641 3efd8e31 2022-10-23 thomas static void
1642 62ee7d94 2023-01-10 thomas repo_write_dispatch_session(int fd, short event, void *arg)
1643 3efd8e31 2022-10-23 thomas {
1644 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1645 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
1646 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
1647 3efd8e31 2022-10-23 thomas struct imsg imsg;
1648 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1649 3efd8e31 2022-10-23 thomas ssize_t n;
1650 d98779cd 2023-01-19 thomas int shut = 0, have_packfile = 0;
1651 3efd8e31 2022-10-23 thomas
1652 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
1653 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1654 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
1655 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
1656 3efd8e31 2022-10-23 thomas shut = 1;
1657 3efd8e31 2022-10-23 thomas }
1658 3efd8e31 2022-10-23 thomas
1659 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
1660 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
1661 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
1662 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
1663 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
1664 3efd8e31 2022-10-23 thomas shut = 1;
1665 3efd8e31 2022-10-23 thomas }
1666 3efd8e31 2022-10-23 thomas
1667 3efd8e31 2022-10-23 thomas for (;;) {
1668 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1669 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get error", __func__);
1670 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
1671 3efd8e31 2022-10-23 thomas break;
1672 3efd8e31 2022-10-23 thomas
1673 9148c8a7 2023-01-02 thomas if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1674 9148c8a7 2023-01-02 thomas client->id == 0) {
1675 9148c8a7 2023-01-02 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1676 9148c8a7 2023-01-02 thomas break;
1677 9148c8a7 2023-01-02 thomas }
1678 9148c8a7 2023-01-02 thomas
1679 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1680 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS_INTERNAL:
1681 9148c8a7 2023-01-02 thomas err = list_refs(&imsg);
1682 3efd8e31 2022-10-23 thomas if (err)
1683 42f290d4 2023-03-05 thomas log_warnx("ls-refs: %s", err->msg);
1684 3efd8e31 2022-10-23 thomas break;
1685 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
1686 9148c8a7 2023-01-02 thomas err = recv_ref_update(&imsg);
1687 3efd8e31 2022-10-23 thomas if (err)
1688 42f290d4 2023-03-05 thomas log_warnx("ref-update: %s", err->msg);
1689 3efd8e31 2022-10-23 thomas break;
1690 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PIPE:
1691 9148c8a7 2023-01-02 thomas err = receive_pack_pipe(&imsg, iev);
1692 3efd8e31 2022-10-23 thomas if (err) {
1693 42f290d4 2023-03-05 thomas log_warnx("receiving pack pipe: %s", err->msg);
1694 3efd8e31 2022-10-23 thomas break;
1695 3efd8e31 2022-10-23 thomas }
1696 3efd8e31 2022-10-23 thomas break;
1697 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKIDX_FILE:
1698 9148c8a7 2023-01-02 thomas err = receive_pack_idx(&imsg, iev);
1699 3efd8e31 2022-10-23 thomas if (err) {
1700 42f290d4 2023-03-05 thomas log_warnx("receiving pack index: %s",
1701 42f290d4 2023-03-05 thomas err->msg);
1702 3efd8e31 2022-10-23 thomas break;
1703 3efd8e31 2022-10-23 thomas }
1704 3efd8e31 2022-10-23 thomas break;
1705 3efd8e31 2022-10-23 thomas case GOTD_IMSG_RECV_PACKFILE:
1706 6d7eb4f7 2023-04-04 thomas err = protect_refs_from_deletion();
1707 6d7eb4f7 2023-04-04 thomas if (err)
1708 6d7eb4f7 2023-04-04 thomas break;
1709 d98779cd 2023-01-19 thomas err = recv_packfile(&have_packfile, &imsg);
1710 3efd8e31 2022-10-23 thomas if (err) {
1711 42f290d4 2023-03-05 thomas log_warnx("receive packfile: %s", err->msg);
1712 3efd8e31 2022-10-23 thomas break;
1713 3efd8e31 2022-10-23 thomas }
1714 d98779cd 2023-01-19 thomas if (have_packfile) {
1715 d98779cd 2023-01-19 thomas err = verify_packfile();
1716 d98779cd 2023-01-19 thomas if (err) {
1717 42f290d4 2023-03-05 thomas log_warnx("verify packfile: %s",
1718 42f290d4 2023-03-05 thomas err->msg);
1719 d98779cd 2023-01-19 thomas break;
1720 d98779cd 2023-01-19 thomas }
1721 d98779cd 2023-01-19 thomas err = install_packfile(iev);
1722 d98779cd 2023-01-19 thomas if (err) {
1723 42f290d4 2023-03-05 thomas log_warnx("install packfile: %s",
1724 42f290d4 2023-03-05 thomas err->msg);
1725 d98779cd 2023-01-19 thomas break;
1726 d98779cd 2023-01-19 thomas }
1727 3efd8e31 2022-10-23 thomas }
1728 9148c8a7 2023-01-02 thomas err = update_refs(iev);
1729 3efd8e31 2022-10-23 thomas if (err) {
1730 42f290d4 2023-03-05 thomas log_warnx("update refs: %s", err->msg);
1731 3efd8e31 2022-10-23 thomas }
1732 3efd8e31 2022-10-23 thomas break;
1733 62ee7d94 2023-01-10 thomas default:
1734 42f290d4 2023-03-05 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1735 62ee7d94 2023-01-10 thomas break;
1736 62ee7d94 2023-01-10 thomas }
1737 62ee7d94 2023-01-10 thomas
1738 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1739 62ee7d94 2023-01-10 thomas }
1740 62ee7d94 2023-01-10 thomas
1741 62ee7d94 2023-01-10 thomas if (!shut && check_cancelled(NULL) == NULL) {
1742 62ee7d94 2023-01-10 thomas if (err &&
1743 62ee7d94 2023-01-10 thomas gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1744 62ee7d94 2023-01-10 thomas client->id, err) == -1) {
1745 62ee7d94 2023-01-10 thomas log_warnx("could not send error to parent: %s",
1746 62ee7d94 2023-01-10 thomas err->msg);
1747 62ee7d94 2023-01-10 thomas }
1748 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1749 62ee7d94 2023-01-10 thomas } else {
1750 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1751 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1752 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
1753 62ee7d94 2023-01-10 thomas }
1754 62ee7d94 2023-01-10 thomas }
1755 62ee7d94 2023-01-10 thomas
1756 62ee7d94 2023-01-10 thomas static const struct got_error *
1757 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
1758 62ee7d94 2023-01-10 thomas {
1759 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &repo_write.session_iev;
1760 62ee7d94 2023-01-10 thomas size_t datalen;
1761 62ee7d94 2023-01-10 thomas
1762 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1763 62ee7d94 2023-01-10 thomas if (datalen != 0)
1764 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1765 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1766 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1767 62ee7d94 2023-01-10 thomas
1768 62ee7d94 2023-01-10 thomas if (repo_write.session_fd != -1)
1769 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1770 62ee7d94 2023-01-10 thomas
1771 62ee7d94 2023-01-10 thomas repo_write.session_fd = imsg->fd;
1772 62ee7d94 2023-01-10 thomas
1773 62ee7d94 2023-01-10 thomas imsg_init(&iev->ibuf, repo_write.session_fd);
1774 62ee7d94 2023-01-10 thomas iev->handler = repo_write_dispatch_session;
1775 62ee7d94 2023-01-10 thomas iev->events = EV_READ;
1776 62ee7d94 2023-01-10 thomas iev->handler_arg = NULL;
1777 62ee7d94 2023-01-10 thomas event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1778 62ee7d94 2023-01-10 thomas repo_write_dispatch_session, iev);
1779 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1780 62ee7d94 2023-01-10 thomas
1781 62ee7d94 2023-01-10 thomas return NULL;
1782 62ee7d94 2023-01-10 thomas }
1783 62ee7d94 2023-01-10 thomas
1784 62ee7d94 2023-01-10 thomas static void
1785 62ee7d94 2023-01-10 thomas repo_write_dispatch(int fd, short event, void *arg)
1786 62ee7d94 2023-01-10 thomas {
1787 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1788 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1789 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1790 62ee7d94 2023-01-10 thomas struct imsg imsg;
1791 62ee7d94 2023-01-10 thomas ssize_t n;
1792 62ee7d94 2023-01-10 thomas int shut = 0;
1793 62ee7d94 2023-01-10 thomas struct repo_write_client *client = &repo_write_client;
1794 62ee7d94 2023-01-10 thomas
1795 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1796 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1797 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1798 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
1799 9148c8a7 2023-01-02 thomas shut = 1;
1800 62ee7d94 2023-01-10 thomas }
1801 62ee7d94 2023-01-10 thomas
1802 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1803 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1804 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1805 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1806 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
1807 62ee7d94 2023-01-10 thomas shut = 1;
1808 62ee7d94 2023-01-10 thomas }
1809 62ee7d94 2023-01-10 thomas
1810 62ee7d94 2023-01-10 thomas while (err == NULL && check_cancelled(NULL) == NULL) {
1811 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1812 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get", __func__);
1813 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1814 3efd8e31 2022-10-23 thomas break;
1815 62ee7d94 2023-01-10 thomas
1816 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1817 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
1818 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
1819 62ee7d94 2023-01-10 thomas break;
1820 3efd8e31 2022-10-23 thomas default:
1821 42f290d4 2023-03-05 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1822 3efd8e31 2022-10-23 thomas break;
1823 3efd8e31 2022-10-23 thomas }
1824 3efd8e31 2022-10-23 thomas
1825 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1826 3efd8e31 2022-10-23 thomas }
1827 3efd8e31 2022-10-23 thomas
1828 3efd8e31 2022-10-23 thomas if (!shut && check_cancelled(NULL) == NULL) {
1829 3efd8e31 2022-10-23 thomas if (err &&
1830 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1831 9148c8a7 2023-01-02 thomas client->id, err) == -1) {
1832 3efd8e31 2022-10-23 thomas log_warnx("could not send error to parent: %s",
1833 3efd8e31 2022-10-23 thomas err->msg);
1834 3efd8e31 2022-10-23 thomas }
1835 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
1836 3efd8e31 2022-10-23 thomas } else {
1837 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
1838 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
1839 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
1840 3efd8e31 2022-10-23 thomas }
1841 3efd8e31 2022-10-23 thomas }
1842 3efd8e31 2022-10-23 thomas
1843 3efd8e31 2022-10-23 thomas void
1844 414e37cb 2022-12-30 thomas repo_write_main(const char *title, const char *repo_path,
1845 6d7eb4f7 2023-04-04 thomas int *pack_fds, int *temp_fds,
1846 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *protected_tag_namespaces,
1847 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *protected_branch_namespaces,
1848 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *protected_branches)
1849 3efd8e31 2022-10-23 thomas {
1850 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1851 92db09ff 2023-02-17 thomas struct repo_write_client *client = &repo_write_client;
1852 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
1853 3efd8e31 2022-10-23 thomas
1854 92db09ff 2023-02-17 thomas client->fd = -1;
1855 92db09ff 2023-02-17 thomas client->pack_pipe = -1;
1856 92db09ff 2023-02-17 thomas client->packidx_fd = -1;
1857 92db09ff 2023-02-17 thomas client->pack.fd = -1;
1858 92db09ff 2023-02-17 thomas
1859 3efd8e31 2022-10-23 thomas repo_write.title = title;
1860 3efd8e31 2022-10-23 thomas repo_write.pid = getpid();
1861 3efd8e31 2022-10-23 thomas repo_write.pack_fds = pack_fds;
1862 3efd8e31 2022-10-23 thomas repo_write.temp_fds = temp_fds;
1863 62ee7d94 2023-01-10 thomas repo_write.session_fd = -1;
1864 62ee7d94 2023-01-10 thomas repo_write.session_iev.ibuf.fd = -1;
1865 6d7eb4f7 2023-04-04 thomas repo_write.protected_tag_namespaces = protected_tag_namespaces;
1866 6d7eb4f7 2023-04-04 thomas repo_write.protected_branch_namespaces = protected_branch_namespaces;
1867 6d7eb4f7 2023-04-04 thomas repo_write.protected_branches = protected_branches;
1868 3efd8e31 2022-10-23 thomas
1869 9148c8a7 2023-01-02 thomas STAILQ_INIT(&repo_write_client.ref_updates);
1870 3efd8e31 2022-10-23 thomas
1871 414e37cb 2022-12-30 thomas err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1872 3efd8e31 2022-10-23 thomas if (err)
1873 3efd8e31 2022-10-23 thomas goto done;
1874 3efd8e31 2022-10-23 thomas if (!got_repo_is_bare(repo_write.repo)) {
1875 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1876 3efd8e31 2022-10-23 thomas "bare git repository required");
1877 3efd8e31 2022-10-23 thomas goto done;
1878 3efd8e31 2022-10-23 thomas }
1879 3efd8e31 2022-10-23 thomas
1880 3efd8e31 2022-10-23 thomas got_repo_temp_fds_set(repo_write.repo, temp_fds);
1881 3efd8e31 2022-10-23 thomas
1882 3efd8e31 2022-10-23 thomas signal(SIGINT, catch_sigint);
1883 3efd8e31 2022-10-23 thomas signal(SIGTERM, catch_sigterm);
1884 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
1885 3efd8e31 2022-10-23 thomas signal(SIGHUP, SIG_IGN);
1886 3efd8e31 2022-10-23 thomas
1887 bb3a6ce9 2022-11-17 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1888 3efd8e31 2022-10-23 thomas iev.handler = repo_write_dispatch;
1889 3efd8e31 2022-10-23 thomas iev.events = EV_READ;
1890 3efd8e31 2022-10-23 thomas iev.handler_arg = NULL;
1891 3efd8e31 2022-10-23 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1892 85b37c72 2022-12-30 thomas if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1893 85b37c72 2022-12-30 thomas PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1894 85b37c72 2022-12-30 thomas err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1895 3efd8e31 2022-10-23 thomas goto done;
1896 3efd8e31 2022-10-23 thomas }
1897 3efd8e31 2022-10-23 thomas
1898 3efd8e31 2022-10-23 thomas event_dispatch();
1899 3efd8e31 2022-10-23 thomas done:
1900 3efd8e31 2022-10-23 thomas if (err)
1901 3efd8e31 2022-10-23 thomas log_warnx("%s: %s", title, err->msg);
1902 3efd8e31 2022-10-23 thomas repo_write_shutdown();
1903 3efd8e31 2022-10-23 thomas }
1904 3efd8e31 2022-10-23 thomas
1905 3efd8e31 2022-10-23 thomas void
1906 3efd8e31 2022-10-23 thomas repo_write_shutdown(void)
1907 3efd8e31 2022-10-23 thomas {
1908 92db09ff 2023-02-17 thomas struct repo_write_client *client = &repo_write_client;
1909 92db09ff 2023-02-17 thomas struct gotd_ref_update *ref_update;
1910 92db09ff 2023-02-17 thomas
1911 7161c4dc 2023-03-05 thomas log_debug("shutting down");
1912 92db09ff 2023-02-17 thomas
1913 92db09ff 2023-02-17 thomas while (!STAILQ_EMPTY(&client->ref_updates)) {
1914 92db09ff 2023-02-17 thomas ref_update = STAILQ_FIRST(&client->ref_updates);
1915 92db09ff 2023-02-17 thomas STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1916 92db09ff 2023-02-17 thomas got_ref_close(ref_update->ref);
1917 92db09ff 2023-02-17 thomas free(ref_update);
1918 92db09ff 2023-02-17 thomas }
1919 92db09ff 2023-02-17 thomas
1920 92db09ff 2023-02-17 thomas got_pack_close(&client->pack);
1921 92db09ff 2023-02-17 thomas if (client->fd != -1)
1922 92db09ff 2023-02-17 thomas close(client->fd);
1923 92db09ff 2023-02-17 thomas if (client->pack_pipe != -1)
1924 92db09ff 2023-02-17 thomas close(client->pack_pipe);
1925 92db09ff 2023-02-17 thomas if (client->packidx_fd != -1)
1926 92db09ff 2023-02-17 thomas close(client->packidx_fd);
1927 92db09ff 2023-02-17 thomas
1928 3efd8e31 2022-10-23 thomas if (repo_write.repo)
1929 3efd8e31 2022-10-23 thomas got_repo_close(repo_write.repo);
1930 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(repo_write.pack_fds);
1931 80536967 2022-10-30 thomas got_repo_temp_fds_close(repo_write.temp_fds);
1932 62ee7d94 2023-01-10 thomas if (repo_write.session_fd != -1)
1933 62ee7d94 2023-01-10 thomas close(repo_write.session_fd);
1934 3efd8e31 2022-10-23 thomas exit(0);
1935 3efd8e31 2022-10-23 thomas }