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>
21 #include <sys/socket.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.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"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
75 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
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)
82 const struct got_error *err = NULL;
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);
96 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
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)
110 int imsg_fetchfds[2], imsg_idxfds[2];
111 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
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;
128 char *progress = NULL;
133 * Prevent fetching of references that won't make any
134 * sense outside of the remote repository's context.
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);
146 repo_path = got_repo_get_path_git_dir(repo);
148 for (i = 0; i < nitems(tmpfds); i++)
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);
161 TAILQ_FOREACH(re, &my_refs, entry) {
162 struct got_object_id *id;
165 if (got_ref_is_symbolic(re->ref))
168 err = got_ref_resolve(&id, repo, re->ref);
171 refname = strdup(got_ref_get_name(re->ref));
172 if (refname == NULL) {
173 err = got_error_from_errno("strdup");
176 err = got_pathlist_append(&have_refs, refname, id);
181 if (list_refs_only) {
182 packfd = got_opentempfd();
184 err = got_error_from_errno("got_opentempfd");
188 if (asprintf(&path, "%s/%s/fetching.pack",
189 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
190 err = got_error_from_errno("asprintf");
193 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
197 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
198 err = got_error_from_errno2("fchmod", tmppackpath);
202 if (list_refs_only) {
203 idxfd = got_opentempfd();
205 err = got_error_from_errno("got_opentempfd");
209 if (asprintf(&path, "%s/%s/fetching.idx",
210 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
211 err = got_error_from_errno("asprintf");
214 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
218 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
219 err = got_error_from_errno2("fchmod", tmpidxpath);
225 err = got_error_from_errno("dup");
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");
237 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
238 err = got_error_from_errno("socketpair");
243 if (fetchpid == -1) {
244 err = got_error_from_errno("fork");
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");
255 imsg_init(&fetchibuf, imsg_fetchfds[0]);
256 nfetchfd = dup(fetchfd);
257 if (nfetchfd == -1) {
258 err = got_error_from_errno("dup");
261 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
262 fetch_all_branches, wanted_branches, wanted_refs,
263 list_refs_only, verbosity);
267 npackfd = dup(packfd);
269 err = got_error_from_errno("dup");
272 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
278 progress = calloc(GOT_PKT_MAX, 1);
279 if (progress == NULL) {
280 err = got_error_from_errno("calloc");
284 *pack_hash = calloc(1, sizeof(**pack_hash));
285 if (*pack_hash == NULL) {
286 err = got_error_from_errno("calloc");
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);
301 /* Don't report size progress for an empty pack file. */
302 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
303 packfile_size_cur = 0;
304 if (!done && refname && id) {
305 err = got_pathlist_insert(NULL, refs, refname, id);
308 } else if (!done && server_progress) {
311 * XXX git-daemon tends to send batched output with
312 * lines spanning separate packets. Buffer progress
313 * output until we see a CR or LF to avoid giving
314 * partial lines of progress output to the callback.
316 if (strlcat(progress, server_progress,
317 GOT_PKT_MAX) >= GOT_PKT_MAX) {
318 progress[0] = '\0'; /* discard */
321 while ((p = strchr(progress, '\r')) != NULL ||
322 (p = strchr(progress, '\n')) != NULL) {
327 if (asprintf(&s, "%s%s", progress,
328 c == '\n' ? "\n" : "") == -1) {
329 err = got_error_from_errno("asprintf");
332 err = progress_cb(progress_arg, s,
333 packfile_size_cur, 0, 0, 0, 0);
337 n = strlen(progress);
338 if (n < GOT_PKT_MAX - 1) {
339 memmove(progress, &progress[n + 1],
340 GOT_PKT_MAX - n - 1);
344 free(server_progress);
347 } else if (!done && packfile_size_cur != packfile_size) {
348 err = progress_cb(progress_arg, NULL,
349 packfile_size_cur, 0, 0, 0, 0);
352 packfile_size = packfile_size_cur;
355 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
356 err = got_error_from_errno("waitpid");
360 if (lseek(packfd, 0, SEEK_SET) == -1) {
361 err = got_error_from_errno("lseek");
365 /* If zero data was fetched without error we are already up-to-date. */
366 if (packfile_size == 0) {
370 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
371 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
376 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
378 err = got_error_from_errno("read");
381 if (n != ssizeof(pack_hdr)) {
382 err = got_error(GOT_ERR_IO);
385 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
386 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
387 "bad pack file signature");
390 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
391 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
392 "bad pack file version");
395 nobj = be32toh(pack_hdr.nobjects);
397 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
398 return got_error_msg(GOT_ERR_BAD_PACKFILE,
399 "bad pack file with zero objects");
401 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
402 return got_error_msg(GOT_ERR_BAD_PACKFILE,
403 "empty pack file with non-zero object count");
407 * If the pack file contains no objects, we may only need to update
408 * references in our repository. The caller will take care of that.
416 if (lseek(packfd, 0, SEEK_SET) == -1) {
417 err = got_error_from_errno("lseek");
421 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
422 err = got_error_from_errno("socketpair");
427 err= got_error_from_errno("fork");
429 } else if (idxpid == 0)
430 got_privsep_exec_child(imsg_idxfds,
431 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
432 if (close(imsg_idxfds[1]) == -1) {
433 err = got_error_from_errno("close");
436 imsg_init(&idxibuf, imsg_idxfds[0]);
438 npackfd = dup(packfd);
440 err = got_error_from_errno("dup");
443 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
448 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
452 for (i = 0; i < nitems(tmpfds); i++) {
453 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
460 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
462 err = got_privsep_recv_index_progress(&done, &nobj_total,
463 &nobj_indexed, &nobj_loose, &nobj_resolved,
467 if (nobj_indexed != 0) {
468 err = progress_cb(progress_arg, NULL,
469 packfile_size, nobj_total,
470 nobj_indexed, nobj_loose, nobj_resolved);
474 imsg_clear(&idxibuf);
476 if (close(imsg_idxfds[0]) == -1) {
477 err = got_error_from_errno("close");
480 if (waitpid(idxpid, &idxstatus, 0) == -1) {
481 err = got_error_from_errno("waitpid");
485 err = got_object_id_str(&id_str, *pack_hash);
488 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
489 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
490 err = got_error_from_errno("asprintf");
494 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
495 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
496 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");
536 TAILQ_FOREACH(pe, &have_refs, entry) {
537 free((char *)pe->path);
540 got_pathlist_free(&have_refs);
541 got_ref_list_free(&my_refs);
545 TAILQ_FOREACH(pe, refs, entry) {
546 free((void *)pe->path);
549 got_pathlist_free(refs);
550 TAILQ_FOREACH(pe, symrefs, entry) {
551 free((void *)pe->path);
554 got_pathlist_free(symrefs);