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>
37 #include <uuid.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,
103 got_fetch_progress_cb progress_cb, void *progress_arg)
105 size_t i;
106 int imsg_fetchfds[2], imsg_idxfds[2];
107 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
108 int tmpfds[3];
109 int fetchstatus, idxstatus, done = 0;
110 const struct got_error *err;
111 struct imsgbuf fetchibuf, idxibuf;
112 pid_t fetchpid, idxpid;
113 char *tmppackpath = NULL, *tmpidxpath = NULL;
114 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
115 const char *repo_path = NULL;
116 struct got_pathlist_head have_refs;
117 struct got_pathlist_entry *pe;
118 struct got_reflist_head my_refs;
119 struct got_reflist_entry *re;
120 off_t packfile_size = 0;
121 struct got_packfile_hdr pack_hdr;
122 uint32_t nobj = 0;
123 char *path;
124 char *progress = NULL;
126 *pack_hash = NULL;
128 /*
129 * Prevent fetching of references that won't make any
130 * sense outside of the remote repository's context.
131 */
132 TAILQ_FOREACH(pe, wanted_refs, entry) {
133 const char *refname = pe->path;
134 if (strncmp(refname, "refs/got/", 9) == 0 ||
135 strncmp(refname, "got/", 4) == 0 ||
136 strncmp(refname, "refs/remotes/", 13) == 0 ||
137 strncmp(refname, "remotes/", 8) == 0)
138 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
141 if (!list_refs_only)
142 repo_path = got_repo_get_path_git_dir(repo);
144 for (i = 0; i < nitems(tmpfds); i++)
145 tmpfds[i] = -1;
147 TAILQ_INIT(&have_refs);
148 TAILQ_INIT(&my_refs);
150 if (!list_refs_only) {
151 err = got_ref_list(&my_refs, repo, NULL,
152 got_ref_cmp_by_name, NULL);
153 if (err)
154 goto done;
157 TAILQ_FOREACH(re, &my_refs, entry) {
158 struct got_object_id *id;
159 const char *refname;
161 if (got_ref_is_symbolic(re->ref))
162 continue;
164 err = got_ref_resolve(&id, repo, re->ref);
165 if (err)
166 goto done;
167 refname = strdup(got_ref_get_name(re->ref));
168 if (refname == NULL) {
169 err = got_error_from_errno("strdup");
170 goto done;
172 err = got_pathlist_append(&have_refs, refname, id);
173 if (err)
174 goto done;
177 if (list_refs_only) {
178 packfd = got_opentempfd();
179 if (packfd == -1) {
180 err = got_error_from_errno("got_opentempfd");
181 goto done;
183 } else {
184 if (asprintf(&path, "%s/%s/fetching.pack",
185 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
186 err = got_error_from_errno("asprintf");
187 goto done;
189 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
190 free(path);
191 if (err)
192 goto done;
193 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
194 err = got_error_from_errno2("fchmod", tmppackpath);
195 goto done;
198 if (list_refs_only) {
199 idxfd = got_opentempfd();
200 if (idxfd == -1) {
201 err = got_error_from_errno("got_opentempfd");
202 goto done;
204 } else {
205 if (asprintf(&path, "%s/%s/fetching.idx",
206 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
207 err = got_error_from_errno("asprintf");
208 goto done;
210 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
211 free(path);
212 if (err)
213 goto done;
214 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
215 err = got_error_from_errno2("fchmod", tmpidxpath);
216 goto done;
219 nidxfd = dup(idxfd);
220 if (nidxfd == -1) {
221 err = got_error_from_errno("dup");
222 goto done;
225 for (i = 0; i < nitems(tmpfds); i++) {
226 tmpfds[i] = got_opentempfd();
227 if (tmpfds[i] == -1) {
228 err = got_error_from_errno("got_opentempfd");
229 goto done;
233 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
234 err = got_error_from_errno("socketpair");
235 goto done;
238 fetchpid = fork();
239 if (fetchpid == -1) {
240 err = got_error_from_errno("fork");
241 goto done;
242 } else if (fetchpid == 0){
243 got_privsep_exec_child(imsg_fetchfds,
244 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
247 if (close(imsg_fetchfds[1]) == -1) {
248 err = got_error_from_errno("close");
249 goto done;
251 imsg_init(&fetchibuf, imsg_fetchfds[0]);
252 nfetchfd = dup(fetchfd);
253 if (nfetchfd == -1) {
254 err = got_error_from_errno("dup");
255 goto done;
257 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
258 fetch_all_branches, wanted_branches, wanted_refs,
259 list_refs_only, verbosity);
260 if (err != NULL)
261 goto done;
262 nfetchfd = -1;
263 npackfd = dup(packfd);
264 if (npackfd == -1) {
265 err = got_error_from_errno("dup");
266 goto done;
268 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
269 if (err != NULL)
270 goto done;
271 npackfd = -1;
273 packfile_size = 0;
274 progress = calloc(GOT_PKT_MAX, 1);
275 if (progress == NULL) {
276 err = got_error_from_errno("calloc");
277 goto done;
280 *pack_hash = calloc(1, sizeof(**pack_hash));
281 if (*pack_hash == NULL) {
282 err = got_error_from_errno("calloc");
283 goto done;
286 while (!done) {
287 struct got_object_id *id = NULL;
288 char *refname = NULL;
289 char *server_progress = NULL;
290 off_t packfile_size_cur = 0;
292 err = got_privsep_recv_fetch_progress(&done,
293 &id, &refname, symrefs, &server_progress,
294 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
295 if (err != NULL)
296 goto done;
297 /* Don't report size progress for an empty pack file. */
298 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
299 packfile_size_cur = 0;
300 if (!done && refname && id) {
301 err = got_pathlist_insert(NULL, refs, refname, id);
302 if (err)
303 goto done;
304 } else if (!done && server_progress) {
305 char *p;
306 /*
307 * XXX git-daemon tends to send batched output with
308 * lines spanning separate packets. Buffer progress
309 * output until we see a CR or LF to avoid giving
310 * partial lines of progress output to the callback.
311 */
312 if (strlcat(progress, server_progress,
313 GOT_PKT_MAX) >= GOT_PKT_MAX) {
314 progress[0] = '\0'; /* discard */
315 continue;
317 while ((p = strchr(progress, '\r')) != NULL ||
318 (p = strchr(progress, '\n')) != NULL) {
319 char *s;
320 size_t n;
321 char c = *p;
322 *p = '\0';
323 if (asprintf(&s, "%s%s", progress,
324 c == '\n' ? "\n" : "") == -1) {
325 err = got_error_from_errno("asprintf");
326 goto done;
328 err = progress_cb(progress_arg, s,
329 packfile_size_cur, 0, 0, 0, 0);
330 free(s);
331 if (err)
332 break;
333 n = strlen(progress);
334 if (n < GOT_PKT_MAX - 1) {
335 memmove(progress, &progress[n + 1],
336 GOT_PKT_MAX - n - 1);
337 } else
338 progress[0] = '\0';
340 free(server_progress);
341 if (err)
342 goto done;
343 } else if (!done && packfile_size_cur != packfile_size) {
344 err = progress_cb(progress_arg, NULL,
345 packfile_size_cur, 0, 0, 0, 0);
346 if (err)
347 break;
348 packfile_size = packfile_size_cur;
351 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
352 err = got_error_from_errno("waitpid");
353 goto done;
356 if (lseek(packfd, 0, SEEK_SET) == -1) {
357 err = got_error_from_errno("lseek");
358 goto done;
361 /* If zero data was fetched without error we are already up-to-date. */
362 if (packfile_size == 0) {
363 free(*pack_hash);
364 *pack_hash = NULL;
365 goto done;
366 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
367 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
368 goto done;
369 } else {
370 ssize_t n;
372 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
373 if (n == -1) {
374 err = got_error_from_errno("read");
375 goto done;
377 if (n != ssizeof(pack_hdr)) {
378 err = got_error(GOT_ERR_IO);
379 goto done;
381 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
382 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
383 "bad pack file signature");
384 goto done;
386 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
387 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
388 "bad pack file version");
389 goto done;
391 nobj = be32toh(pack_hdr.nobjects);
392 if (nobj == 0 &&
393 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
394 return got_error_msg(GOT_ERR_BAD_PACKFILE,
395 "bad pack file with zero objects");
396 if (nobj != 0 &&
397 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
398 return got_error_msg(GOT_ERR_BAD_PACKFILE,
399 "empty pack file with non-zero object count");
402 /*
403 * If the pack file contains no objects, we may only need to update
404 * references in our repository. The caller will take care of that.
405 */
406 if (nobj == 0) {
407 free(*pack_hash);
408 *pack_hash = NULL;
409 goto done;
412 if (lseek(packfd, 0, SEEK_SET) == -1) {
413 err = got_error_from_errno("lseek");
414 goto done;
417 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
418 err = got_error_from_errno("socketpair");
419 goto done;
421 idxpid = fork();
422 if (idxpid == -1) {
423 err= got_error_from_errno("fork");
424 goto done;
425 } else if (idxpid == 0)
426 got_privsep_exec_child(imsg_idxfds,
427 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
428 if (close(imsg_idxfds[1]) == -1) {
429 err = got_error_from_errno("close");
430 goto done;
432 imsg_init(&idxibuf, imsg_idxfds[0]);
434 npackfd = dup(packfd);
435 if (npackfd == -1) {
436 err = got_error_from_errno("dup");
437 goto done;
439 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
440 npackfd);
441 if (err != NULL)
442 goto done;
443 npackfd = -1;
444 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
445 if (err != NULL)
446 goto done;
447 nidxfd = -1;
448 for (i = 0; i < nitems(tmpfds); i++) {
449 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
450 if (err != NULL)
451 goto done;
452 tmpfds[i] = -1;
454 done = 0;
455 while (!done) {
456 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
458 err = got_privsep_recv_index_progress(&done, &nobj_total,
459 &nobj_indexed, &nobj_loose, &nobj_resolved,
460 &idxibuf);
461 if (err != NULL)
462 goto done;
463 if (nobj_indexed != 0) {
464 err = progress_cb(progress_arg, NULL,
465 packfile_size, nobj_total,
466 nobj_indexed, nobj_loose, nobj_resolved);
467 if (err)
468 break;
470 imsg_clear(&idxibuf);
472 if (close(imsg_idxfds[0]) == -1) {
473 err = got_error_from_errno("close");
474 goto done;
476 if (waitpid(idxpid, &idxstatus, 0) == -1) {
477 err = got_error_from_errno("waitpid");
478 goto done;
481 err = got_object_id_str(&id_str, *pack_hash);
482 if (err)
483 goto done;
484 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
485 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
486 err = got_error_from_errno("asprintf");
487 goto done;
490 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
491 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
492 err = got_error_from_errno("asprintf");
493 goto done;
496 if (rename(tmppackpath, packpath) == -1) {
497 err = got_error_from_errno3("rename", tmppackpath, packpath);
498 goto done;
500 free(tmppackpath);
501 tmppackpath = NULL;
502 if (rename(tmpidxpath, idxpath) == -1) {
503 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
504 goto done;
506 free(tmpidxpath);
507 tmpidxpath = NULL;
509 done:
510 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
511 err = got_error_from_errno2("unlink", tmppackpath);
512 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
513 err = got_error_from_errno2("unlink", tmpidxpath);
514 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
515 err = got_error_from_errno("close");
516 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
517 err = got_error_from_errno("close");
518 if (packfd != -1 && close(packfd) == -1 && err == NULL)
519 err = got_error_from_errno("close");
520 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
521 err = got_error_from_errno("close");
522 for (i = 0; i < nitems(tmpfds); i++) {
523 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
524 err = got_error_from_errno("close");
526 free(tmppackpath);
527 free(tmpidxpath);
528 free(idxpath);
529 free(packpath);
530 free(progress);
532 TAILQ_FOREACH(pe, &have_refs, entry) {
533 free((char *)pe->path);
534 free(pe->data);
536 got_pathlist_free(&have_refs);
537 got_ref_list_free(&my_refs);
538 if (err) {
539 free(*pack_hash);
540 *pack_hash = NULL;
541 TAILQ_FOREACH(pe, refs, entry) {
542 free((void *)pe->path);
543 free(pe->data);
545 got_pathlist_free(refs);
546 TAILQ_FOREACH(pe, symrefs, entry) {
547 free((void *)pe->path);
548 free(pe->data);
550 got_pathlist_free(symrefs);
552 return err;