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 <sha2.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <unistd.h>
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
42 #include "got_path.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_repository.h"
51 #include "got_lib_ratelimit.h"
52 #include "got_lib_pack_create.h"
53 #include "got_lib_hash.h"
54 #include "got_lib_lockfile.h"
56 #ifndef nitems
57 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 #endif
60 static const struct got_error *
61 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
62 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
63 struct got_repository *repo,
64 got_cancel_cb cancel_cb, void *cancel_arg)
65 {
66 const struct got_error *err = NULL;
67 const size_t alloc_chunksz = 256;
68 size_t nalloc;
69 struct got_reflist_entry *re;
70 int i;
72 *ids = NULL;
73 *nobjects = 0;
75 err = got_reflist_sort(refs,
76 got_ref_cmp_by_commit_timestamp_descending, repo);
77 if (err)
78 return err;
80 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
81 if (*ids == NULL)
82 return got_error_from_errno("reallocarray");
83 nalloc = alloc_chunksz;
85 TAILQ_FOREACH(re, refs, entry) {
86 struct got_object_id *id;
88 if (cancel_cb) {
89 err = cancel_cb(cancel_arg);
90 if (err)
91 goto done;
92 }
94 err = got_ref_resolve(&id, repo, re->ref);
95 if (err)
96 goto done;
98 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
99 int obj_type;
100 err = got_object_get_type(&obj_type, repo, id);
101 if (err)
102 goto done;
103 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
104 free(id);
105 id = NULL;
106 continue;
110 if (nalloc <= *nobjects) {
111 struct got_object_id **new;
112 new = recallocarray(*ids, nalloc,
113 nalloc + alloc_chunksz,
114 sizeof(struct got_object_id *));
115 if (new == NULL) {
116 err = got_error_from_errno(
117 "recallocarray");
118 goto done;
120 *ids = new;
121 nalloc += alloc_chunksz;
123 (*ids)[*nobjects] = id;
124 if ((*ids)[*nobjects] == NULL) {
125 err = got_error_from_errno("got_object_id_dup");
126 goto done;
128 (*nobjects)++;
130 done:
131 if (err) {
132 for (i = 0; i < *nobjects; i++)
133 free((*ids)[i]);
134 free(*ids);
135 *ids = NULL;
136 *nobjects = 0;
138 return err;
141 const struct got_error *
142 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
143 struct got_reflist_head *include_refs,
144 struct got_reflist_head *exclude_refs, struct got_repository *repo,
145 int loose_obj_only, int force_refdelta,
146 got_pack_progress_cb progress_cb, void *progress_arg,
147 got_cancel_cb cancel_cb, void *cancel_arg)
149 const struct got_error *err = NULL;
150 struct got_object_id **ours = NULL, **theirs = NULL;
151 int nours = 0, ntheirs = 0, packfd = -1, i;
152 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
153 char *sha1_str = NULL;
154 FILE *delta_cache = NULL;
155 struct got_ratelimit rl;
157 *packfile = NULL;
158 *pack_hash = NULL;
160 got_ratelimit_init(&rl, 0, 500);
162 if (asprintf(&path, "%s/%s/packing.pack",
163 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
164 err = got_error_from_errno("asprintf");
165 goto done;
167 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
168 if (err)
169 goto done;
171 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
172 err = got_error_from_errno2("fchmod", tmpfile_path);
173 goto done;
176 delta_cache = got_opentemp();
177 if (delta_cache == NULL) {
178 err = got_error_from_errno("got_opentemp");
179 goto done;
182 err = get_reflist_object_ids(&ours, &nours,
183 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
184 include_refs, repo, cancel_cb, cancel_arg);
185 if (err)
186 goto done;
188 if (nours == 0) {
189 err = got_error(GOT_ERR_CANNOT_PACK);
190 goto done;
193 if (!TAILQ_EMPTY(exclude_refs)) {
194 err = get_reflist_object_ids(&theirs, &ntheirs,
195 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
196 exclude_refs, repo,
197 cancel_cb, cancel_arg);
198 if (err)
199 goto done;
202 *pack_hash = calloc(1, sizeof(**pack_hash));
203 if (*pack_hash == NULL) {
204 err = got_error_from_errno("calloc");
205 goto done;
208 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
209 theirs, ntheirs, ours, nours, repo, loose_obj_only,
210 0, force_refdelta, progress_cb, progress_arg, &rl,
211 cancel_cb, cancel_arg);
212 if (err)
213 goto done;
215 err = got_object_id_str(&sha1_str, *pack_hash);
216 if (err)
217 goto done;
218 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
219 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
220 sha1_str) == -1) {
221 err = got_error_from_errno("asprintf");
222 goto done;
225 if (lseek(packfd, 0L, SEEK_SET) == -1) {
226 err = got_error_from_errno("lseek");
227 goto done;
229 if (rename(tmpfile_path, packfile_path) == -1) {
230 err = got_error_from_errno3("rename", tmpfile_path,
231 packfile_path);
232 goto done;
234 free(tmpfile_path);
235 tmpfile_path = NULL;
237 *packfile = fdopen(packfd, "w");
238 if (*packfile == NULL) {
239 err = got_error_from_errno2("fdopen", tmpfile_path);
240 goto done;
242 packfd = -1;
243 done:
244 for (i = 0; i < nours; i++)
245 free(ours[i]);
246 free(ours);
247 for (i = 0; i < ntheirs; i++)
248 free(theirs[i]);
249 free(theirs);
250 if (packfd != -1 && close(packfd) == -1 && err == NULL)
251 err = got_error_from_errno2("close", packfile_path);
252 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
253 err = got_error_from_errno("fclose");
254 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
255 err = got_error_from_errno2("unlink", tmpfile_path);
256 free(tmpfile_path);
257 free(packfile_path);
258 free(sha1_str);
259 free(path);
260 if (err) {
261 free(*pack_hash);
262 *pack_hash = NULL;
263 if (*packfile)
264 fclose(*packfile);
265 *packfile = NULL;
267 return err;
270 const struct got_error *
271 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
272 struct got_repository *repo,
273 got_pack_index_progress_cb progress_cb, void *progress_arg,
274 got_cancel_cb cancel_cb, void *cancel_arg)
276 size_t i;
277 char *path;
278 int imsg_idxfds[2];
279 int npackfd = -1, idxfd = -1, nidxfd = -1;
280 int tmpfds[3];
281 int idxstatus, done = 0;
282 const struct got_error *err;
283 struct imsgbuf idxibuf;
284 pid_t idxpid;
285 char *tmpidxpath = NULL;
286 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
287 const char *repo_path = got_repo_get_path_git_dir(repo);
288 struct stat sb;
290 for (i = 0; i < nitems(tmpfds); i++)
291 tmpfds[i] = -1;
293 if (asprintf(&path, "%s/%s/indexing.idx",
294 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
295 err = got_error_from_errno("asprintf");
296 goto done;
298 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
299 free(path);
300 if (err)
301 goto done;
302 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
303 err = got_error_from_errno2("fchmod", tmpidxpath);
304 goto done;
307 nidxfd = dup(idxfd);
308 if (nidxfd == -1) {
309 err = got_error_from_errno("dup");
310 goto done;
313 for (i = 0; i < nitems(tmpfds); i++) {
314 tmpfds[i] = got_opentempfd();
315 if (tmpfds[i] == -1) {
316 err = got_error_from_errno("got_opentempfd");
317 goto done;
321 err = got_object_id_str(&id_str, pack_hash);
322 if (err)
323 goto done;
325 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
326 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
327 err = got_error_from_errno("asprintf");
328 goto done;
331 if (fstat(fileno(packfile), &sb) == -1) {
332 err = got_error_from_errno2("fstat", packfile_path);
333 goto done;
336 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
337 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
338 err = got_error_from_errno("asprintf");
339 goto done;
342 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
343 err = got_error_from_errno("socketpair");
344 goto done;
346 idxpid = fork();
347 if (idxpid == -1) {
348 err= got_error_from_errno("fork");
349 goto done;
350 } else if (idxpid == 0)
351 got_privsep_exec_child(imsg_idxfds,
352 GOT_PATH_PROG_INDEX_PACK, packfile_path);
353 if (close(imsg_idxfds[1]) == -1) {
354 err = got_error_from_errno("close");
355 goto done;
357 imsg_init(&idxibuf, imsg_idxfds[0]);
359 npackfd = dup(fileno(packfile));
360 if (npackfd == -1) {
361 err = got_error_from_errno("dup");
362 goto done;
364 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
365 npackfd);
366 if (err != NULL)
367 goto done;
368 npackfd = -1;
369 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
370 if (err != NULL)
371 goto done;
372 nidxfd = -1;
373 for (i = 0; i < nitems(tmpfds); i++) {
374 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
375 if (err != NULL)
376 goto done;
377 tmpfds[i] = -1;
379 done = 0;
380 while (!done) {
381 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
383 if (cancel_cb) {
384 err = cancel_cb(cancel_arg);
385 if (err)
386 goto done;
389 err = got_privsep_recv_index_progress(&done, &nobj_total,
390 &nobj_indexed, &nobj_loose, &nobj_resolved,
391 &idxibuf);
392 if (err != NULL)
393 goto done;
394 if (nobj_indexed != 0) {
395 err = progress_cb(progress_arg, sb.st_size,
396 nobj_total, nobj_indexed, nobj_loose,
397 nobj_resolved);
398 if (err)
399 break;
402 if (close(imsg_idxfds[0]) == -1) {
403 err = got_error_from_errno("close");
404 goto done;
406 if (waitpid(idxpid, &idxstatus, 0) == -1) {
407 err = got_error_from_errno("waitpid");
408 goto done;
411 if (rename(tmpidxpath, idxpath) == -1) {
412 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
413 goto done;
415 free(tmpidxpath);
416 tmpidxpath = NULL;
418 done:
419 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
420 err = got_error_from_errno2("unlink", tmpidxpath);
421 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
422 err = got_error_from_errno("close");
423 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
424 err = got_error_from_errno("close");
425 for (i = 0; i < nitems(tmpfds); i++) {
426 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
427 err = got_error_from_errno("close");
429 free(tmpidxpath);
430 free(idxpath);
431 free(packfile_path);
432 return err;
435 const struct got_error *
436 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
437 struct got_repository *repo, const char *packfile_path)
439 const struct got_error *err = NULL;
440 const char *packdir_path = NULL;
441 char *packfile_name = NULL, *p, *dot;
442 struct got_object_id id;
443 int packfd = -1;
445 *packfile = NULL;
446 *pack_hash = NULL;
448 packdir_path = got_repo_get_path_objects_pack(repo);
449 if (packdir_path == NULL)
450 return got_error_from_errno("got_repo_get_path_objects_pack");
452 if (!got_path_is_child(packfile_path, packdir_path,
453 strlen(packdir_path))) {
454 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
455 goto done;
459 err = got_path_basename(&packfile_name, packfile_path);
460 if (err)
461 goto done;
462 p = packfile_name;
464 if (strncmp(p, "pack-", 5) != 0) {
465 err = got_error_fmt(GOT_ERR_BAD_PATH,
466 "'%s' is not a valid pack file name",
467 packfile_name);
468 goto done;
470 p += 5;
471 dot = strchr(p, '.');
472 if (dot == NULL) {
473 err = got_error_fmt(GOT_ERR_BAD_PATH,
474 "'%s' is not a valid pack file name",
475 packfile_name);
476 goto done;
478 if (strcmp(dot + 1, "pack") != 0) {
479 err = got_error_fmt(GOT_ERR_BAD_PATH,
480 "'%s' is not a valid pack file name",
481 packfile_name);
482 goto done;
484 *dot = '\0';
485 if (!got_parse_object_id(&id, p, GOT_HASH_SHA1)) {
486 err = got_error_fmt(GOT_ERR_BAD_PATH,
487 "'%s' is not a valid pack file name",
488 packfile_name);
489 goto done;
492 *pack_hash = got_object_id_dup(&id);
493 if (*pack_hash == NULL) {
494 err = got_error_from_errno("got_object_id_dup");
495 goto done;
498 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
499 if (packfd == -1) {
500 err = got_error_from_errno2("open", packfile_path);
501 goto done;
504 *packfile = fdopen(packfd, "r");
505 if (*packfile == NULL) {
506 err = got_error_from_errno2("fdopen", packfile_path);
507 goto done;
509 packfd = -1;
510 done:
511 if (packfd != -1 && close(packfd) == -1 && err == NULL)
512 err = got_error_from_errno2("close", packfile_path);
513 free(packfile_name);
514 if (err) {
515 free(*pack_hash);
516 *pack_hash = NULL;
518 return err;
521 const struct got_error *
522 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
523 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
524 got_cancel_cb cancel_cb, void *cancel_arg)
526 const struct got_error *err = NULL;
527 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
528 struct got_packidx *packidx = NULL;
529 struct got_pack *pack = NULL;
530 uint32_t nobj, i;
532 err = got_object_id_str(&id_str, pack_hash);
533 if (err)
534 goto done;
536 if (asprintf(&packpath, "%s/pack-%s.pack",
537 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
538 err = got_error_from_errno("asprintf");
539 goto done;
541 if (asprintf(&idxpath, "%s/pack-%s.idx",
542 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
543 err = got_error_from_errno("asprintf");
544 goto done;
547 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
548 if (err)
549 goto done;
551 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
552 if (err)
553 goto done;
555 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
556 for (i = 0; i < nobj; i++) {
557 struct got_packidx_object_id *oid;
558 struct got_object_id id, base_id;
559 off_t offset, base_offset = 0;
560 uint8_t type;
561 uint64_t size;
562 size_t tslen, len;
564 if (cancel_cb) {
565 err = cancel_cb(cancel_arg);
566 if (err)
567 break;
569 oid = &packidx->hdr.sorted_ids[i];
570 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
572 offset = got_packidx_get_object_offset(packidx, i);
573 if (offset == -1) {
574 err = got_error(GOT_ERR_BAD_PACKIDX);
575 goto done;
578 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
579 pack, offset);
580 if (err)
581 goto done;
583 switch (type) {
584 case GOT_OBJ_TYPE_OFFSET_DELTA:
585 err = got_pack_parse_offset_delta(&base_offset, &len,
586 pack, offset, tslen);
587 if (err)
588 goto done;
589 break;
590 case GOT_OBJ_TYPE_REF_DELTA:
591 err = got_pack_parse_ref_delta(&base_id,
592 pack, offset, tslen);
593 if (err)
594 goto done;
595 break;
597 err = (*list_cb)(list_arg, &id, type, offset, size,
598 base_offset, &base_id);
599 if (err)
600 goto done;
603 done:
604 free(id_str);
605 free(idxpath);
606 free(packpath);
607 if (packidx)
608 got_packidx_close(packidx);
609 return err;
612 static const struct got_error *
613 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
614 void *progress_arg, struct got_ratelimit *rl,
615 int nloose, int ncommits, int npurged)
617 const struct got_error *err;
618 int elapsed;
620 if (progress_cb == NULL)
621 return NULL;
623 err = got_ratelimit_check(&elapsed, rl);
624 if (err || !elapsed)
625 return err;
627 return progress_cb(progress_arg, nloose, ncommits, npurged);
630 static const struct got_error *
631 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
632 got_cleanup_progress_cb progress_cb, void *progress_arg,
633 struct got_ratelimit *rl, struct got_repository *repo)
635 const struct got_error *err = NULL;
636 char *path_objects = NULL, *path = NULL;
637 DIR *dir = NULL;
638 struct got_object *obj = NULL;
639 struct got_object_id id;
640 int i, fd = -1;
641 struct stat sb;
643 *ondisk_size = 0;
644 *loose_ids = got_object_idset_alloc();
645 if (*loose_ids == NULL)
646 return got_error_from_errno("got_object_idset_alloc");
648 path_objects = got_repo_get_path_objects(repo);
649 if (path_objects == NULL) {
650 err = got_error_from_errno("got_repo_get_path_objects");
651 goto done;
654 for (i = 0; i <= 0xff; i++) {
655 struct dirent *dent;
657 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
658 err = got_error_from_errno("asprintf");
659 break;
662 dir = opendir(path);
663 if (dir == NULL) {
664 if (errno == ENOENT) {
665 err = NULL;
666 continue;
668 err = got_error_from_errno2("opendir", path);
669 break;
672 while ((dent = readdir(dir)) != NULL) {
673 char *id_str;
675 if (strcmp(dent->d_name, ".") == 0 ||
676 strcmp(dent->d_name, "..") == 0)
677 continue;
679 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
680 err = got_error_from_errno("asprintf");
681 goto done;
684 if (!got_parse_object_id(&id, id_str,
685 GOT_HASH_SHA1)) {
686 free(id_str);
687 continue;
689 free(id_str);
691 err = got_object_open_loose_fd(&fd, &id, repo);
692 if (err)
693 goto done;
694 if (fstat(fd, &sb) == -1) {
695 err = got_error_from_errno("fstat");
696 goto done;
698 err = got_object_read_header_privsep(&obj, &id, repo,
699 fd);
700 if (err)
701 goto done;
702 fd = -1; /* already closed */
704 switch (obj->type) {
705 case GOT_OBJ_TYPE_COMMIT:
706 case GOT_OBJ_TYPE_TREE:
707 case GOT_OBJ_TYPE_BLOB:
708 case GOT_OBJ_TYPE_TAG:
709 break;
710 default:
711 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
712 "%d", obj->type);
713 goto done;
715 got_object_close(obj);
716 obj = NULL;
717 (*ondisk_size) += sb.st_size;
718 err = got_object_idset_add(*loose_ids, &id, NULL);
719 if (err)
720 goto done;
721 err = report_cleanup_progress(progress_cb,
722 progress_arg, rl,
723 got_object_idset_num_elements(*loose_ids), -1, -1);
724 if (err)
725 goto done;
728 if (closedir(dir) != 0) {
729 err = got_error_from_errno("closedir");
730 goto done;
732 dir = NULL;
734 free(path);
735 path = NULL;
737 done:
738 if (dir && closedir(dir) != 0 && err == NULL)
739 err = got_error_from_errno("closedir");
740 if (fd != -1 && close(fd) == -1 && err == NULL)
741 err = got_error_from_errno("close");
742 if (err) {
743 got_object_idset_free(*loose_ids);
744 *loose_ids = NULL;
746 if (obj)
747 got_object_close(obj);
748 free(path_objects);
749 free(path);
750 return err;
753 static const struct got_error *
754 preserve_loose_object(struct got_object_idset *loose_ids,
755 struct got_object_id *id, struct got_repository *repo, int *npacked)
757 const struct got_error *err = NULL;
758 struct got_object *obj;
760 if (!got_object_idset_contains(loose_ids, id))
761 return NULL;
763 /*
764 * Try to open this object from a pack file. This ensures that
765 * we do in fact have a valid packed copy of the object. Otherwise
766 * we should not delete the loose representation of this object.
767 */
768 err = got_object_open_packed(&obj, id, repo);
769 if (err == NULL) {
770 got_object_close(obj);
771 /*
772 * The object is referenced and packed.
773 * We can purge the redundantly stored loose object.
774 */
775 (*npacked)++;
776 return NULL;
777 } else if (err->code != GOT_ERR_NO_OBJ)
778 return err;
780 /*
781 * This object is referenced and not packed.
782 * Remove it from our purge set.
783 */
784 return got_object_idset_remove(NULL, loose_ids, id);
787 static const struct got_error *
788 load_tree_entries(struct got_object_id_queue *ids,
789 struct got_object_idset *loose_ids,
790 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
791 const char *dpath, struct got_repository *repo, int *npacked,
792 got_cancel_cb cancel_cb, void *cancel_arg)
794 const struct got_error *err;
795 struct got_tree_object *tree;
796 char *p = NULL;
797 int i;
799 err = got_object_open_as_tree(&tree, repo, tree_id);
800 if (err)
801 return err;
803 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
804 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
805 struct got_object_id *id = got_tree_entry_get_id(e);
806 mode_t mode = got_tree_entry_get_mode(e);
808 if (cancel_cb) {
809 err = (*cancel_cb)(cancel_arg);
810 if (err)
811 break;
814 if (got_object_tree_entry_is_symlink(e) ||
815 got_object_tree_entry_is_submodule(e) ||
816 got_object_idset_contains(traversed_ids, id))
817 continue;
819 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
820 got_tree_entry_get_name(e)) == -1) {
821 err = got_error_from_errno("asprintf");
822 break;
825 if (S_ISDIR(mode)) {
826 struct got_object_qid *qid;
827 err = got_object_qid_alloc(&qid, id);
828 if (err)
829 break;
830 STAILQ_INSERT_TAIL(ids, qid, entry);
831 } else if (S_ISREG(mode)) {
832 /* This blob is referenced. */
833 err = preserve_loose_object(loose_ids, id, repo,
834 npacked);
835 if (err)
836 break;
837 err = got_object_idset_add(traversed_ids, id, NULL);
838 if (err)
839 break;
841 free(p);
842 p = NULL;
845 got_object_tree_close(tree);
846 free(p);
847 return err;
850 static const struct got_error *
851 load_tree(struct got_object_idset *loose_ids,
852 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
853 const char *dpath, struct got_repository *repo, int *npacked,
854 got_cancel_cb cancel_cb, void *cancel_arg)
856 const struct got_error *err = NULL;
857 struct got_object_id_queue tree_ids;
858 struct got_object_qid *qid;
860 err = got_object_qid_alloc(&qid, tree_id);
861 if (err)
862 return err;
864 STAILQ_INIT(&tree_ids);
865 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
867 while (!STAILQ_EMPTY(&tree_ids)) {
868 if (cancel_cb) {
869 err = (*cancel_cb)(cancel_arg);
870 if (err)
871 break;
874 qid = STAILQ_FIRST(&tree_ids);
875 STAILQ_REMOVE_HEAD(&tree_ids, entry);
877 if (got_object_idset_contains(traversed_ids, &qid->id)) {
878 got_object_qid_free(qid);
879 continue;
882 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
883 if (err) {
884 got_object_qid_free(qid);
885 break;
888 /* This tree is referenced. */
889 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
890 if (err)
891 break;
893 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
894 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
895 got_object_qid_free(qid);
896 if (err)
897 break;
900 got_object_id_queue_free(&tree_ids);
901 return err;
904 static const struct got_error *
905 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
906 int *npacked, struct got_object_idset *traversed_ids,
907 struct got_object_id *id, struct got_repository *repo,
908 got_cleanup_progress_cb progress_cb, void *progress_arg,
909 struct got_ratelimit *rl, int nloose,
910 got_cancel_cb cancel_cb, void *cancel_arg)
912 const struct got_error *err;
913 struct got_commit_object *commit = NULL;
914 struct got_tag_object *tag = NULL;
915 struct got_object_id *tree_id = NULL;
916 struct got_object_id_queue ids;
917 struct got_object_qid *qid;
918 int obj_type;
920 err = got_object_qid_alloc(&qid, id);
921 if (err)
922 return err;
924 STAILQ_INIT(&ids);
925 STAILQ_INSERT_TAIL(&ids, qid, entry);
927 while (!STAILQ_EMPTY(&ids)) {
928 if (cancel_cb) {
929 err = (*cancel_cb)(cancel_arg);
930 if (err)
931 break;
934 qid = STAILQ_FIRST(&ids);
935 STAILQ_REMOVE_HEAD(&ids, entry);
937 if (got_object_idset_contains(traversed_ids, &qid->id)) {
938 got_object_qid_free(qid);
939 qid = NULL;
940 continue;
943 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
944 if (err)
945 break;
947 /* This commit or tag is referenced. */
948 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
949 if (err)
950 break;
952 err = got_object_get_type(&obj_type, repo, &qid->id);
953 if (err)
954 break;
955 switch (obj_type) {
956 case GOT_OBJ_TYPE_COMMIT:
957 err = got_object_open_as_commit(&commit, repo,
958 &qid->id);
959 if (err)
960 goto done;
961 break;
962 case GOT_OBJ_TYPE_TAG:
963 err = got_object_open_as_tag(&tag, repo, &qid->id);
964 if (err)
965 goto done;
966 break;
967 default:
968 /* should not happen */
969 err = got_error(GOT_ERR_OBJ_TYPE);
970 goto done;
973 /* Find a tree object to scan. */
974 if (commit) {
975 tree_id = got_object_commit_get_tree_id(commit);
976 } else if (tag) {
977 obj_type = got_object_tag_get_object_type(tag);
978 switch (obj_type) {
979 case GOT_OBJ_TYPE_COMMIT:
980 err = got_object_open_as_commit(&commit, repo,
981 got_object_tag_get_object_id(tag));
982 if (err)
983 goto done;
984 tree_id = got_object_commit_get_tree_id(commit);
985 break;
986 case GOT_OBJ_TYPE_TREE:
987 tree_id = got_object_tag_get_object_id(tag);
988 break;
989 default:
990 /*
991 * Tag points at something other than a
992 * commit or tree. Leave this weird tag object
993 * and the object it points to on disk.
994 */
995 err = got_object_idset_remove(NULL, loose_ids,
996 &qid->id);
997 if (err && err->code != GOT_ERR_NO_OBJ)
998 goto done;
999 err = got_object_idset_remove(NULL, loose_ids,
1000 got_object_tag_get_object_id(tag));
1001 if (err && err->code != GOT_ERR_NO_OBJ)
1002 goto done;
1003 err = NULL;
1004 break;
1008 if (tree_id) {
1009 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1010 repo, npacked, cancel_cb, cancel_arg);
1011 if (err)
1012 break;
1015 if (commit || tag)
1016 (*ncommits)++; /* scanned tags are counted as commits */
1018 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1019 nloose, *ncommits, -1);
1020 if (err)
1021 break;
1023 if (commit) {
1024 /* Find parent commits to scan. */
1025 const struct got_object_id_queue *parent_ids;
1026 parent_ids = got_object_commit_get_parent_ids(commit);
1027 err = got_object_id_queue_copy(parent_ids, &ids);
1028 if (err)
1029 break;
1030 got_object_commit_close(commit);
1031 commit = NULL;
1033 if (tag) {
1034 got_object_tag_close(tag);
1035 tag = NULL;
1037 got_object_qid_free(qid);
1038 qid = NULL;
1040 done:
1041 if (qid)
1042 got_object_qid_free(qid);
1043 if (commit)
1044 got_object_commit_close(commit);
1045 if (tag)
1046 got_object_tag_close(tag);
1047 got_object_id_queue_free(&ids);
1048 return err;
1051 struct purge_loose_object_arg {
1052 struct got_repository *repo;
1053 got_cleanup_progress_cb progress_cb;
1054 void *progress_arg;
1055 struct got_ratelimit *rl;
1056 int nloose;
1057 int ncommits;
1058 int npurged;
1059 off_t size_purged;
1060 int dry_run;
1061 time_t max_mtime;
1062 int ignore_mtime;
1065 static const struct got_error *
1066 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1068 struct purge_loose_object_arg *a = arg;
1069 const struct got_error *err, *unlock_err = NULL;
1070 char *path = NULL;
1071 int fd = -1;
1072 struct stat sb;
1073 struct got_lockfile *lf = NULL;
1075 err = got_object_get_path(&path, id, a->repo);
1076 if (err)
1077 return err;
1079 err = got_object_open_loose_fd(&fd, id, a->repo);
1080 if (err)
1081 goto done;
1083 if (fstat(fd, &sb) == -1) {
1084 err = got_error_from_errno("fstat");
1085 goto done;
1089 * Do not delete objects which are younger than our maximum
1090 * modification time threshold. This prevents a race where
1091 * new objects which are being added to the repository
1092 * concurrently would be deleted.
1094 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1095 if (!a->dry_run) {
1096 err = got_lockfile_lock(&lf, path, -1);
1097 if (err)
1098 goto done;
1099 if (unlink(path) == -1) {
1100 err = got_error_from_errno2("unlink", path);
1101 goto done;
1105 a->npurged++;
1106 a->size_purged += sb.st_size;
1107 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1108 a->rl, a->nloose, a->ncommits, a->npurged);
1109 if (err)
1110 goto done;
1112 done:
1113 if (fd != -1 && close(fd) == -1 && err == NULL)
1114 err = got_error_from_errno("close");
1115 free(path);
1116 if (lf)
1117 unlock_err = got_lockfile_unlock(lf, -1);
1118 return err ? err : unlock_err;
1121 const struct got_error *
1122 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1123 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1124 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1125 got_cancel_cb cancel_cb, void *cancel_arg)
1127 const struct got_error *err;
1128 struct got_object_idset *loose_ids;
1129 struct got_object_idset *traversed_ids;
1130 struct got_object_id **referenced_ids;
1131 int i, nreferenced, nloose, ncommits = 0;
1132 struct got_reflist_head refs;
1133 struct got_reflist_entry *re;
1134 struct purge_loose_object_arg arg;
1135 time_t max_mtime = 0;
1136 struct got_ratelimit rl;
1138 TAILQ_INIT(&refs);
1139 got_ratelimit_init(&rl, 0, 500);
1141 *size_before = 0;
1142 *size_after = 0;
1143 *npacked = 0;
1145 err = get_loose_object_ids(&loose_ids, size_before,
1146 progress_cb, progress_arg, &rl, repo);
1147 if (err)
1148 return err;
1149 nloose = got_object_idset_num_elements(loose_ids);
1150 if (nloose == 0) {
1151 got_object_idset_free(loose_ids);
1152 if (progress_cb) {
1153 err = progress_cb(progress_arg, 0, 0, 0);
1154 if (err)
1155 return err;
1157 return NULL;
1160 traversed_ids = got_object_idset_alloc();
1161 if (traversed_ids == NULL) {
1162 err = got_error_from_errno("got_object_idset_alloc");
1163 goto done;
1166 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1167 if (err)
1168 goto done;
1169 if (!ignore_mtime) {
1170 TAILQ_FOREACH(re, &refs, entry) {
1171 time_t mtime = got_ref_get_mtime(re->ref);
1172 if (mtime > max_mtime)
1173 max_mtime = mtime;
1176 * For safety, keep objects created within 10 minutes
1177 * before the youngest reference was created.
1179 if (max_mtime >= 600)
1180 max_mtime -= 600;
1183 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1184 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1185 &refs, repo, cancel_cb, cancel_arg);
1186 if (err)
1187 goto done;
1189 for (i = 0; i < nreferenced; i++) {
1190 struct got_object_id *id = referenced_ids[i];
1191 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1192 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1193 nloose, cancel_cb, cancel_arg);
1194 if (err)
1195 goto done;
1198 /* Any remaining loose objects are unreferenced and can be purged. */
1199 arg.repo = repo;
1200 arg.progress_arg = progress_arg;
1201 arg.progress_cb = progress_cb;
1202 arg.rl = &rl;
1203 arg.nloose = nloose;
1204 arg.npurged = 0;
1205 arg.size_purged = 0;
1206 arg.ncommits = ncommits;
1207 arg.dry_run = dry_run;
1208 arg.max_mtime = max_mtime;
1209 arg.ignore_mtime = ignore_mtime;
1210 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1211 if (err)
1212 goto done;
1213 *size_after = *size_before - arg.size_purged;
1215 /* Produce a final progress report. */
1216 if (progress_cb) {
1217 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1218 if (err)
1219 goto done;
1221 done:
1222 got_object_idset_free(loose_ids);
1223 got_object_idset_free(traversed_ids);
1224 return err;
1227 static const struct got_error *
1228 remove_packidx(int dir_fd, const char *relpath)
1230 const struct got_error *err, *unlock_err;
1231 struct got_lockfile *lf;
1233 err = got_lockfile_lock(&lf, relpath, dir_fd);
1234 if (err)
1235 return err;
1236 if (unlinkat(dir_fd, relpath, 0) == -1)
1237 err = got_error_from_errno("unlinkat");
1238 unlock_err = got_lockfile_unlock(lf, dir_fd);
1239 return err ? err : unlock_err;
1242 const struct got_error *
1243 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1244 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1245 got_cancel_cb cancel_cb, void *cancel_arg)
1247 const struct got_error *err = NULL;
1248 DIR *packdir = NULL;
1249 struct dirent *dent;
1250 char *pack_relpath = NULL;
1251 int packdir_fd;
1252 struct stat sb;
1254 packdir_fd = openat(got_repo_get_fd(repo),
1255 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1256 if (packdir_fd == -1) {
1257 if (errno == ENOENT)
1258 return NULL;
1259 return got_error_from_errno_fmt("openat: %s/%s",
1260 got_repo_get_path_git_dir(repo),
1261 GOT_OBJECTS_PACK_DIR);
1264 packdir = fdopendir(packdir_fd);
1265 if (packdir == NULL) {
1266 err = got_error_from_errno("fdopendir");
1267 goto done;
1270 while ((dent = readdir(packdir)) != NULL) {
1271 if (cancel_cb) {
1272 err = cancel_cb(cancel_arg);
1273 if (err)
1274 goto done;
1277 if (!got_repo_is_packidx_filename(dent->d_name,
1278 strlen(dent->d_name)))
1279 continue;
1281 err = got_packidx_get_packfile_path(&pack_relpath,
1282 dent->d_name);
1283 if (err)
1284 goto done;
1286 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1287 free(pack_relpath);
1288 pack_relpath = NULL;
1289 continue;
1291 if (errno != ENOENT) {
1292 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1293 got_repo_get_path_git_dir(repo),
1294 GOT_OBJECTS_PACK_DIR,
1295 pack_relpath);
1296 goto done;
1299 if (!dry_run) {
1300 err = remove_packidx(packdir_fd, dent->d_name);
1301 if (err)
1302 goto done;
1304 if (progress_cb) {
1305 char *path;
1306 if (asprintf(&path, "%s/%s/%s",
1307 got_repo_get_path_git_dir(repo),
1308 GOT_OBJECTS_PACK_DIR,
1309 dent->d_name) == -1) {
1310 err = got_error_from_errno("asprintf");
1311 goto done;
1313 err = progress_cb(progress_arg, path);
1314 free(path);
1315 if (err)
1316 goto done;
1318 free(pack_relpath);
1319 pack_relpath = NULL;
1321 done:
1322 if (packdir && closedir(packdir) != 0 && err == NULL)
1323 err = got_error_from_errno("closedir");
1324 free(pack_relpath);
1325 return err;