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"
57 #include "got_lib_ratelimit.h"
59 #ifndef nitems
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 #endif
63 static const struct got_error *
64 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
65 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
66 struct got_repository *repo,
67 got_cancel_cb cancel_cb, void *cancel_arg)
68 {
69 const struct got_error *err = NULL;
70 const size_t alloc_chunksz = 256;
71 size_t nalloc;
72 struct got_reflist_entry *re;
73 int i;
75 *ids = NULL;
76 *nobjects = 0;
78 err = got_reflist_sort(refs,
79 got_ref_cmp_by_commit_timestamp_descending, repo);
80 if (err)
81 return err;
83 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
84 if (*ids == NULL)
85 return got_error_from_errno("reallocarray");
86 nalloc = alloc_chunksz;
88 TAILQ_FOREACH(re, refs, entry) {
89 struct got_object_id *id;
91 if (cancel_cb) {
92 err = cancel_cb(cancel_arg);
93 if (err)
94 goto done;
95 }
97 err = got_ref_resolve(&id, repo, re->ref);
98 if (err)
99 goto done;
101 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
102 int obj_type;
103 err = got_object_get_type(&obj_type, repo, id);
104 if (err)
105 goto done;
106 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
107 free(id);
108 id = NULL;
109 continue;
113 if (nalloc <= *nobjects) {
114 struct got_object_id **new;
115 new = recallocarray(*ids, nalloc,
116 nalloc + alloc_chunksz,
117 sizeof(struct got_object_id *));
118 if (new == NULL) {
119 err = got_error_from_errno(
120 "recallocarray");
121 goto done;
123 *ids = new;
124 nalloc += alloc_chunksz;
126 (*ids)[*nobjects] = id;
127 if ((*ids)[*nobjects] == NULL) {
128 err = got_error_from_errno("got_object_id_dup");
129 goto done;
131 (*nobjects)++;
133 done:
134 if (err) {
135 for (i = 0; i < *nobjects; i++)
136 free((*ids)[i]);
137 free(*ids);
138 *ids = NULL;
139 *nobjects = 0;
141 return err;
144 const struct got_error *
145 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
146 struct got_reflist_head *include_refs,
147 struct got_reflist_head *exclude_refs, struct got_repository *repo,
148 int loose_obj_only,
149 got_pack_progress_cb progress_cb, void *progress_arg,
150 got_cancel_cb cancel_cb, void *cancel_arg)
152 const struct got_error *err = NULL;
153 struct got_object_id **ours = NULL, **theirs = NULL;
154 int nours = 0, ntheirs = 0, packfd = -1, i;
155 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
156 char *sha1_str = NULL;
158 *packfile = NULL;
159 *pack_hash = NULL;
161 if (asprintf(&path, "%s/%s/packing.pack",
162 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
163 err = got_error_from_errno("asprintf");
164 goto done;
166 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
167 if (err)
168 goto done;
170 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
171 err = got_error_from_errno2("fchmod", tmpfile_path);
172 goto done;
175 *packfile = fdopen(packfd, "w");
176 if (*packfile == NULL) {
177 err = got_error_from_errno2("fdopen", tmpfile_path);
178 goto done;
180 packfd = -1;
182 err = get_reflist_object_ids(&ours, &nours,
183 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
184 include_refs, repo, cancel_cb, cancel_arg);
185 if (err)
186 goto done;
188 if (nours == 0) {
189 err = got_error(GOT_ERR_CANNOT_PACK);
190 goto done;
193 if (!TAILQ_EMPTY(exclude_refs)) {
194 err = get_reflist_object_ids(&theirs, &ntheirs,
195 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
196 exclude_refs, repo,
197 cancel_cb, cancel_arg);
198 if (err)
199 goto done;
202 *pack_hash = calloc(1, sizeof(**pack_hash));
203 if (*pack_hash == NULL) {
204 err = got_error_from_errno("calloc");
205 goto done;
208 err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
209 ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
210 cancel_cb, cancel_arg);
211 if (err)
212 goto done;
214 err = got_object_id_str(&sha1_str, *pack_hash);
215 if (err)
216 goto done;
217 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
218 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
219 sha1_str) == -1) {
220 err = got_error_from_errno("asprintf");
221 goto done;
224 if (fflush(*packfile) == -1) {
225 err = got_error_from_errno("fflush");
226 goto done;
228 if (fseek(*packfile, 0L, SEEK_SET) == -1) {
229 err = got_error_from_errno("fseek");
230 goto done;
232 if (rename(tmpfile_path, packfile_path) == -1) {
233 err = got_error_from_errno3("rename", tmpfile_path,
234 packfile_path);
235 goto done;
237 free(tmpfile_path);
238 tmpfile_path = NULL;
239 done:
240 for (i = 0; i < nours; i++)
241 free(ours[i]);
242 free(ours);
243 for (i = 0; i < ntheirs; i++)
244 free(theirs[i]);
245 free(theirs);
246 if (packfd != -1 && close(packfd) == -1 && err == NULL)
247 err = got_error_from_errno2("close", packfile_path);
248 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
249 err = got_error_from_errno2("unlink", tmpfile_path);
250 free(tmpfile_path);
251 free(packfile_path);
252 free(sha1_str);
253 free(path);
254 if (err) {
255 free(*pack_hash);
256 *pack_hash = NULL;
257 if (*packfile)
258 fclose(*packfile);
259 *packfile = NULL;
261 return err;
264 const struct got_error *
265 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
266 struct got_repository *repo,
267 got_pack_index_progress_cb progress_cb, void *progress_arg,
268 got_cancel_cb cancel_cb, void *cancel_arg)
270 size_t i;
271 char *path;
272 int imsg_idxfds[2];
273 int npackfd = -1, idxfd = -1, nidxfd = -1;
274 int tmpfds[3];
275 int idxstatus, done = 0;
276 const struct got_error *err;
277 struct imsgbuf idxibuf;
278 pid_t idxpid;
279 char *tmpidxpath = NULL;
280 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
281 const char *repo_path = got_repo_get_path_git_dir(repo);
282 struct stat sb;
284 for (i = 0; i < nitems(tmpfds); i++)
285 tmpfds[i] = -1;
287 if (asprintf(&path, "%s/%s/indexing.idx",
288 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
289 err = got_error_from_errno("asprintf");
290 goto done;
292 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
293 free(path);
294 if (err)
295 goto done;
296 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
297 err = got_error_from_errno2("fchmod", tmpidxpath);
298 goto done;
301 nidxfd = dup(idxfd);
302 if (nidxfd == -1) {
303 err = got_error_from_errno("dup");
304 goto done;
307 for (i = 0; i < nitems(tmpfds); i++) {
308 tmpfds[i] = got_opentempfd();
309 if (tmpfds[i] == -1) {
310 err = got_error_from_errno("got_opentempfd");
311 goto done;
315 err = got_object_id_str(&id_str, pack_hash);
316 if (err)
317 goto done;
319 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
320 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
321 err = got_error_from_errno("asprintf");
322 goto done;
325 if (fstat(fileno(packfile), &sb) == -1) {
326 err = got_error_from_errno2("fstat", packfile_path);
327 goto done;
330 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
331 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
332 err = got_error_from_errno("asprintf");
333 goto done;
336 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
337 err = got_error_from_errno("socketpair");
338 goto done;
340 idxpid = fork();
341 if (idxpid == -1) {
342 err= got_error_from_errno("fork");
343 goto done;
344 } else if (idxpid == 0)
345 got_privsep_exec_child(imsg_idxfds,
346 GOT_PATH_PROG_INDEX_PACK, packfile_path);
347 if (close(imsg_idxfds[1]) == -1) {
348 err = got_error_from_errno("close");
349 goto done;
351 imsg_init(&idxibuf, imsg_idxfds[0]);
353 npackfd = dup(fileno(packfile));
354 if (npackfd == -1) {
355 err = got_error_from_errno("dup");
356 goto done;
358 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
359 npackfd);
360 if (err != NULL)
361 goto done;
362 npackfd = -1;
363 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
364 if (err != NULL)
365 goto done;
366 nidxfd = -1;
367 for (i = 0; i < nitems(tmpfds); i++) {
368 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
369 if (err != NULL)
370 goto done;
371 tmpfds[i] = -1;
373 done = 0;
374 while (!done) {
375 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
377 if (cancel_cb) {
378 err = cancel_cb(cancel_arg);
379 if (err)
380 goto done;
383 err = got_privsep_recv_index_progress(&done, &nobj_total,
384 &nobj_indexed, &nobj_loose, &nobj_resolved,
385 &idxibuf);
386 if (err != NULL)
387 goto done;
388 if (nobj_indexed != 0) {
389 err = progress_cb(progress_arg, sb.st_size,
390 nobj_total, nobj_indexed, nobj_loose,
391 nobj_resolved);
392 if (err)
393 break;
396 if (close(imsg_idxfds[0]) == -1) {
397 err = got_error_from_errno("close");
398 goto done;
400 if (waitpid(idxpid, &idxstatus, 0) == -1) {
401 err = got_error_from_errno("waitpid");
402 goto done;
405 if (rename(tmpidxpath, idxpath) == -1) {
406 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
407 goto done;
409 free(tmpidxpath);
410 tmpidxpath = NULL;
412 done:
413 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
414 err = got_error_from_errno2("unlink", tmpidxpath);
415 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
416 err = got_error_from_errno("close");
417 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
418 err = got_error_from_errno("close");
419 for (i = 0; i < nitems(tmpfds); i++) {
420 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
421 err = got_error_from_errno("close");
423 free(tmpidxpath);
424 free(idxpath);
425 free(packfile_path);
426 return err;
429 const struct got_error *
430 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
431 struct got_repository *repo, const char *packfile_path)
433 const struct got_error *err = NULL;
434 const char *packdir_path = NULL;
435 char *packfile_name = NULL, *p, *dot;
436 struct got_object_id id;
437 int packfd = -1;
439 *packfile = NULL;
440 *pack_hash = NULL;
442 packdir_path = got_repo_get_path_objects_pack(repo);
443 if (packdir_path == NULL)
444 return got_error_from_errno("got_repo_get_path_objects_pack");
446 if (!got_path_is_child(packfile_path, packdir_path,
447 strlen(packdir_path))) {
448 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
449 goto done;
453 err = got_path_basename(&packfile_name, packfile_path);
454 if (err)
455 goto done;
456 p = packfile_name;
458 if (strncmp(p, "pack-", 5) != 0) {
459 err = got_error_fmt(GOT_ERR_BAD_PATH,
460 "'%s' is not a valid pack file name",
461 packfile_name);
462 goto done;
464 p += 5;
465 dot = strchr(p, '.');
466 if (dot == NULL) {
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 if (strcmp(dot + 1, "pack") != 0) {
473 err = got_error_fmt(GOT_ERR_BAD_PATH,
474 "'%s' is not a valid pack file name",
475 packfile_name);
476 goto done;
478 *dot = '\0';
479 if (!got_parse_sha1_digest(id.sha1, p)) {
480 err = got_error_fmt(GOT_ERR_BAD_PATH,
481 "'%s' is not a valid pack file name",
482 packfile_name);
483 goto done;
486 *pack_hash = got_object_id_dup(&id);
487 if (*pack_hash == NULL) {
488 err = got_error_from_errno("got_object_id_dup");
489 goto done;
492 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
493 if (packfd == -1) {
494 err = got_error_from_errno2("open", packfile_path);
495 goto done;
498 *packfile = fdopen(packfd, "r");
499 if (*packfile == NULL) {
500 err = got_error_from_errno2("fdopen", packfile_path);
501 goto done;
503 packfd = -1;
504 done:
505 if (packfd != -1 && close(packfd) == -1 && err == NULL)
506 err = got_error_from_errno2("close", packfile_path);
507 free(packfile_name);
508 if (err) {
509 free(*pack_hash);
510 *pack_hash = NULL;
512 return err;
515 const struct got_error *
516 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
517 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
518 got_cancel_cb cancel_cb, void *cancel_arg)
520 const struct got_error *err = NULL;
521 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
522 struct got_packidx *packidx = NULL;
523 struct got_pack *pack = NULL;
524 uint32_t nobj, i;
526 err = got_object_id_str(&id_str, pack_hash);
527 if (err)
528 goto done;
530 if (asprintf(&packpath, "%s/pack-%s.pack",
531 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
532 err = got_error_from_errno("asprintf");
533 goto done;
535 if (asprintf(&idxpath, "%s/pack-%s.idx",
536 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
537 err = got_error_from_errno("asprintf");
538 goto done;
541 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
542 if (err)
543 goto done;
545 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
546 if (err)
547 goto done;
549 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
550 for (i = 0; i < nobj; i++) {
551 struct got_packidx_object_id *oid;
552 struct got_object_id id, base_id;
553 off_t offset, base_offset = 0;
554 uint8_t type;
555 uint64_t size;
556 size_t tslen, len;
558 if (cancel_cb) {
559 err = cancel_cb(cancel_arg);
560 if (err)
561 break;
563 oid = &packidx->hdr.sorted_ids[i];
564 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
566 offset = got_packidx_get_object_offset(packidx, i);
567 if (offset == -1) {
568 err = got_error(GOT_ERR_BAD_PACKIDX);
569 goto done;
572 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
573 pack, offset);
574 if (err)
575 goto done;
577 switch (type) {
578 case GOT_OBJ_TYPE_OFFSET_DELTA:
579 err = got_pack_parse_offset_delta(&base_offset, &len,
580 pack, offset, tslen);
581 if (err)
582 goto done;
583 break;
584 case GOT_OBJ_TYPE_REF_DELTA:
585 err = got_pack_parse_ref_delta(&base_id,
586 pack, offset, tslen);
587 if (err)
588 goto done;
589 break;
591 err = (*list_cb)(list_arg, &id, type, offset, size,
592 base_offset, &base_id);
593 if (err)
594 goto done;
597 done:
598 free(id_str);
599 free(idxpath);
600 free(packpath);
601 if (packidx)
602 got_packidx_close(packidx);
603 return err;
606 static const struct got_error *
607 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
608 void *progress_arg, struct got_ratelimit *rl,
609 int nloose, int ncommits, int npurged)
611 const struct got_error *err;
612 int elapsed;
614 if (progress_cb == NULL)
615 return NULL;
617 err = got_ratelimit_check(&elapsed, rl);
618 if (err || !elapsed)
619 return err;
621 return progress_cb(progress_arg, nloose, ncommits, npurged);
624 static const struct got_error *
625 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
626 got_cleanup_progress_cb progress_cb, void *progress_arg,
627 struct got_ratelimit *rl, struct got_repository *repo)
629 const struct got_error *err = NULL;
630 char *path_objects = NULL, *path = NULL;
631 DIR *dir = NULL;
632 struct got_object *obj = NULL;
633 struct got_object_id id;
634 int i, fd = -1;
635 struct stat sb;
637 *ondisk_size = 0;
638 *loose_ids = got_object_idset_alloc();
639 if (*loose_ids == NULL)
640 return got_error_from_errno("got_object_idset_alloc");
642 path_objects = got_repo_get_path_objects(repo);
643 if (path_objects == NULL) {
644 err = got_error_from_errno("got_repo_get_path_objects");
645 goto done;
648 for (i = 0; i <= 0xff; i++) {
649 struct dirent *dent;
651 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
652 err = got_error_from_errno("asprintf");
653 break;
656 dir = opendir(path);
657 if (dir == NULL) {
658 if (errno == ENOENT) {
659 err = NULL;
660 continue;
662 err = got_error_from_errno2("opendir", path);
663 break;
666 while ((dent = readdir(dir)) != NULL) {
667 char *id_str;
669 if (strcmp(dent->d_name, ".") == 0 ||
670 strcmp(dent->d_name, "..") == 0)
671 continue;
673 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
674 err = got_error_from_errno("asprintf");
675 goto done;
678 memset(&id, 0, sizeof(id));
679 if (!got_parse_sha1_digest(id.sha1, id_str)) {
680 free(id_str);
681 continue;
683 free(id_str);
685 err = got_object_open_loose_fd(&fd, &id, repo);
686 if (err)
687 goto done;
688 if (fstat(fd, &sb) == -1) {
689 err = got_error_from_errno("fstat");
690 goto done;
692 err = got_object_read_header_privsep(&obj, &id, repo,
693 fd);
694 if (err)
695 goto done;
696 fd = -1; /* already closed */
698 switch (obj->type) {
699 case GOT_OBJ_TYPE_COMMIT:
700 case GOT_OBJ_TYPE_TREE:
701 case GOT_OBJ_TYPE_BLOB:
702 case GOT_OBJ_TYPE_TAG:
703 break;
704 default:
705 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
706 "%d", obj->type);
707 goto done;
709 got_object_close(obj);
710 obj = NULL;
711 (*ondisk_size) += sb.st_size;
712 err = got_object_idset_add(*loose_ids, &id, NULL);
713 if (err)
714 goto done;
715 err = report_cleanup_progress(progress_cb,
716 progress_arg, rl,
717 got_object_idset_num_elements(*loose_ids), -1, -1);
718 if (err)
719 goto done;
722 if (closedir(dir) != 0) {
723 err = got_error_from_errno("closedir");
724 goto done;
726 dir = NULL;
728 free(path);
729 path = NULL;
731 done:
732 if (dir && closedir(dir) != 0 && err == NULL)
733 err = got_error_from_errno("closedir");
734 if (fd != -1 && close(fd) == -1 && err == NULL)
735 err = got_error_from_errno("close");
736 if (err) {
737 got_object_idset_free(*loose_ids);
738 *loose_ids = NULL;
740 if (obj)
741 got_object_close(obj);
742 free(path_objects);
743 free(path);
744 return err;
747 static const struct got_error *
748 preserve_loose_object(struct got_object_idset *loose_ids,
749 struct got_object_id *id, struct got_repository *repo, int *npacked)
751 const struct got_error *err = NULL;
752 struct got_object *obj;
754 if (!got_object_idset_contains(loose_ids, id))
755 return NULL;
757 /*
758 * Try to open this object from a pack file. This ensures that
759 * we do in fact have a valid packed copy of the object. Otherwise
760 * we should not delete the loose representation of this object.
761 */
762 err = got_object_open_packed(&obj, id, repo);
763 if (err == NULL) {
764 got_object_close(obj);
765 /*
766 * The object is referenced and packed.
767 * We can purge the redundantly stored loose object.
768 */
769 (*npacked)++;
770 return NULL;
771 } else if (err->code != GOT_ERR_NO_OBJ)
772 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;
835 free(p);
836 p = NULL;
839 got_object_tree_close(tree);
840 free(p);
841 return err;
844 static const struct got_error *
845 load_tree(struct got_object_idset *loose_ids,
846 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
847 const char *dpath, struct got_repository *repo, int *npacked,
848 got_cancel_cb cancel_cb, void *cancel_arg)
850 const struct got_error *err = NULL;
851 struct got_object_id_queue tree_ids;
852 struct got_object_qid *qid;
854 err = got_object_qid_alloc(&qid, tree_id);
855 if (err)
856 return err;
858 STAILQ_INIT(&tree_ids);
859 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
861 while (!STAILQ_EMPTY(&tree_ids)) {
862 if (cancel_cb) {
863 err = (*cancel_cb)(cancel_arg);
864 if (err)
865 break;
868 qid = STAILQ_FIRST(&tree_ids);
869 STAILQ_REMOVE_HEAD(&tree_ids, entry);
871 if (got_object_idset_contains(traversed_ids, &qid->id)) {
872 got_object_qid_free(qid);
873 continue;
876 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
877 if (err) {
878 got_object_qid_free(qid);
879 break;
882 /* This tree is referenced. */
883 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
884 if (err)
885 break;
887 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
888 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
889 got_object_qid_free(qid);
890 if (err)
891 break;
894 got_object_id_queue_free(&tree_ids);
895 return err;
898 static const struct got_error *
899 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
900 int *npacked, struct got_object_idset *traversed_ids,
901 struct got_object_id *id, struct got_repository *repo,
902 got_cleanup_progress_cb progress_cb, void *progress_arg,
903 struct got_ratelimit *rl, 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,
952 &qid->id);
953 if (err)
954 goto done;
955 break;
956 case GOT_OBJ_TYPE_TAG:
957 err = got_object_open_as_tag(&tag, repo, &qid->id);
958 if (err)
959 goto done;
960 break;
961 default:
962 /* should not happen */
963 err = got_error(GOT_ERR_OBJ_TYPE);
964 goto done;
967 /* Find a tree object to scan. */
968 if (commit) {
969 tree_id = got_object_commit_get_tree_id(commit);
970 } else if (tag) {
971 obj_type = got_object_tag_get_object_type(tag);
972 switch (obj_type) {
973 case GOT_OBJ_TYPE_COMMIT:
974 err = got_object_open_as_commit(&commit, repo,
975 got_object_tag_get_object_id(tag));
976 if (err)
977 goto done;
978 tree_id = got_object_commit_get_tree_id(commit);
979 break;
980 case GOT_OBJ_TYPE_TREE:
981 tree_id = got_object_tag_get_object_id(tag);
982 break;
983 default:
984 /*
985 * Tag points at something other than a
986 * commit or tree. Leave this weird tag object
987 * and the object it points to on disk.
988 */
989 err = got_object_idset_remove(NULL, loose_ids,
990 &qid->id);
991 if (err && err->code != GOT_ERR_NO_OBJ)
992 goto done;
993 err = got_object_idset_remove(NULL, loose_ids,
994 got_object_tag_get_object_id(tag));
995 if (err && err->code != GOT_ERR_NO_OBJ)
996 goto done;
997 err = NULL;
998 break;
1002 if (tree_id) {
1003 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1004 repo, npacked, cancel_cb, cancel_arg);
1005 if (err)
1006 break;
1009 if (commit || tag)
1010 (*ncommits)++; /* scanned tags are counted as commits */
1012 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1013 nloose, *ncommits, -1);
1014 if (err)
1015 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 struct got_ratelimit *rl;
1050 int nloose;
1051 int ncommits;
1052 int npurged;
1053 off_t size_purged;
1054 int dry_run;
1055 time_t max_mtime;
1056 int ignore_mtime;
1059 static const struct got_error *
1060 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1062 struct purge_loose_object_arg *a = arg;
1063 const struct got_error *err, *unlock_err = NULL;
1064 char *path = NULL;
1065 int fd = -1;
1066 struct stat sb;
1067 struct got_lockfile *lf = NULL;
1069 err = got_object_get_path(&path, id, a->repo);
1070 if (err)
1071 return err;
1073 err = got_object_open_loose_fd(&fd, id, a->repo);
1074 if (err)
1075 goto done;
1077 if (fstat(fd, &sb) == -1) {
1078 err = got_error_from_errno("fstat");
1079 goto done;
1083 * Do not delete objects which are younger than our maximum
1084 * modification time threshold. This prevents a race where
1085 * new objects which are being added to the repository
1086 * concurrently would be deleted.
1088 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1089 if (!a->dry_run) {
1090 err = got_lockfile_lock(&lf, path, -1);
1091 if (err)
1092 goto done;
1093 if (unlink(path) == -1) {
1094 err = got_error_from_errno2("unlink", path);
1095 goto done;
1099 a->npurged++;
1100 a->size_purged += sb.st_size;
1101 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1102 a->rl, a->nloose, a->ncommits, a->npurged);
1103 if (err)
1104 goto done;
1106 done:
1107 if (fd != -1 && close(fd) == -1 && err == NULL)
1108 err = got_error_from_errno("close");
1109 free(path);
1110 if (lf)
1111 unlock_err = got_lockfile_unlock(lf, -1);
1112 return err ? err : unlock_err;
1115 const struct got_error *
1116 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1117 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1118 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1119 got_cancel_cb cancel_cb, void *cancel_arg)
1121 const struct got_error *err;
1122 struct got_object_idset *loose_ids;
1123 struct got_object_idset *traversed_ids;
1124 struct got_object_id **referenced_ids;
1125 int i, nreferenced, nloose, ncommits = 0;
1126 struct got_reflist_head refs;
1127 struct got_reflist_entry *re;
1128 struct purge_loose_object_arg arg;
1129 time_t max_mtime = 0;
1130 struct got_ratelimit rl;
1132 TAILQ_INIT(&refs);
1133 got_ratelimit_init(&rl, 0, 500);
1135 *size_before = 0;
1136 *size_after = 0;
1137 *npacked = 0;
1139 err = get_loose_object_ids(&loose_ids, size_before,
1140 progress_cb, progress_arg, &rl, repo);
1141 if (err)
1142 return err;
1143 nloose = got_object_idset_num_elements(loose_ids);
1144 if (nloose == 0) {
1145 got_object_idset_free(loose_ids);
1146 if (progress_cb) {
1147 err = progress_cb(progress_arg, 0, 0, 0);
1148 if (err)
1149 return err;
1151 return NULL;
1154 traversed_ids = got_object_idset_alloc();
1155 if (traversed_ids == NULL) {
1156 err = got_error_from_errno("got_object_idset_alloc");
1157 goto done;
1160 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1161 if (err)
1162 goto done;
1163 if (!ignore_mtime) {
1164 TAILQ_FOREACH(re, &refs, entry) {
1165 time_t mtime = got_ref_get_mtime(re->ref);
1166 if (mtime > max_mtime)
1167 max_mtime = mtime;
1170 * For safety, keep objects created within 10 minutes
1171 * before the youngest reference was created.
1173 if (max_mtime >= 600)
1174 max_mtime -= 600;
1177 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1178 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1179 &refs, repo, cancel_cb, cancel_arg);
1180 if (err)
1181 goto done;
1183 for (i = 0; i < nreferenced; i++) {
1184 struct got_object_id *id = referenced_ids[i];
1185 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1186 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1187 nloose, cancel_cb, cancel_arg);
1188 if (err)
1189 goto done;
1192 /* Any remaining loose objects are unreferenced and can be purged. */
1193 arg.repo = repo;
1194 arg.progress_arg = progress_arg;
1195 arg.progress_cb = progress_cb;
1196 arg.rl = &rl;
1197 arg.nloose = nloose;
1198 arg.npurged = 0;
1199 arg.size_purged = 0;
1200 arg.ncommits = ncommits;
1201 arg.dry_run = dry_run;
1202 arg.max_mtime = max_mtime;
1203 arg.ignore_mtime = ignore_mtime;
1204 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1205 if (err)
1206 goto done;
1207 *size_after = *size_before - arg.size_purged;
1209 /* Produce a final progress report. */
1210 if (progress_cb) {
1211 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1212 if (err)
1213 goto done;
1215 done:
1216 got_object_idset_free(loose_ids);
1217 got_object_idset_free(traversed_ids);
1218 return err;
1221 static const struct got_error *
1222 remove_packidx(int dir_fd, const char *relpath)
1224 const struct got_error *err, *unlock_err;
1225 struct got_lockfile *lf;
1227 err = got_lockfile_lock(&lf, relpath, dir_fd);
1228 if (err)
1229 return err;
1230 if (unlinkat(dir_fd, relpath, 0) == -1)
1231 err = got_error_from_errno("unlinkat");
1232 unlock_err = got_lockfile_unlock(lf, dir_fd);
1233 return err ? err : unlock_err;
1236 const struct got_error *
1237 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1238 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1239 got_cancel_cb cancel_cb, void *cancel_arg)
1241 const struct got_error *err = NULL;
1242 DIR *packdir = NULL;
1243 struct dirent *dent;
1244 char *pack_relpath = NULL;
1245 int packdir_fd;
1246 struct stat sb;
1248 packdir_fd = openat(got_repo_get_fd(repo),
1249 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1250 if (packdir_fd == -1) {
1251 if (errno == ENOENT)
1252 return NULL;
1253 return got_error_from_errno_fmt("openat: %s/%s",
1254 got_repo_get_path_git_dir(repo),
1255 GOT_OBJECTS_PACK_DIR);
1258 packdir = fdopendir(packdir_fd);
1259 if (packdir == NULL) {
1260 err = got_error_from_errno("fdopendir");
1261 goto done;
1264 while ((dent = readdir(packdir)) != NULL) {
1265 if (cancel_cb) {
1266 err = cancel_cb(cancel_arg);
1267 if (err)
1268 goto done;
1271 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1272 continue;
1274 err = got_packidx_get_packfile_path(&pack_relpath,
1275 dent->d_name);
1276 if (err)
1277 goto done;
1279 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1280 free(pack_relpath);
1281 pack_relpath = NULL;
1282 continue;
1284 if (errno != ENOENT) {
1285 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1286 got_repo_get_path_git_dir(repo),
1287 GOT_OBJECTS_PACK_DIR,
1288 pack_relpath);
1289 goto done;
1292 if (!dry_run) {
1293 err = remove_packidx(packdir_fd, dent->d_name);
1294 if (err)
1295 goto done;
1297 if (progress_cb) {
1298 char *path;
1299 if (asprintf(&path, "%s/%s/%s",
1300 got_repo_get_path_git_dir(repo),
1301 GOT_OBJECTS_PACK_DIR,
1302 dent->d_name) == -1) {
1303 err = got_error_from_errno("asprintf");
1304 goto done;
1306 err = progress_cb(progress_arg, path);
1307 free(path);
1308 if (err)
1309 goto done;
1311 free(pack_relpath);
1312 pack_relpath = NULL;
1314 done:
1315 if (packdir && closedir(packdir) != 0 && err == NULL)
1316 err = got_error_from_errno("closedir");
1317 free(pack_relpath);
1318 return err;