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