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