2 20662ea0 2021-04-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 20662ea0 2021-04-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 20662ea0 2021-04-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 20662ea0 2021-04-10 stsp * copyright notice and this permission notice appear in all copies.
8 20662ea0 2021-04-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 20662ea0 2021-04-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 20662ea0 2021-04-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 20662ea0 2021-04-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 20662ea0 2021-04-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 20662ea0 2021-04-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 20662ea0 2021-04-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 20662ea0 2021-04-10 stsp #include <sys/queue.h>
19 20662ea0 2021-04-10 stsp #include <getopt.h>
20 20662ea0 2021-04-10 stsp #include <err.h>
21 20662ea0 2021-04-10 stsp #include <errno.h>
22 20662ea0 2021-04-10 stsp #include <locale.h>
23 20662ea0 2021-04-10 stsp #include <stdio.h>
24 20662ea0 2021-04-10 stsp #include <stdlib.h>
25 20662ea0 2021-04-10 stsp #include <signal.h>
26 20662ea0 2021-04-10 stsp #include <string.h>
27 20662ea0 2021-04-10 stsp #include <unistd.h>
28 20662ea0 2021-04-10 stsp #include <util.h>
30 20662ea0 2021-04-10 stsp #include "got_version.h"
31 20662ea0 2021-04-10 stsp #include "got_error.h"
32 20662ea0 2021-04-10 stsp #include "got_object.h"
33 20662ea0 2021-04-10 stsp #include "got_reference.h"
34 20662ea0 2021-04-10 stsp #include "got_repository.h"
35 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
36 20662ea0 2021-04-10 stsp #include "got_path.h"
37 20662ea0 2021-04-10 stsp #include "got_cancel.h"
38 20662ea0 2021-04-10 stsp #include "got_privsep.h"
39 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
41 20662ea0 2021-04-10 stsp #ifndef nitems
42 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigint_received;
46 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigpipe_received;
49 20662ea0 2021-04-10 stsp catch_sigint(int signo)
51 20662ea0 2021-04-10 stsp sigint_received = 1;
55 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
57 20662ea0 2021-04-10 stsp sigpipe_received = 1;
61 20662ea0 2021-04-10 stsp struct gotadmin_cmd {
62 20662ea0 2021-04-10 stsp const char *cmd_name;
63 20662ea0 2021-04-10 stsp const struct got_error *(*cmd_main)(int, char *[]);
64 20662ea0 2021-04-10 stsp void (*cmd_usage)(void);
65 20662ea0 2021-04-10 stsp const char *cmd_alias;
68 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
69 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
71 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
73 20662ea0 2021-04-10 stsp static struct gotadmin_cmd gotadmin_commands[] = {
74 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
78 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
82 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
83 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
84 20662ea0 2021-04-10 stsp struct gotadmin_cmd *cmd = &gotadmin_commands[i];
85 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
87 20662ea0 2021-04-10 stsp fputc('\n', fp);
91 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
93 20662ea0 2021-04-10 stsp struct gotadmin_cmd *cmd;
96 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
97 20662ea0 2021-04-10 stsp static struct option longopts[] = {
98 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
99 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
102 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
104 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
105 20662ea0 2021-04-10 stsp switch (ch) {
113 20662ea0 2021-04-10 stsp usage(hflag, 1);
114 20662ea0 2021-04-10 stsp /* NOTREACHED */
118 20662ea0 2021-04-10 stsp argc -= optind;
119 20662ea0 2021-04-10 stsp argv += optind;
120 20662ea0 2021-04-10 stsp optind = 1;
121 20662ea0 2021-04-10 stsp optreset = 1;
123 20662ea0 2021-04-10 stsp if (Vflag) {
124 20662ea0 2021-04-10 stsp got_version_print_str();
128 20662ea0 2021-04-10 stsp if (argc <= 0)
129 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
131 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
132 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
134 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
135 20662ea0 2021-04-10 stsp const struct got_error *error;
137 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
139 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
140 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
144 20662ea0 2021-04-10 stsp gotadmin_commands[i].cmd_usage();
146 20662ea0 2021-04-10 stsp error = gotadmin_commands[i].cmd_main(argc, argv);
147 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
148 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
149 20662ea0 2021-04-10 stsp !(sigpipe_received &&
150 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
151 20662ea0 2021-04-10 stsp !(sigint_received &&
152 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
153 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
160 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
161 20662ea0 2021-04-10 stsp list_commands(stderr);
165 20662ea0 2021-04-10 stsp __dead static void
166 20662ea0 2021-04-10 stsp usage(int hflag, int status)
168 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
170 20662ea0 2021-04-10 stsp fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
171 20662ea0 2021-04-10 stsp getprogname());
173 20662ea0 2021-04-10 stsp list_commands(fp);
174 20662ea0 2021-04-10 stsp exit(status);
177 20662ea0 2021-04-10 stsp static const struct got_error *
178 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
180 20662ea0 2021-04-10 stsp const struct got_error *err;
182 20662ea0 2021-04-10 stsp #ifdef PROFILE
183 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
184 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
186 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
187 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
189 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
190 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
192 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
193 20662ea0 2021-04-10 stsp if (err != NULL)
194 20662ea0 2021-04-10 stsp return err;
196 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
197 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
199 20662ea0 2021-04-10 stsp return NULL;
202 20662ea0 2021-04-10 stsp __dead static void
203 20662ea0 2021-04-10 stsp usage_info(void)
205 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
206 20662ea0 2021-04-10 stsp getprogname());
210 20662ea0 2021-04-10 stsp static const struct got_error *
211 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
213 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
214 20662ea0 2021-04-10 stsp char *cwd = NULL, *repo_path = NULL;
215 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
216 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
217 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
218 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
219 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
221 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
222 20662ea0 2021-04-10 stsp switch (ch) {
224 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
225 20662ea0 2021-04-10 stsp if (repo_path == NULL)
226 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
228 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
231 20662ea0 2021-04-10 stsp usage_info();
232 20662ea0 2021-04-10 stsp /* NOTREACHED */
236 20662ea0 2021-04-10 stsp argc -= optind;
237 20662ea0 2021-04-10 stsp argv += optind;
239 20662ea0 2021-04-10 stsp #ifndef PROFILE
240 20662ea0 2021-04-10 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
241 20662ea0 2021-04-10 stsp NULL) == -1)
242 20662ea0 2021-04-10 stsp err(1, "pledge");
244 20662ea0 2021-04-10 stsp cwd = getcwd(NULL, 0);
245 20662ea0 2021-04-10 stsp if (cwd == NULL) {
246 20662ea0 2021-04-10 stsp error = got_error_from_errno("getcwd");
250 20662ea0 2021-04-10 stsp error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
254 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
258 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
260 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
261 20662ea0 2021-04-10 stsp if (gotconfig) {
262 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
263 20662ea0 2021-04-10 stsp int i, nremotes;
264 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
265 20662ea0 2021-04-10 stsp printf("default author: %s\n",
266 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
268 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
269 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
270 20662ea0 2021-04-10 stsp printf("remote \"%s\": %s\n", remotes[i].name,
271 20662ea0 2021-04-10 stsp remotes[i].url);
275 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
276 20662ea0 2021-04-10 stsp &packsize, repo);
279 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
280 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
281 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
282 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
285 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
286 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
289 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
292 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
293 20662ea0 2021-04-10 stsp if (nobj > 0) {
294 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
295 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
298 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
302 20662ea0 2021-04-10 stsp got_repo_close(repo);
304 20662ea0 2021-04-10 stsp return error;