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 err = got_reflist_sort(refs,
74 got_ref_cmp_by_commit_timestamp_descending, repo);
75 if (err)
76 return err;
78 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
79 if (*ids == NULL)
80 return got_error_from_errno("reallocarray");
81 nalloc = alloc_chunksz;
83 TAILQ_FOREACH(re, refs, entry) {
84 struct got_object_id *id;
86 if (cancel_cb) {
87 err = cancel_cb(cancel_arg);
88 if (err)
89 goto done;
90 }
92 err = got_ref_resolve(&id, repo, re->ref);
93 if (err)
94 goto done;
96 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
97 int obj_type;
98 err = got_object_get_type(&obj_type, repo, id);
99 if (err)
100 goto done;
101 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
102 free(id);
103 id = NULL;
104 continue;
108 if (nalloc <= *nobjects) {
109 struct got_object_id **new;
110 new = recallocarray(*ids, nalloc,
111 nalloc + alloc_chunksz,
112 sizeof(struct got_object_id *));
113 if (new == NULL) {
114 err = got_error_from_errno(
115 "recallocarray");
116 goto done;
118 *ids = new;
119 nalloc += alloc_chunksz;
121 (*ids)[*nobjects] = id;
122 if ((*ids)[*nobjects] == NULL) {
123 err = got_error_from_errno("got_object_id_dup");
124 goto done;
126 (*nobjects)++;
128 done:
129 if (err) {
130 for (i = 0; i < *nobjects; i++)
131 free((*ids)[i]);
132 free(*ids);
133 *ids = NULL;
134 *nobjects = 0;
136 return err;
139 const struct got_error *
140 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
141 struct got_reflist_head *include_refs,
142 struct got_reflist_head *exclude_refs, struct got_repository *repo,
143 int loose_obj_only,
144 got_pack_progress_cb progress_cb, void *progress_arg,
145 got_cancel_cb cancel_cb, void *cancel_arg)
147 const struct got_error *err = NULL;
148 struct got_object_id **ours = NULL, **theirs = NULL;
149 int nours = 0, ntheirs = 0, packfd = -1, i;
150 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
151 char *sha1_str = NULL;
153 *packfile = NULL;
154 *pack_hash = NULL;
156 if (asprintf(&path, "%s/%s/packing.pack",
157 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
158 err = got_error_from_errno("asprintf");
159 goto done;
161 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
162 if (err)
163 goto done;
165 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
166 err = got_error_from_errno2("fchmod", tmpfile_path);
167 goto done;
170 *packfile = fdopen(packfd, "w");
171 if (*packfile == NULL) {
172 err = got_error_from_errno2("fdopen", tmpfile_path);
173 goto done;
175 packfd = -1;
177 err = get_reflist_object_ids(&ours, &nours,
178 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
179 include_refs, repo, cancel_cb, cancel_arg);
180 if (err)
181 goto done;
183 if (nours == 0) {
184 err = got_error(GOT_ERR_CANNOT_PACK);
185 goto done;
188 if (!TAILQ_EMPTY(exclude_refs)) {
189 err = get_reflist_object_ids(&theirs, &ntheirs,
190 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
191 exclude_refs, repo,
192 cancel_cb, cancel_arg);
193 if (err)
194 goto done;
197 *pack_hash = calloc(1, sizeof(**pack_hash));
198 if (*pack_hash == NULL) {
199 err = got_error_from_errno("calloc");
200 goto done;
203 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
204 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
205 cancel_cb, cancel_arg);
206 if (err)
207 goto done;
209 err = got_object_id_str(&sha1_str, *pack_hash);
210 if (err)
211 goto done;
212 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
213 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
214 sha1_str) == -1) {
215 err = got_error_from_errno("asprintf");
216 goto done;
219 if (fflush(*packfile) == -1) {
220 err = got_error_from_errno("fflush");
221 goto done;
223 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
224 err = got_error_from_errno("fseek");
225 goto done;
227 if (rename(tmpfile_path, packfile_path) == -1) {
228 err = got_error_from_errno3("rename", tmpfile_path,
229 packfile_path);
230 goto done;
232 free(tmpfile_path);
233 tmpfile_path = NULL;
234 done:
235 for (i = 0; i < nours; i++)
236 free(ours[i]);
237 free(ours);
238 for (i = 0; i < ntheirs; i++)
239 free(theirs[i]);
240 free(theirs);
241 if (packfd != -1 && close(packfd) == -1 && err == NULL)
242 err = got_error_from_errno2("close", packfile_path);
243 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
244 err = got_error_from_errno2("unlink", tmpfile_path);
245 free(tmpfile_path);
246 free(packfile_path);
247 free(sha1_str);
248 free(path);
249 if (err) {
250 free(*pack_hash);
251 *pack_hash = NULL;
252 if (*packfile)
253 fclose(*packfile);
254 *packfile = NULL;
256 return err;
259 const struct got_error *
260 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
261 struct got_repository *repo,
262 got_pack_index_progress_cb progress_cb, void *progress_arg,
263 got_cancel_cb cancel_cb, void *cancel_arg)
265 size_t i;
266 char *path;
267 int imsg_idxfds[2];
268 int npackfd = -1, idxfd = -1, nidxfd = -1;
269 int tmpfds[3];
270 int idxstatus, done = 0;
271 const struct got_error *err;
272 struct imsgbuf idxibuf;
273 pid_t idxpid;
274 char *tmpidxpath = NULL;
275 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
276 const char *repo_path = got_repo_get_path_git_dir(repo);
277 struct stat sb;
279 for (i = 0; i < nitems(tmpfds); i++)
280 tmpfds[i] = -1;
282 if (asprintf(&path, "%s/%s/indexing.idx",
283 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
284 err = got_error_from_errno("asprintf");
285 goto done;
287 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
288 free(path);
289 if (err)
290 goto done;
291 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
292 err = got_error_from_errno2("fchmod", tmpidxpath);
293 goto done;
296 nidxfd = dup(idxfd);
297 if (nidxfd == -1) {
298 err = got_error_from_errno("dup");
299 goto done;
302 for (i = 0; i < nitems(tmpfds); i++) {
303 tmpfds[i] = got_opentempfd();
304 if (tmpfds[i] == -1) {
305 err = got_error_from_errno("got_opentempfd");
306 goto done;
310 err = got_object_id_str(&id_str, pack_hash);
311 if (err)
312 goto done;
314 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
315 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
316 err = got_error_from_errno("asprintf");
317 goto done;
320 if (fstat(fileno(packfile), &sb) == -1) {
321 err = got_error_from_errno2("fstat", packfile_path);
322 goto done;
325 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
326 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
327 err = got_error_from_errno("asprintf");
328 goto done;
331 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
332 err = got_error_from_errno("socketpair");
333 goto done;
335 idxpid = fork();
336 if (idxpid == -1) {
337 err= got_error_from_errno("fork");
338 goto done;
339 } else if (idxpid == 0)
340 got_privsep_exec_child(imsg_idxfds,
341 GOT_PATH_PROG_INDEX_PACK, packfile_path);
342 if (close(imsg_idxfds[1]) == -1) {
343 err = got_error_from_errno("close");
344 goto done;
346 imsg_init(&idxibuf, imsg_idxfds[0]);
348 npackfd = dup(fileno(packfile));
349 if (npackfd == -1) {
350 err = got_error_from_errno("dup");
351 goto done;
353 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
354 npackfd);
355 if (err != NULL)
356 goto done;
357 npackfd = -1;
358 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
359 if (err != NULL)
360 goto done;
361 nidxfd = -1;
362 for (i = 0; i < nitems(tmpfds); i++) {
363 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
364 if (err != NULL)
365 goto done;
366 tmpfds[i] = -1;
368 done = 0;
369 while (!done) {
370 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
372 if (cancel_cb) {
373 err = cancel_cb(cancel_arg);
374 if (err)
375 goto done;
378 err = got_privsep_recv_index_progress(&done, &nobj_total,
379 &nobj_indexed, &nobj_loose, &nobj_resolved,
380 &idxibuf);
381 if (err != NULL)
382 goto done;
383 if (nobj_indexed != 0) {
384 err = progress_cb(progress_arg, sb.st_size,
385 nobj_total, nobj_indexed, nobj_loose,
386 nobj_resolved);
387 if (err)
388 break;
391 if (close(imsg_idxfds[0]) == -1) {
392 err = got_error_from_errno("close");
393 goto done;
395 if (waitpid(idxpid, &idxstatus, 0) == -1) {
396 err = got_error_from_errno("waitpid");
397 goto done;
400 if (rename(tmpidxpath, idxpath) == -1) {
401 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
402 goto done;
404 free(tmpidxpath);
405 tmpidxpath = NULL;
407 done:
408 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
409 err = got_error_from_errno2("unlink", tmpidxpath);
410 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
411 err = got_error_from_errno("close");
412 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
413 err = got_error_from_errno("close");
414 for (i = 0; i < nitems(tmpfds); i++) {
415 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
416 err = got_error_from_errno("close");
418 free(tmpidxpath);
419 free(idxpath);
420 free(packfile_path);
421 return err;
424 const struct got_error *
425 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
426 struct got_repository *repo, const char *packfile_path)
428 const struct got_error *err = NULL;
429 const char *packdir_path = NULL;
430 char *packfile_name = NULL, *p, *dot;
431 struct got_object_id id;
432 int packfd = -1;
434 *packfile = NULL;
435 *pack_hash = NULL;
437 packdir_path = got_repo_get_path_objects_pack(repo);
438 if (packdir_path == NULL)
439 return got_error_from_errno("got_repo_get_path_objects_pack");
441 if (!got_path_is_child(packfile_path, packdir_path,
442 strlen(packdir_path))) {
443 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
444 goto done;
448 err = got_path_basename(&packfile_name, packfile_path);
449 if (err)
450 goto done;
451 p = packfile_name;
453 if (strncmp(p, "pack-", 5) != 0) {
454 err = got_error_fmt(GOT_ERR_BAD_PATH,
455 "'%s' is not a valid pack file name",
456 packfile_name);
457 goto done;
459 p += 5;
460 dot = strchr(p, '.');
461 if (dot == NULL) {
462 err = got_error_fmt(GOT_ERR_BAD_PATH,
463 "'%s' is not a valid pack file name",
464 packfile_name);
465 goto done;
467 if (strcmp(dot + 1, "pack") != 0) {
468 err = got_error_fmt(GOT_ERR_BAD_PATH,
469 "'%s' is not a valid pack file name",
470 packfile_name);
471 goto done;
473 *dot = '\0';
474 if (!got_parse_sha1_digest(id.sha1, p)) {
475 err = got_error_fmt(GOT_ERR_BAD_PATH,
476 "'%s' is not a valid pack file name",
477 packfile_name);
478 goto done;
481 *pack_hash = got_object_id_dup(&id);
482 if (*pack_hash == NULL) {
483 err = got_error_from_errno("got_object_id_dup");
484 goto done;
487 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
488 if (packfd == -1) {
489 err = got_error_from_errno2("open", packfile_path);
490 goto done;
493 *packfile = fdopen(packfd, "r");
494 if (*packfile == NULL) {
495 err = got_error_from_errno2("fdopen", packfile_path);
496 goto done;
498 packfd = -1;
499 done:
500 if (packfd != -1 && close(packfd) == -1 && err == NULL)
501 err = got_error_from_errno2("close", packfile_path);
502 free(packfile_name);
503 if (err) {
504 free(*pack_hash);
505 *pack_hash = NULL;
507 return err;
510 const struct got_error *
511 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
512 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
513 got_cancel_cb cancel_cb, void *cancel_arg)
515 const struct got_error *err = NULL;
516 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
517 struct got_packidx *packidx = NULL;
518 struct got_pack *pack = NULL;
519 uint32_t nobj, i;
521 err = got_object_id_str(&id_str, pack_hash);
522 if (err)
523 goto done;
525 if (asprintf(&packpath, "%s/pack-%s.pack",
526 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
527 err = got_error_from_errno("asprintf");
528 goto done;
530 if (asprintf(&idxpath, "%s/pack-%s.idx",
531 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
532 err = got_error_from_errno("asprintf");
533 goto done;
536 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
537 if (err)
538 goto done;
540 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
541 if (err)
542 goto done;
544 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
545 for (i = 0; i < nobj; i++) {
546 struct got_packidx_object_id *oid;
547 struct got_object_id id, base_id;
548 off_t offset, base_offset = 0;
549 uint8_t type;
550 uint64_t size;
551 size_t tslen, len;
553 if (cancel_cb) {
554 err = cancel_cb(cancel_arg);
555 if (err)
556 break;
558 oid = &packidx->hdr.sorted_ids[i];
559 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
561 offset = got_packidx_get_object_offset(packidx, i);
562 if (offset == -1) {
563 err = got_error(GOT_ERR_BAD_PACKIDX);
564 goto done;
567 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
568 pack, offset);
569 if (err)
570 goto done;
572 switch (type) {
573 case GOT_OBJ_TYPE_OFFSET_DELTA:
574 err = got_pack_parse_offset_delta(&base_offset, &len,
575 pack, offset, tslen);
576 if (err)
577 goto done;
578 break;
579 case GOT_OBJ_TYPE_REF_DELTA:
580 err = got_pack_parse_ref_delta(&base_id,
581 pack, offset, tslen);
582 if (err)
583 goto done;
584 break;
586 err = (*list_cb)(list_arg, &id, type, offset, size,
587 base_offset, &base_id);
588 if (err)
589 goto done;
592 done:
593 free(id_str);
594 free(idxpath);
595 free(packpath);
596 if (packidx)
597 got_packidx_close(packidx);
598 return err;
601 static const struct got_error *
602 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
603 void *progress_arg, struct got_ratelimit *rl,
604 int nloose, int ncommits, int npurged)
606 const struct got_error *err;
607 int elapsed;
609 if (progress_cb == NULL)
610 return NULL;
612 err = got_ratelimit_check(&elapsed, rl);
613 if (err || !elapsed)
614 return err;
616 return progress_cb(progress_arg, nloose, ncommits, npurged);
619 static const struct got_error *
620 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
621 got_cleanup_progress_cb progress_cb, void *progress_arg,
622 struct got_ratelimit *rl, struct got_repository *repo)
624 const struct got_error *err = NULL;
625 char *path_objects = NULL, *path = NULL;
626 DIR *dir = NULL;
627 struct got_object *obj = NULL;
628 struct got_object_id id;
629 int i, fd = -1;
630 struct stat sb;
632 *ondisk_size = 0;
633 *loose_ids = got_object_idset_alloc();
634 if (*loose_ids == NULL)
635 return got_error_from_errno("got_object_idset_alloc");
637 path_objects = got_repo_get_path_objects(repo);
638 if (path_objects == NULL) {
639 err = got_error_from_errno("got_repo_get_path_objects");
640 goto done;
643 for (i = 0; i <= 0xff; i++) {
644 struct dirent *dent;
646 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
647 err = got_error_from_errno("asprintf");
648 break;
651 dir = opendir(path);
652 if (dir == NULL) {
653 if (errno == ENOENT) {
654 err = NULL;
655 continue;
657 err = got_error_from_errno2("opendir", path);
658 break;
661 while ((dent = readdir(dir)) != NULL) {
662 char *id_str;
664 if (strcmp(dent->d_name, ".") == 0 ||
665 strcmp(dent->d_name, "..") == 0)
666 continue;
668 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
669 err = got_error_from_errno("asprintf");
670 goto done;
673 memset(&id, 0, sizeof(id));
674 if (!got_parse_sha1_digest(id.sha1, id_str)) {
675 free(id_str);
676 continue;
678 free(id_str);
680 err = got_object_open_loose_fd(&fd, &id, repo);
681 if (err)
682 goto done;
683 if (fstat(fd, &sb) == -1) {
684 err = got_error_from_errno("fstat");
685 goto done;
687 err = got_object_read_header_privsep(&obj, &id, repo,
688 fd);
689 if (err)
690 goto done;
691 fd = -1; /* already closed */
693 switch (obj->type) {
694 case GOT_OBJ_TYPE_COMMIT:
695 case GOT_OBJ_TYPE_TREE:
696 case GOT_OBJ_TYPE_BLOB:
697 case GOT_OBJ_TYPE_TAG:
698 break;
699 default:
700 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
701 "%d", obj->type);
702 goto done;
704 got_object_close(obj);
705 obj = NULL;
706 (*ondisk_size) += sb.st_size;
707 err = got_object_idset_add(*loose_ids, &id, NULL);
708 if (err)
709 goto done;
710 err = report_cleanup_progress(progress_cb,
711 progress_arg, rl,
712 got_object_idset_num_elements(*loose_ids), -1, -1);
713 if (err)
714 goto done;
717 if (closedir(dir) != 0) {
718 err = got_error_from_errno("closedir");
719 goto done;
721 dir = NULL;
723 free(path);
724 path = NULL;
726 done:
727 if (dir && closedir(dir) != 0 && err == NULL)
728 err = got_error_from_errno("closedir");
729 if (fd != -1 && close(fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
731 if (err) {
732 got_object_idset_free(*loose_ids);
733 *loose_ids = NULL;
735 if (obj)
736 got_object_close(obj);
737 free(path_objects);
738 free(path);
739 return err;
742 static const struct got_error *
743 preserve_loose_object(struct got_object_idset *loose_ids,
744 struct got_object_id *id, struct got_repository *repo, int *npacked)
746 const struct got_error *err = NULL;
747 struct got_object *obj;
749 if (!got_object_idset_contains(loose_ids, id))
750 return NULL;
752 /*
753 * Try to open this object from a pack file. This ensures that
754 * we do in fact have a valid packed copy of the object. Otherwise
755 * we should not delete the loose representation of this object.
756 */
757 err = got_object_open_packed(&obj, id, repo);
758 if (err == NULL) {
759 got_object_close(obj);
760 /*
761 * The object is referenced and packed.
762 * We can purge the redundantly stored loose object.
763 */
764 (*npacked)++;
765 return NULL;
766 } else if (err->code != GOT_ERR_NO_OBJ)
767 return err;
769 /*
770 * This object is referenced and not packed.
771 * Remove it from our purge set.
772 */
773 return got_object_idset_remove(NULL, loose_ids, id);
776 static const struct got_error *
777 load_tree_entries(struct got_object_id_queue *ids,
778 struct got_object_idset *loose_ids,
779 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
780 const char *dpath, struct got_repository *repo, int *npacked,
781 got_cancel_cb cancel_cb, void *cancel_arg)
783 const struct got_error *err;
784 struct got_tree_object *tree;
785 char *p = NULL;
786 int i;
788 err = got_object_open_as_tree(&tree, repo, tree_id);
789 if (err)
790 return err;
792 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
793 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
794 struct got_object_id *id = got_tree_entry_get_id(e);
795 mode_t mode = got_tree_entry_get_mode(e);
797 if (cancel_cb) {
798 err = (*cancel_cb)(cancel_arg);
799 if (err)
800 break;
803 if (got_object_tree_entry_is_symlink(e) ||
804 got_object_tree_entry_is_submodule(e) ||
805 got_object_idset_contains(traversed_ids, id))
806 continue;
808 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
809 got_tree_entry_get_name(e)) == -1) {
810 err = got_error_from_errno("asprintf");
811 break;
814 if (S_ISDIR(mode)) {
815 struct got_object_qid *qid;
816 err = got_object_qid_alloc(&qid, id);
817 if (err)
818 break;
819 STAILQ_INSERT_TAIL(ids, qid, entry);
820 } else if (S_ISREG(mode)) {
821 /* This blob is referenced. */
822 err = preserve_loose_object(loose_ids, id, repo,
823 npacked);
824 if (err)
825 break;
826 err = got_object_idset_add(traversed_ids, id, NULL);
827 if (err)
828 break;
831 free(p);
832 p = NULL;
835 got_object_tree_close(tree);
836 free(p);
837 return err;
840 static const struct got_error *
841 load_tree(struct got_object_idset *loose_ids,
842 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
843 const char *dpath, struct got_repository *repo, int *npacked,
844 got_cancel_cb cancel_cb, void *cancel_arg)
846 const struct got_error *err = NULL;
847 struct got_object_id_queue tree_ids;
848 struct got_object_qid *qid;
850 err = got_object_qid_alloc(&qid, tree_id);
851 if (err)
852 return err;
854 STAILQ_INIT(&tree_ids);
855 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
857 while (!STAILQ_EMPTY(&tree_ids)) {
858 if (cancel_cb) {
859 err = (*cancel_cb)(cancel_arg);
860 if (err)
861 break;
864 qid = STAILQ_FIRST(&tree_ids);
865 STAILQ_REMOVE_HEAD(&tree_ids, entry);
867 if (got_object_idset_contains(traversed_ids, &qid->id)) {
868 got_object_qid_free(qid);
869 continue;
872 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
873 if (err) {
874 got_object_qid_free(qid);
875 break;
878 /* This tree is referenced. */
879 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
880 if (err)
881 break;
883 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
884 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
885 got_object_qid_free(qid);
886 if (err)
887 break;
890 got_object_id_queue_free(&tree_ids);
891 return err;
894 static const struct got_error *
895 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
896 int *npacked, struct got_object_idset *traversed_ids,
897 struct got_object_id *id, struct got_repository *repo,
898 got_cleanup_progress_cb progress_cb, void *progress_arg,
899 struct got_ratelimit *rl, int nloose,
900 got_cancel_cb cancel_cb, void *cancel_arg)
902 const struct got_error *err;
903 struct got_commit_object *commit = NULL;
904 struct got_tag_object *tag = NULL;
905 struct got_object_id *tree_id = NULL;
906 struct got_object_id_queue ids;
907 struct got_object_qid *qid;
908 int obj_type;
910 err = got_object_qid_alloc(&qid, id);
911 if (err)
912 return err;
914 STAILQ_INIT(&ids);
915 STAILQ_INSERT_TAIL(&ids, qid, entry);
917 while (!STAILQ_EMPTY(&ids)) {
918 if (cancel_cb) {
919 err = (*cancel_cb)(cancel_arg);
920 if (err)
921 break;
924 qid = STAILQ_FIRST(&ids);
925 STAILQ_REMOVE_HEAD(&ids, entry);
927 if (got_object_idset_contains(traversed_ids, &qid->id)) {
928 got_object_qid_free(qid);
929 qid = NULL;
930 continue;
933 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
934 if (err)
935 break;
937 /* This commit or tag is referenced. */
938 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
939 if (err)
940 break;
942 err = got_object_get_type(&obj_type, repo, &qid->id);
943 if (err)
944 break;
945 switch (obj_type) {
946 case GOT_OBJ_TYPE_COMMIT:
947 err = got_object_open_as_commit(&commit, repo,
948 &qid->id);
949 if (err)
950 goto done;
951 break;
952 case GOT_OBJ_TYPE_TAG:
953 err = got_object_open_as_tag(&tag, repo, &qid->id);
954 if (err)
955 goto done;
956 break;
957 default:
958 /* should not happen */
959 err = got_error(GOT_ERR_OBJ_TYPE);
960 goto done;
963 /* Find a tree object to scan. */
964 if (commit) {
965 tree_id = got_object_commit_get_tree_id(commit);
966 } else if (tag) {
967 obj_type = got_object_tag_get_object_type(tag);
968 switch (obj_type) {
969 case GOT_OBJ_TYPE_COMMIT:
970 err = got_object_open_as_commit(&commit, repo,
971 got_object_tag_get_object_id(tag));
972 if (err)
973 goto done;
974 tree_id = got_object_commit_get_tree_id(commit);
975 break;
976 case GOT_OBJ_TYPE_TREE:
977 tree_id = got_object_tag_get_object_id(tag);
978 break;
979 default:
980 /*
981 * Tag points at something other than a
982 * commit or tree. Leave this weird tag object
983 * and the object it points to on disk.
984 */
985 err = got_object_idset_remove(NULL, loose_ids,
986 &qid->id);
987 if (err && err->code != GOT_ERR_NO_OBJ)
988 goto done;
989 err = got_object_idset_remove(NULL, loose_ids,
990 got_object_tag_get_object_id(tag));
991 if (err && err->code != GOT_ERR_NO_OBJ)
992 goto done;
993 err = NULL;
994 break;
998 if (tree_id) {
999 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1000 repo, npacked, cancel_cb, cancel_arg);
1001 if (err)
1002 break;
1005 if (commit || tag)
1006 (*ncommits)++; /* scanned tags are counted as commits */
1008 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1009 nloose, *ncommits, -1);
1010 if (err)
1011 break;
1013 if (commit) {
1014 /* Find parent commits to scan. */
1015 const struct got_object_id_queue *parent_ids;
1016 parent_ids = got_object_commit_get_parent_ids(commit);
1017 err = got_object_id_queue_copy(parent_ids, &ids);
1018 if (err)
1019 break;
1020 got_object_commit_close(commit);
1021 commit = NULL;
1023 if (tag) {
1024 got_object_tag_close(tag);
1025 tag = NULL;
1027 got_object_qid_free(qid);
1028 qid = NULL;
1030 done:
1031 if (qid)
1032 got_object_qid_free(qid);
1033 if (commit)
1034 got_object_commit_close(commit);
1035 if (tag)
1036 got_object_tag_close(tag);
1037 got_object_id_queue_free(&ids);
1038 return err;
1041 struct purge_loose_object_arg {
1042 struct got_repository *repo;
1043 got_cleanup_progress_cb progress_cb;
1044 void *progress_arg;
1045 struct got_ratelimit *rl;
1046 int nloose;
1047 int ncommits;
1048 int npurged;
1049 off_t size_purged;
1050 int dry_run;
1051 time_t max_mtime;
1052 int ignore_mtime;
1055 static const struct got_error *
1056 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1058 struct purge_loose_object_arg *a = arg;
1059 const struct got_error *err, *unlock_err = NULL;
1060 char *path = NULL;
1061 int fd = -1;
1062 struct stat sb;
1063 struct got_lockfile *lf = NULL;
1065 err = got_object_get_path(&path, id, a->repo);
1066 if (err)
1067 return err;
1069 err = got_object_open_loose_fd(&fd, id, a->repo);
1070 if (err)
1071 goto done;
1073 if (fstat(fd, &sb) == -1) {
1074 err = got_error_from_errno("fstat");
1075 goto done;
1079 * Do not delete objects which are younger than our maximum
1080 * modification time threshold. This prevents a race where
1081 * new objects which are being added to the repository
1082 * concurrently would be deleted.
1084 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1085 if (!a->dry_run) {
1086 err = got_lockfile_lock(&lf, path, -1);
1087 if (err)
1088 goto done;
1089 if (unlink(path) == -1) {
1090 err = got_error_from_errno2("unlink", path);
1091 goto done;
1095 a->npurged++;
1096 a->size_purged += sb.st_size;
1097 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1098 a->rl, a->nloose, a->ncommits, a->npurged);
1099 if (err)
1100 goto done;
1102 done:
1103 if (fd != -1 && close(fd) == -1 && err == NULL)
1104 err = got_error_from_errno("close");
1105 free(path);
1106 if (lf)
1107 unlock_err = got_lockfile_unlock(lf, -1);
1108 return err ? err : unlock_err;
1111 const struct got_error *
1112 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1113 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1114 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1115 got_cancel_cb cancel_cb, void *cancel_arg)
1117 const struct got_error *err;
1118 struct got_object_idset *loose_ids;
1119 struct got_object_idset *traversed_ids;
1120 struct got_object_id **referenced_ids;
1121 int i, nreferenced, nloose, ncommits = 0;
1122 struct got_reflist_head refs;
1123 struct got_reflist_entry *re;
1124 struct purge_loose_object_arg arg;
1125 time_t max_mtime = 0;
1126 struct got_ratelimit rl;
1128 TAILQ_INIT(&refs);
1129 got_ratelimit_init(&rl, 0, 500);
1131 *size_before = 0;
1132 *size_after = 0;
1133 *npacked = 0;
1135 err = get_loose_object_ids(&loose_ids, size_before,
1136 progress_cb, progress_arg, &rl, repo);
1137 if (err)
1138 return err;
1139 nloose = got_object_idset_num_elements(loose_ids);
1140 if (nloose == 0) {
1141 got_object_idset_free(loose_ids);
1142 if (progress_cb) {
1143 err = progress_cb(progress_arg, 0, 0, 0);
1144 if (err)
1145 return err;
1147 return NULL;
1150 traversed_ids = got_object_idset_alloc();
1151 if (traversed_ids == NULL) {
1152 err = got_error_from_errno("got_object_idset_alloc");
1153 goto done;
1156 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1157 if (err)
1158 goto done;
1159 if (!ignore_mtime) {
1160 TAILQ_FOREACH(re, &refs, entry) {
1161 time_t mtime = got_ref_get_mtime(re->ref);
1162 if (mtime > max_mtime)
1163 max_mtime = mtime;
1166 * For safety, keep objects created within 10 minutes
1167 * before the youngest reference was created.
1169 if (max_mtime >= 600)
1170 max_mtime -= 600;
1173 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1174 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1175 &refs, repo, cancel_cb, cancel_arg);
1176 if (err)
1177 goto done;
1179 for (i = 0; i < nreferenced; i++) {
1180 struct got_object_id *id = referenced_ids[i];
1181 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1182 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1183 nloose, cancel_cb, cancel_arg);
1184 if (err)
1185 goto done;
1188 /* Any remaining loose objects are unreferenced and can be purged. */
1189 arg.repo = repo;
1190 arg.progress_arg = progress_arg;
1191 arg.progress_cb = progress_cb;
1192 arg.rl = &rl;
1193 arg.nloose = nloose;
1194 arg.npurged = 0;
1195 arg.size_purged = 0;
1196 arg.ncommits = ncommits;
1197 arg.dry_run = dry_run;
1198 arg.max_mtime = max_mtime;
1199 arg.ignore_mtime = ignore_mtime;
1200 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1201 if (err)
1202 goto done;
1203 *size_after = *size_before - arg.size_purged;
1205 /* Produce a final progress report. */
1206 if (progress_cb) {
1207 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1208 if (err)
1209 goto done;
1211 done:
1212 got_object_idset_free(loose_ids);
1213 got_object_idset_free(traversed_ids);
1214 return err;
1217 static const struct got_error *
1218 remove_packidx(int dir_fd, const char *relpath)
1220 const struct got_error *err, *unlock_err;
1221 struct got_lockfile *lf;
1223 err = got_lockfile_lock(&lf, relpath, dir_fd);
1224 if (err)
1225 return err;
1226 if (unlinkat(dir_fd, relpath, 0) == -1)
1227 err = got_error_from_errno("unlinkat");
1228 unlock_err = got_lockfile_unlock(lf, dir_fd);
1229 return err ? err : unlock_err;
1232 const struct got_error *
1233 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1234 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1235 got_cancel_cb cancel_cb, void *cancel_arg)
1237 const struct got_error *err;
1238 DIR *packdir = NULL;
1239 struct dirent *dent;
1240 char *pack_relpath = NULL;
1241 int packdir_fd;
1242 struct stat sb;
1244 packdir_fd = openat(got_repo_get_fd(repo),
1245 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1246 if (packdir_fd == -1) {
1247 if (errno == ENOENT)
1248 return NULL;
1249 return got_error_from_errno_fmt("openat: %s/%s",
1250 got_repo_get_path_git_dir(repo),
1251 GOT_OBJECTS_PACK_DIR);
1254 packdir = fdopendir(packdir_fd);
1255 if (packdir == NULL) {
1256 err = got_error_from_errno("fdopendir");
1257 goto done;
1260 while ((dent = readdir(packdir)) != NULL) {
1261 if (cancel_cb) {
1262 err = cancel_cb(cancel_arg);
1263 if (err)
1264 goto done;
1267 if (!got_repo_is_packidx_filename(dent->d_name,
1268 strlen(dent->d_name)))
1269 continue;
1271 err = got_packidx_get_packfile_path(&pack_relpath,
1272 dent->d_name);
1273 if (err)
1274 goto done;
1276 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1277 free(pack_relpath);
1278 pack_relpath = NULL;
1279 continue;
1281 if (errno != ENOENT) {
1282 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1283 got_repo_get_path_git_dir(repo),
1284 GOT_OBJECTS_PACK_DIR,
1285 pack_relpath);
1286 goto done;
1289 if (!dry_run) {
1290 err = remove_packidx(packdir_fd, dent->d_name);
1291 if (err)
1292 goto done;
1294 if (progress_cb) {
1295 char *path;
1296 if (asprintf(&path, "%s/%s/%s",
1297 got_repo_get_path_git_dir(repo),
1298 GOT_OBJECTS_PACK_DIR,
1299 dent->d_name) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 goto done;
1303 err = progress_cb(progress_arg, path);
1304 free(path);
1305 if (err)
1306 goto done;
1308 free(pack_relpath);
1309 pack_relpath = NULL;
1311 done:
1312 if (packdir && closedir(packdir) != 0 && err == NULL)
1313 err = got_error_from_errno("closedir");
1314 free(pack_relpath);
1315 return err;