Blob


1 /*
2 * Copyright (c) 2023 Omar Polo <op@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/queue.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/tree.h>
22 #include <sys/types.h>
23 #include <sys/uio.h>
24 #include <sys/wait.h>
26 #include <endian.h>
27 #include <limits.h>
28 #include <sha1.h>
29 #include <sha2.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <imsg.h>
37 #include "got_error.h"
38 #include "got_cancel.h"
39 #include "got_object.h"
40 #include "got_opentemp.h"
41 #include "got_path.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"
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 #ifndef ssizeof
62 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
63 #endif
65 static const struct got_error *
66 temp_file(int *fd, char **path, const char *ext, struct got_repository *repo)
67 {
68 const struct got_error *err;
69 char p[PATH_MAX];
70 int r;
72 *path = NULL;
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);
80 if (err)
81 return err;
83 if (fchmod(*fd, GOT_DEFAULT_FILE_MODE) == -1)
84 return got_error_from_errno("fchmod");
86 return NULL;
87 }
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)
93 {
94 const struct got_error *err;
95 int elapsed;
97 if (progress_cb == NULL)
98 return NULL;
100 err = got_ratelimit_check(&elapsed, rl);
101 if (err || !elapsed)
102 return err;
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;
120 *tot = 0;
121 got_hash_init(&hash, GOT_HASH_SHA1);
123 for (;;) {
124 err = cancel_cb(cancel_arg);
125 if (err)
126 return err;
128 r = fread(buf, 1, sizeof(buf), in);
129 if (r == 0)
130 break;
132 /*
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
142 * we've reached EOF.
143 */
145 if (r >= sizeof(sha1buf)) {
146 *tot += sha1buflen;
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);
155 *tot += r;
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);
162 if (err)
163 return err;
165 continue;
168 if (sha1buflen == 0)
169 return got_error(GOT_ERR_BAD_PACKFILE);
171 /* short read, we've reached EOF */
172 *tot += r;
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);
179 break;
182 if (sha1buflen == 0)
183 return got_error(GOT_ERR_BAD_PACKFILE);
185 got_hash_final_object_id(&hash, id);
187 /* XXX SHA256 */
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);
200 if (err)
201 return err;
203 return NULL;
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;
223 char *line = NULL;
224 size_t linesize = 0;
225 ssize_t linelen;
226 size_t i;
227 ssize_t n;
228 off_t packsiz;
229 int tmpfds[3] = {-1, -1, -1};
230 int imsg_idxfds[2] = {-1, -1};
231 int ch, done, nobj, idxstatus;
232 pid_t idxpid;
234 got_ratelimit_init(&rl, 0, 500);
236 repo_path = got_repo_get_path_git_dir(repo);
238 linelen = getline(&line, &linesize, in);
239 if (linelen == -1) {
240 err = got_ferror(in, GOT_ERR_IO);
241 goto done;
244 if (strcmp(line, GIT_BUNDLE_SIGNATURE_V2) != 0) {
245 err = got_error(GOT_ERR_BUNDLE_FORMAT);
246 goto done;
249 /* Parse the prerequisite */
250 for (;;) {
251 ch = fgetc(in);
252 if (ch != '-') {
253 if (ch != EOF)
254 ungetc(ch, in);
255 break;
258 linelen = getline(&line, &linesize, in);
259 if (linelen == -1) {
260 err = got_ferror(in, GOT_ERR_IO);
261 goto done;
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);
269 goto done;
272 err = got_object_open(&obj, repo, &id);
273 if (err)
274 goto done;
275 got_object_close(obj);
278 /* Read references */
279 for (;;) {
280 struct got_object_id *id;
281 char *dup;
283 linelen = getline(&line, &linesize, in);
284 if (linelen == -1) {
285 err = got_ferror(in, GOT_ERR_IO);
286 goto done;
288 if (line[linelen - 1] == '\n')
289 line[linelen - 1] = '\0';
290 if (*line == '\0')
291 break;
293 spc = strchr(line, ' ');
294 if (spc == NULL) {
295 err = got_error(GOT_ERR_IO);
296 goto done;
298 *spc = '\0';
300 refname = spc + 1;
301 if (!got_ref_name_is_valid(refname)) {
302 err = got_error(GOT_ERR_BAD_REF_DATA);
303 goto done;
306 id = malloc(sizeof(*id));
307 if (id == NULL) {
308 err = got_error_from_errno("malloc");
309 goto done;
312 if (!got_parse_object_id(id, line, GOT_HASH_SHA1)) {
313 free(id);
314 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
315 goto done;
318 dup = strdup(refname);
319 if (dup == NULL) {
320 free(id);
321 err = got_error_from_errno("strdup");
322 goto done;
325 err = got_pathlist_append(refs_found, dup, id);
326 if (err) {
327 free(id);
328 free(dup);
329 goto done;
333 if (list_refs_only)
334 goto done;
336 err = temp_file(&packfd, &tmppackpath, ".pack", repo);
337 if (err)
338 goto done;
340 err = temp_file(&idxfd, &tmpidxpath, ".idx", repo);
341 if (err)
342 goto done;
344 err = copypack(in, packfd, &packsiz, &id, &rl,
345 progress_cb, progress_arg, cancel_cb, cancel_arg);
346 if (err)
347 goto done;
349 if (lseek(packfd, 0, SEEK_SET) == -1) {
350 err = got_error_from_errno("lseek");
351 goto done;
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");
357 goto done;
360 n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
361 if (n == -1) {
362 err = got_error_from_errno("read");
363 goto done;
365 if (n != ssizeof(pack_hdr)) {
366 err = got_error(GOT_ERR_IO);
367 goto done;
369 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
370 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
371 "bad pack file signature");
372 goto done;
374 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
375 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
376 "bad pack file version");
377 goto done;
379 nobj = be32toh(pack_hdr.nobjects);
380 if (nobj == 0 &&
381 packsiz > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
382 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
383 "bad pack file with zero objects");
384 goto done;
386 if (nobj != 0 &&
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");
390 goto done;
393 /* nothing to do if there are no objects. */
394 if (nobj == 0)
395 goto done;
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");
401 goto done;
405 if (lseek(packfd, 0, SEEK_SET) == -1) {
406 err = got_error_from_errno("lseek");
407 goto done;
410 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
411 err = got_error_from_errno("socketpair");
412 goto done;
414 idxpid = fork();
415 if (idxpid == -1) {
416 err= got_error_from_errno("fork");
417 goto done;
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");
423 goto done;
425 imsg_idxfds[1] = -1;
426 imsg_init(&idxibuf, imsg_idxfds[0]);
428 err = got_privsep_send_index_pack_req(&idxibuf, id.sha1, packfd);
429 if (err)
430 goto done;
431 packfd = -1;
433 err = got_privsep_send_index_pack_outfd(&idxibuf, idxfd);
434 if (err)
435 goto done;
436 idxfd = -1;
438 for (i = 0; i < nitems(tmpfds); i++) {
439 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
440 if (err != NULL)
441 goto done;
442 tmpfds[i] = -1;
445 done = 0;
446 while (!done) {
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);
451 if (err)
452 goto done;
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);
457 if (err)
458 goto done;
461 if (close(imsg_idxfds[0]) == -1) {
462 err = got_error_from_errno("close");
463 goto done;
465 imsg_idxfds[0] = -1;
466 if (waitpid(idxpid, &idxstatus, 0) == -1) {
467 err = got_error_from_errno("waitpid");
468 goto done;
471 if (noop)
472 goto done;
474 err = got_object_id_str(&id_str, &id);
475 if (err)
476 goto done;
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");
481 goto done;
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");
487 goto done;
490 if (rename(tmppackpath, packpath) == -1) {
491 err = got_error_from_errno3("rename", tmppackpath, packpath);
492 goto done;
494 free(tmppackpath);
495 tmppackpath = NULL;
497 if (rename(tmpidxpath, idxpath) == -1) {
498 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
499 goto done;
501 free(tmpidxpath);
502 tmpidxpath = NULL;
504 done:
505 free(line);
506 free(packpath);
507 free(idxpath);
508 free(id_str);
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");
514 free(tmppackpath);
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");
520 free(tmpidxpath);
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");
531 return err;