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