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