2 * Copyright (c) 2023 Omar Polo <op@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/queue.h>
18 #include <sys/socket.h>
22 #include <sys/types.h>
37 #include "got_error.h"
38 #include "got_cancel.h"
39 #include "got_object.h"
40 #include "got_opentemp.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_repository_load.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_ratelimit.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_privsep.h"
55 #define GIT_BUNDLE_SIGNATURE_V2 "# v2 git bundle\n"
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
65 static const struct got_error *
66 temp_file(int *fd, char **path, const char *ext, struct got_repository *repo)
68 const struct got_error *err;
74 r = snprintf(p, sizeof(p), "%s/%s/loading",
75 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR);
76 if (r < 0 || (size_t)r >= sizeof(p))
77 return got_error_from_errno("snprintf");
79 err = got_opentemp_named_fd(path, fd, p, ext);
83 if (fchmod(*fd, GOT_DEFAULT_FILE_MODE) == -1)
84 return got_error_from_errno("fchmod");
89 static const struct got_error *
90 load_report_progress(got_load_progress_cb progress_cb, void *progress_arg,
91 struct got_ratelimit *rl, off_t packsiz, int nobj_total,
92 int nobj_indexed, int nobj_loose, int nobj_resolved)
94 const struct got_error *err;
97 if (progress_cb == NULL)
100 err = got_ratelimit_check(&elapsed, rl);
104 return progress_cb(progress_arg, packsiz, nobj_total, nobj_indexed,
105 nobj_loose, nobj_resolved);
108 static const struct got_error *
109 copypack(FILE *in, int outfd, off_t *tot,
110 struct got_object_id *id, struct got_ratelimit *rl,
111 got_load_progress_cb progress_cb, void *progress_arg,
112 got_cancel_cb cancel_cb, void *cancel_arg)
114 const struct got_error *err;
115 struct got_hash hash;
116 struct got_object_id expected_id;
117 char buf[BUFSIZ], sha1buf[SHA1_DIGEST_LENGTH];
118 size_t r, sha1buflen = 0;
121 got_hash_init(&hash, GOT_HASH_SHA1);
124 err = cancel_cb(cancel_arg);
128 r = fread(buf, 1, sizeof(buf), in);
133 * An expected SHA1 checksum sits at the end of the
134 * pack file. Since we don't know the file size ahead
135 * of time we have to keep SHA1_DIGEST_LENGTH bytes
136 * buffered and avoid mixing those bytes int our hash
137 * computation until we know for sure that additional
138 * pack file data bytes follow.
140 * We can assume that BUFSIZE is greater than
141 * SHA1_DIGEST_LENGTH and that a short read means that
145 if (r >= sizeof(sha1buf)) {
147 got_hash_update(&hash, sha1buf, sha1buflen);
148 if (write(outfd, sha1buf, sha1buflen) == -1)
149 return got_error_from_errno("write");
151 r -= sizeof(sha1buf);
152 memcpy(sha1buf, &buf[r], sizeof(sha1buf));
153 sha1buflen = sizeof(sha1buf);
156 got_hash_update(&hash, buf, r);
157 if (write(outfd, buf, r) == -1)
158 return got_error_from_errno("write");
160 err = load_report_progress(progress_cb, progress_arg,
161 rl, *tot, 0, 0, 0, 0);
169 return got_error(GOT_ERR_BAD_PACKFILE);
171 /* short read, we've reached EOF */
173 got_hash_update(&hash, sha1buf, r);
174 if (write(outfd, sha1buf, r) == -1)
175 return got_error_from_errno("write");
177 memmove(&sha1buf[0], &sha1buf[r], sizeof(sha1buf) - r);
178 memcpy(&sha1buf[sizeof(sha1buf) - r], buf, r);
183 return got_error(GOT_ERR_BAD_PACKFILE);
185 got_hash_final_object_id(&hash, id);
188 memset(&expected_id, 0, sizeof(expected_id));
189 memcpy(&expected_id.sha1, sha1buf, sizeof(expected_id.sha1));
191 if (got_object_id_cmp(id, &expected_id) != 0)
192 return got_error(GOT_ERR_PACKIDX_CSUM);
194 /* re-add the expected hash at the end of the pack */
195 if (write(outfd, sha1buf, sizeof(sha1buf)) == -1)
196 return got_error_from_errno("write");
198 *tot += sizeof(sha1buf);
199 err = progress_cb(progress_arg, *tot, 0, 0, 0, 0);
206 const struct got_error *
207 got_repo_load(FILE *in, struct got_pathlist_head *refs_found,
208 struct got_repository *repo, int list_refs_only, int noop,
209 got_load_progress_cb progress_cb, void *progress_arg,
210 got_cancel_cb cancel_cb, void *cancel_arg)
212 const struct got_error *err = NULL;
213 struct got_object_id id;
214 struct got_object *obj;
215 struct got_packfile_hdr pack_hdr;
216 struct got_ratelimit rl;
217 struct imsgbuf idxibuf;
218 const char *repo_path;
219 char *packpath = NULL, *idxpath = NULL;
220 char *tmppackpath = NULL, *tmpidxpath = NULL;
221 int packfd = -1, idxfd = -1;
222 char *spc, *refname, *id_str = NULL;
229 int tmpfds[3] = {-1, -1, -1};
230 int imsg_idxfds[2] = {-1, -1};
231 int ch, done, nobj, idxstatus;
234 got_ratelimit_init(&rl, 0, 500);
236 repo_path = got_repo_get_path_git_dir(repo);
238 linelen = getline(&line, &linesize, in);
240 err = got_ferror(in, GOT_ERR_IO);
244 if (strcmp(line, GIT_BUNDLE_SIGNATURE_V2) != 0) {
245 err = got_error(GOT_ERR_BUNDLE_FORMAT);
249 /* Parse the prerequisite */
258 linelen = getline(&line, &linesize, in);
260 err = got_ferror(in, GOT_ERR_IO);
264 if (line[linelen - 1] == '\n')
265 line[linelen - 1] = '\0';
267 if (!got_parse_object_id(&id, line, GOT_HASH_SHA1)) {
268 err = got_error_path(line, GOT_ERR_BAD_OBJ_ID_STR);
272 err = got_object_open(&obj, repo, &id);
275 got_object_close(obj);
278 /* Read references */
280 struct got_object_id *id;
283 linelen = getline(&line, &linesize, in);
285 err = got_ferror(in, GOT_ERR_IO);
288 if (line[linelen - 1] == '\n')
289 line[linelen - 1] = '\0';
293 spc = strchr(line, ' ');
295 err = got_error(GOT_ERR_IO);
301 if (!got_ref_name_is_valid(refname)) {
302 err = got_error(GOT_ERR_BAD_REF_DATA);
306 id = malloc(sizeof(*id));
308 err = got_error_from_errno("malloc");
312 if (!got_parse_object_id(id, line, GOT_HASH_SHA1)) {
314 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
318 dup = strdup(refname);
321 err = got_error_from_errno("strdup");
325 err = got_pathlist_append(refs_found, dup, id);
336 err = temp_file(&packfd, &tmppackpath, ".pack", repo);
340 err = temp_file(&idxfd, &tmpidxpath, ".idx", repo);
344 err = copypack(in, packfd, &packsiz, &id, &rl,
345 progress_cb, progress_arg, cancel_cb, cancel_arg);
349 if (lseek(packfd, 0, SEEK_SET) == -1) {
350 err = got_error_from_errno("lseek");
354 /* Safety checks on the pack' content. */
355 if (packsiz <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
356 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
360 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
362 err = got_error_from_errno("read");
365 if (n != ssizeof(pack_hdr)) {
366 err = got_error(GOT_ERR_IO);
369 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
370 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
371 "bad pack file signature");
374 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
375 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
376 "bad pack file version");
379 nobj = be32toh(pack_hdr.nobjects);
381 packsiz > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
382 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
383 "bad pack file with zero objects");
387 packsiz <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
388 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
389 "empty pack file with non-zero object count");
393 /* nothing to do if there are no objects. */
397 for (i = 0; i < nitems(tmpfds); i++) {
398 tmpfds[i] = got_opentempfd();
399 if (tmpfds[i] == -1) {
400 err = got_error_from_errno("got_opentempfd");
405 if (lseek(packfd, 0, SEEK_SET) == -1) {
406 err = got_error_from_errno("lseek");
410 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
411 err = got_error_from_errno("socketpair");
416 err= got_error_from_errno("fork");
418 } else if (idxpid == 0)
419 got_privsep_exec_child(imsg_idxfds,
420 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
421 if (close(imsg_idxfds[1]) == -1) {
422 err = got_error_from_errno("close");
426 imsg_init(&idxibuf, imsg_idxfds[0]);
428 err = got_privsep_send_index_pack_req(&idxibuf, id.sha1, packfd);
433 err = got_privsep_send_index_pack_outfd(&idxibuf, idxfd);
438 for (i = 0; i < nitems(tmpfds); i++) {
439 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
447 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
449 err = got_privsep_recv_index_progress(&done, &nobj_total,
450 &nobj_indexed, &nobj_loose, &nobj_resolved, &idxibuf);
453 if (nobj_indexed != 0) {
454 err = load_report_progress(progress_cb, progress_arg,
455 &rl, packsiz, nobj_total, nobj_indexed,
456 nobj_loose, nobj_resolved);
461 if (close(imsg_idxfds[0]) == -1) {
462 err = got_error_from_errno("close");
466 if (waitpid(idxpid, &idxstatus, 0) == -1) {
467 err = got_error_from_errno("waitpid");
474 err = got_object_id_str(&id_str, &id);
478 if (asprintf(&packpath, "%s/%s/pack-%s.pack", repo_path,
479 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
480 err = got_error_from_errno("asprintf");
484 if (asprintf(&idxpath, "%s/%s/pack-%s.idx", repo_path,
485 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
486 err = got_error_from_errno("asprintf");
490 if (rename(tmppackpath, packpath) == -1) {
491 err = got_error_from_errno3("rename", tmppackpath, packpath);
497 if (rename(tmpidxpath, idxpath) == -1) {
498 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
510 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
511 err = got_error_from_errno2("unlink", tmppackpath);
512 if (packfd != -1 && close(packfd) == -1 && err == NULL)
513 err = got_error_from_errno("close");
516 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
517 err = got_error_from_errno2("unlink", tmpidxpath);
518 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
519 err = got_error_from_errno("close");
522 if (imsg_idxfds[0] != -1 && close(imsg_idxfds[0]) == -1 && err == NULL)
523 err = got_error_from_errno("close");
524 if (imsg_idxfds[1] != -1 && close(imsg_idxfds[1]) == -1 && err == NULL)
525 err = got_error_from_errno("close");
527 for (i = 0; i < nitems(tmpfds); ++i)
528 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
529 err = got_error_from_errno("close");