Improve config file search order

Previously the search order is:

~/.config/compton/compton.conf
/etc/xdg/compton/compton.conf
~/.config/compton.conf
/etc/xdg/compton.conf
...

Now the search order is:

~/.config/compton/compton.conf
~/.config/compton.conf
~/.compton.conf
/etc/xdg/compton/compton.conf
...

In other word, compton will now search all possible user config file
path first before searching for a system config file.

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2019-01-30 00:27:14 +00:00
parent 2eb8cf961e
commit 14cf5f455e
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
1 changed files with 48 additions and 21 deletions

View File

@ -37,6 +37,28 @@ lcfg_lookup_bool(const config_t *config, const char *path, bool *value) {
return ret; return ret;
} }
/// Search for config file under a base directory
FILE *
open_config_file_at(const char *base, char **out_path) {
static const char *config_paths[] = {
"/compton.conf",
"/compton/compton.conf"
};
for (size_t i = 0; i < ARR_SIZE(config_paths); i++) {
char *path = mstrjoin(base, config_paths[i]);
FILE *ret = fopen(path, "r");
if (ret && out_path) {
*out_path = path;
} else {
free(path);
}
if (ret) {
return ret;
}
}
return NULL;
}
/** /**
* Get a file stream of the configuration file to read. * Get a file stream of the configuration file to read.
* *
@ -44,10 +66,6 @@ lcfg_lookup_bool(const config_t *config, const char *path, bool *value) {
*/ */
FILE * FILE *
open_config_file(const char *cpath, char **ppath) { open_config_file(const char *cpath, char **ppath) {
static const char *config_paths[] = {
"/compton.conf",
"/compton/compton.conf"
};
static const char config_filename_legacy[] = "/.compton.conf"; static const char config_filename_legacy[] = "/.compton.conf";
if (cpath) { if (cpath) {
@ -57,31 +75,40 @@ open_config_file(const char *cpath, char **ppath) {
return ret; return ret;
} }
for (size_t i = 0; i < ARR_SIZE(config_paths); i++) { // First search for config file in user config directory
char *path = xdgConfigFind(config_paths[i], NULL); auto config_home = xdgConfigHome(NULL);
FILE *ret = fopen(path, "r"); auto ret = open_config_file_at(config_home, ppath);
free((void *)config_home);
if (ret) {
return ret;
}
// Fall back to legacy config file in user home directory
const char *home = getenv("HOME");
if (home && strlen(home)) {
auto path = mstrjoin(home, config_filename_legacy);
ret = fopen(path, "r");
if (ret && ppath) { if (ret && ppath) {
*ppath = strdup(path); *ppath = path;
} else {
free(path);
} }
free(path);
if (ret) { if (ret) {
return ret; return ret;
} }
} }
// Fall back to legacy config file names // Fall back to config file in system config directory
const char *home = getenv("HOME"); auto config_dirs = xdgConfigDirectories(NULL);
if (home && strlen(home)) { for (int i = 0; config_dirs[i]; i++) {
auto path = ccalloc(strlen(home)+strlen(config_filename_legacy)+1, char); ret = open_config_file_at(config_dirs[i], ppath);
strcpy(path, home); if (ret) {
strcpy(path+strlen(home), config_filename_legacy); free((void *)config_dirs);
FILE *ret = fopen(path, "r"); return ret;
if (ret && ppath) }
*ppath = path;
else
free(path);
return ret;
} }
free((void *)config_dirs);
return NULL; return NULL;
} }