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 <sys/queue.h>
30 #include "got_version.h"
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_reference.h"
34 #include "got_repository.h"
35 #include "got_gotconfig.h"
37 #include "got_cancel.h"
38 #include "got_privsep.h"
39 #include "got_opentemp.h"
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 static volatile sig_atomic_t sigint_received;
46 static volatile sig_atomic_t sigpipe_received;
49 catch_sigint(int signo)
55 catch_sigpipe(int signo)
63 const struct got_error *(*cmd_main)(int, char *[]);
64 void (*cmd_usage)(void);
65 const char *cmd_alias;
68 __dead static void usage(int, int);
69 __dead static void usage_info(void);
71 static const struct got_error* cmd_info(int, char *[]);
73 static struct gotadmin_cmd gotadmin_commands[] = {
74 { "info", cmd_info, usage_info, "" },
78 list_commands(FILE *fp)
82 fprintf(fp, "commands:");
83 for (i = 0; i < nitems(gotadmin_commands); i++) {
84 struct gotadmin_cmd *cmd = &gotadmin_commands[i];
85 fprintf(fp, " %s", cmd->cmd_name);
91 main(int argc, char *argv[])
93 struct gotadmin_cmd *cmd;
96 int hflag = 0, Vflag = 0;
97 static struct option longopts[] = {
98 { "version", no_argument, NULL, 'V' },
102 setlocale(LC_CTYPE, "");
104 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
124 got_version_print_str();
129 usage(hflag, hflag ? 0 : 1);
131 signal(SIGINT, catch_sigint);
132 signal(SIGPIPE, catch_sigpipe);
134 for (i = 0; i < nitems(gotadmin_commands); i++) {
135 const struct got_error *error;
137 cmd = &gotadmin_commands[i];
139 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
140 strcmp(cmd->cmd_alias, argv[0]) != 0)
144 gotadmin_commands[i].cmd_usage();
146 error = gotadmin_commands[i].cmd_main(argc, argv);
147 if (error && error->code != GOT_ERR_CANCELLED &&
148 error->code != GOT_ERR_PRIVSEP_EXIT &&
149 !(sigpipe_received &&
150 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
152 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
153 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
160 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
161 list_commands(stderr);
166 usage(int hflag, int status)
168 FILE *fp = (status == 0) ? stdout : stderr;
170 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
177 static const struct got_error *
178 apply_unveil(const char *repo_path, int repo_read_only)
180 const struct got_error *err;
183 if (unveil("gmon.out", "rwc") != 0)
184 return got_error_from_errno2("unveil", "gmon.out");
186 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
187 return got_error_from_errno2("unveil", repo_path);
189 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
190 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
192 err = got_privsep_unveil_exec_helpers();
196 if (unveil(NULL, NULL) != 0)
197 return got_error_from_errno("unveil");
205 fprintf(stderr, "usage: %s info [-r repository-path]\n",
210 static const struct got_error *
211 cmd_info(int argc, char *argv[])
213 const struct got_error *error = NULL;
214 char *cwd = NULL, *repo_path = NULL;
215 struct got_repository *repo = NULL;
216 const struct got_gotconfig *gotconfig = NULL;
217 int ch, npackfiles, npackedobj, nobj;
218 off_t packsize, loose_size;
219 char scaled[FMT_SCALED_STRSIZE];
221 while ((ch = getopt(argc, argv, "r:")) != -1) {
224 repo_path = realpath(optarg, NULL);
225 if (repo_path == NULL)
226 return got_error_from_errno2("realpath",
228 got_path_strip_trailing_slashes(repo_path);
240 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
244 cwd = getcwd(NULL, 0);
246 error = got_error_from_errno("getcwd");
250 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
254 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
258 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
260 gotconfig = got_repo_get_gotconfig(repo);
262 const struct got_remote_repo *remotes;
264 if (got_gotconfig_get_author(gotconfig)) {
265 printf("default author: %s\n",
266 got_gotconfig_get_author(gotconfig));
268 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
269 for (i = 0; i < nremotes; i++) {
270 printf("remote \"%s\": %s\n", remotes[i].name,
275 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
279 printf("pack files: %d\n", npackfiles);
280 if (npackfiles > 0) {
281 if (fmt_scaled(packsize, scaled) == -1) {
282 error = got_error_from_errno("fmt_scaled");
285 printf("packed objects: %d\n", npackedobj);
286 printf("packed total size: %s\n", scaled);
289 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
292 printf("loose objects: %d\n", nobj);
294 if (fmt_scaled(loose_size, scaled) == -1) {
295 error = got_error_from_errno("fmt_scaled");
298 printf("loose total size: %s\n", scaled);
302 got_repo_close(repo);