Merge pull request #3324 from orestisf1993/xdg_config

Correct XDG paths precedence for config files
This commit is contained in:
Ingo Bürk 2018-07-10 08:23:32 +02:00 committed by GitHub
commit b8505bb611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 33 deletions

View File

@ -506,11 +506,11 @@ int logical_px(const int logical);
char *resolve_tilde(const char *path); char *resolve_tilde(const char *path);
/** /**
* Get the path of the first configuration file found. If override_configpath * Get the path of the first configuration file found. If override_configpath is
* is specified, that path is returned and saved for further calls. Otherwise, * specified, that path is returned and saved for further calls. Otherwise,
* checks the home directory first, then the system directory first, always * checks the home directory first, then the system directory, always taking
* taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME, * into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
* $XDG_CONFIG_DIRS) * $XDG_CONFIG_DIRS).
* *
*/ */
char *get_config_path(const char *override_configpath, bool use_system_paths); char *get_config_path(const char *override_configpath, bool use_system_paths);

View File

@ -21,11 +21,11 @@ static bool path_exists(const char *path) {
} }
/* /*
* Get the path of the first configuration file found. If override_configpath * Get the path of the first configuration file found. If override_configpath is
* is specified, that path is returned and saved for further calls. Otherwise, * specified, that path is returned and saved for further calls. Otherwise,
* checks the home directory first, then the system directory first, always * checks the home directory first, then the system directory, always taking
* taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME, * into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
* $XDG_CONFIG_DIRS) * $XDG_CONFIG_DIRS).
* *
*/ */
char *get_config_path(const char *override_configpath, bool use_system_paths) { char *get_config_path(const char *override_configpath, bool use_system_paths) {
@ -38,40 +38,41 @@ char *get_config_path(const char *override_configpath, bool use_system_paths) {
return sstrdup(saved_configpath); return sstrdup(saved_configpath);
} }
if (saved_configpath != NULL) if (saved_configpath != NULL) {
return sstrdup(saved_configpath); return sstrdup(saved_configpath);
}
/* 1: check the traditional path under the home directory */ /* 1: check for $XDG_CONFIG_HOME/i3/config */
config_path = resolve_tilde("~/.i3/config"); if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
if (path_exists(config_path))
return config_path;
free(config_path);
/* 2: check for $XDG_CONFIG_HOME/i3/config */
if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
xdg_config_home = "~/.config"; xdg_config_home = "~/.config";
}
xdg_config_home = resolve_tilde(xdg_config_home); xdg_config_home = resolve_tilde(xdg_config_home);
sasprintf(&config_path, "%s/i3/config", xdg_config_home); sasprintf(&config_path, "%s/i3/config", xdg_config_home);
free(xdg_config_home); free(xdg_config_home);
if (path_exists(config_path)) if (path_exists(config_path)) {
return config_path; return config_path;
}
free(config_path);
/* 2: check the traditional path under the home directory */
config_path = resolve_tilde("~/.i3/config");
if (path_exists(config_path)) {
return config_path;
}
free(config_path); free(config_path);
/* The below paths are considered system-level, and can be skipped if the /* The below paths are considered system-level, and can be skipped if the
* caller only wants user-level configs. */ * caller only wants user-level configs. */
if (!use_system_paths) if (!use_system_paths) {
return NULL; return NULL;
}
/* 3: check the traditional path under /etc */ /* 3: check for $XDG_CONFIG_DIRS/i3/config */
config_path = SYSCONFDIR "/i3/config"; if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL) {
if (path_exists(config_path))
return sstrdup(config_path);
/* 4: check for $XDG_CONFIG_DIRS/i3/config */
if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
xdg_config_dirs = SYSCONFDIR "/xdg"; xdg_config_dirs = SYSCONFDIR "/xdg";
}
char *buf = sstrdup(xdg_config_dirs); char *buf = sstrdup(xdg_config_dirs);
char *tok = strtok(buf, ":"); char *tok = strtok(buf, ":");
@ -88,5 +89,11 @@ char *get_config_path(const char *override_configpath, bool use_system_paths) {
} }
free(buf); free(buf);
/* 4: check the traditional path under /etc */
config_path = SYSCONFDIR "/i3/config";
if (path_exists(config_path)) {
return sstrdup(config_path);
}
return NULL; return NULL;
} }

View File

@ -170,10 +170,10 @@ Exits i3.
When starting, i3 looks for configuration files in the following order: When starting, i3 looks for configuration files in the following order:
1. ~/.i3/config 1. ~/.config/i3/config (or $XDG_CONFIG_HOME/i3/config if set)
2. ~/.config/i3/config (or $XDG_CONFIG_HOME/i3/config if set) 2. ~/.i3/config
3. /etc/i3/config 3. /etc/xdg/i3/config (or $XDG_CONFIG_DIRS/i3/config if set)
4. /etc/xdg/i3/config (or $XDG_CONFIG_DIRS/i3/config if set) 4. /etc/i3/config
You can specify a custom path using the -c option. You can specify a custom path using the -c option.

View File

@ -49,7 +49,8 @@ bool parse_configuration(const char *override_configpath, bool use_nagbar) {
char *path = get_config_path(override_configpath, true); char *path = get_config_path(override_configpath, true);
if (path == NULL) { if (path == NULL) {
die("Unable to find the configuration file (looked at " die("Unable to find the configuration file (looked at "
"~/.i3/config, $XDG_CONFIG_HOME/i3/config, " SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)"); "$XDG_CONFIG_HOME/i3/config, ~/.i3/config, $XDG_CONFIG_DIRS/i3/config "
"and " SYSCONFDIR "/i3/config)");
} }
LOG("Parsing configfile %s\n", path); LOG("Parsing configfile %s\n", path);