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 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
406 "bad pack file with zero objects");
407 goto done;
409 if (nobj != 0 &&
410 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
411 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
412 "empty pack file with non-zero object count");
413 goto done;
417 /*
418 * If the pack file contains no objects, we may only need to update
419 * references in our repository. The caller will take care of that.
420 */
421 if (nobj == 0) {
422 free(*pack_hash);
423 *pack_hash = NULL;
424 goto done;
427 if (lseek(packfd, 0, SEEK_SET) == -1) {
428 err = got_error_from_errno("lseek");
429 goto done;
432 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
433 err = got_error_from_errno("socketpair");
434 goto done;
436 idxpid = fork();
437 if (idxpid == -1) {
438 err= got_error_from_errno("fork");
439 goto done;
440 } else if (idxpid == 0)
441 got_privsep_exec_child(imsg_idxfds,
442 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
443 if (close(imsg_idxfds[1]) == -1) {
444 err = got_error_from_errno("close");
445 goto done;
447 imsg_init(&idxibuf, imsg_idxfds[0]);
449 npackfd = dup(packfd);
450 if (npackfd == -1) {
451 err = got_error_from_errno("dup");
452 goto done;
454 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
455 npackfd);
456 if (err != NULL)
457 goto done;
458 npackfd = -1;
459 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
460 if (err != NULL)
461 goto done;
462 nidxfd = -1;
463 for (i = 0; i < nitems(tmpfds); i++) {
464 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
465 if (err != NULL)
466 goto done;
467 tmpfds[i] = -1;
469 done = 0;
470 while (!done) {
471 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
473 err = got_privsep_recv_index_progress(&done, &nobj_total,
474 &nobj_indexed, &nobj_loose, &nobj_resolved,
475 &idxibuf);
476 if (err != NULL)
477 goto done;
478 if (nobj_indexed != 0) {
479 err = progress_cb(progress_arg, NULL,
480 packfile_size, nobj_total,
481 nobj_indexed, nobj_loose, nobj_resolved);
482 if (err)
483 break;
486 if (close(imsg_idxfds[0]) == -1) {
487 err = got_error_from_errno("close");
488 goto done;
490 if (waitpid(idxpid, &idxstatus, 0) == -1) {
491 err = got_error_from_errno("waitpid");
492 goto done;
495 err = got_object_id_str(&id_str, *pack_hash);
496 if (err)
497 goto done;
498 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
499 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
500 err = got_error_from_errno("asprintf");
501 goto done;
504 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
505 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
506 err = got_error_from_errno("asprintf");
507 goto done;
509 free(id_str);
510 id_str = NULL;
512 if (rename(tmppackpath, packpath) == -1) {
513 err = got_error_from_errno3("rename", tmppackpath, packpath);
514 goto done;
516 free(tmppackpath);
517 tmppackpath = NULL;
518 if (rename(tmpidxpath, idxpath) == -1) {
519 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
520 goto done;
522 free(tmpidxpath);
523 tmpidxpath = NULL;
525 done:
526 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
527 err = got_error_from_errno2("unlink", tmppackpath);
528 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
529 err = got_error_from_errno2("unlink", tmpidxpath);
530 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
531 err = got_error_from_errno("close");
532 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
533 err = got_error_from_errno("close");
534 if (packfd != -1 && close(packfd) == -1 && err == NULL)
535 err = got_error_from_errno("close");
536 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
537 err = got_error_from_errno("close");
538 for (i = 0; i < nitems(tmpfds); i++) {
539 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
540 err = got_error_from_errno("close");
542 free(tmppackpath);
543 free(tmpidxpath);
544 free(idxpath);
545 free(id_str);
546 free(packpath);
547 free(progress);
549 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
550 got_ref_list_free(&my_refs);
551 if (err) {
552 free(*pack_hash);
553 *pack_hash = NULL;
554 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
555 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);
557 return err;