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>
19 #include <getopt.h>
20 #include <err.h>
21 #include <errno.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <util.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"
36 #include "got_path.h"
37 #include "got_cancel.h"
38 #include "got_privsep.h"
39 #include "got_opentemp.h"
41 #ifndef nitems
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 #endif
45 static volatile sig_atomic_t sigint_received;
46 static volatile sig_atomic_t sigpipe_received;
48 static void
49 catch_sigint(int signo)
50 {
51 sigint_received = 1;
52 }
54 static void
55 catch_sigpipe(int signo)
56 {
57 sigpipe_received = 1;
58 }
61 struct gotadmin_cmd {
62 const char *cmd_name;
63 const struct got_error *(*cmd_main)(int, char *[]);
64 void (*cmd_usage)(void);
65 const char *cmd_alias;
66 };
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, "" },
75 };
77 static void
78 list_commands(FILE *fp)
79 {
80 size_t i;
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);
86 }
87 fputc('\n', fp);
88 }
90 int
91 main(int argc, char *argv[])
92 {
93 struct gotadmin_cmd *cmd;
94 size_t i;
95 int ch;
96 int hflag = 0, Vflag = 0;
97 static struct option longopts[] = {
98 { "version", no_argument, NULL, 'V' },
99 { NULL, 0, NULL, 0 }
100 };
102 setlocale(LC_CTYPE, "");
104 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
105 switch (ch) {
106 case 'h':
107 hflag = 1;
108 break;
109 case 'V':
110 Vflag = 1;
111 break;
112 default:
113 usage(hflag, 1);
114 /* NOTREACHED */
118 argc -= optind;
119 argv += optind;
120 optind = 1;
121 optreset = 1;
123 if (Vflag) {
124 got_version_print_str();
125 return 0;
128 if (argc <= 0)
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)
141 continue;
143 if (hflag)
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) &&
151 !(sigint_received &&
152 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
153 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
154 return 1;
157 return 0;
160 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
161 list_commands(stderr);
162 return 1;
165 __dead static void
166 usage(int hflag, int status)
168 FILE *fp = (status == 0) ? stdout : stderr;
170 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
171 getprogname());
172 if (hflag)
173 list_commands(fp);
174 exit(status);
177 static const struct got_error *
178 apply_unveil(const char *repo_path, int repo_read_only)
180 const struct got_error *err;
182 #ifdef PROFILE
183 if (unveil("gmon.out", "rwc") != 0)
184 return got_error_from_errno2("unveil", "gmon.out");
185 #endif
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();
193 if (err != NULL)
194 return err;
196 if (unveil(NULL, NULL) != 0)
197 return got_error_from_errno("unveil");
199 return NULL;
202 __dead static void
203 usage_info(void)
205 fprintf(stderr, "usage: %s info [-r repository-path]\n",
206 getprogname());
207 exit(1);
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) {
222 switch (ch) {
223 case 'r':
224 repo_path = realpath(optarg, NULL);
225 if (repo_path == NULL)
226 return got_error_from_errno2("realpath",
227 optarg);
228 got_path_strip_trailing_slashes(repo_path);
229 break;
230 default:
231 usage_info();
232 /* NOTREACHED */
236 argc -= optind;
237 argv += optind;
239 #ifndef PROFILE
240 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
241 NULL) == -1)
242 err(1, "pledge");
243 #endif
244 cwd = getcwd(NULL, 0);
245 if (cwd == NULL) {
246 error = got_error_from_errno("getcwd");
247 goto done;
250 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
251 if (error)
252 goto done;
254 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
255 if (error)
256 goto done;
258 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
260 gotconfig = got_repo_get_gotconfig(repo);
261 if (gotconfig) {
262 const struct got_remote_repo *remotes;
263 int i, nremotes;
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,
271 remotes[i].url);
275 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
276 &packsize, repo);
277 if (error)
278 goto done;
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");
283 goto done;
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);
290 if (error)
291 goto done;
292 printf("loose objects: %d\n", nobj);
293 if (nobj > 0) {
294 if (fmt_scaled(loose_size, scaled) == -1) {
295 error = got_error_from_errno("fmt_scaled");
296 goto done;
298 printf("loose total size: %s\n", scaled);
300 done:
301 if (repo)
302 got_repo_close(repo);
303 free(cwd);
304 return error;