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