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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/resource.h>
23 #include <sys/socket.h>
25 #include <errno.h>
26 #include <err.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <time.h>
38 #include "got_error.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_path.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_object.h"
45 #include "got_opentemp.h"
46 #include "got_fetch.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_object_cache.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_dial.h"
59 #include "got_lib_pkt.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 #ifndef ssizeof
66 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
67 #endif
69 #ifndef MIN
70 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
71 #endif
73 const struct got_error *
74 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
75 const char *host, const char *port, const char *server_path, int verbosity)
76 {
77 const struct got_error *err = NULL;
79 *fetchpid = -1;
80 *fetchfd = -1;
82 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
83 err = got_dial_ssh(fetchpid, fetchfd, host, port,
84 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
85 else if (strcmp(proto, "git") == 0)
86 err = got_dial_git(fetchfd, host, port, server_path,
87 GOT_DIAL_DIRECTION_FETCH);
88 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
89 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
90 else
91 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
92 return err;
93 }
95 const struct got_error *
96 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
97 struct got_pathlist_head *symrefs, const char *remote_name,
98 int mirror_references, int fetch_all_branches,
99 struct got_pathlist_head *wanted_branches,
100 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
101 int fetchfd, struct got_repository *repo,
102 got_fetch_progress_cb progress_cb, void *progress_arg)
104 size_t i;
105 int imsg_fetchfds[2], imsg_idxfds[2];
106 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
107 int tmpfds[3];
108 int fetchstatus, idxstatus, done = 0;
109 const struct got_error *err;
110 struct imsgbuf fetchibuf, idxibuf;
111 pid_t fetchpid, idxpid;
112 char *tmppackpath = NULL, *tmpidxpath = NULL;
113 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
114 const char *repo_path = NULL;
115 struct got_pathlist_head have_refs;
116 struct got_pathlist_entry *pe;
117 struct got_reflist_head my_refs;
118 struct got_reflist_entry *re;
119 off_t packfile_size = 0;
120 struct got_packfile_hdr pack_hdr;
121 uint32_t nobj = 0;
122 char *path;
123 char *progress = NULL;
125 *pack_hash = NULL;
127 /*
128 * Prevent fetching of references that won't make any
129 * sense outside of the remote repository's context.
130 */
131 TAILQ_FOREACH(pe, wanted_refs, entry) {
132 const char *refname = pe->path;
133 if (strncmp(refname, "refs/got/", 9) == 0 ||
134 strncmp(refname, "got/", 4) == 0 ||
135 strncmp(refname, "refs/remotes/", 13) == 0 ||
136 strncmp(refname, "remotes/", 8) == 0)
137 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
140 if (!list_refs_only)
141 repo_path = got_repo_get_path_git_dir(repo);
143 for (i = 0; i < nitems(tmpfds); i++)
144 tmpfds[i] = -1;
146 TAILQ_INIT(&have_refs);
147 TAILQ_INIT(&my_refs);
149 if (!list_refs_only) {
150 err = got_ref_list(&my_refs, repo, NULL,
151 got_ref_cmp_by_name, NULL);
152 if (err)
153 goto done;
156 TAILQ_FOREACH(re, &my_refs, entry) {
157 struct got_object_id *id;
158 const char *refname;
160 if (got_ref_is_symbolic(re->ref))
161 continue;
163 err = got_ref_resolve(&id, repo, re->ref);
164 if (err)
165 goto done;
166 refname = strdup(got_ref_get_name(re->ref));
167 if (refname == NULL) {
168 err = got_error_from_errno("strdup");
169 goto done;
171 err = got_pathlist_append(&have_refs, refname, id);
172 if (err)
173 goto done;
176 if (list_refs_only) {
177 packfd = got_opentempfd();
178 if (packfd == -1) {
179 err = got_error_from_errno("got_opentempfd");
180 goto done;
182 } else {
183 if (asprintf(&path, "%s/%s/fetching.pack",
184 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
186 goto done;
188 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
189 free(path);
190 if (err)
191 goto done;
192 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
193 err = got_error_from_errno2("fchmod", tmppackpath);
194 goto done;
197 if (list_refs_only) {
198 idxfd = got_opentempfd();
199 if (idxfd == -1) {
200 err = got_error_from_errno("got_opentempfd");
201 goto done;
203 } else {
204 if (asprintf(&path, "%s/%s/fetching.idx",
205 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
206 err = got_error_from_errno("asprintf");
207 goto done;
209 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
210 free(path);
211 if (err)
212 goto done;
213 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
214 err = got_error_from_errno2("fchmod", tmpidxpath);
215 goto done;
218 nidxfd = dup(idxfd);
219 if (nidxfd == -1) {
220 err = got_error_from_errno("dup");
221 goto done;
224 for (i = 0; i < nitems(tmpfds); i++) {
225 tmpfds[i] = got_opentempfd();
226 if (tmpfds[i] == -1) {
227 err = got_error_from_errno("got_opentempfd");
228 goto done;
232 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
233 err = got_error_from_errno("socketpair");
234 goto done;
237 fetchpid = fork();
238 if (fetchpid == -1) {
239 err = got_error_from_errno("fork");
240 goto done;
241 } else if (fetchpid == 0){
242 got_privsep_exec_child(imsg_fetchfds,
243 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
246 if (close(imsg_fetchfds[1]) == -1) {
247 err = got_error_from_errno("close");
248 goto done;
250 imsg_init(&fetchibuf, imsg_fetchfds[0]);
251 nfetchfd = dup(fetchfd);
252 if (nfetchfd == -1) {
253 err = got_error_from_errno("dup");
254 goto done;
256 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
257 fetch_all_branches, wanted_branches, wanted_refs,
258 list_refs_only, verbosity);
259 if (err != NULL)
260 goto done;
261 nfetchfd = -1;
262 npackfd = dup(packfd);
263 if (npackfd == -1) {
264 err = got_error_from_errno("dup");
265 goto done;
267 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
268 if (err != NULL)
269 goto done;
270 npackfd = -1;
272 packfile_size = 0;
273 progress = calloc(GOT_PKT_MAX, 1);
274 if (progress == NULL) {
275 err = got_error_from_errno("calloc");
276 goto done;
279 *pack_hash = calloc(1, sizeof(**pack_hash));
280 if (*pack_hash == NULL) {
281 err = got_error_from_errno("calloc");
282 goto done;
285 while (!done) {
286 struct got_object_id *id = NULL;
287 char *refname = NULL;
288 char *server_progress = NULL;
289 off_t packfile_size_cur = 0;
291 err = got_privsep_recv_fetch_progress(&done,
292 &id, &refname, symrefs, &server_progress,
293 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
294 if (err != NULL)
295 goto done;
296 /* Don't report size progress for an empty pack file. */
297 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
298 packfile_size_cur = 0;
299 if (!done && refname && id) {
300 err = got_pathlist_insert(NULL, refs, refname, id);
301 if (err)
302 goto done;
303 } else if (!done && server_progress) {
304 char *p;
305 /*
306 * XXX git-daemon tends to send batched output with
307 * lines spanning separate packets. Buffer progress
308 * output until we see a CR or LF to avoid giving
309 * partial lines of progress output to the callback.
310 */
311 if (strlcat(progress, server_progress,
312 GOT_PKT_MAX) >= GOT_PKT_MAX) {
313 progress[0] = '\0'; /* discard */
314 continue;
316 while ((p = strchr(progress, '\r')) != NULL ||
317 (p = strchr(progress, '\n')) != NULL) {
318 char *s;
319 size_t n;
320 char c = *p;
321 *p = '\0';
322 if (asprintf(&s, "%s%s", progress,
323 c == '\n' ? "\n" : "") == -1) {
324 err = got_error_from_errno("asprintf");
325 goto done;
327 err = progress_cb(progress_arg, s,
328 packfile_size_cur, 0, 0, 0, 0);
329 free(s);
330 if (err)
331 break;
332 n = strlen(progress);
333 if (n < GOT_PKT_MAX - 1) {
334 memmove(progress, &progress[n + 1],
335 GOT_PKT_MAX - n - 1);
336 } else
337 progress[0] = '\0';
339 free(server_progress);
340 if (err)
341 goto done;
342 } else if (!done && packfile_size_cur != packfile_size) {
343 err = progress_cb(progress_arg, NULL,
344 packfile_size_cur, 0, 0, 0, 0);
345 if (err)
346 break;
347 packfile_size = packfile_size_cur;
350 if (close(imsg_fetchfds[0]) == -1) {
351 err = got_error_from_errno("close");
352 goto done;
354 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
355 err = got_error_from_errno("waitpid");
356 goto done;
359 if (lseek(packfd, 0, SEEK_SET) == -1) {
360 err = got_error_from_errno("lseek");
361 goto done;
364 /* If zero data was fetched without error we are already up-to-date. */
365 if (packfile_size == 0) {
366 free(*pack_hash);
367 *pack_hash = NULL;
368 goto done;
369 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
370 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
371 goto done;
372 } else {
373 ssize_t n;
375 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
376 if (n == -1) {
377 err = got_error_from_errno("read");
378 goto done;
380 if (n != ssizeof(pack_hdr)) {
381 err = got_error(GOT_ERR_IO);
382 goto done;
384 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
385 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
386 "bad pack file signature");
387 goto done;
389 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
390 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
391 "bad pack file version");
392 goto done;
394 nobj = be32toh(pack_hdr.nobjects);
395 if (nobj == 0 &&
396 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
397 return got_error_msg(GOT_ERR_BAD_PACKFILE,
398 "bad pack file with zero objects");
399 if (nobj != 0 &&
400 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
401 return got_error_msg(GOT_ERR_BAD_PACKFILE,
402 "empty pack file with non-zero object count");
405 /*
406 * If the pack file contains no objects, we may only need to update
407 * references in our repository. The caller will take care of that.
408 */
409 if (nobj == 0) {
410 free(*pack_hash);
411 *pack_hash = NULL;
412 goto done;
415 if (lseek(packfd, 0, SEEK_SET) == -1) {
416 err = got_error_from_errno("lseek");
417 goto done;
420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
421 err = got_error_from_errno("socketpair");
422 goto done;
424 idxpid = fork();
425 if (idxpid == -1) {
426 err= got_error_from_errno("fork");
427 goto done;
428 } else if (idxpid == 0)
429 got_privsep_exec_child(imsg_idxfds,
430 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
431 if (close(imsg_idxfds[1]) == -1) {
432 err = got_error_from_errno("close");
433 goto done;
435 imsg_init(&idxibuf, imsg_idxfds[0]);
437 npackfd = dup(packfd);
438 if (npackfd == -1) {
439 err = got_error_from_errno("dup");
440 goto done;
442 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
443 npackfd);
444 if (err != NULL)
445 goto done;
446 npackfd = -1;
447 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
448 if (err != NULL)
449 goto done;
450 nidxfd = -1;
451 for (i = 0; i < nitems(tmpfds); i++) {
452 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
453 if (err != NULL)
454 goto done;
455 tmpfds[i] = -1;
457 done = 0;
458 while (!done) {
459 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
461 err = got_privsep_recv_index_progress(&done, &nobj_total,
462 &nobj_indexed, &nobj_loose, &nobj_resolved,
463 &idxibuf);
464 if (err != NULL)
465 goto done;
466 if (nobj_indexed != 0) {
467 err = progress_cb(progress_arg, NULL,
468 packfile_size, nobj_total,
469 nobj_indexed, nobj_loose, nobj_resolved);
470 if (err)
471 break;
474 if (close(imsg_idxfds[0]) == -1) {
475 err = got_error_from_errno("close");
476 goto done;
478 if (waitpid(idxpid, &idxstatus, 0) == -1) {
479 err = got_error_from_errno("waitpid");
480 goto done;
483 err = got_object_id_str(&id_str, *pack_hash);
484 if (err)
485 goto done;
486 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
487 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
488 err = got_error_from_errno("asprintf");
489 goto done;
492 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
493 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
494 err = got_error_from_errno("asprintf");
495 goto done;
498 if (rename(tmppackpath, packpath) == -1) {
499 err = got_error_from_errno3("rename", tmppackpath, packpath);
500 goto done;
502 free(tmppackpath);
503 tmppackpath = NULL;
504 if (rename(tmpidxpath, idxpath) == -1) {
505 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
506 goto done;
508 free(tmpidxpath);
509 tmpidxpath = NULL;
511 done:
512 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
513 err = got_error_from_errno2("unlink", tmppackpath);
514 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
515 err = got_error_from_errno2("unlink", tmpidxpath);
516 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
517 err = got_error_from_errno("close");
518 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
519 err = got_error_from_errno("close");
520 if (packfd != -1 && close(packfd) == -1 && err == NULL)
521 err = got_error_from_errno("close");
522 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
523 err = got_error_from_errno("close");
524 for (i = 0; i < nitems(tmpfds); i++) {
525 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
526 err = got_error_from_errno("close");
528 free(tmppackpath);
529 free(tmpidxpath);
530 free(idxpath);
531 free(packpath);
532 free(progress);
534 TAILQ_FOREACH(pe, &have_refs, entry) {
535 free((char *)pe->path);
536 free(pe->data);
538 got_pathlist_free(&have_refs);
539 got_ref_list_free(&my_refs);
540 if (err) {
541 free(*pack_hash);
542 *pack_hash = NULL;
543 TAILQ_FOREACH(pe, refs, entry) {
544 free((void *)pe->path);
545 free(pe->data);
547 got_pathlist_free(refs);
548 TAILQ_FOREACH(pe, symrefs, entry) {
549 free((void *)pe->path);
550 free(pe->data);
552 got_pathlist_free(symrefs);
554 return err;