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_sha1.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,
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_FILE_MODE) != 0) {
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, 0,
209 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
210 if (err)
211 goto done;
213 err = got_object_id_str(&sha1_str, *pack_hash);
214 if (err)
215 goto done;
216 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
217 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
218 sha1_str) == -1) {
219 err = got_error_from_errno("asprintf");
220 goto done;
223 if (lseek(packfd, 0L, SEEK_SET) == -1) {
224 err = got_error_from_errno("lseek");
225 goto done;
227 if (rename(tmpfile_path, packfile_path) == -1) {
228 err = got_error_from_errno3("rename", tmpfile_path,
229 packfile_path);
230 goto done;
232 free(tmpfile_path);
233 tmpfile_path = NULL;
235 *packfile = fdopen(packfd, "w");
236 if (*packfile == NULL) {
237 err = got_error_from_errno2("fdopen", tmpfile_path);
238 goto done;
240 packfd = -1;
241 done:
242 for (i = 0; i < nours; i++)
243 free(ours[i]);
244 free(ours);
245 for (i = 0; i < ntheirs; i++)
246 free(theirs[i]);
247 free(theirs);
248 if (packfd != -1 && close(packfd) == -1 && err == NULL)
249 err = got_error_from_errno2("close", packfile_path);
250 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
251 err = got_error_from_errno("fclose");
252 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
253 err = got_error_from_errno2("unlink", tmpfile_path);
254 free(tmpfile_path);
255 free(packfile_path);
256 free(sha1_str);
257 free(path);
258 if (err) {
259 free(*pack_hash);
260 *pack_hash = NULL;
261 if (*packfile)
262 fclose(*packfile);
263 *packfile = NULL;
265 return err;
268 const struct got_error *
269 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
270 struct got_repository *repo,
271 got_pack_index_progress_cb progress_cb, void *progress_arg,
272 got_cancel_cb cancel_cb, void *cancel_arg)
274 size_t i;
275 char *path;
276 int imsg_idxfds[2];
277 int npackfd = -1, idxfd = -1, nidxfd = -1;
278 int tmpfds[3];
279 int idxstatus, done = 0;
280 const struct got_error *err;
281 struct imsgbuf idxibuf;
282 pid_t idxpid;
283 char *tmpidxpath = NULL;
284 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
285 const char *repo_path = got_repo_get_path_git_dir(repo);
286 struct stat sb;
288 for (i = 0; i < nitems(tmpfds); i++)
289 tmpfds[i] = -1;
291 if (asprintf(&path, "%s/%s/indexing.idx",
292 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
294 goto done;
296 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
297 free(path);
298 if (err)
299 goto done;
300 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
301 err = got_error_from_errno2("fchmod", tmpidxpath);
302 goto done;
305 nidxfd = dup(idxfd);
306 if (nidxfd == -1) {
307 err = got_error_from_errno("dup");
308 goto done;
311 for (i = 0; i < nitems(tmpfds); i++) {
312 tmpfds[i] = got_opentempfd();
313 if (tmpfds[i] == -1) {
314 err = got_error_from_errno("got_opentempfd");
315 goto done;
319 err = got_object_id_str(&id_str, pack_hash);
320 if (err)
321 goto done;
323 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
324 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
325 err = got_error_from_errno("asprintf");
326 goto done;
329 if (fstat(fileno(packfile), &sb) == -1) {
330 err = got_error_from_errno2("fstat", packfile_path);
331 goto done;
334 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
335 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
336 err = got_error_from_errno("asprintf");
337 goto done;
340 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
341 err = got_error_from_errno("socketpair");
342 goto done;
344 idxpid = fork();
345 if (idxpid == -1) {
346 err= got_error_from_errno("fork");
347 goto done;
348 } else if (idxpid == 0)
349 got_privsep_exec_child(imsg_idxfds,
350 GOT_PATH_PROG_INDEX_PACK, packfile_path);
351 if (close(imsg_idxfds[1]) == -1) {
352 err = got_error_from_errno("close");
353 goto done;
355 imsg_init(&idxibuf, imsg_idxfds[0]);
357 npackfd = dup(fileno(packfile));
358 if (npackfd == -1) {
359 err = got_error_from_errno("dup");
360 goto done;
362 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
363 npackfd);
364 if (err != NULL)
365 goto done;
366 npackfd = -1;
367 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
368 if (err != NULL)
369 goto done;
370 nidxfd = -1;
371 for (i = 0; i < nitems(tmpfds); i++) {
372 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
373 if (err != NULL)
374 goto done;
375 tmpfds[i] = -1;
377 done = 0;
378 while (!done) {
379 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
381 if (cancel_cb) {
382 err = cancel_cb(cancel_arg);
383 if (err)
384 goto done;
387 err = got_privsep_recv_index_progress(&done, &nobj_total,
388 &nobj_indexed, &nobj_loose, &nobj_resolved,
389 &idxibuf);
390 if (err != NULL)
391 goto done;
392 if (nobj_indexed != 0) {
393 err = progress_cb(progress_arg, sb.st_size,
394 nobj_total, nobj_indexed, nobj_loose,
395 nobj_resolved);
396 if (err)
397 break;
400 if (close(imsg_idxfds[0]) == -1) {
401 err = got_error_from_errno("close");
402 goto done;
404 if (waitpid(idxpid, &idxstatus, 0) == -1) {
405 err = got_error_from_errno("waitpid");
406 goto done;
409 if (rename(tmpidxpath, idxpath) == -1) {
410 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
411 goto done;
413 free(tmpidxpath);
414 tmpidxpath = NULL;
416 done:
417 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
418 err = got_error_from_errno2("unlink", tmpidxpath);
419 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
420 err = got_error_from_errno("close");
421 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
422 err = got_error_from_errno("close");
423 for (i = 0; i < nitems(tmpfds); i++) {
424 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
425 err = got_error_from_errno("close");
427 free(tmpidxpath);
428 free(idxpath);
429 free(packfile_path);
430 return err;
433 const struct got_error *
434 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
435 struct got_repository *repo, const char *packfile_path)
437 const struct got_error *err = NULL;
438 const char *packdir_path = NULL;
439 char *packfile_name = NULL, *p, *dot;
440 struct got_object_id id;
441 int packfd = -1;
443 *packfile = NULL;
444 *pack_hash = NULL;
446 packdir_path = got_repo_get_path_objects_pack(repo);
447 if (packdir_path == NULL)
448 return got_error_from_errno("got_repo_get_path_objects_pack");
450 if (!got_path_is_child(packfile_path, packdir_path,
451 strlen(packdir_path))) {
452 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
453 goto done;
457 err = got_path_basename(&packfile_name, packfile_path);
458 if (err)
459 goto done;
460 p = packfile_name;
462 if (strncmp(p, "pack-", 5) != 0) {
463 err = got_error_fmt(GOT_ERR_BAD_PATH,
464 "'%s' is not a valid pack file name",
465 packfile_name);
466 goto done;
468 p += 5;
469 dot = strchr(p, '.');
470 if (dot == NULL) {
471 err = got_error_fmt(GOT_ERR_BAD_PATH,
472 "'%s' is not a valid pack file name",
473 packfile_name);
474 goto done;
476 if (strcmp(dot + 1, "pack") != 0) {
477 err = got_error_fmt(GOT_ERR_BAD_PATH,
478 "'%s' is not a valid pack file name",
479 packfile_name);
480 goto done;
482 *dot = '\0';
483 if (!got_parse_sha1_digest(id.sha1, p)) {
484 err = got_error_fmt(GOT_ERR_BAD_PATH,
485 "'%s' is not a valid pack file name",
486 packfile_name);
487 goto done;
490 *pack_hash = got_object_id_dup(&id);
491 if (*pack_hash == NULL) {
492 err = got_error_from_errno("got_object_id_dup");
493 goto done;
496 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
497 if (packfd == -1) {
498 err = got_error_from_errno2("open", packfile_path);
499 goto done;
502 *packfile = fdopen(packfd, "r");
503 if (*packfile == NULL) {
504 err = got_error_from_errno2("fdopen", packfile_path);
505 goto done;
507 packfd = -1;
508 done:
509 if (packfd != -1 && close(packfd) == -1 && err == NULL)
510 err = got_error_from_errno2("close", packfile_path);
511 free(packfile_name);
512 if (err) {
513 free(*pack_hash);
514 *pack_hash = NULL;
516 return err;
519 const struct got_error *
520 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
521 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
522 got_cancel_cb cancel_cb, void *cancel_arg)
524 const struct got_error *err = NULL;
525 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
526 struct got_packidx *packidx = NULL;
527 struct got_pack *pack = NULL;
528 uint32_t nobj, i;
530 err = got_object_id_str(&id_str, pack_hash);
531 if (err)
532 goto done;
534 if (asprintf(&packpath, "%s/pack-%s.pack",
535 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
536 err = got_error_from_errno("asprintf");
537 goto done;
539 if (asprintf(&idxpath, "%s/pack-%s.idx",
540 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
541 err = got_error_from_errno("asprintf");
542 goto done;
545 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
546 if (err)
547 goto done;
549 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
550 if (err)
551 goto done;
553 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
554 for (i = 0; i < nobj; i++) {
555 struct got_packidx_object_id *oid;
556 struct got_object_id id, base_id;
557 off_t offset, base_offset = 0;
558 uint8_t type;
559 uint64_t size;
560 size_t tslen, len;
562 if (cancel_cb) {
563 err = cancel_cb(cancel_arg);
564 if (err)
565 break;
567 oid = &packidx->hdr.sorted_ids[i];
568 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
570 offset = got_packidx_get_object_offset(packidx, i);
571 if (offset == -1) {
572 err = got_error(GOT_ERR_BAD_PACKIDX);
573 goto done;
576 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
577 pack, offset);
578 if (err)
579 goto done;
581 switch (type) {
582 case GOT_OBJ_TYPE_OFFSET_DELTA:
583 err = got_pack_parse_offset_delta(&base_offset, &len,
584 pack, offset, tslen);
585 if (err)
586 goto done;
587 break;
588 case GOT_OBJ_TYPE_REF_DELTA:
589 err = got_pack_parse_ref_delta(&base_id,
590 pack, offset, tslen);
591 if (err)
592 goto done;
593 break;
595 err = (*list_cb)(list_arg, &id, type, offset, size,
596 base_offset, &base_id);
597 if (err)
598 goto done;
601 done:
602 free(id_str);
603 free(idxpath);
604 free(packpath);
605 if (packidx)
606 got_packidx_close(packidx);
607 return err;
610 static const struct got_error *
611 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
612 void *progress_arg, struct got_ratelimit *rl,
613 int nloose, int ncommits, int npurged)
615 const struct got_error *err;
616 int elapsed;
618 if (progress_cb == NULL)
619 return NULL;
621 err = got_ratelimit_check(&elapsed, rl);
622 if (err || !elapsed)
623 return err;
625 return progress_cb(progress_arg, nloose, ncommits, npurged);
628 static const struct got_error *
629 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
630 got_cleanup_progress_cb progress_cb, void *progress_arg,
631 struct got_ratelimit *rl, struct got_repository *repo)
633 const struct got_error *err = NULL;
634 char *path_objects = NULL, *path = NULL;
635 DIR *dir = NULL;
636 struct got_object *obj = NULL;
637 struct got_object_id id;
638 int i, fd = -1;
639 struct stat sb;
641 *ondisk_size = 0;
642 *loose_ids = got_object_idset_alloc();
643 if (*loose_ids == NULL)
644 return got_error_from_errno("got_object_idset_alloc");
646 path_objects = got_repo_get_path_objects(repo);
647 if (path_objects == NULL) {
648 err = got_error_from_errno("got_repo_get_path_objects");
649 goto done;
652 for (i = 0; i <= 0xff; i++) {
653 struct dirent *dent;
655 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
656 err = got_error_from_errno("asprintf");
657 break;
660 dir = opendir(path);
661 if (dir == NULL) {
662 if (errno == ENOENT) {
663 err = NULL;
664 continue;
666 err = got_error_from_errno2("opendir", path);
667 break;
670 while ((dent = readdir(dir)) != NULL) {
671 char *id_str;
673 if (strcmp(dent->d_name, ".") == 0 ||
674 strcmp(dent->d_name, "..") == 0)
675 continue;
677 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
678 err = got_error_from_errno("asprintf");
679 goto done;
682 memset(&id, 0, sizeof(id));
683 if (!got_parse_sha1_digest(id.sha1, id_str)) {
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;