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 search_packidx(int *found, struct got_object_id *id,
725 struct got_repository *repo)
727 const struct got_error *err = NULL;
728 struct got_packidx *packidx = NULL;
729 int idx;
731 *found = 0;
733 err = got_repo_search_packidx(&packidx, &idx, repo, id);
734 if (err == NULL)
735 *found = 1; /* object is already packed */
736 else if (err->code == GOT_ERR_NO_OBJ)
737 err = NULL;
738 return err;
741 static const struct got_error *
742 preserve_loose_object(struct got_object_idset *loose_ids,
743 struct got_object_id *id, struct got_repository *repo, int *npacked)
745 const struct got_error *err = NULL;
746 int is_packed;
748 if (!got_object_idset_contains(loose_ids, id))
749 return NULL;
751 err = search_packidx(&is_packed, id, repo);
752 if (err)
753 return err;
754 if (is_packed) {
755 struct got_object *obj;
757 /*
758 * Sanity check: Open the packed object to prevent a
759 * corrupt pack index from misleading us.
760 */
761 err = got_object_open_packed(&obj, id, repo);
762 if (err == NULL) {
763 got_object_close(obj);
764 /*
765 * The object is referenced and packed.
766 * We can purge the redundantly stored loose object.
767 */
768 (*npacked)++;
769 return NULL;
770 } else if (err->code != GOT_ERR_NO_OBJ)
771 return err;
774 /*
775 * This object is referenced and not packed.
776 * Remove it from our purge set.
777 */
778 return got_object_idset_remove(NULL, loose_ids, id);
781 static const struct got_error *
782 load_tree_entries(struct got_object_id_queue *ids,
783 struct got_object_idset *loose_ids,
784 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
785 const char *dpath, struct got_repository *repo, int *npacked,
786 got_cancel_cb cancel_cb, void *cancel_arg)
788 const struct got_error *err;
789 struct got_tree_object *tree;
790 char *p = NULL;
791 int i;
793 err = got_object_open_as_tree(&tree, repo, tree_id);
794 if (err)
795 return err;
797 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
798 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
799 struct got_object_id *id = got_tree_entry_get_id(e);
800 mode_t mode = got_tree_entry_get_mode(e);
802 if (cancel_cb) {
803 err = (*cancel_cb)(cancel_arg);
804 if (err)
805 break;
808 if (got_object_tree_entry_is_symlink(e) ||
809 got_object_tree_entry_is_submodule(e) ||
810 got_object_idset_contains(traversed_ids, id))
811 continue;
813 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
814 got_tree_entry_get_name(e)) == -1) {
815 err = got_error_from_errno("asprintf");
816 break;
819 if (S_ISDIR(mode)) {
820 struct got_object_qid *qid;
821 err = got_object_qid_alloc(&qid, id);
822 if (err)
823 break;
824 STAILQ_INSERT_TAIL(ids, qid, entry);
825 } else if (S_ISREG(mode)) {
826 /* This blob is referenced. */
827 err = preserve_loose_object(loose_ids, id, repo,
828 npacked);
829 if (err)
830 break;
831 err = got_object_idset_add(traversed_ids, id, NULL);
832 if (err)
833 break;
836 free(p);
837 p = NULL;
840 got_object_tree_close(tree);
841 free(p);
842 return err;
845 static const struct got_error *
846 load_tree(struct got_object_idset *loose_ids,
847 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
848 const char *dpath, struct got_repository *repo, int *npacked,
849 got_cancel_cb cancel_cb, void *cancel_arg)
851 const struct got_error *err = NULL;
852 struct got_object_id_queue tree_ids;
853 struct got_object_qid *qid;
855 err = got_object_qid_alloc(&qid, tree_id);
856 if (err)
857 return err;
859 STAILQ_INIT(&tree_ids);
860 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
862 while (!STAILQ_EMPTY(&tree_ids)) {
863 if (cancel_cb) {
864 err = (*cancel_cb)(cancel_arg);
865 if (err)
866 break;
869 qid = STAILQ_FIRST(&tree_ids);
870 STAILQ_REMOVE_HEAD(&tree_ids, entry);
872 if (got_object_idset_contains(traversed_ids, qid->id)) {
873 got_object_qid_free(qid);
874 continue;
877 err = got_object_idset_add(traversed_ids, qid->id, NULL);
878 if (err) {
879 got_object_qid_free(qid);
880 break;
883 /* This tree is referenced. */
884 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
885 if (err)
886 break;
888 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
889 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
890 got_object_qid_free(qid);
891 if (err)
892 break;
895 got_object_id_queue_free(&tree_ids);
896 return err;
899 static const struct got_error *
900 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
901 int *npacked, struct got_object_idset *traversed_ids,
902 struct got_object_id *id, struct got_repository *repo,
903 got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
904 got_cancel_cb cancel_cb, void *cancel_arg)
906 const struct got_error *err;
907 struct got_commit_object *commit = NULL;
908 struct got_tag_object *tag = NULL;
909 struct got_object_id *tree_id = NULL;
910 struct got_object_id_queue ids;
911 struct got_object_qid *qid;
912 int obj_type;
914 err = got_object_qid_alloc(&qid, id);
915 if (err)
916 return err;
918 STAILQ_INIT(&ids);
919 STAILQ_INSERT_TAIL(&ids, qid, entry);
921 while (!STAILQ_EMPTY(&ids)) {
922 if (cancel_cb) {
923 err = (*cancel_cb)(cancel_arg);
924 if (err)
925 break;
928 qid = STAILQ_FIRST(&ids);
929 STAILQ_REMOVE_HEAD(&ids, entry);
931 if (got_object_idset_contains(traversed_ids, qid->id)) {
932 got_object_qid_free(qid);
933 qid = NULL;
934 continue;
937 err = got_object_idset_add(traversed_ids, qid->id, NULL);
938 if (err)
939 break;
941 /* This commit or tag is referenced. */
942 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
943 if (err)
944 break;
946 err = got_object_get_type(&obj_type, repo, qid->id);
947 if (err)
948 break;
949 switch (obj_type) {
950 case GOT_OBJ_TYPE_COMMIT:
951 err = got_object_open_as_commit(&commit, repo, qid->id);
952 if (err)
953 goto done;
954 break;
955 case GOT_OBJ_TYPE_TAG:
956 err = got_object_open_as_tag(&tag, repo, qid->id);
957 if (err)
958 goto done;
959 break;
960 default:
961 /* should not happen */
962 err = got_error(GOT_ERR_OBJ_TYPE);
963 goto done;
966 /* Find a tree object to scan. */
967 if (commit) {
968 tree_id = got_object_commit_get_tree_id(commit);
969 } else if (tag) {
970 obj_type = got_object_tag_get_object_type(tag);
971 switch (obj_type) {
972 case GOT_OBJ_TYPE_COMMIT:
973 err = got_object_open_as_commit(&commit, repo,
974 got_object_tag_get_object_id(tag));
975 if (err)
976 goto done;
977 tree_id = got_object_commit_get_tree_id(commit);
978 break;
979 case GOT_OBJ_TYPE_TREE:
980 tree_id = got_object_tag_get_object_id(tag);
981 break;
982 default:
983 /*
984 * Tag points at something other than a
985 * commit or tree. Leave this weird tag object
986 * and the object it points to on disk.
987 */
988 err = got_object_idset_remove(NULL, loose_ids,
989 qid->id);
990 if (err && err->code != GOT_ERR_NO_OBJ)
991 goto done;
992 err = got_object_idset_remove(NULL, loose_ids,
993 got_object_tag_get_object_id(tag));
994 if (err && err->code != GOT_ERR_NO_OBJ)
995 goto done;
996 err = NULL;
997 break;
1001 if (tree_id) {
1002 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1003 repo, npacked, cancel_cb, cancel_arg);
1004 if (err)
1005 break;
1008 if (commit || tag)
1009 (*ncommits)++; /* scanned tags are counted as commits */
1011 if (progress_cb) {
1012 err = progress_cb(progress_arg, nloose, *ncommits, -1);
1013 if (err)
1014 break;
1017 if (commit) {
1018 /* Find parent commits to scan. */
1019 const struct got_object_id_queue *parent_ids;
1020 parent_ids = got_object_commit_get_parent_ids(commit);
1021 err = got_object_id_queue_copy(parent_ids, &ids);
1022 if (err)
1023 break;
1024 got_object_commit_close(commit);
1025 commit = NULL;
1027 if (tag) {
1028 got_object_tag_close(tag);
1029 tag = NULL;
1031 got_object_qid_free(qid);
1032 qid = NULL;
1034 done:
1035 if (qid)
1036 got_object_qid_free(qid);
1037 if (commit)
1038 got_object_commit_close(commit);
1039 if (tag)
1040 got_object_tag_close(tag);
1041 got_object_id_queue_free(&ids);
1042 return err;
1045 struct purge_loose_object_arg {
1046 struct got_repository *repo;
1047 got_cleanup_progress_cb progress_cb;
1048 void *progress_arg;
1049 int nloose;
1050 int ncommits;
1051 int npurged;
1052 off_t size_purged;
1053 int dry_run;
1056 static const struct got_error *
1057 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1059 struct purge_loose_object_arg *a = arg;
1060 const struct got_error *err, *unlock_err = NULL;
1061 char *path = NULL;
1062 int fd = -1;
1063 struct stat sb;
1064 struct got_lockfile *lf = NULL;
1066 err = got_object_get_path(&path, id, a->repo);
1067 if (err)
1068 return err;
1070 err = got_object_open_loose_fd(&fd, id, a->repo);
1071 if (err)
1072 goto done;
1074 if (fstat(fd, &sb) == -1) {
1075 err = got_error_from_errno("fstat");
1076 goto done;
1079 if (!a->dry_run) {
1080 err = got_lockfile_lock(&lf, path);
1081 if (err)
1082 goto done;
1083 if (unlink(path) == -1) {
1084 err = got_error_from_errno2("unlink", path);
1085 goto done;
1089 a->npurged++;
1090 a->size_purged += sb.st_size;
1091 if (a->progress_cb) {
1092 err = a->progress_cb(a->progress_arg, a->nloose,
1093 a->ncommits, a->npurged);
1095 done:
1096 if (fd != -1 && close(fd) == -1 && err == NULL)
1097 err = got_error_from_errno("close");
1098 free(path);
1099 if (lf)
1100 unlock_err = got_lockfile_unlock(lf);
1101 return err ? err : unlock_err;
1104 const struct got_error *
1105 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1106 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1107 got_cleanup_progress_cb progress_cb, void *progress_arg,
1108 got_cancel_cb cancel_cb, void *cancel_arg)
1110 const struct got_error *err;
1111 struct got_object_idset *loose_ids;
1112 struct got_object_idset *traversed_ids;
1113 struct got_object_id **referenced_ids;
1114 int i, nreferenced, nloose, ncommits = 0;
1115 struct got_reflist_head refs;
1116 struct purge_loose_object_arg arg;
1118 TAILQ_INIT(&refs);
1120 *size_before = 0;
1121 *size_after = 0;
1122 *npacked = 0;
1124 err = get_loose_object_ids(&loose_ids, size_before,
1125 progress_cb, progress_arg, repo);
1126 if (err)
1127 return err;
1128 nloose = got_object_idset_num_elements(loose_ids);
1129 if (nloose == 0) {
1130 got_object_idset_free(loose_ids);
1131 return NULL;
1134 traversed_ids = got_object_idset_alloc();
1135 if (traversed_ids == NULL) {
1136 err = got_error_from_errno("got_object_idset_alloc");
1137 goto done;
1140 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1141 if (err)
1142 goto done;
1144 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1145 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1146 &refs, repo, cancel_cb, cancel_arg);
1147 if (err)
1148 goto done;
1150 for (i = 0; i < nreferenced; i++) {
1151 struct got_object_id *id = referenced_ids[i];
1152 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1153 traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1154 cancel_cb, cancel_arg);
1155 if (err)
1156 goto done;
1159 /* Produce a final progress report in case no objects can be purged. */
1160 if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1161 err = progress_cb(progress_arg, nloose, ncommits, 0);
1162 if (err)
1163 goto done;
1166 /* Any remaining loose objects are unreferenced and can be purged. */
1167 arg.repo = repo;
1168 arg.progress_arg = progress_arg;
1169 arg.progress_cb = progress_cb;
1170 arg.nloose = nloose;
1171 arg.npurged = 0;
1172 arg.size_purged = 0;
1173 arg.ncommits = ncommits;
1174 arg.dry_run = dry_run;
1175 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1176 if (err)
1177 goto done;
1178 *size_after = *size_before - arg.size_purged;
1179 done:
1180 got_object_idset_free(loose_ids);
1181 got_object_idset_free(traversed_ids);
1182 return err;