gotwebd: fix some got_error_from_errno2/3 calls
[got-portable.git] / gotadmin / gotadmin.c
bloba72a024f791dd1baf5ea67c1f1416a7f42950866
1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <getopt.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <locale.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "got_version.h"
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_cancel.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_gotconfig.h"
42 #include "got_path.h"
43 #include "got_privsep.h"
44 #include "got_opentemp.h"
45 #include "got_worktree.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
51 static volatile sig_atomic_t sigint_received;
52 static volatile sig_atomic_t sigpipe_received;
54 static void
55 catch_sigint(int signo)
57 sigint_received = 1;
60 static void
61 catch_sigpipe(int signo)
63 sigpipe_received = 1;
66 static const struct got_error *
67 check_cancelled(void *arg)
69 if (sigint_received || sigpipe_received)
70 return got_error(GOT_ERR_CANCELLED);
71 return NULL;
74 struct gotadmin_cmd {
75 const char *cmd_name;
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
81 __dead static void usage(int, int);
82 __dead static void usage_init(void);
83 __dead static void usage_info(void);
84 __dead static void usage_pack(void);
85 __dead static void usage_indexpack(void);
86 __dead static void usage_listpack(void);
87 __dead static void usage_cleanup(void);
89 static const struct got_error* cmd_init(int, char *[]);
90 static const struct got_error* cmd_info(int, char *[]);
91 static const struct got_error* cmd_pack(int, char *[]);
92 static const struct got_error* cmd_indexpack(int, char *[]);
93 static const struct got_error* cmd_listpack(int, char *[]);
94 static const struct got_error* cmd_cleanup(int, char *[]);
96 static const struct gotadmin_cmd gotadmin_commands[] = {
97 { "init", cmd_init, usage_init, "" },
98 { "info", cmd_info, usage_info, "" },
99 { "pack", cmd_pack, usage_pack, "" },
100 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
101 { "listpack", cmd_listpack, usage_listpack, "ls" },
102 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
105 static void
106 list_commands(FILE *fp)
108 size_t i;
110 fprintf(fp, "commands:");
111 for (i = 0; i < nitems(gotadmin_commands); i++) {
112 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
113 fprintf(fp, " %s", cmd->cmd_name);
115 fputc('\n', fp);
119 main(int argc, char *argv[])
121 const struct gotadmin_cmd *cmd;
122 size_t i;
123 int ch;
124 int hflag = 0, Vflag = 0;
125 static const struct option longopts[] = {
126 { "version", no_argument, NULL, 'V' },
127 { NULL, 0, NULL, 0 }
130 setlocale(LC_CTYPE, "");
132 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
133 switch (ch) {
134 case 'h':
135 hflag = 1;
136 break;
137 case 'V':
138 Vflag = 1;
139 break;
140 default:
141 usage(hflag, 1);
142 /* NOTREACHED */
146 argc -= optind;
147 argv += optind;
148 optind = 1;
149 optreset = 1;
151 if (Vflag) {
152 got_version_print_str();
153 return 0;
156 if (argc <= 0)
157 usage(hflag, hflag ? 0 : 1);
159 signal(SIGINT, catch_sigint);
160 signal(SIGPIPE, catch_sigpipe);
162 for (i = 0; i < nitems(gotadmin_commands); i++) {
163 const struct got_error *error;
165 cmd = &gotadmin_commands[i];
167 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
168 strcmp(cmd->cmd_alias, argv[0]) != 0)
169 continue;
171 if (hflag)
172 cmd->cmd_usage();
174 error = cmd->cmd_main(argc, argv);
175 if (error && error->code != GOT_ERR_CANCELLED &&
176 error->code != GOT_ERR_PRIVSEP_EXIT &&
177 !(sigpipe_received &&
178 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
179 !(sigint_received &&
180 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
181 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
182 return 1;
185 return 0;
188 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
189 list_commands(stderr);
190 return 1;
193 __dead static void
194 usage(int hflag, int status)
196 FILE *fp = (status == 0) ? stdout : stderr;
198 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
199 getprogname());
200 if (hflag)
201 list_commands(fp);
202 exit(status);
205 static const struct got_error *
206 apply_unveil(const char *repo_path, int repo_read_only)
208 const struct got_error *err;
210 #ifdef PROFILE
211 if (unveil("gmon.out", "rwc") != 0)
212 return got_error_from_errno2("unveil", "gmon.out");
213 #endif
214 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
215 return got_error_from_errno2("unveil", repo_path);
217 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
218 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
220 err = got_privsep_unveil_exec_helpers();
221 if (err != NULL)
222 return err;
224 if (unveil(NULL, NULL) != 0)
225 return got_error_from_errno("unveil");
227 return NULL;
230 __dead static void
231 usage_info(void)
233 fprintf(stderr, "usage: %s info [-r repository-path]\n",
234 getprogname());
235 exit(1);
238 static const struct got_error *
239 get_repo_path(char **repo_path)
241 const struct got_error *err = NULL;
242 struct got_worktree *worktree = NULL;
243 char *cwd;
245 *repo_path = NULL;
247 cwd = getcwd(NULL, 0);
248 if (cwd == NULL)
249 return got_error_from_errno("getcwd");
251 err = got_worktree_open(&worktree, cwd);
252 if (err) {
253 if (err->code != GOT_ERR_NOT_WORKTREE)
254 goto done;
255 err = NULL;
258 if (worktree)
259 *repo_path = strdup(got_worktree_get_repo_path(worktree));
260 else
261 *repo_path = strdup(cwd);
262 if (*repo_path == NULL)
263 err = got_error_from_errno("strdup");
264 done:
265 if (worktree)
266 got_worktree_close(worktree);
267 free(cwd);
268 return err;
271 __dead static void
272 usage_init(void)
274 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
275 getprogname());
276 exit(1);
279 static const struct got_error *
280 cmd_init(int argc, char *argv[])
282 const struct got_error *error = NULL;
283 const char *head_name = NULL;
284 char *repo_path = NULL;
285 int ch;
287 #ifndef PROFILE
288 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
289 err(1, "pledge");
290 #endif
292 while ((ch = getopt(argc, argv, "b:")) != -1) {
293 switch (ch) {
294 case 'b':
295 head_name = optarg;
296 break;
297 default:
298 usage_init();
299 /* NOTREACHED */
303 argc -= optind;
304 argv += optind;
306 if (argc != 1)
307 usage_init();
309 repo_path = strdup(argv[0]);
310 if (repo_path == NULL)
311 return got_error_from_errno("strdup");
313 got_path_strip_trailing_slashes(repo_path);
315 error = got_path_mkdir(repo_path);
316 if (error &&
317 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
318 goto done;
320 error = apply_unveil(repo_path, 0);
321 if (error)
322 goto done;
324 error = got_repo_init(repo_path, head_name);
325 done:
326 free(repo_path);
327 return error;
330 static const struct got_error *
331 cmd_info(int argc, char *argv[])
333 const struct got_error *error = NULL;
334 char *repo_path = NULL;
335 struct got_repository *repo = NULL;
336 const struct got_gotconfig *gotconfig = NULL;
337 int ch, npackfiles, npackedobj, nobj;
338 off_t packsize, loose_size;
339 char scaled[FMT_SCALED_STRSIZE];
340 int *pack_fds = NULL;
342 #ifndef PROFILE
343 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
344 NULL) == -1)
345 err(1, "pledge");
346 #endif
348 while ((ch = getopt(argc, argv, "r:")) != -1) {
349 switch (ch) {
350 case 'r':
351 repo_path = realpath(optarg, NULL);
352 if (repo_path == NULL)
353 return got_error_from_errno2("realpath",
354 optarg);
355 got_path_strip_trailing_slashes(repo_path);
356 break;
357 default:
358 usage_info();
359 /* NOTREACHED */
363 argc -= optind;
364 argv += optind;
366 if (repo_path == NULL) {
367 error = get_repo_path(&repo_path);
368 if (error)
369 goto done;
371 error = got_repo_pack_fds_open(&pack_fds);
372 if (error != NULL)
373 goto done;
374 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
375 if (error)
376 goto done;
377 #ifndef PROFILE
378 /* Remove "cpath" promise. */
379 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
380 NULL) == -1)
381 err(1, "pledge");
382 #endif
383 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
384 if (error)
385 goto done;
387 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
389 gotconfig = got_repo_get_gotconfig(repo);
390 if (gotconfig) {
391 const struct got_remote_repo *remotes;
392 int i, nremotes;
393 if (got_gotconfig_get_author(gotconfig)) {
394 printf("default author: %s\n",
395 got_gotconfig_get_author(gotconfig));
397 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
398 for (i = 0; i < nremotes; i++) {
399 const char *fetch_url = remotes[i].fetch_url;
400 const char *send_url = remotes[i].send_url;
401 if (strcmp(fetch_url, send_url) == 0) {
402 printf("remote \"%s\": %s\n", remotes[i].name,
403 remotes[i].fetch_url);
404 } else {
405 printf("remote \"%s\" (fetch): %s\n",
406 remotes[i].name, remotes[i].fetch_url);
407 printf("remote \"%s\" (send): %s\n",
408 remotes[i].name, remotes[i].send_url);
413 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
414 &packsize, repo);
415 if (error)
416 goto done;
417 printf("pack files: %d\n", npackfiles);
418 if (npackfiles > 0) {
419 if (fmt_scaled(packsize, scaled) == -1) {
420 error = got_error_from_errno("fmt_scaled");
421 goto done;
423 printf("packed objects: %d\n", npackedobj);
424 printf("packed total size: %s\n", scaled);
427 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
428 if (error)
429 goto done;
430 printf("loose objects: %d\n", nobj);
431 if (nobj > 0) {
432 if (fmt_scaled(loose_size, scaled) == -1) {
433 error = got_error_from_errno("fmt_scaled");
434 goto done;
436 printf("loose total size: %s\n", scaled);
438 done:
439 if (repo)
440 got_repo_close(repo);
441 if (pack_fds) {
442 const struct got_error *pack_err =
443 got_repo_pack_fds_close(pack_fds);
444 if (error == NULL)
445 error = pack_err;
448 free(repo_path);
449 return error;
452 __dead static void
453 usage_pack(void)
455 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
456 "[-x reference] [reference ...]\n", getprogname());
457 exit(1);
460 struct got_pack_progress_arg {
461 char last_scaled_size[FMT_SCALED_STRSIZE];
462 int last_ncolored;
463 int last_nfound;
464 int last_ntrees;
465 int loading_done;
466 int last_ncommits;
467 int last_nobj_total;
468 int last_p_deltify;
469 int last_p_written;
470 int last_p_indexed;
471 int last_p_resolved;
472 int verbosity;
473 int printed_something;
476 static void
477 print_load_info(int print_colored, int print_found, int print_trees,
478 int ncolored, int nfound, int ntrees)
480 if (print_colored) {
481 printf("%d commit%s colored", ncolored,
482 ncolored == 1 ? "" : "s");
484 if (print_found) {
485 printf("%s%d object%s found",
486 ncolored > 0 ? "; " : "",
487 nfound, nfound == 1 ? "" : "s");
489 if (print_trees) {
490 printf("; %d tree%s scanned", ntrees,
491 ntrees == 1 ? "" : "s");
495 static const struct got_error *
496 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
497 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
498 int nobj_written)
500 struct got_pack_progress_arg *a = arg;
501 char scaled_size[FMT_SCALED_STRSIZE];
502 int p_deltify, p_written;
503 int print_colored = 0, print_found = 0, print_trees = 0;
504 int print_searching = 0, print_total = 0;
505 int print_deltify = 0, print_written = 0;
507 if (a->verbosity < 0)
508 return NULL;
510 if (a->last_ncolored != ncolored) {
511 print_colored = 1;
512 a->last_ncolored = ncolored;
515 if (a->last_nfound != nfound) {
516 print_colored = 1;
517 print_found = 1;
518 a->last_nfound = nfound;
521 if (a->last_ntrees != ntrees) {
522 print_colored = 1;
523 print_found = 1;
524 print_trees = 1;
525 a->last_ntrees = ntrees;
528 if ((print_colored || print_found || print_trees) &&
529 !a->loading_done) {
530 printf("\r");
531 print_load_info(print_colored, print_found, print_trees,
532 ncolored, nfound, ntrees);
533 a->printed_something = 1;
534 fflush(stdout);
535 return NULL;
536 } else if (!a->loading_done) {
537 printf("\r");
538 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
539 printf("\n");
540 a->loading_done = 1;
543 if (fmt_scaled(packfile_size, scaled_size) == -1)
544 return got_error_from_errno("fmt_scaled");
546 if (a->last_ncommits != ncommits) {
547 print_searching = 1;
548 a->last_ncommits = ncommits;
551 if (a->last_nobj_total != nobj_total) {
552 print_searching = 1;
553 print_total = 1;
554 a->last_nobj_total = nobj_total;
557 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
558 strcmp(scaled_size, a->last_scaled_size)) != 0) {
559 if (strlcpy(a->last_scaled_size, scaled_size,
560 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
561 return got_error(GOT_ERR_NO_SPACE);
564 if (nobj_deltify > 0 || nobj_written > 0) {
565 if (nobj_deltify > 0) {
566 p_deltify = (nobj_deltify * 100) / nobj_total;
567 if (p_deltify != a->last_p_deltify) {
568 a->last_p_deltify = p_deltify;
569 print_searching = 1;
570 print_total = 1;
571 print_deltify = 1;
574 if (nobj_written > 0) {
575 p_written = (nobj_written * 100) / nobj_total;
576 if (p_written != a->last_p_written) {
577 a->last_p_written = p_written;
578 print_searching = 1;
579 print_total = 1;
580 print_deltify = 1;
581 print_written = 1;
586 if (print_searching || print_total || print_deltify || print_written)
587 printf("\r");
588 if (print_searching)
589 printf("packing %d reference%s", ncommits,
590 ncommits == 1 ? "" : "s");
591 if (print_total)
592 printf("; %d object%s", nobj_total,
593 nobj_total == 1 ? "" : "s");
594 if (print_deltify)
595 printf("; deltify: %d%%", p_deltify);
596 if (print_written)
597 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
598 scaled_size, p_written);
599 if (print_searching || print_total || print_deltify ||
600 print_written) {
601 a->printed_something = 1;
602 fflush(stdout);
604 return NULL;
607 static const struct got_error *
608 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
609 int nobj_indexed, int nobj_loose, int nobj_resolved)
611 struct got_pack_progress_arg *a = arg;
612 char scaled_size[FMT_SCALED_STRSIZE];
613 int p_indexed, p_resolved;
614 int print_size = 0, print_indexed = 0, print_resolved = 0;
616 if (a->verbosity < 0)
617 return NULL;
619 if (packfile_size > 0 || nobj_indexed > 0) {
620 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
621 (a->last_scaled_size[0] == '\0' ||
622 strcmp(scaled_size, a->last_scaled_size)) != 0) {
623 print_size = 1;
624 if (strlcpy(a->last_scaled_size, scaled_size,
625 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
626 return got_error(GOT_ERR_NO_SPACE);
628 if (nobj_indexed > 0) {
629 p_indexed = (nobj_indexed * 100) / nobj_total;
630 if (p_indexed != a->last_p_indexed) {
631 a->last_p_indexed = p_indexed;
632 print_indexed = 1;
633 print_size = 1;
636 if (nobj_resolved > 0) {
637 p_resolved = (nobj_resolved * 100) /
638 (nobj_total - nobj_loose);
639 if (p_resolved != a->last_p_resolved) {
640 a->last_p_resolved = p_resolved;
641 print_resolved = 1;
642 print_indexed = 1;
643 print_size = 1;
648 if (print_size || print_indexed || print_resolved)
649 printf("\r");
650 if (print_size)
651 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
652 if (print_indexed)
653 printf("; indexing %d%%", p_indexed);
654 if (print_resolved)
655 printf("; resolving deltas %d%%", p_resolved);
656 if (print_size || print_indexed || print_resolved)
657 fflush(stdout);
659 return NULL;
662 static const struct got_error *
663 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
664 const char *refname, struct got_repository *repo)
666 const struct got_error *err;
667 struct got_reference *ref;
669 *new = NULL;
671 err = got_ref_open(&ref, repo, refname, 0);
672 if (err) {
673 if (err->code != GOT_ERR_NOT_REF)
674 return err;
676 /* Treat argument as a reference prefix. */
677 err = got_ref_list(refs, repo, refname,
678 got_ref_cmp_by_name, NULL);
679 } else {
680 err = got_reflist_insert(new, refs, ref,
681 got_ref_cmp_by_name, NULL);
682 if (err || *new == NULL /* duplicate */)
683 got_ref_close(ref);
686 return err;
689 static const struct got_error *
690 cmd_pack(int argc, char *argv[])
692 const struct got_error *error = NULL;
693 char *repo_path = NULL;
694 struct got_repository *repo = NULL;
695 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
696 struct got_object_id *pack_hash = NULL;
697 char *id_str = NULL;
698 struct got_pack_progress_arg ppa;
699 FILE *packfile = NULL;
700 struct got_pathlist_head exclude_args;
701 struct got_pathlist_entry *pe;
702 struct got_reflist_head exclude_refs;
703 struct got_reflist_head include_refs;
704 struct got_reflist_entry *re, *new;
705 int *pack_fds = NULL;
707 TAILQ_INIT(&exclude_args);
708 TAILQ_INIT(&exclude_refs);
709 TAILQ_INIT(&include_refs);
711 #ifndef PROFILE
712 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
713 NULL) == -1)
714 err(1, "pledge");
715 #endif
717 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
718 switch (ch) {
719 case 'a':
720 loose_obj_only = 0;
721 break;
722 case 'D':
723 force_refdelta = 1;
724 break;
725 case 'q':
726 verbosity = -1;
727 break;
728 case 'r':
729 repo_path = realpath(optarg, NULL);
730 if (repo_path == NULL)
731 return got_error_from_errno2("realpath",
732 optarg);
733 got_path_strip_trailing_slashes(repo_path);
734 break;
735 case 'x':
736 got_path_strip_trailing_slashes(optarg);
737 error = got_pathlist_append(&exclude_args,
738 optarg, NULL);
739 if (error)
740 return error;
741 break;
742 default:
743 usage_pack();
744 /* NOTREACHED */
748 argc -= optind;
749 argv += optind;
751 if (repo_path == NULL) {
752 error = get_repo_path(&repo_path);
753 if (error)
754 goto done;
756 error = got_repo_pack_fds_open(&pack_fds);
757 if (error != NULL)
758 goto done;
759 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
760 if (error)
761 goto done;
763 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
764 if (error)
765 goto done;
767 TAILQ_FOREACH(pe, &exclude_args, entry) {
768 const char *refname = pe->path;
769 error = add_ref(&new, &exclude_refs, refname, repo);
770 if (error)
771 goto done;
774 if (argc == 0) {
775 error = got_ref_list(&include_refs, repo, "",
776 got_ref_cmp_by_name, NULL);
777 if (error)
778 goto done;
779 } else {
780 for (i = 0; i < argc; i++) {
781 const char *refname;
782 got_path_strip_trailing_slashes(argv[i]);
783 refname = argv[i];
784 error = add_ref(&new, &include_refs, refname, repo);
785 if (error)
786 goto done;
790 /* Ignore references in the refs/got/ namespace. */
791 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
792 const char *refname = got_ref_get_name(re->ref);
793 if (strncmp("refs/got/", refname, 9) != 0)
794 continue;
795 TAILQ_REMOVE(&include_refs, re, entry);
796 got_ref_close(re->ref);
797 free(re);
800 memset(&ppa, 0, sizeof(ppa));
801 ppa.last_scaled_size[0] = '\0';
802 ppa.last_p_indexed = -1;
803 ppa.last_p_resolved = -1;
804 ppa.verbosity = verbosity;
806 error = got_repo_pack_objects(&packfile, &pack_hash,
807 &include_refs, &exclude_refs, repo, loose_obj_only,
808 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
809 if (error) {
810 if (ppa.printed_something)
811 printf("\n");
812 goto done;
815 error = got_object_id_str(&id_str, pack_hash);
816 if (error)
817 goto done;
818 if (verbosity >= 0)
819 printf("\nWrote %s.pack\n", id_str);
821 error = got_repo_index_pack(packfile, pack_hash, repo,
822 pack_index_progress, &ppa, check_cancelled, NULL);
823 if (error)
824 goto done;
825 if (verbosity >= 0)
826 printf("\nIndexed %s.pack\n", id_str);
827 done:
828 if (repo)
829 got_repo_close(repo);
830 if (pack_fds) {
831 const struct got_error *pack_err =
832 got_repo_pack_fds_close(pack_fds);
833 if (error == NULL)
834 error = pack_err;
836 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
837 got_ref_list_free(&exclude_refs);
838 got_ref_list_free(&include_refs);
839 free(id_str);
840 free(pack_hash);
841 free(repo_path);
842 return error;
845 __dead static void
846 usage_indexpack(void)
848 fprintf(stderr, "usage: %s indexpack packfile-path\n",
849 getprogname());
850 exit(1);
853 static const struct got_error *
854 cmd_indexpack(int argc, char *argv[])
856 const struct got_error *error = NULL;
857 struct got_repository *repo = NULL;
858 int ch;
859 struct got_object_id *pack_hash = NULL;
860 char *packfile_path = NULL;
861 char *id_str = NULL;
862 struct got_pack_progress_arg ppa;
863 FILE *packfile = NULL;
864 int *pack_fds = NULL;
866 while ((ch = getopt(argc, argv, "")) != -1) {
867 switch (ch) {
868 default:
869 usage_indexpack();
870 /* NOTREACHED */
874 argc -= optind;
875 argv += optind;
877 if (argc != 1)
878 usage_indexpack();
880 packfile_path = realpath(argv[0], NULL);
881 if (packfile_path == NULL)
882 return got_error_from_errno2("realpath", argv[0]);
884 #ifndef PROFILE
885 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
886 NULL) == -1)
887 err(1, "pledge");
888 #endif
890 error = got_repo_pack_fds_open(&pack_fds);
891 if (error != NULL)
892 goto done;
893 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
894 if (error)
895 goto done;
897 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
898 if (error)
899 goto done;
901 memset(&ppa, 0, sizeof(ppa));
902 ppa.last_scaled_size[0] = '\0';
903 ppa.last_p_indexed = -1;
904 ppa.last_p_resolved = -1;
906 error = got_repo_find_pack(&packfile, &pack_hash, repo,
907 packfile_path);
908 if (error)
909 goto done;
911 error = got_object_id_str(&id_str, pack_hash);
912 if (error)
913 goto done;
915 error = got_repo_index_pack(packfile, pack_hash, repo,
916 pack_index_progress, &ppa, check_cancelled, NULL);
917 if (error)
918 goto done;
919 printf("\nIndexed %s.pack\n", id_str);
920 done:
921 if (repo)
922 got_repo_close(repo);
923 if (pack_fds) {
924 const struct got_error *pack_err =
925 got_repo_pack_fds_close(pack_fds);
926 if (error == NULL)
927 error = pack_err;
929 free(id_str);
930 free(pack_hash);
931 return error;
934 __dead static void
935 usage_listpack(void)
937 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
938 getprogname());
939 exit(1);
942 struct gotadmin_list_pack_cb_args {
943 int nblobs;
944 int ntrees;
945 int ncommits;
946 int ntags;
947 int noffdeltas;
948 int nrefdeltas;
949 int human_readable;
952 static const struct got_error *
953 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
954 off_t size, off_t base_offset, struct got_object_id *base_id)
956 const struct got_error *err;
957 struct gotadmin_list_pack_cb_args *a = arg;
958 char *id_str, *delta_str = NULL, *base_id_str = NULL;
959 const char *type_str;
961 err = got_object_id_str(&id_str, id);
962 if (err)
963 return err;
965 switch (type) {
966 case GOT_OBJ_TYPE_BLOB:
967 type_str = GOT_OBJ_LABEL_BLOB;
968 a->nblobs++;
969 break;
970 case GOT_OBJ_TYPE_TREE:
971 type_str = GOT_OBJ_LABEL_TREE;
972 a->ntrees++;
973 break;
974 case GOT_OBJ_TYPE_COMMIT:
975 type_str = GOT_OBJ_LABEL_COMMIT;
976 a->ncommits++;
977 break;
978 case GOT_OBJ_TYPE_TAG:
979 type_str = GOT_OBJ_LABEL_TAG;
980 a->ntags++;
981 break;
982 case GOT_OBJ_TYPE_OFFSET_DELTA:
983 type_str = "offset-delta";
984 if (asprintf(&delta_str, " base-offset %lld",
985 (long long)base_offset) == -1) {
986 err = got_error_from_errno("asprintf");
987 goto done;
989 a->noffdeltas++;
990 break;
991 case GOT_OBJ_TYPE_REF_DELTA:
992 type_str = "ref-delta";
993 err = got_object_id_str(&base_id_str, base_id);
994 if (err)
995 goto done;
996 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
997 err = got_error_from_errno("asprintf");
998 goto done;
1000 a->nrefdeltas++;
1001 break;
1002 default:
1003 err = got_error(GOT_ERR_OBJ_TYPE);
1004 goto done;
1006 if (a->human_readable) {
1007 char scaled[FMT_SCALED_STRSIZE];
1008 char *s;;
1009 if (fmt_scaled(size, scaled) == -1) {
1010 err = got_error_from_errno("fmt_scaled");
1011 goto done;
1013 s = scaled;
1014 while (isspace((unsigned char)*s))
1015 s++;
1016 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1017 (long long)offset, s, delta_str ? delta_str : "");
1018 } else {
1019 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1020 (long long)offset, (long long)size,
1021 delta_str ? delta_str : "");
1023 done:
1024 free(id_str);
1025 free(base_id_str);
1026 free(delta_str);
1027 return err;
1030 static const struct got_error *
1031 cmd_listpack(int argc, char *argv[])
1033 const struct got_error *error = NULL;
1034 struct got_repository *repo = NULL;
1035 int ch;
1036 struct got_object_id *pack_hash = NULL;
1037 char *packfile_path = NULL;
1038 char *id_str = NULL;
1039 struct gotadmin_list_pack_cb_args lpa;
1040 FILE *packfile = NULL;
1041 int show_stats = 0, human_readable = 0;
1042 int *pack_fds = NULL;
1044 #ifndef PROFILE
1045 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1046 NULL) == -1)
1047 err(1, "pledge");
1048 #endif
1050 while ((ch = getopt(argc, argv, "hs")) != -1) {
1051 switch (ch) {
1052 case 'h':
1053 human_readable = 1;
1054 break;
1055 case 's':
1056 show_stats = 1;
1057 break;
1058 default:
1059 usage_listpack();
1060 /* NOTREACHED */
1064 argc -= optind;
1065 argv += optind;
1067 if (argc != 1)
1068 usage_listpack();
1069 packfile_path = realpath(argv[0], NULL);
1070 if (packfile_path == NULL)
1071 return got_error_from_errno2("realpath", argv[0]);
1073 error = got_repo_pack_fds_open(&pack_fds);
1074 if (error != NULL)
1075 goto done;
1076 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1077 if (error)
1078 goto done;
1079 #ifndef PROFILE
1080 /* Remove "cpath" promise. */
1081 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1082 NULL) == -1)
1083 err(1, "pledge");
1084 #endif
1085 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1086 if (error)
1087 goto done;
1089 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1090 packfile_path);
1091 if (error)
1092 goto done;
1093 error = got_object_id_str(&id_str, pack_hash);
1094 if (error)
1095 goto done;
1097 memset(&lpa, 0, sizeof(lpa));
1098 lpa.human_readable = human_readable;
1099 error = got_repo_list_pack(packfile, pack_hash, repo,
1100 list_pack_cb, &lpa, check_cancelled, NULL);
1101 if (error)
1102 goto done;
1103 if (show_stats) {
1104 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1105 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1106 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1107 lpa.noffdeltas + lpa.nrefdeltas,
1108 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1109 lpa.noffdeltas, lpa.nrefdeltas);
1111 done:
1112 if (repo)
1113 got_repo_close(repo);
1114 if (pack_fds) {
1115 const struct got_error *pack_err =
1116 got_repo_pack_fds_close(pack_fds);
1117 if (error == NULL)
1118 error = pack_err;
1120 free(id_str);
1121 free(pack_hash);
1122 free(packfile_path);
1123 return error;
1126 __dead static void
1127 usage_cleanup(void)
1129 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1130 getprogname());
1131 exit(1);
1134 struct got_cleanup_progress_arg {
1135 int last_nloose;
1136 int last_ncommits;
1137 int last_npurged;
1138 int verbosity;
1139 int printed_something;
1140 int dry_run;
1143 static const struct got_error *
1144 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1146 struct got_cleanup_progress_arg *a = arg;
1147 int print_loose = 0, print_commits = 0, print_purged = 0;
1149 if (a->last_nloose != nloose) {
1150 print_loose = 1;
1151 a->last_nloose = nloose;
1153 if (a->last_ncommits != ncommits) {
1154 print_loose = 1;
1155 print_commits = 1;
1156 a->last_ncommits = ncommits;
1158 if (a->last_npurged != npurged) {
1159 print_loose = 1;
1160 print_commits = 1;
1161 print_purged = 1;
1162 a->last_npurged = npurged;
1165 if (a->verbosity < 0)
1166 return NULL;
1168 if (print_loose || print_commits || print_purged)
1169 printf("\r");
1170 if (print_loose)
1171 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1172 if (print_commits)
1173 printf("; %d commit%s scanned", ncommits,
1174 ncommits == 1 ? "" : "s");
1175 if (print_purged) {
1176 if (a->dry_run) {
1177 printf("; %d object%s could be purged", npurged,
1178 npurged == 1 ? "" : "s");
1179 } else {
1180 printf("; %d object%s purged", npurged,
1181 npurged == 1 ? "" : "s");
1184 if (print_loose || print_commits || print_purged) {
1185 a->printed_something = 1;
1186 fflush(stdout);
1188 return NULL;
1191 struct got_lonely_packidx_progress_arg {
1192 int verbosity;
1193 int printed_something;
1194 int dry_run;
1197 static const struct got_error *
1198 lonely_packidx_progress(void *arg, const char *path)
1200 struct got_lonely_packidx_progress_arg *a = arg;
1202 if (a->verbosity < 0)
1203 return NULL;
1205 if (a->dry_run)
1206 printf("%s could be removed\n", path);
1207 else
1208 printf("%s removed\n", path);
1210 a->printed_something = 1;
1211 return NULL;
1214 static const struct got_error *
1215 cmd_cleanup(int argc, char *argv[])
1217 const struct got_error *error = NULL;
1218 char *repo_path = NULL;
1219 struct got_repository *repo = NULL;
1220 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1221 int remove_lonely_packidx = 0, ignore_mtime = 0;
1222 struct got_cleanup_progress_arg cpa;
1223 struct got_lonely_packidx_progress_arg lpa;
1224 off_t size_before, size_after;
1225 char scaled_before[FMT_SCALED_STRSIZE];
1226 char scaled_after[FMT_SCALED_STRSIZE];
1227 char scaled_diff[FMT_SCALED_STRSIZE];
1228 int *pack_fds = NULL;
1230 #ifndef PROFILE
1231 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1232 NULL) == -1)
1233 err(1, "pledge");
1234 #endif
1236 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1237 switch (ch) {
1238 case 'a':
1239 ignore_mtime = 1;
1240 break;
1241 case 'n':
1242 dry_run = 1;
1243 break;
1244 case 'p':
1245 remove_lonely_packidx = 1;
1246 break;
1247 case 'q':
1248 verbosity = -1;
1249 break;
1250 case 'r':
1251 repo_path = realpath(optarg, NULL);
1252 if (repo_path == NULL)
1253 return got_error_from_errno2("realpath",
1254 optarg);
1255 got_path_strip_trailing_slashes(repo_path);
1256 break;
1257 default:
1258 usage_cleanup();
1259 /* NOTREACHED */
1263 argc -= optind;
1264 argv += optind;
1266 if (repo_path == NULL) {
1267 error = get_repo_path(&repo_path);
1268 if (error)
1269 goto done;
1271 error = got_repo_pack_fds_open(&pack_fds);
1272 if (error != NULL)
1273 goto done;
1274 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1275 if (error)
1276 goto done;
1278 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1279 if (error)
1280 goto done;
1282 if (got_repo_has_extension(repo, "preciousObjects")) {
1283 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1284 "the preciousObjects Git extension is enabled; "
1285 "this implies that objects must not be deleted");
1286 goto done;
1289 if (remove_lonely_packidx) {
1290 memset(&lpa, 0, sizeof(lpa));
1291 lpa.dry_run = dry_run;
1292 lpa.verbosity = verbosity;
1293 error = got_repo_remove_lonely_packidx(repo, dry_run,
1294 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1295 goto done;
1298 memset(&cpa, 0, sizeof(cpa));
1299 cpa.last_ncommits = -1;
1300 cpa.last_npurged = -1;
1301 cpa.dry_run = dry_run;
1302 cpa.verbosity = verbosity;
1303 error = got_repo_purge_unreferenced_loose_objects(repo,
1304 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1305 cleanup_progress, &cpa, check_cancelled, NULL);
1306 if (cpa.printed_something)
1307 printf("\n");
1308 if (error)
1309 goto done;
1310 if (cpa.printed_something) {
1311 if (fmt_scaled(size_before, scaled_before) == -1) {
1312 error = got_error_from_errno("fmt_scaled");
1313 goto done;
1315 if (fmt_scaled(size_after, scaled_after) == -1) {
1316 error = got_error_from_errno("fmt_scaled");
1317 goto done;
1319 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1320 error = got_error_from_errno("fmt_scaled");
1321 goto done;
1323 printf("loose total size before: %s\n", scaled_before);
1324 printf("loose total size after: %s\n", scaled_after);
1325 if (dry_run) {
1326 printf("disk space which would be freed: %s\n",
1327 scaled_diff);
1328 } else
1329 printf("disk space freed: %s\n", scaled_diff);
1330 printf("loose objects also found in pack files: %d\n", npacked);
1332 done:
1333 if (repo)
1334 got_repo_close(repo);
1335 if (pack_fds) {
1336 const struct got_error *pack_err =
1337 got_repo_pack_fds_close(pack_fds);
1338 if (error == NULL)
1339 error = pack_err;
1341 free(repo_path);
1342 return error;