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