Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_cancel.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
39 #include "got_repository_admin.h"
40 #include "got_opentemp.h"
41 #include "got_path.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_object_cache.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_pack_create.h"
51 #include "got_lib_sha1.h"
52 #include "got_lib_lockfile.h"
53 #include "got_lib_ratelimit.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static const struct got_error *
60 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
61 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
62 struct got_repository *repo,
63 got_cancel_cb cancel_cb, void *cancel_arg)
64 {
65 const struct got_error *err = NULL;
66 const size_t alloc_chunksz = 256;
67 size_t nalloc;
68 struct got_reflist_entry *re;
69 int i;
71 *ids = NULL;
72 *nobjects = 0;
74 err = got_reflist_sort(refs,
75 got_ref_cmp_by_commit_timestamp_descending, repo);
76 if (err)
77 return err;
79 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
80 if (*ids == NULL)
81 return got_error_from_errno("reallocarray");
82 nalloc = alloc_chunksz;
84 TAILQ_FOREACH(re, refs, entry) {
85 struct got_object_id *id;
87 if (cancel_cb) {
88 err = cancel_cb(cancel_arg);
89 if (err)
90 goto done;
91 }
93 err = got_ref_resolve(&id, repo, re->ref);
94 if (err)
95 goto done;
97 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
98 int obj_type;
99 err = got_object_get_type(&obj_type, repo, id);
100 if (err)
101 goto done;
102 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
103 free(id);
104 id = NULL;
105 continue;
109 if (nalloc <= *nobjects) {
110 struct got_object_id **new;
111 new = recallocarray(*ids, nalloc,
112 nalloc + alloc_chunksz,
113 sizeof(struct got_object_id *));
114 if (new == NULL) {
115 err = got_error_from_errno(
116 "recallocarray");
117 goto done;
119 *ids = new;
120 nalloc += alloc_chunksz;
122 (*ids)[*nobjects] = id;
123 if ((*ids)[*nobjects] == NULL) {
124 err = got_error_from_errno("got_object_id_dup");
125 goto done;
127 (*nobjects)++;
129 done:
130 if (err) {
131 for (i = 0; i < *nobjects; i++)
132 free((*ids)[i]);
133 free(*ids);
134 *ids = NULL;
135 *nobjects = 0;
137 return err;
140 const struct got_error *
141 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
142 struct got_reflist_head *include_refs,
143 struct got_reflist_head *exclude_refs, struct got_repository *repo,
144 int loose_obj_only,
145 got_pack_progress_cb progress_cb, void *progress_arg,
146 got_cancel_cb cancel_cb, void *cancel_arg)
148 const struct got_error *err = NULL;
149 struct got_object_id **ours = NULL, **theirs = NULL;
150 int nours = 0, ntheirs = 0, packfd = -1, i;
151 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
152 char *sha1_str = NULL;
154 *packfile = NULL;
155 *pack_hash = NULL;
157 if (asprintf(&path, "%s/%s/packing.pack",
158 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
159 err = got_error_from_errno("asprintf");
160 goto done;
162 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
163 if (err)
164 goto done;
166 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
167 err = got_error_from_errno2("fchmod", tmpfile_path);
168 goto done;
171 *packfile = fdopen(packfd, "w");
172 if (*packfile == NULL) {
173 err = got_error_from_errno2("fdopen", tmpfile_path);
174 goto done;
176 packfd = -1;
178 err = get_reflist_object_ids(&ours, &nours,
179 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
180 include_refs, repo, cancel_cb, cancel_arg);
181 if (err)
182 goto done;
184 if (nours == 0) {
185 err = got_error(GOT_ERR_CANNOT_PACK);
186 goto done;
189 if (!TAILQ_EMPTY(exclude_refs)) {
190 err = get_reflist_object_ids(&theirs, &ntheirs,
191 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
192 exclude_refs, repo,
193 cancel_cb, cancel_arg);
194 if (err)
195 goto done;
198 *pack_hash = calloc(1, sizeof(**pack_hash));
199 if (*pack_hash == NULL) {
200 err = got_error_from_errno("calloc");
201 goto done;
204 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
205 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
206 cancel_cb, cancel_arg);
207 if (err)
208 goto done;
210 err = got_object_id_str(&sha1_str, *pack_hash);
211 if (err)
212 goto done;
213 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
214 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
215 sha1_str) == -1) {
216 err = got_error_from_errno("asprintf");
217 goto done;
220 if (fflush(*packfile) == -1) {
221 err = got_error_from_errno("fflush");
222 goto done;
224 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
225 err = got_error_from_errno("fseek");
226 goto done;
228 if (rename(tmpfile_path, packfile_path) == -1) {
229 err = got_error_from_errno3("rename", tmpfile_path,
230 packfile_path);
231 goto done;
233 free(tmpfile_path);
234 tmpfile_path = NULL;
235 done:
236 for (i = 0; i < nours; i++)
237 free(ours[i]);
238 free(ours);
239 for (i = 0; i < ntheirs; i++)
240 free(theirs[i]);
241 free(theirs);
242 if (packfd != -1 && close(packfd) == -1 && err == NULL)
243 err = got_error_from_errno2("close", packfile_path);
244 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
245 err = got_error_from_errno2("unlink", tmpfile_path);
246 free(tmpfile_path);
247 free(packfile_path);
248 free(sha1_str);
249 free(path);
250 if (err) {
251 free(*pack_hash);
252 *pack_hash = NULL;
253 if (*packfile)
254 fclose(*packfile);
255 *packfile = NULL;
257 return err;
260 const struct got_error *
261 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
262 struct got_repository *repo,
263 got_pack_index_progress_cb progress_cb, void *progress_arg,
264 got_cancel_cb cancel_cb, void *cancel_arg)
266 size_t i;
267 char *path;
268 int imsg_idxfds[2];
269 int npackfd = -1, idxfd = -1, nidxfd = -1;
270 int tmpfds[3];
271 int idxstatus, done = 0;
272 const struct got_error *err;
273 struct imsgbuf idxibuf;
274 pid_t idxpid;
275 char *tmpidxpath = NULL;
276 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
277 const char *repo_path = got_repo_get_path_git_dir(repo);
278 struct stat sb;
280 for (i = 0; i < nitems(tmpfds); i++)
281 tmpfds[i] = -1;
283 if (asprintf(&path, "%s/%s/indexing.idx",
284 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
285 err = got_error_from_errno("asprintf");
286 goto done;
288 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
289 free(path);
290 if (err)
291 goto done;
292 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
293 err = got_error_from_errno2("fchmod", tmpidxpath);
294 goto done;
297 nidxfd = dup(idxfd);
298 if (nidxfd == -1) {
299 err = got_error_from_errno("dup");
300 goto done;
303 for (i = 0; i < nitems(tmpfds); i++) {
304 tmpfds[i] = got_opentempfd();
305 if (tmpfds[i] == -1) {
306 err = got_error_from_errno("got_opentempfd");
307 goto done;
311 err = got_object_id_str(&id_str, pack_hash);
312 if (err)
313 goto done;
315 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
316 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
317 err = got_error_from_errno("asprintf");
318 goto done;
321 if (fstat(fileno(packfile), &sb) == -1) {
322 err = got_error_from_errno2("fstat", packfile_path);
323 goto done;
326 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
327 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
328 err = got_error_from_errno("asprintf");
329 goto done;
332 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
333 err = got_error_from_errno("socketpair");
334 goto done;
336 idxpid = fork();
337 if (idxpid == -1) {
338 err= got_error_from_errno("fork");
339 goto done;
340 } else if (idxpid == 0)
341 got_privsep_exec_child(imsg_idxfds,
342 GOT_PATH_PROG_INDEX_PACK, packfile_path);
343 if (close(imsg_idxfds[1]) == -1) {
344 err = got_error_from_errno("close");
345 goto done;
347 imsg_init(&idxibuf, imsg_idxfds[0]);
349 npackfd = dup(fileno(packfile));
350 if (npackfd == -1) {
351 err = got_error_from_errno("dup");
352 goto done;
354 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
355 npackfd);
356 if (err != NULL)
357 goto done;
358 npackfd = -1;
359 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
360 if (err != NULL)
361 goto done;
362 nidxfd = -1;
363 for (i = 0; i < nitems(tmpfds); i++) {
364 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
365 if (err != NULL)
366 goto done;
367 tmpfds[i] = -1;
369 done = 0;
370 while (!done) {
371 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
373 if (cancel_cb) {
374 err = cancel_cb(cancel_arg);
375 if (err)
376 goto done;
379 err = got_privsep_recv_index_progress(&done, &nobj_total,
380 &nobj_indexed, &nobj_loose, &nobj_resolved,
381 &idxibuf);
382 if (err != NULL)
383 goto done;
384 if (nobj_indexed != 0) {
385 err = progress_cb(progress_arg, sb.st_size,
386 nobj_total, nobj_indexed, nobj_loose,
387 nobj_resolved);
388 if (err)
389 break;
392 if (close(imsg_idxfds[0]) == -1) {
393 err = got_error_from_errno("close");
394 goto done;
396 if (waitpid(idxpid, &idxstatus, 0) == -1) {
397 err = got_error_from_errno("waitpid");
398 goto done;
401 if (rename(tmpidxpath, idxpath) == -1) {
402 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
403 goto done;
405 free(tmpidxpath);
406 tmpidxpath = NULL;
408 done:
409 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
410 err = got_error_from_errno2("unlink", tmpidxpath);
411 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
412 err = got_error_from_errno("close");
413 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
414 err = got_error_from_errno("close");
415 for (i = 0; i < nitems(tmpfds); i++) {
416 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
417 err = got_error_from_errno("close");
419 free(tmpidxpath);
420 free(idxpath);
421 free(packfile_path);
422 return err;
425 const struct got_error *
426 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
427 struct got_repository *repo, const char *packfile_path)
429 const struct got_error *err = NULL;
430 const char *packdir_path = NULL;
431 char *packfile_name = NULL, *p, *dot;
432 struct got_object_id id;
433 int packfd = -1;
435 *packfile = NULL;
436 *pack_hash = NULL;
438 packdir_path = got_repo_get_path_objects_pack(repo);
439 if (packdir_path == NULL)
440 return got_error_from_errno("got_repo_get_path_objects_pack");
442 if (!got_path_is_child(packfile_path, packdir_path,
443 strlen(packdir_path))) {
444 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
445 goto done;
449 err = got_path_basename(&packfile_name, packfile_path);
450 if (err)
451 goto done;
452 p = packfile_name;
454 if (strncmp(p, "pack-", 5) != 0) {
455 err = got_error_fmt(GOT_ERR_BAD_PATH,
456 "'%s' is not a valid pack file name",
457 packfile_name);
458 goto done;
460 p += 5;
461 dot = strchr(p, '.');
462 if (dot == NULL) {
463 err = got_error_fmt(GOT_ERR_BAD_PATH,
464 "'%s' is not a valid pack file name",
465 packfile_name);
466 goto done;
468 if (strcmp(dot + 1, "pack") != 0) {
469 err = got_error_fmt(GOT_ERR_BAD_PATH,
470 "'%s' is not a valid pack file name",
471 packfile_name);
472 goto done;
474 *dot = '\0';
475 if (!got_parse_sha1_digest(id.sha1, p)) {
476 err = got_error_fmt(GOT_ERR_BAD_PATH,
477 "'%s' is not a valid pack file name",
478 packfile_name);
479 goto done;
482 *pack_hash = got_object_id_dup(&id);
483 if (*pack_hash == NULL) {
484 err = got_error_from_errno("got_object_id_dup");
485 goto done;
488 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
489 if (packfd == -1) {
490 err = got_error_from_errno2("open", packfile_path);
491 goto done;
494 *packfile = fdopen(packfd, "r");
495 if (*packfile == NULL) {
496 err = got_error_from_errno2("fdopen", packfile_path);
497 goto done;
499 packfd = -1;
500 done:
501 if (packfd != -1 && close(packfd) == -1 && err == NULL)
502 err = got_error_from_errno2("close", packfile_path);
503 free(packfile_name);
504 if (err) {
505 free(*pack_hash);
506 *pack_hash = NULL;
508 return err;
511 const struct got_error *
512 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
513 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
514 got_cancel_cb cancel_cb, void *cancel_arg)
516 const struct got_error *err = NULL;
517 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
518 struct got_packidx *packidx = NULL;
519 struct got_pack *pack = NULL;
520 uint32_t nobj, i;
522 err = got_object_id_str(&id_str, pack_hash);
523 if (err)
524 goto done;
526 if (asprintf(&packpath, "%s/pack-%s.pack",
527 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
528 err = got_error_from_errno("asprintf");
529 goto done;
531 if (asprintf(&idxpath, "%s/pack-%s.idx",
532 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
533 err = got_error_from_errno("asprintf");
534 goto done;
537 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
538 if (err)
539 goto done;
541 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
542 if (err)
543 goto done;
545 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
546 for (i = 0; i < nobj; i++) {
547 struct got_packidx_object_id *oid;
548 struct got_object_id id, base_id;
549 off_t offset, base_offset = 0;
550 uint8_t type;
551 uint64_t size;
552 size_t tslen, len;
554 if (cancel_cb) {
555 err = cancel_cb(cancel_arg);
556 if (err)
557 break;
559 oid = &packidx->hdr.sorted_ids[i];
560 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
562 offset = got_packidx_get_object_offset(packidx, i);
563 if (offset == -1) {
564 err = got_error(GOT_ERR_BAD_PACKIDX);
565 goto done;
568 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
569 pack, offset);
570 if (err)
571 goto done;
573 switch (type) {
574 case GOT_OBJ_TYPE_OFFSET_DELTA:
575 err = got_pack_parse_offset_delta(&base_offset, &len,
576 pack, offset, tslen);
577 if (err)
578 goto done;
579 break;
580 case GOT_OBJ_TYPE_REF_DELTA:
581 err = got_pack_parse_ref_delta(&base_id,
582 pack, offset, tslen);
583 if (err)
584 goto done;
585 break;
587 err = (*list_cb)(list_arg, &id, type, offset, size,
588 base_offset, &base_id);
589 if (err)
590 goto done;
593 done:
594 free(id_str);
595 free(idxpath);
596 free(packpath);
597 if (packidx)
598 got_packidx_close(packidx);
599 return err;
602 static const struct got_error *
603 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
604 void *progress_arg, struct got_ratelimit *rl,
605 int nloose, int ncommits, int npurged)
607 const struct got_error *err;
608 int elapsed;
610 if (progress_cb == NULL)
611 return NULL;
613 err = got_ratelimit_check(&elapsed, rl);
614 if (err || !elapsed)
615 return err;
617 return progress_cb(progress_arg, nloose, ncommits, npurged);
620 static const struct got_error *
621 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
622 got_cleanup_progress_cb progress_cb, void *progress_arg,
623 struct got_ratelimit *rl, struct got_repository *repo)
625 const struct got_error *err = NULL;
626 char *path_objects = NULL, *path = NULL;
627 DIR *dir = NULL;
628 struct got_object *obj = NULL;
629 struct got_object_id id;
630 int i, fd = -1;
631 struct stat sb;
633 *ondisk_size = 0;
634 *loose_ids = got_object_idset_alloc();
635 if (*loose_ids == NULL)
636 return got_error_from_errno("got_object_idset_alloc");
638 path_objects = got_repo_get_path_objects(repo);
639 if (path_objects == NULL) {
640 err = got_error_from_errno("got_repo_get_path_objects");
641 goto done;
644 for (i = 0; i <= 0xff; i++) {
645 struct dirent *dent;
647 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
648 err = got_error_from_errno("asprintf");
649 break;
652 dir = opendir(path);
653 if (dir == NULL) {
654 if (errno == ENOENT) {
655 err = NULL;
656 continue;
658 err = got_error_from_errno2("opendir", path);
659 break;
662 while ((dent = readdir(dir)) != NULL) {
663 char *id_str;
665 if (strcmp(dent->d_name, ".") == 0 ||
666 strcmp(dent->d_name, "..") == 0)
667 continue;
669 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
670 err = got_error_from_errno("asprintf");
671 goto done;
674 memset(&id, 0, sizeof(id));
675 if (!got_parse_sha1_digest(id.sha1, id_str)) {
676 free(id_str);
677 continue;
679 free(id_str);
681 err = got_object_open_loose_fd(&fd, &id, repo);
682 if (err)
683 goto done;
684 if (fstat(fd, &sb) == -1) {
685 err = got_error_from_errno("fstat");
686 goto done;
688 err = got_object_read_header_privsep(&obj, &id, repo,
689 fd);
690 if (err)
691 goto done;
692 fd = -1; /* already closed */
694 switch (obj->type) {
695 case GOT_OBJ_TYPE_COMMIT:
696 case GOT_OBJ_TYPE_TREE:
697 case GOT_OBJ_TYPE_BLOB:
698 case GOT_OBJ_TYPE_TAG:
699 break;
700 default:
701 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
702 "%d", obj->type);
703 goto done;
705 got_object_close(obj);
706 obj = NULL;
707 (*ondisk_size) += sb.st_size;
708 err = got_object_idset_add(*loose_ids, &id, NULL);
709 if (err)
710 goto done;
711 err = report_cleanup_progress(progress_cb,
712 progress_arg, rl,
713 got_object_idset_num_elements(*loose_ids), -1, -1);
714 if (err)
715 goto done;
718 if (closedir(dir) != 0) {
719 err = got_error_from_errno("closedir");
720 goto done;
722 dir = NULL;
724 free(path);
725 path = NULL;
727 done:
728 if (dir && closedir(dir) != 0 && err == NULL)
729 err = got_error_from_errno("closedir");
730 if (fd != -1 && close(fd) == -1 && err == NULL)
731 err = got_error_from_errno("close");
732 if (err) {
733 got_object_idset_free(*loose_ids);
734 *loose_ids = NULL;
736 if (obj)
737 got_object_close(obj);
738 free(path_objects);
739 free(path);
740 return err;
743 static const struct got_error *
744 preserve_loose_object(struct got_object_idset *loose_ids,
745 struct got_object_id *id, struct got_repository *repo, int *npacked)
747 const struct got_error *err = NULL;
748 struct got_object *obj;
750 if (!got_object_idset_contains(loose_ids, id))
751 return NULL;
753 /*
754 * Try to open this object from a pack file. This ensures that
755 * we do in fact have a valid packed copy of the object. Otherwise
756 * we should not delete the loose representation of this object.
757 */
758 err = got_object_open_packed(&obj, id, repo);
759 if (err == NULL) {
760 got_object_close(obj);
761 /*
762 * The object is referenced and packed.
763 * We can purge the redundantly stored loose object.
764 */
765 (*npacked)++;
766 return NULL;
767 } else if (err->code != GOT_ERR_NO_OBJ)
768 return err;
770 /*
771 * This object is referenced and not packed.
772 * Remove it from our purge set.
773 */
774 return got_object_idset_remove(NULL, loose_ids, id);
777 static const struct got_error *
778 load_tree_entries(struct got_object_id_queue *ids,
779 struct got_object_idset *loose_ids,
780 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
781 const char *dpath, struct got_repository *repo, int *npacked,
782 got_cancel_cb cancel_cb, void *cancel_arg)
784 const struct got_error *err;
785 struct got_tree_object *tree;
786 char *p = NULL;
787 int i;
789 err = got_object_open_as_tree(&tree, repo, tree_id);
790 if (err)
791 return err;
793 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
794 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
795 struct got_object_id *id = got_tree_entry_get_id(e);
796 mode_t mode = got_tree_entry_get_mode(e);
798 if (cancel_cb) {
799 err = (*cancel_cb)(cancel_arg);
800 if (err)
801 break;
804 if (got_object_tree_entry_is_symlink(e) ||
805 got_object_tree_entry_is_submodule(e) ||
806 got_object_idset_contains(traversed_ids, id))
807 continue;
809 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
810 got_tree_entry_get_name(e)) == -1) {
811 err = got_error_from_errno("asprintf");
812 break;
815 if (S_ISDIR(mode)) {
816 struct got_object_qid *qid;
817 err = got_object_qid_alloc(&qid, id);
818 if (err)
819 break;
820 STAILQ_INSERT_TAIL(ids, qid, entry);
821 } else if (S_ISREG(mode)) {
822 /* This blob is referenced. */
823 err = preserve_loose_object(loose_ids, id, repo,
824 npacked);
825 if (err)
826 break;
827 err = got_object_idset_add(traversed_ids, id, NULL);
828 if (err)
829 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,
948 &qid->id);
949 if (err)
950 goto done;
951 break;
952 case GOT_OBJ_TYPE_TAG:
953 err = got_object_open_as_tag(&tag, repo, &qid->id);
954 if (err)
955 goto done;
956 break;
957 default:
958 /* should not happen */
959 err = got_error(GOT_ERR_OBJ_TYPE);
960 goto done;
963 /* Find a tree object to scan. */
964 if (commit) {
965 tree_id = got_object_commit_get_tree_id(commit);
966 } else if (tag) {
967 obj_type = got_object_tag_get_object_type(tag);
968 switch (obj_type) {
969 case GOT_OBJ_TYPE_COMMIT:
970 err = got_object_open_as_commit(&commit, repo,
971 got_object_tag_get_object_id(tag));
972 if (err)
973 goto done;
974 tree_id = got_object_commit_get_tree_id(commit);
975 break;
976 case GOT_OBJ_TYPE_TREE:
977 tree_id = got_object_tag_get_object_id(tag);
978 break;
979 default:
980 /*
981 * Tag points at something other than a
982 * commit or tree. Leave this weird tag object
983 * and the object it points to on disk.
984 */
985 err = got_object_idset_remove(NULL, loose_ids,
986 &qid->id);
987 if (err && err->code != GOT_ERR_NO_OBJ)
988 goto done;
989 err = got_object_idset_remove(NULL, loose_ids,
990 got_object_tag_get_object_id(tag));
991 if (err && err->code != GOT_ERR_NO_OBJ)
992 goto done;
993 err = NULL;
994 break;
998 if (tree_id) {
999 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1000 repo, npacked, cancel_cb, cancel_arg);
1001 if (err)
1002 break;
1005 if (commit || tag)
1006 (*ncommits)++; /* scanned tags are counted as commits */
1008 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1009 nloose, *ncommits, -1);
1010 if (err)
1011 break;
1013 if (commit) {
1014 /* Find parent commits to scan. */
1015 const struct got_object_id_queue *parent_ids;
1016 parent_ids = got_object_commit_get_parent_ids(commit);
1017 err = got_object_id_queue_copy(parent_ids, &ids);
1018 if (err)
1019 break;
1020 got_object_commit_close(commit);
1021 commit = NULL;
1023 if (tag) {
1024 got_object_tag_close(tag);
1025 tag = NULL;
1027 got_object_qid_free(qid);
1028 qid = NULL;
1030 done:
1031 if (qid)
1032 got_object_qid_free(qid);
1033 if (commit)
1034 got_object_commit_close(commit);
1035 if (tag)
1036 got_object_tag_close(tag);
1037 got_object_id_queue_free(&ids);
1038 return err;
1041 struct purge_loose_object_arg {
1042 struct got_repository *repo;
1043 got_cleanup_progress_cb progress_cb;
1044 void *progress_arg;
1045 struct got_ratelimit *rl;
1046 int nloose;
1047 int ncommits;
1048 int npurged;
1049 off_t size_purged;
1050 int dry_run;
1051 time_t max_mtime;
1052 int ignore_mtime;
1055 static const struct got_error *
1056 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1058 struct purge_loose_object_arg *a = arg;
1059 const struct got_error *err, *unlock_err = NULL;
1060 char *path = NULL;
1061 int fd = -1;
1062 struct stat sb;
1063 struct got_lockfile *lf = NULL;
1065 err = got_object_get_path(&path, id, a->repo);
1066 if (err)
1067 return err;
1069 err = got_object_open_loose_fd(&fd, id, a->repo);
1070 if (err)
1071 goto done;
1073 if (fstat(fd, &sb) == -1) {
1074 err = got_error_from_errno("fstat");
1075 goto done;
1079 * Do not delete objects which are younger than our maximum
1080 * modification time threshold. This prevents a race where
1081 * new objects which are being added to the repository
1082 * concurrently would be deleted.
1084 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1085 if (!a->dry_run) {
1086 err = got_lockfile_lock(&lf, path, -1);
1087 if (err)
1088 goto done;
1089 if (unlink(path) == -1) {
1090 err = got_error_from_errno2("unlink", path);
1091 goto done;
1095 a->npurged++;
1096 a->size_purged += sb.st_size;
1097 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1098 a->rl, a->nloose, a->ncommits, a->npurged);
1099 if (err)
1100 goto done;
1102 done:
1103 if (fd != -1 && close(fd) == -1 && err == NULL)
1104 err = got_error_from_errno("close");
1105 free(path);
1106 if (lf)
1107 unlock_err = got_lockfile_unlock(lf, -1);
1108 return err ? err : unlock_err;
1111 const struct got_error *
1112 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1113 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1114 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1115 got_cancel_cb cancel_cb, void *cancel_arg)
1117 const struct got_error *err;
1118 struct got_object_idset *loose_ids;
1119 struct got_object_idset *traversed_ids;
1120 struct got_object_id **referenced_ids;
1121 int i, nreferenced, nloose, ncommits = 0;
1122 struct got_reflist_head refs;
1123 struct got_reflist_entry *re;
1124 struct purge_loose_object_arg arg;
1125 time_t max_mtime = 0;
1126 struct got_ratelimit rl;
1128 TAILQ_INIT(&refs);
1129 got_ratelimit_init(&rl, 0, 500);
1131 *size_before = 0;
1132 *size_after = 0;
1133 *npacked = 0;
1135 err = get_loose_object_ids(&loose_ids, size_before,
1136 progress_cb, progress_arg, &rl, repo);
1137 if (err)
1138 return err;
1139 nloose = got_object_idset_num_elements(loose_ids);
1140 if (nloose == 0) {
1141 got_object_idset_free(loose_ids);
1142 if (progress_cb) {
1143 err = progress_cb(progress_arg, 0, 0, 0);
1144 if (err)
1145 return err;
1147 return NULL;
1150 traversed_ids = got_object_idset_alloc();
1151 if (traversed_ids == NULL) {
1152 err = got_error_from_errno("got_object_idset_alloc");
1153 goto done;
1156 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1157 if (err)
1158 goto done;
1159 if (!ignore_mtime) {
1160 TAILQ_FOREACH(re, &refs, entry) {
1161 time_t mtime = got_ref_get_mtime(re->ref);
1162 if (mtime > max_mtime)
1163 max_mtime = mtime;
1166 * For safety, keep objects created within 10 minutes
1167 * before the youngest reference was created.
1169 if (max_mtime >= 600)
1170 max_mtime -= 600;
1173 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1174 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1175 &refs, repo, cancel_cb, cancel_arg);
1176 if (err)
1177 goto done;
1179 for (i = 0; i < nreferenced; i++) {
1180 struct got_object_id *id = referenced_ids[i];
1181 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1182 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1183 nloose, cancel_cb, cancel_arg);
1184 if (err)
1185 goto done;
1188 /* Any remaining loose objects are unreferenced and can be purged. */
1189 arg.repo = repo;
1190 arg.progress_arg = progress_arg;
1191 arg.progress_cb = progress_cb;
1192 arg.rl = &rl;
1193 arg.nloose = nloose;
1194 arg.npurged = 0;
1195 arg.size_purged = 0;
1196 arg.ncommits = ncommits;
1197 arg.dry_run = dry_run;
1198 arg.max_mtime = max_mtime;
1199 arg.ignore_mtime = ignore_mtime;
1200 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1201 if (err)
1202 goto done;
1203 *size_after = *size_before - arg.size_purged;
1205 /* Produce a final progress report. */
1206 if (progress_cb) {
1207 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1208 if (err)
1209 goto done;
1211 done:
1212 got_object_idset_free(loose_ids);
1213 got_object_idset_free(traversed_ids);
1214 return err;
1217 static const struct got_error *
1218 remove_packidx(int dir_fd, const char *relpath)
1220 const struct got_error *err, *unlock_err;
1221 struct got_lockfile *lf;
1223 err = got_lockfile_lock(&lf, relpath, dir_fd);
1224 if (err)
1225 return err;
1226 if (unlinkat(dir_fd, relpath, 0) == -1)
1227 err = got_error_from_errno("unlinkat");
1228 unlock_err = got_lockfile_unlock(lf, dir_fd);
1229 return err ? err : unlock_err;
1232 const struct got_error *
1233 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1234 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1235 got_cancel_cb cancel_cb, void *cancel_arg)
1237 const struct got_error *err = NULL;
1238 DIR *packdir = NULL;
1239 struct dirent *dent;
1240 char *pack_relpath = NULL;
1241 int packdir_fd;
1242 struct stat sb;
1244 packdir_fd = openat(got_repo_get_fd(repo),
1245 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1246 if (packdir_fd == -1) {
1247 if (errno == ENOENT)
1248 return NULL;
1249 return got_error_from_errno_fmt("openat: %s/%s",
1250 got_repo_get_path_git_dir(repo),
1251 GOT_OBJECTS_PACK_DIR);
1254 packdir = fdopendir(packdir_fd);
1255 if (packdir == NULL) {
1256 err = got_error_from_errno("fdopendir");
1257 goto done;
1260 while ((dent = readdir(packdir)) != NULL) {
1261 if (cancel_cb) {
1262 err = cancel_cb(cancel_arg);
1263 if (err)
1264 goto done;
1267 if (!got_repo_is_packidx_filename(dent->d_name,
1268 strlen(dent->d_name)))
1269 continue;
1271 err = got_packidx_get_packfile_path(&pack_relpath,
1272 dent->d_name);
1273 if (err)
1274 goto done;
1276 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1277 free(pack_relpath);
1278 pack_relpath = NULL;
1279 continue;
1281 if (errno != ENOENT) {
1282 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1283 got_repo_get_path_git_dir(repo),
1284 GOT_OBJECTS_PACK_DIR,
1285 pack_relpath);
1286 goto done;
1289 if (!dry_run) {
1290 err = remove_packidx(packdir_fd, dent->d_name);
1291 if (err)
1292 goto done;
1294 if (progress_cb) {
1295 char *path;
1296 if (asprintf(&path, "%s/%s/%s",
1297 got_repo_get_path_git_dir(repo),
1298 GOT_OBJECTS_PACK_DIR,
1299 dent->d_name) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 goto done;
1303 err = progress_cb(progress_arg, path);
1304 free(path);
1305 if (err)
1306 goto done;
1308 free(pack_relpath);
1309 pack_relpath = NULL;
1311 done:
1312 if (packdir && closedir(packdir) != 0 && err == NULL)
1313 err = got_error_from_errno("closedir");
1314 free(pack_relpath);
1315 return err;