Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
25 #include <dirent.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <sha2.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <imsg.h>
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_repository_admin.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_repository.h"
55 #include "got_lib_ratelimit.h"
56 #include "got_lib_pack_create.h"
57 #include "got_lib_hash.h"
58 #include "got_lib_lockfile.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 static const struct got_error *
65 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
66 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
67 struct got_repository *repo,
68 got_cancel_cb cancel_cb, void *cancel_arg)
69 {
70 const struct got_error *err = NULL;
71 const size_t alloc_chunksz = 256;
72 size_t nalloc;
73 struct got_reflist_entry *re;
74 int i;
76 *ids = NULL;
77 *nobjects = 0;
79 err = got_reflist_sort(refs,
80 got_ref_cmp_by_commit_timestamp_descending, repo);
81 if (err)
82 return err;
84 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
85 if (*ids == NULL)
86 return got_error_from_errno("reallocarray");
87 nalloc = alloc_chunksz;
89 TAILQ_FOREACH(re, refs, entry) {
90 struct got_object_id *id;
92 if (cancel_cb) {
93 err = cancel_cb(cancel_arg);
94 if (err)
95 goto done;
96 }
98 err = got_ref_resolve(&id, repo, re->ref);
99 if (err)
100 goto done;
102 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
103 int obj_type;
104 err = got_object_get_type(&obj_type, repo, id);
105 if (err)
106 goto done;
107 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
108 free(id);
109 id = NULL;
110 continue;
114 if (nalloc <= *nobjects) {
115 struct got_object_id **new;
116 new = recallocarray(*ids, nalloc,
117 nalloc + alloc_chunksz,
118 sizeof(struct got_object_id *));
119 if (new == NULL) {
120 err = got_error_from_errno(
121 "recallocarray");
122 goto done;
124 *ids = new;
125 nalloc += alloc_chunksz;
127 (*ids)[*nobjects] = id;
128 if ((*ids)[*nobjects] == NULL) {
129 err = got_error_from_errno("got_object_id_dup");
130 goto done;
132 (*nobjects)++;
134 done:
135 if (err) {
136 for (i = 0; i < *nobjects; i++)
137 free((*ids)[i]);
138 free(*ids);
139 *ids = NULL;
140 *nobjects = 0;
142 return err;
145 const struct got_error *
146 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
147 struct got_reflist_head *include_refs,
148 struct got_reflist_head *exclude_refs, struct got_repository *repo,
149 int loose_obj_only,
150 got_pack_progress_cb progress_cb, void *progress_arg,
151 got_cancel_cb cancel_cb, void *cancel_arg)
153 const struct got_error *err = NULL;
154 struct got_object_id **ours = NULL, **theirs = NULL;
155 int nours = 0, ntheirs = 0, packfd = -1, i;
156 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
157 char *hash_str = NULL;
158 FILE *delta_cache = NULL;
159 struct got_ratelimit rl;
161 *packfile = NULL;
162 *pack_hash = NULL;
164 got_ratelimit_init(&rl, 0, 500);
166 if (asprintf(&path, "%s/%s/packing.pack",
167 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
168 err = got_error_from_errno("asprintf");
169 goto done;
171 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
172 if (err)
173 goto done;
175 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
176 err = got_error_from_errno2("fchmod", tmpfile_path);
177 goto done;
180 delta_cache = got_opentemp();
181 if (delta_cache == NULL) {
182 err = got_error_from_errno("got_opentemp");
183 goto done;
186 err = get_reflist_object_ids(&ours, &nours,
187 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
188 include_refs, repo, cancel_cb, cancel_arg);
189 if (err)
190 goto done;
192 if (nours == 0) {
193 err = got_error(GOT_ERR_CANNOT_PACK);
194 goto done;
197 if (!TAILQ_EMPTY(exclude_refs)) {
198 err = get_reflist_object_ids(&theirs, &ntheirs,
199 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
200 exclude_refs, repo,
201 cancel_cb, cancel_arg);
202 if (err)
203 goto done;
206 *pack_hash = calloc(1, sizeof(**pack_hash));
207 if (*pack_hash == NULL) {
208 err = got_error_from_errno("calloc");
209 goto done;
212 err = got_pack_create((*pack_hash)->hash, packfd, delta_cache,
213 theirs, ntheirs, ours, nours, repo, loose_obj_only, 0,
214 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
215 if (err)
216 goto done;
218 err = got_object_id_str(&hash_str, *pack_hash);
219 if (err)
220 goto done;
221 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
222 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
223 hash_str) == -1) {
224 err = got_error_from_errno("asprintf");
225 goto done;
228 if (lseek(packfd, 0L, SEEK_SET) == -1) {
229 err = got_error_from_errno("lseek");
230 goto done;
232 if (rename(tmpfile_path, packfile_path) == -1) {
233 err = got_error_from_errno3("rename", tmpfile_path,
234 packfile_path);
235 goto done;
237 free(tmpfile_path);
238 tmpfile_path = NULL;
240 *packfile = fdopen(packfd, "w");
241 if (*packfile == NULL) {
242 err = got_error_from_errno2("fdopen", tmpfile_path);
243 goto done;
245 packfd = -1;
246 done:
247 for (i = 0; i < nours; i++)
248 free(ours[i]);
249 free(ours);
250 for (i = 0; i < ntheirs; i++)
251 free(theirs[i]);
252 free(theirs);
253 if (packfd != -1 && close(packfd) == -1 && err == NULL)
254 err = got_error_from_errno2("close", packfile_path);
255 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
256 err = got_error_from_errno("fclose");
257 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
258 err = got_error_from_errno2("unlink", tmpfile_path);
259 free(tmpfile_path);
260 free(packfile_path);
261 free(hash_str);
262 free(path);
263 if (err) {
264 free(*pack_hash);
265 *pack_hash = NULL;
266 if (*packfile)
267 fclose(*packfile);
268 *packfile = NULL;
270 return err;
273 const struct got_error *
274 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
275 struct got_repository *repo,
276 got_pack_index_progress_cb progress_cb, void *progress_arg,
277 got_cancel_cb cancel_cb, void *cancel_arg)
279 size_t i;
280 char *path;
281 int imsg_idxfds[2];
282 int npackfd = -1, idxfd = -1, nidxfd = -1;
283 int tmpfds[3];
284 int idxstatus, done = 0;
285 const struct got_error *err;
286 struct imsgbuf idxibuf;
287 pid_t idxpid;
288 char *tmpidxpath = NULL;
289 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
290 const char *repo_path = got_repo_get_path_git_dir(repo);
291 struct stat sb;
293 for (i = 0; i < nitems(tmpfds); i++)
294 tmpfds[i] = -1;
296 if (asprintf(&path, "%s/%s/indexing.idx",
297 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
298 err = got_error_from_errno("asprintf");
299 goto done;
301 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
302 free(path);
303 if (err)
304 goto done;
305 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
306 err = got_error_from_errno2("fchmod", tmpidxpath);
307 goto done;
310 nidxfd = dup(idxfd);
311 if (nidxfd == -1) {
312 err = got_error_from_errno("dup");
313 goto done;
316 for (i = 0; i < nitems(tmpfds); i++) {
317 tmpfds[i] = got_opentempfd();
318 if (tmpfds[i] == -1) {
319 err = got_error_from_errno("got_opentempfd");
320 goto done;
324 err = got_object_id_str(&id_str, pack_hash);
325 if (err)
326 goto done;
328 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
329 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
330 err = got_error_from_errno("asprintf");
331 goto done;
334 if (fstat(fileno(packfile), &sb) == -1) {
335 err = got_error_from_errno2("fstat", packfile_path);
336 goto done;
339 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
340 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
341 err = got_error_from_errno("asprintf");
342 goto done;
345 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
346 err = got_error_from_errno("socketpair");
347 goto done;
349 idxpid = fork();
350 if (idxpid == -1) {
351 err= got_error_from_errno("fork");
352 goto done;
353 } else if (idxpid == 0)
354 got_privsep_exec_child(imsg_idxfds,
355 GOT_PATH_PROG_INDEX_PACK, packfile_path);
356 if (close(imsg_idxfds[1]) == -1) {
357 err = got_error_from_errno("close");
358 goto done;
360 imsg_init(&idxibuf, imsg_idxfds[0]);
362 npackfd = dup(fileno(packfile));
363 if (npackfd == -1) {
364 err = got_error_from_errno("dup");
365 goto done;
367 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->hash,
368 npackfd);
369 if (err != NULL)
370 goto done;
371 npackfd = -1;
372 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
373 if (err != NULL)
374 goto done;
375 nidxfd = -1;
376 for (i = 0; i < nitems(tmpfds); i++) {
377 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
378 if (err != NULL)
379 goto done;
380 tmpfds[i] = -1;
382 done = 0;
383 while (!done) {
384 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
386 if (cancel_cb) {
387 err = cancel_cb(cancel_arg);
388 if (err)
389 goto done;
392 err = got_privsep_recv_index_progress(&done, &nobj_total,
393 &nobj_indexed, &nobj_loose, &nobj_resolved,
394 &idxibuf);
395 if (err != NULL)
396 goto done;
397 if (nobj_indexed != 0) {
398 err = progress_cb(progress_arg, sb.st_size,
399 nobj_total, nobj_indexed, nobj_loose,
400 nobj_resolved);
401 if (err)
402 break;
405 if (close(imsg_idxfds[0]) == -1) {
406 err = got_error_from_errno("close");
407 goto done;
409 if (waitpid(idxpid, &idxstatus, 0) == -1) {
410 err = got_error_from_errno("waitpid");
411 goto done;
414 if (rename(tmpidxpath, idxpath) == -1) {
415 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
416 goto done;
418 free(tmpidxpath);
419 tmpidxpath = NULL;
421 done:
422 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
423 err = got_error_from_errno2("unlink", tmpidxpath);
424 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
425 err = got_error_from_errno("close");
426 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
427 err = got_error_from_errno("close");
428 for (i = 0; i < nitems(tmpfds); i++) {
429 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
430 err = got_error_from_errno("close");
432 free(tmpidxpath);
433 free(idxpath);
434 free(packfile_path);
435 return err;
438 const struct got_error *
439 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
440 struct got_repository *repo, const char *packfile_path)
442 const struct got_error *err = NULL;
443 const char *packdir_path = NULL;
444 char *packfile_name = NULL, *p, *dot;
445 struct got_object_id id;
446 int packfd = -1;
448 *packfile = NULL;
449 *pack_hash = NULL;
451 packdir_path = got_repo_get_path_objects_pack(repo);
452 if (packdir_path == NULL)
453 return got_error_from_errno("got_repo_get_path_objects_pack");
455 if (!got_path_is_child(packfile_path, packdir_path,
456 strlen(packdir_path))) {
457 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
458 goto done;
462 err = got_path_basename(&packfile_name, packfile_path);
463 if (err)
464 goto done;
465 p = packfile_name;
467 if (strncmp(p, "pack-", 5) != 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 p += 5;
474 dot = strchr(p, '.');
475 if (dot == NULL) {
476 err = got_error_fmt(GOT_ERR_BAD_PATH,
477 "'%s' is not a valid pack file name",
478 packfile_name);
479 goto done;
481 if (strcmp(dot + 1, "pack") != 0) {
482 err = got_error_fmt(GOT_ERR_BAD_PATH,
483 "'%s' is not a valid pack file name",
484 packfile_name);
485 goto done;
487 *dot = '\0';
488 if (!got_parse_sha1_digest(id.hash, p)) {
489 err = got_error_fmt(GOT_ERR_BAD_PATH,
490 "'%s' is not a valid pack file name",
491 packfile_name);
492 goto done;
495 *pack_hash = got_object_id_dup(&id);
496 if (*pack_hash == NULL) {
497 err = got_error_from_errno("got_object_id_dup");
498 goto done;
501 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
502 if (packfd == -1) {
503 err = got_error_from_errno2("open", packfile_path);
504 goto done;
507 *packfile = fdopen(packfd, "r");
508 if (*packfile == NULL) {
509 err = got_error_from_errno2("fdopen", packfile_path);
510 goto done;
512 packfd = -1;
513 done:
514 if (packfd != -1 && close(packfd) == -1 && err == NULL)
515 err = got_error_from_errno2("close", packfile_path);
516 free(packfile_name);
517 if (err) {
518 free(*pack_hash);
519 *pack_hash = NULL;
521 return err;
524 const struct got_error *
525 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
526 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
527 got_cancel_cb cancel_cb, void *cancel_arg)
529 const struct got_error *err = NULL;
530 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
531 struct got_packidx *packidx = NULL;
532 struct got_pack *pack = NULL;
533 uint32_t nobj, i;
535 err = got_object_id_str(&id_str, pack_hash);
536 if (err)
537 goto done;
539 if (asprintf(&packpath, "%s/pack-%s.pack",
540 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
541 err = got_error_from_errno("asprintf");
542 goto done;
544 if (asprintf(&idxpath, "%s/pack-%s.idx",
545 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
546 err = got_error_from_errno("asprintf");
547 goto done;
550 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1,
551 repo->algo);
552 if (err)
553 goto done;
555 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
556 if (err)
557 goto done;
559 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
560 for (i = 0; i < nobj; i++) {
561 struct got_packidx_object_id *oid;
562 struct got_object_id id, base_id;
563 off_t offset, base_offset = 0;
564 uint8_t type;
565 uint64_t size;
566 size_t tslen, len;
568 if (cancel_cb) {
569 err = cancel_cb(cancel_arg);
570 if (err)
571 break;
573 oid = &packidx->hdr.sorted_ids[i];
574 memcpy(id.hash, oid->hash, SHA1_DIGEST_LENGTH);
576 offset = got_packidx_get_object_offset(packidx, i);
577 if (offset == -1) {
578 err = got_error(GOT_ERR_BAD_PACKIDX);
579 goto done;
582 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
583 pack, offset);
584 if (err)
585 goto done;
587 switch (type) {
588 case GOT_OBJ_TYPE_OFFSET_DELTA:
589 err = got_pack_parse_offset_delta(&base_offset, &len,
590 pack, offset, tslen);
591 if (err)
592 goto done;
593 break;
594 case GOT_OBJ_TYPE_REF_DELTA:
595 err = got_pack_parse_ref_delta(&base_id,
596 pack, offset, tslen);
597 if (err)
598 goto done;
599 break;
601 err = (*list_cb)(list_arg, &id, type, offset, size,
602 base_offset, &base_id);
603 if (err)
604 goto done;
607 done:
608 free(id_str);
609 free(idxpath);
610 free(packpath);
611 if (packidx)
612 got_packidx_close(packidx);
613 return err;
616 static const struct got_error *
617 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
618 void *progress_arg, struct got_ratelimit *rl,
619 int nloose, int ncommits, int npurged)
621 const struct got_error *err;
622 int elapsed;
624 if (progress_cb == NULL)
625 return NULL;
627 err = got_ratelimit_check(&elapsed, rl);
628 if (err || !elapsed)
629 return err;
631 return progress_cb(progress_arg, nloose, ncommits, npurged);
634 static const struct got_error *
635 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
636 got_cleanup_progress_cb progress_cb, void *progress_arg,
637 struct got_ratelimit *rl, struct got_repository *repo)
639 const struct got_error *err = NULL;
640 char *path_objects = NULL, *path = NULL;
641 DIR *dir = NULL;
642 struct got_object *obj = NULL;
643 struct got_object_id id;
644 int i, fd = -1;
645 struct stat sb;
647 *ondisk_size = 0;
648 *loose_ids = got_object_idset_alloc();
649 if (*loose_ids == NULL)
650 return got_error_from_errno("got_object_idset_alloc");
652 path_objects = got_repo_get_path_objects(repo);
653 if (path_objects == NULL) {
654 err = got_error_from_errno("got_repo_get_path_objects");
655 goto done;
658 for (i = 0; i <= 0xff; i++) {
659 struct dirent *dent;
661 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
662 err = got_error_from_errno("asprintf");
663 break;
666 dir = opendir(path);
667 if (dir == NULL) {
668 if (errno == ENOENT) {
669 err = NULL;
670 continue;
672 err = got_error_from_errno2("opendir", path);
673 break;
676 while ((dent = readdir(dir)) != NULL) {
677 char *id_str;
679 if (strcmp(dent->d_name, ".") == 0 ||
680 strcmp(dent->d_name, "..") == 0)
681 continue;
683 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
684 err = got_error_from_errno("asprintf");
685 goto done;
688 memset(&id, 0, sizeof(id));
689 if (!got_parse_sha1_digest(id.hash, id_str)) {
690 free(id_str);
691 continue;
693 free(id_str);
695 err = got_object_open_loose_fd(&fd, &id, repo);
696 if (err)
697 goto done;
698 if (fstat(fd, &sb) == -1) {
699 err = got_error_from_errno("fstat");
700 goto done;
702 err = got_object_read_header_privsep(&obj, &id, repo,
703 fd);
704 if (err)
705 goto done;
706 fd = -1; /* already closed */
708 switch (obj->type) {
709 case GOT_OBJ_TYPE_COMMIT:
710 case GOT_OBJ_TYPE_TREE:
711 case GOT_OBJ_TYPE_BLOB:
712 case GOT_OBJ_TYPE_TAG:
713 break;
714 default:
715 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
716 "%d", obj->type);
717 goto done;
719 got_object_close(obj);
720 obj = NULL;
721 (*ondisk_size) += sb.st_size;
722 err = got_object_idset_add(*loose_ids, &id, NULL);
723 if (err)
724 goto done;
725 err = report_cleanup_progress(progress_cb,
726 progress_arg, rl,
727 got_object_idset_num_elements(*loose_ids), -1, -1);
728 if (err)
729 goto done;
732 if (closedir(dir) != 0) {
733 err = got_error_from_errno("closedir");
734 goto done;
736 dir = NULL;
738 free(path);
739 path = NULL;
741 done:
742 if (dir && closedir(dir) != 0 && err == NULL)
743 err = got_error_from_errno("closedir");
744 if (fd != -1 && close(fd) == -1 && err == NULL)
745 err = got_error_from_errno("close");
746 if (err) {
747 got_object_idset_free(*loose_ids);
748 *loose_ids = NULL;
750 if (obj)
751 got_object_close(obj);
752 free(path_objects);
753 free(path);
754 return err;
757 static const struct got_error *
758 preserve_loose_object(struct got_object_idset *loose_ids,
759 struct got_object_id *id, struct got_repository *repo, int *npacked)
761 const struct got_error *err = NULL;
762 struct got_object *obj;
764 if (!got_object_idset_contains(loose_ids, id))
765 return NULL;
767 /*
768 * Try to open this object from a pack file. This ensures that
769 * we do in fact have a valid packed copy of the object. Otherwise
770 * we should not delete the loose representation of this object.
771 */
772 err = got_object_open_packed(&obj, id, repo);
773 if (err == NULL) {
774 got_object_close(obj);
775 /*
776 * The object is referenced and packed.
777 * We can purge the redundantly stored loose object.
778 */
779 (*npacked)++;
780 return NULL;
781 } else if (err->code != GOT_ERR_NO_OBJ)
782 return err;
784 /*
785 * This object is referenced and not packed.
786 * Remove it from our purge set.
787 */
788 return got_object_idset_remove(NULL, loose_ids, id);
791 static const struct got_error *
792 load_tree_entries(struct got_object_id_queue *ids,
793 struct got_object_idset *loose_ids,
794 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
795 const char *dpath, struct got_repository *repo, int *npacked,
796 got_cancel_cb cancel_cb, void *cancel_arg)
798 const struct got_error *err;
799 struct got_tree_object *tree;
800 char *p = NULL;
801 int i;
803 err = got_object_open_as_tree(&tree, repo, tree_id);
804 if (err)
805 return err;
807 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
808 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
809 struct got_object_id *id = got_tree_entry_get_id(e);
810 mode_t mode = got_tree_entry_get_mode(e);
812 if (cancel_cb) {
813 err = (*cancel_cb)(cancel_arg);
814 if (err)
815 break;
818 if (got_object_tree_entry_is_symlink(e) ||
819 got_object_tree_entry_is_submodule(e) ||
820 got_object_idset_contains(traversed_ids, id))
821 continue;
823 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
824 got_tree_entry_get_name(e)) == -1) {
825 err = got_error_from_errno("asprintf");
826 break;
829 if (S_ISDIR(mode)) {
830 struct got_object_qid *qid;
831 err = got_object_qid_alloc(&qid, id);
832 if (err)
833 break;
834 STAILQ_INSERT_TAIL(ids, qid, entry);
835 } else if (S_ISREG(mode)) {
836 /* This blob is referenced. */
837 err = preserve_loose_object(loose_ids, id, repo,
838 npacked);
839 if (err)
840 break;
841 err = got_object_idset_add(traversed_ids, id, NULL);
842 if (err)
843 break;
845 free(p);
846 p = NULL;
849 got_object_tree_close(tree);
850 free(p);
851 return err;
854 static const struct got_error *
855 load_tree(struct got_object_idset *loose_ids,
856 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
857 const char *dpath, struct got_repository *repo, int *npacked,
858 got_cancel_cb cancel_cb, void *cancel_arg)
860 const struct got_error *err = NULL;
861 struct got_object_id_queue tree_ids;
862 struct got_object_qid *qid;
864 err = got_object_qid_alloc(&qid, tree_id);
865 if (err)
866 return err;
868 STAILQ_INIT(&tree_ids);
869 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
871 while (!STAILQ_EMPTY(&tree_ids)) {
872 if (cancel_cb) {
873 err = (*cancel_cb)(cancel_arg);
874 if (err)
875 break;
878 qid = STAILQ_FIRST(&tree_ids);
879 STAILQ_REMOVE_HEAD(&tree_ids, entry);
881 if (got_object_idset_contains(traversed_ids, &qid->id)) {
882 got_object_qid_free(qid);
883 continue;
886 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
887 if (err) {
888 got_object_qid_free(qid);
889 break;
892 /* This tree is referenced. */
893 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
894 if (err)
895 break;
897 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
898 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
899 got_object_qid_free(qid);
900 if (err)
901 break;
904 got_object_id_queue_free(&tree_ids);
905 return err;
908 static const struct got_error *
909 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
910 int *npacked, struct got_object_idset *traversed_ids,
911 struct got_object_id *id, struct got_repository *repo,
912 got_cleanup_progress_cb progress_cb, void *progress_arg,
913 struct got_ratelimit *rl, int nloose,
914 got_cancel_cb cancel_cb, void *cancel_arg)
916 const struct got_error *err;
917 struct got_commit_object *commit = NULL;
918 struct got_tag_object *tag = NULL;
919 struct got_object_id *tree_id = NULL;
920 struct got_object_id_queue ids;
921 struct got_object_qid *qid;
922 int obj_type;
924 err = got_object_qid_alloc(&qid, id);
925 if (err)
926 return err;
928 STAILQ_INIT(&ids);
929 STAILQ_INSERT_TAIL(&ids, qid, entry);
931 while (!STAILQ_EMPTY(&ids)) {
932 if (cancel_cb) {
933 err = (*cancel_cb)(cancel_arg);
934 if (err)
935 break;
938 qid = STAILQ_FIRST(&ids);
939 STAILQ_REMOVE_HEAD(&ids, entry);
941 if (got_object_idset_contains(traversed_ids, &qid->id)) {
942 got_object_qid_free(qid);
943 qid = NULL;
944 continue;
947 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
948 if (err)
949 break;
951 /* This commit or tag is referenced. */
952 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
953 if (err)
954 break;
956 err = got_object_get_type(&obj_type, repo, &qid->id);
957 if (err)
958 break;
959 switch (obj_type) {
960 case GOT_OBJ_TYPE_COMMIT:
961 err = got_object_open_as_commit(&commit, repo,
962 &qid->id);
963 if (err)
964 goto done;
965 break;
966 case GOT_OBJ_TYPE_TAG:
967 err = got_object_open_as_tag(&tag, repo, &qid->id);
968 if (err)
969 goto done;
970 break;
971 default:
972 /* should not happen */
973 err = got_error(GOT_ERR_OBJ_TYPE);
974 goto done;
977 /* Find a tree object to scan. */
978 if (commit) {
979 tree_id = got_object_commit_get_tree_id(commit);
980 } else if (tag) {
981 obj_type = got_object_tag_get_object_type(tag);
982 switch (obj_type) {
983 case GOT_OBJ_TYPE_COMMIT:
984 err = got_object_open_as_commit(&commit, repo,
985 got_object_tag_get_object_id(tag));
986 if (err)
987 goto done;
988 tree_id = got_object_commit_get_tree_id(commit);
989 break;
990 case GOT_OBJ_TYPE_TREE:
991 tree_id = got_object_tag_get_object_id(tag);
992 break;
993 default:
994 /*
995 * Tag points at something other than a
996 * commit or tree. Leave this weird tag object
997 * and the object it points to on disk.
998 */
999 err = got_object_idset_remove(NULL, loose_ids,
1000 &qid->id);
1001 if (err && err->code != GOT_ERR_NO_OBJ)
1002 goto done;
1003 err = got_object_idset_remove(NULL, loose_ids,
1004 got_object_tag_get_object_id(tag));
1005 if (err && err->code != GOT_ERR_NO_OBJ)
1006 goto done;
1007 err = NULL;
1008 break;
1012 if (tree_id) {
1013 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1014 repo, npacked, cancel_cb, cancel_arg);
1015 if (err)
1016 break;
1019 if (commit || tag)
1020 (*ncommits)++; /* scanned tags are counted as commits */
1022 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1023 nloose, *ncommits, -1);
1024 if (err)
1025 break;
1027 if (commit) {
1028 /* Find parent commits to scan. */
1029 const struct got_object_id_queue *parent_ids;
1030 parent_ids = got_object_commit_get_parent_ids(commit);
1031 err = got_object_id_queue_copy(parent_ids, &ids);
1032 if (err)
1033 break;
1034 got_object_commit_close(commit);
1035 commit = NULL;
1037 if (tag) {
1038 got_object_tag_close(tag);
1039 tag = NULL;
1041 got_object_qid_free(qid);
1042 qid = NULL;
1044 done:
1045 if (qid)
1046 got_object_qid_free(qid);
1047 if (commit)
1048 got_object_commit_close(commit);
1049 if (tag)
1050 got_object_tag_close(tag);
1051 got_object_id_queue_free(&ids);
1052 return err;
1055 struct purge_loose_object_arg {
1056 struct got_repository *repo;
1057 got_cleanup_progress_cb progress_cb;
1058 void *progress_arg;
1059 struct got_ratelimit *rl;
1060 int nloose;
1061 int ncommits;
1062 int npurged;
1063 off_t size_purged;
1064 int dry_run;
1065 time_t max_mtime;
1066 int ignore_mtime;
1069 static const struct got_error *
1070 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1072 struct purge_loose_object_arg *a = arg;
1073 const struct got_error *err, *unlock_err = NULL;
1074 char *path = NULL;
1075 int fd = -1;
1076 struct stat sb;
1077 struct got_lockfile *lf = NULL;
1079 err = got_object_get_path(&path, id, a->repo);
1080 if (err)
1081 return err;
1083 err = got_object_open_loose_fd(&fd, id, a->repo);
1084 if (err)
1085 goto done;
1087 if (fstat(fd, &sb) == -1) {
1088 err = got_error_from_errno("fstat");
1089 goto done;
1093 * Do not delete objects which are younger than our maximum
1094 * modification time threshold. This prevents a race where
1095 * new objects which are being added to the repository
1096 * concurrently would be deleted.
1098 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1099 if (!a->dry_run) {
1100 err = got_lockfile_lock(&lf, path, -1);
1101 if (err)
1102 goto done;
1103 if (unlink(path) == -1) {
1104 err = got_error_from_errno2("unlink", path);
1105 goto done;
1109 a->npurged++;
1110 a->size_purged += sb.st_size;
1111 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1112 a->rl, a->nloose, a->ncommits, a->npurged);
1113 if (err)
1114 goto done;
1116 done:
1117 if (fd != -1 && close(fd) == -1 && err == NULL)
1118 err = got_error_from_errno("close");
1119 free(path);
1120 if (lf)
1121 unlock_err = got_lockfile_unlock(lf, -1);
1122 return err ? err : unlock_err;
1125 const struct got_error *
1126 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1127 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1128 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1129 got_cancel_cb cancel_cb, void *cancel_arg)
1131 const struct got_error *err;
1132 struct got_object_idset *loose_ids;
1133 struct got_object_idset *traversed_ids;
1134 struct got_object_id **referenced_ids;
1135 int i, nreferenced, nloose, ncommits = 0;
1136 struct got_reflist_head refs;
1137 struct got_reflist_entry *re;
1138 struct purge_loose_object_arg arg;
1139 time_t max_mtime = 0;
1140 struct got_ratelimit rl;
1142 TAILQ_INIT(&refs);
1143 got_ratelimit_init(&rl, 0, 500);
1145 *size_before = 0;
1146 *size_after = 0;
1147 *npacked = 0;
1149 err = get_loose_object_ids(&loose_ids, size_before,
1150 progress_cb, progress_arg, &rl, repo);
1151 if (err)
1152 return err;
1153 nloose = got_object_idset_num_elements(loose_ids);
1154 if (nloose == 0) {
1155 got_object_idset_free(loose_ids);
1156 if (progress_cb) {
1157 err = progress_cb(progress_arg, 0, 0, 0);
1158 if (err)
1159 return err;
1161 return NULL;
1164 traversed_ids = got_object_idset_alloc();
1165 if (traversed_ids == NULL) {
1166 err = got_error_from_errno("got_object_idset_alloc");
1167 goto done;
1170 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1171 if (err)
1172 goto done;
1173 if (!ignore_mtime) {
1174 TAILQ_FOREACH(re, &refs, entry) {
1175 time_t mtime = got_ref_get_mtime(re->ref);
1176 if (mtime > max_mtime)
1177 max_mtime = mtime;
1180 * For safety, keep objects created within 10 minutes
1181 * before the youngest reference was created.
1183 if (max_mtime >= 600)
1184 max_mtime -= 600;
1187 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1188 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1189 &refs, repo, cancel_cb, cancel_arg);
1190 if (err)
1191 goto done;
1193 for (i = 0; i < nreferenced; i++) {
1194 struct got_object_id *id = referenced_ids[i];
1195 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1196 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1197 nloose, cancel_cb, cancel_arg);
1198 if (err)
1199 goto done;
1202 /* Any remaining loose objects are unreferenced and can be purged. */
1203 arg.repo = repo;
1204 arg.progress_arg = progress_arg;
1205 arg.progress_cb = progress_cb;
1206 arg.rl = &rl;
1207 arg.nloose = nloose;
1208 arg.npurged = 0;
1209 arg.size_purged = 0;
1210 arg.ncommits = ncommits;
1211 arg.dry_run = dry_run;
1212 arg.max_mtime = max_mtime;
1213 arg.ignore_mtime = ignore_mtime;
1214 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1215 if (err)
1216 goto done;
1217 *size_after = *size_before - arg.size_purged;
1219 /* Produce a final progress report. */
1220 if (progress_cb) {
1221 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1222 if (err)
1223 goto done;
1225 done:
1226 got_object_idset_free(loose_ids);
1227 got_object_idset_free(traversed_ids);
1228 return err;
1231 static const struct got_error *
1232 remove_packidx(int dir_fd, const char *relpath)
1234 const struct got_error *err, *unlock_err;
1235 struct got_lockfile *lf;
1237 err = got_lockfile_lock(&lf, relpath, dir_fd);
1238 if (err)
1239 return err;
1240 if (unlinkat(dir_fd, relpath, 0) == -1)
1241 err = got_error_from_errno("unlinkat");
1242 unlock_err = got_lockfile_unlock(lf, dir_fd);
1243 return err ? err : unlock_err;
1246 const struct got_error *
1247 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1248 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1249 got_cancel_cb cancel_cb, void *cancel_arg)
1251 const struct got_error *err = NULL;
1252 DIR *packdir = NULL;
1253 struct dirent *dent;
1254 char *pack_relpath = NULL;
1255 int packdir_fd;
1256 struct stat sb;
1258 packdir_fd = openat(got_repo_get_fd(repo),
1259 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1260 if (packdir_fd == -1) {
1261 if (errno == ENOENT)
1262 return NULL;
1263 return got_error_from_errno_fmt("openat: %s/%s",
1264 got_repo_get_path_git_dir(repo),
1265 GOT_OBJECTS_PACK_DIR);
1268 packdir = fdopendir(packdir_fd);
1269 if (packdir == NULL) {
1270 err = got_error_from_errno("fdopendir");
1271 goto done;
1274 while ((dent = readdir(packdir)) != NULL) {
1275 if (cancel_cb) {
1276 err = cancel_cb(cancel_arg);
1277 if (err)
1278 goto done;
1281 if (!got_repo_is_packidx_filename(repo, dent->d_name,
1282 dent->d_namlen))
1283 continue;
1285 err = got_packidx_get_packfile_path(&pack_relpath,
1286 dent->d_name);
1287 if (err)
1288 goto done;
1290 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1291 free(pack_relpath);
1292 pack_relpath = NULL;
1293 continue;
1295 if (errno != ENOENT) {
1296 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1297 got_repo_get_path_git_dir(repo),
1298 GOT_OBJECTS_PACK_DIR,
1299 pack_relpath);
1300 goto done;
1303 if (!dry_run) {
1304 err = remove_packidx(packdir_fd, dent->d_name);
1305 if (err)
1306 goto done;
1308 if (progress_cb) {
1309 char *path;
1310 if (asprintf(&path, "%s/%s/%s",
1311 got_repo_get_path_git_dir(repo),
1312 GOT_OBJECTS_PACK_DIR,
1313 dent->d_name) == -1) {
1314 err = got_error_from_errno("asprintf");
1315 goto done;
1317 err = progress_cb(progress_arg, path);
1318 free(path);
1319 if (err)
1320 goto done;
1322 free(pack_relpath);
1323 pack_relpath = NULL;
1325 done:
1326 if (packdir && closedir(packdir) != 0 && err == NULL)
1327 err = got_error_from_errno("closedir");
1328 free(pack_relpath);
1329 return err;