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/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <errno.h>
27 #include <err.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <sha1.h>
34 #include <sha2.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <ctype.h>
38 #include <limits.h>
39 #include <time.h>
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
49 #include "got_fetch.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_sha1.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_dial.h"
62 #include "got_lib_pkt.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 #ifndef ssizeof
69 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
70 #endif
72 #ifndef MIN
73 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 #endif
76 const struct got_error *
77 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
78 const char *host, const char *port, const char *server_path, int verbosity)
79 {
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, GOT_DIAL_DIRECTION_FETCH, verbosity);
88 else if (strcmp(proto, "git") == 0)
89 err = got_dial_git(fetchfd, host, port, server_path,
90 GOT_DIAL_DIRECTION_FETCH);
91 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
92 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
93 else
94 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
95 return err;
96 }
98 const struct got_error *
99 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
100 struct got_pathlist_head *symrefs, const char *remote_name,
101 int mirror_references, int fetch_all_branches,
102 struct got_pathlist_head *wanted_branches,
103 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
104 int fetchfd, struct got_repository *repo, const char *worktree_refname,
105 const char *remote_head, int no_head, got_fetch_progress_cb progress_cb,
106 void *progress_arg)
108 size_t i;
109 int imsg_fetchfds[2], imsg_idxfds[2];
110 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
111 int tmpfds[3];
112 int fetchstatus, idxstatus, done = 0;
113 const struct got_error *err;
114 struct imsgbuf fetchibuf, idxibuf;
115 pid_t fetchpid, idxpid;
116 char *tmppackpath = NULL, *tmpidxpath = NULL;
117 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
118 const char *repo_path = NULL;
119 struct got_pathlist_head have_refs;
120 struct got_pathlist_entry *pe;
121 struct got_reflist_head my_refs;
122 struct got_reflist_entry *re;
123 off_t packfile_size = 0;
124 struct got_packfile_hdr pack_hdr;
125 uint32_t nobj = 0;
126 char *path;
127 char *progress = NULL;
129 *pack_hash = NULL;
131 /*
132 * Prevent fetching of references that won't make any
133 * sense outside of the remote repository's context.
134 */
135 TAILQ_FOREACH(pe, wanted_refs, entry) {
136 const char *refname = pe->path;
137 if (strncmp(refname, "refs/got/", 9) == 0 ||
138 strncmp(refname, "got/", 4) == 0 ||
139 strncmp(refname, "refs/remotes/", 13) == 0 ||
140 strncmp(refname, "remotes/", 8) == 0)
141 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
144 if (!list_refs_only)
145 repo_path = got_repo_get_path_git_dir(repo);
147 for (i = 0; i < nitems(tmpfds); i++)
148 tmpfds[i] = -1;
150 TAILQ_INIT(&have_refs);
151 TAILQ_INIT(&my_refs);
153 if (!list_refs_only) {
154 err = got_ref_list(&my_refs, repo, NULL,
155 got_ref_cmp_by_name, NULL);
156 if (err)
157 goto done;
160 TAILQ_FOREACH(re, &my_refs, entry) {
161 struct got_object_id *id;
162 const char *refname;
164 if (got_ref_is_symbolic(re->ref))
165 continue;
167 err = got_ref_resolve(&id, repo, re->ref);
168 if (err)
169 goto done;
170 refname = strdup(got_ref_get_name(re->ref));
171 if (refname == NULL) {
172 err = got_error_from_errno("strdup");
173 goto done;
175 err = got_pathlist_append(&have_refs, refname, id);
176 if (err)
177 goto done;
180 if (list_refs_only) {
181 packfd = got_opentempfd();
182 if (packfd == -1) {
183 err = got_error_from_errno("got_opentempfd");
184 goto done;
186 } else {
187 if (asprintf(&path, "%s/%s/fetching.pack",
188 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
189 err = got_error_from_errno("asprintf");
190 goto done;
192 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
193 free(path);
194 if (err)
195 goto done;
196 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
197 err = got_error_from_errno2("fchmod", tmppackpath);
198 goto done;
201 if (list_refs_only) {
202 idxfd = got_opentempfd();
203 if (idxfd == -1) {
204 err = got_error_from_errno("got_opentempfd");
205 goto done;
207 } else {
208 if (asprintf(&path, "%s/%s/fetching.idx",
209 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
210 err = got_error_from_errno("asprintf");
211 goto done;
213 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
214 free(path);
215 if (err)
216 goto done;
217 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
218 err = got_error_from_errno2("fchmod", tmpidxpath);
219 goto done;
222 nidxfd = dup(idxfd);
223 if (nidxfd == -1) {
224 err = got_error_from_errno("dup");
225 goto done;
228 for (i = 0; i < nitems(tmpfds); i++) {
229 tmpfds[i] = got_opentempfd();
230 if (tmpfds[i] == -1) {
231 err = got_error_from_errno("got_opentempfd");
232 goto done;
236 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
237 err = got_error_from_errno("socketpair");
238 goto done;
241 fetchpid = fork();
242 if (fetchpid == -1) {
243 err = got_error_from_errno("fork");
244 goto done;
245 } else if (fetchpid == 0){
246 got_privsep_exec_child(imsg_fetchfds,
247 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
250 if (close(imsg_fetchfds[1]) == -1) {
251 err = got_error_from_errno("close");
252 goto done;
254 imsg_init(&fetchibuf, imsg_fetchfds[0]);
255 nfetchfd = dup(fetchfd);
256 if (nfetchfd == -1) {
257 err = got_error_from_errno("dup");
258 goto done;
260 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
261 fetch_all_branches, wanted_branches, wanted_refs,
262 list_refs_only, worktree_refname, remote_head, no_head, verbosity);
263 if (err != NULL)
264 goto done;
265 nfetchfd = -1;
266 npackfd = dup(packfd);
267 if (npackfd == -1) {
268 err = got_error_from_errno("dup");
269 goto done;
271 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
272 if (err != NULL)
273 goto done;
274 npackfd = -1;
276 packfile_size = 0;
277 progress = calloc(GOT_PKT_MAX, 1);
278 if (progress == NULL) {
279 err = got_error_from_errno("calloc");
280 goto done;
283 *pack_hash = calloc(1, sizeof(**pack_hash));
284 if (*pack_hash == NULL) {
285 err = got_error_from_errno("calloc");
286 goto done;
289 while (!done) {
290 struct got_object_id *id = NULL;
291 char *refname = NULL;
292 char *server_progress = NULL;
293 off_t packfile_size_cur = 0;
295 err = got_privsep_recv_fetch_progress(&done,
296 &id, &refname, symrefs, &server_progress,
297 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
298 if (err != NULL)
299 goto done;
300 /* Don't report size progress for an empty pack file. */
301 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
302 packfile_size_cur = 0;
303 if (!done && refname && id) {
304 err = got_pathlist_insert(NULL, refs, refname, id);
305 if (err)
306 goto done;
307 } else if (!done && server_progress) {
308 char *p;
309 /*
310 * XXX git-daemon tends to send batched output with
311 * lines spanning separate packets. Buffer progress
312 * output until we see a CR or LF to avoid giving
313 * partial lines of progress output to the callback.
314 */
315 if (strlcat(progress, server_progress,
316 GOT_PKT_MAX) >= GOT_PKT_MAX) {
317 progress[0] = '\0'; /* discard */
318 continue;
320 while ((p = strchr(progress, '\r')) != NULL ||
321 (p = strchr(progress, '\n')) != NULL) {
322 char *s;
323 size_t n;
324 char c = *p;
325 *p = '\0';
326 if (asprintf(&s, "%s%s", progress,
327 c == '\n' ? "\n" : "") == -1) {
328 err = got_error_from_errno("asprintf");
329 goto done;
331 err = progress_cb(progress_arg, s,
332 packfile_size_cur, 0, 0, 0, 0);
333 free(s);
334 if (err)
335 break;
336 n = strlen(progress);
337 if (n < GOT_PKT_MAX - 1) {
338 memmove(progress, &progress[n + 1],
339 GOT_PKT_MAX - n - 1);
340 } else
341 progress[0] = '\0';
343 free(server_progress);
344 if (err)
345 goto done;
346 } else if (!done && packfile_size_cur != packfile_size) {
347 err = progress_cb(progress_arg, NULL,
348 packfile_size_cur, 0, 0, 0, 0);
349 if (err)
350 break;
351 packfile_size = packfile_size_cur;
354 if (close(imsg_fetchfds[0]) == -1) {
355 err = got_error_from_errno("close");
356 goto done;
358 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
359 err = got_error_from_errno("waitpid");
360 goto done;
363 if (lseek(packfd, 0, SEEK_SET) == -1) {
364 err = got_error_from_errno("lseek");
365 goto done;
368 /* If zero data was fetched without error we are already up-to-date. */
369 if (packfile_size == 0) {
370 free(*pack_hash);
371 *pack_hash = NULL;
372 goto done;
373 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
374 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
375 goto done;
376 } else {
377 ssize_t n;
379 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
380 if (n == -1) {
381 err = got_error_from_errno("read");
382 goto done;
384 if (n != ssizeof(pack_hdr)) {
385 err = got_error(GOT_ERR_IO);
386 goto done;
388 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
389 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
390 "bad pack file signature");
391 goto done;
393 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
394 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
395 "bad pack file version");
396 goto done;
398 nobj = be32toh(pack_hdr.nobjects);
399 if (nobj == 0 &&
400 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
401 return got_error_msg(GOT_ERR_BAD_PACKFILE,
402 "bad pack file with zero objects");
403 if (nobj != 0 &&
404 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
405 return got_error_msg(GOT_ERR_BAD_PACKFILE,
406 "empty pack file with non-zero object count");
409 /*
410 * If the pack file contains no objects, we may only need to update
411 * references in our repository. The caller will take care of that.
412 */
413 if (nobj == 0) {
414 free(*pack_hash);
415 *pack_hash = NULL;
416 goto done;
419 if (lseek(packfd, 0, SEEK_SET) == -1) {
420 err = got_error_from_errno("lseek");
421 goto done;
424 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
425 err = got_error_from_errno("socketpair");
426 goto done;
428 idxpid = fork();
429 if (idxpid == -1) {
430 err= got_error_from_errno("fork");
431 goto done;
432 } else if (idxpid == 0)
433 got_privsep_exec_child(imsg_idxfds,
434 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
435 if (close(imsg_idxfds[1]) == -1) {
436 err = got_error_from_errno("close");
437 goto done;
439 imsg_init(&idxibuf, imsg_idxfds[0]);
441 npackfd = dup(packfd);
442 if (npackfd == -1) {
443 err = got_error_from_errno("dup");
444 goto done;
446 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
447 npackfd);
448 if (err != NULL)
449 goto done;
450 npackfd = -1;
451 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
452 if (err != NULL)
453 goto done;
454 nidxfd = -1;
455 for (i = 0; i < nitems(tmpfds); i++) {
456 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
457 if (err != NULL)
458 goto done;
459 tmpfds[i] = -1;
461 done = 0;
462 while (!done) {
463 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
465 err = got_privsep_recv_index_progress(&done, &nobj_total,
466 &nobj_indexed, &nobj_loose, &nobj_resolved,
467 &idxibuf);
468 if (err != NULL)
469 goto done;
470 if (nobj_indexed != 0) {
471 err = progress_cb(progress_arg, NULL,
472 packfile_size, nobj_total,
473 nobj_indexed, nobj_loose, nobj_resolved);
474 if (err)
475 break;
478 if (close(imsg_idxfds[0]) == -1) {
479 err = got_error_from_errno("close");
480 goto done;
482 if (waitpid(idxpid, &idxstatus, 0) == -1) {
483 err = got_error_from_errno("waitpid");
484 goto done;
487 err = got_object_id_str(&id_str, *pack_hash);
488 if (err)
489 goto done;
490 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
491 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
492 err = got_error_from_errno("asprintf");
493 goto done;
496 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
497 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
498 err = got_error_from_errno("asprintf");
499 goto done;
501 free(id_str);
502 id_str = NULL;
504 if (rename(tmppackpath, packpath) == -1) {
505 err = got_error_from_errno3("rename", tmppackpath, packpath);
506 goto done;
508 free(tmppackpath);
509 tmppackpath = NULL;
510 if (rename(tmpidxpath, idxpath) == -1) {
511 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
512 goto done;
514 free(tmpidxpath);
515 tmpidxpath = NULL;
517 done:
518 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
519 err = got_error_from_errno2("unlink", tmppackpath);
520 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
521 err = got_error_from_errno2("unlink", tmpidxpath);
522 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
523 err = got_error_from_errno("close");
524 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
525 err = got_error_from_errno("close");
526 if (packfd != -1 && close(packfd) == -1 && err == NULL)
527 err = got_error_from_errno("close");
528 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
529 err = got_error_from_errno("close");
530 for (i = 0; i < nitems(tmpfds); i++) {
531 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
532 err = got_error_from_errno("close");
534 free(tmppackpath);
535 free(tmpidxpath);
536 free(idxpath);
537 free(id_str);
538 free(packpath);
539 free(progress);
541 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
542 got_ref_list_free(&my_refs);
543 if (err) {
544 free(*pack_hash);
545 *pack_hash = NULL;
546 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
547 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);
549 return err;