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 <unistd.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <limits.h>
37 #include <time.h>
39 #include "got_error.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_object.h"
46 #include "got_opentemp.h"
47 #include "got_fetch.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_dial.h"
60 #include "got_lib_pkt.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 #endif
66 #ifndef ssizeof
67 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
68 #endif
70 #ifndef MIN
71 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
72 #endif
74 const struct got_error *
75 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
76 const char *host, const char *port, const char *server_path, int verbosity)
77 {
78 const struct got_error *err = NULL;
80 *fetchpid = -1;
81 *fetchfd = -1;
83 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
84 err = got_dial_ssh(fetchpid, fetchfd, host, port,
85 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
86 else if (strcmp(proto, "git") == 0)
87 err = got_dial_git(fetchfd, host, port, server_path,
88 GOT_DIAL_DIRECTION_FETCH);
89 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
90 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
91 else
92 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
93 return err;
94 }
96 const struct got_error *
97 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
98 struct got_pathlist_head *symrefs, const char *remote_name,
99 int mirror_references, int fetch_all_branches,
100 struct got_pathlist_head *wanted_branches,
101 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
102 int fetchfd, struct got_repository *repo, const char *worktree_refname,
103 const char *remote_head, int no_head, got_fetch_progress_cb progress_cb,
104 void *progress_arg)
106 size_t i;
107 int imsg_fetchfds[2], imsg_idxfds[2];
108 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
109 int tmpfds[3];
110 int fetchstatus, idxstatus, done = 0;
111 const struct got_error *err;
112 struct imsgbuf fetchibuf, idxibuf;
113 pid_t fetchpid, idxpid;
114 char *tmppackpath = NULL, *tmpidxpath = NULL;
115 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
116 const char *repo_path = NULL;
117 struct got_pathlist_head have_refs;
118 struct got_pathlist_entry *pe;
119 struct got_reflist_head my_refs;
120 struct got_reflist_entry *re;
121 off_t packfile_size = 0;
122 struct got_packfile_hdr pack_hdr;
123 uint32_t nobj = 0;
124 char *path;
125 char *progress = NULL;
127 *pack_hash = NULL;
129 /*
130 * Prevent fetching of references that won't make any
131 * sense outside of the remote repository's context.
132 */
133 TAILQ_FOREACH(pe, wanted_refs, entry) {
134 const char *refname = pe->path;
135 if (strncmp(refname, "refs/got/", 9) == 0 ||
136 strncmp(refname, "got/", 4) == 0 ||
137 strncmp(refname, "refs/remotes/", 13) == 0 ||
138 strncmp(refname, "remotes/", 8) == 0)
139 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
142 if (!list_refs_only)
143 repo_path = got_repo_get_path_git_dir(repo);
145 for (i = 0; i < nitems(tmpfds); i++)
146 tmpfds[i] = -1;
148 TAILQ_INIT(&have_refs);
149 TAILQ_INIT(&my_refs);
151 if (!list_refs_only) {
152 err = got_ref_list(&my_refs, repo, NULL,
153 got_ref_cmp_by_name, NULL);
154 if (err)
155 goto done;
158 TAILQ_FOREACH(re, &my_refs, entry) {
159 struct got_object_id *id;
160 const char *refname;
162 if (got_ref_is_symbolic(re->ref))
163 continue;
165 err = got_ref_resolve(&id, repo, re->ref);
166 if (err)
167 goto done;
168 refname = strdup(got_ref_get_name(re->ref));
169 if (refname == NULL) {
170 err = got_error_from_errno("strdup");
171 goto done;
173 err = got_pathlist_append(&have_refs, refname, id);
174 if (err)
175 goto done;
178 if (list_refs_only) {
179 packfd = got_opentempfd();
180 if (packfd == -1) {
181 err = got_error_from_errno("got_opentempfd");
182 goto done;
184 } else {
185 if (asprintf(&path, "%s/%s/fetching.pack",
186 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
187 err = got_error_from_errno("asprintf");
188 goto done;
190 err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
191 free(path);
192 if (err)
193 goto done;
194 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
195 err = got_error_from_errno2("fchmod", tmppackpath);
196 goto done;
199 if (list_refs_only) {
200 idxfd = got_opentempfd();
201 if (idxfd == -1) {
202 err = got_error_from_errno("got_opentempfd");
203 goto done;
205 } else {
206 if (asprintf(&path, "%s/%s/fetching.idx",
207 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
208 err = got_error_from_errno("asprintf");
209 goto done;
211 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
212 free(path);
213 if (err)
214 goto done;
215 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
216 err = got_error_from_errno2("fchmod", tmpidxpath);
217 goto done;
220 nidxfd = dup(idxfd);
221 if (nidxfd == -1) {
222 err = got_error_from_errno("dup");
223 goto done;
226 for (i = 0; i < nitems(tmpfds); i++) {
227 tmpfds[i] = got_opentempfd();
228 if (tmpfds[i] == -1) {
229 err = got_error_from_errno("got_opentempfd");
230 goto done;
234 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
235 err = got_error_from_errno("socketpair");
236 goto done;
239 fetchpid = fork();
240 if (fetchpid == -1) {
241 err = got_error_from_errno("fork");
242 goto done;
243 } else if (fetchpid == 0){
244 got_privsep_exec_child(imsg_fetchfds,
245 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
248 if (close(imsg_fetchfds[1]) == -1) {
249 err = got_error_from_errno("close");
250 goto done;
252 imsg_init(&fetchibuf, imsg_fetchfds[0]);
253 nfetchfd = dup(fetchfd);
254 if (nfetchfd == -1) {
255 err = got_error_from_errno("dup");
256 goto done;
258 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
259 fetch_all_branches, wanted_branches, wanted_refs,
260 list_refs_only, worktree_refname, remote_head, no_head, verbosity);
261 if (err != NULL)
262 goto done;
263 nfetchfd = -1;
264 npackfd = dup(packfd);
265 if (npackfd == -1) {
266 err = got_error_from_errno("dup");
267 goto done;
269 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
270 if (err != NULL)
271 goto done;
272 npackfd = -1;
274 packfile_size = 0;
275 progress = calloc(GOT_PKT_MAX, 1);
276 if (progress == NULL) {
277 err = got_error_from_errno("calloc");
278 goto done;
281 *pack_hash = calloc(1, sizeof(**pack_hash));
282 if (*pack_hash == NULL) {
283 err = got_error_from_errno("calloc");
284 goto done;
287 while (!done) {
288 struct got_object_id *id = NULL;
289 char *refname = NULL;
290 char *server_progress = NULL;
291 off_t packfile_size_cur = 0;
293 err = got_privsep_recv_fetch_progress(&done,
294 &id, &refname, symrefs, &server_progress,
295 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
296 if (err != NULL)
297 goto done;
298 /* Don't report size progress for an empty pack file. */
299 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
300 packfile_size_cur = 0;
301 if (!done && refname && id) {
302 err = got_pathlist_insert(NULL, refs, refname, id);
303 if (err)
304 goto done;
305 } else if (!done && server_progress) {
306 char *p;
307 /*
308 * XXX git-daemon tends to send batched output with
309 * lines spanning separate packets. Buffer progress
310 * output until we see a CR or LF to avoid giving
311 * partial lines of progress output to the callback.
312 */
313 if (strlcat(progress, server_progress,
314 GOT_PKT_MAX) >= GOT_PKT_MAX) {
315 progress[0] = '\0'; /* discard */
316 continue;
318 while ((p = strchr(progress, '\r')) != NULL ||
319 (p = strchr(progress, '\n')) != NULL) {
320 char *s;
321 size_t n;
322 char c = *p;
323 *p = '\0';
324 if (asprintf(&s, "%s%s", progress,
325 c == '\n' ? "\n" : "") == -1) {
326 err = got_error_from_errno("asprintf");
327 goto done;
329 err = progress_cb(progress_arg, s,
330 packfile_size_cur, 0, 0, 0, 0);
331 free(s);
332 if (err)
333 break;
334 n = strlen(progress);
335 if (n < GOT_PKT_MAX - 1) {
336 memmove(progress, &progress[n + 1],
337 GOT_PKT_MAX - n - 1);
338 } else
339 progress[0] = '\0';
341 free(server_progress);
342 if (err)
343 goto done;
344 } else if (!done && packfile_size_cur != packfile_size) {
345 err = progress_cb(progress_arg, NULL,
346 packfile_size_cur, 0, 0, 0, 0);
347 if (err)
348 break;
349 packfile_size = packfile_size_cur;
352 if (close(imsg_fetchfds[0]) == -1) {
353 err = got_error_from_errno("close");
354 goto done;
356 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
357 err = got_error_from_errno("waitpid");
358 goto done;
361 if (lseek(packfd, 0, SEEK_SET) == -1) {
362 err = got_error_from_errno("lseek");
363 goto done;
366 /* If zero data was fetched without error we are already up-to-date. */
367 if (packfile_size == 0) {
368 free(*pack_hash);
369 *pack_hash = NULL;
370 goto done;
371 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
372 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
373 goto done;
374 } else {
375 ssize_t n;
377 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
378 if (n == -1) {
379 err = got_error_from_errno("read");
380 goto done;
382 if (n != ssizeof(pack_hdr)) {
383 err = got_error(GOT_ERR_IO);
384 goto done;
386 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
387 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
388 "bad pack file signature");
389 goto done;
391 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
392 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
393 "bad pack file version");
394 goto done;
396 nobj = be32toh(pack_hdr.nobjects);
397 if (nobj == 0 &&
398 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
399 return got_error_msg(GOT_ERR_BAD_PACKFILE,
400 "bad pack file with zero objects");
401 if (nobj != 0 &&
402 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
403 return got_error_msg(GOT_ERR_BAD_PACKFILE,
404 "empty pack file with non-zero object count");
407 /*
408 * If the pack file contains no objects, we may only need to update
409 * references in our repository. The caller will take care of that.
410 */
411 if (nobj == 0) {
412 free(*pack_hash);
413 *pack_hash = NULL;
414 goto done;
417 if (lseek(packfd, 0, SEEK_SET) == -1) {
418 err = got_error_from_errno("lseek");
419 goto done;
422 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
423 err = got_error_from_errno("socketpair");
424 goto done;
426 idxpid = fork();
427 if (idxpid == -1) {
428 err= got_error_from_errno("fork");
429 goto done;
430 } else if (idxpid == 0)
431 got_privsep_exec_child(imsg_idxfds,
432 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
433 if (close(imsg_idxfds[1]) == -1) {
434 err = got_error_from_errno("close");
435 goto done;
437 imsg_init(&idxibuf, imsg_idxfds[0]);
439 npackfd = dup(packfd);
440 if (npackfd == -1) {
441 err = got_error_from_errno("dup");
442 goto done;
444 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
445 npackfd);
446 if (err != NULL)
447 goto done;
448 npackfd = -1;
449 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
450 if (err != NULL)
451 goto done;
452 nidxfd = -1;
453 for (i = 0; i < nitems(tmpfds); i++) {
454 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
455 if (err != NULL)
456 goto done;
457 tmpfds[i] = -1;
459 done = 0;
460 while (!done) {
461 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
463 err = got_privsep_recv_index_progress(&done, &nobj_total,
464 &nobj_indexed, &nobj_loose, &nobj_resolved,
465 &idxibuf);
466 if (err != NULL)
467 goto done;
468 if (nobj_indexed != 0) {
469 err = progress_cb(progress_arg, NULL,
470 packfile_size, nobj_total,
471 nobj_indexed, nobj_loose, nobj_resolved);
472 if (err)
473 break;
476 if (close(imsg_idxfds[0]) == -1) {
477 err = got_error_from_errno("close");
478 goto done;
480 if (waitpid(idxpid, &idxstatus, 0) == -1) {
481 err = got_error_from_errno("waitpid");
482 goto done;
485 err = got_object_id_str(&id_str, *pack_hash);
486 if (err)
487 goto done;
488 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
489 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
490 err = got_error_from_errno("asprintf");
491 goto done;
494 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
495 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
496 err = got_error_from_errno("asprintf");
497 goto done;
499 free(id_str);
500 id_str = NULL;
502 if (rename(tmppackpath, packpath) == -1) {
503 err = got_error_from_errno3("rename", tmppackpath, packpath);
504 goto done;
506 free(tmppackpath);
507 tmppackpath = NULL;
508 if (rename(tmpidxpath, idxpath) == -1) {
509 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
510 goto done;
512 free(tmpidxpath);
513 tmpidxpath = NULL;
515 done:
516 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
517 err = got_error_from_errno2("unlink", tmppackpath);
518 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
519 err = got_error_from_errno2("unlink", tmpidxpath);
520 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
521 err = got_error_from_errno("close");
522 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
523 err = got_error_from_errno("close");
524 if (packfd != -1 && close(packfd) == -1 && err == NULL)
525 err = got_error_from_errno("close");
526 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
527 err = got_error_from_errno("close");
528 for (i = 0; i < nitems(tmpfds); i++) {
529 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
530 err = got_error_from_errno("close");
532 free(tmppackpath);
533 free(tmpidxpath);
534 free(idxpath);
535 free(id_str);
536 free(packpath);
537 free(progress);
539 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
540 got_ref_list_free(&my_refs);
541 if (err) {
542 free(*pack_hash);
543 *pack_hash = NULL;
544 got_pathlist_free(refs, GOT_PATHLIST_FREE_ALL);
545 got_pathlist_free(symrefs, GOT_PATHLIST_FREE_ALL);
547 return err;