2 * Copyright (c) 2020 Stefan Sperling <stsp@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/queue.h>
21 #include <sys/socket.h>
36 #include "got_error.h"
37 #include "got_cancel.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_opentemp.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_repository.h"
51 #include "got_lib_pack_create.h"
52 #include "got_lib_sha1.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static const struct got_error *
59 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
60 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
61 struct got_repository *repo,
62 got_cancel_cb cancel_cb, void *cancel_arg)
64 const struct got_error *err = NULL;
65 const size_t alloc_chunksz = 256;
67 struct got_reflist_entry *re;
73 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
75 return got_error_from_errno("reallocarray");
76 nalloc = alloc_chunksz;
78 TAILQ_FOREACH(re, refs, entry) {
79 struct got_object_id *id;
82 err = cancel_cb(cancel_arg);
87 err = got_ref_resolve(&id, repo, re->ref);
91 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
93 err = got_object_get_type(&obj_type, repo, id);
96 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
103 if (nalloc >= *nobjects) {
104 struct got_object_id **new;
105 new = recallocarray(*ids, nalloc,
106 nalloc + alloc_chunksz,
107 sizeof(struct got_object_id *));
109 err = got_error_from_errno(
114 nalloc += alloc_chunksz;
116 (*ids)[*nobjects] = id;
117 if ((*ids)[*nobjects] == NULL) {
118 err = got_error_from_errno("got_object_id_dup");
125 for (i = 0; i < *nobjects; i++)
134 const struct got_error *
135 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
136 struct got_reflist_head *include_refs,
137 struct got_reflist_head *exclude_refs, struct got_repository *repo,
138 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
139 got_cancel_cb cancel_cb, void *cancel_arg)
141 const struct got_error *err = NULL;
142 struct got_object_id **ours = NULL, **theirs = NULL;
143 int nours = 0, ntheirs = 0, packfd = -1, i;
144 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
145 char *sha1_str = NULL;
150 if (asprintf(&path, "%s/%s/packing.pack",
151 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
152 err = got_error_from_errno("asprintf");
155 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
159 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
160 err = got_error_from_errno2("fchmod", tmpfile_path);
164 *packfile = fdopen(packfd, "w");
165 if (*packfile == NULL) {
166 err = got_error_from_errno2("fdopen", tmpfile_path);
171 err = get_reflist_object_ids(&ours, &nours,
172 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
173 include_refs, repo, cancel_cb, cancel_arg);
178 err = got_error(GOT_ERR_CANNOT_PACK);
182 if (!TAILQ_EMPTY(exclude_refs)) {
183 err = get_reflist_object_ids(&theirs, &ntheirs,
184 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
186 cancel_cb, cancel_arg);
191 *pack_hash = calloc(1, sizeof(**pack_hash));
192 if (*pack_hash == NULL) {
193 err = got_error_from_errno("calloc");
197 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
198 ours, nours, repo, loose_obj_only, progress_cb, progress_arg,
199 cancel_cb, cancel_arg);
203 err = got_object_id_str(&sha1_str, *pack_hash);
206 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
207 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
209 err = got_error_from_errno("asprintf");
213 if (fflush(*packfile) == -1) {
214 err = got_error_from_errno("fflush");
217 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
218 err = got_error_from_errno("fseek");
221 if (rename(tmpfile_path, packfile_path) == -1) {
222 err = got_error_from_errno3("rename", tmpfile_path,
229 for (i = 0; i < nours; i++)
232 for (i = 0; i < ntheirs; i++)
235 if (packfd != -1 && close(packfd) == -1 && err == NULL)
236 err = got_error_from_errno2("close", packfile_path);
237 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
238 err = got_error_from_errno2("unlink", tmpfile_path);
253 const struct got_error *
254 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
255 struct got_repository *repo,
256 got_pack_index_progress_cb progress_cb, void *progress_arg,
257 got_cancel_cb cancel_cb, void *cancel_arg)
262 int npackfd = -1, idxfd = -1, nidxfd = -1;
264 int idxstatus, done = 0;
265 const struct got_error *err;
266 struct imsgbuf idxibuf;
268 char *tmpidxpath = NULL;
269 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
270 const char *repo_path = got_repo_get_path_git_dir(repo);
273 for (i = 0; i < nitems(tmpfds); i++)
276 if (asprintf(&path, "%s/%s/indexing.idx",
277 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
278 err = got_error_from_errno("asprintf");
281 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
285 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
286 err = got_error_from_errno2("fchmod", tmpidxpath);
292 err = got_error_from_errno("dup");
296 for (i = 0; i < nitems(tmpfds); i++) {
297 tmpfds[i] = got_opentempfd();
298 if (tmpfds[i] == -1) {
299 err = got_error_from_errno("got_opentempfd");
304 err = got_object_id_str(&id_str, pack_hash);
308 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
309 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
310 err = got_error_from_errno("asprintf");
314 if (fstat(fileno(packfile), &sb) == -1) {
315 err = got_error_from_errno2("fstat", packfile_path);
319 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
320 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
321 err = got_error_from_errno("asprintf");
325 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
326 err = got_error_from_errno("socketpair");
331 err= got_error_from_errno("fork");
333 } else if (idxpid == 0)
334 got_privsep_exec_child(imsg_idxfds,
335 GOT_PATH_PROG_INDEX_PACK, packfile_path);
336 if (close(imsg_idxfds[1]) == -1) {
337 err = got_error_from_errno("close");
340 imsg_init(&idxibuf, imsg_idxfds[0]);
342 npackfd = dup(fileno(packfile));
344 err = got_error_from_errno("dup");
347 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
352 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
356 for (i = 0; i < nitems(tmpfds); i++) {
357 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
364 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
367 err = cancel_cb(cancel_arg);
372 err = got_privsep_recv_index_progress(&done, &nobj_total,
373 &nobj_indexed, &nobj_loose, &nobj_resolved,
377 if (nobj_indexed != 0) {
378 err = progress_cb(progress_arg, sb.st_size,
379 nobj_total, nobj_indexed, nobj_loose,
384 imsg_clear(&idxibuf);
386 if (close(imsg_idxfds[0]) == -1) {
387 err = got_error_from_errno("close");
390 if (waitpid(idxpid, &idxstatus, 0) == -1) {
391 err = got_error_from_errno("waitpid");
395 if (rename(tmpidxpath, idxpath) == -1) {
396 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
403 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
404 err = got_error_from_errno2("unlink", tmpidxpath);
405 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
406 err = got_error_from_errno("close");
407 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
408 err = got_error_from_errno("close");
409 for (i = 0; i < nitems(tmpfds); i++) {
410 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
411 err = got_error_from_errno("close");
419 const struct got_error *
420 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
421 struct got_repository *repo, const char *packfile_path)
423 const struct got_error *err = NULL;
424 const char *packdir_path = NULL;
425 char *packfile_name = NULL, *p, *dot;
426 struct got_object_id id;
432 packdir_path = got_repo_get_path_objects_pack(repo);
433 if (packdir_path == NULL)
434 return got_error_from_errno("got_repo_get_path_objects_pack");
436 if (!got_path_is_child(packfile_path, packdir_path,
437 strlen(packdir_path))) {
438 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
443 err = got_path_basename(&packfile_name, packfile_path);
448 if (strncmp(p, "pack-", 5) != 0) {
449 err = got_error_fmt(GOT_ERR_BAD_PATH,
450 "'%s' is not a valid pack file name",
455 dot = strchr(p, '.');
457 err = got_error_fmt(GOT_ERR_BAD_PATH,
458 "'%s' is not a valid pack file name",
462 if (strcmp(dot + 1, "pack") != 0) {
463 err = got_error_fmt(GOT_ERR_BAD_PATH,
464 "'%s' is not a valid pack file name",
469 if (!got_parse_sha1_digest(id.sha1, p)) {
470 err = got_error_fmt(GOT_ERR_BAD_PATH,
471 "'%s' is not a valid pack file name",
476 *pack_hash = got_object_id_dup(&id);
477 if (*pack_hash == NULL) {
478 err = got_error_from_errno("got_object_id_dup");
482 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW);
484 err = got_error_from_errno2("open", packfile_path);
488 *packfile = fdopen(packfd, "r");
489 if (*packfile == NULL) {
490 err = got_error_from_errno2("fdopen", packfile_path);
495 if (packfd != -1 && close(packfd) == -1 && err == NULL)
496 err = got_error_from_errno2("close", packfile_path);
505 const struct got_error *
506 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
507 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
508 got_cancel_cb cancel_cb, void *cancel_arg)
510 const struct got_error *err = NULL;
511 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
512 struct got_packidx *packidx = NULL;
513 struct got_pack *pack = NULL;
516 err = got_object_id_str(&id_str, pack_hash);
520 if (asprintf(&packpath, "%s/pack-%s.pack",
521 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
522 err = got_error_from_errno("asprintf");
525 if (asprintf(&idxpath, "%s/pack-%s.idx",
526 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
527 err = got_error_from_errno("asprintf");
531 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
535 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
539 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
540 for (i = 0; i < nobj; i++) {
541 struct got_packidx_object_id *oid;
542 struct got_object_id id, base_id;
543 off_t offset, base_offset = 0;
549 err = cancel_cb(cancel_arg);
553 oid = &packidx->hdr.sorted_ids[i];
554 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
556 offset = got_packidx_get_object_offset(packidx, i);
558 err = got_error(GOT_ERR_BAD_PACKIDX);
562 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
568 case GOT_OBJ_TYPE_OFFSET_DELTA:
569 err = got_pack_parse_offset_delta(&base_offset, &len,
570 pack, offset, tslen);
574 case GOT_OBJ_TYPE_REF_DELTA:
575 err = got_pack_parse_ref_delta(&base_id,
576 pack, offset, tslen);
581 err = (*list_cb)(list_arg, &id, type, offset, size,
582 base_offset, &base_id);
592 got_packidx_close(packidx);