Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <dirent.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdint.h>
29 #include <sha1.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.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_reference.h"
41 #include "got_repository.h"
42 #include "got_repository_admin.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_pack_create.h"
53 #include "got_lib_sha1.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static const struct got_error *
60 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
61 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
62 struct got_repository *repo,
63 got_cancel_cb cancel_cb, void *cancel_arg)
64 {
65 const struct got_error *err = NULL;
66 const size_t alloc_chunksz = 256;
67 size_t nalloc;
68 struct got_reflist_entry *re;
69 int i;
71 *ids = NULL;
72 *nobjects = 0;
74 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
75 if (*ids == NULL)
76 return got_error_from_errno("reallocarray");
77 nalloc = alloc_chunksz;
79 TAILQ_FOREACH(re, refs, entry) {
80 struct got_object_id *id;
82 if (cancel_cb) {
83 err = cancel_cb(cancel_arg);
84 if (err)
85 goto done;
86 }
88 err = got_ref_resolve(&id, repo, re->ref);
89 if (err)
90 goto done;
92 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
93 int obj_type;
94 err = got_object_get_type(&obj_type, repo, id);
95 if (err)
96 goto done;
97 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
98 free(id);
99 id = NULL;
100 continue;
104 if (nalloc >= *nobjects) {
105 struct got_object_id **new;
106 new = recallocarray(*ids, nalloc,
107 nalloc + alloc_chunksz,
108 sizeof(struct got_object_id *));
109 if (new == NULL) {
110 err = got_error_from_errno(
111 "recallocarray");
112 goto done;
114 *ids = new;
115 nalloc += alloc_chunksz;
117 (*ids)[*nobjects] = id;
118 if ((*ids)[*nobjects] == NULL) {
119 err = got_error_from_errno("got_object_id_dup");
120 goto done;
122 (*nobjects)++;
124 done:
125 if (err) {
126 for (i = 0; i < *nobjects; i++)
127 free((*ids)[i]);
128 free(*ids);
129 *ids = NULL;
130 *nobjects = 0;
132 return err;
135 const struct got_error *
136 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
137 struct got_reflist_head *include_refs,
138 struct got_reflist_head *exclude_refs, struct got_repository *repo,
139 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
140 got_cancel_cb cancel_cb, void *cancel_arg)
142 const struct got_error *err = NULL;
143 struct got_object_id **ours = NULL, **theirs = NULL;
144 int nours = 0, ntheirs = 0, packfd = -1, i;
145 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
146 char *sha1_str = NULL;
148 *packfile = NULL;
149 *pack_hash = NULL;
151 if (asprintf(&path, "%s/%s/packing.pack",
152 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
153 err = got_error_from_errno("asprintf");
154 goto done;
156 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
157 if (err)
158 goto done;
160 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
161 err = got_error_from_errno2("fchmod", tmpfile_path);
162 goto done;
165 *packfile = fdopen(packfd, "w");
166 if (*packfile == NULL) {
167 err = got_error_from_errno2("fdopen", tmpfile_path);
168 goto done;
170 packfd = -1;
172 err = get_reflist_object_ids(&ours, &nours,
173 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
174 include_refs, repo, cancel_cb, cancel_arg);
175 if (err)
176 goto done;
178 if (nours == 0) {
179 err = got_error(GOT_ERR_CANNOT_PACK);
180 goto done;
183 if (!TAILQ_EMPTY(exclude_refs)) {
184 err = get_reflist_object_ids(&theirs, &ntheirs,
185 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
186 exclude_refs, repo,
187 cancel_cb, cancel_arg);
188 if (err)
189 goto done;
192 *pack_hash = calloc(1, sizeof(**pack_hash));
193 if (*pack_hash == NULL) {
194 err = got_error_from_errno("calloc");
195 goto done;
198 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
199 ours, nours, repo, loose_obj_only, progress_cb, progress_arg,
200 cancel_cb, cancel_arg);
201 if (err)
202 goto done;
204 err = got_object_id_str(&sha1_str, *pack_hash);
205 if (err)
206 goto done;
207 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
208 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
209 sha1_str) == -1) {
210 err = got_error_from_errno("asprintf");
211 goto done;
214 if (fflush(*packfile) == -1) {
215 err = got_error_from_errno("fflush");
216 goto done;
218 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
219 err = got_error_from_errno("fseek");
220 goto done;
222 if (rename(tmpfile_path, packfile_path) == -1) {
223 err = got_error_from_errno3("rename", tmpfile_path,
224 packfile_path);
225 goto done;
227 free(tmpfile_path);
228 tmpfile_path = NULL;
229 done:
230 for (i = 0; i < nours; i++)
231 free(ours[i]);
232 free(ours);
233 for (i = 0; i < ntheirs; i++)
234 free(theirs[i]);
235 free(theirs);
236 if (packfd != -1 && close(packfd) == -1 && err == NULL)
237 err = got_error_from_errno2("close", packfile_path);
238 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
239 err = got_error_from_errno2("unlink", tmpfile_path);
240 free(tmpfile_path);
241 free(packfile_path);
242 free(sha1_str);
243 free(path);
244 if (err) {
245 free(*pack_hash);
246 *pack_hash = NULL;
247 if (*packfile)
248 fclose(*packfile);
249 *packfile = NULL;
251 return err;
254 const struct got_error *
255 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
256 struct got_repository *repo,
257 got_pack_index_progress_cb progress_cb, void *progress_arg,
258 got_cancel_cb cancel_cb, void *cancel_arg)
260 size_t i;
261 char *path;
262 int imsg_idxfds[2];
263 int npackfd = -1, idxfd = -1, nidxfd = -1;
264 int tmpfds[3];
265 int idxstatus, done = 0;
266 const struct got_error *err;
267 struct imsgbuf idxibuf;
268 pid_t idxpid;
269 char *tmpidxpath = NULL;
270 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
271 const char *repo_path = got_repo_get_path_git_dir(repo);
272 struct stat sb;
274 for (i = 0; i < nitems(tmpfds); i++)
275 tmpfds[i] = -1;
277 if (asprintf(&path, "%s/%s/indexing.idx",
278 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
279 err = got_error_from_errno("asprintf");
280 goto done;
282 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
283 free(path);
284 if (err)
285 goto done;
286 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
287 err = got_error_from_errno2("fchmod", tmpidxpath);
288 goto done;
291 nidxfd = dup(idxfd);
292 if (nidxfd == -1) {
293 err = got_error_from_errno("dup");
294 goto done;
297 for (i = 0; i < nitems(tmpfds); i++) {
298 tmpfds[i] = got_opentempfd();
299 if (tmpfds[i] == -1) {
300 err = got_error_from_errno("got_opentempfd");
301 goto done;
305 err = got_object_id_str(&id_str, pack_hash);
306 if (err)
307 goto done;
309 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
310 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
311 err = got_error_from_errno("asprintf");
312 goto done;
315 if (fstat(fileno(packfile), &sb) == -1) {
316 err = got_error_from_errno2("fstat", packfile_path);
317 goto done;
320 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
321 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
322 err = got_error_from_errno("asprintf");
323 goto done;
326 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
327 err = got_error_from_errno("socketpair");
328 goto done;
330 idxpid = fork();
331 if (idxpid == -1) {
332 err= got_error_from_errno("fork");
333 goto done;
334 } else if (idxpid == 0)
335 got_privsep_exec_child(imsg_idxfds,
336 GOT_PATH_PROG_INDEX_PACK, packfile_path);
337 if (close(imsg_idxfds[1]) == -1) {
338 err = got_error_from_errno("close");
339 goto done;
341 imsg_init(&idxibuf, imsg_idxfds[0]);
343 npackfd = dup(fileno(packfile));
344 if (npackfd == -1) {
345 err = got_error_from_errno("dup");
346 goto done;
348 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
349 npackfd);
350 if (err != NULL)
351 goto done;
352 npackfd = -1;
353 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
354 if (err != NULL)
355 goto done;
356 nidxfd = -1;
357 for (i = 0; i < nitems(tmpfds); i++) {
358 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
359 if (err != NULL)
360 goto done;
361 tmpfds[i] = -1;
363 done = 0;
364 while (!done) {
365 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
367 if (cancel_cb) {
368 err = cancel_cb(cancel_arg);
369 if (err)
370 goto done;
373 err = got_privsep_recv_index_progress(&done, &nobj_total,
374 &nobj_indexed, &nobj_loose, &nobj_resolved,
375 &idxibuf);
376 if (err != NULL)
377 goto done;
378 if (nobj_indexed != 0) {
379 err = progress_cb(progress_arg, sb.st_size,
380 nobj_total, nobj_indexed, nobj_loose,
381 nobj_resolved);
382 if (err)
383 break;
385 imsg_clear(&idxibuf);
387 if (close(imsg_idxfds[0]) == -1) {
388 err = got_error_from_errno("close");
389 goto done;
391 if (waitpid(idxpid, &idxstatus, 0) == -1) {
392 err = got_error_from_errno("waitpid");
393 goto done;
396 if (rename(tmpidxpath, idxpath) == -1) {
397 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
398 goto done;
400 free(tmpidxpath);
401 tmpidxpath = NULL;
403 done:
404 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
405 err = got_error_from_errno2("unlink", tmpidxpath);
406 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
407 err = got_error_from_errno("close");
408 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
409 err = got_error_from_errno("close");
410 for (i = 0; i < nitems(tmpfds); i++) {
411 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
412 err = got_error_from_errno("close");
414 free(tmpidxpath);
415 free(idxpath);
416 free(packfile_path);
417 return err;
420 const struct got_error *
421 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
422 struct got_repository *repo, const char *packfile_path)
424 const struct got_error *err = NULL;
425 const char *packdir_path = NULL;
426 char *packfile_name = NULL, *p, *dot;
427 struct got_object_id id;
428 int packfd = -1;
430 *packfile = NULL;
431 *pack_hash = NULL;
433 packdir_path = got_repo_get_path_objects_pack(repo);
434 if (packdir_path == NULL)
435 return got_error_from_errno("got_repo_get_path_objects_pack");
437 if (!got_path_is_child(packfile_path, packdir_path,
438 strlen(packdir_path))) {
439 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
440 goto done;
444 err = got_path_basename(&packfile_name, packfile_path);
445 if (err)
446 goto done;
447 p = packfile_name;
449 if (strncmp(p, "pack-", 5) != 0) {
450 err = got_error_fmt(GOT_ERR_BAD_PATH,
451 "'%s' is not a valid pack file name",
452 packfile_name);
453 goto done;
455 p += 5;
456 dot = strchr(p, '.');
457 if (dot == NULL) {
458 err = got_error_fmt(GOT_ERR_BAD_PATH,
459 "'%s' is not a valid pack file name",
460 packfile_name);
461 goto done;
463 if (strcmp(dot + 1, "pack") != 0) {
464 err = got_error_fmt(GOT_ERR_BAD_PATH,
465 "'%s' is not a valid pack file name",
466 packfile_name);
467 goto done;
469 *dot = '\0';
470 if (!got_parse_sha1_digest(id.sha1, p)) {
471 err = got_error_fmt(GOT_ERR_BAD_PATH,
472 "'%s' is not a valid pack file name",
473 packfile_name);
474 goto done;
477 *pack_hash = got_object_id_dup(&id);
478 if (*pack_hash == NULL) {
479 err = got_error_from_errno("got_object_id_dup");
480 goto done;
483 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW);
484 if (packfd == -1) {
485 err = got_error_from_errno2("open", packfile_path);
486 goto done;
489 *packfile = fdopen(packfd, "r");
490 if (*packfile == NULL) {
491 err = got_error_from_errno2("fdopen", packfile_path);
492 goto done;
494 packfd = -1;
495 done:
496 if (packfd != -1 && close(packfd) == -1 && err == NULL)
497 err = got_error_from_errno2("close", packfile_path);
498 free(packfile_name);
499 if (err) {
500 free(*pack_hash);
501 *pack_hash = NULL;
503 return err;
506 const struct got_error *
507 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
508 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
509 got_cancel_cb cancel_cb, void *cancel_arg)
511 const struct got_error *err = NULL;
512 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
513 struct got_packidx *packidx = NULL;
514 struct got_pack *pack = NULL;
515 uint32_t nobj, i;
517 err = got_object_id_str(&id_str, pack_hash);
518 if (err)
519 goto done;
521 if (asprintf(&packpath, "%s/pack-%s.pack",
522 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
523 err = got_error_from_errno("asprintf");
524 goto done;
526 if (asprintf(&idxpath, "%s/pack-%s.idx",
527 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
528 err = got_error_from_errno("asprintf");
529 goto done;
532 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
533 if (err)
534 goto done;
536 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
537 if (err)
538 goto done;
540 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
541 for (i = 0; i < nobj; i++) {
542 struct got_packidx_object_id *oid;
543 struct got_object_id id, base_id;
544 off_t offset, base_offset = 0;
545 uint8_t type;
546 uint64_t size;
547 size_t tslen, len;
549 if (cancel_cb) {
550 err = cancel_cb(cancel_arg);
551 if (err)
552 break;
554 oid = &packidx->hdr.sorted_ids[i];
555 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
557 offset = got_packidx_get_object_offset(packidx, i);
558 if (offset == -1) {
559 err = got_error(GOT_ERR_BAD_PACKIDX);
560 goto done;
563 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
564 pack, offset);
565 if (err)
566 goto done;
568 switch (type) {
569 case GOT_OBJ_TYPE_OFFSET_DELTA:
570 err = got_pack_parse_offset_delta(&base_offset, &len,
571 pack, offset, tslen);
572 if (err)
573 goto done;
574 break;
575 case GOT_OBJ_TYPE_REF_DELTA:
576 err = got_pack_parse_ref_delta(&base_id,
577 pack, offset, tslen);
578 if (err)
579 goto done;
580 break;
582 err = (*list_cb)(list_arg, &id, type, offset, size,
583 base_offset, &base_id);
584 if (err)
585 goto done;
588 done:
589 free(id_str);
590 free(idxpath);
591 free(packpath);
592 if (packidx)
593 got_packidx_close(packidx);
594 return err;