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 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 93658fb9 2020-03-18 stsp #include "got_lib_sha1.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 dcb64fea 2022-02-23 stsp #include "got_lib_ratelimit.h"
54 74737945 2022-10-30 stsp
55 74737945 2022-10-30 stsp #ifndef MIN
56 74737945 2022-10-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 74737945 2022-10-30 stsp #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 188f8dcf 2023-02-07 stsp 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 5e91dae4 2022-08-30 stsp 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 dcb64fea 2022-02-23 stsp send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
149 dcb64fea 2022-02-23 stsp struct got_ratelimit *rl)
150 e70bf110 2020-03-22 stsp {
151 dcb64fea 2022-02-23 stsp const struct got_error *err;
152 dcb64fea 2022-02-23 stsp int elapsed = 0;
153 dcb64fea 2022-02-23 stsp
154 dcb64fea 2022-02-23 stsp if (rl) {
155 dcb64fea 2022-02-23 stsp err = got_ratelimit_check(&elapsed, rl);
156 dcb64fea 2022-02-23 stsp if (err || !elapsed)
157 dcb64fea 2022-02-23 stsp return err;
158 dcb64fea 2022-02-23 stsp }
159 dcb64fea 2022-02-23 stsp
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 99fd9ff4 2022-11-17 op 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 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
244 1453347d 2022-05-19 stsp 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 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
254 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
255 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
256 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 1453347d 2022-05-19 stsp if (imsg_add(wbuf, name, name_len) == -1)
258 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 1453347d 2022-05-19 stsp if (imsg_add(wbuf, target, target_len) == -1)
260 1453347d 2022-05-19 stsp 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 wbuf->fd = -1;
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 wbuf->fd = -1;
290 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
291 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
292 188f8dcf 2023-02-07 stsp }
293 188f8dcf 2023-02-07 stsp
294 188f8dcf 2023-02-07 stsp static const struct got_error *
295 188f8dcf 2023-02-07 stsp fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
296 188f8dcf 2023-02-07 stsp struct got_object_id *have, struct got_object_id *want,
297 188f8dcf 2023-02-07 stsp const char *refname, const char *id_str)
298 188f8dcf 2023-02-07 stsp {
299 188f8dcf 2023-02-07 stsp const struct got_error *err;
300 188f8dcf 2023-02-07 stsp char *theirs = NULL, *mine = NULL;
301 188f8dcf 2023-02-07 stsp
302 188f8dcf 2023-02-07 stsp if (!got_parse_sha1_digest(want->sha1, id_str)) {
303 188f8dcf 2023-02-07 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
304 188f8dcf 2023-02-07 stsp goto done;
305 188f8dcf 2023-02-07 stsp }
306 188f8dcf 2023-02-07 stsp
307 188f8dcf 2023-02-07 stsp match_remote_ref(have_refs, have, refname);
308 188f8dcf 2023-02-07 stsp err = send_fetch_ref(ibuf, want, refname);
309 188f8dcf 2023-02-07 stsp if (err)
310 188f8dcf 2023-02-07 stsp goto done;
311 188f8dcf 2023-02-07 stsp
312 188f8dcf 2023-02-07 stsp if (chattygot)
313 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: %s will be fetched\n",
314 188f8dcf 2023-02-07 stsp getprogname(), refname);
315 188f8dcf 2023-02-07 stsp if (chattygot > 1) {
316 188f8dcf 2023-02-07 stsp err = got_object_id_str(&theirs, want);
317 188f8dcf 2023-02-07 stsp if (err)
318 188f8dcf 2023-02-07 stsp goto done;
319 188f8dcf 2023-02-07 stsp err = got_object_id_str(&mine, have);
320 188f8dcf 2023-02-07 stsp if (err)
321 188f8dcf 2023-02-07 stsp goto done;
322 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
323 188f8dcf 2023-02-07 stsp getprogname(), theirs, getprogname(), mine);
324 188f8dcf 2023-02-07 stsp }
325 188f8dcf 2023-02-07 stsp done:
326 188f8dcf 2023-02-07 stsp free(theirs);
327 188f8dcf 2023-02-07 stsp free(mine);
328 188f8dcf 2023-02-07 stsp return err;
329 e70bf110 2020-03-22 stsp }
330 729743d1 2020-03-23 stsp
331 e70bf110 2020-03-22 stsp static const struct got_error *
332 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
333 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
334 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
335 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
336 0dd7613c 2023-02-13 mark const char *worktree_branch, 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 2a19e2e2 2023-02-14 mark char *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 dc671e91 2020-03-24 stsp SHA1_CTX sha1_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 dcb64fea 2022-02-23 stsp struct got_ratelimit rl;
356 93658fb9 2020-03-18 stsp
357 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
358 dc671e91 2020-03-24 stsp SHA1Init(&sha1_ctx);
359 dcb64fea 2022-02-23 stsp 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 e99d9267 2021-10-07 stsp free(id_str);
380 e99d9267 2021-10-07 stsp 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 188f8dcf 2023-02-07 stsp
386 188f8dcf 2023-02-07 stsp if (refsz == nref + 1) {
387 ec218e16 2023-02-07 mark struct got_object_id *h, *w;
388 ec218e16 2023-02-07 mark
389 188f8dcf 2023-02-07 stsp refsz *= 2;
390 ec218e16 2023-02-07 mark h = reallocarray(have, refsz, sizeof(have[0]));
391 ec218e16 2023-02-07 mark if (h == NULL) {
392 188f8dcf 2023-02-07 stsp err = got_error_from_errno("reallocarray");
393 188f8dcf 2023-02-07 stsp goto done;
394 188f8dcf 2023-02-07 stsp }
395 ec218e16 2023-02-07 mark have = h;
396 ec218e16 2023-02-07 mark w = reallocarray(want, refsz, sizeof(want[0]));
397 ec218e16 2023-02-07 mark if (w == NULL) {
398 188f8dcf 2023-02-07 stsp err = got_error_from_errno("reallocarray");
399 188f8dcf 2023-02-07 stsp goto done;
400 188f8dcf 2023-02-07 stsp }
401 ec218e16 2023-02-07 mark want = w;
402 188f8dcf 2023-02-07 stsp }
403 188f8dcf 2023-02-07 stsp
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 c9f1ac46 2022-11-08 stsp if (default_branch)
432 c9f1ac46 2022-11-08 stsp 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 2a19e2e2 2023-02-14 mark
442 2a19e2e2 2023-02-14 mark /* always fetch remote HEAD unless -b was specified */
443 2a19e2e2 2023-02-14 mark if (!no_head && default_branch &&
444 2a19e2e2 2023-02-14 mark strcmp(refname, default_branch) == 0 &&
445 2a19e2e2 2023-02-14 mark strncmp(default_branch, "refs/heads/", 11) == 0) {
446 2a19e2e2 2023-02-14 mark err = fetch_ref(ibuf, have_refs, &have[nref],
447 2a19e2e2 2023-02-14 mark &want[nref], default_branch, id_str);
448 2a19e2e2 2023-02-14 mark if (err)
449 9ff10419 2020-03-18 stsp goto done;
450 2a19e2e2 2023-02-14 mark nref++;
451 2a19e2e2 2023-02-14 mark continue;
452 93658fb9 2020-03-18 stsp }
453 858b0dfb 2020-03-20 stsp
454 188f8dcf 2023-02-07 stsp if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
455 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
456 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
457 858b0dfb 2020-03-20 stsp if (err)
458 858b0dfb 2020-03-20 stsp goto done;
459 188f8dcf 2023-02-07 stsp nref++;
460 188f8dcf 2023-02-07 stsp } else if (strncmp(refname, "refs/heads/", 11) == 0) {
461 188f8dcf 2023-02-07 stsp if (fetch_all_branches) {
462 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
463 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
464 188f8dcf 2023-02-07 stsp if (err)
465 188f8dcf 2023-02-07 stsp goto done;
466 188f8dcf 2023-02-07 stsp nref++;
467 188f8dcf 2023-02-07 stsp continue;
468 858b0dfb 2020-03-20 stsp }
469 188f8dcf 2023-02-07 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
470 188f8dcf 2023-02-07 stsp if (match_branch(refname, pe->path))
471 188f8dcf 2023-02-07 stsp break;
472 188f8dcf 2023-02-07 stsp }
473 188f8dcf 2023-02-07 stsp if (pe != NULL || (worktree_branch != NULL &&
474 188f8dcf 2023-02-07 stsp match_branch(refname, worktree_branch))) {
475 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
476 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
477 188f8dcf 2023-02-07 stsp if (err)
478 188f8dcf 2023-02-07 stsp goto done;
479 188f8dcf 2023-02-07 stsp nref++;
480 188f8dcf 2023-02-07 stsp } else if (chattygot) {
481 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: ignoring %s\n",
482 188f8dcf 2023-02-07 stsp getprogname(), refname);
483 188f8dcf 2023-02-07 stsp }
484 188f8dcf 2023-02-07 stsp } else {
485 188f8dcf 2023-02-07 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
486 188f8dcf 2023-02-07 stsp if (match_wanted_ref(refname, pe->path))
487 188f8dcf 2023-02-07 stsp break;
488 188f8dcf 2023-02-07 stsp }
489 188f8dcf 2023-02-07 stsp if (pe != NULL) {
490 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
491 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
492 188f8dcf 2023-02-07 stsp if (err)
493 188f8dcf 2023-02-07 stsp goto done;
494 188f8dcf 2023-02-07 stsp nref++;
495 188f8dcf 2023-02-07 stsp } else if (chattygot) {
496 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: ignoring %s\n",
497 188f8dcf 2023-02-07 stsp getprogname(), refname);
498 188f8dcf 2023-02-07 stsp }
499 858b0dfb 2020-03-20 stsp }
500 93658fb9 2020-03-18 stsp }
501 93658fb9 2020-03-18 stsp
502 41b0de12 2020-03-21 stsp if (list_refs_only)
503 41b0de12 2020-03-21 stsp goto done;
504 188f8dcf 2023-02-07 stsp
505 188f8dcf 2023-02-07 stsp /* Abort if we haven't found anything to fetch. */
506 188f8dcf 2023-02-07 stsp if (nref == 0) {
507 f72ce919 2023-02-13 mark struct got_pathlist_entry *pe;
508 f72ce919 2023-02-13 mark static char msg[PATH_MAX + 33];
509 f72ce919 2023-02-13 mark
510 f72ce919 2023-02-13 mark pe = TAILQ_FIRST(wanted_branches);
511 f72ce919 2023-02-13 mark if (pe) {
512 f72ce919 2023-02-13 mark snprintf(msg, sizeof(msg),
513 f72ce919 2023-02-13 mark "branch \"%s\" not found on server", pe->path);
514 f72ce919 2023-02-13 mark err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
515 f72ce919 2023-02-13 mark goto done;
516 f72ce919 2023-02-13 mark }
517 f72ce919 2023-02-13 mark
518 f72ce919 2023-02-13 mark pe = TAILQ_FIRST(wanted_refs);
519 f72ce919 2023-02-13 mark if (pe) {
520 f72ce919 2023-02-13 mark snprintf(msg, sizeof(msg),
521 f72ce919 2023-02-13 mark "reference \"%s\" not found on server", pe->path);
522 f72ce919 2023-02-13 mark err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
523 f72ce919 2023-02-13 mark goto done;
524 f72ce919 2023-02-13 mark }
525 f72ce919 2023-02-13 mark
526 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
527 659e7fbd 2020-03-20 stsp goto done;
528 659e7fbd 2020-03-20 stsp }
529 659e7fbd 2020-03-20 stsp
530 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
531 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
532 93658fb9 2020-03-18 stsp continue;
533 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
534 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
535 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
536 a90356f7 2021-08-26 stsp "" : my_capabilities);
537 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= sizeof(buf)) {
538 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
539 9ff10419 2020-03-18 stsp goto done;
540 9ff10419 2020-03-18 stsp }
541 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
542 344e4747 2020-03-18 stsp if (err)
543 9ff10419 2020-03-18 stsp goto done;
544 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
545 7848a0e1 2020-03-19 stsp nwant++;
546 93658fb9 2020-03-18 stsp }
547 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
548 38c670f1 2020-03-18 stsp if (err)
549 38c670f1 2020-03-18 stsp goto done;
550 7848a0e1 2020-03-19 stsp
551 3c912d14 2020-03-19 stsp if (nwant == 0)
552 7848a0e1 2020-03-19 stsp goto done;
553 7848a0e1 2020-03-19 stsp
554 dd088d95 2021-10-06 stsp TAILQ_FOREACH(pe, have_refs, entry) {
555 dd088d95 2021-10-06 stsp struct got_object_id *id = pe->data;
556 dd088d95 2021-10-06 stsp got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
557 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
558 438d0cc3 2022-08-16 op 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 7848a0e1 2020-03-19 stsp nhave++;
566 93658fb9 2020-03-18 stsp }
567 7848a0e1 2020-03-19 stsp
568 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
569 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
570 7848a0e1 2020-03-19 stsp
571 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
572 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
573 38c670f1 2020-03-18 stsp if (err)
574 38c670f1 2020-03-18 stsp goto done;
575 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
576 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
577 7848a0e1 2020-03-19 stsp goto done;
578 7848a0e1 2020-03-19 stsp }
579 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
580 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
581 7848a0e1 2020-03-19 stsp continue;
582 7848a0e1 2020-03-19 stsp }
583 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
584 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
585 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
586 7848a0e1 2020-03-19 stsp "unexpected message from server");
587 7848a0e1 2020-03-19 stsp goto done;
588 7848a0e1 2020-03-19 stsp }
589 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
590 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
591 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
592 7848a0e1 2020-03-19 stsp goto done;
593 7848a0e1 2020-03-19 stsp }
594 7848a0e1 2020-03-19 stsp acked++;
595 93658fb9 2020-03-18 stsp }
596 7848a0e1 2020-03-19 stsp
597 ea8d9c76 2022-08-16 op n = strlcpy(buf, "done\n", sizeof(buf));
598 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
599 344e4747 2020-03-18 stsp if (err)
600 9ff10419 2020-03-18 stsp goto done;
601 93658fb9 2020-03-18 stsp
602 7848a0e1 2020-03-19 stsp if (nhave == 0) {
603 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
604 7848a0e1 2020-03-19 stsp if (err)
605 7848a0e1 2020-03-19 stsp goto done;
606 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
607 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
608 7848a0e1 2020-03-19 stsp "unexpected message from server");
609 7848a0e1 2020-03-19 stsp goto done;
610 7848a0e1 2020-03-19 stsp }
611 04c53c18 2020-03-18 stsp }
612 93658fb9 2020-03-18 stsp
613 858b0dfb 2020-03-20 stsp if (chattygot)
614 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
615 858b0dfb 2020-03-20 stsp
616 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
617 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
618 531c3985 2020-03-18 stsp have_sidebands = 1;
619 531c3985 2020-03-18 stsp
620 9ff10419 2020-03-18 stsp while (1) {
621 dc671e91 2020-03-24 stsp ssize_t r = 0;
622 531c3985 2020-03-18 stsp int datalen = -1;
623 531c3985 2020-03-18 stsp
624 531c3985 2020-03-18 stsp if (have_sidebands) {
625 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
626 531c3985 2020-03-18 stsp if (err)
627 531c3985 2020-03-18 stsp goto done;
628 531c3985 2020-03-18 stsp if (datalen <= 0)
629 531c3985 2020-03-18 stsp break;
630 531c3985 2020-03-18 stsp
631 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
632 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
633 531c3985 2020-03-18 stsp if (r == -1) {
634 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
635 531c3985 2020-03-18 stsp goto done;
636 531c3985 2020-03-18 stsp }
637 531c3985 2020-03-18 stsp if (r != 1) {
638 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
639 531c3985 2020-03-18 stsp "short packet");
640 531c3985 2020-03-18 stsp goto done;
641 531c3985 2020-03-18 stsp }
642 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
643 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
644 531c3985 2020-03-18 stsp "bad packet length");
645 531c3985 2020-03-18 stsp goto done;
646 531c3985 2020-03-18 stsp }
647 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
648 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
649 531c3985 2020-03-18 stsp /* Read packfile data. */
650 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
651 531c3985 2020-03-18 stsp if (err)
652 531c3985 2020-03-18 stsp goto done;
653 531c3985 2020-03-18 stsp if (r != datalen) {
654 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
655 531c3985 2020-03-18 stsp "packet too short");
656 531c3985 2020-03-18 stsp goto done;
657 531c3985 2020-03-18 stsp }
658 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
659 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
660 531c3985 2020-03-18 stsp if (err)
661 531c3985 2020-03-18 stsp goto done;
662 531c3985 2020-03-18 stsp if (r != datalen) {
663 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
664 531c3985 2020-03-18 stsp "packet too short");
665 531c3985 2020-03-18 stsp goto done;
666 531c3985 2020-03-18 stsp }
667 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
668 531c3985 2020-03-18 stsp if (err)
669 531c3985 2020-03-18 stsp goto done;
670 531c3985 2020-03-18 stsp continue;
671 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
672 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
673 531c3985 2020-03-18 stsp if (err)
674 531c3985 2020-03-18 stsp goto done;
675 531c3985 2020-03-18 stsp if (r != datalen) {
676 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
677 531c3985 2020-03-18 stsp "packet too short");
678 531c3985 2020-03-18 stsp goto done;
679 531c3985 2020-03-18 stsp }
680 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
681 531c3985 2020-03-18 stsp goto done;
682 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
683 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
684 98f64f14 2021-01-05 stsp if (err)
685 98f64f14 2021-01-05 stsp goto done;
686 98f64f14 2021-01-05 stsp if (r != datalen) {
687 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
688 98f64f14 2021-01-05 stsp "packet too short");
689 98f64f14 2021-01-05 stsp goto done;
690 98f64f14 2021-01-05 stsp }
691 98f64f14 2021-01-05 stsp /*
692 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
693 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
694 98f64f14 2021-01-05 stsp */
695 98f64f14 2021-01-05 stsp buf[r] = '\0';
696 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
697 98f64f14 2021-01-05 stsp continue; /* ignore */
698 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
699 98f64f14 2021-01-05 stsp "unexpected message from server");
700 98f64f14 2021-01-05 stsp goto done;
701 531c3985 2020-03-18 stsp } else {
702 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
703 531c3985 2020-03-18 stsp "unknown side-band received from server");
704 531c3985 2020-03-18 stsp goto done;
705 531c3985 2020-03-18 stsp }
706 531c3985 2020-03-18 stsp } else {
707 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
708 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, sizeof buf);
709 531c3985 2020-03-18 stsp if (err)
710 531c3985 2020-03-18 stsp goto done;
711 531c3985 2020-03-18 stsp if (r <= 0)
712 531c3985 2020-03-18 stsp break;
713 531c3985 2020-03-18 stsp }
714 dc671e91 2020-03-24 stsp
715 dc671e91 2020-03-24 stsp /*
716 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
717 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
718 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
719 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
720 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
721 dc671e91 2020-03-24 stsp *
722 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
723 dc671e91 2020-03-24 stsp */
724 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
725 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
726 dc671e91 2020-03-24 stsp /*
727 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
728 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
729 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
730 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
731 dc671e91 2020-03-24 stsp */
732 74737945 2022-10-30 stsp if (sha1_buf_len > 0 &&
733 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
734 74737945 2022-10-30 stsp size_t nshift = MIN(sha1_buf_len + r -
735 74737945 2022-10-30 stsp SHA1_DIGEST_LENGTH, sha1_buf_len);
736 74737945 2022-10-30 stsp SHA1Update(&sha1_ctx, sha1_buf, nshift);
737 74737945 2022-10-30 stsp memmove(sha1_buf, sha1_buf + nshift,
738 74737945 2022-10-30 stsp sha1_buf_len - nshift);
739 74737945 2022-10-30 stsp sha1_buf_len -= nshift;
740 dc671e91 2020-03-24 stsp }
741 dc671e91 2020-03-24 stsp
742 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
743 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
744 dc671e91 2020-03-24 stsp sha1_buf_len += r;
745 dc671e91 2020-03-24 stsp } else {
746 dc671e91 2020-03-24 stsp /*
747 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
748 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
749 dc671e91 2020-03-24 stsp */
750 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, r);
751 531c3985 2020-03-18 stsp
752 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
753 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
754 dc671e91 2020-03-24 stsp sha1_buf_len - r);
755 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
756 dc671e91 2020-03-24 stsp }
757 dc671e91 2020-03-24 stsp } else {
758 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
759 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
760 dc671e91 2020-03-24 stsp
761 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
762 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
763 dc671e91 2020-03-24 stsp
764 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
765 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
766 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
767 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
768 dc671e91 2020-03-24 stsp }
769 3168e5da 2020-09-10 stsp
770 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
771 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
772 9ff10419 2020-03-18 stsp if (w == -1) {
773 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
774 9ff10419 2020-03-18 stsp goto done;
775 9ff10419 2020-03-18 stsp }
776 fe53745c 2020-03-18 stsp if (w != r) {
777 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
778 9ff10419 2020-03-18 stsp goto done;
779 9ff10419 2020-03-18 stsp }
780 531c3985 2020-03-18 stsp packsz += w;
781 5672d305 2020-03-18 stsp
782 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
783 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
784 dcb64fea 2022-02-23 stsp err = send_fetch_download_progress(ibuf, packsz, &rl);
785 5672d305 2020-03-18 stsp if (err)
786 5672d305 2020-03-18 stsp goto done;
787 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
788 5672d305 2020-03-18 stsp }
789 93658fb9 2020-03-18 stsp }
790 dcb64fea 2022-02-23 stsp err = send_fetch_download_progress(ibuf, packsz, NULL);
791 5672d305 2020-03-18 stsp if (err)
792 5672d305 2020-03-18 stsp goto done;
793 dc671e91 2020-03-24 stsp
794 dc671e91 2020-03-24 stsp SHA1Final(pack_sha1, &sha1_ctx);
795 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
796 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
797 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
798 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
799 dc671e91 2020-03-24 stsp }
800 9ff10419 2020-03-18 stsp done:
801 d8bacb93 2023-01-10 mark got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
802 9ff10419 2020-03-18 stsp free(have);
803 9ff10419 2020-03-18 stsp free(want);
804 00cd0e0a 2020-03-18 stsp free(id_str);
805 00cd0e0a 2020-03-18 stsp free(refname);
806 00cd0e0a 2020-03-18 stsp free(server_capabilities);
807 8f2d01a6 2020-03-18 stsp return err;
808 93658fb9 2020-03-18 stsp }
809 93658fb9 2020-03-18 stsp
810 93658fb9 2020-03-18 stsp
811 93658fb9 2020-03-18 stsp int
812 93658fb9 2020-03-18 stsp main(int argc, char **argv)
813 93658fb9 2020-03-18 stsp {
814 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
815 9114dd43 2023-01-10 mark int fetchfd = -1, packfd = -1;
816 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
817 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
818 93658fb9 2020-03-18 stsp struct imsg imsg;
819 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
820 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
821 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
822 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
823 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
824 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
825 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
826 030daac8 2021-09-25 stsp size_t datalen, i;
827 188f8dcf 2023-02-07 stsp char *worktree_branch = NULL;
828 7848a0e1 2020-03-19 stsp #if 0
829 7848a0e1 2020-03-19 stsp static int attached;
830 7848a0e1 2020-03-19 stsp while (!attached)
831 7848a0e1 2020-03-19 stsp sleep (1);
832 7848a0e1 2020-03-19 stsp #endif
833 33501562 2020-03-18 stsp
834 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
835 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
836 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
837 858b0dfb 2020-03-20 stsp
838 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
839 ffb5f621 2020-03-18 stsp #ifndef PROFILE
840 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
841 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
842 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
843 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
844 ffb5f621 2020-03-18 stsp return 1;
845 ffb5f621 2020-03-18 stsp }
846 ffb5f621 2020-03-18 stsp #endif
847 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
848 9ca26ac3 2021-08-06 stsp if (err) {
849 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
850 93658fb9 2020-03-18 stsp err = NULL;
851 93658fb9 2020-03-18 stsp goto done;
852 93658fb9 2020-03-18 stsp }
853 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
854 93658fb9 2020-03-18 stsp goto done;
855 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
856 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
857 93658fb9 2020-03-18 stsp goto done;
858 93658fb9 2020-03-18 stsp }
859 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
860 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
861 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
862 93658fb9 2020-03-18 stsp goto done;
863 93658fb9 2020-03-18 stsp }
864 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
865 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
866 188f8dcf 2023-02-07 stsp
867 188f8dcf 2023-02-07 stsp if (datalen != sizeof(fetch_req) +
868 188f8dcf 2023-02-07 stsp fetch_req.worktree_branch_len) {
869 188f8dcf 2023-02-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
870 188f8dcf 2023-02-07 stsp goto done;
871 188f8dcf 2023-02-07 stsp }
872 188f8dcf 2023-02-07 stsp
873 188f8dcf 2023-02-07 stsp if (fetch_req.worktree_branch_len != 0) {
874 188f8dcf 2023-02-07 stsp worktree_branch = strndup(imsg.data +
875 188f8dcf 2023-02-07 stsp sizeof(fetch_req), fetch_req.worktree_branch_len);
876 188f8dcf 2023-02-07 stsp if (worktree_branch == NULL) {
877 188f8dcf 2023-02-07 stsp err = got_error_from_errno("strndup");
878 188f8dcf 2023-02-07 stsp goto done;
879 188f8dcf 2023-02-07 stsp }
880 188f8dcf 2023-02-07 stsp }
881 188f8dcf 2023-02-07 stsp
882 4ba14133 2020-03-20 stsp imsg_free(&imsg);
883 4ba14133 2020-03-20 stsp
884 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
885 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
886 2690194b 2020-03-21 stsp
887 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
888 7848a0e1 2020-03-19 stsp struct got_object_id *id;
889 7848a0e1 2020-03-19 stsp char *refname;
890 7848a0e1 2020-03-19 stsp
891 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
892 9ca26ac3 2021-08-06 stsp if (err) {
893 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
894 4ba14133 2020-03-20 stsp err = NULL;
895 4ba14133 2020-03-20 stsp goto done;
896 4ba14133 2020-03-20 stsp }
897 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
898 4ba14133 2020-03-20 stsp goto done;
899 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
900 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
901 4ba14133 2020-03-20 stsp goto done;
902 4ba14133 2020-03-20 stsp }
903 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
904 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
905 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
906 659e7fbd 2020-03-20 stsp goto done;
907 659e7fbd 2020-03-20 stsp }
908 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
909 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
910 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
911 7848a0e1 2020-03-19 stsp goto done;
912 7848a0e1 2020-03-19 stsp }
913 00b3e9ae 2023-01-11 op refname = strndup(imsg.data + sizeof(href), href.name_len);
914 7848a0e1 2020-03-19 stsp if (refname == NULL) {
915 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
916 7848a0e1 2020-03-19 stsp goto done;
917 7848a0e1 2020-03-19 stsp }
918 659e7fbd 2020-03-20 stsp
919 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
920 7848a0e1 2020-03-19 stsp if (id == NULL) {
921 7848a0e1 2020-03-19 stsp free(refname);
922 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
923 7848a0e1 2020-03-19 stsp goto done;
924 7848a0e1 2020-03-19 stsp }
925 4b4da3bb 2023-02-01 op memcpy(id, &href.id, sizeof(*id));
926 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
927 7848a0e1 2020-03-19 stsp if (err) {
928 7848a0e1 2020-03-19 stsp free(refname);
929 7848a0e1 2020-03-19 stsp free(id);
930 7848a0e1 2020-03-19 stsp goto done;
931 7848a0e1 2020-03-19 stsp }
932 4ba14133 2020-03-20 stsp
933 4ba14133 2020-03-20 stsp imsg_free(&imsg);
934 659e7fbd 2020-03-20 stsp }
935 93658fb9 2020-03-18 stsp
936 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
937 4ba14133 2020-03-20 stsp char *refname;
938 4ba14133 2020-03-20 stsp
939 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
940 9ca26ac3 2021-08-06 stsp if (err) {
941 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
942 4ba14133 2020-03-20 stsp err = NULL;
943 4ba14133 2020-03-20 stsp goto done;
944 4ba14133 2020-03-20 stsp }
945 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
946 4ba14133 2020-03-20 stsp goto done;
947 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
948 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
949 4ba14133 2020-03-20 stsp goto done;
950 4ba14133 2020-03-20 stsp }
951 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
952 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
953 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
954 4ba14133 2020-03-20 stsp goto done;
955 4ba14133 2020-03-20 stsp }
956 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
957 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
958 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
959 4ba14133 2020-03-20 stsp goto done;
960 4ba14133 2020-03-20 stsp }
961 00b3e9ae 2023-01-11 op refname = strndup(imsg.data + sizeof(wbranch),
962 00b3e9ae 2023-01-11 op wbranch.name_len);
963 4ba14133 2020-03-20 stsp if (refname == NULL) {
964 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
965 4ba14133 2020-03-20 stsp goto done;
966 4ba14133 2020-03-20 stsp }
967 4ba14133 2020-03-20 stsp
968 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
969 4ba14133 2020-03-20 stsp if (err) {
970 4ba14133 2020-03-20 stsp free(refname);
971 4ba14133 2020-03-20 stsp goto done;
972 4ba14133 2020-03-20 stsp }
973 4ba14133 2020-03-20 stsp
974 4ba14133 2020-03-20 stsp imsg_free(&imsg);
975 4ba14133 2020-03-20 stsp }
976 4ba14133 2020-03-20 stsp
977 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
978 0e4002ca 2020-03-21 stsp char *refname;
979 0e4002ca 2020-03-21 stsp
980 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
981 9ca26ac3 2021-08-06 stsp if (err) {
982 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
983 0e4002ca 2020-03-21 stsp err = NULL;
984 0e4002ca 2020-03-21 stsp goto done;
985 0e4002ca 2020-03-21 stsp }
986 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
987 0e4002ca 2020-03-21 stsp goto done;
988 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
989 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
990 0e4002ca 2020-03-21 stsp goto done;
991 0e4002ca 2020-03-21 stsp }
992 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
993 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
994 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
995 0e4002ca 2020-03-21 stsp goto done;
996 0e4002ca 2020-03-21 stsp }
997 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
998 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
999 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 0e4002ca 2020-03-21 stsp goto done;
1001 0e4002ca 2020-03-21 stsp }
1002 00b3e9ae 2023-01-11 op refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1003 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1004 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
1005 0e4002ca 2020-03-21 stsp goto done;
1006 0e4002ca 2020-03-21 stsp }
1007 0e4002ca 2020-03-21 stsp
1008 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1009 0e4002ca 2020-03-21 stsp if (err) {
1010 0e4002ca 2020-03-21 stsp free(refname);
1011 0e4002ca 2020-03-21 stsp goto done;
1012 0e4002ca 2020-03-21 stsp }
1013 0e4002ca 2020-03-21 stsp
1014 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1015 0e4002ca 2020-03-21 stsp }
1016 0e4002ca 2020-03-21 stsp
1017 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1018 9ca26ac3 2021-08-06 stsp if (err) {
1019 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1020 93658fb9 2020-03-18 stsp err = NULL;
1021 93658fb9 2020-03-18 stsp goto done;
1022 93658fb9 2020-03-18 stsp }
1023 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1024 93658fb9 2020-03-18 stsp goto done;
1025 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1026 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 93658fb9 2020-03-18 stsp goto done;
1028 93658fb9 2020-03-18 stsp }
1029 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1030 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1031 93658fb9 2020-03-18 stsp goto done;
1032 93658fb9 2020-03-18 stsp }
1033 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1034 93658fb9 2020-03-18 stsp
1035 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1036 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1037 188f8dcf 2023-02-07 stsp &wanted_refs, fetch_req.list_refs_only,
1038 0dd7613c 2023-02-13 mark worktree_branch, fetch_req.no_head, &ibuf);
1039 93658fb9 2020-03-18 stsp done:
1040 188f8dcf 2023-02-07 stsp free(worktree_branch);
1041 d8bacb93 2023-01-10 mark got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1042 d8bacb93 2023-01-10 mark got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1043 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1044 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1045 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1046 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1047 9ff10419 2020-03-18 stsp if (err != NULL)
1048 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1049 93658fb9 2020-03-18 stsp else
1050 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1051 cf875574 2020-03-18 stsp if (err != NULL) {
1052 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1053 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1054 93658fb9 2020-03-18 stsp }
1055 93658fb9 2020-03-18 stsp
1056 93658fb9 2020-03-18 stsp exit(0);
1057 93658fb9 2020-03-18 stsp }