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 <unistd.h>
37 #include <zlib.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <imsg.h>
41 #include <time.h>
42 #include <uuid.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
51 #include "got_opentemp.h"
52 #include "got_fetch.h"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_dial.h"
65 #include "got_lib_pkt.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 #ifndef ssizeof
72 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
73 #endif
75 #ifndef MIN
76 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
77 #endif
79 const struct got_error *
80 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
81 const char *host, const char *port, const char *server_path, int verbosity)
82 {
83 const struct got_error *err = NULL;
85 *fetchpid = -1;
86 *fetchfd = -1;
88 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
89 err = got_dial_ssh(fetchpid, fetchfd, host, port,
90 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
91 else if (strcmp(proto, "git") == 0)
92 err = got_dial_git(fetchfd, host, port, server_path,
93 GOT_DIAL_DIRECTION_FETCH);
94 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
95 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
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,
108 got_fetch_progress_cb progress_cb, void *progress_arg)
110 size_t i;
111 int imsg_fetchfds[2], imsg_idxfds[2];
112 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
113 int tmpfds[3];
114 int fetchstatus, idxstatus, done = 0;
115 const struct got_error *err;
116 struct imsgbuf fetchibuf, idxibuf;
117 pid_t fetchpid, idxpid;
118 char *tmppackpath = NULL, *tmpidxpath = NULL;
119 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
120 const char *repo_path = NULL;
121 struct got_pathlist_head have_refs;
122 struct got_pathlist_entry *pe;
123 struct got_reflist_head my_refs;
124 struct got_reflist_entry *re;
125 off_t packfile_size = 0;
126 struct got_packfile_hdr pack_hdr;
127 uint32_t nobj = 0;
128 char *path;
129 char *progress = NULL;
131 *pack_hash = NULL;
133 /*
134 * Prevent fetching of references that won't make any
135 * sense outside of the remote repository's context.
136 */
137 TAILQ_FOREACH(pe, wanted_refs, entry) {
138 const char *refname = pe->path;
139 if (strncmp(refname, "refs/got/", 9) == 0 ||
140 strncmp(refname, "got/", 4) == 0 ||
141 strncmp(refname, "refs/remotes/", 13) == 0 ||
142 strncmp(refname, "remotes/", 8) == 0)
143 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
146 if (!list_refs_only)
147 repo_path = got_repo_get_path_git_dir(repo);
149 for (i = 0; i < nitems(tmpfds); i++)
150 tmpfds[i] = -1;
152 TAILQ_INIT(&have_refs);
153 TAILQ_INIT(&my_refs);
155 if (!list_refs_only) {
156 err = got_ref_list(&my_refs, repo, NULL,
157 got_ref_cmp_by_name, NULL);
158 if (err)
159 goto done;
162 TAILQ_FOREACH(re, &my_refs, entry) {
163 struct got_object_id *id;
164 const char *refname;
166 if (got_ref_is_symbolic(re->ref))
167 continue;
169 err = got_ref_resolve(&id, repo, re->ref);
170 if (err)
171 goto done;
172 refname = strdup(got_ref_get_name(re->ref));
173 if (refname == NULL) {
174 err = got_error_from_errno("strdup");
175 goto done;
177 err = got_pathlist_append(&have_refs, refname, id);
178 if (err)
179 goto done;
182 if (list_refs_only) {
183 packfd = got_opentempfd();
184 if (packfd == -1) {
185 err = got_error_from_errno("got_opentempfd");
186 goto done;
188 } else {
189 if (asprintf(&path, "%s/%s/fetching.pack",
190 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
191 err = got_error_from_errno("asprintf");
192 goto done;
194 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
195 free(path);
196 if (err)
197 goto done;
198 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
199 err = got_error_from_errno2("fchmod", tmppackpath);
200 goto done;
203 if (list_refs_only) {
204 idxfd = got_opentempfd();
205 if (idxfd == -1) {
206 err = got_error_from_errno("got_opentempfd");
207 goto done;
209 } else {
210 if (asprintf(&path, "%s/%s/fetching.idx",
211 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
212 err = got_error_from_errno("asprintf");
213 goto done;
215 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
216 free(path);
217 if (err)
218 goto done;
219 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
220 err = got_error_from_errno2("fchmod", tmpidxpath);
221 goto done;
224 nidxfd = dup(idxfd);
225 if (nidxfd == -1) {
226 err = got_error_from_errno("dup");
227 goto done;
230 for (i = 0; i < nitems(tmpfds); i++) {
231 tmpfds[i] = got_opentempfd();
232 if (tmpfds[i] == -1) {
233 err = got_error_from_errno("got_opentempfd");
234 goto done;
238 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
239 err = got_error_from_errno("socketpair");
240 goto done;
243 fetchpid = fork();
244 if (fetchpid == -1) {
245 err = got_error_from_errno("fork");
246 goto done;
247 } else if (fetchpid == 0){
248 got_privsep_exec_child(imsg_fetchfds,
249 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
252 if (close(imsg_fetchfds[1]) == -1) {
253 err = got_error_from_errno("close");
254 goto done;
256 imsg_init(&fetchibuf, imsg_fetchfds[0]);
257 nfetchfd = dup(fetchfd);
258 if (nfetchfd == -1) {
259 err = got_error_from_errno("dup");
260 goto done;
262 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
263 fetch_all_branches, wanted_branches, wanted_refs,
264 list_refs_only, verbosity);
265 if (err != NULL)
266 goto done;
267 nfetchfd = -1;
268 npackfd = dup(packfd);
269 if (npackfd == -1) {
270 err = got_error_from_errno("dup");
271 goto done;
273 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
274 if (err != NULL)
275 goto done;
276 npackfd = -1;
278 packfile_size = 0;
279 progress = calloc(GOT_PKT_MAX, 1);
280 if (progress == NULL) {
281 err = got_error_from_errno("calloc");
282 goto done;
285 *pack_hash = calloc(1, sizeof(**pack_hash));
286 if (*pack_hash == NULL) {
287 err = got_error_from_errno("calloc");
288 goto done;
291 while (!done) {
292 struct got_object_id *id = NULL;
293 char *refname = NULL;
294 char *server_progress = NULL;
295 off_t packfile_size_cur = 0;
297 err = got_privsep_recv_fetch_progress(&done,
298 &id, &refname, symrefs, &server_progress,
299 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
300 if (err != NULL)
301 goto done;
302 /* Don't report size progress for an empty pack file. */
303 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
304 packfile_size_cur = 0;
305 if (!done && refname && id) {
306 err = got_pathlist_insert(NULL, refs, refname, id);
307 if (err)
308 goto done;
309 } else if (!done && server_progress) {
310 char *p;
311 /*
312 * XXX git-daemon tends to send batched output with
313 * lines spanning separate packets. Buffer progress
314 * output until we see a CR or LF to avoid giving
315 * partial lines of progress output to the callback.
316 */
317 if (strlcat(progress, server_progress,
318 GOT_PKT_MAX) >= GOT_PKT_MAX) {
319 progress[0] = '\0'; /* discard */
320 continue;
322 while ((p = strchr(progress, '\r')) != NULL ||
323 (p = strchr(progress, '\n')) != NULL) {
324 char *s;
325 size_t n;
326 char c = *p;
327 *p = '\0';
328 if (asprintf(&s, "%s%s", progress,
329 c == '\n' ? "\n" : "") == -1) {
330 err = got_error_from_errno("asprintf");
331 goto done;
333 err = progress_cb(progress_arg, s,
334 packfile_size_cur, 0, 0, 0, 0);
335 free(s);
336 if (err)
337 break;
338 n = strlen(progress);
339 if (n < GOT_PKT_MAX - 1) {
340 memmove(progress, &progress[n + 1],
341 GOT_PKT_MAX - n - 1);
342 } else
343 progress[0] = '\0';
345 free(server_progress);
346 if (err)
347 goto done;
348 } else if (!done && packfile_size_cur != packfile_size) {
349 err = progress_cb(progress_arg, NULL,
350 packfile_size_cur, 0, 0, 0, 0);
351 if (err)
352 break;
353 packfile_size = packfile_size_cur;
356 if (close(imsg_fetchfds[0]) == -1) {
357 err = got_error_from_errno("close");
358 goto done;
360 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
361 err = got_error_from_errno("waitpid");
362 goto done;
365 if (lseek(packfd, 0, SEEK_SET) == -1) {
366 err = got_error_from_errno("lseek");
367 goto done;
370 /* If zero data was fetched without error we are already up-to-date. */
371 if (packfile_size == 0) {
372 free(*pack_hash);
373 *pack_hash = NULL;
374 goto done;
375 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
376 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
377 goto done;
378 } else {
379 ssize_t n;
381 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
382 if (n == -1) {
383 err = got_error_from_errno("read");
384 goto done;
386 if (n != ssizeof(pack_hdr)) {
387 err = got_error(GOT_ERR_IO);
388 goto done;
390 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
391 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
392 "bad pack file signature");
393 goto done;
395 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
396 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
397 "bad pack file version");
398 goto done;
400 nobj = be32toh(pack_hdr.nobjects);
401 if (nobj == 0 &&
402 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
403 return got_error_msg(GOT_ERR_BAD_PACKFILE,
404 "bad pack file with zero objects");
405 if (nobj != 0 &&
406 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
407 return got_error_msg(GOT_ERR_BAD_PACKFILE,
408 "empty pack file with non-zero object count");
411 /*
412 * If the pack file contains no objects, we may only need to update
413 * references in our repository. The caller will take care of that.
414 */
415 if (nobj == 0) {
416 free(*pack_hash);
417 *pack_hash = NULL;
418 goto done;
421 if (lseek(packfd, 0, SEEK_SET) == -1) {
422 err = got_error_from_errno("lseek");
423 goto done;
426 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
427 err = got_error_from_errno("socketpair");
428 goto done;
430 idxpid = fork();
431 if (idxpid == -1) {
432 err= got_error_from_errno("fork");
433 goto done;
434 } else if (idxpid == 0)
435 got_privsep_exec_child(imsg_idxfds,
436 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
437 if (close(imsg_idxfds[1]) == -1) {
438 err = got_error_from_errno("close");
439 goto done;
441 imsg_init(&idxibuf, imsg_idxfds[0]);
443 npackfd = dup(packfd);
444 if (npackfd == -1) {
445 err = got_error_from_errno("dup");
446 goto done;
448 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
449 npackfd);
450 if (err != NULL)
451 goto done;
452 npackfd = -1;
453 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
454 if (err != NULL)
455 goto done;
456 nidxfd = -1;
457 for (i = 0; i < nitems(tmpfds); i++) {
458 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
459 if (err != NULL)
460 goto done;
461 tmpfds[i] = -1;
463 done = 0;
464 while (!done) {
465 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
467 err = got_privsep_recv_index_progress(&done, &nobj_total,
468 &nobj_indexed, &nobj_loose, &nobj_resolved,
469 &idxibuf);
470 if (err != NULL)
471 goto done;
472 if (nobj_indexed != 0) {
473 err = progress_cb(progress_arg, NULL,
474 packfile_size, nobj_total,
475 nobj_indexed, nobj_loose, nobj_resolved);
476 if (err)
477 break;
480 if (close(imsg_idxfds[0]) == -1) {
481 err = got_error_from_errno("close");
482 goto done;
484 if (waitpid(idxpid, &idxstatus, 0) == -1) {
485 err = got_error_from_errno("waitpid");
486 goto done;
489 err = got_object_id_str(&id_str, *pack_hash);
490 if (err)
491 goto done;
492 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
493 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
494 err = got_error_from_errno("asprintf");
495 goto done;
498 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
499 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
500 err = got_error_from_errno("asprintf");
501 goto done;
503 free(id_str);
504 id_str = NULL;
506 if (rename(tmppackpath, packpath) == -1) {
507 err = got_error_from_errno3("rename", tmppackpath, packpath);
508 goto done;
510 free(tmppackpath);
511 tmppackpath = NULL;
512 if (rename(tmpidxpath, idxpath) == -1) {
513 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
514 goto done;
516 free(tmpidxpath);
517 tmpidxpath = NULL;
519 done:
520 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
521 err = got_error_from_errno2("unlink", tmppackpath);
522 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
523 err = got_error_from_errno2("unlink", tmpidxpath);
524 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
525 err = got_error_from_errno("close");
526 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
527 err = got_error_from_errno("close");
528 if (packfd != -1 && close(packfd) == -1 && err == NULL)
529 err = got_error_from_errno("close");
530 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
531 err = got_error_from_errno("close");
532 for (i = 0; i < nitems(tmpfds); i++) {
533 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
534 err = got_error_from_errno("close");
536 free(tmppackpath);
537 free(tmpidxpath);
538 free(idxpath);
539 free(id_str);
540 free(packpath);
541 free(progress);
543 TAILQ_FOREACH(pe, &have_refs, entry) {
544 free((char *)pe->path);
545 free(pe->data);
547 got_pathlist_free(&have_refs);
548 got_ref_list_free(&my_refs);
549 if (err) {
550 free(*pack_hash);
551 *pack_hash = NULL;
552 TAILQ_FOREACH(pe, refs, entry) {
553 free((void *)pe->path);
554 free(pe->data);
556 got_pathlist_free(refs);
557 TAILQ_FOREACH(pe, symrefs, entry) {
558 free((void *)pe->path);
559 free(pe->data);
561 got_pathlist_free(symrefs);
563 return err;