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, repo->algo)) {
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, repo->algo)) {
684 free(id_str);
685 continue;
687 free(id_str);
689 err = got_object_open_loose_fd(&fd, &id, repo);
690 if (err)
691 goto done;
692 if (fstat(fd, &sb) == -1) {
693 err = got_error_from_errno("fstat");
694 goto done;
696 err = got_object_read_header_privsep(&obj, &id, repo,
697 fd);
698 if (err)
699 goto done;
700 fd = -1; /* already closed */
702 switch (obj->type) {
703 case GOT_OBJ_TYPE_COMMIT:
704 case GOT_OBJ_TYPE_TREE:
705 case GOT_OBJ_TYPE_BLOB:
706 case GOT_OBJ_TYPE_TAG:
707 break;
708 default:
709 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
710 "%d", obj->type);
711 goto done;
713 got_object_close(obj);
714 obj = NULL;
715 (*ondisk_size) += sb.st_size;
716 err = got_object_idset_add(*loose_ids, &id, NULL);
717 if (err)
718 goto done;
719 err = report_cleanup_progress(progress_cb,
720 progress_arg, rl,
721 got_object_idset_num_elements(*loose_ids), -1, -1);
722 if (err)
723 goto done;
726 if (closedir(dir) != 0) {
727 err = got_error_from_errno("closedir");
728 goto done;
730 dir = NULL;
732 free(path);
733 path = NULL;
735 done:
736 if (dir && closedir(dir) != 0 && err == NULL)
737 err = got_error_from_errno("closedir");
738 if (fd != -1 && close(fd) == -1 && err == NULL)
739 err = got_error_from_errno("close");
740 if (err) {
741 got_object_idset_free(*loose_ids);
742 *loose_ids = NULL;
744 if (obj)
745 got_object_close(obj);
746 free(path_objects);
747 free(path);
748 return err;
751 static const struct got_error *
752 preserve_loose_object(struct got_object_idset *loose_ids,
753 struct got_object_id *id, struct got_repository *repo, int *npacked)
755 const struct got_error *err = NULL;
756 struct got_object *obj;
758 if (!got_object_idset_contains(loose_ids, id))
759 return NULL;
761 /*
762 * Try to open this object from a pack file. This ensures that
763 * we do in fact have a valid packed copy of the object. Otherwise
764 * we should not delete the loose representation of this object.
765 */
766 err = got_object_open_packed(&obj, id, repo);
767 if (err == NULL) {
768 got_object_close(obj);
769 /*
770 * The object is referenced and packed.
771 * We can purge the redundantly stored loose object.
772 */
773 (*npacked)++;
774 return NULL;
775 } else if (err->code != GOT_ERR_NO_OBJ)
776 return err;
778 /*
779 * This object is referenced and not packed.
780 * Remove it from our purge set.
781 */
782 return got_object_idset_remove(NULL, loose_ids, id);
785 static const struct got_error *
786 load_tree_entries(struct got_object_id_queue *ids,
787 struct got_object_idset *loose_ids,
788 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
789 const char *dpath, struct got_repository *repo, int *npacked,
790 got_cancel_cb cancel_cb, void *cancel_arg)
792 const struct got_error *err;
793 struct got_tree_object *tree;
794 char *p = NULL;
795 int i;
797 err = got_object_open_as_tree(&tree, repo, tree_id);
798 if (err)
799 return err;
801 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
802 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
803 struct got_object_id *id = got_tree_entry_get_id(e);
804 mode_t mode = got_tree_entry_get_mode(e);
806 if (cancel_cb) {
807 err = (*cancel_cb)(cancel_arg);
808 if (err)
809 break;
812 if (got_object_tree_entry_is_symlink(e) ||
813 got_object_tree_entry_is_submodule(e) ||
814 got_object_idset_contains(traversed_ids, id))
815 continue;
817 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
818 got_tree_entry_get_name(e)) == -1) {
819 err = got_error_from_errno("asprintf");
820 break;
823 if (S_ISDIR(mode)) {
824 struct got_object_qid *qid;
825 err = got_object_qid_alloc(&qid, id);
826 if (err)
827 break;
828 STAILQ_INSERT_TAIL(ids, qid, entry);
829 } else if (S_ISREG(mode)) {
830 /* This blob is referenced. */
831 err = preserve_loose_object(loose_ids, id, repo,
832 npacked);
833 if (err)
834 break;
835 err = got_object_idset_add(traversed_ids, id, NULL);
836 if (err)
837 break;
839 free(p);
840 p = NULL;
843 got_object_tree_close(tree);
844 free(p);
845 return err;
848 static const struct got_error *
849 load_tree(struct got_object_idset *loose_ids,
850 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
851 const char *dpath, struct got_repository *repo, int *npacked,
852 got_cancel_cb cancel_cb, void *cancel_arg)
854 const struct got_error *err = NULL;
855 struct got_object_id_queue tree_ids;
856 struct got_object_qid *qid;
858 err = got_object_qid_alloc(&qid, tree_id);
859 if (err)
860 return err;
862 STAILQ_INIT(&tree_ids);
863 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
865 while (!STAILQ_EMPTY(&tree_ids)) {
866 if (cancel_cb) {
867 err = (*cancel_cb)(cancel_arg);
868 if (err)
869 break;
872 qid = STAILQ_FIRST(&tree_ids);
873 STAILQ_REMOVE_HEAD(&tree_ids, entry);
875 if (got_object_idset_contains(traversed_ids, &qid->id)) {
876 got_object_qid_free(qid);
877 continue;
880 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
881 if (err) {
882 got_object_qid_free(qid);
883 break;
886 /* This tree is referenced. */
887 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
888 if (err)
889 break;
891 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
892 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
893 got_object_qid_free(qid);
894 if (err)
895 break;
898 got_object_id_queue_free(&tree_ids);
899 return err;
902 static const struct got_error *
903 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
904 int *npacked, struct got_object_idset *traversed_ids,
905 struct got_object_id *id, struct got_repository *repo,
906 got_cleanup_progress_cb progress_cb, void *progress_arg,
907 struct got_ratelimit *rl, int nloose,
908 got_cancel_cb cancel_cb, void *cancel_arg)
910 const struct got_error *err;
911 struct got_commit_object *commit = NULL;
912 struct got_tag_object *tag = NULL;
913 struct got_object_id *tree_id = NULL;
914 struct got_object_id_queue ids;
915 struct got_object_qid *qid;
916 int obj_type;
918 err = got_object_qid_alloc(&qid, id);
919 if (err)
920 return err;
922 STAILQ_INIT(&ids);
923 STAILQ_INSERT_TAIL(&ids, qid, entry);
925 while (!STAILQ_EMPTY(&ids)) {
926 if (cancel_cb) {
927 err = (*cancel_cb)(cancel_arg);
928 if (err)
929 break;
932 qid = STAILQ_FIRST(&ids);
933 STAILQ_REMOVE_HEAD(&ids, entry);
935 if (got_object_idset_contains(traversed_ids, &qid->id)) {
936 got_object_qid_free(qid);
937 qid = NULL;
938 continue;
941 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
942 if (err)
943 break;
945 /* This commit or tag is referenced. */
946 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
947 if (err)
948 break;
950 err = got_object_get_type(&obj_type, repo, &qid->id);
951 if (err)
952 break;
953 switch (obj_type) {
954 case GOT_OBJ_TYPE_COMMIT:
955 err = got_object_open_as_commit(&commit, repo,
956 &qid->id);
957 if (err)
958 goto done;
959 break;
960 case GOT_OBJ_TYPE_TAG:
961 err = got_object_open_as_tag(&tag, repo, &qid->id);
962 if (err)
963 goto done;
964 break;
965 default:
966 /* should not happen */
967 err = got_error(GOT_ERR_OBJ_TYPE);
968 goto done;
971 /* Find a tree object to scan. */
972 if (commit) {
973 tree_id = got_object_commit_get_tree_id(commit);
974 } else if (tag) {
975 obj_type = got_object_tag_get_object_type(tag);
976 switch (obj_type) {
977 case GOT_OBJ_TYPE_COMMIT:
978 err = got_object_open_as_commit(&commit, repo,
979 got_object_tag_get_object_id(tag));
980 if (err)
981 goto done;
982 tree_id = got_object_commit_get_tree_id(commit);
983 break;
984 case GOT_OBJ_TYPE_TREE:
985 tree_id = got_object_tag_get_object_id(tag);
986 break;
987 default:
988 /*
989 * Tag points at something other than a
990 * commit or tree. Leave this weird tag object
991 * and the object it points to on disk.
992 */
993 err = got_object_idset_remove(NULL, loose_ids,
994 &qid->id);
995 if (err && err->code != GOT_ERR_NO_OBJ)
996 goto done;
997 err = got_object_idset_remove(NULL, loose_ids,
998 got_object_tag_get_object_id(tag));
999 if (err && err->code != GOT_ERR_NO_OBJ)
1000 goto done;
1001 err = NULL;
1002 break;
1006 if (tree_id) {
1007 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1008 repo, npacked, cancel_cb, cancel_arg);
1009 if (err)
1010 break;
1013 if (commit || tag)
1014 (*ncommits)++; /* scanned tags are counted as commits */
1016 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1017 nloose, *ncommits, -1);
1018 if (err)
1019 break;
1021 if (commit) {
1022 /* Find parent commits to scan. */
1023 const struct got_object_id_queue *parent_ids;
1024 parent_ids = got_object_commit_get_parent_ids(commit);
1025 err = got_object_id_queue_copy(parent_ids, &ids);
1026 if (err)
1027 break;
1028 got_object_commit_close(commit);
1029 commit = NULL;
1031 if (tag) {
1032 got_object_tag_close(tag);
1033 tag = NULL;
1035 got_object_qid_free(qid);
1036 qid = NULL;
1038 done:
1039 if (qid)
1040 got_object_qid_free(qid);
1041 if (commit)
1042 got_object_commit_close(commit);
1043 if (tag)
1044 got_object_tag_close(tag);
1045 got_object_id_queue_free(&ids);
1046 return err;
1049 struct purge_loose_object_arg {
1050 struct got_repository *repo;
1051 got_cleanup_progress_cb progress_cb;
1052 void *progress_arg;
1053 struct got_ratelimit *rl;
1054 int nloose;
1055 int ncommits;
1056 int npurged;
1057 off_t size_purged;
1058 int dry_run;
1059 time_t max_mtime;
1060 int ignore_mtime;
1063 static const struct got_error *
1064 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1066 struct purge_loose_object_arg *a = arg;
1067 const struct got_error *err, *unlock_err = NULL;
1068 char *path = NULL;
1069 int fd = -1;
1070 struct stat sb;
1071 struct got_lockfile *lf = NULL;
1073 err = got_object_get_path(&path, id, a->repo);
1074 if (err)
1075 return err;
1077 err = got_object_open_loose_fd(&fd, id, a->repo);
1078 if (err)
1079 goto done;
1081 if (fstat(fd, &sb) == -1) {
1082 err = got_error_from_errno("fstat");
1083 goto done;
1087 * Do not delete objects which are younger than our maximum
1088 * modification time threshold. This prevents a race where
1089 * new objects which are being added to the repository
1090 * concurrently would be deleted.
1092 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1093 if (!a->dry_run) {
1094 err = got_lockfile_lock(&lf, path, -1);
1095 if (err)
1096 goto done;
1097 if (unlink(path) == -1) {
1098 err = got_error_from_errno2("unlink", path);
1099 goto done;
1103 a->npurged++;
1104 a->size_purged += sb.st_size;
1105 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1106 a->rl, a->nloose, a->ncommits, a->npurged);
1107 if (err)
1108 goto done;
1110 done:
1111 if (fd != -1 && close(fd) == -1 && err == NULL)
1112 err = got_error_from_errno("close");
1113 free(path);
1114 if (lf)
1115 unlock_err = got_lockfile_unlock(lf, -1);
1116 return err ? err : unlock_err;
1119 const struct got_error *
1120 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1121 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1122 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1123 got_cancel_cb cancel_cb, void *cancel_arg)
1125 const struct got_error *err;
1126 struct got_object_idset *loose_ids;
1127 struct got_object_idset *traversed_ids;
1128 struct got_object_id **referenced_ids;
1129 int i, nreferenced, nloose, ncommits = 0;
1130 struct got_reflist_head refs;
1131 struct got_reflist_entry *re;
1132 struct purge_loose_object_arg arg;
1133 time_t max_mtime = 0;
1134 struct got_ratelimit rl;
1136 TAILQ_INIT(&refs);
1137 got_ratelimit_init(&rl, 0, 500);
1139 *size_before = 0;
1140 *size_after = 0;
1141 *npacked = 0;
1143 err = get_loose_object_ids(&loose_ids, size_before,
1144 progress_cb, progress_arg, &rl, repo);
1145 if (err)
1146 return err;
1147 nloose = got_object_idset_num_elements(loose_ids);
1148 if (nloose == 0) {
1149 got_object_idset_free(loose_ids);
1150 if (progress_cb) {
1151 err = progress_cb(progress_arg, 0, 0, 0);
1152 if (err)
1153 return err;
1155 return NULL;
1158 traversed_ids = got_object_idset_alloc();
1159 if (traversed_ids == NULL) {
1160 err = got_error_from_errno("got_object_idset_alloc");
1161 goto done;
1164 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1165 if (err)
1166 goto done;
1167 if (!ignore_mtime) {
1168 TAILQ_FOREACH(re, &refs, entry) {
1169 time_t mtime = got_ref_get_mtime(re->ref);
1170 if (mtime > max_mtime)
1171 max_mtime = mtime;
1174 * For safety, keep objects created within 10 minutes
1175 * before the youngest reference was created.
1177 if (max_mtime >= 600)
1178 max_mtime -= 600;
1181 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1182 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1183 &refs, repo, cancel_cb, cancel_arg);
1184 if (err)
1185 goto done;
1187 for (i = 0; i < nreferenced; i++) {
1188 struct got_object_id *id = referenced_ids[i];
1189 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1190 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1191 nloose, cancel_cb, cancel_arg);
1192 if (err)
1193 goto done;
1196 /* Any remaining loose objects are unreferenced and can be purged. */
1197 arg.repo = repo;
1198 arg.progress_arg = progress_arg;
1199 arg.progress_cb = progress_cb;
1200 arg.rl = &rl;
1201 arg.nloose = nloose;
1202 arg.npurged = 0;
1203 arg.size_purged = 0;
1204 arg.ncommits = ncommits;
1205 arg.dry_run = dry_run;
1206 arg.max_mtime = max_mtime;
1207 arg.ignore_mtime = ignore_mtime;
1208 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1209 if (err)
1210 goto done;
1211 *size_after = *size_before - arg.size_purged;
1213 /* Produce a final progress report. */
1214 if (progress_cb) {
1215 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1216 if (err)
1217 goto done;
1219 done:
1220 got_object_idset_free(loose_ids);
1221 got_object_idset_free(traversed_ids);
1222 return err;
1225 static const struct got_error *
1226 remove_packidx(int dir_fd, const char *relpath)
1228 const struct got_error *err, *unlock_err;
1229 struct got_lockfile *lf;
1231 err = got_lockfile_lock(&lf, relpath, dir_fd);
1232 if (err)
1233 return err;
1234 if (unlinkat(dir_fd, relpath, 0) == -1)
1235 err = got_error_from_errno("unlinkat");
1236 unlock_err = got_lockfile_unlock(lf, dir_fd);
1237 return err ? err : unlock_err;
1240 const struct got_error *
1241 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1242 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1243 got_cancel_cb cancel_cb, void *cancel_arg)
1245 const struct got_error *err = NULL;
1246 DIR *packdir = NULL;
1247 struct dirent *dent;
1248 char *pack_relpath = NULL;
1249 int packdir_fd;
1250 struct stat sb;
1252 packdir_fd = openat(got_repo_get_fd(repo),
1253 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1254 if (packdir_fd == -1) {
1255 if (errno == ENOENT)
1256 return NULL;
1257 return got_error_from_errno_fmt("openat: %s/%s",
1258 got_repo_get_path_git_dir(repo),
1259 GOT_OBJECTS_PACK_DIR);
1262 packdir = fdopendir(packdir_fd);
1263 if (packdir == NULL) {
1264 err = got_error_from_errno("fdopendir");
1265 goto done;
1268 while ((dent = readdir(packdir)) != NULL) {
1269 if (cancel_cb) {
1270 err = cancel_cb(cancel_arg);
1271 if (err)
1272 goto done;
1275 if (!got_repo_is_packidx_filename(dent->d_name,
1276 strlen(dent->d_name)))
1277 continue;
1279 err = got_packidx_get_packfile_path(&pack_relpath,
1280 dent->d_name);
1281 if (err)
1282 goto done;
1284 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1285 free(pack_relpath);
1286 pack_relpath = NULL;
1287 continue;
1289 if (errno != ENOENT) {
1290 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1291 got_repo_get_path_git_dir(repo),
1292 GOT_OBJECTS_PACK_DIR,
1293 pack_relpath);
1294 goto done;
1297 if (!dry_run) {
1298 err = remove_packidx(packdir_fd, dent->d_name);
1299 if (err)
1300 goto done;
1302 if (progress_cb) {
1303 char *path;
1304 if (asprintf(&path, "%s/%s/%s",
1305 got_repo_get_path_git_dir(repo),
1306 GOT_OBJECTS_PACK_DIR,
1307 dent->d_name) == -1) {
1308 err = got_error_from_errno("asprintf");
1309 goto done;
1311 err = progress_cb(progress_arg, path);
1312 free(path);
1313 if (err)
1314 goto done;
1316 free(pack_relpath);
1317 pack_relpath = NULL;
1319 done:
1320 if (packdir && closedir(packdir) != 0 && err == NULL)
1321 err = got_error_from_errno("closedir");
1322 free(pack_relpath);
1323 return err;