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 */
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_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_hash.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, int verbosity)
78 {
79 const struct got_error *err = NULL;
81 *fetchpid = -1;
82 *fetchfd = -1;
84 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
85 err = got_dial_ssh(fetchpid, fetchfd, host, port,
86 server_path, GOT_DIAL_CMD_FETCH, verbosity);
87 else if (strcmp(proto, "git") == 0)
88 err = got_dial_git(fetchfd, host, port, server_path,
89 GOT_DIAL_CMD_FETCH);
90 else if (strcmp(proto, "http") == 0 ||
91 strcmp(proto, "git+http") == 0 ||
92 strcmp(proto, "https") == 0 ||
93 strcmp(proto, "git+https") == 0)
94 err = got_dial_http(fetchpid, fetchfd, host, port,
95 server_path, verbosity, strstr(proto, "https") != NULL);
96 else
97 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
98 return err;
99 }
101 const struct got_error *
102 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
103 struct got_pathlist_head *symrefs, const char *remote_name,
104 int mirror_references, int fetch_all_branches,
105 struct got_pathlist_head *wanted_branches,
106 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
107 int fetchfd, struct got_repository *repo, const char *worktree_refname,
108 const char *remote_head, int no_head, got_fetch_progress_cb progress_cb,
109 void *progress_arg)
111 size_t i;
112 int imsg_fetchfds[2], imsg_idxfds[2];
113 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
114 int tmpfds[3];
115 int fetchstatus, idxstatus, done = 0;
116 const struct got_error *err;
117 struct imsgbuf fetchibuf, idxibuf;
118 pid_t fetchpid, idxpid;
119 char *tmppackpath = NULL, *tmpidxpath = NULL;
120 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
121 const char *repo_path = NULL;
122 struct got_pathlist_head have_refs;
123 struct got_pathlist_entry *pe;
124 struct got_reflist_head my_refs;
125 struct got_reflist_entry *re;
126 off_t packfile_size = 0;
127 struct got_packfile_hdr pack_hdr;
128 uint32_t nobj = 0;
129 char *path;
130 char *progress = NULL;
132 *pack_hash = NULL;
134 /*
135 * Prevent fetching of references that won't make any
136 * sense outside of the remote repository's context.
137 */
138 TAILQ_FOREACH(pe, wanted_refs, entry) {
139 const char *refname = pe->path;
140 if (strncmp(refname, "refs/got/", 9) == 0 ||
141 strncmp(refname, "got/", 4) == 0 ||
142 strncmp(refname, "refs/remotes/", 13) == 0 ||
143 strncmp(refname, "remotes/", 8) == 0)
144 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
147 if (!list_refs_only)
148 repo_path = got_repo_get_path_git_dir(repo);
150 for (i = 0; i < nitems(tmpfds); i++)
151 tmpfds[i] = -1;
153 TAILQ_INIT(&have_refs);
154 TAILQ_INIT(&my_refs);
156 if (!list_refs_only) {
157 err = got_ref_list(&my_refs, repo, NULL,
158 got_ref_cmp_by_name, NULL);
159 if (err)
160 goto done;
163 TAILQ_FOREACH(re, &my_refs, entry) {
164 struct got_object_id *id;
165 const char *refname;
167 if (got_ref_is_symbolic(re->ref))
168 continue;
170 err = got_ref_resolve(&id, repo, re->ref);
171 if (err)
172 goto done;
173 refname = strdup(got_ref_get_name(re->ref));
174 if (refname == NULL) {
175 err = got_error_from_errno("strdup");
176 goto done;
178 err = got_pathlist_append(&have_refs, refname, id);
179 if (err)
180 goto done;
183 if (list_refs_only) {
184 packfd = got_opentempfd();
185 if (packfd == -1) {
186 err = got_error_from_errno("got_opentempfd");
187 goto done;
189 } else {
190 if (asprintf(&path, "%s/%s/fetching.pack",
191 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
192 err = got_error_from_errno("asprintf");
193 goto done;
195 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
196 free(path);
197 if (err)
198 goto done;
199 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
200 err = got_error_from_errno2("fchmod", tmppackpath);
201 goto done;
204 if (list_refs_only) {
205 idxfd = got_opentempfd();
206 if (idxfd == -1) {
207 err = got_error_from_errno("got_opentempfd");
208 goto done;
210 } else {
211 if (asprintf(&path, "%s/%s/fetching.idx",
212 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
213 err = got_error_from_errno("asprintf");
214 goto done;
216 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
217 free(path);
218 if (err)
219 goto done;
220 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
221 err = got_error_from_errno2("fchmod", tmpidxpath);
222 goto done;
225 nidxfd = dup(idxfd);
226 if (nidxfd == -1) {
227 err = got_error_from_errno("dup");
228 goto done;
231 for (i = 0; i < nitems(tmpfds); i++) {
232 tmpfds[i] = got_opentempfd();
233 if (tmpfds[i] == -1) {
234 err = got_error_from_errno("got_opentempfd");
235 goto done;
239 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
240 err = got_error_from_errno("socketpair");
241 goto done;
244 fetchpid = fork();
245 if (fetchpid == -1) {
246 err = got_error_from_errno("fork");
247 goto done;
248 } else if (fetchpid == 0){
249 got_privsep_exec_child(imsg_fetchfds,
250 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
253 if (close(imsg_fetchfds[1]) == -1) {
254 err = got_error_from_errno("close");
255 goto done;
257 imsg_init(&fetchibuf, imsg_fetchfds[0]);
258 nfetchfd = dup(fetchfd);
259 if (nfetchfd == -1) {
260 err = got_error_from_errno("dup");
261 goto done;
263 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
264 fetch_all_branches, wanted_branches, wanted_refs,
265 list_refs_only, worktree_refname, remote_head, no_head, verbosity);
266 if (err != NULL)
267 goto done;
268 nfetchfd = -1;
269 npackfd = dup(packfd);
270 if (npackfd == -1) {
271 err = got_error_from_errno("dup");
272 goto done;
274 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
275 if (err != NULL)
276 goto done;
277 npackfd = -1;
279 packfile_size = 0;
280 progress = calloc(GOT_PKT_MAX, 1);
281 if (progress == NULL) {
282 err = got_error_from_errno("calloc");
283 goto done;
286 *pack_hash = calloc(1, sizeof(**pack_hash));
287 if (*pack_hash == NULL) {
288 err = got_error_from_errno("calloc");
289 goto done;
292 while (!done) {
293 struct got_object_id *id = NULL;
294 char *refname = NULL;
295 char *server_progress = NULL;
296 off_t packfile_size_cur = 0;
298 err = got_privsep_recv_fetch_progress(&done,
299 &id, &refname, symrefs, &server_progress,
300 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
301 if (err != NULL)
302 goto done;
303 /* Don't report size progress for an empty pack file. */
304 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
305 packfile_size_cur = 0;
306 if (!done && refname && id) {
307 err = got_pathlist_insert(NULL, refs, refname, id);
308 if (err)
309 goto done;
310 } else if (!done && server_progress) {
311 char *p;
312 /*
313 * XXX git-daemon tends to send batched output with
314 * lines spanning separate packets. Buffer progress
315 * output until we see a CR or LF to avoid giving
316 * partial lines of progress output to the callback.
317 */
318 if (strlcat(progress, server_progress,
319 GOT_PKT_MAX) >= GOT_PKT_MAX) {
320 progress[0] = '\0'; /* discard */
321 continue;
323 while ((p = strchr(progress, '\r')) != NULL ||
324 (p = strchr(progress, '\n')) != NULL) {
325 char *s;
326 size_t n;
327 char c = *p;
328 *p = '\0';
329 if (asprintf(&s, "%s%s", progress,
330 c == '\n' ? "\n" : "") == -1) {
331 err = got_error_from_errno("asprintf");
332 goto done;
334 err = progress_cb(progress_arg, s,
335 packfile_size_cur, 0, 0, 0, 0);
336 free(s);
337 if (err)
338 break;
339 n = strlen(progress);
340 if (n < GOT_PKT_MAX - 1) {
341 memmove(progress, &progress[n + 1],
342 GOT_PKT_MAX - n - 1);
343 } else
344 progress[0] = '\0';
346 free(server_progress);
347 if (err)
348 goto done;
349 } else if (!done && packfile_size_cur != packfile_size) {
350 err = progress_cb(progress_arg, NULL,
351 packfile_size_cur, 0, 0, 0, 0);
352 if (err)
353 break;
354 packfile_size = packfile_size_cur;
357 if (close(imsg_fetchfds[0]) == -1) {
358 err = got_error_from_errno("close");
359 goto done;
361 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
362 err = got_error_from_errno("waitpid");
363 goto done;
366 if (lseek(packfd, 0, SEEK_SET) == -1) {
367 err = got_error_from_errno("lseek");
368 goto done;
371 /* If zero data was fetched without error we are already up-to-date. */
372 if (packfile_size == 0) {
373 free(*pack_hash);
374 *pack_hash = NULL;
375 goto done;
376 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
377 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
378 goto done;
379 } else {
380 ssize_t n;
382 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
383 if (n == -1) {
384 err = got_error_from_errno("read");
385 goto done;
387 if (n != ssizeof(pack_hdr)) {
388 err = got_error(GOT_ERR_IO);
389 goto done;
391 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
392 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
393 "bad pack file signature");
394 goto done;
396 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
397 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
398 "bad pack file version");
399 goto done;
401 nobj = be32toh(pack_hdr.nobjects);
402 if (nobj == 0 &&
403 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
404 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
405 "bad pack file with zero objects");
406 goto done;
408 if (nobj != 0 &&
409 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
410 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
411 "empty pack file with non-zero object count");
412 goto done;
416 /*
417 * If the pack file contains no objects, we may only need to update
418 * references in our repository. The caller will take care of that.
419 */
420 if (nobj == 0) {
421 free(*pack_hash);
422 *pack_hash = NULL;
423 goto done;
426 if (lseek(packfd, 0, SEEK_SET) == -1) {
427 err = got_error_from_errno("lseek");
428 goto done;
431 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
432 err = got_error_from_errno("socketpair");
433 goto done;
435 idxpid = fork();
436 if (idxpid == -1) {
437 err= got_error_from_errno("fork");
438 goto done;
439 } else if (idxpid == 0)
440 got_privsep_exec_child(imsg_idxfds,
441 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
442 if (close(imsg_idxfds[1]) == -1) {
443 err = got_error_from_errno("close");
444 goto done;
446 imsg_init(&idxibuf, imsg_idxfds[0]);
448 npackfd = dup(packfd);
449 if (npackfd == -1) {
450 err = got_error_from_errno("dup");
451 goto done;
453 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
454 npackfd);
455 if (err != NULL)
456 goto done;
457 npackfd = -1;
458 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
459 if (err != NULL)
460 goto done;
461 nidxfd = -1;
462 for (i = 0; i < nitems(tmpfds); i++) {
463 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
464 if (err != NULL)
465 goto done;
466 tmpfds[i] = -1;
468 done = 0;
469 while (!done) {
470 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
472 err = got_privsep_recv_index_progress(&done, &nobj_total,
473 &nobj_indexed, &nobj_loose, &nobj_resolved,
474 &idxibuf);
475 if (err != NULL)
476 goto done;
477 if (nobj_indexed != 0) {
478 err = progress_cb(progress_arg, NULL,
479 packfile_size, nobj_total,
480 nobj_indexed, nobj_loose, nobj_resolved);
481 if (err)
482 break;
485 if (close(imsg_idxfds[0]) == -1) {
486 err = got_error_from_errno("close");
487 goto done;
489 if (waitpid(idxpid, &idxstatus, 0) == -1) {
490 err = got_error_from_errno("waitpid");
491 goto done;
494 err = got_object_id_str(&id_str, *pack_hash);
495 if (err)
496 goto done;
497 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
498 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
499 err = got_error_from_errno("asprintf");
500 goto done;
503 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
504 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
505 err = got_error_from_errno("asprintf");
506 goto done;
508 free(id_str);
509 id_str = NULL;
511 if (rename(tmppackpath, packpath) == -1) {
512 err = got_error_from_errno3("rename", tmppackpath, packpath);
513 goto done;
515 free(tmppackpath);
516 tmppackpath = NULL;
517 if (rename(tmpidxpath, idxpath) == -1) {
518 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
519 goto done;
521 free(tmpidxpath);
522 tmpidxpath = NULL;
524 done:
525 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
526 err = got_error_from_errno2("unlink", tmppackpath);
527 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
528 err = got_error_from_errno2("unlink", tmpidxpath);
529 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
530 err = got_error_from_errno("close");
531 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
532 err = got_error_from_errno("close");
533 if (packfd != -1 && close(packfd) == -1 && err == NULL)
534 err = got_error_from_errno("close");
535 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
536 err = got_error_from_errno("close");
537 for (i = 0; i < nitems(tmpfds); i++) {
538 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
539 err = got_error_from_errno("close");
541 free(tmppackpath);
542 free(tmpidxpath);
543 free(idxpath);
544 free(id_str);
545 free(packpath);
546 free(progress);
548 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
549 got_ref_list_free(&my_refs);
550 if (err) {
551 free(*pack_hash);
552 *pack_hash = NULL;
553 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
554 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);
556 return err;