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, 0, 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, &id, repo,
668 fd);
669 if (err)
670 goto done;
671 fd = -1; /* already closed */
673 switch (obj->type) {
674 case GOT_OBJ_TYPE_COMMIT:
675 case GOT_OBJ_TYPE_TREE:
676 case GOT_OBJ_TYPE_BLOB:
677 case GOT_OBJ_TYPE_TAG:
678 break;
679 default:
680 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
681 "%d", obj->type);
682 goto done;
684 got_object_close(obj);
685 obj = NULL;
686 (*ondisk_size) += sb.st_size;
687 err = got_object_idset_add(*loose_ids, &id, NULL);
688 if (err)
689 goto done;
690 if (progress_cb) {
691 err = progress_cb(progress_arg,
692 got_object_idset_num_elements(*loose_ids),
693 -1, -1);
694 if (err)
695 goto done;
699 if (closedir(dir) != 0) {
700 err = got_error_from_errno("closedir");
701 goto done;
703 dir = NULL;
705 free(path);
706 path = NULL;
708 done:
709 if (dir && closedir(dir) != 0 && err == NULL)
710 err = got_error_from_errno("closedir");
711 if (fd != -1 && close(fd) == -1 && err == NULL)
712 err = got_error_from_errno("close");
713 if (err) {
714 got_object_idset_free(*loose_ids);
715 *loose_ids = NULL;
717 if (obj)
718 got_object_close(obj);
719 free(path_objects);
720 free(path);
721 return err;
724 static const struct got_error *
725 preserve_loose_object(struct got_object_idset *loose_ids,
726 struct got_object_id *id, struct got_repository *repo, int *npacked)
728 const struct got_error *err = NULL;
729 struct got_object *obj;
731 if (!got_object_idset_contains(loose_ids, id))
732 return NULL;
734 /*
735 * Try to open this object from a pack file. This ensures that
736 * we do in fact have a valid packed copy of the object. Otherwise
737 * we should not delete the loose representation of this object.
738 */
739 err = got_object_open_packed(&obj, id, repo);
740 if (err == NULL) {
741 got_object_close(obj);
742 /*
743 * The object is referenced and packed.
744 * We can purge the redundantly stored loose object.
745 */
746 (*npacked)++;
747 return NULL;
748 } else if (err->code != GOT_ERR_NO_OBJ)
749 return err;
751 /*
752 * This object is referenced and not packed.
753 * Remove it from our purge set.
754 */
755 return got_object_idset_remove(NULL, loose_ids, id);
758 static const struct got_error *
759 load_tree_entries(struct got_object_id_queue *ids,
760 struct got_object_idset *loose_ids,
761 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
762 const char *dpath, struct got_repository *repo, int *npacked,
763 got_cancel_cb cancel_cb, void *cancel_arg)
765 const struct got_error *err;
766 struct got_tree_object *tree;
767 char *p = NULL;
768 int i;
770 err = got_object_open_as_tree(&tree, repo, tree_id);
771 if (err)
772 return err;
774 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
775 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
776 struct got_object_id *id = got_tree_entry_get_id(e);
777 mode_t mode = got_tree_entry_get_mode(e);
779 if (cancel_cb) {
780 err = (*cancel_cb)(cancel_arg);
781 if (err)
782 break;
785 if (got_object_tree_entry_is_symlink(e) ||
786 got_object_tree_entry_is_submodule(e) ||
787 got_object_idset_contains(traversed_ids, id))
788 continue;
790 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
791 got_tree_entry_get_name(e)) == -1) {
792 err = got_error_from_errno("asprintf");
793 break;
796 if (S_ISDIR(mode)) {
797 struct got_object_qid *qid;
798 err = got_object_qid_alloc(&qid, id);
799 if (err)
800 break;
801 STAILQ_INSERT_TAIL(ids, qid, entry);
802 } else if (S_ISREG(mode)) {
803 /* This blob is referenced. */
804 err = preserve_loose_object(loose_ids, id, repo,
805 npacked);
806 if (err)
807 break;
808 err = got_object_idset_add(traversed_ids, id, NULL);
809 if (err)
810 break;
813 free(p);
814 p = NULL;
817 got_object_tree_close(tree);
818 free(p);
819 return err;
822 static const struct got_error *
823 load_tree(struct got_object_idset *loose_ids,
824 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
825 const char *dpath, struct got_repository *repo, int *npacked,
826 got_cancel_cb cancel_cb, void *cancel_arg)
828 const struct got_error *err = NULL;
829 struct got_object_id_queue tree_ids;
830 struct got_object_qid *qid;
832 err = got_object_qid_alloc(&qid, tree_id);
833 if (err)
834 return err;
836 STAILQ_INIT(&tree_ids);
837 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
839 while (!STAILQ_EMPTY(&tree_ids)) {
840 if (cancel_cb) {
841 err = (*cancel_cb)(cancel_arg);
842 if (err)
843 break;
846 qid = STAILQ_FIRST(&tree_ids);
847 STAILQ_REMOVE_HEAD(&tree_ids, entry);
849 if (got_object_idset_contains(traversed_ids, qid->id)) {
850 got_object_qid_free(qid);
851 continue;
854 err = got_object_idset_add(traversed_ids, qid->id, NULL);
855 if (err) {
856 got_object_qid_free(qid);
857 break;
860 /* This tree is referenced. */
861 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
862 if (err)
863 break;
865 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
866 qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
867 got_object_qid_free(qid);
868 if (err)
869 break;
872 got_object_id_queue_free(&tree_ids);
873 return err;
876 static const struct got_error *
877 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
878 int *npacked, struct got_object_idset *traversed_ids,
879 struct got_object_id *id, struct got_repository *repo,
880 got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
881 got_cancel_cb cancel_cb, void *cancel_arg)
883 const struct got_error *err;
884 struct got_commit_object *commit = NULL;
885 struct got_tag_object *tag = NULL;
886 struct got_object_id *tree_id = NULL;
887 struct got_object_id_queue ids;
888 struct got_object_qid *qid;
889 int obj_type;
891 err = got_object_qid_alloc(&qid, id);
892 if (err)
893 return err;
895 STAILQ_INIT(&ids);
896 STAILQ_INSERT_TAIL(&ids, qid, entry);
898 while (!STAILQ_EMPTY(&ids)) {
899 if (cancel_cb) {
900 err = (*cancel_cb)(cancel_arg);
901 if (err)
902 break;
905 qid = STAILQ_FIRST(&ids);
906 STAILQ_REMOVE_HEAD(&ids, entry);
908 if (got_object_idset_contains(traversed_ids, qid->id)) {
909 got_object_qid_free(qid);
910 qid = NULL;
911 continue;
914 err = got_object_idset_add(traversed_ids, qid->id, NULL);
915 if (err)
916 break;
918 /* This commit or tag is referenced. */
919 err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
920 if (err)
921 break;
923 err = got_object_get_type(&obj_type, repo, qid->id);
924 if (err)
925 break;
926 switch (obj_type) {
927 case GOT_OBJ_TYPE_COMMIT:
928 err = got_object_open_as_commit(&commit, repo, qid->id);
929 if (err)
930 goto done;
931 break;
932 case GOT_OBJ_TYPE_TAG:
933 err = got_object_open_as_tag(&tag, repo, qid->id);
934 if (err)
935 goto done;
936 break;
937 default:
938 /* should not happen */
939 err = got_error(GOT_ERR_OBJ_TYPE);
940 goto done;
943 /* Find a tree object to scan. */
944 if (commit) {
945 tree_id = got_object_commit_get_tree_id(commit);
946 } else if (tag) {
947 obj_type = got_object_tag_get_object_type(tag);
948 switch (obj_type) {
949 case GOT_OBJ_TYPE_COMMIT:
950 err = got_object_open_as_commit(&commit, repo,
951 got_object_tag_get_object_id(tag));
952 if (err)
953 goto done;
954 tree_id = got_object_commit_get_tree_id(commit);
955 break;
956 case GOT_OBJ_TYPE_TREE:
957 tree_id = got_object_tag_get_object_id(tag);
958 break;
959 default:
960 /*
961 * Tag points at something other than a
962 * commit or tree. Leave this weird tag object
963 * and the object it points to on disk.
964 */
965 err = got_object_idset_remove(NULL, loose_ids,
966 qid->id);
967 if (err && err->code != GOT_ERR_NO_OBJ)
968 goto done;
969 err = got_object_idset_remove(NULL, loose_ids,
970 got_object_tag_get_object_id(tag));
971 if (err && err->code != GOT_ERR_NO_OBJ)
972 goto done;
973 err = NULL;
974 break;
978 if (tree_id) {
979 err = load_tree(loose_ids, traversed_ids, tree_id, "",
980 repo, npacked, cancel_cb, cancel_arg);
981 if (err)
982 break;
985 if (commit || tag)
986 (*ncommits)++; /* scanned tags are counted as commits */
988 if (progress_cb) {
989 err = progress_cb(progress_arg, nloose, *ncommits, -1);
990 if (err)
991 break;
994 if (commit) {
995 /* Find parent commits to scan. */
996 const struct got_object_id_queue *parent_ids;
997 parent_ids = got_object_commit_get_parent_ids(commit);
998 err = got_object_id_queue_copy(parent_ids, &ids);
999 if (err)
1000 break;
1001 got_object_commit_close(commit);
1002 commit = NULL;
1004 if (tag) {
1005 got_object_tag_close(tag);
1006 tag = NULL;
1008 got_object_qid_free(qid);
1009 qid = NULL;
1011 done:
1012 if (qid)
1013 got_object_qid_free(qid);
1014 if (commit)
1015 got_object_commit_close(commit);
1016 if (tag)
1017 got_object_tag_close(tag);
1018 got_object_id_queue_free(&ids);
1019 return err;
1022 struct purge_loose_object_arg {
1023 struct got_repository *repo;
1024 got_cleanup_progress_cb progress_cb;
1025 void *progress_arg;
1026 int nloose;
1027 int ncommits;
1028 int npurged;
1029 off_t size_purged;
1030 int dry_run;
1031 time_t max_mtime;
1032 int ignore_mtime;
1035 static const struct got_error *
1036 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1038 struct purge_loose_object_arg *a = arg;
1039 const struct got_error *err, *unlock_err = NULL;
1040 char *path = NULL;
1041 int fd = -1;
1042 struct stat sb;
1043 struct got_lockfile *lf = NULL;
1045 err = got_object_get_path(&path, id, a->repo);
1046 if (err)
1047 return err;
1049 err = got_object_open_loose_fd(&fd, id, a->repo);
1050 if (err)
1051 goto done;
1053 if (fstat(fd, &sb) == -1) {
1054 err = got_error_from_errno("fstat");
1055 goto done;
1059 * Do not delete objects which are younger than our maximum
1060 * modification time threshold. This prevents a race where
1061 * new objects which are being added to the repository
1062 * concurrently would be deleted.
1064 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1065 if (!a->dry_run) {
1066 err = got_lockfile_lock(&lf, path, -1);
1067 if (err)
1068 goto done;
1069 if (unlink(path) == -1) {
1070 err = got_error_from_errno2("unlink", path);
1071 goto done;
1075 a->npurged++;
1076 a->size_purged += sb.st_size;
1077 if (a->progress_cb) {
1078 err = a->progress_cb(a->progress_arg, a->nloose,
1079 a->ncommits, a->npurged);
1082 done:
1083 if (fd != -1 && close(fd) == -1 && err == NULL)
1084 err = got_error_from_errno("close");
1085 free(path);
1086 if (lf)
1087 unlock_err = got_lockfile_unlock(lf, -1);
1088 return err ? err : unlock_err;
1091 const struct got_error *
1092 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1093 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1094 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1095 got_cancel_cb cancel_cb, void *cancel_arg)
1097 const struct got_error *err;
1098 struct got_object_idset *loose_ids;
1099 struct got_object_idset *traversed_ids;
1100 struct got_object_id **referenced_ids;
1101 int i, nreferenced, nloose, ncommits = 0;
1102 struct got_reflist_head refs;
1103 struct got_reflist_entry *re;
1104 struct purge_loose_object_arg arg;
1105 time_t max_mtime = 0;
1107 TAILQ_INIT(&refs);
1109 *size_before = 0;
1110 *size_after = 0;
1111 *npacked = 0;
1113 err = get_loose_object_ids(&loose_ids, size_before,
1114 progress_cb, progress_arg, repo);
1115 if (err)
1116 return err;
1117 nloose = got_object_idset_num_elements(loose_ids);
1118 if (nloose == 0) {
1119 got_object_idset_free(loose_ids);
1120 return NULL;
1123 traversed_ids = got_object_idset_alloc();
1124 if (traversed_ids == NULL) {
1125 err = got_error_from_errno("got_object_idset_alloc");
1126 goto done;
1129 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1130 if (err)
1131 goto done;
1132 if (!ignore_mtime) {
1133 TAILQ_FOREACH(re, &refs, entry) {
1134 time_t mtime = got_ref_get_mtime(re->ref);
1135 if (mtime > max_mtime)
1136 max_mtime = mtime;
1139 * For safety, keep objects created within 10 minutes
1140 * before the youngest reference was created.
1142 if (max_mtime >= 600)
1143 max_mtime -= 600;
1146 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1147 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1148 &refs, repo, cancel_cb, cancel_arg);
1149 if (err)
1150 goto done;
1152 for (i = 0; i < nreferenced; i++) {
1153 struct got_object_id *id = referenced_ids[i];
1154 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1155 traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1156 cancel_cb, cancel_arg);
1157 if (err)
1158 goto done;
1161 /* Produce a final progress report in case no objects can be purged. */
1162 if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1163 err = progress_cb(progress_arg, nloose, ncommits, 0);
1164 if (err)
1165 goto done;
1168 /* Any remaining loose objects are unreferenced and can be purged. */
1169 arg.repo = repo;
1170 arg.progress_arg = progress_arg;
1171 arg.progress_cb = progress_cb;
1172 arg.nloose = nloose;
1173 arg.npurged = 0;
1174 arg.size_purged = 0;
1175 arg.ncommits = ncommits;
1176 arg.dry_run = dry_run;
1177 arg.max_mtime = max_mtime;
1178 arg.ignore_mtime = ignore_mtime;
1179 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1180 if (err)
1181 goto done;
1182 *size_after = *size_before - arg.size_purged;
1183 done:
1184 got_object_idset_free(loose_ids);
1185 got_object_idset_free(traversed_ids);
1186 return err;
1189 static const struct got_error *
1190 remove_packidx(int dir_fd, const char *relpath)
1192 const struct got_error *err, *unlock_err;
1193 struct got_lockfile *lf;
1195 err = got_lockfile_lock(&lf, relpath, dir_fd);
1196 if (err)
1197 return err;
1198 if (unlinkat(dir_fd, relpath, 0) == -1)
1199 err = got_error_from_errno("unlinkat");
1200 unlock_err = got_lockfile_unlock(lf, dir_fd);
1201 return err ? err : unlock_err;
1204 const struct got_error *
1205 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1206 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1207 got_cancel_cb cancel_cb, void *cancel_arg)
1209 const struct got_error *err;
1210 DIR *packdir = NULL;
1211 struct dirent *dent;
1212 char *pack_relpath = NULL;
1213 int packdir_fd;
1214 struct stat sb;
1216 packdir_fd = openat(got_repo_get_fd(repo),
1217 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1218 if (packdir_fd == -1) {
1219 if (errno == ENOENT)
1220 return NULL;
1221 return got_error_from_errno_fmt("openat: %s/%s",
1222 got_repo_get_path_git_dir(repo),
1223 GOT_OBJECTS_PACK_DIR);
1226 packdir = fdopendir(packdir_fd);
1227 if (packdir == NULL) {
1228 err = got_error_from_errno("fdopendir");
1229 goto done;
1232 while ((dent = readdir(packdir)) != NULL) {
1233 if (cancel_cb) {
1234 err = cancel_cb(cancel_arg);
1235 if (err)
1236 goto done;
1239 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1240 continue;
1242 err = got_packidx_get_packfile_path(&pack_relpath,
1243 dent->d_name);
1244 if (err)
1245 goto done;
1247 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1248 free(pack_relpath);
1249 pack_relpath = NULL;
1250 continue;
1252 if (errno != ENOENT) {
1253 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1254 got_repo_get_path_git_dir(repo),
1255 GOT_OBJECTS_PACK_DIR,
1256 pack_relpath);
1257 goto done;
1260 if (!dry_run) {
1261 err = remove_packidx(packdir_fd, dent->d_name);
1262 if (err)
1263 goto done;
1265 if (progress_cb) {
1266 char *path;
1267 if (asprintf(&path, "%s/%s/%s",
1268 got_repo_get_path_git_dir(repo),
1269 GOT_OBJECTS_PACK_DIR,
1270 dent->d_name) == -1) {
1271 err = got_error_from_errno("asprintf");
1272 goto done;
1274 err = progress_cb(progress_arg, path);
1275 free(path);
1276 if (err)
1277 goto done;
1279 free(pack_relpath);
1280 pack_relpath = NULL;
1282 done:
1283 if (packdir && closedir(packdir) != 0 && err == NULL)
1284 err = got_error_from_errno("closedir");
1285 free(pack_relpath);
1286 return err;