Blame


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