2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <sys/socket.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
45 #include "got_error.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_object.h"
52 #include "got_opentemp.h"
53 #include "got_fetch.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_hash.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_dial.h"
66 #include "got_lib_pkt.h"
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
77 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
80 const struct got_error *
81 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
82 const char *host, const char *port, const char *server_path, int verbosity)
84 const struct got_error *err = NULL;
89 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
90 err = got_dial_ssh(fetchpid, fetchfd, host, port,
91 server_path, GOT_DIAL_CMD_FETCH, verbosity);
92 else if (strcmp(proto, "git") == 0)
93 err = got_dial_git(fetchfd, host, port, server_path,
95 else if (strcmp(proto, "http") == 0 ||
96 strcmp(proto, "git+http") == 0 ||
97 strcmp(proto, "https") == 0 ||
98 strcmp(proto, "git+https") == 0)
99 err = got_dial_http(fetchpid, fetchfd, host, port,
100 server_path, verbosity, strstr(proto, "https") != NULL);
102 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
106 const struct got_error *
107 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
108 struct got_pathlist_head *symrefs, const char *remote_name,
109 int mirror_references, int fetch_all_branches,
110 struct got_pathlist_head *wanted_branches,
111 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
112 int fetchfd, struct got_repository *repo, const char *worktree_refname,
113 const char *remote_head, int no_head, got_fetch_progress_cb progress_cb,
117 int imsg_fetchfds[2], imsg_idxfds[2];
118 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
120 int fetchstatus, idxstatus, done = 0;
121 const struct got_error *err;
122 struct imsgbuf fetchibuf, idxibuf;
123 pid_t fetchpid, idxpid;
124 char *tmppackpath = NULL, *tmpidxpath = NULL;
125 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
126 const char *repo_path = NULL;
127 struct got_pathlist_head have_refs;
128 struct got_pathlist_entry *pe;
129 struct got_reflist_head my_refs;
130 struct got_reflist_entry *re;
131 off_t packfile_size = 0;
132 struct got_packfile_hdr pack_hdr;
135 char *progress = NULL;
140 * Prevent fetching of references that won't make any
141 * sense outside of the remote repository's context.
143 TAILQ_FOREACH(pe, wanted_refs, entry) {
144 const char *refname = pe->path;
145 if (strncmp(refname, "refs/got/", 9) == 0 ||
146 strncmp(refname, "got/", 4) == 0 ||
147 strncmp(refname, "refs/remotes/", 13) == 0 ||
148 strncmp(refname, "remotes/", 8) == 0)
149 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
153 repo_path = got_repo_get_path_git_dir(repo);
155 for (i = 0; i < nitems(tmpfds); i++)
158 TAILQ_INIT(&have_refs);
159 TAILQ_INIT(&my_refs);
161 if (!list_refs_only) {
162 err = got_ref_list(&my_refs, repo, NULL,
163 got_ref_cmp_by_name, NULL);
168 TAILQ_FOREACH(re, &my_refs, entry) {
169 struct got_object_id *id;
172 if (got_ref_is_symbolic(re->ref))
175 err = got_ref_resolve(&id, repo, re->ref);
178 refname = strdup(got_ref_get_name(re->ref));
179 if (refname == NULL) {
180 err = got_error_from_errno("strdup");
183 err = got_pathlist_append(&have_refs, refname, id);
188 if (list_refs_only) {
189 packfd = got_opentempfd();
191 err = got_error_from_errno("got_opentempfd");
195 if (asprintf(&path, "%s/%s/fetching.pack",
196 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
197 err = got_error_from_errno("asprintf");
200 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
204 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
205 err = got_error_from_errno2("fchmod", tmppackpath);
209 if (list_refs_only) {
210 idxfd = got_opentempfd();
212 err = got_error_from_errno("got_opentempfd");
216 if (asprintf(&path, "%s/%s/fetching.idx",
217 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
218 err = got_error_from_errno("asprintf");
221 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
225 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
226 err = got_error_from_errno2("fchmod", tmpidxpath);
232 err = got_error_from_errno("dup");
236 for (i = 0; i < nitems(tmpfds); i++) {
237 tmpfds[i] = got_opentempfd();
238 if (tmpfds[i] == -1) {
239 err = got_error_from_errno("got_opentempfd");
244 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
245 err = got_error_from_errno("socketpair");
250 if (fetchpid == -1) {
251 err = got_error_from_errno("fork");
253 } else if (fetchpid == 0){
254 got_privsep_exec_child(imsg_fetchfds,
255 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
258 if (close(imsg_fetchfds[1]) == -1) {
259 err = got_error_from_errno("close");
262 imsg_init(&fetchibuf, imsg_fetchfds[0]);
263 nfetchfd = dup(fetchfd);
264 if (nfetchfd == -1) {
265 err = got_error_from_errno("dup");
268 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
269 fetch_all_branches, wanted_branches, wanted_refs,
270 list_refs_only, worktree_refname, remote_head, no_head, verbosity);
274 npackfd = dup(packfd);
276 err = got_error_from_errno("dup");
279 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
285 progress = calloc(GOT_PKT_MAX, 1);
286 if (progress == NULL) {
287 err = got_error_from_errno("calloc");
291 *pack_hash = calloc(1, sizeof(**pack_hash));
292 if (*pack_hash == NULL) {
293 err = got_error_from_errno("calloc");
298 struct got_object_id *id = NULL;
299 char *refname = NULL;
300 char *server_progress = NULL;
301 off_t packfile_size_cur = 0;
303 err = got_privsep_recv_fetch_progress(&done,
304 &id, &refname, symrefs, &server_progress,
305 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
308 /* Don't report size progress for an empty pack file. */
309 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
310 packfile_size_cur = 0;
311 if (!done && refname && id) {
312 err = got_pathlist_insert(NULL, refs, refname, id);
315 } else if (!done && server_progress) {
318 * XXX git-daemon tends to send batched output with
319 * lines spanning separate packets. Buffer progress
320 * output until we see a CR or LF to avoid giving
321 * partial lines of progress output to the callback.
323 if (strlcat(progress, server_progress,
324 GOT_PKT_MAX) >= GOT_PKT_MAX) {
325 progress[0] = '\0'; /* discard */
328 while ((p = strchr(progress, '\r')) != NULL ||
329 (p = strchr(progress, '\n')) != NULL) {
334 if (asprintf(&s, "%s%s", progress,
335 c == '\n' ? "\n" : "") == -1) {
336 err = got_error_from_errno("asprintf");
339 err = progress_cb(progress_arg, s,
340 packfile_size_cur, 0, 0, 0, 0);
344 n = strlen(progress);
345 if (n < GOT_PKT_MAX - 1) {
346 memmove(progress, &progress[n + 1],
347 GOT_PKT_MAX - n - 1);
351 free(server_progress);
354 } else if (!done && packfile_size_cur != packfile_size) {
355 err = progress_cb(progress_arg, NULL,
356 packfile_size_cur, 0, 0, 0, 0);
359 packfile_size = packfile_size_cur;
362 if (close(imsg_fetchfds[0]) == -1) {
363 err = got_error_from_errno("close");
366 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
367 err = got_error_from_errno("waitpid");
371 if (lseek(packfd, 0, SEEK_SET) == -1) {
372 err = got_error_from_errno("lseek");
376 /* If zero data was fetched without error we are already up-to-date. */
377 if (packfile_size == 0) {
381 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
382 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
387 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
389 err = got_error_from_errno("read");
392 if (n != ssizeof(pack_hdr)) {
393 err = got_error(GOT_ERR_IO);
396 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
397 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
398 "bad pack file signature");
401 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
402 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
403 "bad pack file version");
406 nobj = be32toh(pack_hdr.nobjects);
408 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
409 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
410 "bad pack file with zero objects");
414 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
415 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
416 "empty pack file with non-zero object count");
422 * If the pack file contains no objects, we may only need to update
423 * references in our repository. The caller will take care of that.
431 if (lseek(packfd, 0, SEEK_SET) == -1) {
432 err = got_error_from_errno("lseek");
436 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
437 err = got_error_from_errno("socketpair");
442 err= got_error_from_errno("fork");
444 } else if (idxpid == 0)
445 got_privsep_exec_child(imsg_idxfds,
446 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
447 if (close(imsg_idxfds[1]) == -1) {
448 err = got_error_from_errno("close");
451 imsg_init(&idxibuf, imsg_idxfds[0]);
453 npackfd = dup(packfd);
455 err = got_error_from_errno("dup");
458 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
463 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
467 for (i = 0; i < nitems(tmpfds); i++) {
468 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
475 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
477 err = got_privsep_recv_index_progress(&done, &nobj_total,
478 &nobj_indexed, &nobj_loose, &nobj_resolved,
482 if (nobj_indexed != 0) {
483 err = progress_cb(progress_arg, NULL,
484 packfile_size, nobj_total,
485 nobj_indexed, nobj_loose, nobj_resolved);
490 if (close(imsg_idxfds[0]) == -1) {
491 err = got_error_from_errno("close");
494 if (waitpid(idxpid, &idxstatus, 0) == -1) {
495 err = got_error_from_errno("waitpid");
499 err = got_object_id_str(&id_str, *pack_hash);
502 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
503 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
504 err = got_error_from_errno("asprintf");
508 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
509 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
510 err = got_error_from_errno("asprintf");
516 if (rename(tmppackpath, packpath) == -1) {
517 err = got_error_from_errno3("rename", tmppackpath, packpath);
522 if (rename(tmpidxpath, idxpath) == -1) {
523 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
530 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
531 err = got_error_from_errno2("unlink", tmppackpath);
532 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
533 err = got_error_from_errno2("unlink", tmpidxpath);
534 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
535 err = got_error_from_errno("close");
536 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
537 err = got_error_from_errno("close");
538 if (packfd != -1 && close(packfd) == -1 && err == NULL)
539 err = got_error_from_errno("close");
540 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
541 err = got_error_from_errno("close");
542 for (i = 0; i < nitems(tmpfds); i++) {
543 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
544 err = got_error_from_errno("close");
553 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
554 got_ref_list_free(&my_refs);
558 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
559 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);