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>
20 #include <sys/socket.h>
22 #include <sys/resource.h>
23 #include <sys/socket.h>
38 #include "got_error.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_object.h"
45 #include "got_opentemp.h"
46 #include "got_fetch.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_object_cache.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_dial.h"
59 #include "got_lib_pkt.h"
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
70 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
73 const struct got_error *
74 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
75 const char *host, const char *port, const char *server_path, int verbosity)
77 const struct got_error *err = NULL;
82 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
83 err = got_dial_ssh(fetchpid, fetchfd, host, port,
84 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
85 else if (strcmp(proto, "git") == 0)
86 err = got_dial_git(fetchfd, host, port, server_path,
87 GOT_DIAL_DIRECTION_FETCH);
88 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
89 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
91 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
95 const struct got_error *
96 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
97 struct got_pathlist_head *symrefs, const char *remote_name,
98 int mirror_references, int fetch_all_branches,
99 struct got_pathlist_head *wanted_branches,
100 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
101 int fetchfd, struct got_repository *repo,
102 got_fetch_progress_cb progress_cb, void *progress_arg)
105 int imsg_fetchfds[2], imsg_idxfds[2];
106 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
108 int fetchstatus, idxstatus, done = 0;
109 const struct got_error *err;
110 struct imsgbuf fetchibuf, idxibuf;
111 pid_t fetchpid, idxpid;
112 char *tmppackpath = NULL, *tmpidxpath = NULL;
113 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
114 const char *repo_path = NULL;
115 struct got_pathlist_head have_refs;
116 struct got_pathlist_entry *pe;
117 struct got_reflist_head my_refs;
118 struct got_reflist_entry *re;
119 off_t packfile_size = 0;
120 struct got_packfile_hdr pack_hdr;
123 char *progress = NULL;
128 * Prevent fetching of references that won't make any
129 * sense outside of the remote repository's context.
131 TAILQ_FOREACH(pe, wanted_refs, entry) {
132 const char *refname = pe->path;
133 if (strncmp(refname, "refs/got/", 9) == 0 ||
134 strncmp(refname, "got/", 4) == 0 ||
135 strncmp(refname, "refs/remotes/", 13) == 0 ||
136 strncmp(refname, "remotes/", 8) == 0)
137 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
141 repo_path = got_repo_get_path_git_dir(repo);
143 for (i = 0; i < nitems(tmpfds); i++)
146 TAILQ_INIT(&have_refs);
147 TAILQ_INIT(&my_refs);
149 if (!list_refs_only) {
150 err = got_ref_list(&my_refs, repo, NULL,
151 got_ref_cmp_by_name, NULL);
156 TAILQ_FOREACH(re, &my_refs, entry) {
157 struct got_object_id *id;
160 if (got_ref_is_symbolic(re->ref))
163 err = got_ref_resolve(&id, repo, re->ref);
166 refname = strdup(got_ref_get_name(re->ref));
167 if (refname == NULL) {
168 err = got_error_from_errno("strdup");
171 err = got_pathlist_append(&have_refs, refname, id);
176 if (list_refs_only) {
177 packfd = got_opentempfd();
179 err = got_error_from_errno("got_opentempfd");
183 if (asprintf(&path, "%s/%s/fetching.pack",
184 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
188 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
192 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
193 err = got_error_from_errno2("fchmod", tmppackpath);
197 if (list_refs_only) {
198 idxfd = got_opentempfd();
200 err = got_error_from_errno("got_opentempfd");
204 if (asprintf(&path, "%s/%s/fetching.idx",
205 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
206 err = got_error_from_errno("asprintf");
209 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
213 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
214 err = got_error_from_errno2("fchmod", tmpidxpath);
220 err = got_error_from_errno("dup");
224 for (i = 0; i < nitems(tmpfds); i++) {
225 tmpfds[i] = got_opentempfd();
226 if (tmpfds[i] == -1) {
227 err = got_error_from_errno("got_opentempfd");
232 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
233 err = got_error_from_errno("socketpair");
238 if (fetchpid == -1) {
239 err = got_error_from_errno("fork");
241 } else if (fetchpid == 0){
242 got_privsep_exec_child(imsg_fetchfds,
243 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
246 if (close(imsg_fetchfds[1]) == -1) {
247 err = got_error_from_errno("close");
250 imsg_init(&fetchibuf, imsg_fetchfds[0]);
251 nfetchfd = dup(fetchfd);
252 if (nfetchfd == -1) {
253 err = got_error_from_errno("dup");
256 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
257 fetch_all_branches, wanted_branches, wanted_refs,
258 list_refs_only, verbosity);
262 npackfd = dup(packfd);
264 err = got_error_from_errno("dup");
267 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
273 progress = calloc(GOT_PKT_MAX, 1);
274 if (progress == NULL) {
275 err = got_error_from_errno("calloc");
279 *pack_hash = calloc(1, sizeof(**pack_hash));
280 if (*pack_hash == NULL) {
281 err = got_error_from_errno("calloc");
286 struct got_object_id *id = NULL;
287 char *refname = NULL;
288 char *server_progress = NULL;
289 off_t packfile_size_cur = 0;
291 err = got_privsep_recv_fetch_progress(&done,
292 &id, &refname, symrefs, &server_progress,
293 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
296 /* Don't report size progress for an empty pack file. */
297 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
298 packfile_size_cur = 0;
299 if (!done && refname && id) {
300 err = got_pathlist_insert(NULL, refs, refname, id);
303 } else if (!done && server_progress) {
306 * XXX git-daemon tends to send batched output with
307 * lines spanning separate packets. Buffer progress
308 * output until we see a CR or LF to avoid giving
309 * partial lines of progress output to the callback.
311 if (strlcat(progress, server_progress,
312 GOT_PKT_MAX) >= GOT_PKT_MAX) {
313 progress[0] = '\0'; /* discard */
316 while ((p = strchr(progress, '\r')) != NULL ||
317 (p = strchr(progress, '\n')) != NULL) {
322 if (asprintf(&s, "%s%s", progress,
323 c == '\n' ? "\n" : "") == -1) {
324 err = got_error_from_errno("asprintf");
327 err = progress_cb(progress_arg, s,
328 packfile_size_cur, 0, 0, 0, 0);
332 n = strlen(progress);
333 if (n < GOT_PKT_MAX - 1) {
334 memmove(progress, &progress[n + 1],
335 GOT_PKT_MAX - n - 1);
339 free(server_progress);
342 } else if (!done && packfile_size_cur != packfile_size) {
343 err = progress_cb(progress_arg, NULL,
344 packfile_size_cur, 0, 0, 0, 0);
347 packfile_size = packfile_size_cur;
350 if (close(imsg_fetchfds[0]) == -1) {
351 err = got_error_from_errno("close");
354 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
355 err = got_error_from_errno("waitpid");
359 if (lseek(packfd, 0, SEEK_SET) == -1) {
360 err = got_error_from_errno("lseek");
364 /* If zero data was fetched without error we are already up-to-date. */
365 if (packfile_size == 0) {
369 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
370 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
375 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
377 err = got_error_from_errno("read");
380 if (n != ssizeof(pack_hdr)) {
381 err = got_error(GOT_ERR_IO);
384 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
385 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
386 "bad pack file signature");
389 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
390 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
391 "bad pack file version");
394 nobj = be32toh(pack_hdr.nobjects);
396 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
397 return got_error_msg(GOT_ERR_BAD_PACKFILE,
398 "bad pack file with zero objects");
400 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
401 return got_error_msg(GOT_ERR_BAD_PACKFILE,
402 "empty pack file with non-zero object count");
406 * If the pack file contains no objects, we may only need to update
407 * references in our repository. The caller will take care of that.
415 if (lseek(packfd, 0, SEEK_SET) == -1) {
416 err = got_error_from_errno("lseek");
420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
421 err = got_error_from_errno("socketpair");
426 err= got_error_from_errno("fork");
428 } else if (idxpid == 0)
429 got_privsep_exec_child(imsg_idxfds,
430 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
431 if (close(imsg_idxfds[1]) == -1) {
432 err = got_error_from_errno("close");
435 imsg_init(&idxibuf, imsg_idxfds[0]);
437 npackfd = dup(packfd);
439 err = got_error_from_errno("dup");
442 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
447 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
451 for (i = 0; i < nitems(tmpfds); i++) {
452 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
459 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
461 err = got_privsep_recv_index_progress(&done, &nobj_total,
462 &nobj_indexed, &nobj_loose, &nobj_resolved,
466 if (nobj_indexed != 0) {
467 err = progress_cb(progress_arg, NULL,
468 packfile_size, nobj_total,
469 nobj_indexed, nobj_loose, nobj_resolved);
474 if (close(imsg_idxfds[0]) == -1) {
475 err = got_error_from_errno("close");
478 if (waitpid(idxpid, &idxstatus, 0) == -1) {
479 err = got_error_from_errno("waitpid");
483 err = got_object_id_str(&id_str, *pack_hash);
486 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
487 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
488 err = got_error_from_errno("asprintf");
492 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
493 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
494 err = got_error_from_errno("asprintf");
500 if (rename(tmppackpath, packpath) == -1) {
501 err = got_error_from_errno3("rename", tmppackpath, packpath);
506 if (rename(tmpidxpath, idxpath) == -1) {
507 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
514 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
515 err = got_error_from_errno2("unlink", tmppackpath);
516 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
517 err = got_error_from_errno2("unlink", tmpidxpath);
518 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
519 err = got_error_from_errno("close");
520 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
521 err = got_error_from_errno("close");
522 if (packfd != -1 && close(packfd) == -1 && err == NULL)
523 err = got_error_from_errno("close");
524 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
525 err = got_error_from_errno("close");
526 for (i = 0; i < nitems(tmpfds); i++) {
527 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
528 err = got_error_from_errno("close");
537 TAILQ_FOREACH(pe, &have_refs, entry) {
538 free((char *)pe->path);
541 got_pathlist_free(&have_refs);
542 got_ref_list_free(&my_refs);
546 TAILQ_FOREACH(pe, refs, entry) {
547 free((void *)pe->path);
550 got_pathlist_free(refs);
551 TAILQ_FOREACH(pe, symrefs, entry) {
552 free((void *)pe->path);
555 got_pathlist_free(symrefs);