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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/socket.h>
24 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
42 #include "got_opentemp.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_repository_load.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_hash.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_ratelimit.h"
54 #include "got_lib_repository.h"
55 #include "got_lib_privsep.h"
57 #define GIT_BUNDLE_SIGNATURE_V2 "# v2 git bundle\n"
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
67 static const struct got_error *
68 temp_file(int *fd, char **path, const char *ext, struct got_repository *repo)
70 const struct got_error *err;
76 r = snprintf(p, sizeof(p), "%s/%s/loading",
77 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR);
78 if (r < 0 || (size_t)r >= sizeof(p))
79 return got_error_from_errno("snprintf");
81 err = got_opentemp_named_fd(path, fd, p, ext);
85 if (fchmod(*fd, GOT_DEFAULT_FILE_MODE) == -1)
86 return got_error_from_errno("fchmod");
91 static const struct got_error *
92 load_report_progress(got_load_progress_cb progress_cb, void *progress_arg,
93 struct got_ratelimit *rl, off_t packsiz, int nobj_total,
94 int nobj_indexed, int nobj_loose, int nobj_resolved)
96 const struct got_error *err;
99 if (progress_cb == NULL)
102 err = got_ratelimit_check(&elapsed, rl);
106 return progress_cb(progress_arg, packsiz, nobj_total, nobj_indexed,
107 nobj_loose, nobj_resolved);
110 static const struct got_error *
111 copypack(FILE *in, int outfd, off_t *tot,
112 struct got_object_id *id, struct got_ratelimit *rl,
113 got_load_progress_cb progress_cb, void *progress_arg,
114 got_cancel_cb cancel_cb, void *cancel_arg)
116 const struct got_error *err;
117 struct got_hash hash;
118 struct got_object_id expected_id;
119 char buf[BUFSIZ], sha1buf[SHA1_DIGEST_LENGTH];
120 size_t r, sha1buflen = 0;
123 got_hash_init(&hash, GOT_HASH_SHA1);
126 err = cancel_cb(cancel_arg);
130 r = fread(buf, 1, sizeof(buf), in);
135 * An expected SHA1 checksum sits at the end of the
136 * pack file. Since we don't know the file size ahead
137 * of time we have to keep SHA1_DIGEST_LENGTH bytes
138 * buffered and avoid mixing those bytes int our hash
139 * computation until we know for sure that additional
140 * pack file data bytes follow.
142 * We can assume that BUFSIZE is greater than
143 * SHA1_DIGEST_LENGTH and that a short read means that
147 if (r >= sizeof(sha1buf)) {
149 got_hash_update(&hash, sha1buf, sha1buflen);
150 if (write(outfd, sha1buf, sha1buflen) == -1)
151 return got_error_from_errno("write");
153 r -= sizeof(sha1buf);
154 memcpy(sha1buf, &buf[r], sizeof(sha1buf));
155 sha1buflen = sizeof(sha1buf);
158 got_hash_update(&hash, buf, r);
159 if (write(outfd, buf, r) == -1)
160 return got_error_from_errno("write");
162 err = load_report_progress(progress_cb, progress_arg,
163 rl, *tot, 0, 0, 0, 0);
171 return got_error(GOT_ERR_BAD_PACKFILE);
173 /* short read, we've reached EOF */
175 got_hash_update(&hash, sha1buf, r);
176 if (write(outfd, sha1buf, r) == -1)
177 return got_error_from_errno("write");
179 memmove(&sha1buf[0], &sha1buf[r], sizeof(sha1buf) - r);
180 memcpy(&sha1buf[sizeof(sha1buf) - r], buf, r);
185 return got_error(GOT_ERR_BAD_PACKFILE);
187 got_hash_final_object_id(&hash, id);
190 memset(&expected_id, 0, sizeof(expected_id));
191 memcpy(&expected_id.sha1, sha1buf, sizeof(expected_id.sha1));
193 if (got_object_id_cmp(id, &expected_id) != 0)
194 return got_error(GOT_ERR_PACKIDX_CSUM);
196 /* re-add the expected hash at the end of the pack */
197 if (write(outfd, sha1buf, sizeof(sha1buf)) == -1)
198 return got_error_from_errno("write");
200 *tot += sizeof(sha1buf);
201 err = progress_cb(progress_arg, *tot, 0, 0, 0, 0);
208 const struct got_error *
209 got_repo_load(FILE *in, struct got_pathlist_head *refs_found,
210 struct got_repository *repo, int list_refs_only, int noop,
211 got_load_progress_cb progress_cb, void *progress_arg,
212 got_cancel_cb cancel_cb, void *cancel_arg)
214 const struct got_error *err = NULL;
215 struct got_object_id id;
216 struct got_object *obj;
217 struct got_packfile_hdr pack_hdr;
218 struct got_ratelimit rl;
219 struct imsgbuf idxibuf;
220 const char *repo_path;
221 char *packpath = NULL, *idxpath = NULL;
222 char *tmppackpath = NULL, *tmpidxpath = NULL;
223 int packfd = -1, idxfd = -1;
224 char *spc, *refname, *id_str = NULL;
231 int tmpfds[3] = {-1, -1, -1};
232 int imsg_idxfds[2] = {-1, -1};
233 int ch, done, nobj, idxstatus;
236 got_ratelimit_init(&rl, 0, 500);
238 repo_path = got_repo_get_path_git_dir(repo);
240 linelen = getline(&line, &linesize, in);
242 err = got_ferror(in, GOT_ERR_IO);
246 if (strcmp(line, GIT_BUNDLE_SIGNATURE_V2) != 0) {
247 err = got_error(GOT_ERR_BUNDLE_FORMAT);
251 /* Parse the prerequisite */
260 linelen = getline(&line, &linesize, in);
262 err = got_ferror(in, GOT_ERR_IO);
266 if (line[linelen - 1] == '\n')
267 line[linelen - 1] = '\0';
269 if (!got_parse_object_id(&id, line, GOT_HASH_SHA1)) {
270 err = got_error_path(line, GOT_ERR_BAD_OBJ_ID_STR);
274 err = got_object_open(&obj, repo, &id);
277 got_object_close(obj);
280 /* Read references */
282 struct got_object_id *id;
285 linelen = getline(&line, &linesize, in);
287 err = got_ferror(in, GOT_ERR_IO);
290 if (line[linelen - 1] == '\n')
291 line[linelen - 1] = '\0';
295 spc = strchr(line, ' ');
297 err = got_error(GOT_ERR_IO);
303 if (!got_ref_name_is_valid(refname)) {
304 err = got_error(GOT_ERR_BAD_REF_DATA);
308 id = malloc(sizeof(*id));
310 err = got_error_from_errno("malloc");
314 if (!got_parse_object_id(id, line, GOT_HASH_SHA1)) {
316 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
320 dup = strdup(refname);
323 err = got_error_from_errno("strdup");
327 err = got_pathlist_append(refs_found, dup, id);
338 err = temp_file(&packfd, &tmppackpath, ".pack", repo);
342 err = temp_file(&idxfd, &tmpidxpath, ".idx", repo);
346 err = copypack(in, packfd, &packsiz, &id, &rl,
347 progress_cb, progress_arg, cancel_cb, cancel_arg);
351 if (lseek(packfd, 0, SEEK_SET) == -1) {
352 err = got_error_from_errno("lseek");
356 /* Safety checks on the pack' content. */
357 if (packsiz <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
358 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
362 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
364 err = got_error_from_errno("read");
367 if (n != ssizeof(pack_hdr)) {
368 err = got_error(GOT_ERR_IO);
371 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
372 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
373 "bad pack file signature");
376 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
377 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
378 "bad pack file version");
381 nobj = be32toh(pack_hdr.nobjects);
383 packsiz > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
384 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
385 "bad pack file with zero objects");
389 packsiz <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
390 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
391 "empty pack file with non-zero object count");
395 /* nothing to do if there are no objects. */
399 for (i = 0; i < nitems(tmpfds); i++) {
400 tmpfds[i] = got_opentempfd();
401 if (tmpfds[i] == -1) {
402 err = got_error_from_errno("got_opentempfd");
407 if (lseek(packfd, 0, SEEK_SET) == -1) {
408 err = got_error_from_errno("lseek");
412 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
413 err = got_error_from_errno("socketpair");
418 err= got_error_from_errno("fork");
420 } else if (idxpid == 0)
421 got_privsep_exec_child(imsg_idxfds,
422 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
423 if (close(imsg_idxfds[1]) == -1) {
424 err = got_error_from_errno("close");
428 imsg_init(&idxibuf, imsg_idxfds[0]);
430 err = got_privsep_send_index_pack_req(&idxibuf, id.sha1, packfd);
435 err = got_privsep_send_index_pack_outfd(&idxibuf, idxfd);
440 for (i = 0; i < nitems(tmpfds); i++) {
441 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
449 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
451 err = got_privsep_recv_index_progress(&done, &nobj_total,
452 &nobj_indexed, &nobj_loose, &nobj_resolved, &idxibuf);
455 if (nobj_indexed != 0) {
456 err = load_report_progress(progress_cb, progress_arg,
457 &rl, packsiz, nobj_total, nobj_indexed,
458 nobj_loose, nobj_resolved);
463 if (close(imsg_idxfds[0]) == -1) {
464 err = got_error_from_errno("close");
468 if (waitpid(idxpid, &idxstatus, 0) == -1) {
469 err = got_error_from_errno("waitpid");
476 err = got_object_id_str(&id_str, &id);
480 if (asprintf(&packpath, "%s/%s/pack-%s.pack", repo_path,
481 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
482 err = got_error_from_errno("asprintf");
486 if (asprintf(&idxpath, "%s/%s/pack-%s.idx", repo_path,
487 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
488 err = got_error_from_errno("asprintf");
492 if (rename(tmppackpath, packpath) == -1) {
493 err = got_error_from_errno3("rename", tmppackpath, packpath);
499 if (rename(tmpidxpath, idxpath) == -1) {
500 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
512 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
513 err = got_error_from_errno2("unlink", tmppackpath);
514 if (packfd != -1 && close(packfd) == -1 && err == NULL)
515 err = got_error_from_errno("close");
518 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
519 err = got_error_from_errno2("unlink", tmpidxpath);
520 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
521 err = got_error_from_errno("close");
524 if (imsg_idxfds[0] != -1 && close(imsg_idxfds[0]) == -1 && err == NULL)
525 err = got_error_from_errno("close");
526 if (imsg_idxfds[1] != -1 && close(imsg_idxfds[1]) == -1 && err == NULL)
527 err = got_error_from_errno("close");
529 for (i = 0; i < nitems(tmpfds); ++i)
530 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
531 err = got_error_from_errno("close");