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