Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <sha1.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <ctype.h>
38 #include <limits.h>
39 #include <imsg.h>
40 #include <time.h>
41 #include <uuid.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
51 #include "got_fetch.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_sha1.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_dial.h"
64 #include "got_lib_pkt.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 #ifndef ssizeof
71 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
72 #endif
74 #ifndef MIN
75 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
76 #endif
78 const struct got_error *
79 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
80 const char *host, const char *port, const char *server_path, int verbosity)
81 {
82 const struct got_error *err = NULL;
84 *fetchpid = -1;
85 *fetchfd = -1;
87 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
88 err = got_dial_ssh(fetchpid, fetchfd, host, port,
89 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
90 else if (strcmp(proto, "git") == 0)
91 err = got_dial_git(fetchfd, host, port, server_path,
92 GOT_DIAL_DIRECTION_FETCH);
93 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
94 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
95 else
96 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
97 return err;
98 }
100 const struct got_error*
101 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
102 struct got_pathlist_head *symrefs, const char *remote_name,
103 int mirror_references, int fetch_all_branches,
104 struct got_pathlist_head *wanted_branches,
105 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
106 int fetchfd, struct got_repository *repo,
107 got_fetch_progress_cb progress_cb, void *progress_arg)
109 size_t i;
110 int imsg_fetchfds[2], imsg_idxfds[2];
111 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
112 int tmpfds[3];
113 int fetchstatus, idxstatus, done = 0;
114 const struct got_error *err;
115 struct imsgbuf fetchibuf, idxibuf;
116 pid_t fetchpid, idxpid;
117 char *tmppackpath = NULL, *tmpidxpath = NULL;
118 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
119 const char *repo_path = NULL;
120 struct got_pathlist_head have_refs;
121 struct got_pathlist_entry *pe;
122 struct got_reflist_head my_refs;
123 struct got_reflist_entry *re;
124 off_t packfile_size = 0;
125 struct got_packfile_hdr pack_hdr;
126 uint32_t nobj = 0;
127 char *path;
128 char *progress = NULL;
130 *pack_hash = NULL;
132 /*
133 * Prevent fetching of references that won't make any
134 * sense outside of the remote repository's context.
135 */
136 TAILQ_FOREACH(pe, wanted_refs, entry) {
137 const char *refname = pe->path;
138 if (strncmp(refname, "refs/got/", 9) == 0 ||
139 strncmp(refname, "got/", 4) == 0 ||
140 strncmp(refname, "refs/remotes/", 13) == 0 ||
141 strncmp(refname, "remotes/", 8) == 0)
142 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
145 if (!list_refs_only)
146 repo_path = got_repo_get_path_git_dir(repo);
148 for (i = 0; i < nitems(tmpfds); i++)
149 tmpfds[i] = -1;
151 TAILQ_INIT(&have_refs);
152 TAILQ_INIT(&my_refs);
154 if (!list_refs_only) {
155 err = got_ref_list(&my_refs, repo, NULL,
156 got_ref_cmp_by_name, NULL);
157 if (err)
158 goto done;
161 TAILQ_FOREACH(re, &my_refs, entry) {
162 struct got_object_id *id;
163 const char *refname;
165 if (got_ref_is_symbolic(re->ref))
166 continue;
168 err = got_ref_resolve(&id, repo, re->ref);
169 if (err)
170 goto done;
171 refname = strdup(got_ref_get_name(re->ref));
172 if (refname == NULL) {
173 err = got_error_from_errno("strdup");
174 goto done;
176 err = got_pathlist_append(&have_refs, refname, id);
177 if (err)
178 goto done;
181 if (list_refs_only) {
182 packfd = got_opentempfd();
183 if (packfd == -1) {
184 err = got_error_from_errno("got_opentempfd");
185 goto done;
187 } else {
188 if (asprintf(&path, "%s/%s/fetching.pack",
189 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
190 err = got_error_from_errno("asprintf");
191 goto done;
193 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
194 free(path);
195 if (err)
196 goto done;
197 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
198 err = got_error_from_errno2("fchmod", tmppackpath);
199 goto done;
202 if (list_refs_only) {
203 idxfd = got_opentempfd();
204 if (idxfd == -1) {
205 err = got_error_from_errno("got_opentempfd");
206 goto done;
208 } else {
209 if (asprintf(&path, "%s/%s/fetching.idx",
210 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
211 err = got_error_from_errno("asprintf");
212 goto done;
214 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
215 free(path);
216 if (err)
217 goto done;
218 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
219 err = got_error_from_errno2("fchmod", tmpidxpath);
220 goto done;
223 nidxfd = dup(idxfd);
224 if (nidxfd == -1) {
225 err = got_error_from_errno("dup");
226 goto done;
229 for (i = 0; i < nitems(tmpfds); i++) {
230 tmpfds[i] = got_opentempfd();
231 if (tmpfds[i] == -1) {
232 err = got_error_from_errno("got_opentempfd");
233 goto done;
237 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
238 err = got_error_from_errno("socketpair");
239 goto done;
242 fetchpid = fork();
243 if (fetchpid == -1) {
244 err = got_error_from_errno("fork");
245 goto done;
246 } else if (fetchpid == 0){
247 got_privsep_exec_child(imsg_fetchfds,
248 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
251 if (close(imsg_fetchfds[1]) == -1) {
252 err = got_error_from_errno("close");
253 goto done;
255 imsg_init(&fetchibuf, imsg_fetchfds[0]);
256 nfetchfd = dup(fetchfd);
257 if (nfetchfd == -1) {
258 err = got_error_from_errno("dup");
259 goto done;
261 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
262 fetch_all_branches, wanted_branches, wanted_refs,
263 list_refs_only, verbosity);
264 if (err != NULL)
265 goto done;
266 nfetchfd = -1;
267 npackfd = dup(packfd);
268 if (npackfd == -1) {
269 err = got_error_from_errno("dup");
270 goto done;
272 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
273 if (err != NULL)
274 goto done;
275 npackfd = -1;
277 packfile_size = 0;
278 progress = calloc(GOT_PKT_MAX, 1);
279 if (progress == NULL) {
280 err = got_error_from_errno("calloc");
281 goto done;
284 *pack_hash = calloc(1, sizeof(**pack_hash));
285 if (*pack_hash == NULL) {
286 err = got_error_from_errno("calloc");
287 goto done;
290 while (!done) {
291 struct got_object_id *id = NULL;
292 char *refname = NULL;
293 char *server_progress = NULL;
294 off_t packfile_size_cur = 0;
296 err = got_privsep_recv_fetch_progress(&done,
297 &id, &refname, symrefs, &server_progress,
298 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
299 if (err != NULL)
300 goto done;
301 if (!done && refname && id) {
302 err = got_pathlist_insert(NULL, refs, refname, id);
303 if (err)
304 goto done;
305 } else if (!done && server_progress) {
306 char *p;
307 /*
308 * XXX git-daemon tends to send batched output with
309 * lines spanning separate packets. Buffer progress
310 * output until we see a CR or LF to avoid giving
311 * partial lines of progress output to the callback.
312 */
313 if (strlcat(progress, server_progress,
314 GOT_PKT_MAX) >= GOT_PKT_MAX) {
315 progress[0] = '\0'; /* discard */
316 continue;
318 while ((p = strchr(progress, '\r')) != NULL ||
319 (p = strchr(progress, '\n')) != NULL) {
320 char *s;
321 size_t n;
322 char c = *p;
323 *p = '\0';
324 if (asprintf(&s, "%s%s", progress,
325 c == '\n' ? "\n" : "") == -1) {
326 err = got_error_from_errno("asprintf");
327 goto done;
329 err = progress_cb(progress_arg, s,
330 packfile_size_cur, 0, 0, 0, 0);
331 free(s);
332 if (err)
333 break;
334 n = strlen(progress);
335 if (n < GOT_PKT_MAX - 1) {
336 memmove(progress, &progress[n + 1],
337 GOT_PKT_MAX - n - 1);
338 } else
339 progress[0] = '\0';
341 free(server_progress);
342 if (err)
343 goto done;
344 } else if (!done && packfile_size_cur != packfile_size) {
345 err = progress_cb(progress_arg, NULL,
346 packfile_size_cur, 0, 0, 0, 0);
347 if (err)
348 break;
349 packfile_size = packfile_size_cur;
352 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
353 err = got_error_from_errno("waitpid");
354 goto done;
357 if (lseek(packfd, 0, SEEK_SET) == -1) {
358 err = got_error_from_errno("lseek");
359 goto done;
362 /* If zero data was fetched without error we are already up-to-date. */
363 if (packfile_size == 0) {
364 free(*pack_hash);
365 *pack_hash = NULL;
366 goto done;
367 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
368 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
369 goto done;
370 } else {
371 ssize_t n;
373 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
374 if (n == -1) {
375 err = got_error_from_errno("read");
376 goto done;
378 if (n != ssizeof(pack_hdr)) {
379 err = got_error(GOT_ERR_IO);
380 goto done;
382 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
383 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
384 "bad pack file signature");
385 goto done;
387 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
388 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
389 "bad pack file version");
390 goto done;
392 nobj = be32toh(pack_hdr.nobjects);
393 if (nobj == 0 &&
394 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
395 return got_error_msg(GOT_ERR_BAD_PACKFILE,
396 "bad pack file with zero objects");
397 if (nobj != 0 &&
398 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
399 return got_error_msg(GOT_ERR_BAD_PACKFILE,
400 "empty pack file with non-zero object count");
403 /*
404 * If the pack file contains no objects, we may only need to update
405 * references in our repository. The caller will take care of that.
406 */
407 if (nobj == 0)
408 goto done;
410 if (lseek(packfd, 0, SEEK_SET) == -1) {
411 err = got_error_from_errno("lseek");
412 goto done;
415 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
416 err = got_error_from_errno("socketpair");
417 goto done;
419 idxpid = fork();
420 if (idxpid == -1) {
421 err= got_error_from_errno("fork");
422 goto done;
423 } else if (idxpid == 0)
424 got_privsep_exec_child(imsg_idxfds,
425 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
426 if (close(imsg_idxfds[1]) == -1) {
427 err = got_error_from_errno("close");
428 goto done;
430 imsg_init(&idxibuf, imsg_idxfds[0]);
432 npackfd = dup(packfd);
433 if (npackfd == -1) {
434 err = got_error_from_errno("dup");
435 goto done;
437 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
438 npackfd);
439 if (err != NULL)
440 goto done;
441 npackfd = -1;
442 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
443 if (err != NULL)
444 goto done;
445 nidxfd = -1;
446 for (i = 0; i < nitems(tmpfds); i++) {
447 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
448 if (err != NULL)
449 goto done;
450 tmpfds[i] = -1;
452 done = 0;
453 while (!done) {
454 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
456 err = got_privsep_recv_index_progress(&done, &nobj_total,
457 &nobj_indexed, &nobj_loose, &nobj_resolved,
458 &idxibuf);
459 if (err != NULL)
460 goto done;
461 if (nobj_indexed != 0) {
462 err = progress_cb(progress_arg, NULL,
463 packfile_size, nobj_total,
464 nobj_indexed, nobj_loose, nobj_resolved);
465 if (err)
466 break;
468 imsg_clear(&idxibuf);
470 if (close(imsg_idxfds[0]) == -1) {
471 err = got_error_from_errno("close");
472 goto done;
474 if (waitpid(idxpid, &idxstatus, 0) == -1) {
475 err = got_error_from_errno("waitpid");
476 goto done;
479 err = got_object_id_str(&id_str, *pack_hash);
480 if (err)
481 goto done;
482 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
483 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
484 err = got_error_from_errno("asprintf");
485 goto done;
488 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
489 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
490 err = got_error_from_errno("asprintf");
491 goto done;
494 if (rename(tmppackpath, packpath) == -1) {
495 err = got_error_from_errno3("rename", tmppackpath, packpath);
496 goto done;
498 free(tmppackpath);
499 tmppackpath = NULL;
500 if (rename(tmpidxpath, idxpath) == -1) {
501 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
502 goto done;
504 free(tmpidxpath);
505 tmpidxpath = NULL;
507 done:
508 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
509 err = got_error_from_errno2("unlink", tmppackpath);
510 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
511 err = got_error_from_errno2("unlink", tmpidxpath);
512 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
513 err = got_error_from_errno("close");
514 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
515 err = got_error_from_errno("close");
516 if (packfd != -1 && close(packfd) == -1 && err == NULL)
517 err = got_error_from_errno("close");
518 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
519 err = got_error_from_errno("close");
520 for (i = 0; i < nitems(tmpfds); i++) {
521 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
522 err = got_error_from_errno("close");
524 free(tmppackpath);
525 free(tmpidxpath);
526 free(idxpath);
527 free(packpath);
528 free(progress);
530 TAILQ_FOREACH(pe, &have_refs, entry) {
531 free((char *)pe->path);
532 free(pe->data);
534 got_pathlist_free(&have_refs);
535 got_ref_list_free(&my_refs);
536 if (err) {
537 free(*pack_hash);
538 *pack_hash = NULL;
539 TAILQ_FOREACH(pe, refs, entry) {
540 free((void *)pe->path);
541 free(pe->data);
543 got_pathlist_free(refs);
544 TAILQ_FOREACH(pe, symrefs, entry) {
545 free((void *)pe->path);
546 free(pe->data);
548 got_pathlist_free(symrefs);
550 return err;