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_pack_create.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_lockfile.h"
57 #include "got_lib_ratelimit.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 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
79 if (*ids == NULL)
80 return got_error_from_errno("reallocarray");
81 nalloc = alloc_chunksz;
83 TAILQ_FOREACH(re, refs, entry) {
84 struct got_object_id *id;
86 if (cancel_cb) {
87 err = cancel_cb(cancel_arg);
88 if (err)
89 goto done;
90 }
92 err = got_ref_resolve(&id, repo, re->ref);
93 if (err)
94 goto done;
96 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
97 int obj_type;
98 err = got_object_get_type(&obj_type, repo, id);
99 if (err)
100 goto done;
101 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
102 free(id);
103 id = NULL;
104 continue;
108 if (nalloc <= *nobjects) {
109 struct got_object_id **new;
110 new = recallocarray(*ids, nalloc,
111 nalloc + alloc_chunksz,
112 sizeof(struct got_object_id *));
113 if (new == NULL) {
114 err = got_error_from_errno(
115 "recallocarray");
116 goto done;
118 *ids = new;
119 nalloc += alloc_chunksz;
121 (*ids)[*nobjects] = id;
122 if ((*ids)[*nobjects] == NULL) {
123 err = got_error_from_errno("got_object_id_dup");
124 goto done;
126 (*nobjects)++;
128 done:
129 if (err) {
130 for (i = 0; i < *nobjects; i++)
131 free((*ids)[i]);
132 free(*ids);
133 *ids = NULL;
134 *nobjects = 0;
136 return err;
139 const struct got_error *
140 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
141 struct got_reflist_head *include_refs,
142 struct got_reflist_head *exclude_refs, struct got_repository *repo,
143 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
144 got_cancel_cb cancel_cb, void *cancel_arg)
146 const struct got_error *err = NULL;
147 struct got_object_id **ours = NULL, **theirs = NULL;
148 int nours = 0, ntheirs = 0, packfd = -1, i;
149 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
150 char *sha1_str = NULL;
152 *packfile = NULL;
153 *pack_hash = NULL;
155 if (asprintf(&path, "%s/%s/packing.pack",
156 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
157 err = got_error_from_errno("asprintf");
158 goto done;
160 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
161 if (err)
162 goto done;
164 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
165 err = got_error_from_errno2("fchmod", tmpfile_path);
166 goto done;
169 *packfile = fdopen(packfd, "w");
170 if (*packfile == NULL) {
171 err = got_error_from_errno2("fdopen", tmpfile_path);
172 goto done;
174 packfd = -1;
176 err = get_reflist_object_ids(&ours, &nours,
177 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
178 include_refs, repo, cancel_cb, cancel_arg);
179 if (err)
180 goto done;
182 if (nours == 0) {
183 err = got_error(GOT_ERR_CANNOT_PACK);
184 goto done;
187 if (!TAILQ_EMPTY(exclude_refs)) {
188 err = get_reflist_object_ids(&theirs, &ntheirs,
189 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
190 exclude_refs, repo,
191 cancel_cb, cancel_arg);
192 if (err)
193 goto done;
196 *pack_hash = calloc(1, sizeof(**pack_hash));
197 if (*pack_hash == NULL) {
198 err = got_error_from_errno("calloc");
199 goto done;
202 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
203 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
204 cancel_cb, cancel_arg);
205 if (err)
206 goto done;
208 err = got_object_id_str(&sha1_str, *pack_hash);
209 if (err)
210 goto done;
211 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
212 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
213 sha1_str) == -1) {
214 err = got_error_from_errno("asprintf");
215 goto done;
218 if (fflush(*packfile) == -1) {
219 err = got_error_from_errno("fflush");
220 goto done;
222 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
223 err = got_error_from_errno("fseek");
224 goto done;
226 if (rename(tmpfile_path, packfile_path) == -1) {
227 err = got_error_from_errno3("rename", tmpfile_path,
228 packfile_path);
229 goto done;
231 free(tmpfile_path);
232 tmpfile_path = NULL;
233 done:
234 for (i = 0; i < nours; i++)
235 free(ours[i]);
236 free(ours);
237 for (i = 0; i < ntheirs; i++)
238 free(theirs[i]);
239 free(theirs);
240 if (packfd != -1 && close(packfd) == -1 && err == NULL)
241 err = got_error_from_errno2("close", packfile_path);
242 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
243 err = got_error_from_errno2("unlink", tmpfile_path);
244 free(tmpfile_path);
245 free(packfile_path);
246 free(sha1_str);
247 free(path);
248 if (err) {
249 free(*pack_hash);
250 *pack_hash = NULL;
251 if (*packfile)
252 fclose(*packfile);
253 *packfile = NULL;
255 return err;
258 const struct got_error *
259 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
260 struct got_repository *repo,
261 got_pack_index_progress_cb progress_cb, void *progress_arg,
262 got_cancel_cb cancel_cb, void *cancel_arg)
264 size_t i;
265 char *path;
266 int imsg_idxfds[2];
267 int npackfd = -1, idxfd = -1, nidxfd = -1;
268 int tmpfds[3];
269 int idxstatus, done = 0;
270 const struct got_error *err;
271 struct imsgbuf idxibuf;
272 pid_t idxpid;
273 char *tmpidxpath = NULL;
274 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
275 const char *repo_path = got_repo_get_path_git_dir(repo);
276 struct stat sb;
278 for (i = 0; i < nitems(tmpfds); i++)
279 tmpfds[i] = -1;
281 if (asprintf(&path, "%s/%s/indexing.idx",
282 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
283 err = got_error_from_errno("asprintf");
284 goto done;
286 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
287 free(path);
288 if (err)
289 goto done;
290 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
291 err = got_error_from_errno2("fchmod", tmpidxpath);
292 goto done;
295 nidxfd = dup(idxfd);
296 if (nidxfd == -1) {
297 err = got_error_from_errno("dup");
298 goto done;
301 for (i = 0; i < nitems(tmpfds); i++) {
302 tmpfds[i] = got_opentempfd();
303 if (tmpfds[i] == -1) {
304 err = got_error_from_errno("got_opentempfd");
305 goto done;
309 err = got_object_id_str(&id_str, pack_hash);
310 if (err)
311 goto done;
313 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
314 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
315 err = got_error_from_errno("asprintf");
316 goto done;
319 if (fstat(fileno(packfile), &sb) == -1) {
320 err = got_error_from_errno2("fstat", packfile_path);
321 goto done;
324 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
325 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
326 err = got_error_from_errno("asprintf");
327 goto done;
330 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
331 err = got_error_from_errno("socketpair");
332 goto done;
334 idxpid = fork();
335 if (idxpid == -1) {
336 err= got_error_from_errno("fork");
337 goto done;
338 } else if (idxpid == 0)
339 got_privsep_exec_child(imsg_idxfds,
340 GOT_PATH_PROG_INDEX_PACK, packfile_path);
341 if (close(imsg_idxfds[1]) == -1) {
342 err = got_error_from_errno("close");
343 goto done;
345 imsg_init(&idxibuf, imsg_idxfds[0]);
347 npackfd = dup(fileno(packfile));
348 if (npackfd == -1) {
349 err = got_error_from_errno("dup");
350 goto done;
352 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
353 npackfd);
354 if (err != NULL)
355 goto done;
356 npackfd = -1;
357 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
358 if (err != NULL)
359 goto done;
360 nidxfd = -1;
361 for (i = 0; i < nitems(tmpfds); i++) {
362 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
363 if (err != NULL)
364 goto done;
365 tmpfds[i] = -1;
367 done = 0;
368 while (!done) {
369 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
371 if (cancel_cb) {
372 err = cancel_cb(cancel_arg);
373 if (err)
374 goto done;
377 err = got_privsep_recv_index_progress(&done, &nobj_total,
378 &nobj_indexed, &nobj_loose, &nobj_resolved,
379 &idxibuf);
380 if (err != NULL)
381 goto done;
382 if (nobj_indexed != 0) {
383 err = progress_cb(progress_arg, sb.st_size,
384 nobj_total, nobj_indexed, nobj_loose,
385 nobj_resolved);
386 if (err)
387 break;
389 imsg_clear(&idxibuf);
391 if (close(imsg_idxfds[0]) == -1) {
392 err = got_error_from_errno("close");
393 goto done;
395 if (waitpid(idxpid, &idxstatus, 0) == -1) {
396 err = got_error_from_errno("waitpid");
397 goto done;
400 if (rename(tmpidxpath, idxpath) == -1) {
401 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
402 goto done;
404 free(tmpidxpath);
405 tmpidxpath = NULL;
407 done:
408 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
409 err = got_error_from_errno2("unlink", tmpidxpath);
410 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
411 err = got_error_from_errno("close");
412 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
413 err = got_error_from_errno("close");
414 for (i = 0; i < nitems(tmpfds); i++) {
415 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
416 err = got_error_from_errno("close");
418 free(tmpidxpath);
419 free(idxpath);
420 free(packfile_path);
421 return err;
424 const struct got_error *
425 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
426 struct got_repository *repo, const char *packfile_path)
428 const struct got_error *err = NULL;
429 const char *packdir_path = NULL;
430 char *packfile_name = NULL, *p, *dot;
431 struct got_object_id id;
432 int packfd = -1;
434 *packfile = NULL;
435 *pack_hash = NULL;
437 packdir_path = got_repo_get_path_objects_pack(repo);
438 if (packdir_path == NULL)
439 return got_error_from_errno("got_repo_get_path_objects_pack");
441 if (!got_path_is_child(packfile_path, packdir_path,
442 strlen(packdir_path))) {
443 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
444 goto done;
448 err = got_path_basename(&packfile_name, packfile_path);
449 if (err)
450 goto done;
451 p = packfile_name;
453 if (strncmp(p, "pack-", 5) != 0) {
454 err = got_error_fmt(GOT_ERR_BAD_PATH,
455 "'%s' is not a valid pack file name",
456 packfile_name);
457 goto done;
459 p += 5;
460 dot = strchr(p, '.');
461 if (dot == NULL) {
462 err = got_error_fmt(GOT_ERR_BAD_PATH,
463 "'%s' is not a valid pack file name",
464 packfile_name);
465 goto done;
467 if (strcmp(dot + 1, "pack") != 0) {
468 err = got_error_fmt(GOT_ERR_BAD_PATH,
469 "'%s' is not a valid pack file name",
470 packfile_name);
471 goto done;
473 *dot = '\0';
474 if (!got_parse_sha1_digest(id.sha1, p)) {
475 err = got_error_fmt(GOT_ERR_BAD_PATH,
476 "'%s' is not a valid pack file name",
477 packfile_name);
478 goto done;
481 *pack_hash = got_object_id_dup(&id);
482 if (*pack_hash == NULL) {
483 err = got_error_from_errno("got_object_id_dup");
484 goto done;
487 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
488 if (packfd == -1) {
489 err = got_error_from_errno2("open", packfile_path);
490 goto done;
493 *packfile = fdopen(packfd, "r");
494 if (*packfile == NULL) {
495 err = got_error_from_errno2("fdopen", packfile_path);
496 goto done;
498 packfd = -1;
499 done:
500 if (packfd != -1 && close(packfd) == -1 && err == NULL)
501 err = got_error_from_errno2("close", packfile_path);
502 free(packfile_name);
503 if (err) {
504 free(*pack_hash);
505 *pack_hash = NULL;
507 return err;
510 const struct got_error *
511 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
512 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
513 got_cancel_cb cancel_cb, void *cancel_arg)
515 const struct got_error *err = NULL;
516 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
517 struct got_packidx *packidx = NULL;
518 struct got_pack *pack = NULL;
519 uint32_t nobj, i;
521 err = got_object_id_str(&id_str, pack_hash);
522 if (err)
523 goto done;
525 if (asprintf(&packpath, "%s/pack-%s.pack",
526 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
527 err = got_error_from_errno("asprintf");
528 goto done;
530 if (asprintf(&idxpath, "%s/pack-%s.idx",
531 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
532 err = got_error_from_errno("asprintf");
533 goto done;
536 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
537 if (err)
538 goto done;
540 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
541 if (err)
542 goto done;
544 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
545 for (i = 0; i < nobj; i++) {
546 struct got_packidx_object_id *oid;
547 struct got_object_id id, base_id;
548 off_t offset, base_offset = 0;
549 uint8_t type;
550 uint64_t size;
551 size_t tslen, len;
553 if (cancel_cb) {
554 err = cancel_cb(cancel_arg);
555 if (err)
556 break;
558 oid = &packidx->hdr.sorted_ids[i];
559 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
561 offset = got_packidx_get_object_offset(packidx, i);
562 if (offset == -1) {
563 err = got_error(GOT_ERR_BAD_PACKIDX);
564 goto done;
567 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
568 pack, offset);
569 if (err)
570 goto done;
572 switch (type) {
573 case GOT_OBJ_TYPE_OFFSET_DELTA:
574 err = got_pack_parse_offset_delta(&base_offset, &len,
575 pack, offset, tslen);
576 if (err)
577 goto done;
578 break;
579 case GOT_OBJ_TYPE_REF_DELTA:
580 err = got_pack_parse_ref_delta(&base_id,
581 pack, offset, tslen);
582 if (err)
583 goto done;
584 break;
586 err = (*list_cb)(list_arg, &id, type, offset, size,
587 base_offset, &base_id);
588 if (err)
589 goto done;
592 done:
593 free(id_str);
594 free(idxpath);
595 free(packpath);
596 if (packidx)
597 got_packidx_close(packidx);
598 return err;
601 static const struct got_error *
602 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
603 void *progress_arg, struct got_ratelimit *rl,
604 int nloose, int ncommits, int npurged)
606 const struct got_error *err;
607 int elapsed;
609 if (progress_cb == NULL)
610 return NULL;
612 err = got_ratelimit_check(&elapsed, rl);
613 if (err || !elapsed)
614 return err;
616 return progress_cb(progress_arg, nloose, ncommits, npurged);
619 static const struct got_error *
620 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
621 got_cleanup_progress_cb progress_cb, void *progress_arg,
622 struct got_ratelimit *rl, struct got_repository *repo)
624 const struct got_error *err = NULL;
625 char *path_objects = NULL, *path = NULL;
626 DIR *dir = NULL;
627 struct got_object *obj = NULL;
628 struct got_object_id id;
629 int i, fd = -1;
630 struct stat sb;
632 *ondisk_size = 0;
633 *loose_ids = got_object_idset_alloc();
634 if (*loose_ids == NULL)
635 return got_error_from_errno("got_object_idset_alloc");
637 path_objects = got_repo_get_path_objects(repo);
638 if (path_objects == NULL) {
639 err = got_error_from_errno("got_repo_get_path_objects");
640 goto done;
643 for (i = 0; i <= 0xff; i++) {
644 struct dirent *dent;
646 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
647 err = got_error_from_errno("asprintf");
648 break;
651 dir = opendir(path);
652 if (dir == NULL) {
653 if (errno == ENOENT) {
654 err = NULL;
655 continue;
657 err = got_error_from_errno2("opendir", path);
658 break;
661 while ((dent = readdir(dir)) != NULL) {
662 char *id_str;
664 if (strcmp(dent->d_name, ".") == 0 ||
665 strcmp(dent->d_name, "..") == 0)
666 continue;
668 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
669 err = got_error_from_errno("asprintf");
670 goto done;
673 memset(&id, 0, sizeof(id));
674 if (!got_parse_sha1_digest(id.sha1, id_str)) {
675 free(id_str);
676 continue;
678 free(id_str);
680 err = got_object_open_loose_fd(&fd, &id, repo);
681 if (err)
682 goto done;
683 if (fstat(fd, &sb) == -1) {
684 err = got_error_from_errno("fstat");
685 goto done;
687 err = got_object_read_header_privsep(&obj, &id, repo,
688 fd);
689 if (err)
690 goto done;
691 fd = -1; /* already closed */
693 switch (obj->type) {
694 case GOT_OBJ_TYPE_COMMIT:
695 case GOT_OBJ_TYPE_TREE:
696 case GOT_OBJ_TYPE_BLOB:
697 case GOT_OBJ_TYPE_TAG:
698 break;
699 default:
700 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
701 "%d", obj->type);
702 goto done;
704 got_object_close(obj);
705 obj = NULL;
706 (*ondisk_size) += sb.st_size;
707 err = got_object_idset_add(*loose_ids, &id, NULL);
708 if (err)
709 goto done;
710 err = report_cleanup_progress(progress_cb,
711 progress_arg, rl,
712 got_object_idset_num_elements(*loose_ids), -1, -1);
713 if (err)
714 goto done;
717 if (closedir(dir) != 0) {
718 err = got_error_from_errno("closedir");
719 goto done;
721 dir = NULL;
723 free(path);
724 path = NULL;
726 done:
727 if (dir && closedir(dir) != 0 && err == NULL)
728 err = got_error_from_errno("closedir");
729 if (fd != -1 && close(fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
731 if (err) {
732 got_object_idset_free(*loose_ids);
733 *loose_ids = NULL;
735 if (obj)
736 got_object_close(obj);
737 free(path_objects);
738 free(path);
739 return err;
742 static const struct got_error *
743 preserve_loose_object(struct got_object_idset *loose_ids,
744 struct got_object_id *id, struct got_repository *repo, int *npacked)
746 const struct got_error *err = NULL;
747 struct got_object *obj;
749 if (!got_object_idset_contains(loose_ids, id))
750 return NULL;
752 /*
753 * Try to open this object from a pack file. This ensures that
754 * we do in fact have a valid packed copy of the object. Otherwise
755 * we should not delete the loose representation of this object.
756 */
757 err = got_object_open_packed(&obj, id, repo);
758 if (err == NULL) {
759 got_object_close(obj);
760 /*
761 * The object is referenced and packed.
762 * We can purge the redundantly stored loose object.
763 */
764 (*npacked)++;
765 return NULL;
766 } else if (err->code != GOT_ERR_NO_OBJ)
767 return err;
769 /*
770 * This object is referenced and not packed.
771 * Remove it from our purge set.
772 */
773 return got_object_idset_remove(NULL, loose_ids, id);
776 static const struct got_error *
777 load_tree_entries(struct got_object_id_queue *ids,
778 struct got_object_idset *loose_ids,
779 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
780 const char *dpath, struct got_repository *repo, int *npacked,
781 got_cancel_cb cancel_cb, void *cancel_arg)
783 const struct got_error *err;
784 struct got_tree_object *tree;
785 char *p = NULL;
786 int i;
788 err = got_object_open_as_tree(&tree, repo, tree_id);
789 if (err)
790 return err;
792 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
793 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
794 struct got_object_id *id = got_tree_entry_get_id(e);
795 mode_t mode = got_tree_entry_get_mode(e);
797 if (cancel_cb) {
798 err = (*cancel_cb)(cancel_arg);
799 if (err)
800 break;
803 if (got_object_tree_entry_is_symlink(e) ||
804 got_object_tree_entry_is_submodule(e) ||
805 got_object_idset_contains(traversed_ids, id))
806 continue;
808 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
809 got_tree_entry_get_name(e)) == -1) {
810 err = got_error_from_errno("asprintf");
811 break;
814 if (S_ISDIR(mode)) {
815 struct got_object_qid *qid;
816 err = got_object_qid_alloc(&qid, id);
817 if (err)
818 break;
819 STAILQ_INSERT_TAIL(ids, qid, entry);
820 } else if (S_ISREG(mode)) {
821 /* This blob is referenced. */
822 err = preserve_loose_object(loose_ids, id, repo,
823 npacked);
824 if (err)
825 break;
826 err = got_object_idset_add(traversed_ids, id, NULL);
827 if (err)
828 break;
831 free(p);
832 p = NULL;
835 got_object_tree_close(tree);
836 free(p);
837 return err;
840 static const struct got_error *
841 load_tree(struct got_object_idset *loose_ids,
842 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
843 const char *dpath, struct got_repository *repo, int *npacked,
844 got_cancel_cb cancel_cb, void *cancel_arg)
846 const struct got_error *err = NULL;
847 struct got_object_id_queue tree_ids;
848 struct got_object_qid *qid;
850 err = got_object_qid_alloc(&qid, tree_id);
851 if (err)
852 return err;
854 STAILQ_INIT(&tree_ids);
855 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
857 while (!STAILQ_EMPTY(&tree_ids)) {
858 if (cancel_cb) {
859 err = (*cancel_cb)(cancel_arg);
860 if (err)
861 break;
864 qid = STAILQ_FIRST(&tree_ids);
865 STAILQ_REMOVE_HEAD(&tree_ids, entry);
867 if (got_object_idset_contains(traversed_ids, qid->id)) {
868 got_object_qid_free(qid);
869 continue;
872 err = got_object_idset_add(traversed_ids, qid->id, NULL);
873 if (err) {
874 got_object_qid_free(qid);
875 break;
878 /* This tree is referenced. */
879 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
880 if (err)
881 break;
883 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
884 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
885 got_object_qid_free(qid);
886 if (err)
887 break;
890 got_object_id_queue_free(&tree_ids);
891 return err;
894 static const struct got_error *
895 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
896 int *npacked, struct got_object_idset *traversed_ids,
897 struct got_object_id *id, struct got_repository *repo,
898 got_cleanup_progress_cb progress_cb, void *progress_arg,
899 struct got_ratelimit *rl, int nloose,
900 got_cancel_cb cancel_cb, void *cancel_arg)
902 const struct got_error *err;
903 struct got_commit_object *commit = NULL;
904 struct got_tag_object *tag = NULL;
905 struct got_object_id *tree_id = NULL;
906 struct got_object_id_queue ids;
907 struct got_object_qid *qid;
908 int obj_type;
910 err = got_object_qid_alloc(&qid, id);
911 if (err)
912 return err;
914 STAILQ_INIT(&ids);
915 STAILQ_INSERT_TAIL(&ids, qid, entry);
917 while (!STAILQ_EMPTY(&ids)) {
918 if (cancel_cb) {
919 err = (*cancel_cb)(cancel_arg);
920 if (err)
921 break;
924 qid = STAILQ_FIRST(&ids);
925 STAILQ_REMOVE_HEAD(&ids, entry);
927 if (got_object_idset_contains(traversed_ids, qid->id)) {
928 got_object_qid_free(qid);
929 qid = NULL;
930 continue;
933 err = got_object_idset_add(traversed_ids, qid->id, NULL);
934 if (err)
935 break;
937 /* This commit or tag is referenced. */
938 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
939 if (err)
940 break;
942 err = got_object_get_type(&obj_type, repo, qid->id);
943 if (err)
944 break;
945 switch (obj_type) {
946 case GOT_OBJ_TYPE_COMMIT:
947 err = got_object_open_as_commit(&commit, repo, qid->id);
948 if (err)
949 goto done;
950 break;
951 case GOT_OBJ_TYPE_TAG:
952 err = got_object_open_as_tag(&tag, repo, qid->id);
953 if (err)
954 goto done;
955 break;
956 default:
957 /* should not happen */
958 err = got_error(GOT_ERR_OBJ_TYPE);
959 goto done;
962 /* Find a tree object to scan. */
963 if (commit) {
964 tree_id = got_object_commit_get_tree_id(commit);
965 } else if (tag) {
966 obj_type = got_object_tag_get_object_type(tag);
967 switch (obj_type) {
968 case GOT_OBJ_TYPE_COMMIT:
969 err = got_object_open_as_commit(&commit, repo,
970 got_object_tag_get_object_id(tag));
971 if (err)
972 goto done;
973 tree_id = got_object_commit_get_tree_id(commit);
974 break;
975 case GOT_OBJ_TYPE_TREE:
976 tree_id = got_object_tag_get_object_id(tag);
977 break;
978 default:
979 /*
980 * Tag points at something other than a
981 * commit or tree. Leave this weird tag object
982 * and the object it points to on disk.
983 */
984 err = got_object_idset_remove(NULL, loose_ids,
985 qid->id);
986 if (err && err->code != GOT_ERR_NO_OBJ)
987 goto done;
988 err = got_object_idset_remove(NULL, loose_ids,
989 got_object_tag_get_object_id(tag));
990 if (err && err->code != GOT_ERR_NO_OBJ)
991 goto done;
992 err = NULL;
993 break;
997 if (tree_id) {
998 err = load_tree(loose_ids, traversed_ids, tree_id, "",
999 repo, npacked, cancel_cb, cancel_arg);
1000 if (err)
1001 break;
1004 if (commit || tag)
1005 (*ncommits)++; /* scanned tags are counted as commits */
1007 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1008 nloose, *ncommits, -1);
1009 if (err)
1010 break;
1012 if (commit) {
1013 /* Find parent commits to scan. */
1014 const struct got_object_id_queue *parent_ids;
1015 parent_ids = got_object_commit_get_parent_ids(commit);
1016 err = got_object_id_queue_copy(parent_ids, &ids);
1017 if (err)
1018 break;
1019 got_object_commit_close(commit);
1020 commit = NULL;
1022 if (tag) {
1023 got_object_tag_close(tag);
1024 tag = NULL;
1026 got_object_qid_free(qid);
1027 qid = NULL;
1029 done:
1030 if (qid)
1031 got_object_qid_free(qid);
1032 if (commit)
1033 got_object_commit_close(commit);
1034 if (tag)
1035 got_object_tag_close(tag);
1036 got_object_id_queue_free(&ids);
1037 return err;
1040 struct purge_loose_object_arg {
1041 struct got_repository *repo;
1042 got_cleanup_progress_cb progress_cb;
1043 void *progress_arg;
1044 struct got_ratelimit *rl;
1045 int nloose;
1046 int ncommits;
1047 int npurged;
1048 off_t size_purged;
1049 int dry_run;
1050 time_t max_mtime;
1051 int ignore_mtime;
1054 static const struct got_error *
1055 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1057 struct purge_loose_object_arg *a = arg;
1058 const struct got_error *err, *unlock_err = NULL;
1059 char *path = NULL;
1060 int fd = -1;
1061 struct stat sb;
1062 struct got_lockfile *lf = NULL;
1064 err = got_object_get_path(&path, id, a->repo);
1065 if (err)
1066 return err;
1068 err = got_object_open_loose_fd(&fd, id, a->repo);
1069 if (err)
1070 goto done;
1072 if (fstat(fd, &sb) == -1) {
1073 err = got_error_from_errno("fstat");
1074 goto done;
1078 * Do not delete objects which are younger than our maximum
1079 * modification time threshold. This prevents a race where
1080 * new objects which are being added to the repository
1081 * concurrently would be deleted.
1083 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1084 if (!a->dry_run) {
1085 err = got_lockfile_lock(&lf, path, -1);
1086 if (err)
1087 goto done;
1088 if (unlink(path) == -1) {
1089 err = got_error_from_errno2("unlink", path);
1090 goto done;
1094 a->npurged++;
1095 a->size_purged += sb.st_size;
1096 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1097 a->rl, a->nloose, a->ncommits, a->npurged);
1098 if (err)
1099 goto done;
1101 done:
1102 if (fd != -1 && close(fd) == -1 && err == NULL)
1103 err = got_error_from_errno("close");
1104 free(path);
1105 if (lf)
1106 unlock_err = got_lockfile_unlock(lf, -1);
1107 return err ? err : unlock_err;
1110 const struct got_error *
1111 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1112 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1113 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1114 got_cancel_cb cancel_cb, void *cancel_arg)
1116 const struct got_error *err;
1117 struct got_object_idset *loose_ids;
1118 struct got_object_idset *traversed_ids;
1119 struct got_object_id **referenced_ids;
1120 int i, nreferenced, nloose, ncommits = 0;
1121 struct got_reflist_head refs;
1122 struct got_reflist_entry *re;
1123 struct purge_loose_object_arg arg;
1124 time_t max_mtime = 0;
1125 struct got_ratelimit rl;
1127 TAILQ_INIT(&refs);
1128 got_ratelimit_init(&rl, 0, 500);
1130 *size_before = 0;
1131 *size_after = 0;
1132 *npacked = 0;
1134 err = get_loose_object_ids(&loose_ids, size_before,
1135 progress_cb, progress_arg, &rl, repo);
1136 if (err)
1137 return err;
1138 nloose = got_object_idset_num_elements(loose_ids);
1139 if (nloose == 0) {
1140 got_object_idset_free(loose_ids);
1141 if (progress_cb) {
1142 err = progress_cb(progress_arg, 0, 0, 0);
1143 if (err)
1144 return err;
1146 return NULL;
1149 traversed_ids = got_object_idset_alloc();
1150 if (traversed_ids == NULL) {
1151 err = got_error_from_errno("got_object_idset_alloc");
1152 goto done;
1155 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1156 if (err)
1157 goto done;
1158 if (!ignore_mtime) {
1159 TAILQ_FOREACH(re, &refs, entry) {
1160 time_t mtime = got_ref_get_mtime(re->ref);
1161 if (mtime > max_mtime)
1162 max_mtime = mtime;
1165 * For safety, keep objects created within 10 minutes
1166 * before the youngest reference was created.
1168 if (max_mtime >= 600)
1169 max_mtime -= 600;
1172 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1173 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1174 &refs, repo, cancel_cb, cancel_arg);
1175 if (err)
1176 goto done;
1178 for (i = 0; i < nreferenced; i++) {
1179 struct got_object_id *id = referenced_ids[i];
1180 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1181 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1182 nloose, cancel_cb, cancel_arg);
1183 if (err)
1184 goto done;
1187 /* Any remaining loose objects are unreferenced and can be purged. */
1188 arg.repo = repo;
1189 arg.progress_arg = progress_arg;
1190 arg.progress_cb = progress_cb;
1191 arg.rl = &rl;
1192 arg.nloose = nloose;
1193 arg.npurged = 0;
1194 arg.size_purged = 0;
1195 arg.ncommits = ncommits;
1196 arg.dry_run = dry_run;
1197 arg.max_mtime = max_mtime;
1198 arg.ignore_mtime = ignore_mtime;
1199 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1200 if (err)
1201 goto done;
1202 *size_after = *size_before - arg.size_purged;
1204 /* Produce a final progress report. */
1205 if (progress_cb) {
1206 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1207 if (err)
1208 goto done;
1210 done:
1211 got_object_idset_free(loose_ids);
1212 got_object_idset_free(traversed_ids);
1213 return err;
1216 static const struct got_error *
1217 remove_packidx(int dir_fd, const char *relpath)
1219 const struct got_error *err, *unlock_err;
1220 struct got_lockfile *lf;
1222 err = got_lockfile_lock(&lf, relpath, dir_fd);
1223 if (err)
1224 return err;
1225 if (unlinkat(dir_fd, relpath, 0) == -1)
1226 err = got_error_from_errno("unlinkat");
1227 unlock_err = got_lockfile_unlock(lf, dir_fd);
1228 return err ? err : unlock_err;
1231 const struct got_error *
1232 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1233 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1234 got_cancel_cb cancel_cb, void *cancel_arg)
1236 const struct got_error *err;
1237 DIR *packdir = NULL;
1238 struct dirent *dent;
1239 char *pack_relpath = NULL;
1240 int packdir_fd;
1241 struct stat sb;
1243 packdir_fd = openat(got_repo_get_fd(repo),
1244 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1245 if (packdir_fd == -1) {
1246 if (errno == ENOENT)
1247 return NULL;
1248 return got_error_from_errno_fmt("openat: %s/%s",
1249 got_repo_get_path_git_dir(repo),
1250 GOT_OBJECTS_PACK_DIR);
1253 packdir = fdopendir(packdir_fd);
1254 if (packdir == NULL) {
1255 err = got_error_from_errno("fdopendir");
1256 goto done;
1259 while ((dent = readdir(packdir)) != NULL) {
1260 if (cancel_cb) {
1261 err = cancel_cb(cancel_arg);
1262 if (err)
1263 goto done;
1266 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1267 continue;
1269 err = got_packidx_get_packfile_path(&pack_relpath,
1270 dent->d_name);
1271 if (err)
1272 goto done;
1274 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1275 free(pack_relpath);
1276 pack_relpath = NULL;
1277 continue;
1279 if (errno != ENOENT) {
1280 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1281 got_repo_get_path_git_dir(repo),
1282 GOT_OBJECTS_PACK_DIR,
1283 pack_relpath);
1284 goto done;
1287 if (!dry_run) {
1288 err = remove_packidx(packdir_fd, dent->d_name);
1289 if (err)
1290 goto done;
1292 if (progress_cb) {
1293 char *path;
1294 if (asprintf(&path, "%s/%s/%s",
1295 got_repo_get_path_git_dir(repo),
1296 GOT_OBJECTS_PACK_DIR,
1297 dent->d_name) == -1) {
1298 err = got_error_from_errno("asprintf");
1299 goto done;
1301 err = progress_cb(progress_arg, path);
1302 free(path);
1303 if (err)
1304 goto done;
1306 free(pack_relpath);
1307 pack_relpath = NULL;
1309 done:
1310 if (packdir && closedir(packdir) != 0 && err == NULL)
1311 err = got_error_from_errno("closedir");
1312 free(pack_relpath);
1313 return err;