fix a typo in CHANGES
[got-portable.git] / lib / fetch.c
blob0a728e189bd598248853f6fa3b318a1caaf2e224
1 /*
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.
16 #include "got_compat.h"
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
27 #include <errno.h>
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <time.h>
40 #include "got_error.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_object.h"
47 #include "got_opentemp.h"
48 #include "got_fetch.h"
50 #include "got_lib_hash.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_privsep.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_dial.h"
61 #include "got_lib_pkt.h"
63 #ifndef nitems
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 #endif
67 #ifndef ssizeof
68 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
69 #endif
71 #ifndef MIN
72 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
73 #endif
75 const struct got_error *
76 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
77 const char *host, const char *port, const char *server_path,
78 const char *jumphost, const char *identity_file, int verbosity)
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, jumphost, identity_file, GOT_DIAL_CMD_FETCH,
88 verbosity);
89 else if (strcmp(proto, "git") == 0)
90 err = got_dial_git(fetchfd, host, port, server_path,
91 GOT_DIAL_CMD_FETCH);
92 else if (strcmp(proto, "http") == 0 ||
93 strcmp(proto, "git+http") == 0 ||
94 strcmp(proto, "https") == 0 ||
95 strcmp(proto, "git+https") == 0)
96 err = got_dial_http(fetchpid, fetchfd, host, port,
97 server_path, verbosity, strstr(proto, "https") != NULL);
98 else
99 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
100 return err;
103 const struct got_error *
104 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
105 struct got_pathlist_head *symrefs, const char *remote_name,
106 int mirror_references, int fetch_all_branches,
107 struct got_pathlist_head *wanted_branches,
108 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
109 int fetchfd, struct got_repository *repo, const char *worktree_refname,
110 const char *remote_head, int no_head, got_fetch_progress_cb progress_cb,
111 void *progress_arg)
113 size_t i;
114 int imsg_fetchfds[2], imsg_idxfds[2];
115 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
116 int tmpfds[3];
117 int fetchstatus, idxstatus, done = 0;
118 const struct got_error *err;
119 struct imsgbuf fetchibuf, idxibuf;
120 pid_t fetchpid, idxpid;
121 char *tmppackpath = NULL, *tmpidxpath = NULL;
122 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
123 const char *repo_path = NULL;
124 struct got_pathlist_head have_refs;
125 struct got_pathlist_entry *pe, *new;
126 struct got_reflist_head my_refs;
127 struct got_reflist_entry *re;
128 off_t packfile_size = 0;
129 struct got_packfile_hdr pack_hdr;
130 uint32_t nobj = 0;
131 char *path;
132 char *progress = NULL;
134 *pack_hash = NULL;
135 memset(&fetchibuf, 0, sizeof(fetchibuf));
136 memset(&idxibuf, 0, sizeof(idxibuf));
138 if (repo && got_repo_get_object_format(repo) != GOT_HASH_SHA1)
139 return got_error_fmt(GOT_ERR_NOT_IMPL,
140 "sha256 object IDs unsupported in network protocol");
143 * Prevent fetching of references that won't make any
144 * sense outside of the remote repository's context.
146 RB_FOREACH(pe, got_pathlist_head, wanted_refs) {
147 const char *refname = pe->path;
148 if (strncmp(refname, "refs/got/", 9) == 0 ||
149 strncmp(refname, "got/", 4) == 0 ||
150 strncmp(refname, "refs/remotes/", 13) == 0 ||
151 strncmp(refname, "remotes/", 8) == 0)
152 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
155 if (!list_refs_only)
156 repo_path = got_repo_get_path_git_dir(repo);
158 for (i = 0; i < nitems(tmpfds); i++)
159 tmpfds[i] = -1;
161 RB_INIT(&have_refs);
162 TAILQ_INIT(&my_refs);
164 if (!list_refs_only) {
165 err = got_ref_list(&my_refs, repo, NULL,
166 got_ref_cmp_by_name, NULL);
167 if (err)
168 goto done;
171 TAILQ_FOREACH(re, &my_refs, entry) {
172 struct got_object_id *id;
173 char *refname;
175 if (got_ref_is_symbolic(re->ref))
176 continue;
178 err = got_ref_resolve(&id, repo, re->ref);
179 if (err)
180 goto done;
181 refname = strdup(got_ref_get_name(re->ref));
182 if (refname == NULL) {
183 err = got_error_from_errno("strdup");
184 goto done;
186 err = got_pathlist_insert(&new, &have_refs, refname, id);
187 if (err)
188 goto done;
189 if (new == NULL){
190 free(refname);
191 free(id);
196 if (list_refs_only) {
197 packfd = got_opentempfd();
198 if (packfd == -1) {
199 err = got_error_from_errno("got_opentempfd");
200 goto done;
202 } else {
203 if (asprintf(&path, "%s/%s/fetching.pack",
204 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
205 err = got_error_from_errno("asprintf");
206 goto done;
208 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
209 free(path);
210 if (err)
211 goto done;
212 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
213 err = got_error_from_errno2("fchmod", tmppackpath);
214 goto done;
217 if (list_refs_only) {
218 idxfd = got_opentempfd();
219 if (idxfd == -1) {
220 err = got_error_from_errno("got_opentempfd");
221 goto done;
223 } else {
224 if (asprintf(&path, "%s/%s/fetching.idx",
225 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
226 err = got_error_from_errno("asprintf");
227 goto done;
229 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
230 free(path);
231 if (err)
232 goto done;
233 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
234 err = got_error_from_errno2("fchmod", tmpidxpath);
235 goto done;
238 nidxfd = dup(idxfd);
239 if (nidxfd == -1) {
240 err = got_error_from_errno("dup");
241 goto done;
244 for (i = 0; i < nitems(tmpfds); i++) {
245 tmpfds[i] = got_opentempfd();
246 if (tmpfds[i] == -1) {
247 err = got_error_from_errno("got_opentempfd");
248 goto done;
252 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
253 err = got_error_from_errno("socketpair");
254 goto done;
257 fetchpid = fork();
258 if (fetchpid == -1) {
259 err = got_error_from_errno("fork");
260 goto done;
261 } else if (fetchpid == 0){
262 got_privsep_exec_child(imsg_fetchfds,
263 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
266 if (close(imsg_fetchfds[1]) == -1) {
267 err = got_error_from_errno("close");
268 goto done;
270 if (imsgbuf_init(&fetchibuf, imsg_fetchfds[0]) == -1) {
271 err = got_error_from_errno("imsgbuf_init");
272 goto done;
274 imsgbuf_allow_fdpass(&fetchibuf);
275 nfetchfd = dup(fetchfd);
276 if (nfetchfd == -1) {
277 err = got_error_from_errno("dup");
278 goto done;
280 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
281 fetch_all_branches, wanted_branches, wanted_refs,
282 list_refs_only, worktree_refname, remote_head, no_head, verbosity);
283 if (err != NULL)
284 goto done;
285 nfetchfd = -1;
286 npackfd = dup(packfd);
287 if (npackfd == -1) {
288 err = got_error_from_errno("dup");
289 goto done;
291 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
292 if (err != NULL)
293 goto done;
294 npackfd = -1;
296 packfile_size = 0;
297 progress = calloc(GOT_PKT_MAX, 1);
298 if (progress == NULL) {
299 err = got_error_from_errno("calloc");
300 goto done;
303 *pack_hash = calloc(1, sizeof(**pack_hash));
304 if (*pack_hash == NULL) {
305 err = got_error_from_errno("calloc");
306 goto done;
309 while (!done) {
310 struct got_object_id *id = NULL;
311 char *refname = NULL;
312 char *server_progress = NULL;
313 off_t packfile_size_cur = 0;
315 err = got_privsep_recv_fetch_progress(&done,
316 &id, &refname, symrefs, &server_progress,
317 &packfile_size_cur, (*pack_hash)->hash, &fetchibuf);
318 if (err != NULL)
319 goto done;
320 /* Don't report size progress for an empty pack file. */
321 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
322 packfile_size_cur = 0;
323 if (!done && refname && id) {
324 err = got_pathlist_insert(&pe, refs, refname, id);
325 if (err || pe == NULL) {
326 free(id);
327 free(refname);
328 if (err != NULL)
329 goto done;
331 } else if (!done && server_progress) {
332 char *p;
334 * XXX git-daemon tends to send batched output with
335 * lines spanning separate packets. Buffer progress
336 * output until we see a CR or LF to avoid giving
337 * partial lines of progress output to the callback.
339 if (strlcat(progress, server_progress,
340 GOT_PKT_MAX) >= GOT_PKT_MAX) {
341 progress[0] = '\0'; /* discard */
342 continue;
344 while ((p = strchr(progress, '\r')) != NULL ||
345 (p = strchr(progress, '\n')) != NULL) {
346 char *s;
347 size_t n;
348 char c = *p;
349 *p = '\0';
350 if (asprintf(&s, "%s%s", progress,
351 c == '\n' ? "\n" : "") == -1) {
352 err = got_error_from_errno("asprintf");
353 goto done;
355 err = progress_cb(progress_arg, s,
356 packfile_size_cur, 0, 0, 0, 0);
357 free(s);
358 if (err)
359 break;
360 n = strlen(progress);
361 if (n < GOT_PKT_MAX - 1) {
362 memmove(progress, &progress[n + 1],
363 GOT_PKT_MAX - n - 1);
364 } else
365 progress[0] = '\0';
367 free(server_progress);
368 if (err)
369 goto done;
370 } else if (!done && packfile_size_cur != packfile_size) {
371 err = progress_cb(progress_arg, NULL,
372 packfile_size_cur, 0, 0, 0, 0);
373 if (err)
374 break;
375 packfile_size = packfile_size_cur;
378 if (close(imsg_fetchfds[0]) == -1) {
379 err = got_error_from_errno("close");
380 goto done;
382 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
383 err = got_error_from_errno("waitpid");
384 goto done;
387 if (lseek(packfd, 0, SEEK_SET) == -1) {
388 err = got_error_from_errno("lseek");
389 goto done;
392 /* If zero data was fetched without error we are already up-to-date. */
393 if (packfile_size == 0) {
394 free(*pack_hash);
395 *pack_hash = NULL;
396 goto done;
397 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
398 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
399 goto done;
400 } else {
401 ssize_t n;
403 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
404 if (n == -1) {
405 err = got_error_from_errno("read");
406 goto done;
408 if (n != ssizeof(pack_hdr)) {
409 err = got_error(GOT_ERR_IO);
410 goto done;
412 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
413 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
414 "bad pack file signature");
415 goto done;
417 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
418 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
419 "bad pack file version");
420 goto done;
422 nobj = be32toh(pack_hdr.nobjects);
423 if (nobj == 0 &&
424 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
425 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
426 "bad pack file with zero objects");
427 goto done;
429 if (nobj != 0 &&
430 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
431 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
432 "empty pack file with non-zero object count");
433 goto done;
438 * If the pack file contains no objects, we may only need to update
439 * references in our repository. The caller will take care of that.
441 if (nobj == 0) {
442 free(*pack_hash);
443 *pack_hash = NULL;
444 goto done;
447 if (lseek(packfd, 0, SEEK_SET) == -1) {
448 err = got_error_from_errno("lseek");
449 goto done;
452 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
453 err = got_error_from_errno("socketpair");
454 goto done;
456 idxpid = fork();
457 if (idxpid == -1) {
458 err= got_error_from_errno("fork");
459 goto done;
460 } else if (idxpid == 0)
461 got_privsep_exec_child(imsg_idxfds,
462 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
463 if (close(imsg_idxfds[1]) == -1) {
464 err = got_error_from_errno("close");
465 goto done;
467 if (imsgbuf_init(&idxibuf, imsg_idxfds[0]) == -1) {
468 err = got_error_from_errno("imsgbuf_init");
469 goto done;
471 imsgbuf_allow_fdpass(&idxibuf);
473 npackfd = dup(packfd);
474 if (npackfd == -1) {
475 err = got_error_from_errno("dup");
476 goto done;
478 err = got_privsep_send_index_pack_req(&idxibuf, *pack_hash, npackfd);
479 if (err != NULL)
480 goto done;
481 npackfd = -1;
482 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
483 if (err != NULL)
484 goto done;
485 nidxfd = -1;
486 for (i = 0; i < nitems(tmpfds); i++) {
487 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
488 if (err != NULL)
489 goto done;
490 tmpfds[i] = -1;
492 done = 0;
493 while (!done) {
494 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
496 err = got_privsep_recv_index_progress(&done, &nobj_total,
497 &nobj_indexed, &nobj_loose, &nobj_resolved,
498 &idxibuf);
499 if (err != NULL)
500 goto done;
501 if (nobj_indexed != 0) {
502 err = progress_cb(progress_arg, NULL,
503 packfile_size, nobj_total,
504 nobj_indexed, nobj_loose, nobj_resolved);
505 if (err)
506 break;
509 if (close(imsg_idxfds[0]) == -1) {
510 err = got_error_from_errno("close");
511 goto done;
513 if (waitpid(idxpid, &idxstatus, 0) == -1) {
514 err = got_error_from_errno("waitpid");
515 goto done;
518 err = got_object_id_str(&id_str, *pack_hash);
519 if (err)
520 goto done;
521 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
522 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
523 err = got_error_from_errno("asprintf");
524 goto done;
527 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
528 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
529 err = got_error_from_errno("asprintf");
530 goto done;
532 free(id_str);
533 id_str = NULL;
535 if (rename(tmppackpath, packpath) == -1) {
536 err = got_error_from_errno3("rename", tmppackpath, packpath);
537 goto done;
539 free(tmppackpath);
540 tmppackpath = NULL;
541 if (rename(tmpidxpath, idxpath) == -1) {
542 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
543 goto done;
545 free(tmpidxpath);
546 tmpidxpath = NULL;
548 done:
549 if (fetchibuf.w)
550 imsgbuf_clear(&fetchibuf);
551 if (idxibuf.w)
552 imsgbuf_clear(&idxibuf);
553 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
554 err = got_error_from_errno2("unlink", tmppackpath);
555 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
556 err = got_error_from_errno2("unlink", tmpidxpath);
557 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
558 err = got_error_from_errno("close");
559 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
560 err = got_error_from_errno("close");
561 if (packfd != -1 && close(packfd) == -1 && err == NULL)
562 err = got_error_from_errno("close");
563 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
564 err = got_error_from_errno("close");
565 for (i = 0; i < nitems(tmpfds); i++) {
566 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
567 err = got_error_from_errno("close");
569 free(tmppackpath);
570 free(tmpidxpath);
571 free(idxpath);
572 free(id_str);
573 free(packpath);
574 free(progress);
576 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
577 got_ref_list_free(&my_refs);
578 if (err) {
579 free(*pack_hash);
580 *pack_hash = NULL;
581 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
582 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);
584 return err;