Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/pkg/pkg.c
Show First 20 Lines • Show All 1,039 Lines • ▼ Show 20 Lines | cleanup: | ||||
return (ret); | return (ret); | ||||
} | } | ||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
{ | { | ||||
char pkgpath[MAXPATHLEN]; | char pkgpath[MAXPATHLEN]; | ||||
const char *pkgarg; | const char *pkgarg, *repo_name; | ||||
bool activation_test, add_pkg, bootstrap_only, force, yes; | bool activation_test, add_pkg, bootstrap_only, force, yes; | ||||
signed char ch; | signed char ch; | ||||
const char *fetchOpts; | const char *fetchOpts; | ||||
char *command; | char *command; | ||||
activation_test = false; | activation_test = false; | ||||
add_pkg = false; | add_pkg = false; | ||||
bootstrap_only = false; | bootstrap_only = false; | ||||
command = NULL; | command = NULL; | ||||
fetchOpts = ""; | fetchOpts = ""; | ||||
force = false; | force = false; | ||||
pkgarg = NULL; | pkgarg = NULL; | ||||
repo_name = NULL; | |||||
yes = false; | yes = false; | ||||
struct option longopts[] = { | struct option longopts[] = { | ||||
{ "force", no_argument, NULL, 'f' }, | { "force", no_argument, NULL, 'f' }, | ||||
{ "only-ipv4", no_argument, NULL, '4' }, | { "only-ipv4", no_argument, NULL, '4' }, | ||||
{ "only-ipv6", no_argument, NULL, '6' }, | { "only-ipv6", no_argument, NULL, '6' }, | ||||
{ "yes", no_argument, NULL, 'y' }, | { "yes", no_argument, NULL, 'y' }, | ||||
{ NULL, 0, NULL, 0 }, | { NULL, 0, NULL, 0 }, | ||||
}; | }; | ||||
snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", getlocalbase()); | snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", getlocalbase()); | ||||
while ((ch = getopt_long(argc, argv, "-:fyN46", longopts, NULL)) != -1) { | while ((ch = getopt_long(argc, argv, "-:fr::yN46", longopts, NULL)) != -1) { | ||||
switch (ch) { | switch (ch) { | ||||
case 'f': | case 'f': | ||||
force = true; | force = true; | ||||
break; | break; | ||||
case 'N': | case 'N': | ||||
activation_test = true; | activation_test = true; | ||||
break; | break; | ||||
case 'y': | case 'y': | ||||
yes = true; | yes = true; | ||||
break; | break; | ||||
case '4': | case '4': | ||||
fetchOpts = "4"; | fetchOpts = "4"; | ||||
break; | break; | ||||
case '6': | case '6': | ||||
fetchOpts = "6"; | fetchOpts = "6"; | ||||
break; | break; | ||||
case 'r': | |||||
/* | |||||
* The repository can only be specified for an explicit | |||||
* bootstrap request at this time, so that we don't | |||||
* confuse the user if they're trying to use a verb that | |||||
* has some other conflicting meaning but we need to | |||||
* bootstrap. | |||||
* | |||||
* For that reason, we specify that -r has an optional | |||||
* argument above and process the next index ourselves. | |||||
* This is mostly significant because getopt(3) will | |||||
* otherwise eat the next argument, which could be | |||||
* something we need to try and make sense of. | |||||
* | |||||
* At worst this gets us false positives that we ignore | |||||
* in other contexts, and we have to do a little fudging | |||||
* in order to support separating -r from the reponame | |||||
* with a space since it's not actually optional in | |||||
* the bootstrap/add sense. | |||||
*/ | |||||
if (add_pkg || bootstrap_only) { | |||||
if (optarg != NULL) { | |||||
repo_name = optarg; | |||||
} else if (optind < argc) { | |||||
repo_name = argv[optind]; | |||||
} | |||||
if (repo_name == NULL || *repo_name == '\0') { | |||||
fprintf(stderr, | |||||
"Must specify a repository with -r!\n"); | |||||
exit(EXIT_FAILURE); | |||||
} | |||||
if (optarg == NULL) { | |||||
/* Advance past repo name. */ | |||||
optreset = 1; | |||||
optind++; | |||||
} | |||||
} | |||||
break; | |||||
case 1: | case 1: | ||||
// Non-option arguments, first one is the command | // Non-option arguments, first one is the command | ||||
if (command == NULL) { | if (command == NULL) { | ||||
command = argv[optind-1]; | command = argv[optind-1]; | ||||
if (strcmp(command, "add") == 0) { | if (strcmp(command, "add") == 0) { | ||||
add_pkg = true; | add_pkg = true; | ||||
} | } | ||||
else if (strcmp(command, "bootstrap") == 0) { | else if (strcmp(command, "bootstrap") == 0) { | ||||
Show All 23 Lines | if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) { | ||||
/* | /* | ||||
* To allow 'pkg -N' to be used as a reliable test for whether | * To allow 'pkg -N' to be used as a reliable test for whether | ||||
* a system is configured to use pkg, don't bootstrap pkg | * a system is configured to use pkg, don't bootstrap pkg | ||||
* when that that option is passed. | * when that that option is passed. | ||||
*/ | */ | ||||
if (activation_test) | if (activation_test) | ||||
errx(EXIT_FAILURE, "pkg is not installed"); | errx(EXIT_FAILURE, "pkg is not installed"); | ||||
config_init(); | config_init(repo_name); | ||||
if (add_pkg) { | if (add_pkg) { | ||||
if (pkgarg == NULL) { | if (pkgarg == NULL) { | ||||
fprintf(stderr, "Path to pkg.txz required\n"); | fprintf(stderr, "Path to pkg.txz required\n"); | ||||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | ||||
} | } | ||||
if (access(pkgarg, R_OK) == -1) { | if (access(pkgarg, R_OK) == -1) { | ||||
fprintf(stderr, "No such file: %s\n", pkgarg); | fprintf(stderr, "No such file: %s\n", pkgarg); | ||||
Show All 39 Lines |