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/uio.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_repository_admin.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_repository.h"
49 #include "got_lib_pack_create.h"
50 #include "got_lib_sha1.h"
51 #include "got_lib_lockfile.h"
52 #include "got_lib_ratelimit.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 #endif
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)
63 {
64 const struct got_error *err = NULL;
65 const size_t alloc_chunksz = 256;
66 size_t nalloc;
67 struct got_reflist_entry *re;
68 int i;
70 *ids = NULL;
71 *nobjects = 0;
73 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
74 if (*ids == NULL)
75 return got_error_from_errno("reallocarray");
76 nalloc = alloc_chunksz;
78 TAILQ_FOREACH(re, refs, entry) {
79 struct got_object_id *id;
81 if (cancel_cb) {
82 err = cancel_cb(cancel_arg);
83 if (err)
84 goto done;
85 }
87 err = got_ref_resolve(&id, repo, re->ref);
88 if (err)
89 goto done;
91 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
92 int obj_type;
93 err = got_object_get_type(&obj_type, repo, id);
94 if (err)
95 goto done;
96 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
97 free(id);
98 id = NULL;
99 continue;
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 *));
108 if (new == NULL) {
109 err = got_error_from_errno(
110 "recallocarray");
111 goto done;
113 *ids = new;
114 nalloc += alloc_chunksz;
116 (*ids)[*nobjects] = id;
117 if ((*ids)[*nobjects] == NULL) {
118 err = got_error_from_errno("got_object_id_dup");
119 goto done;
121 (*nobjects)++;
123 done:
124 if (err) {
125 for (i = 0; i < *nobjects; i++)
126 free((*ids)[i]);
127 free(*ids);
128 *ids = NULL;
129 *nobjects = 0;
131 return err;
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,
139 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;
386 if (close(imsg_idxfds[0]) == -1) {
387 err = got_error_from_errno("close");
388 goto done;
390 if (waitpid(idxpid, &idxstatus, 0) == -1) {
391 err = got_error_from_errno("waitpid");
392 goto done;
395 if (rename(tmpidxpath, idxpath) == -1) {
396 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
397 goto done;
399 free(tmpidxpath);
400 tmpidxpath = NULL;
402 done:
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");
413 free(tmpidxpath);
414 free(idxpath);
415 free(packfile_path);
416 return err;
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;
427 int packfd = -1;
429 *packfile = NULL;
430 *pack_hash = NULL;
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);
439 goto done;
443 err = got_path_basename(&packfile_name, packfile_path);
444 if (err)
445 goto done;
446 p = packfile_name;
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",
451 packfile_name);
452 goto done;
454 p += 5;
455 dot = strchr(p, '.');
456 if (dot == NULL) {
457 err = got_error_fmt(GOT_ERR_BAD_PATH,
458 "'%s' is not a valid pack file name",
459 packfile_name);
460 goto done;
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",
465 packfile_name);
466 goto done;
468 *dot = '\0';
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",
472 packfile_name);
473 goto done;
476 *pack_hash = got_object_id_dup(&id);
477 if (*pack_hash == NULL) {
478 err = got_error_from_errno("got_object_id_dup");
479 goto done;
482 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
483 if (packfd == -1) {
484 err = got_error_from_errno2("open", packfile_path);
485 goto done;
488 *packfile = fdopen(packfd, "r");
489 if (*packfile == NULL) {
490 err = got_error_from_errno2("fdopen", packfile_path);
491 goto done;
493 packfd = -1;
494 done:
495 if (packfd != -1 && close(packfd) == -1 && err == NULL)
496 err = got_error_from_errno2("close", packfile_path);
497 free(packfile_name);
498 if (err) {
499 free(*pack_hash);
500 *pack_hash = NULL;
502 return err;
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;
514 uint32_t nobj, i;
516 err = got_object_id_str(&id_str, pack_hash);
517 if (err)
518 goto done;
520 if (asprintf(&packpath, "%s/pack-%s.pack",
521 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
522 err = got_error_from_errno("asprintf");
523 goto done;
525 if (asprintf(&idxpath, "%s/pack-%s.idx",
526 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
527 err = got_error_from_errno("asprintf");
528 goto done;
531 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
532 if (err)
533 goto done;
535 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
536 if (err)
537 goto done;
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;
544 uint8_t type;
545 uint64_t size;
546 size_t tslen, len;
548 if (cancel_cb) {
549 err = cancel_cb(cancel_arg);
550 if (err)
551 break;
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);
557 if (offset == -1) {
558 err = got_error(GOT_ERR_BAD_PACKIDX);
559 goto done;
562 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
563 pack, offset);
564 if (err)
565 goto done;
567 switch (type) {
568 case GOT_OBJ_TYPE_OFFSET_DELTA:
569 err = got_pack_parse_offset_delta(&base_offset, &len,
570 pack, offset, tslen);
571 if (err)
572 goto done;
573 break;
574 case GOT_OBJ_TYPE_REF_DELTA:
575 err = got_pack_parse_ref_delta(&base_id,
576 pack, offset, tslen);
577 if (err)
578 goto done;
579 break;
581 err = (*list_cb)(list_arg, &id, type, offset, size,
582 base_offset, &base_id);
583 if (err)
584 goto done;
587 done:
588 free(id_str);
589 free(idxpath);
590 free(packpath);
591 if (packidx)
592 got_packidx_close(packidx);
593 return err;
596 static const struct got_error *
597 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
598 void *progress_arg, struct got_ratelimit *rl,
599 int nloose, int ncommits, int npurged)
601 const struct got_error *err;
602 int elapsed;
604 if (progress_cb == NULL)
605 return NULL;
607 err = got_ratelimit_check(&elapsed, rl);
608 if (err || !elapsed)
609 return err;
611 return progress_cb(progress_arg, nloose, ncommits, npurged);
614 static const struct got_error *
615 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
616 got_cleanup_progress_cb progress_cb, void *progress_arg,
617 struct got_ratelimit *rl, struct got_repository *repo)
619 const struct got_error *err = NULL;
620 char *path_objects = NULL, *path = NULL;
621 DIR *dir = NULL;
622 struct got_object *obj = NULL;
623 struct got_object_id id;
624 int i, fd = -1;
625 struct stat sb;
627 *ondisk_size = 0;
628 *loose_ids = got_object_idset_alloc();
629 if (*loose_ids == NULL)
630 return got_error_from_errno("got_object_idset_alloc");
632 path_objects = got_repo_get_path_objects(repo);
633 if (path_objects == NULL) {
634 err = got_error_from_errno("got_repo_get_path_objects");
635 goto done;
638 for (i = 0; i <= 0xff; i++) {
639 struct dirent *dent;
641 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
642 err = got_error_from_errno("asprintf");
643 break;
646 dir = opendir(path);
647 if (dir == NULL) {
648 if (errno == ENOENT) {
649 err = NULL;
650 continue;
652 err = got_error_from_errno2("opendir", path);
653 break;
656 while ((dent = readdir(dir)) != NULL) {
657 char *id_str;
659 if (strcmp(dent->d_name, ".") == 0 ||
660 strcmp(dent->d_name, "..") == 0)
661 continue;
663 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
664 err = got_error_from_errno("asprintf");
665 goto done;
668 memset(&id, 0, sizeof(id));
669 if (!got_parse_sha1_digest(id.sha1, id_str)) {
670 free(id_str);
671 continue;
673 free(id_str);
675 err = got_object_open_loose_fd(&fd, &id, repo);
676 if (err)
677 goto done;
678 if (fstat(fd, &sb) == -1) {
679 err = got_error_from_errno("fstat");
680 goto done;
682 err = got_object_read_header_privsep(&obj, &id, repo,
683 fd);
684 if (err)
685 goto done;
686 fd = -1; /* already closed */
688 switch (obj->type) {
689 case GOT_OBJ_TYPE_COMMIT:
690 case GOT_OBJ_TYPE_TREE:
691 case GOT_OBJ_TYPE_BLOB:
692 case GOT_OBJ_TYPE_TAG:
693 break;
694 default:
695 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
696 "%d", obj->type);
697 goto done;
699 got_object_close(obj);
700 obj = NULL;
701 (*ondisk_size) += sb.st_size;
702 err = got_object_idset_add(*loose_ids, &id, NULL);
703 if (err)
704 goto done;
705 err = report_cleanup_progress(progress_cb,
706 progress_arg, rl,
707 got_object_idset_num_elements(*loose_ids), -1, -1);
708 if (err)
709 goto done;
712 if (closedir(dir) != 0) {
713 err = got_error_from_errno("closedir");
714 goto done;
716 dir = NULL;
718 free(path);
719 path = NULL;
721 done:
722 if (dir && closedir(dir) != 0 && err == NULL)
723 err = got_error_from_errno("closedir");
724 if (fd != -1 && close(fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (err) {
727 got_object_idset_free(*loose_ids);
728 *loose_ids = NULL;
730 if (obj)
731 got_object_close(obj);
732 free(path_objects);
733 free(path);
734 return err;
737 static const struct got_error *
738 preserve_loose_object(struct got_object_idset *loose_ids,
739 struct got_object_id *id, struct got_repository *repo, int *npacked)
741 const struct got_error *err = NULL;
742 struct got_object *obj;
744 if (!got_object_idset_contains(loose_ids, id))
745 return NULL;
747 /*
748 * Try to open this object from a pack file. This ensures that
749 * we do in fact have a valid packed copy of the object. Otherwise
750 * we should not delete the loose representation of this object.
751 */
752 err = got_object_open_packed(&obj, id, repo);
753 if (err == NULL) {
754 got_object_close(obj);
755 /*
756 * The object is referenced and packed.
757 * We can purge the redundantly stored loose object.
758 */
759 (*npacked)++;
760 return NULL;
761 } else if (err->code != GOT_ERR_NO_OBJ)
762 return err;
764 /*
765 * This object is referenced and not packed.
766 * Remove it from our purge set.
767 */
768 return got_object_idset_remove(NULL, loose_ids, id);
771 static const struct got_error *
772 load_tree_entries(struct got_object_id_queue *ids,
773 struct got_object_idset *loose_ids,
774 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
775 const char *dpath, struct got_repository *repo, int *npacked,
776 got_cancel_cb cancel_cb, void *cancel_arg)
778 const struct got_error *err;
779 struct got_tree_object *tree;
780 char *p = NULL;
781 int i;
783 err = got_object_open_as_tree(&tree, repo, tree_id);
784 if (err)
785 return err;
787 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
788 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
789 struct got_object_id *id = got_tree_entry_get_id(e);
790 mode_t mode = got_tree_entry_get_mode(e);
792 if (cancel_cb) {
793 err = (*cancel_cb)(cancel_arg);
794 if (err)
795 break;
798 if (got_object_tree_entry_is_symlink(e) ||
799 got_object_tree_entry_is_submodule(e) ||
800 got_object_idset_contains(traversed_ids, id))
801 continue;
803 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
804 got_tree_entry_get_name(e)) == -1) {
805 err = got_error_from_errno("asprintf");
806 break;
809 if (S_ISDIR(mode)) {
810 struct got_object_qid *qid;
811 err = got_object_qid_alloc(&qid, id);
812 if (err)
813 break;
814 STAILQ_INSERT_TAIL(ids, qid, entry);
815 } else if (S_ISREG(mode)) {
816 /* This blob is referenced. */
817 err = preserve_loose_object(loose_ids, id, repo,
818 npacked);
819 if (err)
820 break;
821 err = got_object_idset_add(traversed_ids, id, NULL);
822 if (err)
823 break;
826 free(p);
827 p = NULL;
830 got_object_tree_close(tree);
831 free(p);
832 return err;
835 static const struct got_error *
836 load_tree(struct got_object_idset *loose_ids,
837 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
838 const char *dpath, struct got_repository *repo, int *npacked,
839 got_cancel_cb cancel_cb, void *cancel_arg)
841 const struct got_error *err = NULL;
842 struct got_object_id_queue tree_ids;
843 struct got_object_qid *qid;
845 err = got_object_qid_alloc(&qid, tree_id);
846 if (err)
847 return err;
849 STAILQ_INIT(&tree_ids);
850 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
852 while (!STAILQ_EMPTY(&tree_ids)) {
853 if (cancel_cb) {
854 err = (*cancel_cb)(cancel_arg);
855 if (err)
856 break;
859 qid = STAILQ_FIRST(&tree_ids);
860 STAILQ_REMOVE_HEAD(&tree_ids, entry);
862 if (got_object_idset_contains(traversed_ids, qid->id)) {
863 got_object_qid_free(qid);
864 continue;
867 err = got_object_idset_add(traversed_ids, qid->id, NULL);
868 if (err) {
869 got_object_qid_free(qid);
870 break;
873 /* This tree is referenced. */
874 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
875 if (err)
876 break;
878 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
879 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
880 got_object_qid_free(qid);
881 if (err)
882 break;
885 got_object_id_queue_free(&tree_ids);
886 return err;
889 static const struct got_error *
890 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
891 int *npacked, struct got_object_idset *traversed_ids,
892 struct got_object_id *id, struct got_repository *repo,
893 got_cleanup_progress_cb progress_cb, void *progress_arg,
894 struct got_ratelimit *rl, int nloose,
895 got_cancel_cb cancel_cb, void *cancel_arg)
897 const struct got_error *err;
898 struct got_commit_object *commit = NULL;
899 struct got_tag_object *tag = NULL;
900 struct got_object_id *tree_id = NULL;
901 struct got_object_id_queue ids;
902 struct got_object_qid *qid;
903 int obj_type;
905 err = got_object_qid_alloc(&qid, id);
906 if (err)
907 return err;
909 STAILQ_INIT(&ids);
910 STAILQ_INSERT_TAIL(&ids, qid, entry);
912 while (!STAILQ_EMPTY(&ids)) {
913 if (cancel_cb) {
914 err = (*cancel_cb)(cancel_arg);
915 if (err)
916 break;
919 qid = STAILQ_FIRST(&ids);
920 STAILQ_REMOVE_HEAD(&ids, entry);
922 if (got_object_idset_contains(traversed_ids, qid->id)) {
923 got_object_qid_free(qid);
924 qid = NULL;
925 continue;
928 err = got_object_idset_add(traversed_ids, qid->id, NULL);
929 if (err)
930 break;
932 /* This commit or tag is referenced. */
933 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
934 if (err)
935 break;
937 err = got_object_get_type(&obj_type, repo, qid->id);
938 if (err)
939 break;
940 switch (obj_type) {
941 case GOT_OBJ_TYPE_COMMIT:
942 err = got_object_open_as_commit(&commit, repo, qid->id);
943 if (err)
944 goto done;
945 break;
946 case GOT_OBJ_TYPE_TAG:
947 err = got_object_open_as_tag(&tag, repo, qid->id);
948 if (err)
949 goto done;
950 break;
951 default:
952 /* should not happen */
953 err = got_error(GOT_ERR_OBJ_TYPE);
954 goto done;
957 /* Find a tree object to scan. */
958 if (commit) {
959 tree_id = got_object_commit_get_tree_id(commit);
960 } else if (tag) {
961 obj_type = got_object_tag_get_object_type(tag);
962 switch (obj_type) {
963 case GOT_OBJ_TYPE_COMMIT:
964 err = got_object_open_as_commit(&commit, repo,
965 got_object_tag_get_object_id(tag));
966 if (err)
967 goto done;
968 tree_id = got_object_commit_get_tree_id(commit);
969 break;
970 case GOT_OBJ_TYPE_TREE:
971 tree_id = got_object_tag_get_object_id(tag);
972 break;
973 default:
974 /*
975 * Tag points at something other than a
976 * commit or tree. Leave this weird tag object
977 * and the object it points to on disk.
978 */
979 err = got_object_idset_remove(NULL, loose_ids,
980 qid->id);
981 if (err && err->code != GOT_ERR_NO_OBJ)
982 goto done;
983 err = got_object_idset_remove(NULL, loose_ids,
984 got_object_tag_get_object_id(tag));
985 if (err && err->code != GOT_ERR_NO_OBJ)
986 goto done;
987 err = NULL;
988 break;
992 if (tree_id) {
993 err = load_tree(loose_ids, traversed_ids, tree_id, "",
994 repo, npacked, cancel_cb, cancel_arg);
995 if (err)
996 break;
999 if (commit || tag)
1000 (*ncommits)++; /* scanned tags are counted as commits */
1002 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1003 nloose, *ncommits, -1);
1004 if (err)
1005 break;
1007 if (commit) {
1008 /* Find parent commits to scan. */
1009 const struct got_object_id_queue *parent_ids;
1010 parent_ids = got_object_commit_get_parent_ids(commit);
1011 err = got_object_id_queue_copy(parent_ids, &ids);
1012 if (err)
1013 break;
1014 got_object_commit_close(commit);
1015 commit = NULL;
1017 if (tag) {
1018 got_object_tag_close(tag);
1019 tag = NULL;
1021 got_object_qid_free(qid);
1022 qid = NULL;
1024 done:
1025 if (qid)
1026 got_object_qid_free(qid);
1027 if (commit)
1028 got_object_commit_close(commit);
1029 if (tag)
1030 got_object_tag_close(tag);
1031 got_object_id_queue_free(&ids);
1032 return err;
1035 struct purge_loose_object_arg {
1036 struct got_repository *repo;
1037 got_cleanup_progress_cb progress_cb;
1038 void *progress_arg;
1039 struct got_ratelimit *rl;
1040 int nloose;
1041 int ncommits;
1042 int npurged;
1043 off_t size_purged;
1044 int dry_run;
1045 time_t max_mtime;
1046 int ignore_mtime;
1049 static const struct got_error *
1050 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1052 struct purge_loose_object_arg *a = arg;
1053 const struct got_error *err, *unlock_err = NULL;
1054 char *path = NULL;
1055 int fd = -1;
1056 struct stat sb;
1057 struct got_lockfile *lf = NULL;
1059 err = got_object_get_path(&path, id, a->repo);
1060 if (err)
1061 return err;
1063 err = got_object_open_loose_fd(&fd, id, a->repo);
1064 if (err)
1065 goto done;
1067 if (fstat(fd, &sb) == -1) {
1068 err = got_error_from_errno("fstat");
1069 goto done;
1073 * Do not delete objects which are younger than our maximum
1074 * modification time threshold. This prevents a race where
1075 * new objects which are being added to the repository
1076 * concurrently would be deleted.
1078 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1079 if (!a->dry_run) {
1080 err = got_lockfile_lock(&lf, path, -1);
1081 if (err)
1082 goto done;
1083 if (unlink(path) == -1) {
1084 err = got_error_from_errno2("unlink", path);
1085 goto done;
1089 a->npurged++;
1090 a->size_purged += sb.st_size;
1091 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1092 a->rl, a->nloose, a->ncommits, a->npurged);
1093 if (err)
1094 goto done;
1096 done:
1097 if (fd != -1 && close(fd) == -1 && err == NULL)
1098 err = got_error_from_errno("close");
1099 free(path);
1100 if (lf)
1101 unlock_err = got_lockfile_unlock(lf, -1);
1102 return err ? err : unlock_err;
1105 const struct got_error *
1106 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1107 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1108 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1109 got_cancel_cb cancel_cb, void *cancel_arg)
1111 const struct got_error *err;
1112 struct got_object_idset *loose_ids;
1113 struct got_object_idset *traversed_ids;
1114 struct got_object_id **referenced_ids;
1115 int i, nreferenced, nloose, ncommits = 0;
1116 struct got_reflist_head refs;
1117 struct got_reflist_entry *re;
1118 struct purge_loose_object_arg arg;
1119 time_t max_mtime = 0;
1120 struct got_ratelimit rl;
1122 TAILQ_INIT(&refs);
1123 got_ratelimit_init(&rl, 0, 500);
1125 *size_before = 0;
1126 *size_after = 0;
1127 *npacked = 0;
1129 err = get_loose_object_ids(&loose_ids, size_before,
1130 progress_cb, progress_arg, &rl, repo);
1131 if (err)
1132 return err;
1133 nloose = got_object_idset_num_elements(loose_ids);
1134 if (nloose == 0) {
1135 got_object_idset_free(loose_ids);
1136 if (progress_cb) {
1137 err = progress_cb(progress_arg, 0, 0, 0);
1138 if (err)
1139 return err;
1141 return NULL;
1144 traversed_ids = got_object_idset_alloc();
1145 if (traversed_ids == NULL) {
1146 err = got_error_from_errno("got_object_idset_alloc");
1147 goto done;
1150 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1151 if (err)
1152 goto done;
1153 if (!ignore_mtime) {
1154 TAILQ_FOREACH(re, &refs, entry) {
1155 time_t mtime = got_ref_get_mtime(re->ref);
1156 if (mtime > max_mtime)
1157 max_mtime = mtime;
1160 * For safety, keep objects created within 10 minutes
1161 * before the youngest reference was created.
1163 if (max_mtime >= 600)
1164 max_mtime -= 600;
1167 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1168 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1169 &refs, repo, cancel_cb, cancel_arg);
1170 if (err)
1171 goto done;
1173 for (i = 0; i < nreferenced; i++) {
1174 struct got_object_id *id = referenced_ids[i];
1175 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1176 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1177 nloose, cancel_cb, cancel_arg);
1178 if (err)
1179 goto done;
1182 /* Any remaining loose objects are unreferenced and can be purged. */
1183 arg.repo = repo;
1184 arg.progress_arg = progress_arg;
1185 arg.progress_cb = progress_cb;
1186 arg.rl = &rl;
1187 arg.nloose = nloose;
1188 arg.npurged = 0;
1189 arg.size_purged = 0;
1190 arg.ncommits = ncommits;
1191 arg.dry_run = dry_run;
1192 arg.max_mtime = max_mtime;
1193 arg.ignore_mtime = ignore_mtime;
1194 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1195 if (err)
1196 goto done;
1197 *size_after = *size_before - arg.size_purged;
1199 /* Produce a final progress report. */
1200 if (progress_cb) {
1201 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1202 if (err)
1203 goto done;
1205 done:
1206 got_object_idset_free(loose_ids);
1207 got_object_idset_free(traversed_ids);
1208 return err;
1211 static const struct got_error *
1212 remove_packidx(int dir_fd, const char *relpath)
1214 const struct got_error *err, *unlock_err;
1215 struct got_lockfile *lf;
1217 err = got_lockfile_lock(&lf, relpath, dir_fd);
1218 if (err)
1219 return err;
1220 if (unlinkat(dir_fd, relpath, 0) == -1)
1221 err = got_error_from_errno("unlinkat");
1222 unlock_err = got_lockfile_unlock(lf, dir_fd);
1223 return err ? err : unlock_err;
1226 const struct got_error *
1227 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1228 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1229 got_cancel_cb cancel_cb, void *cancel_arg)
1231 const struct got_error *err;
1232 DIR *packdir = NULL;
1233 struct dirent *dent;
1234 char *pack_relpath = NULL;
1235 int packdir_fd;
1236 struct stat sb;
1238 packdir_fd = openat(got_repo_get_fd(repo),
1239 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1240 if (packdir_fd == -1) {
1241 if (errno == ENOENT)
1242 return NULL;
1243 return got_error_from_errno_fmt("openat: %s/%s",
1244 got_repo_get_path_git_dir(repo),
1245 GOT_OBJECTS_PACK_DIR);
1248 packdir = fdopendir(packdir_fd);
1249 if (packdir == NULL) {
1250 err = got_error_from_errno("fdopendir");
1251 goto done;
1254 while ((dent = readdir(packdir)) != NULL) {
1255 if (cancel_cb) {
1256 err = cancel_cb(cancel_arg);
1257 if (err)
1258 goto done;
1261 if (!got_repo_is_packidx_filename(dent->d_name,
1262 strlen(dent->d_name)))
1263 continue;
1265 err = got_packidx_get_packfile_path(&pack_relpath,
1266 dent->d_name);
1267 if (err)
1268 goto done;
1270 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1271 free(pack_relpath);
1272 pack_relpath = NULL;
1273 continue;
1275 if (errno != ENOENT) {
1276 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1277 got_repo_get_path_git_dir(repo),
1278 GOT_OBJECTS_PACK_DIR,
1279 pack_relpath);
1280 goto done;
1283 if (!dry_run) {
1284 err = remove_packidx(packdir_fd, dent->d_name);
1285 if (err)
1286 goto done;
1288 if (progress_cb) {
1289 char *path;
1290 if (asprintf(&path, "%s/%s/%s",
1291 got_repo_get_path_git_dir(repo),
1292 GOT_OBJECTS_PACK_DIR,
1293 dent->d_name) == -1) {
1294 err = got_error_from_errno("asprintf");
1295 goto done;
1297 err = progress_cb(progress_arg, path);
1298 free(path);
1299 if (err)
1300 goto done;
1302 free(pack_relpath);
1303 pack_relpath = NULL;
1305 done:
1306 if (packdir && closedir(packdir) != 0 && err == NULL)
1307 err = got_error_from_errno("closedir");
1308 free(pack_relpath);
1309 return err;