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 <errno.h>
26 #include <fcntl.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_cancel.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository.h"
39 #include "got_repository_admin.h"
40 #include "got_opentemp.h"
41 #include "got_path.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_object_cache.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_pack_create.h"
51 #include "got_lib_sha1.h"
52 #include "got_lib_lockfile.h"
53 #include "got_lib_ratelimit.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static const struct got_error *
60 get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
61 unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
62 struct got_repository *repo,
63 got_cancel_cb cancel_cb, void *cancel_arg)
64 {
65 const struct got_error *err = NULL;
66 const size_t alloc_chunksz = 256;
67 size_t nalloc;
68 struct got_reflist_entry *re;
69 int i;
71 *ids = NULL;
72 *nobjects = 0;
74 err = got_reflist_sort(refs,
75 got_ref_cmp_by_commit_timestamp_descending, repo);
76 if (err)
77 return err;
79 *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
80 if (*ids == NULL)
81 return got_error_from_errno("reallocarray");
82 nalloc = alloc_chunksz;
84 TAILQ_FOREACH(re, refs, entry) {
85 struct got_object_id *id;
87 if (cancel_cb) {
88 err = cancel_cb(cancel_arg);
89 if (err)
90 goto done;
91 }
93 err = got_ref_resolve(&id, repo, re->ref);
94 if (err)
95 goto done;
97 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
98 int obj_type;
99 err = got_object_get_type(&obj_type, repo, id);
100 if (err)
101 goto done;
102 if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
103 free(id);
104 id = NULL;
105 continue;
109 if (nalloc <= *nobjects) {
110 struct got_object_id **new;
111 new = recallocarray(*ids, nalloc,
112 nalloc + alloc_chunksz,
113 sizeof(struct got_object_id *));
114 if (new == NULL) {
115 err = got_error_from_errno(
116 "recallocarray");
117 goto done;
119 *ids = new;
120 nalloc += alloc_chunksz;
122 (*ids)[*nobjects] = id;
123 if ((*ids)[*nobjects] == NULL) {
124 err = got_error_from_errno("got_object_id_dup");
125 goto done;
127 (*nobjects)++;
129 done:
130 if (err) {
131 for (i = 0; i < *nobjects; i++)
132 free((*ids)[i]);
133 free(*ids);
134 *ids = NULL;
135 *nobjects = 0;
137 return err;
140 const struct got_error *
141 got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
142 struct got_reflist_head *include_refs,
143 struct got_reflist_head *exclude_refs, struct got_repository *repo,
144 int loose_obj_only,
145 got_pack_progress_cb progress_cb, void *progress_arg,
146 got_cancel_cb cancel_cb, void *cancel_arg)
148 const struct got_error *err = NULL;
149 struct got_object_id **ours = NULL, **theirs = NULL;
150 int nours = 0, ntheirs = 0, packfd = -1, i;
151 char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
152 char *sha1_str = NULL;
153 FILE *delta_cache = NULL;
155 *packfile = NULL;
156 *pack_hash = NULL;
158 if (asprintf(&path, "%s/%s/packing.pack",
159 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
160 err = got_error_from_errno("asprintf");
161 goto done;
163 err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
164 if (err)
165 goto done;
167 if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
168 err = got_error_from_errno2("fchmod", tmpfile_path);
169 goto done;
172 delta_cache = got_opentemp();
173 if (delta_cache == NULL) {
174 err = got_error_from_errno("got_opentemp");
175 goto done;
178 err = get_reflist_object_ids(&ours, &nours,
179 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
180 include_refs, repo, cancel_cb, cancel_arg);
181 if (err)
182 goto done;
184 if (nours == 0) {
185 err = got_error(GOT_ERR_CANNOT_PACK);
186 goto done;
189 if (!TAILQ_EMPTY(exclude_refs)) {
190 err = get_reflist_object_ids(&theirs, &ntheirs,
191 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
192 exclude_refs, repo,
193 cancel_cb, cancel_arg);
194 if (err)
195 goto done;
198 *pack_hash = calloc(1, sizeof(**pack_hash));
199 if (*pack_hash == NULL) {
200 err = got_error_from_errno("calloc");
201 goto done;
204 err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
205 theirs, ntheirs, ours, nours, repo, loose_obj_only, 0,
206 progress_cb, progress_arg, cancel_cb, cancel_arg);
207 if (err)
208 goto done;
210 err = got_object_id_str(&sha1_str, *pack_hash);
211 if (err)
212 goto done;
213 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
214 got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
215 sha1_str) == -1) {
216 err = got_error_from_errno("asprintf");
217 goto done;
220 if (lseek(packfd, 0L, SEEK_SET) == -1) {
221 err = got_error_from_errno("lseek");
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;
232 *packfile = fdopen(packfd, "w");
233 if (*packfile == NULL) {
234 err = got_error_from_errno2("fdopen", tmpfile_path);
235 goto done;
237 packfd = -1;
238 done:
239 for (i = 0; i < nours; i++)
240 free(ours[i]);
241 free(ours);
242 for (i = 0; i < ntheirs; i++)
243 free(theirs[i]);
244 free(theirs);
245 if (packfd != -1 && close(packfd) == -1 && err == NULL)
246 err = got_error_from_errno2("close", packfile_path);
247 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
248 err = got_error_from_errno("fclose");
249 if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
250 err = got_error_from_errno2("unlink", tmpfile_path);
251 free(tmpfile_path);
252 free(packfile_path);
253 free(sha1_str);
254 free(path);
255 if (err) {
256 free(*pack_hash);
257 *pack_hash = NULL;
258 if (*packfile)
259 fclose(*packfile);
260 *packfile = NULL;
262 return err;
265 const struct got_error *
266 got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
267 struct got_repository *repo,
268 got_pack_index_progress_cb progress_cb, void *progress_arg,
269 got_cancel_cb cancel_cb, void *cancel_arg)
271 size_t i;
272 char *path;
273 int imsg_idxfds[2];
274 int npackfd = -1, idxfd = -1, nidxfd = -1;
275 int tmpfds[3];
276 int idxstatus, done = 0;
277 const struct got_error *err;
278 struct imsgbuf idxibuf;
279 pid_t idxpid;
280 char *tmpidxpath = NULL;
281 char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
282 const char *repo_path = got_repo_get_path_git_dir(repo);
283 struct stat sb;
285 for (i = 0; i < nitems(tmpfds); i++)
286 tmpfds[i] = -1;
288 if (asprintf(&path, "%s/%s/indexing.idx",
289 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 goto done;
293 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
294 free(path);
295 if (err)
296 goto done;
297 if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
298 err = got_error_from_errno2("fchmod", tmpidxpath);
299 goto done;
302 nidxfd = dup(idxfd);
303 if (nidxfd == -1) {
304 err = got_error_from_errno("dup");
305 goto done;
308 for (i = 0; i < nitems(tmpfds); i++) {
309 tmpfds[i] = got_opentempfd();
310 if (tmpfds[i] == -1) {
311 err = got_error_from_errno("got_opentempfd");
312 goto done;
316 err = got_object_id_str(&id_str, pack_hash);
317 if (err)
318 goto done;
320 if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
321 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
322 err = got_error_from_errno("asprintf");
323 goto done;
326 if (fstat(fileno(packfile), &sb) == -1) {
327 err = got_error_from_errno2("fstat", packfile_path);
328 goto done;
331 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
332 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
333 err = got_error_from_errno("asprintf");
334 goto done;
337 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
338 err = got_error_from_errno("socketpair");
339 goto done;
341 idxpid = fork();
342 if (idxpid == -1) {
343 err= got_error_from_errno("fork");
344 goto done;
345 } else if (idxpid == 0)
346 got_privsep_exec_child(imsg_idxfds,
347 GOT_PATH_PROG_INDEX_PACK, packfile_path);
348 if (close(imsg_idxfds[1]) == -1) {
349 err = got_error_from_errno("close");
350 goto done;
352 imsg_init(&idxibuf, imsg_idxfds[0]);
354 npackfd = dup(fileno(packfile));
355 if (npackfd == -1) {
356 err = got_error_from_errno("dup");
357 goto done;
359 err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
360 npackfd);
361 if (err != NULL)
362 goto done;
363 npackfd = -1;
364 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
365 if (err != NULL)
366 goto done;
367 nidxfd = -1;
368 for (i = 0; i < nitems(tmpfds); i++) {
369 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
370 if (err != NULL)
371 goto done;
372 tmpfds[i] = -1;
374 done = 0;
375 while (!done) {
376 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
378 if (cancel_cb) {
379 err = cancel_cb(cancel_arg);
380 if (err)
381 goto done;
384 err = got_privsep_recv_index_progress(&done, &nobj_total,
385 &nobj_indexed, &nobj_loose, &nobj_resolved,
386 &idxibuf);
387 if (err != NULL)
388 goto done;
389 if (nobj_indexed != 0) {
390 err = progress_cb(progress_arg, sb.st_size,
391 nobj_total, nobj_indexed, nobj_loose,
392 nobj_resolved);
393 if (err)
394 break;
397 if (close(imsg_idxfds[0]) == -1) {
398 err = got_error_from_errno("close");
399 goto done;
401 if (waitpid(idxpid, &idxstatus, 0) == -1) {
402 err = got_error_from_errno("waitpid");
403 goto done;
406 if (rename(tmpidxpath, idxpath) == -1) {
407 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
408 goto done;
410 free(tmpidxpath);
411 tmpidxpath = NULL;
413 done:
414 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
415 err = got_error_from_errno2("unlink", tmpidxpath);
416 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
417 err = got_error_from_errno("close");
418 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
419 err = got_error_from_errno("close");
420 for (i = 0; i < nitems(tmpfds); i++) {
421 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
422 err = got_error_from_errno("close");
424 free(tmpidxpath);
425 free(idxpath);
426 free(packfile_path);
427 return err;
430 const struct got_error *
431 got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
432 struct got_repository *repo, const char *packfile_path)
434 const struct got_error *err = NULL;
435 const char *packdir_path = NULL;
436 char *packfile_name = NULL, *p, *dot;
437 struct got_object_id id;
438 int packfd = -1;
440 *packfile = NULL;
441 *pack_hash = NULL;
443 packdir_path = got_repo_get_path_objects_pack(repo);
444 if (packdir_path == NULL)
445 return got_error_from_errno("got_repo_get_path_objects_pack");
447 if (!got_path_is_child(packfile_path, packdir_path,
448 strlen(packdir_path))) {
449 err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
450 goto done;
454 err = got_path_basename(&packfile_name, packfile_path);
455 if (err)
456 goto done;
457 p = packfile_name;
459 if (strncmp(p, "pack-", 5) != 0) {
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 p += 5;
466 dot = strchr(p, '.');
467 if (dot == NULL) {
468 err = got_error_fmt(GOT_ERR_BAD_PATH,
469 "'%s' is not a valid pack file name",
470 packfile_name);
471 goto done;
473 if (strcmp(dot + 1, "pack") != 0) {
474 err = got_error_fmt(GOT_ERR_BAD_PATH,
475 "'%s' is not a valid pack file name",
476 packfile_name);
477 goto done;
479 *dot = '\0';
480 if (!got_parse_sha1_digest(id.sha1, p)) {
481 err = got_error_fmt(GOT_ERR_BAD_PATH,
482 "'%s' is not a valid pack file name",
483 packfile_name);
484 goto done;
487 *pack_hash = got_object_id_dup(&id);
488 if (*pack_hash == NULL) {
489 err = got_error_from_errno("got_object_id_dup");
490 goto done;
493 packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
494 if (packfd == -1) {
495 err = got_error_from_errno2("open", packfile_path);
496 goto done;
499 *packfile = fdopen(packfd, "r");
500 if (*packfile == NULL) {
501 err = got_error_from_errno2("fdopen", packfile_path);
502 goto done;
504 packfd = -1;
505 done:
506 if (packfd != -1 && close(packfd) == -1 && err == NULL)
507 err = got_error_from_errno2("close", packfile_path);
508 free(packfile_name);
509 if (err) {
510 free(*pack_hash);
511 *pack_hash = NULL;
513 return err;
516 const struct got_error *
517 got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
518 struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
519 got_cancel_cb cancel_cb, void *cancel_arg)
521 const struct got_error *err = NULL;
522 char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
523 struct got_packidx *packidx = NULL;
524 struct got_pack *pack = NULL;
525 uint32_t nobj, i;
527 err = got_object_id_str(&id_str, pack_hash);
528 if (err)
529 goto done;
531 if (asprintf(&packpath, "%s/pack-%s.pack",
532 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
533 err = got_error_from_errno("asprintf");
534 goto done;
536 if (asprintf(&idxpath, "%s/pack-%s.idx",
537 GOT_OBJECTS_PACK_DIR, id_str) == -1) {
538 err = got_error_from_errno("asprintf");
539 goto done;
542 err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
543 if (err)
544 goto done;
546 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
547 if (err)
548 goto done;
550 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
551 for (i = 0; i < nobj; i++) {
552 struct got_packidx_object_id *oid;
553 struct got_object_id id, base_id;
554 off_t offset, base_offset = 0;
555 uint8_t type;
556 uint64_t size;
557 size_t tslen, len;
559 if (cancel_cb) {
560 err = cancel_cb(cancel_arg);
561 if (err)
562 break;
564 oid = &packidx->hdr.sorted_ids[i];
565 memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
567 offset = got_packidx_get_object_offset(packidx, i);
568 if (offset == -1) {
569 err = got_error(GOT_ERR_BAD_PACKIDX);
570 goto done;
573 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
574 pack, offset);
575 if (err)
576 goto done;
578 switch (type) {
579 case GOT_OBJ_TYPE_OFFSET_DELTA:
580 err = got_pack_parse_offset_delta(&base_offset, &len,
581 pack, offset, tslen);
582 if (err)
583 goto done;
584 break;
585 case GOT_OBJ_TYPE_REF_DELTA:
586 err = got_pack_parse_ref_delta(&base_id,
587 pack, offset, tslen);
588 if (err)
589 goto done;
590 break;
592 err = (*list_cb)(list_arg, &id, type, offset, size,
593 base_offset, &base_id);
594 if (err)
595 goto done;
598 done:
599 free(id_str);
600 free(idxpath);
601 free(packpath);
602 if (packidx)
603 got_packidx_close(packidx);
604 return err;
607 static const struct got_error *
608 report_cleanup_progress(got_cleanup_progress_cb progress_cb,
609 void *progress_arg, struct got_ratelimit *rl,
610 int nloose, int ncommits, int npurged)
612 const struct got_error *err;
613 int elapsed;
615 if (progress_cb == NULL)
616 return NULL;
618 err = got_ratelimit_check(&elapsed, rl);
619 if (err || !elapsed)
620 return err;
622 return progress_cb(progress_arg, nloose, ncommits, npurged);
625 static const struct got_error *
626 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
627 got_cleanup_progress_cb progress_cb, void *progress_arg,
628 struct got_ratelimit *rl, struct got_repository *repo)
630 const struct got_error *err = NULL;
631 char *path_objects = NULL, *path = NULL;
632 DIR *dir = NULL;
633 struct got_object *obj = NULL;
634 struct got_object_id id;
635 int i, fd = -1;
636 struct stat sb;
638 *ondisk_size = 0;
639 *loose_ids = got_object_idset_alloc();
640 if (*loose_ids == NULL)
641 return got_error_from_errno("got_object_idset_alloc");
643 path_objects = got_repo_get_path_objects(repo);
644 if (path_objects == NULL) {
645 err = got_error_from_errno("got_repo_get_path_objects");
646 goto done;
649 for (i = 0; i <= 0xff; i++) {
650 struct dirent *dent;
652 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
653 err = got_error_from_errno("asprintf");
654 break;
657 dir = opendir(path);
658 if (dir == NULL) {
659 if (errno == ENOENT) {
660 err = NULL;
661 continue;
663 err = got_error_from_errno2("opendir", path);
664 break;
667 while ((dent = readdir(dir)) != NULL) {
668 char *id_str;
670 if (strcmp(dent->d_name, ".") == 0 ||
671 strcmp(dent->d_name, "..") == 0)
672 continue;
674 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
675 err = got_error_from_errno("asprintf");
676 goto done;
679 memset(&id, 0, sizeof(id));
680 if (!got_parse_sha1_digest(id.sha1, id_str)) {
681 free(id_str);
682 continue;
684 free(id_str);
686 err = got_object_open_loose_fd(&fd, &id, repo);
687 if (err)
688 goto done;
689 if (fstat(fd, &sb) == -1) {
690 err = got_error_from_errno("fstat");
691 goto done;
693 err = got_object_read_header_privsep(&obj, &id, repo,
694 fd);
695 if (err)
696 goto done;
697 fd = -1; /* already closed */
699 switch (obj->type) {
700 case GOT_OBJ_TYPE_COMMIT:
701 case GOT_OBJ_TYPE_TREE:
702 case GOT_OBJ_TYPE_BLOB:
703 case GOT_OBJ_TYPE_TAG:
704 break;
705 default:
706 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
707 "%d", obj->type);
708 goto done;
710 got_object_close(obj);
711 obj = NULL;
712 (*ondisk_size) += sb.st_size;
713 err = got_object_idset_add(*loose_ids, &id, NULL);
714 if (err)
715 goto done;
716 err = report_cleanup_progress(progress_cb,
717 progress_arg, rl,
718 got_object_idset_num_elements(*loose_ids), -1, -1);
719 if (err)
720 goto done;
723 if (closedir(dir) != 0) {
724 err = got_error_from_errno("closedir");
725 goto done;
727 dir = NULL;
729 free(path);
730 path = NULL;
732 done:
733 if (dir && closedir(dir) != 0 && err == NULL)
734 err = got_error_from_errno("closedir");
735 if (fd != -1 && close(fd) == -1 && err == NULL)
736 err = got_error_from_errno("close");
737 if (err) {
738 got_object_idset_free(*loose_ids);
739 *loose_ids = NULL;
741 if (obj)
742 got_object_close(obj);
743 free(path_objects);
744 free(path);
745 return err;
748 static const struct got_error *
749 preserve_loose_object(struct got_object_idset *loose_ids,
750 struct got_object_id *id, struct got_repository *repo, int *npacked)
752 const struct got_error *err = NULL;
753 struct got_object *obj;
755 if (!got_object_idset_contains(loose_ids, id))
756 return NULL;
758 /*
759 * Try to open this object from a pack file. This ensures that
760 * we do in fact have a valid packed copy of the object. Otherwise
761 * we should not delete the loose representation of this object.
762 */
763 err = got_object_open_packed(&obj, id, repo);
764 if (err == NULL) {
765 got_object_close(obj);
766 /*
767 * The object is referenced and packed.
768 * We can purge the redundantly stored loose object.
769 */
770 (*npacked)++;
771 return NULL;
772 } else if (err->code != GOT_ERR_NO_OBJ)
773 return err;
775 /*
776 * This object is referenced and not packed.
777 * Remove it from our purge set.
778 */
779 return got_object_idset_remove(NULL, loose_ids, id);
782 static const struct got_error *
783 load_tree_entries(struct got_object_id_queue *ids,
784 struct got_object_idset *loose_ids,
785 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
786 const char *dpath, struct got_repository *repo, int *npacked,
787 got_cancel_cb cancel_cb, void *cancel_arg)
789 const struct got_error *err;
790 struct got_tree_object *tree;
791 char *p = NULL;
792 int i;
794 err = got_object_open_as_tree(&tree, repo, tree_id);
795 if (err)
796 return err;
798 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
799 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
800 struct got_object_id *id = got_tree_entry_get_id(e);
801 mode_t mode = got_tree_entry_get_mode(e);
803 if (cancel_cb) {
804 err = (*cancel_cb)(cancel_arg);
805 if (err)
806 break;
809 if (got_object_tree_entry_is_symlink(e) ||
810 got_object_tree_entry_is_submodule(e) ||
811 got_object_idset_contains(traversed_ids, id))
812 continue;
814 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
815 got_tree_entry_get_name(e)) == -1) {
816 err = got_error_from_errno("asprintf");
817 break;
820 if (S_ISDIR(mode)) {
821 struct got_object_qid *qid;
822 err = got_object_qid_alloc(&qid, id);
823 if (err)
824 break;
825 STAILQ_INSERT_TAIL(ids, qid, entry);
826 } else if (S_ISREG(mode)) {
827 /* This blob is referenced. */
828 err = preserve_loose_object(loose_ids, id, repo,
829 npacked);
830 if (err)
831 break;
832 err = got_object_idset_add(traversed_ids, id, NULL);
833 if (err)
834 break;
836 free(p);
837 p = NULL;
840 got_object_tree_close(tree);
841 free(p);
842 return err;
845 static const struct got_error *
846 load_tree(struct got_object_idset *loose_ids,
847 struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
848 const char *dpath, struct got_repository *repo, int *npacked,
849 got_cancel_cb cancel_cb, void *cancel_arg)
851 const struct got_error *err = NULL;
852 struct got_object_id_queue tree_ids;
853 struct got_object_qid *qid;
855 err = got_object_qid_alloc(&qid, tree_id);
856 if (err)
857 return err;
859 STAILQ_INIT(&tree_ids);
860 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
862 while (!STAILQ_EMPTY(&tree_ids)) {
863 if (cancel_cb) {
864 err = (*cancel_cb)(cancel_arg);
865 if (err)
866 break;
869 qid = STAILQ_FIRST(&tree_ids);
870 STAILQ_REMOVE_HEAD(&tree_ids, entry);
872 if (got_object_idset_contains(traversed_ids, &qid->id)) {
873 got_object_qid_free(qid);
874 continue;
877 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
878 if (err) {
879 got_object_qid_free(qid);
880 break;
883 /* This tree is referenced. */
884 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
885 if (err)
886 break;
888 err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
889 &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
890 got_object_qid_free(qid);
891 if (err)
892 break;
895 got_object_id_queue_free(&tree_ids);
896 return err;
899 static const struct got_error *
900 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
901 int *npacked, struct got_object_idset *traversed_ids,
902 struct got_object_id *id, struct got_repository *repo,
903 got_cleanup_progress_cb progress_cb, void *progress_arg,
904 struct got_ratelimit *rl, int nloose,
905 got_cancel_cb cancel_cb, void *cancel_arg)
907 const struct got_error *err;
908 struct got_commit_object *commit = NULL;
909 struct got_tag_object *tag = NULL;
910 struct got_object_id *tree_id = NULL;
911 struct got_object_id_queue ids;
912 struct got_object_qid *qid;
913 int obj_type;
915 err = got_object_qid_alloc(&qid, id);
916 if (err)
917 return err;
919 STAILQ_INIT(&ids);
920 STAILQ_INSERT_TAIL(&ids, qid, entry);
922 while (!STAILQ_EMPTY(&ids)) {
923 if (cancel_cb) {
924 err = (*cancel_cb)(cancel_arg);
925 if (err)
926 break;
929 qid = STAILQ_FIRST(&ids);
930 STAILQ_REMOVE_HEAD(&ids, entry);
932 if (got_object_idset_contains(traversed_ids, &qid->id)) {
933 got_object_qid_free(qid);
934 qid = NULL;
935 continue;
938 err = got_object_idset_add(traversed_ids, &qid->id, NULL);
939 if (err)
940 break;
942 /* This commit or tag is referenced. */
943 err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
944 if (err)
945 break;
947 err = got_object_get_type(&obj_type, repo, &qid->id);
948 if (err)
949 break;
950 switch (obj_type) {
951 case GOT_OBJ_TYPE_COMMIT:
952 err = got_object_open_as_commit(&commit, repo,
953 &qid->id);
954 if (err)
955 goto done;
956 break;
957 case GOT_OBJ_TYPE_TAG:
958 err = got_object_open_as_tag(&tag, repo, &qid->id);
959 if (err)
960 goto done;
961 break;
962 default:
963 /* should not happen */
964 err = got_error(GOT_ERR_OBJ_TYPE);
965 goto done;
968 /* Find a tree object to scan. */
969 if (commit) {
970 tree_id = got_object_commit_get_tree_id(commit);
971 } else if (tag) {
972 obj_type = got_object_tag_get_object_type(tag);
973 switch (obj_type) {
974 case GOT_OBJ_TYPE_COMMIT:
975 err = got_object_open_as_commit(&commit, repo,
976 got_object_tag_get_object_id(tag));
977 if (err)
978 goto done;
979 tree_id = got_object_commit_get_tree_id(commit);
980 break;
981 case GOT_OBJ_TYPE_TREE:
982 tree_id = got_object_tag_get_object_id(tag);
983 break;
984 default:
985 /*
986 * Tag points at something other than a
987 * commit or tree. Leave this weird tag object
988 * and the object it points to on disk.
989 */
990 err = got_object_idset_remove(NULL, loose_ids,
991 &qid->id);
992 if (err && err->code != GOT_ERR_NO_OBJ)
993 goto done;
994 err = got_object_idset_remove(NULL, loose_ids,
995 got_object_tag_get_object_id(tag));
996 if (err && err->code != GOT_ERR_NO_OBJ)
997 goto done;
998 err = NULL;
999 break;
1003 if (tree_id) {
1004 err = load_tree(loose_ids, traversed_ids, tree_id, "",
1005 repo, npacked, cancel_cb, cancel_arg);
1006 if (err)
1007 break;
1010 if (commit || tag)
1011 (*ncommits)++; /* scanned tags are counted as commits */
1013 err = report_cleanup_progress(progress_cb, progress_arg, rl,
1014 nloose, *ncommits, -1);
1015 if (err)
1016 break;
1018 if (commit) {
1019 /* Find parent commits to scan. */
1020 const struct got_object_id_queue *parent_ids;
1021 parent_ids = got_object_commit_get_parent_ids(commit);
1022 err = got_object_id_queue_copy(parent_ids, &ids);
1023 if (err)
1024 break;
1025 got_object_commit_close(commit);
1026 commit = NULL;
1028 if (tag) {
1029 got_object_tag_close(tag);
1030 tag = NULL;
1032 got_object_qid_free(qid);
1033 qid = NULL;
1035 done:
1036 if (qid)
1037 got_object_qid_free(qid);
1038 if (commit)
1039 got_object_commit_close(commit);
1040 if (tag)
1041 got_object_tag_close(tag);
1042 got_object_id_queue_free(&ids);
1043 return err;
1046 struct purge_loose_object_arg {
1047 struct got_repository *repo;
1048 got_cleanup_progress_cb progress_cb;
1049 void *progress_arg;
1050 struct got_ratelimit *rl;
1051 int nloose;
1052 int ncommits;
1053 int npurged;
1054 off_t size_purged;
1055 int dry_run;
1056 time_t max_mtime;
1057 int ignore_mtime;
1060 static const struct got_error *
1061 purge_loose_object(struct got_object_id *id, void *data, void *arg)
1063 struct purge_loose_object_arg *a = arg;
1064 const struct got_error *err, *unlock_err = NULL;
1065 char *path = NULL;
1066 int fd = -1;
1067 struct stat sb;
1068 struct got_lockfile *lf = NULL;
1070 err = got_object_get_path(&path, id, a->repo);
1071 if (err)
1072 return err;
1074 err = got_object_open_loose_fd(&fd, id, a->repo);
1075 if (err)
1076 goto done;
1078 if (fstat(fd, &sb) == -1) {
1079 err = got_error_from_errno("fstat");
1080 goto done;
1084 * Do not delete objects which are younger than our maximum
1085 * modification time threshold. This prevents a race where
1086 * new objects which are being added to the repository
1087 * concurrently would be deleted.
1089 if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1090 if (!a->dry_run) {
1091 err = got_lockfile_lock(&lf, path, -1);
1092 if (err)
1093 goto done;
1094 if (unlink(path) == -1) {
1095 err = got_error_from_errno2("unlink", path);
1096 goto done;
1100 a->npurged++;
1101 a->size_purged += sb.st_size;
1102 err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1103 a->rl, a->nloose, a->ncommits, a->npurged);
1104 if (err)
1105 goto done;
1107 done:
1108 if (fd != -1 && close(fd) == -1 && err == NULL)
1109 err = got_error_from_errno("close");
1110 free(path);
1111 if (lf)
1112 unlock_err = got_lockfile_unlock(lf, -1);
1113 return err ? err : unlock_err;
1116 const struct got_error *
1117 got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1118 off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1119 int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1120 got_cancel_cb cancel_cb, void *cancel_arg)
1122 const struct got_error *err;
1123 struct got_object_idset *loose_ids;
1124 struct got_object_idset *traversed_ids;
1125 struct got_object_id **referenced_ids;
1126 int i, nreferenced, nloose, ncommits = 0;
1127 struct got_reflist_head refs;
1128 struct got_reflist_entry *re;
1129 struct purge_loose_object_arg arg;
1130 time_t max_mtime = 0;
1131 struct got_ratelimit rl;
1133 TAILQ_INIT(&refs);
1134 got_ratelimit_init(&rl, 0, 500);
1136 *size_before = 0;
1137 *size_after = 0;
1138 *npacked = 0;
1140 err = get_loose_object_ids(&loose_ids, size_before,
1141 progress_cb, progress_arg, &rl, repo);
1142 if (err)
1143 return err;
1144 nloose = got_object_idset_num_elements(loose_ids);
1145 if (nloose == 0) {
1146 got_object_idset_free(loose_ids);
1147 if (progress_cb) {
1148 err = progress_cb(progress_arg, 0, 0, 0);
1149 if (err)
1150 return err;
1152 return NULL;
1155 traversed_ids = got_object_idset_alloc();
1156 if (traversed_ids == NULL) {
1157 err = got_error_from_errno("got_object_idset_alloc");
1158 goto done;
1161 err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1162 if (err)
1163 goto done;
1164 if (!ignore_mtime) {
1165 TAILQ_FOREACH(re, &refs, entry) {
1166 time_t mtime = got_ref_get_mtime(re->ref);
1167 if (mtime > max_mtime)
1168 max_mtime = mtime;
1171 * For safety, keep objects created within 10 minutes
1172 * before the youngest reference was created.
1174 if (max_mtime >= 600)
1175 max_mtime -= 600;
1178 err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1179 (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1180 &refs, repo, cancel_cb, cancel_arg);
1181 if (err)
1182 goto done;
1184 for (i = 0; i < nreferenced; i++) {
1185 struct got_object_id *id = referenced_ids[i];
1186 err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1187 traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1188 nloose, cancel_cb, cancel_arg);
1189 if (err)
1190 goto done;
1193 /* Any remaining loose objects are unreferenced and can be purged. */
1194 arg.repo = repo;
1195 arg.progress_arg = progress_arg;
1196 arg.progress_cb = progress_cb;
1197 arg.rl = &rl;
1198 arg.nloose = nloose;
1199 arg.npurged = 0;
1200 arg.size_purged = 0;
1201 arg.ncommits = ncommits;
1202 arg.dry_run = dry_run;
1203 arg.max_mtime = max_mtime;
1204 arg.ignore_mtime = ignore_mtime;
1205 err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1206 if (err)
1207 goto done;
1208 *size_after = *size_before - arg.size_purged;
1210 /* Produce a final progress report. */
1211 if (progress_cb) {
1212 err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1213 if (err)
1214 goto done;
1216 done:
1217 got_object_idset_free(loose_ids);
1218 got_object_idset_free(traversed_ids);
1219 return err;
1222 static const struct got_error *
1223 remove_packidx(int dir_fd, const char *relpath)
1225 const struct got_error *err, *unlock_err;
1226 struct got_lockfile *lf;
1228 err = got_lockfile_lock(&lf, relpath, dir_fd);
1229 if (err)
1230 return err;
1231 if (unlinkat(dir_fd, relpath, 0) == -1)
1232 err = got_error_from_errno("unlinkat");
1233 unlock_err = got_lockfile_unlock(lf, dir_fd);
1234 return err ? err : unlock_err;
1237 const struct got_error *
1238 got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1239 got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1240 got_cancel_cb cancel_cb, void *cancel_arg)
1242 const struct got_error *err = NULL;
1243 DIR *packdir = NULL;
1244 struct dirent *dent;
1245 char *pack_relpath = NULL;
1246 int packdir_fd;
1247 struct stat sb;
1249 packdir_fd = openat(got_repo_get_fd(repo),
1250 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1251 if (packdir_fd == -1) {
1252 if (errno == ENOENT)
1253 return NULL;
1254 return got_error_from_errno_fmt("openat: %s/%s",
1255 got_repo_get_path_git_dir(repo),
1256 GOT_OBJECTS_PACK_DIR);
1259 packdir = fdopendir(packdir_fd);
1260 if (packdir == NULL) {
1261 err = got_error_from_errno("fdopendir");
1262 goto done;
1265 while ((dent = readdir(packdir)) != NULL) {
1266 if (cancel_cb) {
1267 err = cancel_cb(cancel_arg);
1268 if (err)
1269 goto done;
1272 if (!got_repo_is_packidx_filename(dent->d_name,
1273 strlen(dent->d_name)))
1274 continue;
1276 err = got_packidx_get_packfile_path(&pack_relpath,
1277 dent->d_name);
1278 if (err)
1279 goto done;
1281 if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1282 free(pack_relpath);
1283 pack_relpath = NULL;
1284 continue;
1286 if (errno != ENOENT) {
1287 err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1288 got_repo_get_path_git_dir(repo),
1289 GOT_OBJECTS_PACK_DIR,
1290 pack_relpath);
1291 goto done;
1294 if (!dry_run) {
1295 err = remove_packidx(packdir_fd, dent->d_name);
1296 if (err)
1297 goto done;
1299 if (progress_cb) {
1300 char *path;
1301 if (asprintf(&path, "%s/%s/%s",
1302 got_repo_get_path_git_dir(repo),
1303 GOT_OBJECTS_PACK_DIR,
1304 dent->d_name) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 goto done;
1308 err = progress_cb(progress_arg, path);
1309 free(path);
1310 if (err)
1311 goto done;
1313 free(pack_relpath);
1314 pack_relpath = NULL;
1316 done:
1317 if (packdir && closedir(packdir) != 0 && err == NULL)
1318 err = got_error_from_errno("closedir");
1319 free(pack_relpath);
1320 return err;