cache raw objects in order to speed up gotadmin pack
[got-portable.git] / lib / fetch.c
blob2b173017176381035ce051fd5a542a032ce7ece7
1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
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 <errno.h>
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <time.h>
39 #include <uuid.h>
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
49 #include "got_fetch.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_dial.h"
62 #include "got_lib_pkt.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 #ifndef ssizeof
69 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
70 #endif
72 #ifndef MIN
73 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 #endif
76 const struct got_error *
77 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
78 const char *host, const char *port, const char *server_path, int verbosity)
80 const struct got_error *err = NULL;
82 *fetchpid = -1;
83 *fetchfd = -1;
85 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
86 err = got_dial_ssh(fetchpid, fetchfd, host, port,
87 server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
88 else if (strcmp(proto, "git") == 0)
89 err = got_dial_git(fetchfd, host, port, server_path,
90 GOT_DIAL_DIRECTION_FETCH);
91 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
92 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
93 else
94 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
95 return err;
98 const struct got_error*
99 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
100 struct got_pathlist_head *symrefs, const char *remote_name,
101 int mirror_references, int fetch_all_branches,
102 struct got_pathlist_head *wanted_branches,
103 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
104 int fetchfd, struct got_repository *repo,
105 got_fetch_progress_cb progress_cb, void *progress_arg)
107 size_t i;
108 int imsg_fetchfds[2], imsg_idxfds[2];
109 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
110 int tmpfds[3];
111 int fetchstatus, idxstatus, done = 0;
112 const struct got_error *err;
113 struct imsgbuf fetchibuf, idxibuf;
114 pid_t fetchpid, idxpid;
115 char *tmppackpath = NULL, *tmpidxpath = NULL;
116 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
117 const char *repo_path = NULL;
118 struct got_pathlist_head have_refs;
119 struct got_pathlist_entry *pe;
120 struct got_reflist_head my_refs;
121 struct got_reflist_entry *re;
122 off_t packfile_size = 0;
123 struct got_packfile_hdr pack_hdr;
124 uint32_t nobj = 0;
125 char *path;
126 char *progress = NULL;
128 *pack_hash = NULL;
131 * Prevent fetching of references that won't make any
132 * sense outside of the remote repository's context.
134 TAILQ_FOREACH(pe, wanted_refs, entry) {
135 const char *refname = pe->path;
136 if (strncmp(refname, "refs/got/", 9) == 0 ||
137 strncmp(refname, "got/", 4) == 0 ||
138 strncmp(refname, "refs/remotes/", 13) == 0 ||
139 strncmp(refname, "remotes/", 8) == 0)
140 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
143 if (!list_refs_only)
144 repo_path = got_repo_get_path_git_dir(repo);
146 for (i = 0; i < nitems(tmpfds); i++)
147 tmpfds[i] = -1;
149 TAILQ_INIT(&have_refs);
150 TAILQ_INIT(&my_refs);
152 if (!list_refs_only) {
153 err = got_ref_list(&my_refs, repo, NULL,
154 got_ref_cmp_by_name, NULL);
155 if (err)
156 goto done;
159 TAILQ_FOREACH(re, &my_refs, entry) {
160 struct got_object_id *id;
161 const char *refname;
163 if (got_ref_is_symbolic(re->ref))
164 continue;
166 err = got_ref_resolve(&id, repo, re->ref);
167 if (err)
168 goto done;
169 refname = strdup(got_ref_get_name(re->ref));
170 if (refname == NULL) {
171 err = got_error_from_errno("strdup");
172 goto done;
174 err = got_pathlist_append(&have_refs, refname, id);
175 if (err)
176 goto done;
179 if (list_refs_only) {
180 packfd = got_opentempfd();
181 if (packfd == -1) {
182 err = got_error_from_errno("got_opentempfd");
183 goto done;
185 } else {
186 if (asprintf(&path, "%s/%s/fetching.pack",
187 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
188 err = got_error_from_errno("asprintf");
189 goto done;
191 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
192 free(path);
193 if (err)
194 goto done;
195 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
196 err = got_error_from_errno2("fchmod", tmppackpath);
197 goto done;
200 if (list_refs_only) {
201 idxfd = got_opentempfd();
202 if (idxfd == -1) {
203 err = got_error_from_errno("got_opentempfd");
204 goto done;
206 } else {
207 if (asprintf(&path, "%s/%s/fetching.idx",
208 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
209 err = got_error_from_errno("asprintf");
210 goto done;
212 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
213 free(path);
214 if (err)
215 goto done;
216 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
217 err = got_error_from_errno2("fchmod", tmpidxpath);
218 goto done;
221 nidxfd = dup(idxfd);
222 if (nidxfd == -1) {
223 err = got_error_from_errno("dup");
224 goto done;
227 for (i = 0; i < nitems(tmpfds); i++) {
228 tmpfds[i] = got_opentempfd();
229 if (tmpfds[i] == -1) {
230 err = got_error_from_errno("got_opentempfd");
231 goto done;
235 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
236 err = got_error_from_errno("socketpair");
237 goto done;
240 fetchpid = fork();
241 if (fetchpid == -1) {
242 err = got_error_from_errno("fork");
243 goto done;
244 } else if (fetchpid == 0){
245 got_privsep_exec_child(imsg_fetchfds,
246 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
249 if (close(imsg_fetchfds[1]) == -1) {
250 err = got_error_from_errno("close");
251 goto done;
253 imsg_init(&fetchibuf, imsg_fetchfds[0]);
254 nfetchfd = dup(fetchfd);
255 if (nfetchfd == -1) {
256 err = got_error_from_errno("dup");
257 goto done;
259 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
260 fetch_all_branches, wanted_branches, wanted_refs,
261 list_refs_only, verbosity);
262 if (err != NULL)
263 goto done;
264 nfetchfd = -1;
265 npackfd = dup(packfd);
266 if (npackfd == -1) {
267 err = got_error_from_errno("dup");
268 goto done;
270 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
271 if (err != NULL)
272 goto done;
273 npackfd = -1;
275 packfile_size = 0;
276 progress = calloc(GOT_PKT_MAX, 1);
277 if (progress == NULL) {
278 err = got_error_from_errno("calloc");
279 goto done;
282 *pack_hash = calloc(1, sizeof(**pack_hash));
283 if (*pack_hash == NULL) {
284 err = got_error_from_errno("calloc");
285 goto done;
288 while (!done) {
289 struct got_object_id *id = NULL;
290 char *refname = NULL;
291 char *server_progress = NULL;
292 off_t packfile_size_cur = 0;
294 err = got_privsep_recv_fetch_progress(&done,
295 &id, &refname, symrefs, &server_progress,
296 &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
297 if (err != NULL)
298 goto done;
299 /* Don't report size progress for an empty pack file. */
300 if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
301 packfile_size_cur = 0;
302 if (!done && refname && id) {
303 err = got_pathlist_insert(NULL, refs, refname, id);
304 if (err)
305 goto done;
306 } else if (!done && server_progress) {
307 char *p;
309 * XXX git-daemon tends to send batched output with
310 * lines spanning separate packets. Buffer progress
311 * output until we see a CR or LF to avoid giving
312 * partial lines of progress output to the callback.
314 if (strlcat(progress, server_progress,
315 GOT_PKT_MAX) >= GOT_PKT_MAX) {
316 progress[0] = '\0'; /* discard */
317 continue;
319 while ((p = strchr(progress, '\r')) != NULL ||
320 (p = strchr(progress, '\n')) != NULL) {
321 char *s;
322 size_t n;
323 char c = *p;
324 *p = '\0';
325 if (asprintf(&s, "%s%s", progress,
326 c == '\n' ? "\n" : "") == -1) {
327 err = got_error_from_errno("asprintf");
328 goto done;
330 err = progress_cb(progress_arg, s,
331 packfile_size_cur, 0, 0, 0, 0);
332 free(s);
333 if (err)
334 break;
335 n = strlen(progress);
336 if (n < GOT_PKT_MAX - 1) {
337 memmove(progress, &progress[n + 1],
338 GOT_PKT_MAX - n - 1);
339 } else
340 progress[0] = '\0';
342 free(server_progress);
343 if (err)
344 goto done;
345 } else if (!done && packfile_size_cur != packfile_size) {
346 err = progress_cb(progress_arg, NULL,
347 packfile_size_cur, 0, 0, 0, 0);
348 if (err)
349 break;
350 packfile_size = packfile_size_cur;
353 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
354 err = got_error_from_errno("waitpid");
355 goto done;
358 if (lseek(packfd, 0, SEEK_SET) == -1) {
359 err = got_error_from_errno("lseek");
360 goto done;
363 /* If zero data was fetched without error we are already up-to-date. */
364 if (packfile_size == 0) {
365 free(*pack_hash);
366 *pack_hash = NULL;
367 goto done;
368 } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
369 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
370 goto done;
371 } else {
372 ssize_t n;
374 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
375 if (n == -1) {
376 err = got_error_from_errno("read");
377 goto done;
379 if (n != ssizeof(pack_hdr)) {
380 err = got_error(GOT_ERR_IO);
381 goto done;
383 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
384 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
385 "bad pack file signature");
386 goto done;
388 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
389 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
390 "bad pack file version");
391 goto done;
393 nobj = be32toh(pack_hdr.nobjects);
394 if (nobj == 0 &&
395 packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
396 return got_error_msg(GOT_ERR_BAD_PACKFILE,
397 "bad pack file with zero objects");
398 if (nobj != 0 &&
399 packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
400 return got_error_msg(GOT_ERR_BAD_PACKFILE,
401 "empty pack file with non-zero object count");
405 * If the pack file contains no objects, we may only need to update
406 * references in our repository. The caller will take care of that.
408 if (nobj == 0) {
409 free(*pack_hash);
410 *pack_hash = NULL;
411 goto done;
414 if (lseek(packfd, 0, SEEK_SET) == -1) {
415 err = got_error_from_errno("lseek");
416 goto done;
419 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
420 err = got_error_from_errno("socketpair");
421 goto done;
423 idxpid = fork();
424 if (idxpid == -1) {
425 err= got_error_from_errno("fork");
426 goto done;
427 } else if (idxpid == 0)
428 got_privsep_exec_child(imsg_idxfds,
429 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
430 if (close(imsg_idxfds[1]) == -1) {
431 err = got_error_from_errno("close");
432 goto done;
434 imsg_init(&idxibuf, imsg_idxfds[0]);
436 npackfd = dup(packfd);
437 if (npackfd == -1) {
438 err = got_error_from_errno("dup");
439 goto done;
441 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
442 npackfd);
443 if (err != NULL)
444 goto done;
445 npackfd = -1;
446 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
447 if (err != NULL)
448 goto done;
449 nidxfd = -1;
450 for (i = 0; i < nitems(tmpfds); i++) {
451 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
452 if (err != NULL)
453 goto done;
454 tmpfds[i] = -1;
456 done = 0;
457 while (!done) {
458 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
460 err = got_privsep_recv_index_progress(&done, &nobj_total,
461 &nobj_indexed, &nobj_loose, &nobj_resolved,
462 &idxibuf);
463 if (err != NULL)
464 goto done;
465 if (nobj_indexed != 0) {
466 err = progress_cb(progress_arg, NULL,
467 packfile_size, nobj_total,
468 nobj_indexed, nobj_loose, nobj_resolved);
469 if (err)
470 break;
472 imsg_clear(&idxibuf);
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;