Blob


1 /*
2 * Copyright (c) 2021 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/queue.h>
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <getopt.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <inttypes.h>
26 #include <sha1.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <util.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"
46 #ifndef nitems
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 #endif
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
53 static void
54 catch_sigint(int signo)
55 {
56 sigint_received = 1;
57 }
59 static void
60 catch_sigpipe(int signo)
61 {
62 sigpipe_received = 1;
63 }
65 static const struct got_error *
66 check_cancelled(void *arg)
67 {
68 if (sigint_received || sigpipe_received)
69 return got_error(GOT_ERR_CANCELLED);
70 return NULL;
71 }
73 struct gotadmin_cmd {
74 const char *cmd_name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
78 };
80 __dead static void usage(int, int);
81 __dead static void usage_info(void);
82 __dead static void usage_pack(void);
83 __dead static void usage_indexpack(void);
84 __dead static void usage_listpack(void);
86 static const struct got_error* cmd_info(int, char *[]);
87 static const struct got_error* cmd_pack(int, char *[]);
88 static const struct got_error* cmd_indexpack(int, char *[]);
89 static const struct got_error* cmd_listpack(int, char *[]);
91 static struct gotadmin_cmd gotadmin_commands[] = {
92 { "info", cmd_info, usage_info, "" },
93 { "pack", cmd_pack, usage_pack, "" },
94 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
95 { "listpack", cmd_listpack, usage_listpack, "ls" },
96 };
98 static void
99 list_commands(FILE *fp)
101 size_t i;
103 fprintf(fp, "commands:");
104 for (i = 0; i < nitems(gotadmin_commands); i++) {
105 struct gotadmin_cmd *cmd = &gotadmin_commands[i];
106 fprintf(fp, " %s", cmd->cmd_name);
108 fputc('\n', fp);
111 int
112 main(int argc, char *argv[])
114 struct gotadmin_cmd *cmd;
115 size_t i;
116 int ch;
117 int hflag = 0, Vflag = 0;
118 static struct option longopts[] = {
119 { "version", no_argument, NULL, 'V' },
120 { NULL, 0, NULL, 0 }
121 };
123 setlocale(LC_CTYPE, "");
125 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
126 switch (ch) {
127 case 'h':
128 hflag = 1;
129 break;
130 case 'V':
131 Vflag = 1;
132 break;
133 default:
134 usage(hflag, 1);
135 /* NOTREACHED */
139 argc -= optind;
140 argv += optind;
141 optind = 1;
142 optreset = 1;
144 if (Vflag) {
145 got_version_print_str();
146 return 0;
149 if (argc <= 0)
150 usage(hflag, hflag ? 0 : 1);
152 signal(SIGINT, catch_sigint);
153 signal(SIGPIPE, catch_sigpipe);
155 for (i = 0; i < nitems(gotadmin_commands); i++) {
156 const struct got_error *error;
158 cmd = &gotadmin_commands[i];
160 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
161 strcmp(cmd->cmd_alias, argv[0]) != 0)
162 continue;
164 if (hflag)
165 gotadmin_commands[i].cmd_usage();
167 error = gotadmin_commands[i].cmd_main(argc, argv);
168 if (error && error->code != GOT_ERR_CANCELLED &&
169 error->code != GOT_ERR_PRIVSEP_EXIT &&
170 !(sigpipe_received &&
171 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
172 !(sigint_received &&
173 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
174 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
175 return 1;
178 return 0;
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 list_commands(stderr);
183 return 1;
186 __dead static void
187 usage(int hflag, int status)
189 FILE *fp = (status == 0) ? stdout : stderr;
191 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
192 getprogname());
193 if (hflag)
194 list_commands(fp);
195 exit(status);
198 static const struct got_error *
199 apply_unveil(const char *repo_path, int repo_read_only)
201 const struct got_error *err;
203 #ifdef PROFILE
204 if (unveil("gmon.out", "rwc") != 0)
205 return got_error_from_errno2("unveil", "gmon.out");
206 #endif
207 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
208 return got_error_from_errno2("unveil", repo_path);
210 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
211 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
213 err = got_privsep_unveil_exec_helpers();
214 if (err != NULL)
215 return err;
217 if (unveil(NULL, NULL) != 0)
218 return got_error_from_errno("unveil");
220 return NULL;
223 __dead static void
224 usage_info(void)
226 fprintf(stderr, "usage: %s info [-r repository-path]\n",
227 getprogname());
228 exit(1);
231 static const struct got_error *
232 cmd_info(int argc, char *argv[])
234 const struct got_error *error = NULL;
235 char *cwd = NULL, *repo_path = NULL;
236 struct got_repository *repo = NULL;
237 const struct got_gotconfig *gotconfig = NULL;
238 int ch, npackfiles, npackedobj, nobj;
239 off_t packsize, loose_size;
240 char scaled[FMT_SCALED_STRSIZE];
242 while ((ch = getopt(argc, argv, "r:")) != -1) {
243 switch (ch) {
244 case 'r':
245 repo_path = realpath(optarg, NULL);
246 if (repo_path == NULL)
247 return got_error_from_errno2("realpath",
248 optarg);
249 got_path_strip_trailing_slashes(repo_path);
250 break;
251 default:
252 usage_info();
253 /* NOTREACHED */
257 argc -= optind;
258 argv += optind;
260 #ifndef PROFILE
261 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
262 NULL) == -1)
263 err(1, "pledge");
264 #endif
265 cwd = getcwd(NULL, 0);
266 if (cwd == NULL) {
267 error = got_error_from_errno("getcwd");
268 goto done;
271 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
272 if (error)
273 goto done;
275 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
276 if (error)
277 goto done;
279 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
281 gotconfig = got_repo_get_gotconfig(repo);
282 if (gotconfig) {
283 const struct got_remote_repo *remotes;
284 int i, nremotes;
285 if (got_gotconfig_get_author(gotconfig)) {
286 printf("default author: %s\n",
287 got_gotconfig_get_author(gotconfig));
289 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
290 for (i = 0; i < nremotes; i++) {
291 printf("remote \"%s\": %s\n", remotes[i].name,
292 remotes[i].url);
296 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
297 &packsize, repo);
298 if (error)
299 goto done;
300 printf("pack files: %d\n", npackfiles);
301 if (npackfiles > 0) {
302 if (fmt_scaled(packsize, scaled) == -1) {
303 error = got_error_from_errno("fmt_scaled");
304 goto done;
306 printf("packed objects: %d\n", npackedobj);
307 printf("packed total size: %s\n", scaled);
310 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
311 if (error)
312 goto done;
313 printf("loose objects: %d\n", nobj);
314 if (nobj > 0) {
315 if (fmt_scaled(loose_size, scaled) == -1) {
316 error = got_error_from_errno("fmt_scaled");
317 goto done;
319 printf("loose total size: %s\n", scaled);
321 done:
322 if (repo)
323 got_repo_close(repo);
324 free(cwd);
325 return error;
328 __dead static void
329 usage_pack(void)
331 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
332 "[-x reference] [reference ...]\n",
333 getprogname());
334 exit(1);
337 struct got_pack_progress_arg {
338 char last_scaled_size[FMT_SCALED_STRSIZE];
339 int last_ncommits;
340 int last_nobj_total;
341 int last_p_deltify;
342 int last_p_written;
343 int last_p_indexed;
344 int last_p_resolved;
345 int verbosity;
346 int printed_something;
347 };
349 static const struct got_error *
350 pack_progress(void *arg, off_t packfile_size, int ncommits,
351 int nobj_total, int nobj_deltify, int nobj_written)
353 struct got_pack_progress_arg *a = arg;
354 char scaled_size[FMT_SCALED_STRSIZE];
355 int p_deltify, p_written;
356 int print_searching = 0, print_total = 0;
357 int print_deltify = 0, print_written = 0;
359 if (a->verbosity < 0)
360 return NULL;
362 if (fmt_scaled(packfile_size, scaled_size) == -1)
363 return got_error_from_errno("fmt_scaled");
365 if (a->last_ncommits != ncommits) {
366 print_searching = 1;
367 a->last_ncommits = ncommits;
370 if (a->last_nobj_total != nobj_total) {
371 print_searching = 1;
372 print_total = 1;
373 a->last_nobj_total = nobj_total;
376 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
377 strcmp(scaled_size, a->last_scaled_size)) != 0) {
378 if (strlcpy(a->last_scaled_size, scaled_size,
379 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
380 return got_error(GOT_ERR_NO_SPACE);
383 if (nobj_deltify > 0 || nobj_written > 0) {
384 if (nobj_deltify > 0) {
385 p_deltify = (nobj_deltify * 100) / nobj_total;
386 if (p_deltify != a->last_p_deltify) {
387 a->last_p_deltify = p_deltify;
388 print_searching = 1;
389 print_total = 1;
390 print_deltify = 1;
393 if (nobj_written > 0) {
394 p_written = (nobj_written * 100) / nobj_total;
395 if (p_written != a->last_p_written) {
396 a->last_p_written = p_written;
397 print_searching = 1;
398 print_total = 1;
399 print_deltify = 1;
400 print_written = 1;
405 if (print_searching || print_total || print_deltify || print_written)
406 printf("\r");
407 if (print_searching)
408 printf("packing %d reference%s", ncommits,
409 ncommits == 1 ? "" : "s");
410 if (print_total)
411 printf("; %d object%s", nobj_total,
412 nobj_total == 1 ? "" : "s");
413 if (print_deltify)
414 printf("; deltify: %d%%", p_deltify);
415 if (print_written)
416 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
417 scaled_size, p_written);
418 if (print_searching || print_total || print_deltify ||
419 print_written) {
420 a->printed_something = 1;
421 fflush(stdout);
423 return NULL;
426 static const struct got_error *
427 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
428 int nobj_indexed, int nobj_loose, int nobj_resolved)
430 struct got_pack_progress_arg *a = arg;
431 char scaled_size[FMT_SCALED_STRSIZE];
432 int p_indexed, p_resolved;
433 int print_size = 0, print_indexed = 0, print_resolved = 0;
435 if (a->verbosity < 0)
436 return NULL;
438 if (packfile_size > 0 || nobj_indexed > 0) {
439 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
440 (a->last_scaled_size[0] == '\0' ||
441 strcmp(scaled_size, a->last_scaled_size)) != 0) {
442 print_size = 1;
443 if (strlcpy(a->last_scaled_size, scaled_size,
444 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
445 return got_error(GOT_ERR_NO_SPACE);
447 if (nobj_indexed > 0) {
448 p_indexed = (nobj_indexed * 100) / nobj_total;
449 if (p_indexed != a->last_p_indexed) {
450 a->last_p_indexed = p_indexed;
451 print_indexed = 1;
452 print_size = 1;
455 if (nobj_resolved > 0) {
456 p_resolved = (nobj_resolved * 100) /
457 (nobj_total - nobj_loose);
458 if (p_resolved != a->last_p_resolved) {
459 a->last_p_resolved = p_resolved;
460 print_resolved = 1;
461 print_indexed = 1;
462 print_size = 1;
467 if (print_size || print_indexed || print_resolved)
468 printf("\r");
469 if (print_size)
470 printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
471 if (print_indexed)
472 printf("; indexing %d%%", p_indexed);
473 if (print_resolved)
474 printf("; resolving deltas %d%%", p_resolved);
475 if (print_size || print_indexed || print_resolved)
476 fflush(stdout);
478 return NULL;
481 static const struct got_error *
482 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
483 const char *refname, struct got_repository *repo)
485 const struct got_error *err;
486 struct got_reference *ref;
488 *new = NULL;
490 err = got_ref_open(&ref, repo, refname, 0);
491 if (err) {
492 if (err->code != GOT_ERR_NOT_REF)
493 return err;
495 /* Treat argument as a reference prefix. */
496 err = got_ref_list(refs, repo, refname,
497 got_ref_cmp_by_name, NULL);
498 } else {
499 err = got_reflist_insert(new, refs, ref, repo,
500 got_ref_cmp_by_name, NULL);
501 if (err || *new == NULL /* duplicate */)
502 got_ref_close(ref);
505 return err;
508 static const struct got_error *
509 cmd_pack(int argc, char *argv[])
511 const struct got_error *error = NULL;
512 char *cwd = NULL, *repo_path = NULL;
513 struct got_repository *repo = NULL;
514 int ch, i, loose_obj_only = 1;
515 struct got_object_id *pack_hash = NULL;
516 char *id_str = NULL;
517 struct got_pack_progress_arg ppa;
518 FILE *packfile = NULL;
519 struct got_pathlist_head exclude_args;
520 struct got_pathlist_entry *pe;
521 struct got_reflist_head exclude_refs;
522 struct got_reflist_head include_refs;
523 struct got_reflist_entry *re, *new;
525 TAILQ_INIT(&exclude_args);
526 TAILQ_INIT(&exclude_refs);
527 TAILQ_INIT(&include_refs);
529 while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
530 switch (ch) {
531 case 'a':
532 loose_obj_only = 0;
533 break;
534 case 'r':
535 repo_path = realpath(optarg, NULL);
536 if (repo_path == NULL)
537 return got_error_from_errno2("realpath",
538 optarg);
539 got_path_strip_trailing_slashes(repo_path);
540 break;
541 case 'x':
542 got_path_strip_trailing_slashes(optarg);
543 error = got_pathlist_append(&exclude_args,
544 optarg, NULL);
545 if (error)
546 return error;
547 break;
548 default:
549 usage_pack();
550 /* NOTREACHED */
554 argc -= optind;
555 argv += optind;
557 #ifndef PROFILE
558 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
559 NULL) == -1)
560 err(1, "pledge");
561 #endif
562 cwd = getcwd(NULL, 0);
563 if (cwd == NULL) {
564 error = got_error_from_errno("getcwd");
565 goto done;
568 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
569 if (error)
570 goto done;
572 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
573 if (error)
574 goto done;
576 TAILQ_FOREACH(pe, &exclude_args, entry) {
577 const char *refname = pe->path;
578 error = add_ref(&new, &exclude_refs, refname, repo);
579 if (error)
580 goto done;
584 if (argc == 0) {
585 error = got_ref_list(&include_refs, repo, "",
586 got_ref_cmp_by_name, NULL);
587 if (error)
588 goto done;
589 } else {
590 for (i = 0; i < argc; i++) {
591 const char *refname;
592 got_path_strip_trailing_slashes(argv[i]);
593 refname = argv[i];
594 error = add_ref(&new, &include_refs, refname, repo);
595 if (error)
596 goto done;
600 /* Ignore references in the refs/got/ namespace. */
601 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
602 const char *refname = got_ref_get_name(re->ref);
603 if (strncmp("refs/got/", refname, 9) != 0)
604 continue;
605 TAILQ_REMOVE(&include_refs, re, entry);
606 got_ref_close(re->ref);
607 free(re);
610 memset(&ppa, 0, sizeof(ppa));
611 ppa.last_scaled_size[0] = '\0';
612 ppa.last_p_indexed = -1;
613 ppa.last_p_resolved = -1;
615 error = got_repo_pack_objects(&packfile, &pack_hash,
616 &include_refs, &exclude_refs, repo, loose_obj_only,
617 pack_progress, &ppa, check_cancelled, NULL);
618 if (error) {
619 if (ppa.printed_something)
620 printf("\n");
621 goto done;
624 error = got_object_id_str(&id_str, pack_hash);
625 if (error)
626 goto done;
627 printf("\nWrote %s.pack\n", id_str);
629 error = got_repo_index_pack(packfile, pack_hash, repo,
630 pack_index_progress, &ppa, check_cancelled, NULL);
631 if (error)
632 goto done;
633 printf("\nIndexed %s.pack\n", id_str);
634 done:
635 got_pathlist_free(&exclude_args);
636 got_ref_list_free(&exclude_refs);
637 got_ref_list_free(&include_refs);
638 free(id_str);
639 free(pack_hash);
640 free(cwd);
641 return error;
644 __dead static void
645 usage_indexpack(void)
647 fprintf(stderr, "usage: %s indexpack packfile-path\n",
648 getprogname());
649 exit(1);
652 static const struct got_error *
653 cmd_indexpack(int argc, char *argv[])
655 const struct got_error *error = NULL;
656 struct got_repository *repo = NULL;
657 int ch;
658 struct got_object_id *pack_hash = NULL;
659 char *packfile_path = NULL;
660 char *id_str = NULL;
661 struct got_pack_progress_arg ppa;
662 FILE *packfile = NULL;
664 while ((ch = getopt(argc, argv, "")) != -1) {
665 switch (ch) {
666 default:
667 usage_indexpack();
668 /* NOTREACHED */
672 argc -= optind;
673 argv += optind;
675 if (argc != 1)
676 usage_indexpack();
678 packfile_path = realpath(argv[0], NULL);
679 if (packfile_path == NULL)
680 return got_error_from_errno2("realpath", argv[0]);
682 #ifndef PROFILE
683 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
684 NULL) == -1)
685 err(1, "pledge");
686 #endif
688 error = got_repo_open(&repo, packfile_path, NULL);
689 if (error)
690 goto done;
692 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
693 if (error)
694 goto done;
696 memset(&ppa, 0, sizeof(ppa));
697 ppa.last_scaled_size[0] = '\0';
698 ppa.last_p_indexed = -1;
699 ppa.last_p_resolved = -1;
701 error = got_repo_find_pack(&packfile, &pack_hash, repo,
702 packfile_path);
703 if (error)
704 goto done;
706 error = got_object_id_str(&id_str, pack_hash);
707 if (error)
708 goto done;
710 error = got_repo_index_pack(packfile, pack_hash, repo,
711 pack_index_progress, &ppa, check_cancelled, NULL);
712 if (error)
713 goto done;
714 printf("\nIndexed %s.pack\n", id_str);
715 done:
716 free(id_str);
717 free(pack_hash);
718 return error;
721 __dead static void
722 usage_listpack(void)
724 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
725 getprogname());
726 exit(1);
729 struct gotadmin_list_pack_cb_args {
730 int nblobs;
731 int ntrees;
732 int ncommits;
733 int ntags;
734 int noffdeltas;
735 int nrefdeltas;
736 int human_readable;
737 };
739 static const struct got_error *
740 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
741 off_t size, off_t base_offset, struct got_object_id *base_id)
743 const struct got_error *err;
744 struct gotadmin_list_pack_cb_args *a = arg;
745 char *id_str, *delta_str = NULL, *base_id_str = NULL;
746 const char *type_str;
748 err = got_object_id_str(&id_str, id);
749 if (err)
750 return err;
752 switch (type) {
753 case GOT_OBJ_TYPE_BLOB:
754 type_str = GOT_OBJ_LABEL_BLOB;
755 a->nblobs++;
756 break;
757 case GOT_OBJ_TYPE_TREE:
758 type_str = GOT_OBJ_LABEL_TREE;
759 a->ntrees++;
760 break;
761 case GOT_OBJ_TYPE_COMMIT:
762 type_str = GOT_OBJ_LABEL_COMMIT;
763 a->ncommits++;
764 break;
765 case GOT_OBJ_TYPE_TAG:
766 type_str = GOT_OBJ_LABEL_TAG;
767 a->ntags++;
768 break;
769 case GOT_OBJ_TYPE_OFFSET_DELTA:
770 type_str = "offset-delta";
771 if (asprintf(&delta_str, " base-offset %llu",
772 base_offset) == -1) {
773 err = got_error_from_errno("asprintf");
774 goto done;
776 a->noffdeltas++;
777 break;
778 case GOT_OBJ_TYPE_REF_DELTA:
779 type_str = "ref-delta";
780 err = got_object_id_str(&base_id_str, base_id);
781 if (err)
782 goto done;
783 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
784 err = got_error_from_errno("asprintf");
785 goto done;
787 a->nrefdeltas++;
788 break;
789 default:
790 err = got_error(GOT_ERR_OBJ_TYPE);
791 goto done;
793 if (a->human_readable) {
794 char scaled[FMT_SCALED_STRSIZE];
795 char *s;;
796 if (fmt_scaled(size, scaled) == -1) {
797 err = got_error_from_errno("fmt_scaled");
798 goto done;
800 s = scaled;
801 while (isspace((unsigned char)*s))
802 s++;
803 printf("%s %s at %llu size %s%s\n", id_str, type_str, offset,
804 s, delta_str ? delta_str : "");
805 } else {
806 printf("%s %s at %llu size %llu%s\n", id_str, type_str, offset,
807 size, delta_str ? delta_str : "");
809 done:
810 free(id_str);
811 free(base_id_str);
812 free(delta_str);
813 return err;
816 static const struct got_error *
817 cmd_listpack(int argc, char *argv[])
819 const struct got_error *error = NULL;
820 struct got_repository *repo = NULL;
821 int ch;
822 struct got_object_id *pack_hash = NULL;
823 char *packfile_path = NULL;
824 char *id_str = NULL;
825 struct gotadmin_list_pack_cb_args lpa;
826 FILE *packfile = NULL;
827 int show_stats = 0, human_readable = 0;
829 while ((ch = getopt(argc, argv, "hs")) != -1) {
830 switch (ch) {
831 case 'h':
832 human_readable = 1;
833 break;
834 case 's':
835 show_stats = 1;
836 break;
837 default:
838 usage_listpack();
839 /* NOTREACHED */
843 argc -= optind;
844 argv += optind;
846 if (argc != 1)
847 usage_listpack();
848 packfile_path = realpath(argv[0], NULL);
849 if (packfile_path == NULL)
850 return got_error_from_errno2("realpath", argv[0]);
852 #ifndef PROFILE
853 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
854 NULL) == -1)
855 err(1, "pledge");
856 #endif
857 error = got_repo_open(&repo, packfile_path, NULL);
858 if (error)
859 goto done;
861 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
862 if (error)
863 goto done;
865 error = got_repo_find_pack(&packfile, &pack_hash, repo,
866 packfile_path);
867 if (error)
868 goto done;
869 error = got_object_id_str(&id_str, pack_hash);
870 if (error)
871 goto done;
873 memset(&lpa, 0, sizeof(lpa));
874 lpa.human_readable = human_readable;
875 error = got_repo_list_pack(packfile, pack_hash, repo,
876 list_pack_cb, &lpa, check_cancelled, NULL);
877 if (error)
878 goto done;
879 if (show_stats) {
880 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
881 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
882 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
883 lpa.noffdeltas + lpa.nrefdeltas,
884 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
885 lpa.noffdeltas, lpa.nrefdeltas);
887 done:
888 free(id_str);
889 free(pack_hash);
890 free(packfile_path);
891 return error;