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/tree.h>
20 #include <sys/uio.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <unistd.h>
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
42 #include "got_path.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.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"
53 #include "got_lib_lockfile.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, 0, 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;
597 static const struct got_error *
598 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
599 got_cleanup_progress_cb progress_cb, void *progress_arg,
600 struct got_repository *repo)
602 const struct got_error *err = NULL;
603 char *path_objects = NULL, *path = NULL;
604 DIR *dir = NULL;
605 struct got_object *obj = NULL;
606 struct got_object_id id;
607 int i, fd = -1;
608 struct stat sb;
610 *ondisk_size = 0;
611 *loose_ids = got_object_idset_alloc();
612 if (*loose_ids == NULL)
613 return got_error_from_errno("got_object_idset_alloc");
615 path_objects = got_repo_get_path_objects(repo);
616 if (path_objects == NULL) {
617 err = got_error_from_errno("got_repo_get_path_objects");
618 goto done;
621 for (i = 0; i <= 0xff; i++) {
622 struct dirent *dent;
624 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
625 err = got_error_from_errno("asprintf");
626 break;
629 dir = opendir(path);
630 if (dir == NULL) {
631 if (errno == ENOENT) {
632 err = NULL;
633 continue;
635 err = got_error_from_errno2("opendir", path);
636 break;
639 while ((dent = readdir(dir)) != NULL) {
640 char *id_str;
642 if (strcmp(dent->d_name, ".") == 0 ||
643 strcmp(dent->d_name, "..") == 0)
644 continue;
646 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
647 err = got_error_from_errno("asprintf");
648 goto done;
651 memset(&id, 0, sizeof(id));
652 if (!got_parse_sha1_digest(id.sha1, id_str)) {
653 free(id_str);
654 continue;
656 free(id_str);
658 err = got_object_open_loose_fd(&fd, &id, repo);
659 if (err)
660 goto done;
661 if (fstat(fd, &sb) == -1) {
662 err = got_error_from_errno("fstat");
663 goto done;
665 err = got_object_read_header_privsep(&obj, &id, repo,
666 fd);
667 if (err)
668 goto done;
669 fd = -1; /* already closed */
671 switch (obj->type) {
672 case GOT_OBJ_TYPE_COMMIT:
673 case GOT_OBJ_TYPE_TREE:
674 case GOT_OBJ_TYPE_BLOB:
675 case GOT_OBJ_TYPE_TAG:
676 break;
677 default:
678 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
679 "%d", obj->type);
680 goto done;
682 got_object_close(obj);
683 obj = NULL;
684 (*ondisk_size) += sb.st_size;
685 err = got_object_idset_add(*loose_ids, &id, NULL);
686 if (err)
687 goto done;
688 if (progress_cb) {
689 err = progress_cb(progress_arg,
690 got_object_idset_num_elements(*loose_ids),
691 -1, -1);
692 if (err)
693 goto done;
697 if (closedir(dir) != 0) {
698 err = got_error_from_errno("closedir");
699 goto done;
701 dir = NULL;
703 free(path);
704 path = NULL;
706 done:
707 if (dir && closedir(dir) != 0 && err == NULL)
708 err = got_error_from_errno("closedir");
709 if (fd != -1 && close(fd) == -1 && err == NULL)
710 err = got_error_from_errno("close");
711 if (err) {
712 got_object_idset_free(*loose_ids);
713 *loose_ids = NULL;
715 if (obj)
716 got_object_close(obj);
717 free(path_objects);
718 free(path);
719 return err;
722 static const struct got_error *
723 preserve_loose_object(struct got_object_idset *loose_ids,
724 struct got_object_id *id, struct got_repository *repo, int *npacked)
726 const struct got_error *err = NULL;
727 struct got_object *obj;
729 if (!got_object_idset_contains(loose_ids, id))
730 return NULL;
732 /*
733 * Try to open this object from a pack file. This ensures that
734 * we do in fact have a valid packed copy of the object. Otherwise
735 * we should not delete the loose representation of this object.
736 */
737 err = got_object_open_packed(&obj, id, repo);
738 if (err == NULL) {
739 got_object_close(obj);
740 /*
741 * The object is referenced and packed.
742 * We can purge the redundantly stored loose object.
743 */
744 (*npacked)++;
745 return NULL;
746 } else if (err->code != GOT_ERR_NO_OBJ)
747 return err;
749 /*
750 * This object is referenced and not packed.
751 * Remove it from our purge set.
752 */
753 return got_object_idset_remove(NULL, loose_ids, id);
756 static const struct got_error *
757 load_tree_entries(struct got_object_id_queue *ids,
758 struct got_object_idset *loose_ids,
759 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
760 const char *dpath, struct got_repository *repo, int *npacked,
761 got_cancel_cb cancel_cb, void *cancel_arg)
763 const struct got_error *err;
764 struct got_tree_object *tree;
765 char *p = NULL;
766 int i;
768 err = got_object_open_as_tree(&tree, repo, tree_id);
769 if (err)
770 return err;
772 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
773 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
774 struct got_object_id *id = got_tree_entry_get_id(e);
775 mode_t mode = got_tree_entry_get_mode(e);
777 if (cancel_cb) {
778 err = (*cancel_cb)(cancel_arg);
779 if (err)
780 break;
783 if (got_object_tree_entry_is_symlink(e) ||
784 got_object_tree_entry_is_submodule(e) ||
785 got_object_idset_contains(traversed_ids, id))
786 continue;
788 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
789 got_tree_entry_get_name(e)) == -1) {
790 err = got_error_from_errno("asprintf");
791 break;
794 if (S_ISDIR(mode)) {
795 struct got_object_qid *qid;
796 err = got_object_qid_alloc(&qid, id);
797 if (err)
798 break;
799 STAILQ_INSERT_TAIL(ids, qid, entry);
800 } else if (S_ISREG(mode)) {
801 /* This blob is referenced. */
802 err = preserve_loose_object(loose_ids, id, repo,
803 npacked);
804 if (err)
805 break;
806 err = got_object_idset_add(traversed_ids, id, NULL);
807 if (err)
808 break;
811 free(p);
812 p = NULL;
815 got_object_tree_close(tree);
816 free(p);
817 return err;
820 static const struct got_error *
821 load_tree(struct got_object_idset *loose_ids,
822 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
823 const char *dpath, struct got_repository *repo, int *npacked,
824 got_cancel_cb cancel_cb, void *cancel_arg)
826 const struct got_error *err = NULL;
827 struct got_object_id_queue tree_ids;
828 struct got_object_qid *qid;
830 err = got_object_qid_alloc(&qid, tree_id);
831 if (err)
832 return err;
834 STAILQ_INIT(&tree_ids);
835 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
837 while (!STAILQ_EMPTY(&tree_ids)) {
838 if (cancel_cb) {
839 err = (*cancel_cb)(cancel_arg);
840 if (err)
841 break;
844 qid = STAILQ_FIRST(&tree_ids);
845 STAILQ_REMOVE_HEAD(&tree_ids, entry);
847 if (got_object_idset_contains(traversed_ids, qid->id)) {
848 got_object_qid_free(qid);
849 continue;
852 err = got_object_idset_add(traversed_ids, qid->id, NULL);
853 if (err) {
854 got_object_qid_free(qid);
855 break;
858 /* This tree is referenced. */
859 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
860 if (err)
861 break;
863 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
864 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
865 got_object_qid_free(qid);
866 if (err)
867 break;
870 got_object_id_queue_free(&tree_ids);
871 return err;
874 static const struct got_error *
875 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
876 int *npacked, struct got_object_idset *traversed_ids,
877 struct got_object_id *id, struct got_repository *repo,
878 got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
879 got_cancel_cb cancel_cb, void *cancel_arg)
881 const struct got_error *err;
882 struct got_commit_object *commit = NULL;
883 struct got_tag_object *tag = NULL;
884 struct got_object_id *tree_id = NULL;
885 struct got_object_id_queue ids;
886 struct got_object_qid *qid;
887 int obj_type;
889 err = got_object_qid_alloc(&qid, id);
890 if (err)
891 return err;
893 STAILQ_INIT(&ids);
894 STAILQ_INSERT_TAIL(&ids, qid, entry);
896 while (!STAILQ_EMPTY(&ids)) {
897 if (cancel_cb) {
898 err = (*cancel_cb)(cancel_arg);
899 if (err)
900 break;
903 qid = STAILQ_FIRST(&ids);
904 STAILQ_REMOVE_HEAD(&ids, entry);
906 if (got_object_idset_contains(traversed_ids, qid->id)) {
907 got_object_qid_free(qid);
908 qid = NULL;
909 continue;
912 err = got_object_idset_add(traversed_ids, qid->id, NULL);
913 if (err)
914 break;
916 /* This commit or tag is referenced. */
917 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
918 if (err)
919 break;
921 err = got_object_get_type(&obj_type, repo, qid->id);
922 if (err)
923 break;
924 switch (obj_type) {
925 case GOT_OBJ_TYPE_COMMIT:
926 err = got_object_open_as_commit(&commit, repo, qid->id);
927 if (err)
928 goto done;
929 break;
930 case GOT_OBJ_TYPE_TAG:
931 err = got_object_open_as_tag(&tag, repo, qid->id);
932 if (err)
933 goto done;
934 break;
935 default:
936 /* should not happen */
937 err = got_error(GOT_ERR_OBJ_TYPE);
938 goto done;
941 /* Find a tree object to scan. */
942 if (commit) {
943 tree_id = got_object_commit_get_tree_id(commit);
944 } else if (tag) {
945 obj_type = got_object_tag_get_object_type(tag);
946 switch (obj_type) {
947 case GOT_OBJ_TYPE_COMMIT:
948 err = got_object_open_as_commit(&commit, repo,
949 got_object_tag_get_object_id(tag));
950 if (err)
951 goto done;
952 tree_id = got_object_commit_get_tree_id(commit);
953 break;
954 case GOT_OBJ_TYPE_TREE:
955 tree_id = got_object_tag_get_object_id(tag);
956 break;
957 default:
958 /*
959 * Tag points at something other than a
960 * commit or tree. Leave this weird tag object
961 * and the object it points to on disk.
962 */
963 err = got_object_idset_remove(NULL, loose_ids,
964 qid->id);
965 if (err && err->code != GOT_ERR_NO_OBJ)
966 goto done;
967 err = got_object_idset_remove(NULL, loose_ids,
968 got_object_tag_get_object_id(tag));
969 if (err && err->code != GOT_ERR_NO_OBJ)
970 goto done;
971 err = NULL;
972 break;
976 if (tree_id) {
977 err = load_tree(loose_ids, traversed_ids, tree_id, "",
978 repo, npacked, cancel_cb, cancel_arg);
979 if (err)
980 break;
983 if (commit || tag)
984 (*ncommits)++; /* scanned tags are counted as commits */
986 if (progress_cb) {
987 err = progress_cb(progress_arg, nloose, *ncommits, -1);
988 if (err)
989 break;
992 if (commit) {
993 /* Find parent commits to scan. */
994 const struct got_object_id_queue *parent_ids;
995 parent_ids = got_object_commit_get_parent_ids(commit);
996 err = got_object_id_queue_copy(parent_ids, &ids);
997 if (err)
998 break;
999 got_object_commit_close(commit);
1000 commit = NULL;
1002 if (tag) {
1003 got_object_tag_close(tag);
1004 tag = NULL;
1006 got_object_qid_free(qid);
1007 qid = NULL;
1009 done:
1010 if (qid)
1011 got_object_qid_free(qid);
1012 if (commit)
1013 got_object_commit_close(commit);
1014 if (tag)
1015 got_object_tag_close(tag);
1016 got_object_id_queue_free(&ids);
1017 return err;
1020 struct purge_loose_object_arg {
1021 struct got_repository *repo;
1022 got_cleanup_progress_cb progress_cb;
1023 void *progress_arg;
1024 int nloose;
1025 int ncommits;
1026 int npurged;
1027 off_t size_purged;
1028 int dry_run;
1029 time_t max_mtime;
1030 int ignore_mtime;
1033 static const struct got_error *
1034 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1036 struct purge_loose_object_arg *a = arg;
1037 const struct got_error *err, *unlock_err = NULL;
1038 char *path = NULL;
1039 int fd = -1;
1040 struct stat sb;
1041 struct got_lockfile *lf = NULL;
1043 err = got_object_get_path(&path, id, a->repo);
1044 if (err)
1045 return err;
1047 err = got_object_open_loose_fd(&fd, id, a->repo);
1048 if (err)
1049 goto done;
1051 if (fstat(fd, &sb) == -1) {
1052 err = got_error_from_errno("fstat");
1053 goto done;
1057 * Do not delete objects which are younger than our maximum
1058 * modification time threshold. This prevents a race where
1059 * new objects which are being added to the repository
1060 * concurrently would be deleted.
1062 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1063 if (!a->dry_run) {
1064 err = got_lockfile_lock(&lf, path, -1);
1065 if (err)
1066 goto done;
1067 if (unlink(path) == -1) {
1068 err = got_error_from_errno2("unlink", path);
1069 goto done;
1073 a->npurged++;
1074 a->size_purged += sb.st_size;
1075 if (a->progress_cb) {
1076 err = a->progress_cb(a->progress_arg, a->nloose,
1077 a->ncommits, a->npurged);
1080 done:
1081 if (fd != -1 && close(fd) == -1 && err == NULL)
1082 err = got_error_from_errno("close");
1083 free(path);
1084 if (lf)
1085 unlock_err = got_lockfile_unlock(lf, -1);
1086 return err ? err : unlock_err;
1089 const struct got_error *
1090 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1091 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1092 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1093 got_cancel_cb cancel_cb, void *cancel_arg)
1095 const struct got_error *err;
1096 struct got_object_idset *loose_ids;
1097 struct got_object_idset *traversed_ids;
1098 struct got_object_id **referenced_ids;
1099 int i, nreferenced, nloose, ncommits = 0;
1100 struct got_reflist_head refs;
1101 struct got_reflist_entry *re;
1102 struct purge_loose_object_arg arg;
1103 time_t max_mtime = 0;
1105 TAILQ_INIT(&refs);
1107 *size_before = 0;
1108 *size_after = 0;
1109 *npacked = 0;
1111 err = get_loose_object_ids(&loose_ids, size_before,
1112 progress_cb, progress_arg, repo);
1113 if (err)
1114 return err;
1115 nloose = got_object_idset_num_elements(loose_ids);
1116 if (nloose == 0) {
1117 got_object_idset_free(loose_ids);
1118 return NULL;
1121 traversed_ids = got_object_idset_alloc();
1122 if (traversed_ids == NULL) {
1123 err = got_error_from_errno("got_object_idset_alloc");
1124 goto done;
1127 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1128 if (err)
1129 goto done;
1130 if (!ignore_mtime) {
1131 TAILQ_FOREACH(re, &refs, entry) {
1132 time_t mtime = got_ref_get_mtime(re->ref);
1133 if (mtime > max_mtime)
1134 max_mtime = mtime;
1137 * For safety, keep objects created within 10 minutes
1138 * before the youngest reference was created.
1140 if (max_mtime >= 600)
1141 max_mtime -= 600;
1144 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1145 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1146 &refs, repo, cancel_cb, cancel_arg);
1147 if (err)
1148 goto done;
1150 for (i = 0; i < nreferenced; i++) {
1151 struct got_object_id *id = referenced_ids[i];
1152 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1153 traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1154 cancel_cb, cancel_arg);
1155 if (err)
1156 goto done;
1159 /* Produce a final progress report in case no objects can be purged. */
1160 if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1161 err = progress_cb(progress_arg, nloose, ncommits, 0);
1162 if (err)
1163 goto done;
1166 /* Any remaining loose objects are unreferenced and can be purged. */
1167 arg.repo = repo;
1168 arg.progress_arg = progress_arg;
1169 arg.progress_cb = progress_cb;
1170 arg.nloose = nloose;
1171 arg.npurged = 0;
1172 arg.size_purged = 0;
1173 arg.ncommits = ncommits;
1174 arg.dry_run = dry_run;
1175 arg.max_mtime = max_mtime;
1176 arg.ignore_mtime = ignore_mtime;
1177 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1178 if (err)
1179 goto done;
1180 *size_after = *size_before - arg.size_purged;
1181 done:
1182 got_object_idset_free(loose_ids);
1183 got_object_idset_free(traversed_ids);
1184 return err;
1187 static const struct got_error *
1188 remove_packidx(int dir_fd, const char *relpath)
1190 const struct got_error *err, *unlock_err;
1191 struct got_lockfile *lf;
1193 err = got_lockfile_lock(&lf, relpath, dir_fd);
1194 if (err)
1195 return err;
1196 if (unlinkat(dir_fd, relpath, 0) == -1)
1197 err = got_error_from_errno("unlinkat");
1198 unlock_err = got_lockfile_unlock(lf, dir_fd);
1199 return err ? err : unlock_err;
1202 const struct got_error *
1203 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1204 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1205 got_cancel_cb cancel_cb, void *cancel_arg)
1207 const struct got_error *err;
1208 DIR *packdir = NULL;
1209 struct dirent *dent;
1210 char *pack_relpath = NULL;
1211 int packdir_fd;
1212 struct stat sb;
1214 packdir_fd = openat(got_repo_get_fd(repo),
1215 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1216 if (packdir_fd == -1) {
1217 if (errno == ENOENT)
1218 return NULL;
1219 return got_error_from_errno_fmt("openat: %s/%s",
1220 got_repo_get_path_git_dir(repo),
1221 GOT_OBJECTS_PACK_DIR);
1224 packdir = fdopendir(packdir_fd);
1225 if (packdir == NULL) {
1226 err = got_error_from_errno("fdopendir");
1227 goto done;
1230 while ((dent = readdir(packdir)) != NULL) {
1231 if (cancel_cb) {
1232 err = cancel_cb(cancel_arg);
1233 if (err)
1234 goto done;
1237 if (!got_repo_is_packidx_filename(dent->d_name,
1238 strlen(dent->d_name)))
1239 continue;
1241 err = got_packidx_get_packfile_path(&pack_relpath,
1242 dent->d_name);
1243 if (err)
1244 goto done;
1246 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1247 free(pack_relpath);
1248 pack_relpath = NULL;
1249 continue;
1251 if (errno != ENOENT) {
1252 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1253 got_repo_get_path_git_dir(repo),
1254 GOT_OBJECTS_PACK_DIR,
1255 pack_relpath);
1256 goto done;
1259 if (!dry_run) {
1260 err = remove_packidx(packdir_fd, dent->d_name);
1261 if (err)
1262 goto done;
1264 if (progress_cb) {
1265 char *path;
1266 if (asprintf(&path, "%s/%s/%s",
1267 got_repo_get_path_git_dir(repo),
1268 GOT_OBJECTS_PACK_DIR,
1269 dent->d_name) == -1) {
1270 err = got_error_from_errno("asprintf");
1271 goto done;
1273 err = progress_cb(progress_arg, path);
1274 free(path);
1275 if (err)
1276 goto done;
1278 free(pack_relpath);
1279 pack_relpath = NULL;
1281 done:
1282 if (packdir && closedir(packdir) != 0 && err == NULL)
1283 err = got_error_from_errno("closedir");
1284 free(pack_relpath);
1285 return err;