conf: improve the include paths code

- various cleanups (more straight code and allocations)
- do not add the base config path /usr/share/alsa to the
  explicit include list of directories
  (it's not wanted for ucm configs)

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2019-11-10 13:00:59 +01:00
parent 6598e38856
commit f600310954
3 changed files with 46 additions and 57 deletions

View file

@ -359,7 +359,7 @@ int snd_config_search_alias_hooks(snd_config_t *config,
int _snd_conf_generic_id(const char *id); int _snd_conf_generic_id(const char *id);
int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in, int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
int override, char *default_include_path); int override, const char * const *default_include_path);
/* convenience macros */ /* convenience macros */
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

View file

@ -525,7 +525,7 @@ static inline void snd_config_unlock(void) { }
* The direcotry should be a subdiretory of top configuration directory * The direcotry should be a subdiretory of top configuration directory
* "/usr/share/alsa/". * "/usr/share/alsa/".
*/ */
static int add_include_path(struct filedesc *fd, char *dir) static int add_include_path(struct filedesc *fd, const char *dir)
{ {
struct include_path *path; struct include_path *path;
@ -533,7 +533,12 @@ static int add_include_path(struct filedesc *fd, char *dir)
if (!path) if (!path)
return -ENOMEM; return -ENOMEM;
path->dir = dir; path->dir = strdup(dir);
if (path->dir == NULL) {
free(path);
return -ENOMEM;
}
list_add_tail(&path->list, &fd->include_paths); list_add_tail(&path->list, &fd->include_paths);
return 0; return 0;
} }
@ -598,53 +603,37 @@ static char *_snd_config_path(const char *name)
* *
* This function will search and open the file in the following order * This function will search and open the file in the following order
* of priority: * of priority:
* 1. directly open the file by its name; * 1. directly open the file by its name (only if absolute)
* 2. search for the file name in top configuration directory * 2. search for the file name in in additional configuration directories
* "/usr/share/alsa/"; * specified by users, via alsaconf syntax
* 3. search for the file name in in additional configuration directories * <searchdir:relative-path/to/user/share/alsa>;
* specified by users, via alsaconf syntax * These directories should be subdirectories of /usr/share/alsa.
* <searchdir:relative-path/to/user/share/alsa>;
* These directories should be subdirectories of /usr/share/alsa.
*/ */
static int input_stdio_open(snd_input_t **inputp, const char *file, static int input_stdio_open(snd_input_t **inputp, const char *file,
struct list_head *include_paths) struct list_head *include_paths)
{ {
struct list_head *pos, *base; struct list_head *pos;
struct include_path *path; struct include_path *path;
char full_path[PATH_MAX + 1]; char full_path[PATH_MAX];
int err = 0; int err = 0;
err = snd_input_stdio_open(inputp, file, "r"); if (file[0] == '/')
if (err == 0) return snd_input_stdio_open(inputp, file, "r");
goto out;
if (file[0] == '/') /* not search file with absolute path */
return err;
/* search file in top configuration directory /usr/share/alsa */
snprintf(full_path, PATH_MAX, "%s/%s", snd_config_topdir(), file);
err = snd_input_stdio_open(inputp, full_path, "r");
if (err == 0)
goto out;
/* search file in user specified include paths. These directories /* search file in user specified include paths. These directories
* are subdirectories of /usr/share/alsa. * are subdirectories of /usr/share/alsa.
*/ */
if (include_paths) { list_for_each(pos, include_paths) {
base = include_paths; path = list_entry(pos, struct include_path, list);
list_for_each(pos, base) { if (!path->dir)
path = list_entry(pos, struct include_path, list); continue;
if (!path->dir)
continue;
snprintf(full_path, PATH_MAX, "%s/%s", path->dir, file); snprintf(full_path, PATH_MAX, "%s/%s", path->dir, file);
err = snd_input_stdio_open(inputp, full_path, "r"); err = snd_input_stdio_open(inputp, full_path, "r");
if (err == 0) if (err == 0)
goto out; return 0;
}
} }
out:
return err; return err;
} }
@ -798,9 +787,9 @@ static int get_char_skip_comments(input_t *input)
closedir(dirp); closedir(dirp);
err = add_include_path(input->current, str); err = add_include_path(input->current, str);
free(str);
if (err < 0) { if (err < 0) {
SNDERR("Cannot add search dir %s", str); SNDERR("Cannot add search dir %s", str);
free(str);
return err; return err;
} }
continue; continue;
@ -1835,28 +1824,32 @@ int snd_config_top(snd_config_t **config)
#ifndef DOC_HIDDEN #ifndef DOC_HIDDEN
int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in, int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
int override, char *default_include_path) int override, const char * const *include_paths)
{ {
int err; int err;
input_t input; input_t input;
struct filedesc *fd, *fd_next; struct filedesc *fd, *fd_next;
assert(config && in); assert(config && in);
fd = malloc(sizeof(*fd)); fd = malloc(sizeof(*fd));
if (!fd) { if (!fd)
err = -ENOMEM; return -ENOMEM;
goto _end_inc;
}
fd->name = NULL; fd->name = NULL;
fd->in = in; fd->in = in;
fd->line = 1; fd->line = 1;
fd->column = 0; fd->column = 0;
fd->next = NULL; fd->next = NULL;
INIT_LIST_HEAD(&fd->include_paths); INIT_LIST_HEAD(&fd->include_paths);
if (default_include_path) { if (include_paths) {
err = add_include_path(fd, default_include_path); for (; *include_paths; include_paths++) {
err = add_include_path(fd, *include_paths);
if (err < 0)
goto _end;
}
} else {
err = add_include_path(fd, snd_config_topdir());
if (err < 0) if (err < 0)
goto _end; goto _end;
default_include_path = NULL;
} }
input.current = fd; input.current = fd;
input.unget = 0; input.unget = 0;
@ -1905,8 +1898,6 @@ int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
free_include_paths(fd); free_include_paths(fd);
free(fd); free(fd);
_end_inc:
free(default_include_path);
return err; return err;
} }
#endif #endif

View file

@ -54,7 +54,7 @@ int uc_mgr_config_load(const char *file, snd_config_t **cfg)
FILE *fp; FILE *fp;
snd_input_t *in; snd_input_t *in;
snd_config_t *top; snd_config_t *top;
char *default_path; const char *path, *default_paths[2];
int err; int err;
fp = fopen(file, "r"); fp = fopen(file, "r");
@ -71,15 +71,13 @@ int uc_mgr_config_load(const char *file, snd_config_t **cfg)
if (err < 0) if (err < 0)
goto __err1; goto __err1;
default_path = getenv(ALSA_CONFIG_UCM_VAR); path = getenv(ALSA_CONFIG_UCM_VAR);
if (!default_path || !*default_path) if (!path || path[0] == '\0')
default_path = ALSA_CONFIG_DIR "/ucm"; path = ALSA_CONFIG_DIR "/ucm";
default_path = strdup(default_path);
if (!default_path) { default_paths[0] = path;
err = -ENOMEM; default_paths[1] = NULL;
goto __err2; err = _snd_config_load_with_include(top, in, 0, default_paths);
}
err = _snd_config_load_with_include(top, in, 0, default_path);
if (err < 0) { if (err < 0) {
uc_error("could not load configuration file %s", file); uc_error("could not load configuration file %s", file);
goto __err2; goto __err2;