Blame


1 20662ea0 2021-04-10 stsp /*
2 20662ea0 2021-04-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 20662ea0 2021-04-10 stsp *
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.
7 20662ea0 2021-04-10 stsp *
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.
15 20662ea0 2021-04-10 stsp */
16 20662ea0 2021-04-10 stsp
17 20662ea0 2021-04-10 stsp #include <sys/queue.h>
18 20662ea0 2021-04-10 stsp
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>
29 20662ea0 2021-04-10 stsp
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"
40 20662ea0 2021-04-10 stsp
41 20662ea0 2021-04-10 stsp #ifndef nitems
42 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 20662ea0 2021-04-10 stsp #endif
44 20662ea0 2021-04-10 stsp
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;
47 20662ea0 2021-04-10 stsp
48 20662ea0 2021-04-10 stsp static void
49 20662ea0 2021-04-10 stsp catch_sigint(int signo)
50 20662ea0 2021-04-10 stsp {
51 20662ea0 2021-04-10 stsp sigint_received = 1;
52 20662ea0 2021-04-10 stsp }
53 20662ea0 2021-04-10 stsp
54 20662ea0 2021-04-10 stsp static void
55 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
56 20662ea0 2021-04-10 stsp {
57 20662ea0 2021-04-10 stsp sigpipe_received = 1;
58 20662ea0 2021-04-10 stsp }
59 20662ea0 2021-04-10 stsp
60 20662ea0 2021-04-10 stsp
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;
66 20662ea0 2021-04-10 stsp };
67 20662ea0 2021-04-10 stsp
68 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
69 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
70 20662ea0 2021-04-10 stsp
71 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
72 20662ea0 2021-04-10 stsp
73 20662ea0 2021-04-10 stsp static struct gotadmin_cmd gotadmin_commands[] = {
74 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
75 20662ea0 2021-04-10 stsp };
76 20662ea0 2021-04-10 stsp
77 20662ea0 2021-04-10 stsp static void
78 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
79 20662ea0 2021-04-10 stsp {
80 20662ea0 2021-04-10 stsp size_t i;
81 20662ea0 2021-04-10 stsp
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);
86 20662ea0 2021-04-10 stsp }
87 20662ea0 2021-04-10 stsp fputc('\n', fp);
88 20662ea0 2021-04-10 stsp }
89 20662ea0 2021-04-10 stsp
90 20662ea0 2021-04-10 stsp int
91 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
92 20662ea0 2021-04-10 stsp {
93 20662ea0 2021-04-10 stsp struct gotadmin_cmd *cmd;
94 20662ea0 2021-04-10 stsp size_t i;
95 20662ea0 2021-04-10 stsp int ch;
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 }
100 20662ea0 2021-04-10 stsp };
101 20662ea0 2021-04-10 stsp
102 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
103 20662ea0 2021-04-10 stsp
104 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
105 20662ea0 2021-04-10 stsp switch (ch) {
106 20662ea0 2021-04-10 stsp case 'h':
107 20662ea0 2021-04-10 stsp hflag = 1;
108 20662ea0 2021-04-10 stsp break;
109 20662ea0 2021-04-10 stsp case 'V':
110 20662ea0 2021-04-10 stsp Vflag = 1;
111 20662ea0 2021-04-10 stsp break;
112 20662ea0 2021-04-10 stsp default:
113 20662ea0 2021-04-10 stsp usage(hflag, 1);
114 20662ea0 2021-04-10 stsp /* NOTREACHED */
115 20662ea0 2021-04-10 stsp }
116 20662ea0 2021-04-10 stsp }
117 20662ea0 2021-04-10 stsp
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;
122 20662ea0 2021-04-10 stsp
123 20662ea0 2021-04-10 stsp if (Vflag) {
124 20662ea0 2021-04-10 stsp got_version_print_str();
125 20662ea0 2021-04-10 stsp return 0;
126 20662ea0 2021-04-10 stsp }
127 20662ea0 2021-04-10 stsp
128 20662ea0 2021-04-10 stsp if (argc <= 0)
129 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
130 20662ea0 2021-04-10 stsp
131 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
132 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
133 20662ea0 2021-04-10 stsp
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;
136 20662ea0 2021-04-10 stsp
137 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
138 20662ea0 2021-04-10 stsp
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)
141 20662ea0 2021-04-10 stsp continue;
142 20662ea0 2021-04-10 stsp
143 20662ea0 2021-04-10 stsp if (hflag)
144 20662ea0 2021-04-10 stsp gotadmin_commands[i].cmd_usage();
145 20662ea0 2021-04-10 stsp
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);
154 20662ea0 2021-04-10 stsp return 1;
155 20662ea0 2021-04-10 stsp }
156 20662ea0 2021-04-10 stsp
157 20662ea0 2021-04-10 stsp return 0;
158 20662ea0 2021-04-10 stsp }
159 20662ea0 2021-04-10 stsp
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);
162 20662ea0 2021-04-10 stsp return 1;
163 20662ea0 2021-04-10 stsp }
164 20662ea0 2021-04-10 stsp
165 20662ea0 2021-04-10 stsp __dead static void
166 20662ea0 2021-04-10 stsp usage(int hflag, int status)
167 20662ea0 2021-04-10 stsp {
168 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
169 20662ea0 2021-04-10 stsp
170 20662ea0 2021-04-10 stsp fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
171 20662ea0 2021-04-10 stsp getprogname());
172 20662ea0 2021-04-10 stsp if (hflag)
173 20662ea0 2021-04-10 stsp list_commands(fp);
174 20662ea0 2021-04-10 stsp exit(status);
175 20662ea0 2021-04-10 stsp }
176 20662ea0 2021-04-10 stsp
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)
179 20662ea0 2021-04-10 stsp {
180 20662ea0 2021-04-10 stsp const struct got_error *err;
181 20662ea0 2021-04-10 stsp
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");
185 20662ea0 2021-04-10 stsp #endif
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);
188 20662ea0 2021-04-10 stsp
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);
191 20662ea0 2021-04-10 stsp
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;
195 20662ea0 2021-04-10 stsp
196 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
197 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
198 20662ea0 2021-04-10 stsp
199 20662ea0 2021-04-10 stsp return NULL;
200 20662ea0 2021-04-10 stsp }
201 20662ea0 2021-04-10 stsp
202 20662ea0 2021-04-10 stsp __dead static void
203 20662ea0 2021-04-10 stsp usage_info(void)
204 20662ea0 2021-04-10 stsp {
205 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
206 20662ea0 2021-04-10 stsp getprogname());
207 20662ea0 2021-04-10 stsp exit(1);
208 20662ea0 2021-04-10 stsp }
209 20662ea0 2021-04-10 stsp
210 20662ea0 2021-04-10 stsp static const struct got_error *
211 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
212 20662ea0 2021-04-10 stsp {
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];
220 20662ea0 2021-04-10 stsp
221 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
222 20662ea0 2021-04-10 stsp switch (ch) {
223 20662ea0 2021-04-10 stsp case 'r':
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",
227 20662ea0 2021-04-10 stsp optarg);
228 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
229 20662ea0 2021-04-10 stsp break;
230 20662ea0 2021-04-10 stsp default:
231 20662ea0 2021-04-10 stsp usage_info();
232 20662ea0 2021-04-10 stsp /* NOTREACHED */
233 20662ea0 2021-04-10 stsp }
234 20662ea0 2021-04-10 stsp }
235 20662ea0 2021-04-10 stsp
236 20662ea0 2021-04-10 stsp argc -= optind;
237 20662ea0 2021-04-10 stsp argv += optind;
238 20662ea0 2021-04-10 stsp
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");
243 20662ea0 2021-04-10 stsp #endif
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");
247 20662ea0 2021-04-10 stsp goto done;
248 20662ea0 2021-04-10 stsp }
249 20662ea0 2021-04-10 stsp
250 20662ea0 2021-04-10 stsp error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
251 20662ea0 2021-04-10 stsp if (error)
252 20662ea0 2021-04-10 stsp goto done;
253 20662ea0 2021-04-10 stsp
254 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
255 20662ea0 2021-04-10 stsp if (error)
256 20662ea0 2021-04-10 stsp goto done;
257 20662ea0 2021-04-10 stsp
258 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
259 20662ea0 2021-04-10 stsp
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));
267 20662ea0 2021-04-10 stsp }
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);
272 20662ea0 2021-04-10 stsp }
273 20662ea0 2021-04-10 stsp }
274 20662ea0 2021-04-10 stsp
275 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
276 20662ea0 2021-04-10 stsp &packsize, repo);
277 20662ea0 2021-04-10 stsp if (error)
278 20662ea0 2021-04-10 stsp goto done;
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");
283 20662ea0 2021-04-10 stsp goto done;
284 20662ea0 2021-04-10 stsp }
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);
287 20662ea0 2021-04-10 stsp }
288 20662ea0 2021-04-10 stsp
289 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
290 20662ea0 2021-04-10 stsp if (error)
291 20662ea0 2021-04-10 stsp goto done;
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");
296 20662ea0 2021-04-10 stsp goto done;
297 20662ea0 2021-04-10 stsp }
298 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
299 20662ea0 2021-04-10 stsp }
300 20662ea0 2021-04-10 stsp done:
301 20662ea0 2021-04-10 stsp if (repo)
302 20662ea0 2021-04-10 stsp got_repo_close(repo);
303 20662ea0 2021-04-10 stsp free(cwd);
304 20662ea0 2021-04-10 stsp return error;
305 20662ea0 2021-04-10 stsp }