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 || strcmp(proto, "git+http") == 0)
96 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
97 else
98 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
99 return err;
102 const struct got_error *
103 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
104 struct got_pathlist_head *symrefs, const char *remote_name,
105 int mirror_references, int fetch_all_branches,
106 struct got_pathlist_head *wanted_branches,
107 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
108 int fetchfd, struct got_repository *repo, const char *worktree_refname,
109 const char *remote_head, int no_head, got_fetch_progress_cb progress_cb,
110 void *progress_arg)
112 size_t i;
113 int imsg_fetchfds[2], imsg_idxfds[2];
114 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
115 int tmpfds[3];
116 int fetchstatus, idxstatus, done = 0;
117 const struct got_error *err;
118 struct imsgbuf fetchibuf, idxibuf;
119 pid_t fetchpid, idxpid;
120 char *tmppackpath = NULL, *tmpidxpath = NULL;
121 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
122 const char *repo_path = NULL;
123 struct got_pathlist_head have_refs;
124 struct got_pathlist_entry *pe;
125 struct got_reflist_head my_refs;
126 struct got_reflist_entry *re;
127 off_t packfile_size = 0;
128 struct got_packfile_hdr pack_hdr;
129 uint32_t nobj = 0;
130 char *path;
131 char *progress = NULL;
133 *pack_hash = NULL;
135 /*
136 * Prevent fetching of references that won't make any
137 * sense outside of the remote repository's context.
138 */
139 TAILQ_FOREACH(pe, wanted_refs, entry) {
140 const char *refname = pe->path;
141 if (strncmp(refname, "refs/got/", 9) == 0 ||
142 strncmp(refname, "got/", 4) == 0 ||
143 strncmp(refname, "refs/remotes/", 13) == 0 ||
144 strncmp(refname, "remotes/", 8) == 0)
145 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
148 if (!list_refs_only)
149 repo_path = got_repo_get_path_git_dir(repo);
151 for (i = 0; i < nitems(tmpfds); i++)
152 tmpfds[i] = -1;
154 TAILQ_INIT(&have_refs);
155 TAILQ_INIT(&my_refs);
157 if (!list_refs_only) {
158 err = got_ref_list(&my_refs, repo, NULL,
159 got_ref_cmp_by_name, NULL);
160 if (err)
161 goto done;
164 TAILQ_FOREACH(re, &my_refs, entry) {
165 struct got_object_id *id;
166 const char *refname;
168 if (got_ref_is_symbolic(re->ref))
169 continue;
171 err = got_ref_resolve(&id, repo, re->ref);
172 if (err)
173 goto done;
174 refname = strdup(got_ref_get_name(re->ref));
175 if (refname == NULL) {
176 err = got_error_from_errno("strdup");
177 goto done;
179 err = got_pathlist_append(&have_refs, refname, id);
180 if (err)
181 goto done;
184 if (list_refs_only) {
185 packfd = got_opentempfd();
186 if (packfd == -1) {
187 err = got_error_from_errno("got_opentempfd");
188 goto done;
190 } else {
191 if (asprintf(&path, "%s/%s/fetching.pack",
192 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
193 err = got_error_from_errno("asprintf");
194 goto done;
196 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
197 free(path);
198 if (err)
199 goto done;
200 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
201 err = got_error_from_errno2("fchmod", tmppackpath);
202 goto done;
205 if (list_refs_only) {
206 idxfd = got_opentempfd();
207 if (idxfd == -1) {
208 err = got_error_from_errno("got_opentempfd");
209 goto done;
211 } else {
212 if (asprintf(&path, "%s/%s/fetching.idx",
213 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
214 err = got_error_from_errno("asprintf");
215 goto done;
217 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
218 free(path);
219 if (err)
220 goto done;
221 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
222 err = got_error_from_errno2("fchmod", tmpidxpath);
223 goto done;
226 nidxfd = dup(idxfd);
227 if (nidxfd == -1) {
228 err = got_error_from_errno("dup");
229 goto done;
232 for (i = 0; i < nitems(tmpfds); i++) {
233 tmpfds[i] = got_opentempfd();
234 if (tmpfds[i] == -1) {
235 err = got_error_from_errno("got_opentempfd");
236 goto done;
240 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
241 err = got_error_from_errno("socketpair");
242 goto done;
245 fetchpid = fork();
246 if (fetchpid == -1) {
247 err = got_error_from_errno("fork");
248 goto done;
249 } else if (fetchpid == 0){
250 got_privsep_exec_child(imsg_fetchfds,
251 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
254 if (close(imsg_fetchfds[1]) == -1) {
255 err = got_error_from_errno("close");
256 goto done;
258 imsg_init(&fetchibuf, imsg_fetchfds[0]);
259 nfetchfd = dup(fetchfd);
260 if (nfetchfd == -1) {
261 err = got_error_from_errno("dup");
262 goto done;
264 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
265 fetch_all_branches, wanted_branches, wanted_refs,
266 list_refs_only, worktree_refname, remote_head, no_head, verbosity);
267 if (err != NULL)
268 goto done;
269 nfetchfd = -1;
270 npackfd = dup(packfd);
271 if (npackfd == -1) {
272 err = got_error_from_errno("dup");
273 goto done;
275 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
276 if (err != NULL)
277 goto done;
278 npackfd = -1;
280 packfile_size = 0;
281 progress = calloc(GOT_PKT_MAX, 1);
282 if (progress == NULL) {
283 err = got_error_from_errno("calloc");
284 goto done;
287 *pack_hash = calloc(1, sizeof(**pack_hash));
288 if (*pack_hash == NULL) {
289 err = got_error_from_errno("calloc");
290 goto done;
293 while (!done) {
294 struct got_object_id *id = NULL;
295 char *refname = NULL;
296 char *server_progress = NULL;
297 off_t packfile_size_cur = 0;
299 err = got_privsep_recv_fetch_progress(&done,
300 &id, &refname, symrefs, &server_progress,
301 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
302 if (err != NULL)
303 goto done;
304 /* Don't report size progress for an empty pack file. */
305 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
306 packfile_size_cur = 0;
307 if (!done && refname && id) {
308 err = got_pathlist_insert(NULL, refs, refname, id);
309 if (err)
310 goto done;
311 } else if (!done && server_progress) {
312 char *p;
313 /*
314 * XXX git-daemon tends to send batched output with
315 * lines spanning separate packets. Buffer progress
316 * output until we see a CR or LF to avoid giving
317 * partial lines of progress output to the callback.
318 */
319 if (strlcat(progress, server_progress,
320 GOT_PKT_MAX) >= GOT_PKT_MAX) {
321 progress[0] = '\0'; /* discard */
322 continue;
324 while ((p = strchr(progress, '\r')) != NULL ||
325 (p = strchr(progress, '\n')) != NULL) {
326 char *s;
327 size_t n;
328 char c = *p;
329 *p = '\0';
330 if (asprintf(&s, "%s%s", progress,
331 c == '\n' ? "\n" : "") == -1) {
332 err = got_error_from_errno("asprintf");
333 goto done;
335 err = progress_cb(progress_arg, s,
336 packfile_size_cur, 0, 0, 0, 0);
337 free(s);
338 if (err)
339 break;
340 n = strlen(progress);
341 if (n < GOT_PKT_MAX - 1) {
342 memmove(progress, &progress[n + 1],
343 GOT_PKT_MAX - n - 1);
344 } else
345 progress[0] = '\0';
347 free(server_progress);
348 if (err)
349 goto done;
350 } else if (!done && packfile_size_cur != packfile_size) {
351 err = progress_cb(progress_arg, NULL,
352 packfile_size_cur, 0, 0, 0, 0);
353 if (err)
354 break;
355 packfile_size = packfile_size_cur;
358 if (close(imsg_fetchfds[0]) == -1) {
359 err = got_error_from_errno("close");
360 goto done;
362 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
363 err = got_error_from_errno("waitpid");
364 goto done;
367 if (lseek(packfd, 0, SEEK_SET) == -1) {
368 err = got_error_from_errno("lseek");
369 goto done;
372 /* If zero data was fetched without error we are already up-to-date. */
373 if (packfile_size == 0) {
374 free(*pack_hash);
375 *pack_hash = NULL;
376 goto done;
377 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
378 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
379 goto done;
380 } else {
381 ssize_t n;
383 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
384 if (n == -1) {
385 err = got_error_from_errno("read");
386 goto done;
388 if (n != ssizeof(pack_hdr)) {
389 err = got_error(GOT_ERR_IO);
390 goto done;
392 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
393 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
394 "bad pack file signature");
395 goto done;
397 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
398 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
399 "bad pack file version");
400 goto done;
402 nobj = be32toh(pack_hdr.nobjects);
403 if (nobj == 0 &&
404 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
405 return got_error_msg(GOT_ERR_BAD_PACKFILE,
406 "bad pack file with zero objects");
407 if (nobj != 0 &&
408 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
409 return got_error_msg(GOT_ERR_BAD_PACKFILE,
410 "empty pack file with non-zero object count");
413 /*
414 * If the pack file contains no objects, we may only need to update
415 * references in our repository. The caller will take care of that.
416 */
417 if (nobj == 0) {
418 free(*pack_hash);
419 *pack_hash = NULL;
420 goto done;
423 if (lseek(packfd, 0, SEEK_SET) == -1) {
424 err = got_error_from_errno("lseek");
425 goto done;
428 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
429 err = got_error_from_errno("socketpair");
430 goto done;
432 idxpid = fork();
433 if (idxpid == -1) {
434 err= got_error_from_errno("fork");
435 goto done;
436 } else if (idxpid == 0)
437 got_privsep_exec_child(imsg_idxfds,
438 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
439 if (close(imsg_idxfds[1]) == -1) {
440 err = got_error_from_errno("close");
441 goto done;
443 imsg_init(&idxibuf, imsg_idxfds[0]);
445 npackfd = dup(packfd);
446 if (npackfd == -1) {
447 err = got_error_from_errno("dup");
448 goto done;
450 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
451 npackfd);
452 if (err != NULL)
453 goto done;
454 npackfd = -1;
455 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
456 if (err != NULL)
457 goto done;
458 nidxfd = -1;
459 for (i = 0; i < nitems(tmpfds); i++) {
460 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
461 if (err != NULL)
462 goto done;
463 tmpfds[i] = -1;
465 done = 0;
466 while (!done) {
467 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
469 err = got_privsep_recv_index_progress(&done, &nobj_total,
470 &nobj_indexed, &nobj_loose, &nobj_resolved,
471 &idxibuf);
472 if (err != NULL)
473 goto done;
474 if (nobj_indexed != 0) {
475 err = progress_cb(progress_arg, NULL,
476 packfile_size, nobj_total,
477 nobj_indexed, nobj_loose, nobj_resolved);
478 if (err)
479 break;
482 if (close(imsg_idxfds[0]) == -1) {
483 err = got_error_from_errno("close");
484 goto done;
486 if (waitpid(idxpid, &idxstatus, 0) == -1) {
487 err = got_error_from_errno("waitpid");
488 goto done;
491 err = got_object_id_str(&id_str, *pack_hash);
492 if (err)
493 goto done;
494 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
495 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
496 err = got_error_from_errno("asprintf");
497 goto done;
500 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
501 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
502 err = got_error_from_errno("asprintf");
503 goto done;
505 free(id_str);
506 id_str = NULL;
508 if (rename(tmppackpath, packpath) == -1) {
509 err = got_error_from_errno3("rename", tmppackpath, packpath);
510 goto done;
512 free(tmppackpath);
513 tmppackpath = NULL;
514 if (rename(tmpidxpath, idxpath) == -1) {
515 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
516 goto done;
518 free(tmpidxpath);
519 tmpidxpath = NULL;
521 done:
522 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
523 err = got_error_from_errno2("unlink", tmppackpath);
524 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
525 err = got_error_from_errno2("unlink", tmpidxpath);
526 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
527 err = got_error_from_errno("close");
528 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
529 err = got_error_from_errno("close");
530 if (packfd != -1 && close(packfd) == -1 && err == NULL)
531 err = got_error_from_errno("close");
532 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
533 err = got_error_from_errno("close");
534 for (i = 0; i < nitems(tmpfds); i++) {
535 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
536 err = got_error_from_errno("close");
538 free(tmppackpath);
539 free(tmpidxpath);
540 free(idxpath);
541 free(id_str);
542 free(packpath);
543 free(progress);
545 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
546 got_ref_list_free(&my_refs);
547 if (err) {
548 free(*pack_hash);
549 *pack_hash = NULL;
550 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
551 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);
553 return err;