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