| File: | gotadmin/gotadmin.c |
| Warning: | line 237, column 2 Value stored to 'argv' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 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 | */ |
| 16 | |
| 17 | #include <sys/queue.h> |
| 18 | |
| 19 | #include <getopt.h> |
| 20 | #include <err.h> |
| 21 | #include <errno(*__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> |
| 29 | |
| 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" |
| 40 | |
| 41 | #ifndef nitems |
| 42 | #define nitems(_a)(sizeof((_a)) / sizeof((_a)[0])) (sizeof((_a)) / sizeof((_a)[0])) |
| 43 | #endif |
| 44 | |
| 45 | static volatile sig_atomic_t sigint_received; |
| 46 | static volatile sig_atomic_t sigpipe_received; |
| 47 | |
| 48 | static void |
| 49 | catch_sigint(int signo) |
| 50 | { |
| 51 | sigint_received = 1; |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | catch_sigpipe(int signo) |
| 56 | { |
| 57 | sigpipe_received = 1; |
| 58 | } |
| 59 | |
| 60 | |
| 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 | }; |
| 67 | |
| 68 | __dead__attribute__((__noreturn__)) static void usage(int, int); |
| 69 | __dead__attribute__((__noreturn__)) static void usage_info(void); |
| 70 | |
| 71 | static const struct got_error* cmd_info(int, char *[]); |
| 72 | |
| 73 | static struct gotadmin_cmd gotadmin_commands[] = { |
| 74 | { "info", cmd_info, usage_info, "" }, |
| 75 | }; |
| 76 | |
| 77 | static void |
| 78 | list_commands(FILE *fp) |
| 79 | { |
| 80 | size_t i; |
| 81 | |
| 82 | fprintf(fp, "commands:"); |
| 83 | for (i = 0; i < nitems(gotadmin_commands)(sizeof((gotadmin_commands)) / sizeof((gotadmin_commands)[0]) ); i++) { |
| 84 | struct gotadmin_cmd *cmd = &gotadmin_commands[i]; |
| 85 | fprintf(fp, " %s", cmd->cmd_name); |
| 86 | } |
| 87 | fputc('\n', fp); |
| 88 | } |
| 89 | |
| 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_argument0, NULL((void *)0), 'V' }, |
| 99 | { NULL((void *)0), 0, NULL((void *)0), 0 } |
| 100 | }; |
| 101 | |
| 102 | setlocale(LC_CTYPE2, ""); |
| 103 | |
| 104 | while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL((void *)0))) != -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 */ |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | argc -= optind; |
| 119 | argv += optind; |
| 120 | optind = 1; |
| 121 | optreset = 1; |
| 122 | |
| 123 | if (Vflag) { |
| 124 | got_version_print_str(); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | if (argc <= 0) |
| 129 | usage(hflag, hflag ? 0 : 1); |
| 130 | |
| 131 | signal(SIGINT2, catch_sigint); |
| 132 | signal(SIGPIPE13, catch_sigpipe); |
| 133 | |
| 134 | for (i = 0; i < nitems(gotadmin_commands)(sizeof((gotadmin_commands)) / sizeof((gotadmin_commands)[0]) ); i++) { |
| 135 | const struct got_error *error; |
| 136 | |
| 137 | cmd = &gotadmin_commands[i]; |
| 138 | |
| 139 | if (strcmp(cmd->cmd_name, argv[0]) != 0 && |
| 140 | strcmp(cmd->cmd_alias, argv[0]) != 0) |
| 141 | continue; |
| 142 | |
| 143 | if (hflag) |
| 144 | gotadmin_commands[i].cmd_usage(); |
| 145 | |
| 146 | error = gotadmin_commands[i].cmd_main(argc, argv); |
| 147 | if (error && error->code != GOT_ERR_CANCELLED49 && |
| 148 | error->code != GOT_ERR_PRIVSEP_EXIT41 && |
| 149 | !(sigpipe_received && |
| 150 | error->code == GOT_ERR_ERRNO1 && errno(*__errno()) == EPIPE32) && |
| 151 | !(sigint_received && |
| 152 | error->code == GOT_ERR_ERRNO1 && errno(*__errno()) == EINTR4)) { |
| 153 | fprintf(stderr(&__sF[2]), "%s: %s\n", getprogname(), error->msg); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | fprintf(stderr(&__sF[2]), "%s: unknown command '%s'\n", getprogname(), argv[0]); |
| 161 | list_commands(stderr(&__sF[2])); |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | __dead__attribute__((__noreturn__)) static void |
| 166 | usage(int hflag, int status) |
| 167 | { |
| 168 | FILE *fp = (status == 0) ? stdout(&__sF[1]) : stderr(&__sF[2]); |
| 169 | |
| 170 | fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n", |
| 171 | getprogname()); |
| 172 | if (hflag) |
| 173 | list_commands(fp); |
| 174 | exit(status); |
| 175 | } |
| 176 | |
| 177 | static const struct got_error * |
| 178 | apply_unveil(const char *repo_path, int repo_read_only) |
| 179 | { |
| 180 | const struct got_error *err; |
| 181 | |
| 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); |
| 188 | |
| 189 | if (unveil(GOT_TMPDIR_STR"/tmp", "rwc") != 0) |
| 190 | return got_error_from_errno2("unveil", GOT_TMPDIR_STR"/tmp"); |
| 191 | |
| 192 | err = got_privsep_unveil_exec_helpers(); |
| 193 | if (err != NULL((void *)0)) |
| 194 | return err; |
| 195 | |
| 196 | if (unveil(NULL((void *)0), NULL((void *)0)) != 0) |
| 197 | return got_error_from_errno("unveil"); |
| 198 | |
| 199 | return NULL((void *)0); |
| 200 | } |
| 201 | |
| 202 | __dead__attribute__((__noreturn__)) static void |
| 203 | usage_info(void) |
| 204 | { |
| 205 | fprintf(stderr(&__sF[2]), "usage: %s info [-r repository-path]\n", |
| 206 | getprogname()); |
| 207 | exit(1); |
| 208 | } |
| 209 | |
| 210 | static const struct got_error * |
| 211 | cmd_info(int argc, char *argv[]) |
| 212 | { |
| 213 | const struct got_error *error = NULL((void *)0); |
| 214 | char *cwd = NULL((void *)0), *repo_path = NULL((void *)0); |
| 215 | struct got_repository *repo = NULL((void *)0); |
| 216 | const struct got_gotconfig *gotconfig = NULL((void *)0); |
| 217 | int ch, npackfiles, npackedobj, nobj; |
| 218 | off_t packsize, loose_size; |
| 219 | char scaled[FMT_SCALED_STRSIZE7]; |
| 220 | |
| 221 | while ((ch = getopt(argc, argv, "r:")) != -1) { |
| 222 | switch (ch) { |
| 223 | case 'r': |
| 224 | repo_path = realpath(optarg, NULL((void *)0)); |
| 225 | if (repo_path == NULL((void *)0)) |
| 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 */ |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | argc -= optind; |
| 237 | argv += optind; |
Value stored to 'argv' is never read | |
| 238 | |
| 239 | #ifndef PROFILE |
| 240 | if (pledge("stdio rpath wpath flock proc exec sendfd unveil", |
| 241 | NULL((void *)0)) == -1) |
| 242 | err(1, "pledge"); |
| 243 | #endif |
| 244 | cwd = getcwd(NULL((void *)0), 0); |
| 245 | if (cwd == NULL((void *)0)) { |
| 246 | error = got_error_from_errno("getcwd"); |
| 247 | goto done; |
| 248 | } |
| 249 | |
| 250 | error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL((void *)0)); |
| 251 | if (error) |
| 252 | goto done; |
| 253 | |
| 254 | error = apply_unveil(got_repo_get_path_git_dir(repo), 1); |
| 255 | if (error) |
| 256 | goto done; |
| 257 | |
| 258 | printf("repository: %s\n", got_repo_get_path_git_dir(repo)); |
| 259 | |
| 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)); |
| 267 | } |
| 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); |
| 272 | } |
| 273 | } |
| 274 | |
| 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; |
| 284 | } |
| 285 | printf("packed objects: %d\n", npackedobj); |
| 286 | printf("packed total size: %s\n", scaled); |
| 287 | } |
| 288 | |
| 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; |
| 297 | } |
| 298 | printf("loose total size: %s\n", scaled); |
| 299 | } |
| 300 | done: |
| 301 | if (repo) |
| 302 | got_repo_close(repo); |
| 303 | free(cwd); |
| 304 | return error; |
| 305 | } |