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