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 <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <unistd.h>
36 #include <imsg.h>
38 #include "got_error.h"
39 #include "got_cancel.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_repository_admin.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_idset.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_pack_create.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_lockfile.h"
59 #ifndef nitems
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 #endif
63 static const struct got_error *
64 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
65 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
66 struct got_repository *repo,
67 got_cancel_cb cancel_cb, void *cancel_arg)
68 {
69 const struct got_error *err = NULL;
70 const size_t alloc_chunksz = 256;
71 size_t nalloc;
72 struct got_reflist_entry *re;
73 int i;
75 *ids = NULL;
76 *nobjects = 0;
78 err = got_reflist_sort(refs,
79 got_ref_cmp_by_commit_timestamp_descending, repo);
80 if (err)
81 return err;
83 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
84 if (*ids == NULL)
85 return got_error_from_errno("reallocarray");
86 nalloc = alloc_chunksz;
88 TAILQ_FOREACH(re, refs, entry) {
89 struct got_object_id *id;
91 if (cancel_cb) {
92 err = cancel_cb(cancel_arg);
93 if (err)
94 goto done;
95 }
97 err = got_ref_resolve(&id, repo, re->ref);
98 if (err)
99 goto done;
101 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
102 int obj_type;
103 err = got_object_get_type(&obj_type, repo, id);
104 if (err)
105 goto done;
106 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
107 free(id);
108 id = NULL;
109 continue;
113 if (nalloc <= *nobjects) {
114 struct got_object_id **new;
115 new = recallocarray(*ids, nalloc,
116 nalloc + alloc_chunksz,
117 sizeof(struct got_object_id *));
118 if (new == NULL) {
119 err = got_error_from_errno(
120 "recallocarray");
121 goto done;
123 *ids = new;
124 nalloc += alloc_chunksz;
126 (*ids)[*nobjects] = id;
127 if ((*ids)[*nobjects] == NULL) {
128 err = got_error_from_errno("got_object_id_dup");
129 goto done;
131 (*nobjects)++;
133 done:
134 if (err) {
135 for (i = 0; i < *nobjects; i++)
136 free((*ids)[i]);
137 free(*ids);
138 *ids = NULL;
139 *nobjects = 0;
141 return err;
144 const struct got_error *
145 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
146 struct got_reflist_head *include_refs,
147 struct got_reflist_head *exclude_refs, struct got_repository *repo,
148 int loose_obj_only,
149 got_pack_progress_cb progress_cb, void *progress_arg,
150 got_cancel_cb cancel_cb, void *cancel_arg)
152 const struct got_error *err = NULL;
153 struct got_object_id **ours = NULL, **theirs = NULL;
154 int nours = 0, ntheirs = 0, packfd = -1, i;
155 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
156 char *sha1_str = NULL;
157 FILE *delta_cache = NULL;
158 struct got_ratelimit rl;
160 *packfile = NULL;
161 *pack_hash = NULL;
163 got_ratelimit_init(&rl, 0, 500);
165 if (asprintf(&path, "%s/%s/packing.pack",
166 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
167 err = got_error_from_errno("asprintf");
168 goto done;
170 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
171 if (err)
172 goto done;
174 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
175 err = got_error_from_errno2("fchmod", tmpfile_path);
176 goto done;
179 delta_cache = got_opentemp();
180 if (delta_cache == NULL) {
181 err = got_error_from_errno("got_opentemp");
182 goto done;
185 err = get_reflist_object_ids(&ours, &nours,
186 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
187 include_refs, repo, cancel_cb, cancel_arg);
188 if (err)
189 goto done;
191 if (nours == 0) {
192 err = got_error(GOT_ERR_CANNOT_PACK);
193 goto done;
196 if (!TAILQ_EMPTY(exclude_refs)) {
197 err = get_reflist_object_ids(&theirs, &ntheirs,
198 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
199 exclude_refs, repo,
200 cancel_cb, cancel_arg);
201 if (err)
202 goto done;
205 *pack_hash = calloc(1, sizeof(**pack_hash));
206 if (*pack_hash == NULL) {
207 err = got_error_from_errno("calloc");
208 goto done;
211 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
212 theirs, ntheirs, ours, nours, repo, loose_obj_only, 0,
213 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
214 if (err)
215 goto done;
217 err = got_object_id_str(&sha1_str, *pack_hash);
218 if (err)
219 goto done;
220 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
221 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
222 sha1_str) == -1) {
223 err = got_error_from_errno("asprintf");
224 goto done;
227 if (lseek(packfd, 0L, SEEK_SET) == -1) {
228 err = got_error_from_errno("lseek");
229 goto done;
231 if (rename(tmpfile_path, packfile_path) == -1) {
232 err = got_error_from_errno3("rename", tmpfile_path,
233 packfile_path);
234 goto done;
236 free(tmpfile_path);
237 tmpfile_path = NULL;
239 *packfile = fdopen(packfd, "w");
240 if (*packfile == NULL) {
241 err = got_error_from_errno2("fdopen", tmpfile_path);
242 goto done;
244 packfd = -1;
245 done:
246 for (i = 0; i < nours; i++)
247 free(ours[i]);
248 free(ours);
249 for (i = 0; i < ntheirs; i++)
250 free(theirs[i]);
251 free(theirs);
252 if (packfd != -1 && close(packfd) == -1 && err == NULL)
253 err = got_error_from_errno2("close", packfile_path);
254 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
255 err = got_error_from_errno("fclose");
256 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
257 err = got_error_from_errno2("unlink", tmpfile_path);
258 free(tmpfile_path);
259 free(packfile_path);
260 free(sha1_str);
261 free(path);
262 if (err) {
263 free(*pack_hash);
264 *pack_hash = NULL;
265 if (*packfile)
266 fclose(*packfile);
267 *packfile = NULL;
269 return err;
272 const struct got_error *
273 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
274 struct got_repository *repo,
275 got_pack_index_progress_cb progress_cb, void *progress_arg,
276 got_cancel_cb cancel_cb, void *cancel_arg)
278 size_t i;
279 char *path;
280 int imsg_idxfds[2];
281 int npackfd = -1, idxfd = -1, nidxfd = -1;
282 int tmpfds[3];
283 int idxstatus, done = 0;
284 const struct got_error *err;
285 struct imsgbuf idxibuf;
286 pid_t idxpid;
287 char *tmpidxpath = NULL;
288 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
289 const char *repo_path = got_repo_get_path_git_dir(repo);
290 struct stat sb;
292 for (i = 0; i < nitems(tmpfds); i++)
293 tmpfds[i] = -1;
295 if (asprintf(&path, "%s/%s/indexing.idx",
296 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
297 err = got_error_from_errno("asprintf");
298 goto done;
300 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
301 free(path);
302 if (err)
303 goto done;
304 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
305 err = got_error_from_errno2("fchmod", tmpidxpath);
306 goto done;
309 nidxfd = dup(idxfd);
310 if (nidxfd == -1) {
311 err = got_error_from_errno("dup");
312 goto done;
315 for (i = 0; i < nitems(tmpfds); i++) {
316 tmpfds[i] = got_opentempfd();
317 if (tmpfds[i] == -1) {
318 err = got_error_from_errno("got_opentempfd");
319 goto done;
323 err = got_object_id_str(&id_str, pack_hash);
324 if (err)
325 goto done;
327 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
328 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
329 err = got_error_from_errno("asprintf");
330 goto done;
333 if (fstat(fileno(packfile), &sb) == -1) {
334 err = got_error_from_errno2("fstat", packfile_path);
335 goto done;
338 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
339 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
340 err = got_error_from_errno("asprintf");
341 goto done;
344 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
345 err = got_error_from_errno("socketpair");
346 goto done;
348 idxpid = fork();
349 if (idxpid == -1) {
350 err= got_error_from_errno("fork");
351 goto done;
352 } else if (idxpid == 0)
353 got_privsep_exec_child(imsg_idxfds,
354 GOT_PATH_PROG_INDEX_PACK, packfile_path);
355 if (close(imsg_idxfds[1]) == -1) {
356 err = got_error_from_errno("close");
357 goto done;
359 imsg_init(&idxibuf, imsg_idxfds[0]);
361 npackfd = dup(fileno(packfile));
362 if (npackfd == -1) {
363 err = got_error_from_errno("dup");
364 goto done;
366 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
367 npackfd);
368 if (err != NULL)
369 goto done;
370 npackfd = -1;
371 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
372 if (err != NULL)
373 goto done;
374 nidxfd = -1;
375 for (i = 0; i < nitems(tmpfds); i++) {
376 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
377 if (err != NULL)
378 goto done;
379 tmpfds[i] = -1;
381 done = 0;
382 while (!done) {
383 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
385 if (cancel_cb) {
386 err = cancel_cb(cancel_arg);
387 if (err)
388 goto done;
391 err = got_privsep_recv_index_progress(&done, &nobj_total,
392 &nobj_indexed, &nobj_loose, &nobj_resolved,
393 &idxibuf);
394 if (err != NULL)
395 goto done;
396 if (nobj_indexed != 0) {
397 err = progress_cb(progress_arg, sb.st_size,
398 nobj_total, nobj_indexed, nobj_loose,
399 nobj_resolved);
400 if (err)
401 break;
404 if (close(imsg_idxfds[0]) == -1) {
405 err = got_error_from_errno("close");
406 goto done;
408 if (waitpid(idxpid, &idxstatus, 0) == -1) {
409 err = got_error_from_errno("waitpid");
410 goto done;
413 if (rename(tmpidxpath, idxpath) == -1) {
414 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
415 goto done;
417 free(tmpidxpath);
418 tmpidxpath = NULL;
420 done:
421 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
422 err = got_error_from_errno2("unlink", tmpidxpath);
423 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
424 err = got_error_from_errno("close");
425 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
426 err = got_error_from_errno("close");
427 for (i = 0; i < nitems(tmpfds); i++) {
428 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
429 err = got_error_from_errno("close");
431 free(tmpidxpath);
432 free(idxpath);
433 free(packfile_path);
434 return err;
437 const struct got_error *
438 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
439 struct got_repository *repo, const char *packfile_path)
441 const struct got_error *err = NULL;
442 const char *packdir_path = NULL;
443 char *packfile_name = NULL, *p, *dot;
444 struct got_object_id id;
445 int packfd = -1;
447 *packfile = NULL;
448 *pack_hash = NULL;
450 packdir_path = got_repo_get_path_objects_pack(repo);
451 if (packdir_path == NULL)
452 return got_error_from_errno("got_repo_get_path_objects_pack");
454 if (!got_path_is_child(packfile_path, packdir_path,
455 strlen(packdir_path))) {
456 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
457 goto done;
461 err = got_path_basename(&packfile_name, packfile_path);
462 if (err)
463 goto done;
464 p = packfile_name;
466 if (strncmp(p, "pack-", 5) != 0) {
467 err = got_error_fmt(GOT_ERR_BAD_PATH,
468 "'%s' is not a valid pack file name",
469 packfile_name);
470 goto done;
472 p += 5;
473 dot = strchr(p, '.');
474 if (dot == NULL) {
475 err = got_error_fmt(GOT_ERR_BAD_PATH,
476 "'%s' is not a valid pack file name",
477 packfile_name);
478 goto done;
480 if (strcmp(dot + 1, "pack") != 0) {
481 err = got_error_fmt(GOT_ERR_BAD_PATH,
482 "'%s' is not a valid pack file name",
483 packfile_name);
484 goto done;
486 *dot = '\0';
487 if (!got_parse_sha1_digest(id.sha1, p)) {
488 err = got_error_fmt(GOT_ERR_BAD_PATH,
489 "'%s' is not a valid pack file name",
490 packfile_name);
491 goto done;
494 *pack_hash = got_object_id_dup(&id);
495 if (*pack_hash == NULL) {
496 err = got_error_from_errno("got_object_id_dup");
497 goto done;
500 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
501 if (packfd == -1) {
502 err = got_error_from_errno2("open", packfile_path);
503 goto done;
506 *packfile = fdopen(packfd, "r");
507 if (*packfile == NULL) {
508 err = got_error_from_errno2("fdopen", packfile_path);
509 goto done;
511 packfd = -1;
512 done:
513 if (packfd != -1 && close(packfd) == -1 && err == NULL)
514 err = got_error_from_errno2("close", packfile_path);
515 free(packfile_name);
516 if (err) {
517 free(*pack_hash);
518 *pack_hash = NULL;
520 return err;
523 const struct got_error *
524 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
525 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
526 got_cancel_cb cancel_cb, void *cancel_arg)
528 const struct got_error *err = NULL;
529 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
530 struct got_packidx *packidx = NULL;
531 struct got_pack *pack = NULL;
532 uint32_t nobj, i;
534 err = got_object_id_str(&id_str, pack_hash);
535 if (err)
536 goto done;
538 if (asprintf(&packpath, "%s/pack-%s.pack",
539 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
540 err = got_error_from_errno("asprintf");
541 goto done;
543 if (asprintf(&idxpath, "%s/pack-%s.idx",
544 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
549 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
550 if (err)
551 goto done;
553 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
554 if (err)
555 goto done;
557 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
558 for (i = 0; i < nobj; i++) {
559 struct got_packidx_object_id *oid;
560 struct got_object_id id, base_id;
561 off_t offset, base_offset = 0;
562 uint8_t type;
563 uint64_t size;
564 size_t tslen, len;
566 if (cancel_cb) {
567 err = cancel_cb(cancel_arg);
568 if (err)
569 break;
571 oid = &packidx->hdr.sorted_ids[i];
572 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
574 offset = got_packidx_get_object_offset(packidx, i);
575 if (offset == -1) {
576 err = got_error(GOT_ERR_BAD_PACKIDX);
577 goto done;
580 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
581 pack, offset);
582 if (err)
583 goto done;
585 switch (type) {
586 case GOT_OBJ_TYPE_OFFSET_DELTA:
587 err = got_pack_parse_offset_delta(&base_offset, &len,
588 pack, offset, tslen);
589 if (err)
590 goto done;
591 break;
592 case GOT_OBJ_TYPE_REF_DELTA:
593 err = got_pack_parse_ref_delta(&base_id,
594 pack, offset, tslen);
595 if (err)
596 goto done;
597 break;
599 err = (*list_cb)(list_arg, &id, type, offset, size,
600 base_offset, &base_id);
601 if (err)
602 goto done;
605 done:
606 free(id_str);
607 free(idxpath);
608 free(packpath);
609 if (packidx)
610 got_packidx_close(packidx);
611 return err;
614 static const struct got_error *
615 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
616 void *progress_arg, struct got_ratelimit *rl,
617 int nloose, int ncommits, int npurged)
619 const struct got_error *err;
620 int elapsed;
622 if (progress_cb == NULL)
623 return NULL;
625 err = got_ratelimit_check(&elapsed, rl);
626 if (err || !elapsed)
627 return err;
629 return progress_cb(progress_arg, nloose, ncommits, npurged);
632 static const struct got_error *
633 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
634 got_cleanup_progress_cb progress_cb, void *progress_arg,
635 struct got_ratelimit *rl, struct got_repository *repo)
637 const struct got_error *err = NULL;
638 char *path_objects = NULL, *path = NULL;
639 DIR *dir = NULL;
640 struct got_object *obj = NULL;
641 struct got_object_id id;
642 int i, fd = -1;
643 struct stat sb;
645 *ondisk_size = 0;
646 *loose_ids = got_object_idset_alloc();
647 if (*loose_ids == NULL)
648 return got_error_from_errno("got_object_idset_alloc");
650 path_objects = got_repo_get_path_objects(repo);
651 if (path_objects == NULL) {
652 err = got_error_from_errno("got_repo_get_path_objects");
653 goto done;
656 for (i = 0; i <= 0xff; i++) {
657 struct dirent *dent;
659 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
660 err = got_error_from_errno("asprintf");
661 break;
664 dir = opendir(path);
665 if (dir == NULL) {
666 if (errno == ENOENT) {
667 err = NULL;
668 continue;
670 err = got_error_from_errno2("opendir", path);
671 break;
674 while ((dent = readdir(dir)) != NULL) {
675 char *id_str;
677 if (strcmp(dent->d_name, ".") == 0 ||
678 strcmp(dent->d_name, "..") == 0)
679 continue;
681 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
682 err = got_error_from_errno("asprintf");
683 goto done;
686 memset(&id, 0, sizeof(id));
687 if (!got_parse_sha1_digest(id.sha1, id_str)) {
688 free(id_str);
689 continue;
691 free(id_str);
693 err = got_object_open_loose_fd(&fd, &id, repo);
694 if (err)
695 goto done;
696 if (fstat(fd, &sb) == -1) {
697 err = got_error_from_errno("fstat");
698 goto done;
700 err = got_object_read_header_privsep(&obj, &id, repo,
701 fd);
702 if (err)
703 goto done;
704 fd = -1; /* already closed */
706 switch (obj->type) {
707 case GOT_OBJ_TYPE_COMMIT:
708 case GOT_OBJ_TYPE_TREE:
709 case GOT_OBJ_TYPE_BLOB:
710 case GOT_OBJ_TYPE_TAG:
711 break;
712 default:
713 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
714 "%d", obj->type);
715 goto done;
717 got_object_close(obj);
718 obj = NULL;
719 (*ondisk_size) += sb.st_size;
720 err = got_object_idset_add(*loose_ids, &id, NULL);
721 if (err)
722 goto done;
723 err = report_cleanup_progress(progress_cb,
724 progress_arg, rl,
725 got_object_idset_num_elements(*loose_ids), -1, -1);
726 if (err)
727 goto done;
730 if (closedir(dir) != 0) {
731 err = got_error_from_errno("closedir");
732 goto done;
734 dir = NULL;
736 free(path);
737 path = NULL;
739 done:
740 if (dir && closedir(dir) != 0 && err == NULL)
741 err = got_error_from_errno("closedir");
742 if (fd != -1 && close(fd) == -1 && err == NULL)
743 err = got_error_from_errno("close");
744 if (err) {
745 got_object_idset_free(*loose_ids);
746 *loose_ids = NULL;
748 if (obj)
749 got_object_close(obj);
750 free(path_objects);
751 free(path);
752 return err;
755 static const struct got_error *
756 preserve_loose_object(struct got_object_idset *loose_ids,
757 struct got_object_id *id, struct got_repository *repo, int *npacked)
759 const struct got_error *err = NULL;
760 struct got_object *obj;
762 if (!got_object_idset_contains(loose_ids, id))
763 return NULL;
765 /*
766 * Try to open this object from a pack file. This ensures that
767 * we do in fact have a valid packed copy of the object. Otherwise
768 * we should not delete the loose representation of this object.
769 */
770 err = got_object_open_packed(&obj, id, repo);
771 if (err == NULL) {
772 got_object_close(obj);
773 /*
774 * The object is referenced and packed.
775 * We can purge the redundantly stored loose object.
776 */
777 (*npacked)++;
778 return NULL;
779 } else if (err->code != GOT_ERR_NO_OBJ)
780 return err;
782 /*
783 * This object is referenced and not packed.
784 * Remove it from our purge set.
785 */
786 return got_object_idset_remove(NULL, loose_ids, id);
789 static const struct got_error *
790 load_tree_entries(struct got_object_id_queue *ids,
791 struct got_object_idset *loose_ids,
792 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
793 const char *dpath, struct got_repository *repo, int *npacked,
794 got_cancel_cb cancel_cb, void *cancel_arg)
796 const struct got_error *err;
797 struct got_tree_object *tree;
798 char *p = NULL;
799 int i;
801 err = got_object_open_as_tree(&tree, repo, tree_id);
802 if (err)
803 return err;
805 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
806 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
807 struct got_object_id *id = got_tree_entry_get_id(e);
808 mode_t mode = got_tree_entry_get_mode(e);
810 if (cancel_cb) {
811 err = (*cancel_cb)(cancel_arg);
812 if (err)
813 break;
816 if (got_object_tree_entry_is_symlink(e) ||
817 got_object_tree_entry_is_submodule(e) ||
818 got_object_idset_contains(traversed_ids, id))
819 continue;
821 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
822 got_tree_entry_get_name(e)) == -1) {
823 err = got_error_from_errno("asprintf");
824 break;
827 if (S_ISDIR(mode)) {
828 struct got_object_qid *qid;
829 err = got_object_qid_alloc(&qid, id);
830 if (err)
831 break;
832 STAILQ_INSERT_TAIL(ids, qid, entry);
833 } else if (S_ISREG(mode)) {
834 /* This blob is referenced. */
835 err = preserve_loose_object(loose_ids, id, repo,
836 npacked);
837 if (err)
838 break;
839 err = got_object_idset_add(traversed_ids, id, NULL);
840 if (err)
841 break;
843 free(p);
844 p = NULL;
847 got_object_tree_close(tree);
848 free(p);
849 return err;
852 static const struct got_error *
853 load_tree(struct got_object_idset *loose_ids,
854 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
855 const char *dpath, struct got_repository *repo, int *npacked,
856 got_cancel_cb cancel_cb, void *cancel_arg)
858 const struct got_error *err = NULL;
859 struct got_object_id_queue tree_ids;
860 struct got_object_qid *qid;
862 err = got_object_qid_alloc(&qid, tree_id);
863 if (err)
864 return err;
866 STAILQ_INIT(&tree_ids);
867 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
869 while (!STAILQ_EMPTY(&tree_ids)) {
870 if (cancel_cb) {
871 err = (*cancel_cb)(cancel_arg);
872 if (err)
873 break;
876 qid = STAILQ_FIRST(&tree_ids);
877 STAILQ_REMOVE_HEAD(&tree_ids, entry);
879 if (got_object_idset_contains(traversed_ids, &qid->id)) {
880 got_object_qid_free(qid);
881 continue;
884 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
885 if (err) {
886 got_object_qid_free(qid);
887 break;
890 /* This tree is referenced. */
891 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
892 if (err)
893 break;
895 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
896 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
897 got_object_qid_free(qid);
898 if (err)
899 break;
902 got_object_id_queue_free(&tree_ids);
903 return err;
906 static const struct got_error *
907 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
908 int *npacked, struct got_object_idset *traversed_ids,
909 struct got_object_id *id, struct got_repository *repo,
910 got_cleanup_progress_cb progress_cb, void *progress_arg,
911 struct got_ratelimit *rl, int nloose,
912 got_cancel_cb cancel_cb, void *cancel_arg)
914 const struct got_error *err;
915 struct got_commit_object *commit = NULL;
916 struct got_tag_object *tag = NULL;
917 struct got_object_id *tree_id = NULL;
918 struct got_object_id_queue ids;
919 struct got_object_qid *qid;
920 int obj_type;
922 err = got_object_qid_alloc(&qid, id);
923 if (err)
924 return err;
926 STAILQ_INIT(&ids);
927 STAILQ_INSERT_TAIL(&ids, qid, entry);
929 while (!STAILQ_EMPTY(&ids)) {
930 if (cancel_cb) {
931 err = (*cancel_cb)(cancel_arg);
932 if (err)
933 break;
936 qid = STAILQ_FIRST(&ids);
937 STAILQ_REMOVE_HEAD(&ids, entry);
939 if (got_object_idset_contains(traversed_ids, &qid->id)) {
940 got_object_qid_free(qid);
941 qid = NULL;
942 continue;
945 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
946 if (err)
947 break;
949 /* This commit or tag is referenced. */
950 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
951 if (err)
952 break;
954 err = got_object_get_type(&obj_type, repo, &qid->id);
955 if (err)
956 break;
957 switch (obj_type) {
958 case GOT_OBJ_TYPE_COMMIT:
959 err = got_object_open_as_commit(&commit, repo,
960 &qid->id);
961 if (err)
962 goto done;
963 break;
964 case GOT_OBJ_TYPE_TAG:
965 err = got_object_open_as_tag(&tag, repo, &qid->id);
966 if (err)
967 goto done;
968 break;
969 default:
970 /* should not happen */
971 err = got_error(GOT_ERR_OBJ_TYPE);
972 goto done;
975 /* Find a tree object to scan. */
976 if (commit) {
977 tree_id = got_object_commit_get_tree_id(commit);
978 } else if (tag) {
979 obj_type = got_object_tag_get_object_type(tag);
980 switch (obj_type) {
981 case GOT_OBJ_TYPE_COMMIT:
982 err = got_object_open_as_commit(&commit, repo,
983 got_object_tag_get_object_id(tag));
984 if (err)
985 goto done;
986 tree_id = got_object_commit_get_tree_id(commit);
987 break;
988 case GOT_OBJ_TYPE_TREE:
989 tree_id = got_object_tag_get_object_id(tag);
990 break;
991 default:
992 /*
993 * Tag points at something other than a
994 * commit or tree. Leave this weird tag object
995 * and the object it points to on disk.
996 */
997 err = got_object_idset_remove(NULL, loose_ids,
998 &qid->id);
999 if (err && err->code != GOT_ERR_NO_OBJ)
1000 goto done;
1001 err = got_object_idset_remove(NULL, loose_ids,
1002 got_object_tag_get_object_id(tag));
1003 if (err && err->code != GOT_ERR_NO_OBJ)
1004 goto done;
1005 err = NULL;
1006 break;
1010 if (tree_id) {
1011 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1012 repo, npacked, cancel_cb, cancel_arg);
1013 if (err)
1014 break;
1017 if (commit || tag)
1018 (*ncommits)++; /* scanned tags are counted as commits */
1020 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1021 nloose, *ncommits, -1);
1022 if (err)
1023 break;
1025 if (commit) {
1026 /* Find parent commits to scan. */
1027 const struct got_object_id_queue *parent_ids;
1028 parent_ids = got_object_commit_get_parent_ids(commit);
1029 err = got_object_id_queue_copy(parent_ids, &ids);
1030 if (err)
1031 break;
1032 got_object_commit_close(commit);
1033 commit = NULL;
1035 if (tag) {
1036 got_object_tag_close(tag);
1037 tag = NULL;
1039 got_object_qid_free(qid);
1040 qid = NULL;
1042 done:
1043 if (qid)
1044 got_object_qid_free(qid);
1045 if (commit)
1046 got_object_commit_close(commit);
1047 if (tag)
1048 got_object_tag_close(tag);
1049 got_object_id_queue_free(&ids);
1050 return err;
1053 struct purge_loose_object_arg {
1054 struct got_repository *repo;
1055 got_cleanup_progress_cb progress_cb;
1056 void *progress_arg;
1057 struct got_ratelimit *rl;
1058 int nloose;
1059 int ncommits;
1060 int npurged;
1061 off_t size_purged;
1062 int dry_run;
1063 time_t max_mtime;
1064 int ignore_mtime;
1067 static const struct got_error *
1068 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1070 struct purge_loose_object_arg *a = arg;
1071 const struct got_error *err, *unlock_err = NULL;
1072 char *path = NULL;
1073 int fd = -1;
1074 struct stat sb;
1075 struct got_lockfile *lf = NULL;
1077 err = got_object_get_path(&path, id, a->repo);
1078 if (err)
1079 return err;
1081 err = got_object_open_loose_fd(&fd, id, a->repo);
1082 if (err)
1083 goto done;
1085 if (fstat(fd, &sb) == -1) {
1086 err = got_error_from_errno("fstat");
1087 goto done;
1091 * Do not delete objects which are younger than our maximum
1092 * modification time threshold. This prevents a race where
1093 * new objects which are being added to the repository
1094 * concurrently would be deleted.
1096 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1097 if (!a->dry_run) {
1098 err = got_lockfile_lock(&lf, path, -1);
1099 if (err)
1100 goto done;
1101 if (unlink(path) == -1) {
1102 err = got_error_from_errno2("unlink", path);
1103 goto done;
1107 a->npurged++;
1108 a->size_purged += sb.st_size;
1109 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1110 a->rl, a->nloose, a->ncommits, a->npurged);
1111 if (err)
1112 goto done;
1114 done:
1115 if (fd != -1 && close(fd) == -1 && err == NULL)
1116 err = got_error_from_errno("close");
1117 free(path);
1118 if (lf)
1119 unlock_err = got_lockfile_unlock(lf, -1);
1120 return err ? err : unlock_err;
1123 const struct got_error *
1124 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1125 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1126 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1127 got_cancel_cb cancel_cb, void *cancel_arg)
1129 const struct got_error *err;
1130 struct got_object_idset *loose_ids;
1131 struct got_object_idset *traversed_ids;
1132 struct got_object_id **referenced_ids;
1133 int i, nreferenced, nloose, ncommits = 0;
1134 struct got_reflist_head refs;
1135 struct got_reflist_entry *re;
1136 struct purge_loose_object_arg arg;
1137 time_t max_mtime = 0;
1138 struct got_ratelimit rl;
1140 TAILQ_INIT(&refs);
1141 got_ratelimit_init(&rl, 0, 500);
1143 *size_before = 0;
1144 *size_after = 0;
1145 *npacked = 0;
1147 err = get_loose_object_ids(&loose_ids, size_before,
1148 progress_cb, progress_arg, &rl, repo);
1149 if (err)
1150 return err;
1151 nloose = got_object_idset_num_elements(loose_ids);
1152 if (nloose == 0) {
1153 got_object_idset_free(loose_ids);
1154 if (progress_cb) {
1155 err = progress_cb(progress_arg, 0, 0, 0);
1156 if (err)
1157 return err;
1159 return NULL;
1162 traversed_ids = got_object_idset_alloc();
1163 if (traversed_ids == NULL) {
1164 err = got_error_from_errno("got_object_idset_alloc");
1165 goto done;
1168 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1169 if (err)
1170 goto done;
1171 if (!ignore_mtime) {
1172 TAILQ_FOREACH(re, &refs, entry) {
1173 time_t mtime = got_ref_get_mtime(re->ref);
1174 if (mtime > max_mtime)
1175 max_mtime = mtime;
1178 * For safety, keep objects created within 10 minutes
1179 * before the youngest reference was created.
1181 if (max_mtime >= 600)
1182 max_mtime -= 600;
1185 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1186 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1187 &refs, repo, cancel_cb, cancel_arg);
1188 if (err)
1189 goto done;
1191 for (i = 0; i < nreferenced; i++) {
1192 struct got_object_id *id = referenced_ids[i];
1193 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1194 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1195 nloose, cancel_cb, cancel_arg);
1196 if (err)
1197 goto done;
1200 /* Any remaining loose objects are unreferenced and can be purged. */
1201 arg.repo = repo;
1202 arg.progress_arg = progress_arg;
1203 arg.progress_cb = progress_cb;
1204 arg.rl = &rl;
1205 arg.nloose = nloose;
1206 arg.npurged = 0;
1207 arg.size_purged = 0;
1208 arg.ncommits = ncommits;
1209 arg.dry_run = dry_run;
1210 arg.max_mtime = max_mtime;
1211 arg.ignore_mtime = ignore_mtime;
1212 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1213 if (err)
1214 goto done;
1215 *size_after = *size_before - arg.size_purged;
1217 /* Produce a final progress report. */
1218 if (progress_cb) {
1219 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1220 if (err)
1221 goto done;
1223 done:
1224 got_object_idset_free(loose_ids);
1225 got_object_idset_free(traversed_ids);
1226 return err;
1229 static const struct got_error *
1230 remove_packidx(int dir_fd, const char *relpath)
1232 const struct got_error *err, *unlock_err;
1233 struct got_lockfile *lf;
1235 err = got_lockfile_lock(&lf, relpath, dir_fd);
1236 if (err)
1237 return err;
1238 if (unlinkat(dir_fd, relpath, 0) == -1)
1239 err = got_error_from_errno("unlinkat");
1240 unlock_err = got_lockfile_unlock(lf, dir_fd);
1241 return err ? err : unlock_err;
1244 const struct got_error *
1245 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1246 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1247 got_cancel_cb cancel_cb, void *cancel_arg)
1249 const struct got_error *err = NULL;
1250 DIR *packdir = NULL;
1251 struct dirent *dent;
1252 char *pack_relpath = NULL;
1253 int packdir_fd;
1254 struct stat sb;
1256 packdir_fd = openat(got_repo_get_fd(repo),
1257 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1258 if (packdir_fd == -1) {
1259 if (errno == ENOENT)
1260 return NULL;
1261 return got_error_from_errno_fmt("openat: %s/%s",
1262 got_repo_get_path_git_dir(repo),
1263 GOT_OBJECTS_PACK_DIR);
1266 packdir = fdopendir(packdir_fd);
1267 if (packdir == NULL) {
1268 err = got_error_from_errno("fdopendir");
1269 goto done;
1272 while ((dent = readdir(packdir)) != NULL) {
1273 if (cancel_cb) {
1274 err = cancel_cb(cancel_arg);
1275 if (err)
1276 goto done;
1279 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1280 continue;
1282 err = got_packidx_get_packfile_path(&pack_relpath,
1283 dent->d_name);
1284 if (err)
1285 goto done;
1287 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1288 free(pack_relpath);
1289 pack_relpath = NULL;
1290 continue;
1292 if (errno != ENOENT) {
1293 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1294 got_repo_get_path_git_dir(repo),
1295 GOT_OBJECTS_PACK_DIR,
1296 pack_relpath);
1297 goto done;
1300 if (!dry_run) {
1301 err = remove_packidx(packdir_fd, dent->d_name);
1302 if (err)
1303 goto done;
1305 if (progress_cb) {
1306 char *path;
1307 if (asprintf(&path, "%s/%s/%s",
1308 got_repo_get_path_git_dir(repo),
1309 GOT_OBJECTS_PACK_DIR,
1310 dent->d_name) == -1) {
1311 err = got_error_from_errno("asprintf");
1312 goto done;
1314 err = progress_cb(progress_arg, path);
1315 free(path);
1316 if (err)
1317 goto done;
1319 free(pack_relpath);
1320 pack_relpath = NULL;
1322 done:
1323 if (packdir && closedir(packdir) != 0 && err == NULL)
1324 err = got_error_from_errno("closedir");
1325 free(pack_relpath);
1326 return err;