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