Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 93658fb9 2020-03-18 stsp *
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
7 93658fb9 2020-03-18 stsp *
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 93658fb9 2020-03-18 stsp */
16 d9b944c7 2023-03-08 thomas
17 d9b944c7 2023-03-08 thomas #include "got_compat.h"
18 93658fb9 2020-03-18 stsp
19 93658fb9 2020-03-18 stsp #include <sys/types.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 93658fb9 2020-03-18 stsp #include <sys/uio.h>
22 93658fb9 2020-03-18 stsp #include <sys/time.h>
23 93658fb9 2020-03-18 stsp #include <sys/stat.h>
24 93658fb9 2020-03-18 stsp
25 93658fb9 2020-03-18 stsp #include <stdint.h>
26 93658fb9 2020-03-18 stsp #include <errno.h>
27 93658fb9 2020-03-18 stsp #include <limits.h>
28 93658fb9 2020-03-18 stsp #include <signal.h>
29 93658fb9 2020-03-18 stsp #include <stdio.h>
30 93658fb9 2020-03-18 stsp #include <stdlib.h>
31 93658fb9 2020-03-18 stsp #include <string.h>
32 93658fb9 2020-03-18 stsp #include <ctype.h>
33 93658fb9 2020-03-18 stsp #include <fcntl.h>
34 81a12da5 2020-09-09 naddy #include <unistd.h>
35 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <err.h>
37 93658fb9 2020-03-18 stsp
38 93658fb9 2020-03-18 stsp #include "got_error.h"
39 93658fb9 2020-03-18 stsp #include "got_object.h"
40 abe0f35f 2020-03-18 stsp #include "got_path.h"
41 8a29a085 2020-03-18 stsp #include "got_version.h"
42 f1c6967f 2020-03-19 stsp #include "got_fetch.h"
43 659e7fbd 2020-03-20 stsp #include "got_reference.h"
44 93658fb9 2020-03-18 stsp
45 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
50 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
51 f024663d 2021-09-05 stsp #include "got_lib_pkt.h"
52 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
53 b637eb2e 2022-02-23 thomas #include "got_lib_ratelimit.h"
54 9439b99a 2022-10-30 thomas
55 9439b99a 2022-10-30 thomas #ifndef MIN
56 9439b99a 2022-10-30 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 9439b99a 2022-10-30 thomas #endif
58 93658fb9 2020-03-18 stsp
59 8a29a085 2020-03-18 stsp #ifndef nitems
60 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 8a29a085 2020-03-18 stsp #endif
62 8a29a085 2020-03-18 stsp
63 93658fb9 2020-03-18 stsp struct got_object *indexed;
64 858b0dfb 2020-03-20 stsp static int chattygot;
65 93658fb9 2020-03-18 stsp
66 bd3d9e54 2021-09-05 stsp static const struct got_capability got_capabilities[] = {
67 bd3d9e54 2021-09-05 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
68 bd3d9e54 2021-09-05 stsp { GOT_CAPA_OFS_DELTA, NULL },
69 bd3d9e54 2021-09-05 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
70 bd3d9e54 2021-09-05 stsp };
71 bd3d9e54 2021-09-05 stsp
72 7848a0e1 2020-03-19 stsp static void
73 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
74 9d0a7ee3 2023-02-07 thomas struct got_object_id *my_id, const char *refname)
75 93658fb9 2020-03-18 stsp {
76 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
77 93658fb9 2020-03-18 stsp
78 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
79 7848a0e1 2020-03-19 stsp * we should use a flag instead */
80 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
81 93658fb9 2020-03-18 stsp
82 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
83 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
84 b6b86fd1 2022-08-30 thomas if (strcmp(pe->path, refname) == 0) {
85 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
86 33501562 2020-03-18 stsp break;
87 33501562 2020-03-18 stsp }
88 93658fb9 2020-03-18 stsp }
89 93658fb9 2020-03-18 stsp }
90 93658fb9 2020-03-18 stsp
91 93658fb9 2020-03-18 stsp static int
92 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
93 93658fb9 2020-03-18 stsp {
94 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
95 659e7fbd 2020-03-20 stsp return 0;
96 93658fb9 2020-03-18 stsp
97 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
98 659e7fbd 2020-03-20 stsp wanted_branch += 11;
99 659e7fbd 2020-03-20 stsp
100 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
101 0e4002ca 2020-03-21 stsp }
102 0e4002ca 2020-03-21 stsp
103 0e4002ca 2020-03-21 stsp static int
104 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
105 0e4002ca 2020-03-21 stsp {
106 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
107 0e4002ca 2020-03-21 stsp return 0;
108 0e4002ca 2020-03-21 stsp refname += 5;
109 0e4002ca 2020-03-21 stsp
110 0e4002ca 2020-03-21 stsp /*
111 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
112 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
113 0e4002ca 2020-03-21 stsp */
114 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
115 0e4002ca 2020-03-21 stsp return 0;
116 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
117 0e4002ca 2020-03-21 stsp return 0;
118 0e4002ca 2020-03-21 stsp
119 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
120 0e4002ca 2020-03-21 stsp wanted_ref += 5;
121 0e4002ca 2020-03-21 stsp
122 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
123 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
124 0e4002ca 2020-03-21 stsp return 1;
125 0e4002ca 2020-03-21 stsp
126 0e4002ca 2020-03-21 stsp /* Allow exact match. */
127 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
128 abe0f35f 2020-03-18 stsp }
129 abe0f35f 2020-03-18 stsp
130 abe0f35f 2020-03-18 stsp static const struct got_error *
131 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
132 e70bf110 2020-03-22 stsp {
133 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
134 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
135 e70bf110 2020-03-22 stsp
136 e70bf110 2020-03-22 stsp if (msglen == 0)
137 e70bf110 2020-03-22 stsp return NULL;
138 e70bf110 2020-03-22 stsp
139 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
140 e70bf110 2020-03-22 stsp msg, msglen) == -1)
141 e70bf110 2020-03-22 stsp return got_error_from_errno(
142 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
143 e70bf110 2020-03-22 stsp
144 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
145 531c3985 2020-03-18 stsp }
146 531c3985 2020-03-18 stsp
147 531c3985 2020-03-18 stsp static const struct got_error *
148 b637eb2e 2022-02-23 thomas send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
149 b637eb2e 2022-02-23 thomas struct got_ratelimit *rl)
150 e70bf110 2020-03-22 stsp {
151 b637eb2e 2022-02-23 thomas const struct got_error *err;
152 b637eb2e 2022-02-23 thomas int elapsed = 0;
153 b637eb2e 2022-02-23 thomas
154 b637eb2e 2022-02-23 thomas if (rl) {
155 b637eb2e 2022-02-23 thomas err = got_ratelimit_check(&elapsed, rl);
156 b637eb2e 2022-02-23 thomas if (err || !elapsed)
157 b637eb2e 2022-02-23 thomas return err;
158 b637eb2e 2022-02-23 thomas }
159 b637eb2e 2022-02-23 thomas
160 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
161 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
162 e70bf110 2020-03-22 stsp return got_error_from_errno(
163 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
164 e70bf110 2020-03-22 stsp
165 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
166 e70bf110 2020-03-22 stsp }
167 e70bf110 2020-03-22 stsp
168 e70bf110 2020-03-22 stsp static const struct got_error *
169 1d72a2a0 2020-03-24 stsp send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
170 e70bf110 2020-03-22 stsp {
171 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
172 1d72a2a0 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1)
173 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
174 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
175 e70bf110 2020-03-22 stsp }
176 e70bf110 2020-03-22 stsp
177 e70bf110 2020-03-22 stsp static const struct got_error *
178 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
179 531c3985 2020-03-18 stsp {
180 6059809a 2020-12-17 stsp size_t i;
181 531c3985 2020-03-18 stsp
182 531c3985 2020-03-18 stsp if (len == 0)
183 531c3985 2020-03-18 stsp return NULL;
184 531c3985 2020-03-18 stsp
185 531c3985 2020-03-18 stsp /*
186 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
187 531c3985 2020-03-18 stsp * Server may send up to 64k.
188 531c3985 2020-03-18 stsp */
189 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
191 531c3985 2020-03-18 stsp
192 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
193 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
194 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
195 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
196 531c3985 2020-03-18 stsp continue;
197 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
198 531c3985 2020-03-18 stsp "non-printable progress message received from server");
199 531c3985 2020-03-18 stsp }
200 531c3985 2020-03-18 stsp
201 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
202 8a29a085 2020-03-18 stsp }
203 abe0f35f 2020-03-18 stsp
204 8a29a085 2020-03-18 stsp static const struct got_error *
205 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
206 531c3985 2020-03-18 stsp {
207 531c3985 2020-03-18 stsp static char msg[1024];
208 6059809a 2020-12-17 stsp size_t i;
209 531c3985 2020-03-18 stsp
210 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
211 6771d425 2022-11-17 thomas if (!isprint((unsigned char)buf[i]))
212 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
213 531c3985 2020-03-18 stsp "non-printable error message received from server");
214 531c3985 2020-03-18 stsp msg[i] = buf[i];
215 531c3985 2020-03-18 stsp }
216 531c3985 2020-03-18 stsp msg[i] = '\0';
217 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
218 531c3985 2020-03-18 stsp }
219 531c3985 2020-03-18 stsp
220 531c3985 2020-03-18 stsp static const struct got_error *
221 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
222 e70bf110 2020-03-22 stsp {
223 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
224 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
225 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
226 e70bf110 2020-03-22 stsp
227 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
228 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
229 e70bf110 2020-03-22 stsp const char *target = pe->data;
230 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
231 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
232 e70bf110 2020-03-22 stsp nsymrefs++;
233 e70bf110 2020-03-22 stsp }
234 e70bf110 2020-03-22 stsp
235 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
236 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
237 e70bf110 2020-03-22 stsp
238 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
239 e70bf110 2020-03-22 stsp if (wbuf == NULL)
240 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
241 e70bf110 2020-03-22 stsp
242 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
243 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
244 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
245 e70bf110 2020-03-22 stsp
246 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
247 e70bf110 2020-03-22 stsp const char *name = pe->path;
248 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
249 e70bf110 2020-03-22 stsp const char *target = pe->data;
250 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
251 e70bf110 2020-03-22 stsp
252 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
253 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
254 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
255 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
256 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, name, name_len) == -1)
258 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, target, target_len) == -1)
260 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
261 e70bf110 2020-03-22 stsp }
262 e70bf110 2020-03-22 stsp
263 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
264 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
265 e70bf110 2020-03-22 stsp }
266 e70bf110 2020-03-22 stsp
267 e70bf110 2020-03-22 stsp static const struct got_error *
268 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
269 e70bf110 2020-03-22 stsp const char *refname)
270 e70bf110 2020-03-22 stsp {
271 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
272 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
273 e70bf110 2020-03-22 stsp
274 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
275 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
276 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
277 e70bf110 2020-03-22 stsp
278 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
279 e70bf110 2020-03-22 stsp if (wbuf == NULL)
280 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
281 e70bf110 2020-03-22 stsp
282 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
283 9228a15c 2023-02-03 thomas if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
284 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_REF");
285 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refname, reflen) == -1)
286 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_REF");
287 e70bf110 2020-03-22 stsp
288 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
289 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
290 9d0a7ee3 2023-02-07 thomas }
291 9d0a7ee3 2023-02-07 thomas
292 9d0a7ee3 2023-02-07 thomas static const struct got_error *
293 9d0a7ee3 2023-02-07 thomas fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
294 9d0a7ee3 2023-02-07 thomas struct got_object_id *have, struct got_object_id *want,
295 9d0a7ee3 2023-02-07 thomas const char *refname, const char *id_str)
296 9d0a7ee3 2023-02-07 thomas {
297 9d0a7ee3 2023-02-07 thomas const struct got_error *err;
298 9d0a7ee3 2023-02-07 thomas char *theirs = NULL, *mine = NULL;
299 9d0a7ee3 2023-02-07 thomas
300 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(want, id_str, GOT_HASH_SHA1)) {
301 9d0a7ee3 2023-02-07 thomas err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
302 9d0a7ee3 2023-02-07 thomas goto done;
303 9d0a7ee3 2023-02-07 thomas }
304 9d0a7ee3 2023-02-07 thomas
305 9d0a7ee3 2023-02-07 thomas match_remote_ref(have_refs, have, refname);
306 9d0a7ee3 2023-02-07 thomas err = send_fetch_ref(ibuf, want, refname);
307 9d0a7ee3 2023-02-07 thomas if (err)
308 9d0a7ee3 2023-02-07 thomas goto done;
309 9d0a7ee3 2023-02-07 thomas
310 9d0a7ee3 2023-02-07 thomas if (chattygot)
311 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: %s will be fetched\n",
312 9d0a7ee3 2023-02-07 thomas getprogname(), refname);
313 9d0a7ee3 2023-02-07 thomas if (chattygot > 1) {
314 9d0a7ee3 2023-02-07 thomas err = got_object_id_str(&theirs, want);
315 9d0a7ee3 2023-02-07 thomas if (err)
316 9d0a7ee3 2023-02-07 thomas goto done;
317 9d0a7ee3 2023-02-07 thomas err = got_object_id_str(&mine, have);
318 9d0a7ee3 2023-02-07 thomas if (err)
319 9d0a7ee3 2023-02-07 thomas goto done;
320 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
321 9d0a7ee3 2023-02-07 thomas getprogname(), theirs, getprogname(), mine);
322 9d0a7ee3 2023-02-07 thomas }
323 9d0a7ee3 2023-02-07 thomas done:
324 9d0a7ee3 2023-02-07 thomas free(theirs);
325 9d0a7ee3 2023-02-07 thomas free(mine);
326 9d0a7ee3 2023-02-07 thomas return err;
327 e70bf110 2020-03-22 stsp }
328 729743d1 2020-03-23 stsp
329 e70bf110 2020-03-22 stsp static const struct got_error *
330 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
331 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
332 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
333 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
334 7abf1863 2023-02-20 thomas const char *worktree_branch, const char *remote_head,
335 7abf1863 2023-02-20 thomas int no_head, struct imsgbuf *ibuf)
336 93658fb9 2020-03-18 stsp {
337 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
338 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
339 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
340 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
341 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
342 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
343 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
344 7917d6d5 2023-02-17 thomas char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
345 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
346 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
347 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
348 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
349 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
350 7917d6d5 2023-02-17 thomas int found_branch = 0;
351 b16893ba 2023-02-24 thomas struct got_hash ctx;
352 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
353 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
354 dc671e91 2020-03-24 stsp ssize_t w;
355 b637eb2e 2022-02-23 thomas struct got_ratelimit rl;
356 93658fb9 2020-03-18 stsp
357 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
358 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
359 b637eb2e 2022-02-23 thomas got_ratelimit_init(&rl, 0, 500);
360 abe0f35f 2020-03-18 stsp
361 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
362 9ff10419 2020-03-18 stsp if (have == NULL)
363 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
364 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
365 9ff10419 2020-03-18 stsp if (want == NULL) {
366 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
367 9ff10419 2020-03-18 stsp goto done;
368 9ff10419 2020-03-18 stsp }
369 9ff10419 2020-03-18 stsp while (1) {
370 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
371 fe53745c 2020-03-18 stsp if (err)
372 9ff10419 2020-03-18 stsp goto done;
373 9ff10419 2020-03-18 stsp if (n == 0)
374 93658fb9 2020-03-18 stsp break;
375 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
376 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
377 9ff10419 2020-03-18 stsp goto done;
378 9ff10419 2020-03-18 stsp }
379 35add24a 2021-10-08 thomas free(id_str);
380 35add24a 2021-10-08 thomas free(refname);
381 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
382 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
383 0d0a341c 2020-03-18 stsp if (err)
384 9ff10419 2020-03-18 stsp goto done;
385 9d0a7ee3 2023-02-07 thomas
386 9d0a7ee3 2023-02-07 thomas if (refsz == nref + 1) {
387 84073f62 2023-02-07 thomas struct got_object_id *h, *w;
388 84073f62 2023-02-07 thomas
389 9d0a7ee3 2023-02-07 thomas refsz *= 2;
390 84073f62 2023-02-07 thomas h = reallocarray(have, refsz, sizeof(have[0]));
391 84073f62 2023-02-07 thomas if (h == NULL) {
392 9d0a7ee3 2023-02-07 thomas err = got_error_from_errno("reallocarray");
393 9d0a7ee3 2023-02-07 thomas goto done;
394 9d0a7ee3 2023-02-07 thomas }
395 84073f62 2023-02-07 thomas have = h;
396 84073f62 2023-02-07 thomas w = reallocarray(want, refsz, sizeof(want[0]));
397 84073f62 2023-02-07 thomas if (w == NULL) {
398 9d0a7ee3 2023-02-07 thomas err = got_error_from_errno("reallocarray");
399 9d0a7ee3 2023-02-07 thomas goto done;
400 9d0a7ee3 2023-02-07 thomas }
401 84073f62 2023-02-07 thomas want = w;
402 9d0a7ee3 2023-02-07 thomas }
403 9d0a7ee3 2023-02-07 thomas
404 8a29a085 2020-03-18 stsp if (is_firstpkt) {
405 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
406 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
407 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
408 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
409 bd3d9e54 2021-09-05 stsp &symrefs, server_capabilities,
410 bd3d9e54 2021-09-05 stsp got_capabilities, nitems(got_capabilities));
411 8a29a085 2020-03-18 stsp if (err)
412 8a29a085 2020-03-18 stsp goto done;
413 858b0dfb 2020-03-20 stsp if (chattygot)
414 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
415 a90356f7 2021-08-26 stsp getprogname(), my_capabilities != NULL ?
416 a90356f7 2021-08-26 stsp my_capabilities : "");
417 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
418 abe0f35f 2020-03-18 stsp if (err)
419 abe0f35f 2020-03-18 stsp goto done;
420 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
421 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
422 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
423 659e7fbd 2020-03-20 stsp const char *name = pe->path;
424 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
425 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
426 659e7fbd 2020-03-20 stsp continue;
427 659e7fbd 2020-03-20 stsp default_branch = symref_target;
428 659e7fbd 2020-03-20 stsp break;
429 659e7fbd 2020-03-20 stsp }
430 659e7fbd 2020-03-20 stsp }
431 c22ed3f5 2022-11-08 thomas if (default_branch)
432 c22ed3f5 2022-11-08 thomas continue;
433 8a29a085 2020-03-18 stsp }
434 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
435 2690194b 2020-03-21 stsp if (chattygot) {
436 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
437 2690194b 2020-03-21 stsp getprogname(), refname);
438 2690194b 2020-03-21 stsp }
439 93658fb9 2020-03-18 stsp continue;
440 2690194b 2020-03-21 stsp }
441 7917d6d5 2023-02-17 thomas if (default_branch && default_id_str == NULL &&
442 7917d6d5 2023-02-17 thomas strcmp(refname, default_branch) == 0) {
443 7917d6d5 2023-02-17 thomas default_id_str = strdup(id_str);
444 7917d6d5 2023-02-17 thomas if (default_id_str == NULL) {
445 7917d6d5 2023-02-17 thomas err = got_error_from_errno("strdup");
446 9ff10419 2020-03-18 stsp goto done;
447 7917d6d5 2023-02-17 thomas }
448 93658fb9 2020-03-18 stsp }
449 858b0dfb 2020-03-20 stsp
450 9d0a7ee3 2023-02-07 thomas if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
451 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
452 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
453 858b0dfb 2020-03-20 stsp if (err)
454 858b0dfb 2020-03-20 stsp goto done;
455 9d0a7ee3 2023-02-07 thomas nref++;
456 9d0a7ee3 2023-02-07 thomas } else if (strncmp(refname, "refs/heads/", 11) == 0) {
457 9d0a7ee3 2023-02-07 thomas if (fetch_all_branches) {
458 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
459 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
460 9d0a7ee3 2023-02-07 thomas if (err)
461 9d0a7ee3 2023-02-07 thomas goto done;
462 9d0a7ee3 2023-02-07 thomas nref++;
463 7917d6d5 2023-02-17 thomas found_branch = 1;
464 9d0a7ee3 2023-02-07 thomas continue;
465 858b0dfb 2020-03-20 stsp }
466 9d0a7ee3 2023-02-07 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
467 9d0a7ee3 2023-02-07 thomas if (match_branch(refname, pe->path))
468 9d0a7ee3 2023-02-07 thomas break;
469 9d0a7ee3 2023-02-07 thomas }
470 9d0a7ee3 2023-02-07 thomas if (pe != NULL || (worktree_branch != NULL &&
471 9d0a7ee3 2023-02-07 thomas match_branch(refname, worktree_branch))) {
472 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
473 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
474 9d0a7ee3 2023-02-07 thomas if (err)
475 9d0a7ee3 2023-02-07 thomas goto done;
476 9d0a7ee3 2023-02-07 thomas nref++;
477 7917d6d5 2023-02-17 thomas found_branch = 1;
478 9d0a7ee3 2023-02-07 thomas } else if (chattygot) {
479 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: ignoring %s\n",
480 9d0a7ee3 2023-02-07 thomas getprogname(), refname);
481 9d0a7ee3 2023-02-07 thomas }
482 9d0a7ee3 2023-02-07 thomas } else {
483 9d0a7ee3 2023-02-07 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
484 9d0a7ee3 2023-02-07 thomas if (match_wanted_ref(refname, pe->path))
485 9d0a7ee3 2023-02-07 thomas break;
486 9d0a7ee3 2023-02-07 thomas }
487 9d0a7ee3 2023-02-07 thomas if (pe != NULL) {
488 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
489 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
490 9d0a7ee3 2023-02-07 thomas if (err)
491 9d0a7ee3 2023-02-07 thomas goto done;
492 9d0a7ee3 2023-02-07 thomas nref++;
493 9d0a7ee3 2023-02-07 thomas } else if (chattygot) {
494 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: ignoring %s\n",
495 9d0a7ee3 2023-02-07 thomas getprogname(), refname);
496 9d0a7ee3 2023-02-07 thomas }
497 858b0dfb 2020-03-20 stsp }
498 93658fb9 2020-03-18 stsp }
499 93658fb9 2020-03-18 stsp
500 41b0de12 2020-03-21 stsp if (list_refs_only)
501 41b0de12 2020-03-21 stsp goto done;
502 7917d6d5 2023-02-17 thomas
503 7abf1863 2023-02-20 thomas /*
504 7abf1863 2023-02-20 thomas * If -b was not used and either none of the requested branches
505 7abf1863 2023-02-20 thomas * (got.conf, worktree) were found or the client already has the
506 7abf1863 2023-02-20 thomas * remote HEAD symref but its target changed, fetch remote's HEAD.
507 7abf1863 2023-02-20 thomas */
508 7abf1863 2023-02-20 thomas if (!no_head && default_branch && default_id_str &&
509 7917d6d5 2023-02-17 thomas strncmp(default_branch, "refs/heads/", 11) == 0) {
510 7abf1863 2023-02-20 thomas int remote_head_changed = 0;
511 7abf1863 2023-02-20 thomas
512 7abf1863 2023-02-20 thomas if (remote_head) {
513 7abf1863 2023-02-20 thomas if (strcmp(remote_head, default_branch + 11) != 0)
514 7abf1863 2023-02-20 thomas remote_head_changed = 1;
515 7abf1863 2023-02-20 thomas }
516 7abf1863 2023-02-20 thomas
517 7abf1863 2023-02-20 thomas if (!found_branch || remote_head_changed) {
518 7abf1863 2023-02-20 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
519 7abf1863 2023-02-20 thomas &want[nref], default_branch, default_id_str);
520 7abf1863 2023-02-20 thomas if (err)
521 7abf1863 2023-02-20 thomas goto done;
522 7abf1863 2023-02-20 thomas nref++;
523 7abf1863 2023-02-20 thomas }
524 7917d6d5 2023-02-17 thomas }
525 9d0a7ee3 2023-02-07 thomas
526 9d0a7ee3 2023-02-07 thomas /* Abort if we haven't found anything to fetch. */
527 9d0a7ee3 2023-02-07 thomas if (nref == 0) {
528 d392cc0a 2023-02-17 thomas struct got_pathlist_entry *pe;
529 d392cc0a 2023-02-17 thomas static char msg[PATH_MAX + 33];
530 d392cc0a 2023-02-17 thomas
531 d392cc0a 2023-02-17 thomas pe = TAILQ_FIRST(wanted_branches);
532 d392cc0a 2023-02-17 thomas if (pe) {
533 d392cc0a 2023-02-17 thomas snprintf(msg, sizeof(msg),
534 d392cc0a 2023-02-17 thomas "branch \"%s\" not found on server", pe->path);
535 d392cc0a 2023-02-17 thomas err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
536 d392cc0a 2023-02-17 thomas goto done;
537 d392cc0a 2023-02-17 thomas }
538 d392cc0a 2023-02-17 thomas
539 d392cc0a 2023-02-17 thomas pe = TAILQ_FIRST(wanted_refs);
540 d392cc0a 2023-02-17 thomas if (pe) {
541 d392cc0a 2023-02-17 thomas snprintf(msg, sizeof(msg),
542 d392cc0a 2023-02-17 thomas "reference \"%s\" not found on server", pe->path);
543 d392cc0a 2023-02-17 thomas err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
544 d392cc0a 2023-02-17 thomas goto done;
545 d392cc0a 2023-02-17 thomas }
546 d392cc0a 2023-02-17 thomas
547 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
548 659e7fbd 2020-03-20 stsp goto done;
549 659e7fbd 2020-03-20 stsp }
550 659e7fbd 2020-03-20 stsp
551 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
552 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
553 93658fb9 2020-03-18 stsp continue;
554 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
555 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
556 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
557 a90356f7 2021-08-26 stsp "" : my_capabilities);
558 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(buf)) {
559 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
560 9ff10419 2020-03-18 stsp goto done;
561 9ff10419 2020-03-18 stsp }
562 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
563 344e4747 2020-03-18 stsp if (err)
564 9ff10419 2020-03-18 stsp goto done;
565 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
566 7848a0e1 2020-03-19 stsp nwant++;
567 93658fb9 2020-03-18 stsp }
568 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
569 38c670f1 2020-03-18 stsp if (err)
570 38c670f1 2020-03-18 stsp goto done;
571 7848a0e1 2020-03-19 stsp
572 3c912d14 2020-03-19 stsp if (nwant == 0)
573 7848a0e1 2020-03-19 stsp goto done;
574 7848a0e1 2020-03-19 stsp
575 ea83355f 2021-10-08 thomas TAILQ_FOREACH(pe, have_refs, entry) {
576 ea83355f 2021-10-08 thomas struct got_object_id *id = pe->data;
577 ea83355f 2021-10-08 thomas got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
578 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
579 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(buf)) {
580 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
581 9ff10419 2020-03-18 stsp goto done;
582 9ff10419 2020-03-18 stsp }
583 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
584 344e4747 2020-03-18 stsp if (err)
585 9ff10419 2020-03-18 stsp goto done;
586 7848a0e1 2020-03-19 stsp nhave++;
587 93658fb9 2020-03-18 stsp }
588 7848a0e1 2020-03-19 stsp
589 c12ab9ad 2023-02-21 thomas n = strlcpy(buf, "done\n", sizeof(buf));
590 c12ab9ad 2023-02-21 thomas err = got_pkt_writepkt(fd, buf, n, chattygot);
591 c12ab9ad 2023-02-21 thomas if (err)
592 c12ab9ad 2023-02-21 thomas goto done;
593 c12ab9ad 2023-02-21 thomas
594 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
595 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
596 7848a0e1 2020-03-19 stsp
597 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
598 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
599 38c670f1 2020-03-18 stsp if (err)
600 38c670f1 2020-03-18 stsp goto done;
601 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
602 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
603 7848a0e1 2020-03-19 stsp goto done;
604 7848a0e1 2020-03-19 stsp }
605 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
606 c12ab9ad 2023-02-21 thomas /*
607 c12ab9ad 2023-02-21 thomas * Server could not find a common ancestor.
608 c12ab9ad 2023-02-21 thomas * Perhaps it is an out-of-date mirror, or there
609 c12ab9ad 2023-02-21 thomas * is a repository with unrelated history.
610 c12ab9ad 2023-02-21 thomas */
611 c12ab9ad 2023-02-21 thomas break;
612 7848a0e1 2020-03-19 stsp }
613 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
614 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
615 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
616 7848a0e1 2020-03-19 stsp "unexpected message from server");
617 7848a0e1 2020-03-19 stsp goto done;
618 7848a0e1 2020-03-19 stsp }
619 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&common_id, buf + 4,
620 c8ae092d 2023-02-23 thomas GOT_HASH_SHA1)) {
621 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
622 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
623 7848a0e1 2020-03-19 stsp goto done;
624 7848a0e1 2020-03-19 stsp }
625 7848a0e1 2020-03-19 stsp acked++;
626 93658fb9 2020-03-18 stsp }
627 93658fb9 2020-03-18 stsp
628 7848a0e1 2020-03-19 stsp if (nhave == 0) {
629 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
630 7848a0e1 2020-03-19 stsp if (err)
631 7848a0e1 2020-03-19 stsp goto done;
632 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
633 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
634 7848a0e1 2020-03-19 stsp "unexpected message from server");
635 7848a0e1 2020-03-19 stsp goto done;
636 7848a0e1 2020-03-19 stsp }
637 04c53c18 2020-03-18 stsp }
638 93658fb9 2020-03-18 stsp
639 858b0dfb 2020-03-20 stsp if (chattygot)
640 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
641 858b0dfb 2020-03-20 stsp
642 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
643 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
644 531c3985 2020-03-18 stsp have_sidebands = 1;
645 531c3985 2020-03-18 stsp
646 9ff10419 2020-03-18 stsp while (1) {
647 dc671e91 2020-03-24 stsp ssize_t r = 0;
648 531c3985 2020-03-18 stsp int datalen = -1;
649 531c3985 2020-03-18 stsp
650 531c3985 2020-03-18 stsp if (have_sidebands) {
651 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
652 531c3985 2020-03-18 stsp if (err)
653 531c3985 2020-03-18 stsp goto done;
654 531c3985 2020-03-18 stsp if (datalen <= 0)
655 531c3985 2020-03-18 stsp break;
656 531c3985 2020-03-18 stsp
657 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
658 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
659 531c3985 2020-03-18 stsp if (r == -1) {
660 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
661 531c3985 2020-03-18 stsp goto done;
662 531c3985 2020-03-18 stsp }
663 531c3985 2020-03-18 stsp if (r != 1) {
664 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
665 531c3985 2020-03-18 stsp "short packet");
666 531c3985 2020-03-18 stsp goto done;
667 531c3985 2020-03-18 stsp }
668 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
669 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
670 531c3985 2020-03-18 stsp "bad packet length");
671 531c3985 2020-03-18 stsp goto done;
672 531c3985 2020-03-18 stsp }
673 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
674 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
675 531c3985 2020-03-18 stsp /* Read packfile data. */
676 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
677 531c3985 2020-03-18 stsp if (err)
678 531c3985 2020-03-18 stsp goto done;
679 531c3985 2020-03-18 stsp if (r != datalen) {
680 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
681 531c3985 2020-03-18 stsp "packet too short");
682 531c3985 2020-03-18 stsp goto done;
683 531c3985 2020-03-18 stsp }
684 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
685 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
686 531c3985 2020-03-18 stsp if (err)
687 531c3985 2020-03-18 stsp goto done;
688 531c3985 2020-03-18 stsp if (r != datalen) {
689 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
690 531c3985 2020-03-18 stsp "packet too short");
691 531c3985 2020-03-18 stsp goto done;
692 531c3985 2020-03-18 stsp }
693 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
694 531c3985 2020-03-18 stsp if (err)
695 531c3985 2020-03-18 stsp goto done;
696 531c3985 2020-03-18 stsp continue;
697 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
698 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
699 531c3985 2020-03-18 stsp if (err)
700 531c3985 2020-03-18 stsp goto done;
701 531c3985 2020-03-18 stsp if (r != datalen) {
702 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
703 531c3985 2020-03-18 stsp "packet too short");
704 531c3985 2020-03-18 stsp goto done;
705 531c3985 2020-03-18 stsp }
706 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
707 531c3985 2020-03-18 stsp goto done;
708 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
709 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
710 98f64f14 2021-01-05 stsp if (err)
711 98f64f14 2021-01-05 stsp goto done;
712 98f64f14 2021-01-05 stsp if (r != datalen) {
713 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
714 98f64f14 2021-01-05 stsp "packet too short");
715 98f64f14 2021-01-05 stsp goto done;
716 98f64f14 2021-01-05 stsp }
717 98f64f14 2021-01-05 stsp /*
718 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
719 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
720 98f64f14 2021-01-05 stsp */
721 98f64f14 2021-01-05 stsp buf[r] = '\0';
722 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
723 98f64f14 2021-01-05 stsp continue; /* ignore */
724 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
725 98f64f14 2021-01-05 stsp "unexpected message from server");
726 98f64f14 2021-01-05 stsp goto done;
727 531c3985 2020-03-18 stsp } else {
728 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
729 531c3985 2020-03-18 stsp "unknown side-band received from server");
730 531c3985 2020-03-18 stsp goto done;
731 531c3985 2020-03-18 stsp }
732 531c3985 2020-03-18 stsp } else {
733 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
734 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, sizeof buf);
735 531c3985 2020-03-18 stsp if (err)
736 531c3985 2020-03-18 stsp goto done;
737 531c3985 2020-03-18 stsp if (r <= 0)
738 531c3985 2020-03-18 stsp break;
739 531c3985 2020-03-18 stsp }
740 dc671e91 2020-03-24 stsp
741 dc671e91 2020-03-24 stsp /*
742 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
743 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
744 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
745 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
746 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
747 dc671e91 2020-03-24 stsp *
748 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
749 dc671e91 2020-03-24 stsp */
750 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
751 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
752 dc671e91 2020-03-24 stsp /*
753 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
754 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
755 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
756 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
757 dc671e91 2020-03-24 stsp */
758 9439b99a 2022-10-30 thomas if (sha1_buf_len > 0 &&
759 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
760 9439b99a 2022-10-30 thomas size_t nshift = MIN(sha1_buf_len + r -
761 9439b99a 2022-10-30 thomas SHA1_DIGEST_LENGTH, sha1_buf_len);
762 b16893ba 2023-02-24 thomas got_hash_update(&ctx, sha1_buf,
763 b16893ba 2023-02-24 thomas nshift);
764 9439b99a 2022-10-30 thomas memmove(sha1_buf, sha1_buf + nshift,
765 9439b99a 2022-10-30 thomas sha1_buf_len - nshift);
766 9439b99a 2022-10-30 thomas sha1_buf_len -= nshift;
767 dc671e91 2020-03-24 stsp }
768 dc671e91 2020-03-24 stsp
769 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
770 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
771 dc671e91 2020-03-24 stsp sha1_buf_len += r;
772 dc671e91 2020-03-24 stsp } else {
773 dc671e91 2020-03-24 stsp /*
774 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
775 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
776 dc671e91 2020-03-24 stsp */
777 b16893ba 2023-02-24 thomas got_hash_update(&ctx, sha1_buf, r);
778 531c3985 2020-03-18 stsp
779 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
780 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
781 dc671e91 2020-03-24 stsp sha1_buf_len - r);
782 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
783 dc671e91 2020-03-24 stsp }
784 dc671e91 2020-03-24 stsp } else {
785 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
786 b16893ba 2023-02-24 thomas got_hash_update(&ctx, sha1_buf, sha1_buf_len);
787 dc671e91 2020-03-24 stsp
788 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
789 b16893ba 2023-02-24 thomas got_hash_update(&ctx, buf, r - SHA1_DIGEST_LENGTH);
790 dc671e91 2020-03-24 stsp
791 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
792 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
793 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
794 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
795 dc671e91 2020-03-24 stsp }
796 3168e5da 2020-09-10 stsp
797 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
798 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
799 9ff10419 2020-03-18 stsp if (w == -1) {
800 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
801 9ff10419 2020-03-18 stsp goto done;
802 9ff10419 2020-03-18 stsp }
803 fe53745c 2020-03-18 stsp if (w != r) {
804 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
805 9ff10419 2020-03-18 stsp goto done;
806 9ff10419 2020-03-18 stsp }
807 531c3985 2020-03-18 stsp packsz += w;
808 5672d305 2020-03-18 stsp
809 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
810 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
811 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, &rl);
812 5672d305 2020-03-18 stsp if (err)
813 5672d305 2020-03-18 stsp goto done;
814 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
815 5672d305 2020-03-18 stsp }
816 93658fb9 2020-03-18 stsp }
817 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, NULL);
818 5672d305 2020-03-18 stsp if (err)
819 5672d305 2020-03-18 stsp goto done;
820 dc671e91 2020-03-24 stsp
821 b16893ba 2023-02-24 thomas got_hash_final(&ctx, pack_sha1);
822 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
823 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
824 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
825 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
826 dc671e91 2020-03-24 stsp }
827 9ff10419 2020-03-18 stsp done:
828 21c2d8be 2023-01-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
829 9ff10419 2020-03-18 stsp free(have);
830 9ff10419 2020-03-18 stsp free(want);
831 00cd0e0a 2020-03-18 stsp free(id_str);
832 7917d6d5 2023-02-17 thomas free(default_id_str);
833 00cd0e0a 2020-03-18 stsp free(refname);
834 00cd0e0a 2020-03-18 stsp free(server_capabilities);
835 8f2d01a6 2020-03-18 stsp return err;
836 93658fb9 2020-03-18 stsp }
837 93658fb9 2020-03-18 stsp
838 93658fb9 2020-03-18 stsp
839 93658fb9 2020-03-18 stsp int
840 93658fb9 2020-03-18 stsp main(int argc, char **argv)
841 93658fb9 2020-03-18 stsp {
842 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
843 d639f28b 2023-01-10 thomas int fetchfd = -1, packfd = -1;
844 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
845 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
846 93658fb9 2020-03-18 stsp struct imsg imsg;
847 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
848 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
849 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
850 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
851 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
852 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
853 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
854 01bb5a15 2021-09-25 thomas.ad size_t datalen, i;
855 7abf1863 2023-02-20 thomas char *remote_head = NULL, *worktree_branch = NULL;
856 7848a0e1 2020-03-19 stsp #if 0
857 7848a0e1 2020-03-19 stsp static int attached;
858 7848a0e1 2020-03-19 stsp while (!attached)
859 7848a0e1 2020-03-19 stsp sleep (1);
860 7848a0e1 2020-03-19 stsp #endif
861 33501562 2020-03-18 stsp
862 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
863 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
864 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
865 858b0dfb 2020-03-20 stsp
866 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
867 ffb5f621 2020-03-18 stsp #ifndef PROFILE
868 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
869 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
870 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
871 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
872 ffb5f621 2020-03-18 stsp return 1;
873 ffb5f621 2020-03-18 stsp }
874 97799ccd 2022-02-06 thomas
875 97799ccd 2022-02-06 thomas /* revoke fs access */
876 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
877 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
878 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
879 97799ccd 2022-02-06 thomas return 1;
880 97799ccd 2022-02-06 thomas }
881 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
882 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
883 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
884 5d120ea8 2022-06-23 op return 1;
885 5d120ea8 2022-06-23 op }
886 ffb5f621 2020-03-18 stsp #endif
887 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
888 9ca26ac3 2021-08-06 stsp if (err) {
889 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
890 93658fb9 2020-03-18 stsp err = NULL;
891 93658fb9 2020-03-18 stsp goto done;
892 93658fb9 2020-03-18 stsp }
893 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
894 93658fb9 2020-03-18 stsp goto done;
895 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
896 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
897 93658fb9 2020-03-18 stsp goto done;
898 93658fb9 2020-03-18 stsp }
899 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
900 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
901 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
902 93658fb9 2020-03-18 stsp goto done;
903 93658fb9 2020-03-18 stsp }
904 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
905 3d97effa 2024-01-31 thomas fetchfd = imsg_get_fd(&imsg);
906 9d0a7ee3 2023-02-07 thomas
907 9d0a7ee3 2023-02-07 thomas if (datalen != sizeof(fetch_req) +
908 7abf1863 2023-02-20 thomas fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
909 9d0a7ee3 2023-02-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
910 9d0a7ee3 2023-02-07 thomas goto done;
911 9d0a7ee3 2023-02-07 thomas }
912 9d0a7ee3 2023-02-07 thomas
913 9d0a7ee3 2023-02-07 thomas if (fetch_req.worktree_branch_len != 0) {
914 9d0a7ee3 2023-02-07 thomas worktree_branch = strndup(imsg.data +
915 9d0a7ee3 2023-02-07 thomas sizeof(fetch_req), fetch_req.worktree_branch_len);
916 9d0a7ee3 2023-02-07 thomas if (worktree_branch == NULL) {
917 7abf1863 2023-02-20 thomas err = got_error_from_errno("strndup");
918 7abf1863 2023-02-20 thomas goto done;
919 7abf1863 2023-02-20 thomas }
920 7abf1863 2023-02-20 thomas }
921 7abf1863 2023-02-20 thomas
922 7abf1863 2023-02-20 thomas if (fetch_req.remote_head_len != 0) {
923 7abf1863 2023-02-20 thomas remote_head = strndup(imsg.data + sizeof(fetch_req) +
924 7abf1863 2023-02-20 thomas fetch_req.worktree_branch_len, fetch_req.remote_head_len);
925 7abf1863 2023-02-20 thomas if (remote_head == NULL) {
926 9d0a7ee3 2023-02-07 thomas err = got_error_from_errno("strndup");
927 9d0a7ee3 2023-02-07 thomas goto done;
928 9d0a7ee3 2023-02-07 thomas }
929 9d0a7ee3 2023-02-07 thomas }
930 9d0a7ee3 2023-02-07 thomas
931 4ba14133 2020-03-20 stsp imsg_free(&imsg);
932 4ba14133 2020-03-20 stsp
933 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
934 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
935 2690194b 2020-03-21 stsp
936 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
937 7848a0e1 2020-03-19 stsp struct got_object_id *id;
938 7848a0e1 2020-03-19 stsp char *refname;
939 7848a0e1 2020-03-19 stsp
940 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
941 9ca26ac3 2021-08-06 stsp if (err) {
942 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
943 4ba14133 2020-03-20 stsp err = NULL;
944 4ba14133 2020-03-20 stsp goto done;
945 4ba14133 2020-03-20 stsp }
946 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
947 4ba14133 2020-03-20 stsp goto done;
948 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
949 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
950 4ba14133 2020-03-20 stsp goto done;
951 4ba14133 2020-03-20 stsp }
952 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
953 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
954 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
955 659e7fbd 2020-03-20 stsp goto done;
956 659e7fbd 2020-03-20 stsp }
957 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
958 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
959 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
960 7848a0e1 2020-03-19 stsp goto done;
961 7848a0e1 2020-03-19 stsp }
962 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(href), href.name_len);
963 7848a0e1 2020-03-19 stsp if (refname == NULL) {
964 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
965 7848a0e1 2020-03-19 stsp goto done;
966 7848a0e1 2020-03-19 stsp }
967 659e7fbd 2020-03-20 stsp
968 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
969 7848a0e1 2020-03-19 stsp if (id == NULL) {
970 7848a0e1 2020-03-19 stsp free(refname);
971 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
972 7848a0e1 2020-03-19 stsp goto done;
973 7848a0e1 2020-03-19 stsp }
974 f9f54468 2023-02-03 thomas memcpy(id, &href.id, sizeof(*id));
975 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
976 7848a0e1 2020-03-19 stsp if (err) {
977 7848a0e1 2020-03-19 stsp free(refname);
978 7848a0e1 2020-03-19 stsp free(id);
979 7848a0e1 2020-03-19 stsp goto done;
980 7848a0e1 2020-03-19 stsp }
981 4ba14133 2020-03-20 stsp
982 4ba14133 2020-03-20 stsp imsg_free(&imsg);
983 659e7fbd 2020-03-20 stsp }
984 93658fb9 2020-03-18 stsp
985 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
986 4ba14133 2020-03-20 stsp char *refname;
987 4ba14133 2020-03-20 stsp
988 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
989 9ca26ac3 2021-08-06 stsp if (err) {
990 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
991 4ba14133 2020-03-20 stsp err = NULL;
992 4ba14133 2020-03-20 stsp goto done;
993 4ba14133 2020-03-20 stsp }
994 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
995 4ba14133 2020-03-20 stsp goto done;
996 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
997 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
998 4ba14133 2020-03-20 stsp goto done;
999 4ba14133 2020-03-20 stsp }
1000 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1001 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
1002 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1003 4ba14133 2020-03-20 stsp goto done;
1004 4ba14133 2020-03-20 stsp }
1005 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
1006 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
1007 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1008 4ba14133 2020-03-20 stsp goto done;
1009 4ba14133 2020-03-20 stsp }
1010 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(wbranch),
1011 fcbb06bf 2023-01-14 thomas wbranch.name_len);
1012 4ba14133 2020-03-20 stsp if (refname == NULL) {
1013 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
1014 4ba14133 2020-03-20 stsp goto done;
1015 4ba14133 2020-03-20 stsp }
1016 4ba14133 2020-03-20 stsp
1017 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
1018 4ba14133 2020-03-20 stsp if (err) {
1019 4ba14133 2020-03-20 stsp free(refname);
1020 4ba14133 2020-03-20 stsp goto done;
1021 4ba14133 2020-03-20 stsp }
1022 4ba14133 2020-03-20 stsp
1023 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1024 4ba14133 2020-03-20 stsp }
1025 4ba14133 2020-03-20 stsp
1026 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1027 0e4002ca 2020-03-21 stsp char *refname;
1028 0e4002ca 2020-03-21 stsp
1029 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1030 9ca26ac3 2021-08-06 stsp if (err) {
1031 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1032 0e4002ca 2020-03-21 stsp err = NULL;
1033 0e4002ca 2020-03-21 stsp goto done;
1034 0e4002ca 2020-03-21 stsp }
1035 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1036 0e4002ca 2020-03-21 stsp goto done;
1037 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1038 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1039 0e4002ca 2020-03-21 stsp goto done;
1040 0e4002ca 2020-03-21 stsp }
1041 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1042 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1043 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1044 0e4002ca 2020-03-21 stsp goto done;
1045 0e4002ca 2020-03-21 stsp }
1046 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1047 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1048 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1049 0e4002ca 2020-03-21 stsp goto done;
1050 0e4002ca 2020-03-21 stsp }
1051 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1052 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1053 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
1054 0e4002ca 2020-03-21 stsp goto done;
1055 0e4002ca 2020-03-21 stsp }
1056 0e4002ca 2020-03-21 stsp
1057 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1058 0e4002ca 2020-03-21 stsp if (err) {
1059 0e4002ca 2020-03-21 stsp free(refname);
1060 0e4002ca 2020-03-21 stsp goto done;
1061 0e4002ca 2020-03-21 stsp }
1062 0e4002ca 2020-03-21 stsp
1063 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1064 0e4002ca 2020-03-21 stsp }
1065 0e4002ca 2020-03-21 stsp
1066 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1067 9ca26ac3 2021-08-06 stsp if (err) {
1068 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1069 93658fb9 2020-03-18 stsp err = NULL;
1070 93658fb9 2020-03-18 stsp goto done;
1071 93658fb9 2020-03-18 stsp }
1072 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1073 93658fb9 2020-03-18 stsp goto done;
1074 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1075 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1076 93658fb9 2020-03-18 stsp goto done;
1077 93658fb9 2020-03-18 stsp }
1078 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1079 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1080 93658fb9 2020-03-18 stsp goto done;
1081 93658fb9 2020-03-18 stsp }
1082 3d97effa 2024-01-31 thomas packfd = imsg_get_fd(&imsg);
1083 93658fb9 2020-03-18 stsp
1084 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1085 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1086 9d0a7ee3 2023-02-07 thomas &wanted_refs, fetch_req.list_refs_only,
1087 7abf1863 2023-02-20 thomas worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1088 93658fb9 2020-03-18 stsp done:
1089 9d0a7ee3 2023-02-07 thomas free(worktree_branch);
1090 7abf1863 2023-02-20 thomas free(remote_head);
1091 21c2d8be 2023-01-10 thomas got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1092 21c2d8be 2023-01-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1093 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1094 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1095 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1096 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1097 9ff10419 2020-03-18 stsp if (err != NULL)
1098 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1099 93658fb9 2020-03-18 stsp else
1100 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1101 cf875574 2020-03-18 stsp if (err != NULL) {
1102 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1103 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1104 93658fb9 2020-03-18 stsp }
1105 93658fb9 2020-03-18 stsp
1106 93658fb9 2020-03-18 stsp exit(0);
1107 93658fb9 2020-03-18 stsp }