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 <endian.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdint.h>
29 #include <sha1.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <unistd.h>
35 #include <imsg.h>
37 #include "got_error.h"
38 #include "got_cancel.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_repository_admin.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_idset.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_pack_create.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_lockfile.h"
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 static const struct got_error *
62 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
63 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
64 struct got_repository *repo,
65 got_cancel_cb cancel_cb, void *cancel_arg)
66 {
67 const struct got_error *err = NULL;
68 const size_t alloc_chunksz = 256;
69 size_t nalloc;
70 struct got_reflist_entry *re;
71 int i;
73 *ids = NULL;
74 *nobjects = 0;
76 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
77 if (*ids == NULL)
78 return got_error_from_errno("reallocarray");
79 nalloc = alloc_chunksz;
81 TAILQ_FOREACH(re, refs, entry) {
82 struct got_object_id *id;
84 if (cancel_cb) {
85 err = cancel_cb(cancel_arg);
86 if (err)
87 goto done;
88 }
90 err = got_ref_resolve(&id, repo, re->ref);
91 if (err)
92 goto done;
94 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
95 int obj_type;
96 err = got_object_get_type(&obj_type, repo, id);
97 if (err)
98 goto done;
99 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
100 free(id);
101 id = NULL;
102 continue;
106 if (nalloc <= *nobjects) {
107 struct got_object_id **new;
108 new = recallocarray(*ids, nalloc,
109 nalloc + alloc_chunksz,
110 sizeof(struct got_object_id *));
111 if (new == NULL) {
112 err = got_error_from_errno(
113 "recallocarray");
114 goto done;
116 *ids = new;
117 nalloc += alloc_chunksz;
119 (*ids)[*nobjects] = id;
120 if ((*ids)[*nobjects] == NULL) {
121 err = got_error_from_errno("got_object_id_dup");
122 goto done;
124 (*nobjects)++;
126 done:
127 if (err) {
128 for (i = 0; i < *nobjects; i++)
129 free((*ids)[i]);
130 free(*ids);
131 *ids = NULL;
132 *nobjects = 0;
134 return err;
137 const struct got_error *
138 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
139 struct got_reflist_head *include_refs,
140 struct got_reflist_head *exclude_refs, struct got_repository *repo,
141 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
142 got_cancel_cb cancel_cb, void *cancel_arg)
144 const struct got_error *err = NULL;
145 struct got_object_id **ours = NULL, **theirs = NULL;
146 int nours = 0, ntheirs = 0, packfd = -1, i;
147 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
148 char *sha1_str = NULL;
150 *packfile = NULL;
151 *pack_hash = NULL;
153 if (asprintf(&path, "%s/%s/packing.pack",
154 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
155 err = got_error_from_errno("asprintf");
156 goto done;
158 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
159 if (err)
160 goto done;
162 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
163 err = got_error_from_errno2("fchmod", tmpfile_path);
164 goto done;
167 *packfile = fdopen(packfd, "w");
168 if (*packfile == NULL) {
169 err = got_error_from_errno2("fdopen", tmpfile_path);
170 goto done;
172 packfd = -1;
174 err = get_reflist_object_ids(&ours, &nours,
175 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
176 include_refs, repo, cancel_cb, cancel_arg);
177 if (err)
178 goto done;
180 if (nours == 0) {
181 err = got_error(GOT_ERR_CANNOT_PACK);
182 goto done;
185 if (!TAILQ_EMPTY(exclude_refs)) {
186 err = get_reflist_object_ids(&theirs, &ntheirs,
187 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
188 exclude_refs, repo,
189 cancel_cb, cancel_arg);
190 if (err)
191 goto done;
194 *pack_hash = calloc(1, sizeof(**pack_hash));
195 if (*pack_hash == NULL) {
196 err = got_error_from_errno("calloc");
197 goto done;
200 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
201 ours, nours, repo, loose_obj_only, progress_cb, progress_arg,
202 cancel_cb, cancel_arg);
203 if (err)
204 goto done;
206 err = got_object_id_str(&sha1_str, *pack_hash);
207 if (err)
208 goto done;
209 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
210 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
211 sha1_str) == -1) {
212 err = got_error_from_errno("asprintf");
213 goto done;
216 if (fflush(*packfile) == -1) {
217 err = got_error_from_errno("fflush");
218 goto done;
220 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
221 err = got_error_from_errno("fseek");
222 goto done;
224 if (rename(tmpfile_path, packfile_path) == -1) {
225 err = got_error_from_errno3("rename", tmpfile_path,
226 packfile_path);
227 goto done;
229 free(tmpfile_path);
230 tmpfile_path = NULL;
231 done:
232 for (i = 0; i < nours; i++)
233 free(ours[i]);
234 free(ours);
235 for (i = 0; i < ntheirs; i++)
236 free(theirs[i]);
237 free(theirs);
238 if (packfd != -1 && close(packfd) == -1 && err == NULL)
239 err = got_error_from_errno2("close", packfile_path);
240 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
241 err = got_error_from_errno2("unlink", tmpfile_path);
242 free(tmpfile_path);
243 free(packfile_path);
244 free(sha1_str);
245 free(path);
246 if (err) {
247 free(*pack_hash);
248 *pack_hash = NULL;
249 if (*packfile)
250 fclose(*packfile);
251 *packfile = NULL;
253 return err;
256 const struct got_error *
257 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
258 struct got_repository *repo,
259 got_pack_index_progress_cb progress_cb, void *progress_arg,
260 got_cancel_cb cancel_cb, void *cancel_arg)
262 size_t i;
263 char *path;
264 int imsg_idxfds[2];
265 int npackfd = -1, idxfd = -1, nidxfd = -1;
266 int tmpfds[3];
267 int idxstatus, done = 0;
268 const struct got_error *err;
269 struct imsgbuf idxibuf;
270 pid_t idxpid;
271 char *tmpidxpath = NULL;
272 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
273 const char *repo_path = got_repo_get_path_git_dir(repo);
274 struct stat sb;
276 for (i = 0; i < nitems(tmpfds); i++)
277 tmpfds[i] = -1;
279 if (asprintf(&path, "%s/%s/indexing.idx",
280 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
281 err = got_error_from_errno("asprintf");
282 goto done;
284 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
285 free(path);
286 if (err)
287 goto done;
288 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
289 err = got_error_from_errno2("fchmod", tmpidxpath);
290 goto done;
293 nidxfd = dup(idxfd);
294 if (nidxfd == -1) {
295 err = got_error_from_errno("dup");
296 goto done;
299 for (i = 0; i < nitems(tmpfds); i++) {
300 tmpfds[i] = got_opentempfd();
301 if (tmpfds[i] == -1) {
302 err = got_error_from_errno("got_opentempfd");
303 goto done;
307 err = got_object_id_str(&id_str, pack_hash);
308 if (err)
309 goto done;
311 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
312 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
313 err = got_error_from_errno("asprintf");
314 goto done;
317 if (fstat(fileno(packfile), &sb) == -1) {
318 err = got_error_from_errno2("fstat", packfile_path);
319 goto done;
322 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
323 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
324 err = got_error_from_errno("asprintf");
325 goto done;
328 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
329 err = got_error_from_errno("socketpair");
330 goto done;
332 idxpid = fork();
333 if (idxpid == -1) {
334 err= got_error_from_errno("fork");
335 goto done;
336 } else if (idxpid == 0)
337 got_privsep_exec_child(imsg_idxfds,
338 GOT_PATH_PROG_INDEX_PACK, packfile_path);
339 if (close(imsg_idxfds[1]) == -1) {
340 err = got_error_from_errno("close");
341 goto done;
343 imsg_init(&idxibuf, imsg_idxfds[0]);
345 npackfd = dup(fileno(packfile));
346 if (npackfd == -1) {
347 err = got_error_from_errno("dup");
348 goto done;
350 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
351 npackfd);
352 if (err != NULL)
353 goto done;
354 npackfd = -1;
355 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
356 if (err != NULL)
357 goto done;
358 nidxfd = -1;
359 for (i = 0; i < nitems(tmpfds); i++) {
360 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
361 if (err != NULL)
362 goto done;
363 tmpfds[i] = -1;
365 done = 0;
366 while (!done) {
367 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
369 if (cancel_cb) {
370 err = cancel_cb(cancel_arg);
371 if (err)
372 goto done;
375 err = got_privsep_recv_index_progress(&done, &nobj_total,
376 &nobj_indexed, &nobj_loose, &nobj_resolved,
377 &idxibuf);
378 if (err != NULL)
379 goto done;
380 if (nobj_indexed != 0) {
381 err = progress_cb(progress_arg, sb.st_size,
382 nobj_total, nobj_indexed, nobj_loose,
383 nobj_resolved);
384 if (err)
385 break;
387 imsg_clear(&idxibuf);
389 if (close(imsg_idxfds[0]) == -1) {
390 err = got_error_from_errno("close");
391 goto done;
393 if (waitpid(idxpid, &idxstatus, 0) == -1) {
394 err = got_error_from_errno("waitpid");
395 goto done;
398 if (rename(tmpidxpath, idxpath) == -1) {
399 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
400 goto done;
402 free(tmpidxpath);
403 tmpidxpath = NULL;
405 done:
406 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
407 err = got_error_from_errno2("unlink", tmpidxpath);
408 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
409 err = got_error_from_errno("close");
410 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
411 err = got_error_from_errno("close");
412 for (i = 0; i < nitems(tmpfds); i++) {
413 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
414 err = got_error_from_errno("close");
416 free(tmpidxpath);
417 free(idxpath);
418 free(packfile_path);
419 return err;
422 const struct got_error *
423 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
424 struct got_repository *repo, const char *packfile_path)
426 const struct got_error *err = NULL;
427 const char *packdir_path = NULL;
428 char *packfile_name = NULL, *p, *dot;
429 struct got_object_id id;
430 int packfd = -1;
432 *packfile = NULL;
433 *pack_hash = NULL;
435 packdir_path = got_repo_get_path_objects_pack(repo);
436 if (packdir_path == NULL)
437 return got_error_from_errno("got_repo_get_path_objects_pack");
439 if (!got_path_is_child(packfile_path, packdir_path,
440 strlen(packdir_path))) {
441 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
442 goto done;
446 err = got_path_basename(&packfile_name, packfile_path);
447 if (err)
448 goto done;
449 p = packfile_name;
451 if (strncmp(p, "pack-", 5) != 0) {
452 err = got_error_fmt(GOT_ERR_BAD_PATH,
453 "'%s' is not a valid pack file name",
454 packfile_name);
455 goto done;
457 p += 5;
458 dot = strchr(p, '.');
459 if (dot == NULL) {
460 err = got_error_fmt(GOT_ERR_BAD_PATH,
461 "'%s' is not a valid pack file name",
462 packfile_name);
463 goto done;
465 if (strcmp(dot + 1, "pack") != 0) {
466 err = got_error_fmt(GOT_ERR_BAD_PATH,
467 "'%s' is not a valid pack file name",
468 packfile_name);
469 goto done;
471 *dot = '\0';
472 if (!got_parse_sha1_digest(id.sha1, p)) {
473 err = got_error_fmt(GOT_ERR_BAD_PATH,
474 "'%s' is not a valid pack file name",
475 packfile_name);
476 goto done;
479 *pack_hash = got_object_id_dup(&id);
480 if (*pack_hash == NULL) {
481 err = got_error_from_errno("got_object_id_dup");
482 goto done;
485 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW);
486 if (packfd == -1) {
487 err = got_error_from_errno2("open", packfile_path);
488 goto done;
491 *packfile = fdopen(packfd, "r");
492 if (*packfile == NULL) {
493 err = got_error_from_errno2("fdopen", packfile_path);
494 goto done;
496 packfd = -1;
497 done:
498 if (packfd != -1 && close(packfd) == -1 && err == NULL)
499 err = got_error_from_errno2("close", packfile_path);
500 free(packfile_name);
501 if (err) {
502 free(*pack_hash);
503 *pack_hash = NULL;
505 return err;
508 const struct got_error *
509 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
510 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
511 got_cancel_cb cancel_cb, void *cancel_arg)
513 const struct got_error *err = NULL;
514 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
515 struct got_packidx *packidx = NULL;
516 struct got_pack *pack = NULL;
517 uint32_t nobj, i;
519 err = got_object_id_str(&id_str, pack_hash);
520 if (err)
521 goto done;
523 if (asprintf(&packpath, "%s/pack-%s.pack",
524 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
525 err = got_error_from_errno("asprintf");
526 goto done;
528 if (asprintf(&idxpath, "%s/pack-%s.idx",
529 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
530 err = got_error_from_errno("asprintf");
531 goto done;
534 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
535 if (err)
536 goto done;
538 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
539 if (err)
540 goto done;
542 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
543 for (i = 0; i < nobj; i++) {
544 struct got_packidx_object_id *oid;
545 struct got_object_id id, base_id;
546 off_t offset, base_offset = 0;
547 uint8_t type;
548 uint64_t size;
549 size_t tslen, len;
551 if (cancel_cb) {
552 err = cancel_cb(cancel_arg);
553 if (err)
554 break;
556 oid = &packidx->hdr.sorted_ids[i];
557 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
559 offset = got_packidx_get_object_offset(packidx, i);
560 if (offset == -1) {
561 err = got_error(GOT_ERR_BAD_PACKIDX);
562 goto done;
565 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
566 pack, offset);
567 if (err)
568 goto done;
570 switch (type) {
571 case GOT_OBJ_TYPE_OFFSET_DELTA:
572 err = got_pack_parse_offset_delta(&base_offset, &len,
573 pack, offset, tslen);
574 if (err)
575 goto done;
576 break;
577 case GOT_OBJ_TYPE_REF_DELTA:
578 err = got_pack_parse_ref_delta(&base_id,
579 pack, offset, tslen);
580 if (err)
581 goto done;
582 break;
584 err = (*list_cb)(list_arg, &id, type, offset, size,
585 base_offset, &base_id);
586 if (err)
587 goto done;
590 done:
591 free(id_str);
592 free(idxpath);
593 free(packpath);
594 if (packidx)
595 got_packidx_close(packidx);
596 return err;
599 static const struct got_error *
600 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
601 got_cleanup_progress_cb progress_cb, void *progress_arg,
602 struct got_repository *repo)
604 const struct got_error *err = NULL;
605 char *path_objects = NULL, *path = NULL;
606 DIR *dir = NULL;
607 struct got_object *obj = NULL;
608 struct got_object_id id;
609 int i, fd = -1;
610 struct stat sb;
612 *ondisk_size = 0;
613 *loose_ids = got_object_idset_alloc();
614 if (*loose_ids == NULL)
615 return got_error_from_errno("got_object_idset_alloc");
617 path_objects = got_repo_get_path_objects(repo);
618 if (path_objects == NULL) {
619 err = got_error_from_errno("got_repo_get_path_objects");
620 goto done;
623 for (i = 0; i <= 0xff; i++) {
624 struct dirent *dent;
626 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
627 err = got_error_from_errno("asprintf");
628 break;
631 dir = opendir(path);
632 if (dir == NULL) {
633 if (errno == ENOENT) {
634 err = NULL;
635 continue;
637 err = got_error_from_errno2("opendir", path);
638 break;
641 while ((dent = readdir(dir)) != NULL) {
642 char *id_str;
644 if (strcmp(dent->d_name, ".") == 0 ||
645 strcmp(dent->d_name, "..") == 0)
646 continue;
648 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
649 err = got_error_from_errno("asprintf");
650 goto done;
653 memset(&id, 0, sizeof(id));
654 if (!got_parse_sha1_digest(id.sha1, id_str)) {
655 free(id_str);
656 continue;
658 free(id_str);
660 err = got_object_open_loose_fd(&fd, &id, repo);
661 if (err)
662 goto done;
663 if (fstat(fd, &sb) == -1) {
664 err = got_error_from_errno("fstat");
665 goto done;
667 err = got_object_read_header_privsep(&obj, repo, fd);
668 if (err)
669 goto done;
670 fd = -1; /* already closed */
672 switch (obj->type) {
673 case GOT_OBJ_TYPE_COMMIT:
674 case GOT_OBJ_TYPE_TREE:
675 case GOT_OBJ_TYPE_BLOB:
676 case GOT_OBJ_TYPE_TAG:
677 break;
678 default:
679 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
680 "%d", obj->type);
681 goto done;
683 got_object_close(obj);
684 obj = NULL;
685 (*ondisk_size) += sb.st_size;
686 err = got_object_idset_add(*loose_ids, &id, NULL);
687 if (err)
688 goto done;
689 if (progress_cb) {
690 err = progress_cb(progress_arg,
691 got_object_idset_num_elements(*loose_ids),
692 -1, -1);
693 if (err)
694 goto done;
698 if (closedir(dir) != 0) {
699 err = got_error_from_errno("closedir");
700 goto done;
702 dir = NULL;
704 free(path);
705 path = NULL;
707 done:
708 if (dir && closedir(dir) != 0 && err == NULL)
709 err = got_error_from_errno("closedir");
710 if (fd != -1 && close(fd) == -1 && err == NULL)
711 err = got_error_from_errno("close");
712 if (err) {
713 got_object_idset_free(*loose_ids);
714 *loose_ids = NULL;
716 if (obj)
717 got_object_close(obj);
718 free(path_objects);
719 free(path);
720 return err;
723 static const struct got_error *
724 preserve_loose_object(struct got_object_idset *loose_ids,
725 struct got_object_id *id, struct got_repository *repo, int *npacked)
727 const struct got_error *err = NULL;
728 struct got_object *obj;
730 if (!got_object_idset_contains(loose_ids, id))
731 return NULL;
733 /*
734 * Try to open this object from a pack file. This ensures that
735 * we do in fact have a valid packed copy of the object. Otherwise
736 * we should not delete the loose representation of this object.
737 */
738 err = got_object_open_packed(&obj, id, repo);
739 if (err == NULL) {
740 got_object_close(obj);
741 /*
742 * The object is referenced and packed.
743 * We can purge the redundantly stored loose object.
744 */
745 (*npacked)++;
746 return NULL;
747 } else if (err->code != GOT_ERR_NO_OBJ)
748 return err;
750 /*
751 * This object is referenced and not packed.
752 * Remove it from our purge set.
753 */
754 return got_object_idset_remove(NULL, loose_ids, id);
757 static const struct got_error *
758 load_tree_entries(struct got_object_id_queue *ids,
759 struct got_object_idset *loose_ids,
760 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
761 const char *dpath, struct got_repository *repo, int *npacked,
762 got_cancel_cb cancel_cb, void *cancel_arg)
764 const struct got_error *err;
765 struct got_tree_object *tree;
766 char *p = NULL;
767 int i;
769 err = got_object_open_as_tree(&tree, repo, tree_id);
770 if (err)
771 return err;
773 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
774 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
775 struct got_object_id *id = got_tree_entry_get_id(e);
776 mode_t mode = got_tree_entry_get_mode(e);
778 if (cancel_cb) {
779 err = (*cancel_cb)(cancel_arg);
780 if (err)
781 break;
784 if (got_object_tree_entry_is_symlink(e) ||
785 got_object_tree_entry_is_submodule(e) ||
786 got_object_idset_contains(traversed_ids, id))
787 continue;
789 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
790 got_tree_entry_get_name(e)) == -1) {
791 err = got_error_from_errno("asprintf");
792 break;
795 if (S_ISDIR(mode)) {
796 struct got_object_qid *qid;
797 err = got_object_qid_alloc(&qid, id);
798 if (err)
799 break;
800 STAILQ_INSERT_TAIL(ids, qid, entry);
801 } else if (S_ISREG(mode)) {
802 /* This blob is referenced. */
803 err = preserve_loose_object(loose_ids, id, repo,
804 npacked);
805 if (err)
806 break;
807 err = got_object_idset_add(traversed_ids, id, NULL);
808 if (err)
809 break;
812 free(p);
813 p = NULL;
816 got_object_tree_close(tree);
817 free(p);
818 return err;
821 static const struct got_error *
822 load_tree(struct got_object_idset *loose_ids,
823 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
824 const char *dpath, struct got_repository *repo, int *npacked,
825 got_cancel_cb cancel_cb, void *cancel_arg)
827 const struct got_error *err = NULL;
828 struct got_object_id_queue tree_ids;
829 struct got_object_qid *qid;
831 err = got_object_qid_alloc(&qid, tree_id);
832 if (err)
833 return err;
835 STAILQ_INIT(&tree_ids);
836 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
838 while (!STAILQ_EMPTY(&tree_ids)) {
839 if (cancel_cb) {
840 err = (*cancel_cb)(cancel_arg);
841 if (err)
842 break;
845 qid = STAILQ_FIRST(&tree_ids);
846 STAILQ_REMOVE_HEAD(&tree_ids, entry);
848 if (got_object_idset_contains(traversed_ids, qid->id)) {
849 got_object_qid_free(qid);
850 continue;
853 err = got_object_idset_add(traversed_ids, qid->id, NULL);
854 if (err) {
855 got_object_qid_free(qid);
856 break;
859 /* This tree is referenced. */
860 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
861 if (err)
862 break;
864 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
865 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
866 got_object_qid_free(qid);
867 if (err)
868 break;
871 got_object_id_queue_free(&tree_ids);
872 return err;
875 static const struct got_error *
876 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
877 int *npacked, struct got_object_idset *traversed_ids,
878 struct got_object_id *id, struct got_repository *repo,
879 got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
880 got_cancel_cb cancel_cb, void *cancel_arg)
882 const struct got_error *err;
883 struct got_commit_object *commit = NULL;
884 struct got_tag_object *tag = NULL;
885 struct got_object_id *tree_id = NULL;
886 struct got_object_id_queue ids;
887 struct got_object_qid *qid;
888 int obj_type;
890 err = got_object_qid_alloc(&qid, id);
891 if (err)
892 return err;
894 STAILQ_INIT(&ids);
895 STAILQ_INSERT_TAIL(&ids, qid, entry);
897 while (!STAILQ_EMPTY(&ids)) {
898 if (cancel_cb) {
899 err = (*cancel_cb)(cancel_arg);
900 if (err)
901 break;
904 qid = STAILQ_FIRST(&ids);
905 STAILQ_REMOVE_HEAD(&ids, entry);
907 if (got_object_idset_contains(traversed_ids, qid->id)) {
908 got_object_qid_free(qid);
909 qid = NULL;
910 continue;
913 err = got_object_idset_add(traversed_ids, qid->id, NULL);
914 if (err)
915 break;
917 /* This commit or tag is referenced. */
918 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
919 if (err)
920 break;
922 err = got_object_get_type(&obj_type, repo, qid->id);
923 if (err)
924 break;
925 switch (obj_type) {
926 case GOT_OBJ_TYPE_COMMIT:
927 err = got_object_open_as_commit(&commit, repo, qid->id);
928 if (err)
929 goto done;
930 break;
931 case GOT_OBJ_TYPE_TAG:
932 err = got_object_open_as_tag(&tag, repo, qid->id);
933 if (err)
934 goto done;
935 break;
936 default:
937 /* should not happen */
938 err = got_error(GOT_ERR_OBJ_TYPE);
939 goto done;
942 /* Find a tree object to scan. */
943 if (commit) {
944 tree_id = got_object_commit_get_tree_id(commit);
945 } else if (tag) {
946 obj_type = got_object_tag_get_object_type(tag);
947 switch (obj_type) {
948 case GOT_OBJ_TYPE_COMMIT:
949 err = got_object_open_as_commit(&commit, repo,
950 got_object_tag_get_object_id(tag));
951 if (err)
952 goto done;
953 tree_id = got_object_commit_get_tree_id(commit);
954 break;
955 case GOT_OBJ_TYPE_TREE:
956 tree_id = got_object_tag_get_object_id(tag);
957 break;
958 default:
959 /*
960 * Tag points at something other than a
961 * commit or tree. Leave this weird tag object
962 * and the object it points to on disk.
963 */
964 err = got_object_idset_remove(NULL, loose_ids,
965 qid->id);
966 if (err && err->code != GOT_ERR_NO_OBJ)
967 goto done;
968 err = got_object_idset_remove(NULL, loose_ids,
969 got_object_tag_get_object_id(tag));
970 if (err && err->code != GOT_ERR_NO_OBJ)
971 goto done;
972 err = NULL;
973 break;
977 if (tree_id) {
978 err = load_tree(loose_ids, traversed_ids, tree_id, "",
979 repo, npacked, cancel_cb, cancel_arg);
980 if (err)
981 break;
984 if (commit || tag)
985 (*ncommits)++; /* scanned tags are counted as commits */
987 if (progress_cb) {
988 err = progress_cb(progress_arg, nloose, *ncommits, -1);
989 if (err)
990 break;
993 if (commit) {
994 /* Find parent commits to scan. */
995 const struct got_object_id_queue *parent_ids;
996 parent_ids = got_object_commit_get_parent_ids(commit);
997 err = got_object_id_queue_copy(parent_ids, &ids);
998 if (err)
999 break;
1000 got_object_commit_close(commit);
1001 commit = NULL;
1003 if (tag) {
1004 got_object_tag_close(tag);
1005 tag = NULL;
1007 got_object_qid_free(qid);
1008 qid = NULL;
1010 done:
1011 if (qid)
1012 got_object_qid_free(qid);
1013 if (commit)
1014 got_object_commit_close(commit);
1015 if (tag)
1016 got_object_tag_close(tag);
1017 got_object_id_queue_free(&ids);
1018 return err;
1021 struct purge_loose_object_arg {
1022 struct got_repository *repo;
1023 got_cleanup_progress_cb progress_cb;
1024 void *progress_arg;
1025 int nloose;
1026 int ncommits;
1027 int npurged;
1028 off_t size_purged;
1029 int dry_run;
1032 static const struct got_error *
1033 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1035 struct purge_loose_object_arg *a = arg;
1036 const struct got_error *err, *unlock_err = NULL;
1037 char *path = NULL;
1038 int fd = -1;
1039 struct stat sb;
1040 struct got_lockfile *lf = NULL;
1042 err = got_object_get_path(&path, id, a->repo);
1043 if (err)
1044 return err;
1046 err = got_object_open_loose_fd(&fd, id, a->repo);
1047 if (err)
1048 goto done;
1050 if (fstat(fd, &sb) == -1) {
1051 err = got_error_from_errno("fstat");
1052 goto done;
1055 if (!a->dry_run) {
1056 err = got_lockfile_lock(&lf, path, -1);
1057 if (err)
1058 goto done;
1059 if (unlink(path) == -1) {
1060 err = got_error_from_errno2("unlink", path);
1061 goto done;
1065 a->npurged++;
1066 a->size_purged += sb.st_size;
1067 if (a->progress_cb) {
1068 err = a->progress_cb(a->progress_arg, a->nloose,
1069 a->ncommits, a->npurged);
1071 done:
1072 if (fd != -1 && close(fd) == -1 && err == NULL)
1073 err = got_error_from_errno("close");
1074 free(path);
1075 if (lf)
1076 unlock_err = got_lockfile_unlock(lf, -1);
1077 return err ? err : unlock_err;
1080 const struct got_error *
1081 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1082 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1083 got_cleanup_progress_cb progress_cb, void *progress_arg,
1084 got_cancel_cb cancel_cb, void *cancel_arg)
1086 const struct got_error *err;
1087 struct got_object_idset *loose_ids;
1088 struct got_object_idset *traversed_ids;
1089 struct got_object_id **referenced_ids;
1090 int i, nreferenced, nloose, ncommits = 0;
1091 struct got_reflist_head refs;
1092 struct purge_loose_object_arg arg;
1094 TAILQ_INIT(&refs);
1096 *size_before = 0;
1097 *size_after = 0;
1098 *npacked = 0;
1100 err = get_loose_object_ids(&loose_ids, size_before,
1101 progress_cb, progress_arg, repo);
1102 if (err)
1103 return err;
1104 nloose = got_object_idset_num_elements(loose_ids);
1105 if (nloose == 0) {
1106 got_object_idset_free(loose_ids);
1107 return NULL;
1110 traversed_ids = got_object_idset_alloc();
1111 if (traversed_ids == NULL) {
1112 err = got_error_from_errno("got_object_idset_alloc");
1113 goto done;
1116 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1117 if (err)
1118 goto done;
1120 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1121 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1122 &refs, repo, cancel_cb, cancel_arg);
1123 if (err)
1124 goto done;
1126 for (i = 0; i < nreferenced; i++) {
1127 struct got_object_id *id = referenced_ids[i];
1128 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1129 traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1130 cancel_cb, cancel_arg);
1131 if (err)
1132 goto done;
1135 /* Produce a final progress report in case no objects can be purged. */
1136 if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1137 err = progress_cb(progress_arg, nloose, ncommits, 0);
1138 if (err)
1139 goto done;
1142 /* Any remaining loose objects are unreferenced and can be purged. */
1143 arg.repo = repo;
1144 arg.progress_arg = progress_arg;
1145 arg.progress_cb = progress_cb;
1146 arg.nloose = nloose;
1147 arg.npurged = 0;
1148 arg.size_purged = 0;
1149 arg.ncommits = ncommits;
1150 arg.dry_run = dry_run;
1151 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1152 if (err)
1153 goto done;
1154 *size_after = *size_before - arg.size_purged;
1155 done:
1156 got_object_idset_free(loose_ids);
1157 got_object_idset_free(traversed_ids);
1158 return err;