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/queue.h>
20 #include <sys/tree.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 <endian.h>
28 #include <errno.h>
29 #include <err.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdint.h>
35 #include <sha1.h>
36 #include <sha2.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <ctype.h>
40 #include <limits.h>
41 #include <imsg.h>
42 #include <time.h>
43 #include <uuid.h>
45 #include "got_error.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.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"
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 #ifndef ssizeof
73 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
74 #endif
76 #ifndef MIN
77 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
78 #endif
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)
83 {
84 const struct got_error *err = NULL;
86 *fetchpid = -1;
87 *fetchfd = -1;
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,
94 GOT_DIAL_CMD_FETCH);
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);
101 else
102 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
103 return err;
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,
114 void *progress_arg)
116 size_t i;
117 int imsg_fetchfds[2], imsg_idxfds[2];
118 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
119 int tmpfds[3];
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;
133 uint32_t nobj = 0;
134 char *path;
135 char *progress = NULL;
137 *pack_hash = NULL;
139 /*
140 * Prevent fetching of references that won't make any
141 * sense outside of the remote repository's context.
142 */
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);
152 if (!list_refs_only)
153 repo_path = got_repo_get_path_git_dir(repo);
155 for (i = 0; i < nitems(tmpfds); i++)
156 tmpfds[i] = -1;
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);
164 if (err)
165 goto done;
168 TAILQ_FOREACH(re, &my_refs, entry) {
169 struct got_object_id *id;
170 const char *refname;
172 if (got_ref_is_symbolic(re->ref))
173 continue;
175 err = got_ref_resolve(&id, repo, re->ref);
176 if (err)
177 goto done;
178 refname = strdup(got_ref_get_name(re->ref));
179 if (refname == NULL) {
180 err = got_error_from_errno("strdup");
181 goto done;
183 err = got_pathlist_append(&have_refs, refname, id);
184 if (err)
185 goto done;
188 if (list_refs_only) {
189 packfd = got_opentempfd();
190 if (packfd == -1) {
191 err = got_error_from_errno("got_opentempfd");
192 goto done;
194 } else {
195 if (asprintf(&path, "%s/%s/fetching.pack",
196 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
197 err = got_error_from_errno("asprintf");
198 goto done;
200 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
201 free(path);
202 if (err)
203 goto done;
204 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
205 err = got_error_from_errno2("fchmod", tmppackpath);
206 goto done;
209 if (list_refs_only) {
210 idxfd = got_opentempfd();
211 if (idxfd == -1) {
212 err = got_error_from_errno("got_opentempfd");
213 goto done;
215 } else {
216 if (asprintf(&path, "%s/%s/fetching.idx",
217 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
218 err = got_error_from_errno("asprintf");
219 goto done;
221 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
222 free(path);
223 if (err)
224 goto done;
225 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
226 err = got_error_from_errno2("fchmod", tmpidxpath);
227 goto done;
230 nidxfd = dup(idxfd);
231 if (nidxfd == -1) {
232 err = got_error_from_errno("dup");
233 goto done;
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");
240 goto done;
244 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
245 err = got_error_from_errno("socketpair");
246 goto done;
249 fetchpid = fork();
250 if (fetchpid == -1) {
251 err = got_error_from_errno("fork");
252 goto done;
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");
260 goto done;
262 imsg_init(&fetchibuf, imsg_fetchfds[0]);
263 nfetchfd = dup(fetchfd);
264 if (nfetchfd == -1) {
265 err = got_error_from_errno("dup");
266 goto done;
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);
271 if (err != NULL)
272 goto done;
273 nfetchfd = -1;
274 npackfd = dup(packfd);
275 if (npackfd == -1) {
276 err = got_error_from_errno("dup");
277 goto done;
279 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
280 if (err != NULL)
281 goto done;
282 npackfd = -1;
284 packfile_size = 0;
285 progress = calloc(GOT_PKT_MAX, 1);
286 if (progress == NULL) {
287 err = got_error_from_errno("calloc");
288 goto done;
291 *pack_hash = calloc(1, sizeof(**pack_hash));
292 if (*pack_hash == NULL) {
293 err = got_error_from_errno("calloc");
294 goto done;
297 while (!done) {
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);
306 if (err != NULL)
307 goto done;
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);
313 if (err)
314 goto done;
315 } else if (!done && server_progress) {
316 char *p;
317 /*
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.
322 */
323 if (strlcat(progress, server_progress,
324 GOT_PKT_MAX) >= GOT_PKT_MAX) {
325 progress[0] = '\0'; /* discard */
326 continue;
328 while ((p = strchr(progress, '\r')) != NULL ||
329 (p = strchr(progress, '\n')) != NULL) {
330 char *s;
331 size_t n;
332 char c = *p;
333 *p = '\0';
334 if (asprintf(&s, "%s%s", progress,
335 c == '\n' ? "\n" : "") == -1) {
336 err = got_error_from_errno("asprintf");
337 goto done;
339 err = progress_cb(progress_arg, s,
340 packfile_size_cur, 0, 0, 0, 0);
341 free(s);
342 if (err)
343 break;
344 n = strlen(progress);
345 if (n < GOT_PKT_MAX - 1) {
346 memmove(progress, &progress[n + 1],
347 GOT_PKT_MAX - n - 1);
348 } else
349 progress[0] = '\0';
351 free(server_progress);
352 if (err)
353 goto done;
354 } else if (!done && packfile_size_cur != packfile_size) {
355 err = progress_cb(progress_arg, NULL,
356 packfile_size_cur, 0, 0, 0, 0);
357 if (err)
358 break;
359 packfile_size = packfile_size_cur;
362 if (close(imsg_fetchfds[0]) == -1) {
363 err = got_error_from_errno("close");
364 goto done;
366 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
367 err = got_error_from_errno("waitpid");
368 goto done;
371 if (lseek(packfd, 0, SEEK_SET) == -1) {
372 err = got_error_from_errno("lseek");
373 goto done;
376 /* If zero data was fetched without error we are already up-to-date. */
377 if (packfile_size == 0) {
378 free(*pack_hash);
379 *pack_hash = NULL;
380 goto done;
381 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
382 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
383 goto done;
384 } else {
385 ssize_t n;
387 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
388 if (n == -1) {
389 err = got_error_from_errno("read");
390 goto done;
392 if (n != ssizeof(pack_hdr)) {
393 err = got_error(GOT_ERR_IO);
394 goto done;
396 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
397 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
398 "bad pack file signature");
399 goto done;
401 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
402 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
403 "bad pack file version");
404 goto done;
406 nobj = be32toh(pack_hdr.nobjects);
407 if (nobj == 0 &&
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");
411 goto done;
413 if (nobj != 0 &&
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");
417 goto done;
421 /*
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.
424 */
425 if (nobj == 0) {
426 free(*pack_hash);
427 *pack_hash = NULL;
428 goto done;
431 if (lseek(packfd, 0, SEEK_SET) == -1) {
432 err = got_error_from_errno("lseek");
433 goto done;
436 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
437 err = got_error_from_errno("socketpair");
438 goto done;
440 idxpid = fork();
441 if (idxpid == -1) {
442 err= got_error_from_errno("fork");
443 goto done;
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");
449 goto done;
451 imsg_init(&idxibuf, imsg_idxfds[0]);
453 npackfd = dup(packfd);
454 if (npackfd == -1) {
455 err = got_error_from_errno("dup");
456 goto done;
458 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
459 npackfd);
460 if (err != NULL)
461 goto done;
462 npackfd = -1;
463 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
464 if (err != NULL)
465 goto done;
466 nidxfd = -1;
467 for (i = 0; i < nitems(tmpfds); i++) {
468 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
469 if (err != NULL)
470 goto done;
471 tmpfds[i] = -1;
473 done = 0;
474 while (!done) {
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,
479 &idxibuf);
480 if (err != NULL)
481 goto done;
482 if (nobj_indexed != 0) {
483 err = progress_cb(progress_arg, NULL,
484 packfile_size, nobj_total,
485 nobj_indexed, nobj_loose, nobj_resolved);
486 if (err)
487 break;
490 if (close(imsg_idxfds[0]) == -1) {
491 err = got_error_from_errno("close");
492 goto done;
494 if (waitpid(idxpid, &idxstatus, 0) == -1) {
495 err = got_error_from_errno("waitpid");
496 goto done;
499 err = got_object_id_str(&id_str, *pack_hash);
500 if (err)
501 goto done;
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");
505 goto done;
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");
511 goto done;
513 free(id_str);
514 id_str = NULL;
516 if (rename(tmppackpath, packpath) == -1) {
517 err = got_error_from_errno3("rename", tmppackpath, packpath);
518 goto done;
520 free(tmppackpath);
521 tmppackpath = NULL;
522 if (rename(tmpidxpath, idxpath) == -1) {
523 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
524 goto done;
526 free(tmpidxpath);
527 tmpidxpath = NULL;
529 done:
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");
546 free(tmppackpath);
547 free(tmpidxpath);
548 free(idxpath);
549 free(id_str);
550 free(packpath);
551 free(progress);
553 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
554 got_ref_list_free(&my_refs);
555 if (err) {
556 free(*pack_hash);
557 *pack_hash = NULL;
558 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
559 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);
561 return err;