Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/pkg/config.c
Show First 20 Lines • Show All 316 Lines • ▼ Show 20 Lines | |||||
/*- | /*- | ||||
* Parse new repo style configs in style: | * Parse new repo style configs in style: | ||||
* Name: | * Name: | ||||
* URL: | * URL: | ||||
* MIRROR_TYPE: | * MIRROR_TYPE: | ||||
* etc... | * etc... | ||||
*/ | */ | ||||
static void | static void | ||||
parse_repo_file(ucl_object_t *obj) | parse_repo_file(ucl_object_t *obj, const char *requested_repo) | ||||
{ | { | ||||
ucl_object_iter_t it = NULL; | ucl_object_iter_t it = NULL; | ||||
const ucl_object_t *cur; | const ucl_object_t *cur; | ||||
const char *key; | const char *key; | ||||
while ((cur = ucl_iterate_object(obj, &it, true))) { | while ((cur = ucl_iterate_object(obj, &it, true))) { | ||||
key = ucl_object_key(cur); | key = ucl_object_key(cur); | ||||
if (key == NULL) | if (key == NULL) | ||||
continue; | continue; | ||||
if (cur->type != UCL_OBJECT) | if (cur->type != UCL_OBJECT) | ||||
continue; | continue; | ||||
if (requested_repo != NULL && strcmp(requested_repo, key) != 0) | |||||
continue; | |||||
config_parse(cur, CONFFILE_REPO); | config_parse(cur, CONFFILE_REPO); | ||||
} | } | ||||
} | } | ||||
static int | static int | ||||
read_conf_file(const char *confpath, pkg_conf_file_t conftype) | read_conf_file(const char *confpath, const char *requested_repo, | ||||
pkg_conf_file_t conftype) | |||||
manu: Why is it named requested_conf here ? | |||||
Done Inline ActionsNo good reason, I'll rename it. :-) kevans: No good reason, I'll rename it. :-) | |||||
{ | { | ||||
struct ucl_parser *p; | struct ucl_parser *p; | ||||
ucl_object_t *obj = NULL; | ucl_object_t *obj = NULL; | ||||
p = ucl_parser_new(0); | p = ucl_parser_new(0); | ||||
if (!ucl_parser_add_file(p, confpath)) { | if (!ucl_parser_add_file(p, confpath)) { | ||||
if (errno != ENOENT) | if (errno != ENOENT) | ||||
errx(EXIT_FAILURE, "Unable to parse configuration " | errx(EXIT_FAILURE, "Unable to parse configuration " | ||||
"file %s: %s", confpath, ucl_parser_get_error(p)); | "file %s: %s", confpath, ucl_parser_get_error(p)); | ||||
ucl_parser_free(p); | ucl_parser_free(p); | ||||
/* no configuration present */ | /* no configuration present */ | ||||
return (1); | return (1); | ||||
} | } | ||||
obj = ucl_parser_get_object(p); | obj = ucl_parser_get_object(p); | ||||
if (obj->type != UCL_OBJECT) | if (obj->type != UCL_OBJECT) | ||||
warnx("Invalid configuration format, ignoring the " | warnx("Invalid configuration format, ignoring the " | ||||
"configuration file %s", confpath); | "configuration file %s", confpath); | ||||
else { | else { | ||||
if (conftype == CONFFILE_PKG) | if (conftype == CONFFILE_PKG) | ||||
config_parse(obj, conftype); | config_parse(obj, conftype); | ||||
else if (conftype == CONFFILE_REPO) | else if (conftype == CONFFILE_REPO) | ||||
parse_repo_file(obj); | parse_repo_file(obj, requested_repo); | ||||
} | } | ||||
ucl_object_unref(obj); | ucl_object_unref(obj); | ||||
ucl_parser_free(p); | ucl_parser_free(p); | ||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
load_repositories(const char *repodir) | load_repositories(const char *repodir, const char *requested_repo) | ||||
{ | { | ||||
struct dirent *ent; | struct dirent *ent; | ||||
DIR *d; | DIR *d; | ||||
char *p; | char *p; | ||||
size_t n; | size_t n; | ||||
char path[MAXPATHLEN]; | char path[MAXPATHLEN]; | ||||
int ret; | int ret; | ||||
ret = 0; | ret = 0; | ||||
if ((d = opendir(repodir)) == NULL) | if ((d = opendir(repodir)) == NULL) | ||||
return (1); | return (1); | ||||
while ((ent = readdir(d))) { | while ((ent = readdir(d))) { | ||||
/* Trim out 'repos'. */ | /* Trim out 'repos'. */ | ||||
if ((n = strlen(ent->d_name)) <= 5) | if ((n = strlen(ent->d_name)) <= 5) | ||||
continue; | continue; | ||||
p = &ent->d_name[n - 5]; | p = &ent->d_name[n - 5]; | ||||
if (strcmp(p, ".conf") == 0) { | if (strcmp(p, ".conf") == 0) { | ||||
snprintf(path, sizeof(path), "%s%s%s", | snprintf(path, sizeof(path), "%s%s%s", | ||||
repodir, | repodir, | ||||
repodir[strlen(repodir) - 1] == '/' ? "" : "/", | repodir[strlen(repodir) - 1] == '/' ? "" : "/", | ||||
ent->d_name); | ent->d_name); | ||||
if (access(path, F_OK) == 0 && | if (access(path, F_OK) != 0) | ||||
read_conf_file(path, CONFFILE_REPO)) { | continue; | ||||
if (read_conf_file(path, requested_repo, | |||||
CONFFILE_REPO)) { | |||||
ret = 1; | ret = 1; | ||||
goto cleanup; | goto cleanup; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
cleanup: | cleanup: | ||||
closedir(d); | closedir(d); | ||||
return (ret); | return (ret); | ||||
} | } | ||||
int | int | ||||
config_init(void) | config_init(const char *requested_repo) | ||||
{ | { | ||||
char *val; | char *val; | ||||
int i; | int i; | ||||
const char *localbase; | const char *localbase; | ||||
char *env_list_item; | char *env_list_item; | ||||
char confpath[MAXPATHLEN]; | char confpath[MAXPATHLEN]; | ||||
struct config_value *cv; | struct config_value *cv; | ||||
char abi[BUFSIZ]; | char abi[BUFSIZ]; | ||||
Show All 24 Lines | if (val != NULL) { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/* Read LOCALBASE/etc/pkg.conf first. */ | /* Read LOCALBASE/etc/pkg.conf first. */ | ||||
localbase = getlocalbase(); | localbase = getlocalbase(); | ||||
snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf", localbase); | snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf", localbase); | ||||
if (access(confpath, F_OK) == 0 && read_conf_file(confpath, | if (access(confpath, F_OK) == 0 && read_conf_file(confpath, NULL, | ||||
CONFFILE_PKG)) | CONFFILE_PKG)) | ||||
goto finalize; | goto finalize; | ||||
/* Then read in all repos from REPOS_DIR list of directories. */ | /* Then read in all repos from REPOS_DIR list of directories. */ | ||||
if (c[REPOS_DIR].list == NULL) { | if (c[REPOS_DIR].list == NULL) { | ||||
c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list)); | c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list)); | ||||
STAILQ_INIT(c[REPOS_DIR].list); | STAILQ_INIT(c[REPOS_DIR].list); | ||||
cv = malloc(sizeof(struct config_value)); | cv = malloc(sizeof(struct config_value)); | ||||
cv->value = strdup("/etc/pkg"); | cv->value = strdup("/etc/pkg"); | ||||
STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next); | STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next); | ||||
cv = malloc(sizeof(struct config_value)); | cv = malloc(sizeof(struct config_value)); | ||||
if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0) | if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0) | ||||
goto finalize; | goto finalize; | ||||
STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next); | STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next); | ||||
} | } | ||||
STAILQ_FOREACH(cv, c[REPOS_DIR].list, next) | STAILQ_FOREACH(cv, c[REPOS_DIR].list, next) | ||||
if (load_repositories(cv->value)) | if (load_repositories(cv->value, requested_repo)) | ||||
goto finalize; | goto finalize; | ||||
finalize: | finalize: | ||||
if (c[ABI].val == NULL && c[ABI].value == NULL) { | if (c[ABI].val == NULL && c[ABI].value == NULL) { | ||||
if (pkg_get_myabi(abi, BUFSIZ) != 0) | if (pkg_get_myabi(abi, BUFSIZ) != 0) | ||||
errx(EXIT_FAILURE, "Failed to determine the system " | errx(EXIT_FAILURE, "Failed to determine the system " | ||||
"ABI"); | "ABI"); | ||||
c[ABI].val = abi; | c[ABI].val = abi; | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
Why is it named requested_conf here ?