Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/uio.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
25 #include <dirent.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <unistd.h>
36 #include <imsg.h>
38 #include "got_error.h"
39 #include "got_cancel.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_repository_admin.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_idset.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_pack_create.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_lockfile.h"
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static const struct got_error *
63 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
64 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
65 struct got_repository *repo,
66 got_cancel_cb cancel_cb, void *cancel_arg)
67 {
68 const struct got_error *err = NULL;
69 const size_t alloc_chunksz = 256;
70 size_t nalloc;
71 struct got_reflist_entry *re;
72 int i;
74 *ids = NULL;
75 *nobjects = 0;
77 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
78 if (*ids == NULL)
79 return got_error_from_errno("reallocarray");
80 nalloc = alloc_chunksz;
82 TAILQ_FOREACH(re, refs, entry) {
83 struct got_object_id *id;
85 if (cancel_cb) {
86 err = cancel_cb(cancel_arg);
87 if (err)
88 goto done;
89 }
91 err = got_ref_resolve(&id, repo, re->ref);
92 if (err)
93 goto done;
95 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
96 int obj_type;
97 err = got_object_get_type(&obj_type, repo, id);
98 if (err)
99 goto done;
100 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
101 free(id);
102 id = NULL;
103 continue;
107 if (nalloc <= *nobjects) {
108 struct got_object_id **new;
109 new = recallocarray(*ids, nalloc,
110 nalloc + alloc_chunksz,
111 sizeof(struct got_object_id *));
112 if (new == NULL) {
113 err = got_error_from_errno(
114 "recallocarray");
115 goto done;
117 *ids = new;
118 nalloc += alloc_chunksz;
120 (*ids)[*nobjects] = id;
121 if ((*ids)[*nobjects] == NULL) {
122 err = got_error_from_errno("got_object_id_dup");
123 goto done;
125 (*nobjects)++;
127 done:
128 if (err) {
129 for (i = 0; i < *nobjects; i++)
130 free((*ids)[i]);
131 free(*ids);
132 *ids = NULL;
133 *nobjects = 0;
135 return err;
138 const struct got_error *
139 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
140 struct got_reflist_head *include_refs,
141 struct got_reflist_head *exclude_refs, struct got_repository *repo,
142 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
143 got_cancel_cb cancel_cb, void *cancel_arg)
145 const struct got_error *err = NULL;
146 struct got_object_id **ours = NULL, **theirs = NULL;
147 int nours = 0, ntheirs = 0, packfd = -1, i;
148 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
149 char *sha1_str = NULL;
151 *packfile = NULL;
152 *pack_hash = NULL;
154 if (asprintf(&path, "%s/%s/packing.pack",
155 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
156 err = got_error_from_errno("asprintf");
157 goto done;
159 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
160 if (err)
161 goto done;
163 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
164 err = got_error_from_errno2("fchmod", tmpfile_path);
165 goto done;
168 *packfile = fdopen(packfd, "w");
169 if (*packfile == NULL) {
170 err = got_error_from_errno2("fdopen", tmpfile_path);
171 goto done;
173 packfd = -1;
175 err = get_reflist_object_ids(&ours, &nours,
176 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
177 include_refs, repo, cancel_cb, cancel_arg);
178 if (err)
179 goto done;
181 if (nours == 0) {
182 err = got_error(GOT_ERR_CANNOT_PACK);
183 goto done;
186 if (!TAILQ_EMPTY(exclude_refs)) {
187 err = get_reflist_object_ids(&theirs, &ntheirs,
188 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
189 exclude_refs, repo,
190 cancel_cb, cancel_arg);
191 if (err)
192 goto done;
195 *pack_hash = calloc(1, sizeof(**pack_hash));
196 if (*pack_hash == NULL) {
197 err = got_error_from_errno("calloc");
198 goto done;
201 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
202 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
203 cancel_cb, cancel_arg);
204 if (err)
205 goto done;
207 err = got_object_id_str(&sha1_str, *pack_hash);
208 if (err)
209 goto done;
210 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
211 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
212 sha1_str) == -1) {
213 err = got_error_from_errno("asprintf");
214 goto done;
217 if (fflush(*packfile) == -1) {
218 err = got_error_from_errno("fflush");
219 goto done;
221 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
222 err = got_error_from_errno("fseek");
223 goto done;
225 if (rename(tmpfile_path, packfile_path) == -1) {
226 err = got_error_from_errno3("rename", tmpfile_path,
227 packfile_path);
228 goto done;
230 free(tmpfile_path);
231 tmpfile_path = NULL;
232 done:
233 for (i = 0; i < nours; i++)
234 free(ours[i]);
235 free(ours);
236 for (i = 0; i < ntheirs; i++)
237 free(theirs[i]);
238 free(theirs);
239 if (packfd != -1 && close(packfd) == -1 && err == NULL)
240 err = got_error_from_errno2("close", packfile_path);
241 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
242 err = got_error_from_errno2("unlink", tmpfile_path);
243 free(tmpfile_path);
244 free(packfile_path);
245 free(sha1_str);
246 free(path);
247 if (err) {
248 free(*pack_hash);
249 *pack_hash = NULL;
250 if (*packfile)
251 fclose(*packfile);
252 *packfile = NULL;
254 return err;
257 const struct got_error *
258 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
259 struct got_repository *repo,
260 got_pack_index_progress_cb progress_cb, void *progress_arg,
261 got_cancel_cb cancel_cb, void *cancel_arg)
263 size_t i;
264 char *path;
265 int imsg_idxfds[2];
266 int npackfd = -1, idxfd = -1, nidxfd = -1;
267 int tmpfds[3];
268 int idxstatus, done = 0;
269 const struct got_error *err;
270 struct imsgbuf idxibuf;
271 pid_t idxpid;
272 char *tmpidxpath = NULL;
273 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
274 const char *repo_path = got_repo_get_path_git_dir(repo);
275 struct stat sb;
277 for (i = 0; i < nitems(tmpfds); i++)
278 tmpfds[i] = -1;
280 if (asprintf(&path, "%s/%s/indexing.idx",
281 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
282 err = got_error_from_errno("asprintf");
283 goto done;
285 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
286 free(path);
287 if (err)
288 goto done;
289 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
290 err = got_error_from_errno2("fchmod", tmpidxpath);
291 goto done;
294 nidxfd = dup(idxfd);
295 if (nidxfd == -1) {
296 err = got_error_from_errno("dup");
297 goto done;
300 for (i = 0; i < nitems(tmpfds); i++) {
301 tmpfds[i] = got_opentempfd();
302 if (tmpfds[i] == -1) {
303 err = got_error_from_errno("got_opentempfd");
304 goto done;
308 err = got_object_id_str(&id_str, pack_hash);
309 if (err)
310 goto done;
312 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
313 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
314 err = got_error_from_errno("asprintf");
315 goto done;
318 if (fstat(fileno(packfile), &sb) == -1) {
319 err = got_error_from_errno2("fstat", packfile_path);
320 goto done;
323 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
324 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
325 err = got_error_from_errno("asprintf");
326 goto done;
329 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
330 err = got_error_from_errno("socketpair");
331 goto done;
333 idxpid = fork();
334 if (idxpid == -1) {
335 err= got_error_from_errno("fork");
336 goto done;
337 } else if (idxpid == 0)
338 got_privsep_exec_child(imsg_idxfds,
339 GOT_PATH_PROG_INDEX_PACK, packfile_path);
340 if (close(imsg_idxfds[1]) == -1) {
341 err = got_error_from_errno("close");
342 goto done;
344 imsg_init(&idxibuf, imsg_idxfds[0]);
346 npackfd = dup(fileno(packfile));
347 if (npackfd == -1) {
348 err = got_error_from_errno("dup");
349 goto done;
351 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
352 npackfd);
353 if (err != NULL)
354 goto done;
355 npackfd = -1;
356 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
357 if (err != NULL)
358 goto done;
359 nidxfd = -1;
360 for (i = 0; i < nitems(tmpfds); i++) {
361 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
362 if (err != NULL)
363 goto done;
364 tmpfds[i] = -1;
366 done = 0;
367 while (!done) {
368 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
370 if (cancel_cb) {
371 err = cancel_cb(cancel_arg);
372 if (err)
373 goto done;
376 err = got_privsep_recv_index_progress(&done, &nobj_total,
377 &nobj_indexed, &nobj_loose, &nobj_resolved,
378 &idxibuf);
379 if (err != NULL)
380 goto done;
381 if (nobj_indexed != 0) {
382 err = progress_cb(progress_arg, sb.st_size,
383 nobj_total, nobj_indexed, nobj_loose,
384 nobj_resolved);
385 if (err)
386 break;
388 imsg_clear(&idxibuf);
390 if (close(imsg_idxfds[0]) == -1) {
391 err = got_error_from_errno("close");
392 goto done;
394 if (waitpid(idxpid, &idxstatus, 0) == -1) {
395 err = got_error_from_errno("waitpid");
396 goto done;
399 if (rename(tmpidxpath, idxpath) == -1) {
400 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
401 goto done;
403 free(tmpidxpath);
404 tmpidxpath = NULL;
406 done:
407 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
408 err = got_error_from_errno2("unlink", tmpidxpath);
409 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
410 err = got_error_from_errno("close");
411 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
412 err = got_error_from_errno("close");
413 for (i = 0; i < nitems(tmpfds); i++) {
414 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
415 err = got_error_from_errno("close");
417 free(tmpidxpath);
418 free(idxpath);
419 free(packfile_path);
420 return err;
423 const struct got_error *
424 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
425 struct got_repository *repo, const char *packfile_path)
427 const struct got_error *err = NULL;
428 const char *packdir_path = NULL;
429 char *packfile_name = NULL, *p, *dot;
430 struct got_object_id id;
431 int packfd = -1;
433 *packfile = NULL;
434 *pack_hash = NULL;
436 packdir_path = got_repo_get_path_objects_pack(repo);
437 if (packdir_path == NULL)
438 return got_error_from_errno("got_repo_get_path_objects_pack");
440 if (!got_path_is_child(packfile_path, packdir_path,
441 strlen(packdir_path))) {
442 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
443 goto done;
447 err = got_path_basename(&packfile_name, packfile_path);
448 if (err)
449 goto done;
450 p = packfile_name;
452 if (strncmp(p, "pack-", 5) != 0) {
453 err = got_error_fmt(GOT_ERR_BAD_PATH,
454 "'%s' is not a valid pack file name",
455 packfile_name);
456 goto done;
458 p += 5;
459 dot = strchr(p, '.');
460 if (dot == NULL) {
461 err = got_error_fmt(GOT_ERR_BAD_PATH,
462 "'%s' is not a valid pack file name",
463 packfile_name);
464 goto done;
466 if (strcmp(dot + 1, "pack") != 0) {
467 err = got_error_fmt(GOT_ERR_BAD_PATH,
468 "'%s' is not a valid pack file name",
469 packfile_name);
470 goto done;
472 *dot = '\0';
473 if (!got_parse_sha1_digest(id.sha1, p)) {
474 err = got_error_fmt(GOT_ERR_BAD_PATH,
475 "'%s' is not a valid pack file name",
476 packfile_name);
477 goto done;
480 *pack_hash = got_object_id_dup(&id);
481 if (*pack_hash == NULL) {
482 err = got_error_from_errno("got_object_id_dup");
483 goto done;
486 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW);
487 if (packfd == -1) {
488 err = got_error_from_errno2("open", packfile_path);
489 goto done;
492 *packfile = fdopen(packfd, "r");
493 if (*packfile == NULL) {
494 err = got_error_from_errno2("fdopen", packfile_path);
495 goto done;
497 packfd = -1;
498 done:
499 if (packfd != -1 && close(packfd) == -1 && err == NULL)
500 err = got_error_from_errno2("close", packfile_path);
501 free(packfile_name);
502 if (err) {
503 free(*pack_hash);
504 *pack_hash = NULL;
506 return err;
509 const struct got_error *
510 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
511 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
512 got_cancel_cb cancel_cb, void *cancel_arg)
514 const struct got_error *err = NULL;
515 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
516 struct got_packidx *packidx = NULL;
517 struct got_pack *pack = NULL;
518 uint32_t nobj, i;
520 err = got_object_id_str(&id_str, pack_hash);
521 if (err)
522 goto done;
524 if (asprintf(&packpath, "%s/pack-%s.pack",
525 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
526 err = got_error_from_errno("asprintf");
527 goto done;
529 if (asprintf(&idxpath, "%s/pack-%s.idx",
530 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
531 err = got_error_from_errno("asprintf");
532 goto done;
535 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
536 if (err)
537 goto done;
539 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
540 if (err)
541 goto done;
543 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
544 for (i = 0; i < nobj; i++) {
545 struct got_packidx_object_id *oid;
546 struct got_object_id id, base_id;
547 off_t offset, base_offset = 0;
548 uint8_t type;
549 uint64_t size;
550 size_t tslen, len;
552 if (cancel_cb) {
553 err = cancel_cb(cancel_arg);
554 if (err)
555 break;
557 oid = &packidx->hdr.sorted_ids[i];
558 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
560 offset = got_packidx_get_object_offset(packidx, i);
561 if (offset == -1) {
562 err = got_error(GOT_ERR_BAD_PACKIDX);
563 goto done;
566 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
567 pack, offset);
568 if (err)
569 goto done;
571 switch (type) {
572 case GOT_OBJ_TYPE_OFFSET_DELTA:
573 err = got_pack_parse_offset_delta(&base_offset, &len,
574 pack, offset, tslen);
575 if (err)
576 goto done;
577 break;
578 case GOT_OBJ_TYPE_REF_DELTA:
579 err = got_pack_parse_ref_delta(&base_id,
580 pack, offset, tslen);
581 if (err)
582 goto done;
583 break;
585 err = (*list_cb)(list_arg, &id, type, offset, size,
586 base_offset, &base_id);
587 if (err)
588 goto done;
591 done:
592 free(id_str);
593 free(idxpath);
594 free(packpath);
595 if (packidx)
596 got_packidx_close(packidx);
597 return err;
600 static const struct got_error *
601 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
602 got_cleanup_progress_cb progress_cb, void *progress_arg,
603 struct got_repository *repo)
605 const struct got_error *err = NULL;
606 char *path_objects = NULL, *path = NULL;
607 DIR *dir = NULL;
608 struct got_object *obj = NULL;
609 struct got_object_id id;
610 int i, fd = -1;
611 struct stat sb;
613 *ondisk_size = 0;
614 *loose_ids = got_object_idset_alloc();
615 if (*loose_ids == NULL)
616 return got_error_from_errno("got_object_idset_alloc");
618 path_objects = got_repo_get_path_objects(repo);
619 if (path_objects == NULL) {
620 err = got_error_from_errno("got_repo_get_path_objects");
621 goto done;
624 for (i = 0; i <= 0xff; i++) {
625 struct dirent *dent;
627 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
628 err = got_error_from_errno("asprintf");
629 break;
632 dir = opendir(path);
633 if (dir == NULL) {
634 if (errno == ENOENT) {
635 err = NULL;
636 continue;
638 err = got_error_from_errno2("opendir", path);
639 break;
642 while ((dent = readdir(dir)) != NULL) {
643 char *id_str;
645 if (strcmp(dent->d_name, ".") == 0 ||
646 strcmp(dent->d_name, "..") == 0)
647 continue;
649 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
650 err = got_error_from_errno("asprintf");
651 goto done;
654 memset(&id, 0, sizeof(id));
655 if (!got_parse_sha1_digest(id.sha1, id_str)) {
656 free(id_str);
657 continue;
659 free(id_str);
661 err = got_object_open_loose_fd(&fd, &id, repo);
662 if (err)
663 goto done;
664 if (fstat(fd, &sb) == -1) {
665 err = got_error_from_errno("fstat");
666 goto done;
668 err = got_object_read_header_privsep(&obj, &id, repo,
669 fd);
670 if (err)
671 goto done;
672 fd = -1; /* already closed */
674 switch (obj->type) {
675 case GOT_OBJ_TYPE_COMMIT:
676 case GOT_OBJ_TYPE_TREE:
677 case GOT_OBJ_TYPE_BLOB:
678 case GOT_OBJ_TYPE_TAG:
679 break;
680 default:
681 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
682 "%d", obj->type);
683 goto done;
685 got_object_close(obj);
686 obj = NULL;
687 (*ondisk_size) += sb.st_size;
688 err = got_object_idset_add(*loose_ids, &id, NULL);
689 if (err)
690 goto done;
691 if (progress_cb) {
692 err = progress_cb(progress_arg,
693 got_object_idset_num_elements(*loose_ids),
694 -1, -1);
695 if (err)
696 goto done;
700 if (closedir(dir) != 0) {
701 err = got_error_from_errno("closedir");
702 goto done;
704 dir = NULL;
706 free(path);
707 path = NULL;
709 done:
710 if (dir && closedir(dir) != 0 && err == NULL)
711 err = got_error_from_errno("closedir");
712 if (fd != -1 && close(fd) == -1 && err == NULL)
713 err = got_error_from_errno("close");
714 if (err) {
715 got_object_idset_free(*loose_ids);
716 *loose_ids = NULL;
718 if (obj)
719 got_object_close(obj);
720 free(path_objects);
721 free(path);
722 return err;
725 static const struct got_error *
726 preserve_loose_object(struct got_object_idset *loose_ids,
727 struct got_object_id *id, struct got_repository *repo, int *npacked)
729 const struct got_error *err = NULL;
730 struct got_object *obj;
732 if (!got_object_idset_contains(loose_ids, id))
733 return NULL;
735 /*
736 * Try to open this object from a pack file. This ensures that
737 * we do in fact have a valid packed copy of the object. Otherwise
738 * we should not delete the loose representation of this object.
739 */
740 err = got_object_open_packed(&obj, id, repo);
741 if (err == NULL) {
742 got_object_close(obj);
743 /*
744 * The object is referenced and packed.
745 * We can purge the redundantly stored loose object.
746 */
747 (*npacked)++;
748 return NULL;
749 } else if (err->code != GOT_ERR_NO_OBJ)
750 return err;
752 /*
753 * This object is referenced and not packed.
754 * Remove it from our purge set.
755 */
756 return got_object_idset_remove(NULL, loose_ids, id);
759 static const struct got_error *
760 load_tree_entries(struct got_object_id_queue *ids,
761 struct got_object_idset *loose_ids,
762 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
763 const char *dpath, struct got_repository *repo, int *npacked,
764 got_cancel_cb cancel_cb, void *cancel_arg)
766 const struct got_error *err;
767 struct got_tree_object *tree;
768 char *p = NULL;
769 int i;
771 err = got_object_open_as_tree(&tree, repo, tree_id);
772 if (err)
773 return err;
775 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
776 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
777 struct got_object_id *id = got_tree_entry_get_id(e);
778 mode_t mode = got_tree_entry_get_mode(e);
780 if (cancel_cb) {
781 err = (*cancel_cb)(cancel_arg);
782 if (err)
783 break;
786 if (got_object_tree_entry_is_symlink(e) ||
787 got_object_tree_entry_is_submodule(e) ||
788 got_object_idset_contains(traversed_ids, id))
789 continue;
791 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
792 got_tree_entry_get_name(e)) == -1) {
793 err = got_error_from_errno("asprintf");
794 break;
797 if (S_ISDIR(mode)) {
798 struct got_object_qid *qid;
799 err = got_object_qid_alloc(&qid, id);
800 if (err)
801 break;
802 STAILQ_INSERT_TAIL(ids, qid, entry);
803 } else if (S_ISREG(mode)) {
804 /* This blob is referenced. */
805 err = preserve_loose_object(loose_ids, id, repo,
806 npacked);
807 if (err)
808 break;
809 err = got_object_idset_add(traversed_ids, id, NULL);
810 if (err)
811 break;
814 free(p);
815 p = NULL;
818 got_object_tree_close(tree);
819 free(p);
820 return err;
823 static const struct got_error *
824 load_tree(struct got_object_idset *loose_ids,
825 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
826 const char *dpath, struct got_repository *repo, int *npacked,
827 got_cancel_cb cancel_cb, void *cancel_arg)
829 const struct got_error *err = NULL;
830 struct got_object_id_queue tree_ids;
831 struct got_object_qid *qid;
833 err = got_object_qid_alloc(&qid, tree_id);
834 if (err)
835 return err;
837 STAILQ_INIT(&tree_ids);
838 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
840 while (!STAILQ_EMPTY(&tree_ids)) {
841 if (cancel_cb) {
842 err = (*cancel_cb)(cancel_arg);
843 if (err)
844 break;
847 qid = STAILQ_FIRST(&tree_ids);
848 STAILQ_REMOVE_HEAD(&tree_ids, entry);
850 if (got_object_idset_contains(traversed_ids, qid->id)) {
851 got_object_qid_free(qid);
852 continue;
855 err = got_object_idset_add(traversed_ids, qid->id, NULL);
856 if (err) {
857 got_object_qid_free(qid);
858 break;
861 /* This tree is referenced. */
862 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
863 if (err)
864 break;
866 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
867 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
868 got_object_qid_free(qid);
869 if (err)
870 break;
873 got_object_id_queue_free(&tree_ids);
874 return err;
877 static const struct got_error *
878 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
879 int *npacked, struct got_object_idset *traversed_ids,
880 struct got_object_id *id, struct got_repository *repo,
881 got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
882 got_cancel_cb cancel_cb, void *cancel_arg)
884 const struct got_error *err;
885 struct got_commit_object *commit = NULL;
886 struct got_tag_object *tag = NULL;
887 struct got_object_id *tree_id = NULL;
888 struct got_object_id_queue ids;
889 struct got_object_qid *qid;
890 int obj_type;
892 err = got_object_qid_alloc(&qid, id);
893 if (err)
894 return err;
896 STAILQ_INIT(&ids);
897 STAILQ_INSERT_TAIL(&ids, qid, entry);
899 while (!STAILQ_EMPTY(&ids)) {
900 if (cancel_cb) {
901 err = (*cancel_cb)(cancel_arg);
902 if (err)
903 break;
906 qid = STAILQ_FIRST(&ids);
907 STAILQ_REMOVE_HEAD(&ids, entry);
909 if (got_object_idset_contains(traversed_ids, qid->id)) {
910 got_object_qid_free(qid);
911 qid = NULL;
912 continue;
915 err = got_object_idset_add(traversed_ids, qid->id, NULL);
916 if (err)
917 break;
919 /* This commit or tag is referenced. */
920 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
921 if (err)
922 break;
924 err = got_object_get_type(&obj_type, repo, qid->id);
925 if (err)
926 break;
927 switch (obj_type) {
928 case GOT_OBJ_TYPE_COMMIT:
929 err = got_object_open_as_commit(&commit, repo, qid->id);
930 if (err)
931 goto done;
932 break;
933 case GOT_OBJ_TYPE_TAG:
934 err = got_object_open_as_tag(&tag, repo, qid->id);
935 if (err)
936 goto done;
937 break;
938 default:
939 /* should not happen */
940 err = got_error(GOT_ERR_OBJ_TYPE);
941 goto done;
944 /* Find a tree object to scan. */
945 if (commit) {
946 tree_id = got_object_commit_get_tree_id(commit);
947 } else if (tag) {
948 obj_type = got_object_tag_get_object_type(tag);
949 switch (obj_type) {
950 case GOT_OBJ_TYPE_COMMIT:
951 err = got_object_open_as_commit(&commit, repo,
952 got_object_tag_get_object_id(tag));
953 if (err)
954 goto done;
955 tree_id = got_object_commit_get_tree_id(commit);
956 break;
957 case GOT_OBJ_TYPE_TREE:
958 tree_id = got_object_tag_get_object_id(tag);
959 break;
960 default:
961 /*
962 * Tag points at something other than a
963 * commit or tree. Leave this weird tag object
964 * and the object it points to on disk.
965 */
966 err = got_object_idset_remove(NULL, loose_ids,
967 qid->id);
968 if (err && err->code != GOT_ERR_NO_OBJ)
969 goto done;
970 err = got_object_idset_remove(NULL, loose_ids,
971 got_object_tag_get_object_id(tag));
972 if (err && err->code != GOT_ERR_NO_OBJ)
973 goto done;
974 err = NULL;
975 break;
979 if (tree_id) {
980 err = load_tree(loose_ids, traversed_ids, tree_id, "",
981 repo, npacked, cancel_cb, cancel_arg);
982 if (err)
983 break;
986 if (commit || tag)
987 (*ncommits)++; /* scanned tags are counted as commits */
989 if (progress_cb) {
990 err = progress_cb(progress_arg, nloose, *ncommits, -1);
991 if (err)
992 break;
995 if (commit) {
996 /* Find parent commits to scan. */
997 const struct got_object_id_queue *parent_ids;
998 parent_ids = got_object_commit_get_parent_ids(commit);
999 err = got_object_id_queue_copy(parent_ids, &ids);
1000 if (err)
1001 break;
1002 got_object_commit_close(commit);
1003 commit = NULL;
1005 if (tag) {
1006 got_object_tag_close(tag);
1007 tag = NULL;
1009 got_object_qid_free(qid);
1010 qid = NULL;
1012 done:
1013 if (qid)
1014 got_object_qid_free(qid);
1015 if (commit)
1016 got_object_commit_close(commit);
1017 if (tag)
1018 got_object_tag_close(tag);
1019 got_object_id_queue_free(&ids);
1020 return err;
1023 struct purge_loose_object_arg {
1024 struct got_repository *repo;
1025 got_cleanup_progress_cb progress_cb;
1026 void *progress_arg;
1027 int nloose;
1028 int ncommits;
1029 int npurged;
1030 off_t size_purged;
1031 int dry_run;
1032 time_t max_mtime;
1033 int ignore_mtime;
1036 static const struct got_error *
1037 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1039 struct purge_loose_object_arg *a = arg;
1040 const struct got_error *err, *unlock_err = NULL;
1041 char *path = NULL;
1042 int fd = -1;
1043 struct stat sb;
1044 struct got_lockfile *lf = NULL;
1046 err = got_object_get_path(&path, id, a->repo);
1047 if (err)
1048 return err;
1050 err = got_object_open_loose_fd(&fd, id, a->repo);
1051 if (err)
1052 goto done;
1054 if (fstat(fd, &sb) == -1) {
1055 err = got_error_from_errno("fstat");
1056 goto done;
1060 * Do not delete objects which are younger than our maximum
1061 * modification time threshold. This prevents a race where
1062 * new objects which are being added to the repository
1063 * concurrently would be deleted.
1065 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1066 if (!a->dry_run) {
1067 err = got_lockfile_lock(&lf, path, -1);
1068 if (err)
1069 goto done;
1070 if (unlink(path) == -1) {
1071 err = got_error_from_errno2("unlink", path);
1072 goto done;
1076 a->npurged++;
1077 a->size_purged += sb.st_size;
1078 if (a->progress_cb) {
1079 err = a->progress_cb(a->progress_arg, a->nloose,
1080 a->ncommits, a->npurged);
1083 done:
1084 if (fd != -1 && close(fd) == -1 && err == NULL)
1085 err = got_error_from_errno("close");
1086 free(path);
1087 if (lf)
1088 unlock_err = got_lockfile_unlock(lf, -1);
1089 return err ? err : unlock_err;
1092 const struct got_error *
1093 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1094 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1095 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1096 got_cancel_cb cancel_cb, void *cancel_arg)
1098 const struct got_error *err;
1099 struct got_object_idset *loose_ids;
1100 struct got_object_idset *traversed_ids;
1101 struct got_object_id **referenced_ids;
1102 int i, nreferenced, nloose, ncommits = 0;
1103 struct got_reflist_head refs;
1104 struct got_reflist_entry *re;
1105 struct purge_loose_object_arg arg;
1106 time_t max_mtime = 0;
1108 TAILQ_INIT(&refs);
1110 *size_before = 0;
1111 *size_after = 0;
1112 *npacked = 0;
1114 err = get_loose_object_ids(&loose_ids, size_before,
1115 progress_cb, progress_arg, repo);
1116 if (err)
1117 return err;
1118 nloose = got_object_idset_num_elements(loose_ids);
1119 if (nloose == 0) {
1120 got_object_idset_free(loose_ids);
1121 return NULL;
1124 traversed_ids = got_object_idset_alloc();
1125 if (traversed_ids == NULL) {
1126 err = got_error_from_errno("got_object_idset_alloc");
1127 goto done;
1130 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1131 if (err)
1132 goto done;
1133 if (!ignore_mtime) {
1134 TAILQ_FOREACH(re, &refs, entry) {
1135 time_t mtime = got_ref_get_mtime(re->ref);
1136 if (mtime > max_mtime)
1137 max_mtime = mtime;
1140 * For safety, keep objects created within 10 minutes
1141 * before the youngest reference was created.
1143 if (max_mtime >= 600)
1144 max_mtime -= 600;
1147 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1148 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1149 &refs, repo, cancel_cb, cancel_arg);
1150 if (err)
1151 goto done;
1153 for (i = 0; i < nreferenced; i++) {
1154 struct got_object_id *id = referenced_ids[i];
1155 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1156 traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1157 cancel_cb, cancel_arg);
1158 if (err)
1159 goto done;
1162 /* Produce a final progress report in case no objects can be purged. */
1163 if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1164 err = progress_cb(progress_arg, nloose, ncommits, 0);
1165 if (err)
1166 goto done;
1169 /* Any remaining loose objects are unreferenced and can be purged. */
1170 arg.repo = repo;
1171 arg.progress_arg = progress_arg;
1172 arg.progress_cb = progress_cb;
1173 arg.nloose = nloose;
1174 arg.npurged = 0;
1175 arg.size_purged = 0;
1176 arg.ncommits = ncommits;
1177 arg.dry_run = dry_run;
1178 arg.max_mtime = max_mtime;
1179 arg.ignore_mtime = ignore_mtime;
1180 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1181 if (err)
1182 goto done;
1183 *size_after = *size_before - arg.size_purged;
1184 done:
1185 got_object_idset_free(loose_ids);
1186 got_object_idset_free(traversed_ids);
1187 return err;
1190 static const struct got_error *
1191 remove_packidx(int dir_fd, const char *relpath)
1193 const struct got_error *err, *unlock_err;
1194 struct got_lockfile *lf;
1196 err = got_lockfile_lock(&lf, relpath, dir_fd);
1197 if (err)
1198 return err;
1199 if (unlinkat(dir_fd, relpath, 0) == -1)
1200 err = got_error_from_errno("unlinkat");
1201 unlock_err = got_lockfile_unlock(lf, dir_fd);
1202 return err ? err : unlock_err;
1205 const struct got_error *
1206 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1207 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1208 got_cancel_cb cancel_cb, void *cancel_arg)
1210 const struct got_error *err;
1211 DIR *packdir = NULL;
1212 struct dirent *dent;
1213 char *pack_relpath = NULL;
1214 int packdir_fd;
1215 struct stat sb;
1217 packdir_fd = openat(got_repo_get_fd(repo),
1218 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1219 if (packdir_fd == -1) {
1220 if (errno == ENOENT)
1221 return NULL;
1222 return got_error_from_errno_fmt("openat: %s/%s",
1223 got_repo_get_path_git_dir(repo),
1224 GOT_OBJECTS_PACK_DIR);
1227 packdir = fdopendir(packdir_fd);
1228 if (packdir == NULL) {
1229 err = got_error_from_errno("fdopendir");
1230 goto done;
1233 while ((dent = readdir(packdir)) != NULL) {
1234 if (cancel_cb) {
1235 err = cancel_cb(cancel_arg);
1236 if (err)
1237 goto done;
1240 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1241 continue;
1243 err = got_packidx_get_packfile_path(&pack_relpath,
1244 dent->d_name);
1245 if (err)
1246 goto done;
1248 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1249 free(pack_relpath);
1250 pack_relpath = NULL;
1251 continue;
1253 if (errno != ENOENT) {
1254 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1255 got_repo_get_path_git_dir(repo),
1256 GOT_OBJECTS_PACK_DIR,
1257 pack_relpath);
1258 goto done;
1261 if (!dry_run) {
1262 err = remove_packidx(packdir_fd, dent->d_name);
1263 if (err)
1264 goto done;
1266 if (progress_cb) {
1267 char *path;
1268 if (asprintf(&path, "%s/%s/%s",
1269 got_repo_get_path_git_dir(repo),
1270 GOT_OBJECTS_PACK_DIR,
1271 dent->d_name) == -1) {
1272 err = got_error_from_errno("asprintf");
1273 goto done;
1275 err = progress_cb(progress_arg, path);
1276 free(path);
1277 if (err)
1278 goto done;
1280 free(pack_relpath);
1281 pack_relpath = NULL;
1283 done:
1284 if (packdir && closedir(packdir) != 0 && err == NULL)
1285 err = got_error_from_errno("closedir");
1286 free(pack_relpath);
1287 return err;