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