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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/resource.h>
23 #include <sys/socket.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <err.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <time.h>
39 #include <uuid.h>
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
49 #include "got_fetch.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_dial.h"
62 #include "got_lib_pkt.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 #ifndef ssizeof
69 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
70 #endif
72 #ifndef MIN
73 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 #endif
76 const struct got_error *
77 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
78 const char *host, const char *port, const char *server_path, int verbosity)
79 {
80 const struct got_error *err = NULL;
82 *fetchpid = -1;
83 *fetchfd = -1;
85 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
86 err = got_dial_ssh(fetchpid, fetchfd, host, port,
87 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
88 else if (strcmp(proto, "git") == 0)
89 err = got_dial_git(fetchfd, host, port, server_path,
90 GOT_DIAL_DIRECTION_FETCH);
91 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
92 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
93 else
94 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
95 return err;
96 }
98 const struct got_error*
99 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
100 struct got_pathlist_head *symrefs, const char *remote_name,
101 int mirror_references, int fetch_all_branches,
102 struct got_pathlist_head *wanted_branches,
103 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
104 int fetchfd, struct got_repository *repo,
105 got_fetch_progress_cb progress_cb, void *progress_arg)
107 size_t i;
108 int imsg_fetchfds[2], imsg_idxfds[2];
109 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
110 int tmpfds[3];
111 int fetchstatus, idxstatus, done = 0;
112 const struct got_error *err;
113 struct imsgbuf fetchibuf, idxibuf;
114 pid_t fetchpid, idxpid;
115 char *tmppackpath = NULL, *tmpidxpath = NULL;
116 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
117 const char *repo_path = NULL;
118 struct got_pathlist_head have_refs;
119 struct got_pathlist_entry *pe;
120 struct got_reflist_head my_refs;
121 struct got_reflist_entry *re;
122 off_t packfile_size = 0;
123 struct got_packfile_hdr pack_hdr;
124 uint32_t nobj = 0;
125 char *ref_prefix = NULL;
126 size_t ref_prefixlen = 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 (!mirror_references) {
155 if (asprintf(&ref_prefix, "refs/remotes/%s/",
156 remote_name) == -1)
157 return got_error_from_errno("asprintf");
158 ref_prefixlen = strlen(ref_prefix);
161 if (!list_refs_only) {
162 err = got_ref_list(&my_refs, repo, NULL,
163 got_ref_cmp_by_name, NULL);
164 if (err)
165 goto done;
168 TAILQ_FOREACH(re, &my_refs, entry) {
169 struct got_object_id *id;
170 const char *refname;
172 if (got_ref_is_symbolic(re->ref))
173 continue;
175 refname = got_ref_get_name(re->ref);
177 if (mirror_references) {
178 char *name;
179 err = got_ref_resolve(&id, repo, re->ref);
180 if (err)
181 goto done;
182 name = strdup(refname);
183 if (name == NULL) {
184 err = got_error_from_errno("strdup");
185 goto done;
187 err = got_pathlist_append(&have_refs, name, id);
188 if (err)
189 goto done;
190 continue;
193 if (strncmp("refs/tags/", refname, 10) == 0) {
194 char *tagname;
196 err = got_ref_resolve(&id, repo, re->ref);
197 if (err)
198 goto done;
199 tagname = strdup(refname);
200 if (tagname == NULL) {
201 err = got_error_from_errno("strdup");
202 goto done;
204 err = got_pathlist_append(&have_refs, tagname, id);
205 if (err) {
206 free(tagname);
207 goto done;
211 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
212 char *branchname;
214 err = got_ref_resolve(&id, repo, re->ref);
215 if (err)
216 goto done;
218 if (asprintf(&branchname, "refs/heads/%s",
219 refname + ref_prefixlen) == -1) {
220 err = got_error_from_errno("asprintf");
221 goto done;
223 err = got_pathlist_append(&have_refs, branchname, id);
224 if (err) {
225 free(branchname);
226 goto done;
231 if (list_refs_only) {
232 packfd = got_opentempfd();
233 if (packfd == -1) {
234 err = got_error_from_errno("got_opentempfd");
235 goto done;
237 } else {
238 if (asprintf(&path, "%s/%s/fetching.pack",
239 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
243 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
244 free(path);
245 if (err)
246 goto done;
247 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
248 err = got_error_from_errno2("fchmod", tmppackpath);
249 goto done;
252 if (list_refs_only) {
253 idxfd = got_opentempfd();
254 if (idxfd == -1) {
255 err = got_error_from_errno("got_opentempfd");
256 goto done;
258 } else {
259 if (asprintf(&path, "%s/%s/fetching.idx",
260 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
261 err = got_error_from_errno("asprintf");
262 goto done;
264 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
265 free(path);
266 if (err)
267 goto done;
268 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
269 err = got_error_from_errno2("fchmod", tmpidxpath);
270 goto done;
273 nidxfd = dup(idxfd);
274 if (nidxfd == -1) {
275 err = got_error_from_errno("dup");
276 goto done;
279 for (i = 0; i < nitems(tmpfds); i++) {
280 tmpfds[i] = got_opentempfd();
281 if (tmpfds[i] == -1) {
282 err = got_error_from_errno("got_opentempfd");
283 goto done;
287 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
288 err = got_error_from_errno("socketpair");
289 goto done;
292 fetchpid = fork();
293 if (fetchpid == -1) {
294 err = got_error_from_errno("fork");
295 goto done;
296 } else if (fetchpid == 0){
297 got_privsep_exec_child(imsg_fetchfds,
298 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
301 if (close(imsg_fetchfds[1]) == -1) {
302 err = got_error_from_errno("close");
303 goto done;
305 imsg_init(&fetchibuf, imsg_fetchfds[0]);
306 nfetchfd = dup(fetchfd);
307 if (nfetchfd == -1) {
308 err = got_error_from_errno("dup");
309 goto done;
311 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
312 fetch_all_branches, wanted_branches, wanted_refs,
313 list_refs_only, verbosity);
314 if (err != NULL)
315 goto done;
316 nfetchfd = -1;
317 npackfd = dup(packfd);
318 if (npackfd == -1) {
319 err = got_error_from_errno("dup");
320 goto done;
322 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
323 if (err != NULL)
324 goto done;
325 npackfd = -1;
327 packfile_size = 0;
328 progress = calloc(GOT_PKT_MAX, 1);
329 if (progress == NULL) {
330 err = got_error_from_errno("calloc");
331 goto done;
334 *pack_hash = calloc(1, sizeof(**pack_hash));
335 if (*pack_hash == NULL) {
336 err = got_error_from_errno("calloc");
337 goto done;
340 while (!done) {
341 struct got_object_id *id = NULL;
342 char *refname = NULL;
343 char *server_progress = NULL;
344 off_t packfile_size_cur = 0;
346 err = got_privsep_recv_fetch_progress(&done,
347 &id, &refname, symrefs, &server_progress,
348 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
349 if (err != NULL)
350 goto done;
351 if (!done && refname && id) {
352 err = got_pathlist_insert(NULL, refs, refname, id);
353 if (err)
354 goto done;
355 } else if (!done && server_progress) {
356 char *p;
357 /*
358 * XXX git-daemon tends to send batched output with
359 * lines spanning separate packets. Buffer progress
360 * output until we see a CR or LF to avoid giving
361 * partial lines of progress output to the callback.
362 */
363 if (strlcat(progress, server_progress,
364 GOT_PKT_MAX) >= GOT_PKT_MAX) {
365 progress[0] = '\0'; /* discard */
366 continue;
368 while ((p = strchr(progress, '\r')) != NULL ||
369 (p = strchr(progress, '\n')) != NULL) {
370 char *s;
371 size_t n;
372 char c = *p;
373 *p = '\0';
374 if (asprintf(&s, "%s%s", progress,
375 c == '\n' ? "\n" : "") == -1) {
376 err = got_error_from_errno("asprintf");
377 goto done;
379 err = progress_cb(progress_arg, s,
380 packfile_size_cur, 0, 0, 0, 0);
381 free(s);
382 if (err)
383 break;
384 n = strlen(progress);
385 if (n < GOT_PKT_MAX - 1) {
386 memmove(progress, &progress[n + 1],
387 GOT_PKT_MAX - n - 1);
388 } else
389 progress[0] = '\0';
391 free(server_progress);
392 if (err)
393 goto done;
394 } else if (!done && packfile_size_cur != packfile_size) {
395 err = progress_cb(progress_arg, NULL,
396 packfile_size_cur, 0, 0, 0, 0);
397 if (err)
398 break;
399 packfile_size = packfile_size_cur;
402 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
403 err = got_error_from_errno("waitpid");
404 goto done;
407 if (lseek(packfd, 0, SEEK_SET) == -1) {
408 err = got_error_from_errno("lseek");
409 goto done;
412 /* If zero data was fetched without error we are already up-to-date. */
413 if (packfile_size == 0) {
414 free(*pack_hash);
415 *pack_hash = NULL;
416 goto done;
417 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
418 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
419 goto done;
420 } else {
421 ssize_t n;
423 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
424 if (n == -1) {
425 err = got_error_from_errno("read");
426 goto done;
428 if (n != ssizeof(pack_hdr)) {
429 err = got_error(GOT_ERR_IO);
430 goto done;
432 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
433 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
434 "bad pack file signature");
435 goto done;
437 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
438 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
439 "bad pack file version");
440 goto done;
442 nobj = be32toh(pack_hdr.nobjects);
443 if (nobj == 0 &&
444 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
445 return got_error_msg(GOT_ERR_BAD_PACKFILE,
446 "bad pack file with zero objects");
447 if (nobj != 0 &&
448 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
449 return got_error_msg(GOT_ERR_BAD_PACKFILE,
450 "empty pack file with non-zero object count");
453 /*
454 * If the pack file contains no objects, we may only need to update
455 * references in our repository. The caller will take care of that.
456 */
457 if (nobj == 0)
458 goto done;
460 if (lseek(packfd, 0, SEEK_SET) == -1) {
461 err = got_error_from_errno("lseek");
462 goto done;
465 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
466 err = got_error_from_errno("socketpair");
467 goto done;
469 idxpid = fork();
470 if (idxpid == -1) {
471 err= got_error_from_errno("fork");
472 goto done;
473 } else if (idxpid == 0)
474 got_privsep_exec_child(imsg_idxfds,
475 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
476 if (close(imsg_idxfds[1]) == -1) {
477 err = got_error_from_errno("close");
478 goto done;
480 imsg_init(&idxibuf, imsg_idxfds[0]);
482 npackfd = dup(packfd);
483 if (npackfd == -1) {
484 err = got_error_from_errno("dup");
485 goto done;
487 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
488 npackfd);
489 if (err != NULL)
490 goto done;
491 npackfd = -1;
492 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
493 if (err != NULL)
494 goto done;
495 nidxfd = -1;
496 for (i = 0; i < nitems(tmpfds); i++) {
497 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
498 if (err != NULL)
499 goto done;
500 tmpfds[i] = -1;
502 done = 0;
503 while (!done) {
504 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
506 err = got_privsep_recv_index_progress(&done, &nobj_total,
507 &nobj_indexed, &nobj_loose, &nobj_resolved,
508 &idxibuf);
509 if (err != NULL)
510 goto done;
511 if (nobj_indexed != 0) {
512 err = progress_cb(progress_arg, NULL,
513 packfile_size, nobj_total,
514 nobj_indexed, nobj_loose, nobj_resolved);
515 if (err)
516 break;
518 imsg_clear(&idxibuf);
520 if (close(imsg_idxfds[0]) == -1) {
521 err = got_error_from_errno("close");
522 goto done;
524 if (waitpid(idxpid, &idxstatus, 0) == -1) {
525 err = got_error_from_errno("waitpid");
526 goto done;
529 err = got_object_id_str(&id_str, *pack_hash);
530 if (err)
531 goto done;
532 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
533 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
534 err = got_error_from_errno("asprintf");
535 goto done;
538 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
539 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
540 err = got_error_from_errno("asprintf");
541 goto done;
544 if (rename(tmppackpath, packpath) == -1) {
545 err = got_error_from_errno3("rename", tmppackpath, packpath);
546 goto done;
548 free(tmppackpath);
549 tmppackpath = NULL;
550 if (rename(tmpidxpath, idxpath) == -1) {
551 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
552 goto done;
554 free(tmpidxpath);
555 tmpidxpath = NULL;
557 done:
558 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
559 err = got_error_from_errno2("unlink", tmppackpath);
560 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
561 err = got_error_from_errno2("unlink", tmpidxpath);
562 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
563 err = got_error_from_errno("close");
564 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
565 err = got_error_from_errno("close");
566 if (packfd != -1 && close(packfd) == -1 && err == NULL)
567 err = got_error_from_errno("close");
568 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
569 err = got_error_from_errno("close");
570 for (i = 0; i < nitems(tmpfds); i++) {
571 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
572 err = got_error_from_errno("close");
574 free(tmppackpath);
575 free(tmpidxpath);
576 free(idxpath);
577 free(packpath);
578 free(ref_prefix);
579 free(progress);
581 TAILQ_FOREACH(pe, &have_refs, entry) {
582 free((char *)pe->path);
583 free(pe->data);
585 got_pathlist_free(&have_refs);
586 got_ref_list_free(&my_refs);
587 if (err) {
588 free(*pack_hash);
589 *pack_hash = NULL;
590 TAILQ_FOREACH(pe, refs, entry) {
591 free((void *)pe->path);
592 free(pe->data);
594 got_pathlist_free(refs);
595 TAILQ_FOREACH(pe, symrefs, entry) {
596 free((void *)pe->path);
597 free(pe->data);
599 got_pathlist_free(symrefs);
601 return err;