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