Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2018, 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/stat.h>
19 93658fb9 2020-03-18 stsp #include <sys/uio.h>
20 93658fb9 2020-03-18 stsp #include <sys/socket.h>
21 93658fb9 2020-03-18 stsp #include <sys/wait.h>
22 93658fb9 2020-03-18 stsp #include <sys/resource.h>
23 93658fb9 2020-03-18 stsp #include <sys/socket.h>
24 93658fb9 2020-03-18 stsp
25 93658fb9 2020-03-18 stsp #include <errno.h>
26 5cc27ede 2020-03-18 stsp #include <err.h>
27 93658fb9 2020-03-18 stsp #include <fcntl.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 <stdint.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 <ctype.h>
35 93658fb9 2020-03-18 stsp #include <limits.h>
36 93658fb9 2020-03-18 stsp #include <time.h>
37 93658fb9 2020-03-18 stsp #include <uuid.h>
38 93658fb9 2020-03-18 stsp
39 93658fb9 2020-03-18 stsp #include "got_error.h"
40 93658fb9 2020-03-18 stsp #include "got_reference.h"
41 93658fb9 2020-03-18 stsp #include "got_repository.h"
42 93658fb9 2020-03-18 stsp #include "got_path.h"
43 93658fb9 2020-03-18 stsp #include "got_cancel.h"
44 93658fb9 2020-03-18 stsp #include "got_worktree.h"
45 93658fb9 2020-03-18 stsp #include "got_object.h"
46 fe4e1501 2020-03-18 stsp #include "got_opentemp.h"
47 82ebf666 2020-03-18 stsp #include "got_fetch.h"
48 93658fb9 2020-03-18 stsp
49 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
51 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
52 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
53 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
54 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
58 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
59 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
60 77d7d3bb 2021-09-05 stsp #include "got_lib_pkt.h"
61 d582f26c 2020-03-18 stsp
62 d582f26c 2020-03-18 stsp #ifndef nitems
63 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 ccf6dd5e 2020-12-19 stsp #endif
65 ccf6dd5e 2020-12-19 stsp
66 ccf6dd5e 2020-12-19 stsp #ifndef ssizeof
67 ccf6dd5e 2020-12-19 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
68 d582f26c 2020-03-18 stsp #endif
69 93658fb9 2020-03-18 stsp
70 68999b92 2020-03-18 stsp #ifndef MIN
71 68999b92 2020-03-18 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
72 68999b92 2020-03-18 stsp #endif
73 68999b92 2020-03-18 stsp
74 20eb36d0 2020-03-18 stsp const struct got_error *
75 9c52365f 2020-03-21 stsp got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
76 9c52365f 2020-03-21 stsp const char *host, const char *port, const char *server_path, int verbosity)
77 20eb36d0 2020-03-18 stsp {
78 20eb36d0 2020-03-18 stsp const struct got_error *err = NULL;
79 20eb36d0 2020-03-18 stsp
80 9c52365f 2020-03-21 stsp *fetchpid = -1;
81 20eb36d0 2020-03-18 stsp *fetchfd = -1;
82 20eb36d0 2020-03-18 stsp
83 20eb36d0 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
84 d65a88a2 2021-09-05 stsp err = got_dial_ssh(fetchpid, fetchfd, host, port,
85 d65a88a2 2021-09-05 stsp server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
86 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
87 d65a88a2 2021-09-05 stsp err = got_dial_git(fetchfd, host, port, server_path,
88 d65a88a2 2021-09-05 stsp GOT_DIAL_DIRECTION_FETCH);
89 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
90 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
91 20eb36d0 2020-03-18 stsp else
92 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
93 82ebf666 2020-03-18 stsp return err;
94 849f7557 2020-03-18 stsp }
95 849f7557 2020-03-18 stsp
96 93658fb9 2020-03-18 stsp const struct got_error*
97 07e52fce 2020-03-18 stsp got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
98 469dd726 2020-03-20 stsp struct got_pathlist_head *symrefs, const char *remote_name,
99 4ba14133 2020-03-20 stsp int mirror_references, int fetch_all_branches,
100 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
101 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
102 0e4002ca 2020-03-21 stsp int fetchfd, struct got_repository *repo,
103 469dd726 2020-03-20 stsp got_fetch_progress_cb progress_cb, void *progress_arg)
104 93658fb9 2020-03-18 stsp {
105 16aeacf7 2020-11-26 stsp size_t i;
106 20eb36d0 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2];
107 20eb36d0 2020-03-18 stsp int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
108 16aeacf7 2020-11-26 stsp int tmpfds[3];
109 85e8591f 2020-03-18 stsp int fetchstatus, idxstatus, done = 0;
110 93658fb9 2020-03-18 stsp const struct got_error *err;
111 85e8591f 2020-03-18 stsp struct imsgbuf fetchibuf, idxibuf;
112 85e8591f 2020-03-18 stsp pid_t fetchpid, idxpid;
113 bb64b798 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL;
114 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
115 41b0de12 2020-03-21 stsp const char *repo_path = NULL;
116 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
117 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
118 7848a0e1 2020-03-19 stsp struct got_reflist_head my_refs;
119 7848a0e1 2020-03-19 stsp struct got_reflist_entry *re;
120 d2cdc636 2020-03-18 stsp off_t packfile_size = 0;
121 393fb88d 2020-03-21 stsp struct got_packfile_hdr pack_hdr;
122 393fb88d 2020-03-21 stsp uint32_t nobj = 0;
123 ee61b6d3 2020-03-18 stsp char *path;
124 f1c6967f 2020-03-19 stsp char *progress = NULL;
125 92dc95a8 2020-03-24 stsp
126 92dc95a8 2020-03-24 stsp *pack_hash = NULL;
127 41b0de12 2020-03-21 stsp
128 0e4002ca 2020-03-21 stsp /*
129 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
130 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
131 0e4002ca 2020-03-21 stsp */
132 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
133 0e4002ca 2020-03-21 stsp const char *refname = pe->path;
134 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
135 0e4002ca 2020-03-21 stsp strncmp(refname, "got/", 4) == 0 ||
136 0e4002ca 2020-03-21 stsp strncmp(refname, "refs/remotes/", 13) == 0 ||
137 0e4002ca 2020-03-21 stsp strncmp(refname, "remotes/", 8) == 0)
138 0e4002ca 2020-03-21 stsp return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
139 0e4002ca 2020-03-21 stsp }
140 0e4002ca 2020-03-21 stsp
141 41b0de12 2020-03-21 stsp if (!list_refs_only)
142 41b0de12 2020-03-21 stsp repo_path = got_repo_get_path_git_dir(repo);
143 abe0f35f 2020-03-18 stsp
144 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++)
145 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
146 93658fb9 2020-03-18 stsp
147 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
148 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
149 7848a0e1 2020-03-19 stsp
150 41b0de12 2020-03-21 stsp if (!list_refs_only) {
151 41b0de12 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL,
152 41b0de12 2020-03-21 stsp got_ref_cmp_by_name, NULL);
153 41b0de12 2020-03-21 stsp if (err)
154 41b0de12 2020-03-21 stsp goto done;
155 41b0de12 2020-03-21 stsp }
156 7848a0e1 2020-03-19 stsp
157 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
158 7848a0e1 2020-03-19 stsp struct got_object_id *id;
159 7848a0e1 2020-03-19 stsp const char *refname;
160 7848a0e1 2020-03-19 stsp
161 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(re->ref))
162 7848a0e1 2020-03-19 stsp continue;
163 33501562 2020-03-18 stsp
164 726fb900 2021-09-28 thomas err = got_ref_resolve(&id, repo, re->ref);
165 726fb900 2021-09-28 thomas if (err)
166 726fb900 2021-09-28 thomas goto done;
167 726fb900 2021-09-28 thomas refname = strdup(got_ref_get_name(re->ref));
168 726fb900 2021-09-28 thomas if (refname == NULL) {
169 726fb900 2021-09-28 thomas err = got_error_from_errno("strdup");
170 726fb900 2021-09-28 thomas goto done;
171 469dd726 2020-03-20 stsp }
172 726fb900 2021-09-28 thomas err = got_pathlist_append(&have_refs, refname, id);
173 726fb900 2021-09-28 thomas if (err)
174 726fb900 2021-09-28 thomas goto done;
175 7848a0e1 2020-03-19 stsp }
176 7848a0e1 2020-03-19 stsp
177 41b0de12 2020-03-21 stsp if (list_refs_only) {
178 41b0de12 2020-03-21 stsp packfd = got_opentempfd();
179 41b0de12 2020-03-21 stsp if (packfd == -1) {
180 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
181 41b0de12 2020-03-21 stsp goto done;
182 41b0de12 2020-03-21 stsp }
183 41b0de12 2020-03-21 stsp } else {
184 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.pack",
185 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
186 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
187 41b0de12 2020-03-21 stsp goto done;
188 41b0de12 2020-03-21 stsp }
189 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
190 41b0de12 2020-03-21 stsp free(path);
191 41b0de12 2020-03-21 stsp if (err)
192 0843a4ce 2020-10-31 semarie goto done;
193 0843a4ce 2020-10-31 semarie if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
194 0843a4ce 2020-10-31 semarie err = got_error_from_errno2("fchmod", tmppackpath);
195 41b0de12 2020-03-21 stsp goto done;
196 0843a4ce 2020-10-31 semarie }
197 8e278d17 2020-03-18 stsp }
198 41b0de12 2020-03-21 stsp if (list_refs_only) {
199 41b0de12 2020-03-21 stsp idxfd = got_opentempfd();
200 41b0de12 2020-03-21 stsp if (idxfd == -1) {
201 41b0de12 2020-03-21 stsp err = got_error_from_errno("got_opentempfd");
202 41b0de12 2020-03-21 stsp goto done;
203 41b0de12 2020-03-21 stsp }
204 41b0de12 2020-03-21 stsp } else {
205 41b0de12 2020-03-21 stsp if (asprintf(&path, "%s/%s/fetching.idx",
206 41b0de12 2020-03-21 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
207 41b0de12 2020-03-21 stsp err = got_error_from_errno("asprintf");
208 41b0de12 2020-03-21 stsp goto done;
209 41b0de12 2020-03-21 stsp }
210 41b0de12 2020-03-21 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
211 41b0de12 2020-03-21 stsp free(path);
212 41b0de12 2020-03-21 stsp if (err)
213 0843a4ce 2020-10-31 semarie goto done;
214 0843a4ce 2020-10-31 semarie if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
215 0843a4ce 2020-10-31 semarie err = got_error_from_errno2("fchmod", tmpidxpath);
216 41b0de12 2020-03-21 stsp goto done;
217 0843a4ce 2020-10-31 semarie }
218 ee61b6d3 2020-03-18 stsp }
219 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
220 8e278d17 2020-03-18 stsp if (nidxfd == -1) {
221 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
222 8e278d17 2020-03-18 stsp goto done;
223 8e278d17 2020-03-18 stsp }
224 93658fb9 2020-03-18 stsp
225 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
226 d582f26c 2020-03-18 stsp tmpfds[i] = got_opentempfd();
227 d582f26c 2020-03-18 stsp if (tmpfds[i] == -1) {
228 d582f26c 2020-03-18 stsp err = got_error_from_errno("got_opentempfd");
229 d582f26c 2020-03-18 stsp goto done;
230 d582f26c 2020-03-18 stsp }
231 4788f1ce 2020-03-18 stsp }
232 4788f1ce 2020-03-18 stsp
233 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
234 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
235 8e278d17 2020-03-18 stsp goto done;
236 8e278d17 2020-03-18 stsp }
237 93658fb9 2020-03-18 stsp
238 85e8591f 2020-03-18 stsp fetchpid = fork();
239 85e8591f 2020-03-18 stsp if (fetchpid == -1) {
240 8e278d17 2020-03-18 stsp err = got_error_from_errno("fork");
241 8e278d17 2020-03-18 stsp goto done;
242 85e8591f 2020-03-18 stsp } else if (fetchpid == 0){
243 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds,
244 12491971 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK, tmppackpath);
245 93658fb9 2020-03-18 stsp }
246 93658fb9 2020-03-18 stsp
247 08578a35 2021-01-22 stsp if (close(imsg_fetchfds[1]) == -1) {
248 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
249 8e278d17 2020-03-18 stsp goto done;
250 8e278d17 2020-03-18 stsp }
251 85e8591f 2020-03-18 stsp imsg_init(&fetchibuf, imsg_fetchfds[0]);
252 20eb36d0 2020-03-18 stsp nfetchfd = dup(fetchfd);
253 20eb36d0 2020-03-18 stsp if (nfetchfd == -1) {
254 20eb36d0 2020-03-18 stsp err = got_error_from_errno("dup");
255 20eb36d0 2020-03-18 stsp goto done;
256 20eb36d0 2020-03-18 stsp }
257 659e7fbd 2020-03-20 stsp err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
258 0e4002ca 2020-03-21 stsp fetch_all_branches, wanted_branches, wanted_refs,
259 0e4002ca 2020-03-21 stsp list_refs_only, verbosity);
260 93658fb9 2020-03-18 stsp if (err != NULL)
261 8e278d17 2020-03-18 stsp goto done;
262 20eb36d0 2020-03-18 stsp nfetchfd = -1;
263 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
264 8e278d17 2020-03-18 stsp if (npackfd == -1) {
265 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
266 8e278d17 2020-03-18 stsp goto done;
267 8e278d17 2020-03-18 stsp }
268 393fb88d 2020-03-21 stsp err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
269 393fb88d 2020-03-21 stsp if (err != NULL)
270 393fb88d 2020-03-21 stsp goto done;
271 393fb88d 2020-03-21 stsp npackfd = -1;
272 8e278d17 2020-03-18 stsp
273 d2cdc636 2020-03-18 stsp packfile_size = 0;
274 77d7d3bb 2021-09-05 stsp progress = calloc(GOT_PKT_MAX, 1);
275 f1c6967f 2020-03-19 stsp if (progress == NULL) {
276 f1c6967f 2020-03-19 stsp err = got_error_from_errno("calloc");
277 f1c6967f 2020-03-19 stsp goto done;
278 f1c6967f 2020-03-19 stsp }
279 1d72a2a0 2020-03-24 stsp
280 1d72a2a0 2020-03-24 stsp *pack_hash = calloc(1, sizeof(**pack_hash));
281 1d72a2a0 2020-03-24 stsp if (*pack_hash == NULL) {
282 1d72a2a0 2020-03-24 stsp err = got_error_from_errno("calloc");
283 1d72a2a0 2020-03-24 stsp goto done;
284 1d72a2a0 2020-03-24 stsp }
285 1d72a2a0 2020-03-24 stsp
286 8f2d01a6 2020-03-18 stsp while (!done) {
287 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = NULL;
288 d9b4d0c0 2020-03-18 stsp char *refname = NULL;
289 531c3985 2020-03-18 stsp char *server_progress = NULL;
290 7848a0e1 2020-03-19 stsp off_t packfile_size_cur = 0;
291 8e278d17 2020-03-18 stsp
292 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
293 d2cdc636 2020-03-18 stsp &id, &refname, symrefs, &server_progress,
294 1d72a2a0 2020-03-24 stsp &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
295 8f2d01a6 2020-03-18 stsp if (err != NULL)
296 8e278d17 2020-03-18 stsp goto done;
297 1d72a2a0 2020-03-24 stsp if (!done && refname && id) {
298 41b0de12 2020-03-21 stsp err = got_pathlist_insert(NULL, refs, refname, id);
299 8f2d01a6 2020-03-18 stsp if (err)
300 8e278d17 2020-03-18 stsp goto done;
301 1d72a2a0 2020-03-24 stsp } else if (!done && server_progress) {
302 f1c6967f 2020-03-19 stsp char *p;
303 f1c6967f 2020-03-19 stsp /*
304 f1c6967f 2020-03-19 stsp * XXX git-daemon tends to send batched output with
305 f1c6967f 2020-03-19 stsp * lines spanning separate packets. Buffer progress
306 f1c6967f 2020-03-19 stsp * output until we see a CR or LF to avoid giving
307 f1c6967f 2020-03-19 stsp * partial lines of progress output to the callback.
308 f1c6967f 2020-03-19 stsp */
309 f1c6967f 2020-03-19 stsp if (strlcat(progress, server_progress,
310 77d7d3bb 2021-09-05 stsp GOT_PKT_MAX) >= GOT_PKT_MAX) {
311 f1c6967f 2020-03-19 stsp progress[0] = '\0'; /* discard */
312 f1c6967f 2020-03-19 stsp continue;
313 f1c6967f 2020-03-19 stsp }
314 f1c6967f 2020-03-19 stsp while ((p = strchr(progress, '\r')) != NULL ||
315 f1c6967f 2020-03-19 stsp (p = strchr(progress, '\n')) != NULL) {
316 f1c6967f 2020-03-19 stsp char *s;
317 f1c6967f 2020-03-19 stsp size_t n;
318 f1c6967f 2020-03-19 stsp char c = *p;
319 f1c6967f 2020-03-19 stsp *p = '\0';
320 f1c6967f 2020-03-19 stsp if (asprintf(&s, "%s%s", progress,
321 f1c6967f 2020-03-19 stsp c == '\n' ? "\n" : "") == -1) {
322 f1c6967f 2020-03-19 stsp err = got_error_from_errno("asprintf");
323 f1c6967f 2020-03-19 stsp goto done;
324 f1c6967f 2020-03-19 stsp }
325 668a20f6 2020-03-18 stsp err = progress_cb(progress_arg, s,
326 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
327 f1c6967f 2020-03-19 stsp free(s);
328 531c3985 2020-03-18 stsp if (err)
329 531c3985 2020-03-18 stsp break;
330 f1c6967f 2020-03-19 stsp n = strlen(progress);
331 77d7d3bb 2021-09-05 stsp if (n < GOT_PKT_MAX - 1) {
332 f1c6967f 2020-03-19 stsp memmove(progress, &progress[n + 1],
333 77d7d3bb 2021-09-05 stsp GOT_PKT_MAX - n - 1);
334 f1c6967f 2020-03-19 stsp } else
335 f1c6967f 2020-03-19 stsp progress[0] = '\0';
336 531c3985 2020-03-18 stsp }
337 531c3985 2020-03-18 stsp free(server_progress);
338 531c3985 2020-03-18 stsp if (err)
339 531c3985 2020-03-18 stsp goto done;
340 1d72a2a0 2020-03-24 stsp } else if (!done && packfile_size_cur != packfile_size) {
341 d2cdc636 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
342 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
343 d2cdc636 2020-03-18 stsp if (err)
344 d2cdc636 2020-03-18 stsp break;
345 d2cdc636 2020-03-18 stsp packfile_size = packfile_size_cur;
346 8f2d01a6 2020-03-18 stsp }
347 8f2d01a6 2020-03-18 stsp }
348 85e8591f 2020-03-18 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
349 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
350 393fb88d 2020-03-21 stsp goto done;
351 393fb88d 2020-03-21 stsp }
352 393fb88d 2020-03-21 stsp
353 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
354 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
355 8e278d17 2020-03-18 stsp goto done;
356 8e278d17 2020-03-18 stsp }
357 849f7557 2020-03-18 stsp
358 7848a0e1 2020-03-19 stsp /* If zero data was fetched without error we are already up-to-date. */
359 1d72a2a0 2020-03-24 stsp if (packfile_size == 0) {
360 1d72a2a0 2020-03-24 stsp free(*pack_hash);
361 1d72a2a0 2020-03-24 stsp *pack_hash = NULL;
362 393fb88d 2020-03-21 stsp goto done;
363 ccf6dd5e 2020-12-19 stsp } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
364 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
365 393fb88d 2020-03-21 stsp goto done;
366 393fb88d 2020-03-21 stsp } else {
367 393fb88d 2020-03-21 stsp ssize_t n;
368 393fb88d 2020-03-21 stsp
369 ccf6dd5e 2020-12-19 stsp n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
370 393fb88d 2020-03-21 stsp if (n == -1) {
371 393fb88d 2020-03-21 stsp err = got_error_from_errno("read");
372 393fb88d 2020-03-21 stsp goto done;
373 393fb88d 2020-03-21 stsp }
374 ccf6dd5e 2020-12-19 stsp if (n != ssizeof(pack_hdr)) {
375 393fb88d 2020-03-21 stsp err = got_error(GOT_ERR_IO);
376 393fb88d 2020-03-21 stsp goto done;
377 393fb88d 2020-03-21 stsp }
378 393fb88d 2020-03-21 stsp if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
379 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
380 393fb88d 2020-03-21 stsp "bad pack file signature");
381 393fb88d 2020-03-21 stsp goto done;
382 393fb88d 2020-03-21 stsp }
383 393fb88d 2020-03-21 stsp if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
384 393fb88d 2020-03-21 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
385 393fb88d 2020-03-21 stsp "bad pack file version");
386 393fb88d 2020-03-21 stsp goto done;
387 393fb88d 2020-03-21 stsp }
388 78fb0967 2020-09-09 naddy nobj = be32toh(pack_hdr.nobjects);
389 393fb88d 2020-03-21 stsp if (nobj == 0 &&
390 ccf6dd5e 2020-12-19 stsp packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
391 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
392 393fb88d 2020-03-21 stsp "bad pack file with zero objects");
393 393fb88d 2020-03-21 stsp if (nobj != 0 &&
394 ccf6dd5e 2020-12-19 stsp packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
395 393fb88d 2020-03-21 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
396 393fb88d 2020-03-21 stsp "empty pack file with non-zero object count");
397 393fb88d 2020-03-21 stsp }
398 393fb88d 2020-03-21 stsp
399 393fb88d 2020-03-21 stsp /*
400 393fb88d 2020-03-21 stsp * If the pack file contains no objects, we may only need to update
401 393fb88d 2020-03-21 stsp * references in our repository. The caller will take care of that.
402 393fb88d 2020-03-21 stsp */
403 393fb88d 2020-03-21 stsp if (nobj == 0)
404 849f7557 2020-03-18 stsp goto done;
405 393fb88d 2020-03-21 stsp
406 393fb88d 2020-03-21 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
407 393fb88d 2020-03-21 stsp err = got_error_from_errno("lseek");
408 393fb88d 2020-03-21 stsp goto done;
409 393fb88d 2020-03-21 stsp }
410 531c3985 2020-03-18 stsp
411 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
412 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
413 8e278d17 2020-03-18 stsp goto done;
414 8e278d17 2020-03-18 stsp }
415 85e8591f 2020-03-18 stsp idxpid = fork();
416 85e8591f 2020-03-18 stsp if (idxpid == -1) {
417 8e278d17 2020-03-18 stsp err= got_error_from_errno("fork");
418 8e278d17 2020-03-18 stsp goto done;
419 85e8591f 2020-03-18 stsp } else if (idxpid == 0)
420 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds,
421 12491971 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK, tmppackpath);
422 08578a35 2021-01-22 stsp if (close(imsg_idxfds[1]) == -1) {
423 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
424 8e278d17 2020-03-18 stsp goto done;
425 8e278d17 2020-03-18 stsp }
426 85e8591f 2020-03-18 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
427 93658fb9 2020-03-18 stsp
428 393fb88d 2020-03-21 stsp npackfd = dup(packfd);
429 393fb88d 2020-03-21 stsp if (npackfd == -1) {
430 393fb88d 2020-03-21 stsp err = got_error_from_errno("dup");
431 393fb88d 2020-03-21 stsp goto done;
432 393fb88d 2020-03-21 stsp }
433 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
434 668a20f6 2020-03-18 stsp npackfd);
435 93658fb9 2020-03-18 stsp if (err != NULL)
436 8e278d17 2020-03-18 stsp goto done;
437 8e278d17 2020-03-18 stsp npackfd = -1;
438 73ab1060 2020-03-18 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
439 93658fb9 2020-03-18 stsp if (err != NULL)
440 8e278d17 2020-03-18 stsp goto done;
441 8e278d17 2020-03-18 stsp nidxfd = -1;
442 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
443 d582f26c 2020-03-18 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
444 d582f26c 2020-03-18 stsp if (err != NULL)
445 d582f26c 2020-03-18 stsp goto done;
446 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
447 d582f26c 2020-03-18 stsp }
448 baa9fea0 2020-03-18 stsp done = 0;
449 baa9fea0 2020-03-18 stsp while (!done) {
450 668a20f6 2020-03-18 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
451 668a20f6 2020-03-18 stsp
452 668a20f6 2020-03-18 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
453 668a20f6 2020-03-18 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
454 668a20f6 2020-03-18 stsp &idxibuf);
455 baa9fea0 2020-03-18 stsp if (err != NULL)
456 baa9fea0 2020-03-18 stsp goto done;
457 668a20f6 2020-03-18 stsp if (nobj_indexed != 0) {
458 baa9fea0 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
459 668a20f6 2020-03-18 stsp packfile_size, nobj_total,
460 668a20f6 2020-03-18 stsp nobj_indexed, nobj_loose, nobj_resolved);
461 baa9fea0 2020-03-18 stsp if (err)
462 baa9fea0 2020-03-18 stsp break;
463 baa9fea0 2020-03-18 stsp }
464 baa9fea0 2020-03-18 stsp imsg_clear(&idxibuf);
465 baa9fea0 2020-03-18 stsp }
466 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1) {
467 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
468 8e278d17 2020-03-18 stsp goto done;
469 8e278d17 2020-03-18 stsp }
470 85e8591f 2020-03-18 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
471 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
472 8e278d17 2020-03-18 stsp goto done;
473 8e278d17 2020-03-18 stsp }
474 93658fb9 2020-03-18 stsp
475 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, *pack_hash);
476 afa77e03 2020-03-18 stsp if (err)
477 8e278d17 2020-03-18 stsp goto done;
478 66cba96f 2020-03-18 stsp if (asprintf(&packpath, "%s/%s/pack-%s.pack",
479 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
480 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
481 8e278d17 2020-03-18 stsp goto done;
482 8e278d17 2020-03-18 stsp }
483 93658fb9 2020-03-18 stsp
484 66cba96f 2020-03-18 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
485 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
486 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
487 8e278d17 2020-03-18 stsp goto done;
488 8e278d17 2020-03-18 stsp }
489 afa77e03 2020-03-18 stsp
490 8e278d17 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1) {
491 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmppackpath, packpath);
492 8e278d17 2020-03-18 stsp goto done;
493 8e278d17 2020-03-18 stsp }
494 7848a0e1 2020-03-19 stsp free(tmppackpath);
495 7848a0e1 2020-03-19 stsp tmppackpath = NULL;
496 8e278d17 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1) {
497 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
498 8e278d17 2020-03-18 stsp goto done;
499 8e278d17 2020-03-18 stsp }
500 7848a0e1 2020-03-19 stsp free(tmpidxpath);
501 7848a0e1 2020-03-19 stsp tmpidxpath = NULL;
502 ee61b6d3 2020-03-18 stsp
503 8e278d17 2020-03-18 stsp done:
504 7848a0e1 2020-03-19 stsp if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
505 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmppackpath);
506 7848a0e1 2020-03-19 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
507 7848a0e1 2020-03-19 stsp err = got_error_from_errno2("unlink", tmpidxpath);
508 20eb36d0 2020-03-18 stsp if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
509 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
510 8e278d17 2020-03-18 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
511 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
512 8e278d17 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
513 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
514 8e278d17 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
515 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
516 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
517 d582f26c 2020-03-18 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
518 d582f26c 2020-03-18 stsp err = got_error_from_errno("close");
519 d582f26c 2020-03-18 stsp }
520 afa77e03 2020-03-18 stsp free(tmppackpath);
521 afa77e03 2020-03-18 stsp free(tmpidxpath);
522 fe4e1501 2020-03-18 stsp free(idxpath);
523 afa77e03 2020-03-18 stsp free(packpath);
524 f1c6967f 2020-03-19 stsp free(progress);
525 fe4e1501 2020-03-18 stsp
526 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
527 7848a0e1 2020-03-19 stsp free((char *)pe->path);
528 7848a0e1 2020-03-19 stsp free(pe->data);
529 7848a0e1 2020-03-19 stsp }
530 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
531 7848a0e1 2020-03-19 stsp got_ref_list_free(&my_refs);
532 d9b4d0c0 2020-03-18 stsp if (err) {
533 d9b4d0c0 2020-03-18 stsp free(*pack_hash);
534 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
535 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, refs, entry) {
536 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
537 d9b4d0c0 2020-03-18 stsp free(pe->data);
538 d9b4d0c0 2020-03-18 stsp }
539 d9b4d0c0 2020-03-18 stsp got_pathlist_free(refs);
540 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
541 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
542 d9b4d0c0 2020-03-18 stsp free(pe->data);
543 d9b4d0c0 2020-03-18 stsp }
544 d9b4d0c0 2020-03-18 stsp got_pathlist_free(symrefs);
545 d9b4d0c0 2020-03-18 stsp }
546 8e278d17 2020-03-18 stsp return err;
547 93658fb9 2020-03-18 stsp }