Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_cancel.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
39 #include "got_repository_admin.h"
40 #include "got_opentemp.h"
41 #include "got_path.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_object_cache.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_ratelimit.h"
51 #include "got_lib_pack_create.h"
52 #include "got_lib_hash.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 err = got_reflist_sort(refs,
75 got_ref_cmp_by_commit_timestamp_descending, repo);
76 if (err)
77 return err;
79 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
80 if (*ids == NULL)
81 return got_error_from_errno("reallocarray");
82 nalloc = alloc_chunksz;
84 TAILQ_FOREACH(re, refs, entry) {
85 struct got_object_id *id;
87 if (cancel_cb) {
88 err = cancel_cb(cancel_arg);
89 if (err)
90 goto done;
91 }
93 err = got_ref_resolve(&id, repo, re->ref);
94 if (err)
95 goto done;
97 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
98 int obj_type;
99 err = got_object_get_type(&obj_type, repo, id);
100 if (err)
101 goto done;
102 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
103 free(id);
104 id = NULL;
105 continue;
109 if (nalloc <= *nobjects) {
110 struct got_object_id **new;
111 new = recallocarray(*ids, nalloc,
112 nalloc + alloc_chunksz,
113 sizeof(struct got_object_id *));
114 if (new == NULL) {
115 err = got_error_from_errno(
116 "recallocarray");
117 goto done;
119 *ids = new;
120 nalloc += alloc_chunksz;
122 (*ids)[*nobjects] = id;
123 if ((*ids)[*nobjects] == NULL) {
124 err = got_error_from_errno("got_object_id_dup");
125 goto done;
127 (*nobjects)++;
129 done:
130 if (err) {
131 for (i = 0; i < *nobjects; i++)
132 free((*ids)[i]);
133 free(*ids);
134 *ids = NULL;
135 *nobjects = 0;
137 return err;
140 const struct got_error *
141 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
142 struct got_reflist_head *include_refs,
143 struct got_reflist_head *exclude_refs, struct got_repository *repo,
144 int loose_obj_only, int force_refdelta,
145 got_pack_progress_cb progress_cb, void *progress_arg,
146 got_cancel_cb cancel_cb, void *cancel_arg)
148 const struct got_error *err = NULL;
149 struct got_object_id **ours = NULL, **theirs = NULL;
150 int nours = 0, ntheirs = 0, packfd = -1, i;
151 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
152 char *sha1_str = NULL;
153 FILE *delta_cache = NULL;
154 struct got_ratelimit rl;
156 *packfile = NULL;
157 *pack_hash = NULL;
159 got_ratelimit_init(&rl, 0, 500);
161 if (asprintf(&path, "%s/%s/packing.pack",
162 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
163 err = got_error_from_errno("asprintf");
164 goto done;
166 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
167 if (err)
168 goto done;
170 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
171 err = got_error_from_errno2("fchmod", tmpfile_path);
172 goto done;
175 delta_cache = got_opentemp();
176 if (delta_cache == NULL) {
177 err = got_error_from_errno("got_opentemp");
178 goto done;
181 err = get_reflist_object_ids(&ours, &nours,
182 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
183 include_refs, repo, cancel_cb, cancel_arg);
184 if (err)
185 goto done;
187 if (nours == 0) {
188 err = got_error(GOT_ERR_CANNOT_PACK);
189 goto done;
192 if (!TAILQ_EMPTY(exclude_refs)) {
193 err = get_reflist_object_ids(&theirs, &ntheirs,
194 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
195 exclude_refs, repo,
196 cancel_cb, cancel_arg);
197 if (err)
198 goto done;
201 *pack_hash = calloc(1, sizeof(**pack_hash));
202 if (*pack_hash == NULL) {
203 err = got_error_from_errno("calloc");
204 goto done;
207 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
208 theirs, ntheirs, ours, nours, repo, loose_obj_only,
209 0, force_refdelta, progress_cb, progress_arg, &rl,
210 cancel_cb, cancel_arg);
211 if (err)
212 goto done;
214 err = got_object_id_str(&sha1_str, *pack_hash);
215 if (err)
216 goto done;
217 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
218 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
219 sha1_str) == -1) {
220 err = got_error_from_errno("asprintf");
221 goto done;
224 if (lseek(packfd, 0L, SEEK_SET) == -1) {
225 err = got_error_from_errno("lseek");
226 goto done;
228 if (rename(tmpfile_path, packfile_path) == -1) {
229 err = got_error_from_errno3("rename", tmpfile_path,
230 packfile_path);
231 goto done;
233 free(tmpfile_path);
234 tmpfile_path = NULL;
236 *packfile = fdopen(packfd, "w");
237 if (*packfile == NULL) {
238 err = got_error_from_errno2("fdopen", tmpfile_path);
239 goto done;
241 packfd = -1;
242 done:
243 for (i = 0; i < nours; i++)
244 free(ours[i]);
245 free(ours);
246 for (i = 0; i < ntheirs; i++)
247 free(theirs[i]);
248 free(theirs);
249 if (packfd != -1 && close(packfd) == -1 && err == NULL)
250 err = got_error_from_errno2("close", packfile_path);
251 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
252 err = got_error_from_errno("fclose");
253 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
254 err = got_error_from_errno2("unlink", tmpfile_path);
255 free(tmpfile_path);
256 free(packfile_path);
257 free(sha1_str);
258 free(path);
259 if (err) {
260 free(*pack_hash);
261 *pack_hash = NULL;
262 if (*packfile)
263 fclose(*packfile);
264 *packfile = NULL;
266 return err;
269 const struct got_error *
270 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
271 struct got_repository *repo,
272 got_pack_index_progress_cb progress_cb, void *progress_arg,
273 got_cancel_cb cancel_cb, void *cancel_arg)
275 size_t i;
276 char *path;
277 int imsg_idxfds[2];
278 int npackfd = -1, idxfd = -1, nidxfd = -1;
279 int tmpfds[3];
280 int idxstatus, done = 0;
281 const struct got_error *err;
282 struct imsgbuf idxibuf;
283 pid_t idxpid;
284 char *tmpidxpath = NULL;
285 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
286 const char *repo_path = got_repo_get_path_git_dir(repo);
287 struct stat sb;
289 for (i = 0; i < nitems(tmpfds); i++)
290 tmpfds[i] = -1;
292 if (asprintf(&path, "%s/%s/indexing.idx",
293 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
294 err = got_error_from_errno("asprintf");
295 goto done;
297 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
298 free(path);
299 if (err)
300 goto done;
301 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
302 err = got_error_from_errno2("fchmod", tmpidxpath);
303 goto done;
306 nidxfd = dup(idxfd);
307 if (nidxfd == -1) {
308 err = got_error_from_errno("dup");
309 goto done;
312 for (i = 0; i < nitems(tmpfds); i++) {
313 tmpfds[i] = got_opentempfd();
314 if (tmpfds[i] == -1) {
315 err = got_error_from_errno("got_opentempfd");
316 goto done;
320 err = got_object_id_str(&id_str, pack_hash);
321 if (err)
322 goto done;
324 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
325 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
326 err = got_error_from_errno("asprintf");
327 goto done;
330 if (fstat(fileno(packfile), &sb) == -1) {
331 err = got_error_from_errno2("fstat", packfile_path);
332 goto done;
335 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
336 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
337 err = got_error_from_errno("asprintf");
338 goto done;
341 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
342 err = got_error_from_errno("socketpair");
343 goto done;
345 idxpid = fork();
346 if (idxpid == -1) {
347 err= got_error_from_errno("fork");
348 goto done;
349 } else if (idxpid == 0)
350 got_privsep_exec_child(imsg_idxfds,
351 GOT_PATH_PROG_INDEX_PACK, packfile_path);
352 if (close(imsg_idxfds[1]) == -1) {
353 err = got_error_from_errno("close");
354 goto done;
356 imsg_init(&idxibuf, imsg_idxfds[0]);
358 npackfd = dup(fileno(packfile));
359 if (npackfd == -1) {
360 err = got_error_from_errno("dup");
361 goto done;
363 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
364 npackfd);
365 if (err != NULL)
366 goto done;
367 npackfd = -1;
368 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
369 if (err != NULL)
370 goto done;
371 nidxfd = -1;
372 for (i = 0; i < nitems(tmpfds); i++) {
373 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
374 if (err != NULL)
375 goto done;
376 tmpfds[i] = -1;
378 done = 0;
379 while (!done) {
380 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
382 if (cancel_cb) {
383 err = cancel_cb(cancel_arg);
384 if (err)
385 goto done;
388 err = got_privsep_recv_index_progress(&done, &nobj_total,
389 &nobj_indexed, &nobj_loose, &nobj_resolved,
390 &idxibuf);
391 if (err != NULL)
392 goto done;
393 if (nobj_indexed != 0) {
394 err = progress_cb(progress_arg, sb.st_size,
395 nobj_total, nobj_indexed, nobj_loose,
396 nobj_resolved);
397 if (err)
398 break;
401 if (close(imsg_idxfds[0]) == -1) {
402 err = got_error_from_errno("close");
403 goto done;
405 if (waitpid(idxpid, &idxstatus, 0) == -1) {
406 err = got_error_from_errno("waitpid");
407 goto done;
410 if (rename(tmpidxpath, idxpath) == -1) {
411 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
412 goto done;
414 free(tmpidxpath);
415 tmpidxpath = NULL;
417 done:
418 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
419 err = got_error_from_errno2("unlink", tmpidxpath);
420 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
421 err = got_error_from_errno("close");
422 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
423 err = got_error_from_errno("close");
424 for (i = 0; i < nitems(tmpfds); i++) {
425 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
426 err = got_error_from_errno("close");
428 free(tmpidxpath);
429 free(idxpath);
430 free(packfile_path);
431 return err;
434 const struct got_error *
435 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
436 struct got_repository *repo, const char *packfile_path)
438 const struct got_error *err = NULL;
439 const char *packdir_path = NULL;
440 char *packfile_name = NULL, *p, *dot;
441 struct got_object_id id;
442 int packfd = -1;
444 *packfile = NULL;
445 *pack_hash = NULL;
447 packdir_path = got_repo_get_path_objects_pack(repo);
448 if (packdir_path == NULL)
449 return got_error_from_errno("got_repo_get_path_objects_pack");
451 if (!got_path_is_child(packfile_path, packdir_path,
452 strlen(packdir_path))) {
453 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
454 goto done;
458 err = got_path_basename(&packfile_name, packfile_path);
459 if (err)
460 goto done;
461 p = packfile_name;
463 if (strncmp(p, "pack-", 5) != 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 p += 5;
470 dot = strchr(p, '.');
471 if (dot == NULL) {
472 err = got_error_fmt(GOT_ERR_BAD_PATH,
473 "'%s' is not a valid pack file name",
474 packfile_name);
475 goto done;
477 if (strcmp(dot + 1, "pack") != 0) {
478 err = got_error_fmt(GOT_ERR_BAD_PATH,
479 "'%s' is not a valid pack file name",
480 packfile_name);
481 goto done;
483 *dot = '\0';
484 if (!got_parse_object_id(&id, p, GOT_HASH_SHA1)) {
485 err = got_error_fmt(GOT_ERR_BAD_PATH,
486 "'%s' is not a valid pack file name",
487 packfile_name);
488 goto done;
491 *pack_hash = got_object_id_dup(&id);
492 if (*pack_hash == NULL) {
493 err = got_error_from_errno("got_object_id_dup");
494 goto done;
497 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
498 if (packfd == -1) {
499 err = got_error_from_errno2("open", packfile_path);
500 goto done;
503 *packfile = fdopen(packfd, "r");
504 if (*packfile == NULL) {
505 err = got_error_from_errno2("fdopen", packfile_path);
506 goto done;
508 packfd = -1;
509 done:
510 if (packfd != -1 && close(packfd) == -1 && err == NULL)
511 err = got_error_from_errno2("close", packfile_path);
512 free(packfile_name);
513 if (err) {
514 free(*pack_hash);
515 *pack_hash = NULL;
517 return err;
520 const struct got_error *
521 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
522 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
523 got_cancel_cb cancel_cb, void *cancel_arg)
525 const struct got_error *err = NULL;
526 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
527 struct got_packidx *packidx = NULL;
528 struct got_pack *pack = NULL;
529 uint32_t nobj, i;
531 err = got_object_id_str(&id_str, pack_hash);
532 if (err)
533 goto done;
535 if (asprintf(&packpath, "%s/pack-%s.pack",
536 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
537 err = got_error_from_errno("asprintf");
538 goto done;
540 if (asprintf(&idxpath, "%s/pack-%s.idx",
541 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
542 err = got_error_from_errno("asprintf");
543 goto done;
546 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
547 if (err)
548 goto done;
550 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
551 if (err)
552 goto done;
554 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
555 for (i = 0; i < nobj; i++) {
556 struct got_packidx_object_id *oid;
557 struct got_object_id id, base_id;
558 off_t offset, base_offset = 0;
559 uint8_t type;
560 uint64_t size;
561 size_t tslen, len;
563 if (cancel_cb) {
564 err = cancel_cb(cancel_arg);
565 if (err)
566 break;
568 oid = &packidx->hdr.sorted_ids[i];
569 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
571 offset = got_packidx_get_object_offset(packidx, i);
572 if (offset == -1) {
573 err = got_error(GOT_ERR_BAD_PACKIDX);
574 goto done;
577 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
578 pack, offset);
579 if (err)
580 goto done;
582 switch (type) {
583 case GOT_OBJ_TYPE_OFFSET_DELTA:
584 err = got_pack_parse_offset_delta(&base_offset, &len,
585 pack, offset, tslen);
586 if (err)
587 goto done;
588 break;
589 case GOT_OBJ_TYPE_REF_DELTA:
590 err = got_pack_parse_ref_delta(&base_id,
591 pack, offset, tslen);
592 if (err)
593 goto done;
594 break;
596 err = (*list_cb)(list_arg, &id, type, offset, size,
597 base_offset, &base_id);
598 if (err)
599 goto done;
602 done:
603 free(id_str);
604 free(idxpath);
605 free(packpath);
606 if (packidx)
607 got_packidx_close(packidx);
608 return err;
611 static const struct got_error *
612 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
613 void *progress_arg, struct got_ratelimit *rl,
614 int nloose, int ncommits, int npurged)
616 const struct got_error *err;
617 int elapsed;
619 if (progress_cb == NULL)
620 return NULL;
622 err = got_ratelimit_check(&elapsed, rl);
623 if (err || !elapsed)
624 return err;
626 return progress_cb(progress_arg, nloose, ncommits, npurged);
629 static const struct got_error *
630 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
631 got_cleanup_progress_cb progress_cb, void *progress_arg,
632 struct got_ratelimit *rl, struct got_repository *repo)
634 const struct got_error *err = NULL;
635 char *path_objects = NULL, *path = NULL;
636 DIR *dir = NULL;
637 struct got_object *obj = NULL;
638 struct got_object_id id;
639 int i, fd = -1;
640 struct stat sb;
642 *ondisk_size = 0;
643 *loose_ids = got_object_idset_alloc();
644 if (*loose_ids == NULL)
645 return got_error_from_errno("got_object_idset_alloc");
647 path_objects = got_repo_get_path_objects(repo);
648 if (path_objects == NULL) {
649 err = got_error_from_errno("got_repo_get_path_objects");
650 goto done;
653 for (i = 0; i <= 0xff; i++) {
654 struct dirent *dent;
656 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
657 err = got_error_from_errno("asprintf");
658 break;
661 dir = opendir(path);
662 if (dir == NULL) {
663 if (errno == ENOENT) {
664 err = NULL;
665 continue;
667 err = got_error_from_errno2("opendir", path);
668 break;
671 while ((dent = readdir(dir)) != NULL) {
672 char *id_str;
674 if (strcmp(dent->d_name, ".") == 0 ||
675 strcmp(dent->d_name, "..") == 0)
676 continue;
678 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
679 err = got_error_from_errno("asprintf");
680 goto done;
683 if (!got_parse_object_id(&id, id_str,
684 GOT_HASH_SHA1)) {
685 free(id_str);
686 continue;
688 free(id_str);
690 err = got_object_open_loose_fd(&fd, &id, repo);
691 if (err)
692 goto done;
693 if (fstat(fd, &sb) == -1) {
694 err = got_error_from_errno("fstat");
695 goto done;
697 err = got_object_read_header_privsep(&obj, &id, repo,
698 fd);
699 if (err)
700 goto done;
701 fd = -1; /* already closed */
703 switch (obj->type) {
704 case GOT_OBJ_TYPE_COMMIT:
705 case GOT_OBJ_TYPE_TREE:
706 case GOT_OBJ_TYPE_BLOB:
707 case GOT_OBJ_TYPE_TAG:
708 break;
709 default:
710 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
711 "%d", obj->type);
712 goto done;
714 got_object_close(obj);
715 obj = NULL;
716 (*ondisk_size) += sb.st_size;
717 err = got_object_idset_add(*loose_ids, &id, NULL);
718 if (err)
719 goto done;
720 err = report_cleanup_progress(progress_cb,
721 progress_arg, rl,
722 got_object_idset_num_elements(*loose_ids), -1, -1);
723 if (err)
724 goto done;
727 if (closedir(dir) != 0) {
728 err = got_error_from_errno("closedir");
729 goto done;
731 dir = NULL;
733 free(path);
734 path = NULL;
736 done:
737 if (dir && closedir(dir) != 0 && err == NULL)
738 err = got_error_from_errno("closedir");
739 if (fd != -1 && close(fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (err) {
742 got_object_idset_free(*loose_ids);
743 *loose_ids = NULL;
745 if (obj)
746 got_object_close(obj);
747 free(path_objects);
748 free(path);
749 return err;
752 static const struct got_error *
753 preserve_loose_object(struct got_object_idset *loose_ids,
754 struct got_object_id *id, struct got_repository *repo, int *npacked)
756 const struct got_error *err = NULL;
757 struct got_object *obj;
759 if (!got_object_idset_contains(loose_ids, id))
760 return NULL;
762 /*
763 * Try to open this object from a pack file. This ensures that
764 * we do in fact have a valid packed copy of the object. Otherwise
765 * we should not delete the loose representation of this object.
766 */
767 err = got_object_open_packed(&obj, id, repo);
768 if (err == NULL) {
769 got_object_close(obj);
770 /*
771 * The object is referenced and packed.
772 * We can purge the redundantly stored loose object.
773 */
774 (*npacked)++;
775 return NULL;
776 } else if (err->code != GOT_ERR_NO_OBJ)
777 return err;
779 /*
780 * This object is referenced and not packed.
781 * Remove it from our purge set.
782 */
783 return got_object_idset_remove(NULL, loose_ids, id);
786 static const struct got_error *
787 load_tree_entries(struct got_object_id_queue *ids,
788 struct got_object_idset *loose_ids,
789 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
790 const char *dpath, struct got_repository *repo, int *npacked,
791 got_cancel_cb cancel_cb, void *cancel_arg)
793 const struct got_error *err;
794 struct got_tree_object *tree;
795 char *p = NULL;
796 int i;
798 err = got_object_open_as_tree(&tree, repo, tree_id);
799 if (err)
800 return err;
802 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
803 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
804 struct got_object_id *id = got_tree_entry_get_id(e);
805 mode_t mode = got_tree_entry_get_mode(e);
807 if (cancel_cb) {
808 err = (*cancel_cb)(cancel_arg);
809 if (err)
810 break;
813 if (got_object_tree_entry_is_symlink(e) ||
814 got_object_tree_entry_is_submodule(e) ||
815 got_object_idset_contains(traversed_ids, id))
816 continue;
818 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
819 got_tree_entry_get_name(e)) == -1) {
820 err = got_error_from_errno("asprintf");
821 break;
824 if (S_ISDIR(mode)) {
825 struct got_object_qid *qid;
826 err = got_object_qid_alloc(&qid, id);
827 if (err)
828 break;
829 STAILQ_INSERT_TAIL(ids, qid, entry);
830 } else if (S_ISREG(mode)) {
831 /* This blob is referenced. */
832 err = preserve_loose_object(loose_ids, id, repo,
833 npacked);
834 if (err)
835 break;
836 err = got_object_idset_add(traversed_ids, id, NULL);
837 if (err)
838 break;
840 free(p);
841 p = NULL;
844 got_object_tree_close(tree);
845 free(p);
846 return err;
849 static const struct got_error *
850 load_tree(struct got_object_idset *loose_ids,
851 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
852 const char *dpath, struct got_repository *repo, int *npacked,
853 got_cancel_cb cancel_cb, void *cancel_arg)
855 const struct got_error *err = NULL;
856 struct got_object_id_queue tree_ids;
857 struct got_object_qid *qid;
859 err = got_object_qid_alloc(&qid, tree_id);
860 if (err)
861 return err;
863 STAILQ_INIT(&tree_ids);
864 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
866 while (!STAILQ_EMPTY(&tree_ids)) {
867 if (cancel_cb) {
868 err = (*cancel_cb)(cancel_arg);
869 if (err)
870 break;
873 qid = STAILQ_FIRST(&tree_ids);
874 STAILQ_REMOVE_HEAD(&tree_ids, entry);
876 if (got_object_idset_contains(traversed_ids, &qid->id)) {
877 got_object_qid_free(qid);
878 continue;
881 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
882 if (err) {
883 got_object_qid_free(qid);
884 break;
887 /* This tree is referenced. */
888 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
889 if (err)
890 break;
892 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
893 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
894 got_object_qid_free(qid);
895 if (err)
896 break;
899 got_object_id_queue_free(&tree_ids);
900 return err;
903 static const struct got_error *
904 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
905 int *npacked, struct got_object_idset *traversed_ids,
906 struct got_object_id *id, struct got_repository *repo,
907 got_cleanup_progress_cb progress_cb, void *progress_arg,
908 struct got_ratelimit *rl, int nloose,
909 got_cancel_cb cancel_cb, void *cancel_arg)
911 const struct got_error *err;
912 struct got_commit_object *commit = NULL;
913 struct got_tag_object *tag = NULL;
914 struct got_object_id *tree_id = NULL;
915 struct got_object_id_queue ids;
916 struct got_object_qid *qid;
917 int obj_type;
919 err = got_object_qid_alloc(&qid, id);
920 if (err)
921 return err;
923 STAILQ_INIT(&ids);
924 STAILQ_INSERT_TAIL(&ids, qid, entry);
926 while (!STAILQ_EMPTY(&ids)) {
927 if (cancel_cb) {
928 err = (*cancel_cb)(cancel_arg);
929 if (err)
930 break;
933 qid = STAILQ_FIRST(&ids);
934 STAILQ_REMOVE_HEAD(&ids, entry);
936 if (got_object_idset_contains(traversed_ids, &qid->id)) {
937 got_object_qid_free(qid);
938 qid = NULL;
939 continue;
942 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
943 if (err)
944 break;
946 /* This commit or tag is referenced. */
947 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
948 if (err)
949 break;
951 err = got_object_get_type(&obj_type, repo, &qid->id);
952 if (err)
953 break;
954 switch (obj_type) {
955 case GOT_OBJ_TYPE_COMMIT:
956 err = got_object_open_as_commit(&commit, repo,
957 &qid->id);
958 if (err)
959 goto done;
960 break;
961 case GOT_OBJ_TYPE_TAG:
962 err = got_object_open_as_tag(&tag, repo, &qid->id);
963 if (err)
964 goto done;
965 break;
966 default:
967 /* should not happen */
968 err = got_error(GOT_ERR_OBJ_TYPE);
969 goto done;
972 /* Find a tree object to scan. */
973 if (commit) {
974 tree_id = got_object_commit_get_tree_id(commit);
975 } else if (tag) {
976 obj_type = got_object_tag_get_object_type(tag);
977 switch (obj_type) {
978 case GOT_OBJ_TYPE_COMMIT:
979 err = got_object_open_as_commit(&commit, repo,
980 got_object_tag_get_object_id(tag));
981 if (err)
982 goto done;
983 tree_id = got_object_commit_get_tree_id(commit);
984 break;
985 case GOT_OBJ_TYPE_TREE:
986 tree_id = got_object_tag_get_object_id(tag);
987 break;
988 default:
989 /*
990 * Tag points at something other than a
991 * commit or tree. Leave this weird tag object
992 * and the object it points to on disk.
993 */
994 err = got_object_idset_remove(NULL, loose_ids,
995 &qid->id);
996 if (err && err->code != GOT_ERR_NO_OBJ)
997 goto done;
998 err = got_object_idset_remove(NULL, loose_ids,
999 got_object_tag_get_object_id(tag));
1000 if (err && err->code != GOT_ERR_NO_OBJ)
1001 goto done;
1002 err = NULL;
1003 break;
1007 if (tree_id) {
1008 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1009 repo, npacked, cancel_cb, cancel_arg);
1010 if (err)
1011 break;
1014 if (commit || tag)
1015 (*ncommits)++; /* scanned tags are counted as commits */
1017 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1018 nloose, *ncommits, -1);
1019 if (err)
1020 break;
1022 if (commit) {
1023 /* Find parent commits to scan. */
1024 const struct got_object_id_queue *parent_ids;
1025 parent_ids = got_object_commit_get_parent_ids(commit);
1026 err = got_object_id_queue_copy(parent_ids, &ids);
1027 if (err)
1028 break;
1029 got_object_commit_close(commit);
1030 commit = NULL;
1032 if (tag) {
1033 got_object_tag_close(tag);
1034 tag = NULL;
1036 got_object_qid_free(qid);
1037 qid = NULL;
1039 done:
1040 if (qid)
1041 got_object_qid_free(qid);
1042 if (commit)
1043 got_object_commit_close(commit);
1044 if (tag)
1045 got_object_tag_close(tag);
1046 got_object_id_queue_free(&ids);
1047 return err;
1050 struct purge_loose_object_arg {
1051 struct got_repository *repo;
1052 got_cleanup_progress_cb progress_cb;
1053 void *progress_arg;
1054 struct got_ratelimit *rl;
1055 int nloose;
1056 int ncommits;
1057 int npurged;
1058 off_t size_purged;
1059 int dry_run;
1060 time_t max_mtime;
1061 int ignore_mtime;
1064 static const struct got_error *
1065 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1067 struct purge_loose_object_arg *a = arg;
1068 const struct got_error *err, *unlock_err = NULL;
1069 char *path = NULL;
1070 int fd = -1;
1071 struct stat sb;
1072 struct got_lockfile *lf = NULL;
1074 err = got_object_get_path(&path, id, a->repo);
1075 if (err)
1076 return err;
1078 err = got_object_open_loose_fd(&fd, id, a->repo);
1079 if (err)
1080 goto done;
1082 if (fstat(fd, &sb) == -1) {
1083 err = got_error_from_errno("fstat");
1084 goto done;
1088 * Do not delete objects which are younger than our maximum
1089 * modification time threshold. This prevents a race where
1090 * new objects which are being added to the repository
1091 * concurrently would be deleted.
1093 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1094 if (!a->dry_run) {
1095 err = got_lockfile_lock(&lf, path, -1);
1096 if (err)
1097 goto done;
1098 if (unlink(path) == -1) {
1099 err = got_error_from_errno2("unlink", path);
1100 goto done;
1104 a->npurged++;
1105 a->size_purged += sb.st_size;
1106 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1107 a->rl, a->nloose, a->ncommits, a->npurged);
1108 if (err)
1109 goto done;
1111 done:
1112 if (fd != -1 && close(fd) == -1 && err == NULL)
1113 err = got_error_from_errno("close");
1114 free(path);
1115 if (lf)
1116 unlock_err = got_lockfile_unlock(lf, -1);
1117 return err ? err : unlock_err;
1120 const struct got_error *
1121 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1122 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1123 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1124 got_cancel_cb cancel_cb, void *cancel_arg)
1126 const struct got_error *err;
1127 struct got_object_idset *loose_ids;
1128 struct got_object_idset *traversed_ids;
1129 struct got_object_id **referenced_ids;
1130 int i, nreferenced, nloose, ncommits = 0;
1131 struct got_reflist_head refs;
1132 struct got_reflist_entry *re;
1133 struct purge_loose_object_arg arg;
1134 time_t max_mtime = 0;
1135 struct got_ratelimit rl;
1137 TAILQ_INIT(&refs);
1138 got_ratelimit_init(&rl, 0, 500);
1140 *size_before = 0;
1141 *size_after = 0;
1142 *npacked = 0;
1144 err = get_loose_object_ids(&loose_ids, size_before,
1145 progress_cb, progress_arg, &rl, repo);
1146 if (err)
1147 return err;
1148 nloose = got_object_idset_num_elements(loose_ids);
1149 if (nloose == 0) {
1150 got_object_idset_free(loose_ids);
1151 if (progress_cb) {
1152 err = progress_cb(progress_arg, 0, 0, 0);
1153 if (err)
1154 return err;
1156 return NULL;
1159 traversed_ids = got_object_idset_alloc();
1160 if (traversed_ids == NULL) {
1161 err = got_error_from_errno("got_object_idset_alloc");
1162 goto done;
1165 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1166 if (err)
1167 goto done;
1168 if (!ignore_mtime) {
1169 TAILQ_FOREACH(re, &refs, entry) {
1170 time_t mtime = got_ref_get_mtime(re->ref);
1171 if (mtime > max_mtime)
1172 max_mtime = mtime;
1175 * For safety, keep objects created within 10 minutes
1176 * before the youngest reference was created.
1178 if (max_mtime >= 600)
1179 max_mtime -= 600;
1182 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1183 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1184 &refs, repo, cancel_cb, cancel_arg);
1185 if (err)
1186 goto done;
1188 for (i = 0; i < nreferenced; i++) {
1189 struct got_object_id *id = referenced_ids[i];
1190 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1191 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1192 nloose, cancel_cb, cancel_arg);
1193 if (err)
1194 goto done;
1197 /* Any remaining loose objects are unreferenced and can be purged. */
1198 arg.repo = repo;
1199 arg.progress_arg = progress_arg;
1200 arg.progress_cb = progress_cb;
1201 arg.rl = &rl;
1202 arg.nloose = nloose;
1203 arg.npurged = 0;
1204 arg.size_purged = 0;
1205 arg.ncommits = ncommits;
1206 arg.dry_run = dry_run;
1207 arg.max_mtime = max_mtime;
1208 arg.ignore_mtime = ignore_mtime;
1209 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1210 if (err)
1211 goto done;
1212 *size_after = *size_before - arg.size_purged;
1214 /* Produce a final progress report. */
1215 if (progress_cb) {
1216 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1217 if (err)
1218 goto done;
1220 done:
1221 got_object_idset_free(loose_ids);
1222 got_object_idset_free(traversed_ids);
1223 return err;
1226 static const struct got_error *
1227 remove_packidx(int dir_fd, const char *relpath)
1229 const struct got_error *err, *unlock_err;
1230 struct got_lockfile *lf;
1232 err = got_lockfile_lock(&lf, relpath, dir_fd);
1233 if (err)
1234 return err;
1235 if (unlinkat(dir_fd, relpath, 0) == -1)
1236 err = got_error_from_errno("unlinkat");
1237 unlock_err = got_lockfile_unlock(lf, dir_fd);
1238 return err ? err : unlock_err;
1241 const struct got_error *
1242 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1243 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1244 got_cancel_cb cancel_cb, void *cancel_arg)
1246 const struct got_error *err = NULL;
1247 DIR *packdir = NULL;
1248 struct dirent *dent;
1249 char *pack_relpath = NULL;
1250 int packdir_fd;
1251 struct stat sb;
1253 packdir_fd = openat(got_repo_get_fd(repo),
1254 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1255 if (packdir_fd == -1) {
1256 if (errno == ENOENT)
1257 return NULL;
1258 return got_error_from_errno_fmt("openat: %s/%s",
1259 got_repo_get_path_git_dir(repo),
1260 GOT_OBJECTS_PACK_DIR);
1263 packdir = fdopendir(packdir_fd);
1264 if (packdir == NULL) {
1265 err = got_error_from_errno("fdopendir");
1266 goto done;
1269 while ((dent = readdir(packdir)) != NULL) {
1270 if (cancel_cb) {
1271 err = cancel_cb(cancel_arg);
1272 if (err)
1273 goto done;
1276 if (!got_repo_is_packidx_filename(dent->d_name,
1277 strlen(dent->d_name)))
1278 continue;
1280 err = got_packidx_get_packfile_path(&pack_relpath,
1281 dent->d_name);
1282 if (err)
1283 goto done;
1285 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1286 free(pack_relpath);
1287 pack_relpath = NULL;
1288 continue;
1290 if (errno != ENOENT) {
1291 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1292 got_repo_get_path_git_dir(repo),
1293 GOT_OBJECTS_PACK_DIR,
1294 pack_relpath);
1295 goto done;
1298 if (!dry_run) {
1299 err = remove_packidx(packdir_fd, dent->d_name);
1300 if (err)
1301 goto done;
1303 if (progress_cb) {
1304 char *path;
1305 if (asprintf(&path, "%s/%s/%s",
1306 got_repo_get_path_git_dir(repo),
1307 GOT_OBJECTS_PACK_DIR,
1308 dent->d_name) == -1) {
1309 err = got_error_from_errno("asprintf");
1310 goto done;
1312 err = progress_cb(progress_arg, path);
1313 free(path);
1314 if (err)
1315 goto done;
1317 free(pack_relpath);
1318 pack_relpath = NULL;
1320 done:
1321 if (packdir && closedir(packdir) != 0 && err == NULL)
1322 err = got_error_from_errno("closedir");
1323 free(pack_relpath);
1324 return err;