Basic xdg directory implementation

Search $XDG_CONFIG_HOME and $XDG_CONFIG_DIRS for config files.
This also negates the need to have separate user and global variants of
mp_find_config_file()

Closes #864, #109.

Signed-off-by: wm4 <wm4@nowhere>
This commit is contained in:
Kenneth Zhou
2014-06-18 19:55:40 -04:00
committed by wm4
parent 8bb7d427e2
commit cb250d490c
11 changed files with 214 additions and 157 deletions

View File

@@ -43,100 +43,139 @@
#include "osdep/io.h" #include "osdep/io.h"
#include "osdep/path.h" #include "osdep/path.h"
typedef char *(*lookup_fun)(void *tctx, struct mpv_global *global, const char *);
static const lookup_fun config_lookup_functions[] = {
mp_find_user_config_file,
#if HAVE_COCOA
mp_get_macosx_bundled_path,
#endif
mp_find_global_config_file,
NULL
};
#define STRNULL(s) ((s) ? (s) : "(NULL)") #define STRNULL(s) ((s) ? (s) : "(NULL)")
static void mp_add_xdg_config_dirs(void *talloc_ctx, struct mpv_global *global,
char **dirs, int i)
{
const char *home = getenv("HOME");
const char *tmp = NULL;
tmp = getenv("XDG_CONFIG_HOME");
if (tmp && *tmp)
dirs[i++] = talloc_asprintf(talloc_ctx, "%s/mpv", tmp);
else if (home && *home)
dirs[i++] = talloc_asprintf(talloc_ctx, "%s/.config/mpv", home);
// Maintain compatibility with old ~/.mpv
if (home && *home)
dirs[i++] = talloc_asprintf(talloc_ctx, "%s/.mpv", home);
#if HAVE_COCOA
dirs[i++] = mp_get_macosx_bundle_dir(talloc_ctx);
#endif
tmp = getenv("XDG_CONFIG_DIRS");
if (tmp && *tmp) {
char *xdgdirs = talloc_strdup(talloc_ctx, tmp);
while (xdgdirs) {
char *dir = xdgdirs;
xdgdirs = strchr(xdgdirs, ':');
if (xdgdirs)
*xdgdirs++ = 0;
if (!dir[0])
continue;
dirs[i++] = talloc_asprintf(talloc_ctx, "%s%s", dir, "/mpv");
if (i + 1 >= MAX_CONFIG_PATHS) {
MP_WARN(global, "Too many config files, not reading any more\n");
break;
}
}
}
else {
dirs[i++] = MPLAYER_CONFDIR;
}
}
// Return NULL-terminated array of config directories, from highest to lowest
// priority
static char **mp_config_dirs(void *talloc_ctx, struct mpv_global *global)
{
char **ret = talloc_zero_array(talloc_ctx, char*, MAX_CONFIG_PATHS);
if (global->opts->force_configdir && global->opts->force_configdir[0]) {
ret[0] = talloc_strdup(talloc_ctx, global->opts->force_configdir);
return ret;
}
const char *tmp = NULL;
int i = 0;
tmp = getenv("MPV_HOME");
if (tmp && *tmp)
ret[i++] = talloc_strdup(talloc_ctx, tmp);
#if defined(_WIN32) && !defined(__CYGWIN__)
mp_add_win_config_dirs(talloc_ctx, global, ret, i);
#else
mp_add_xdg_config_dirs(talloc_ctx, global, ret, i);
#endif
MP_VERBOSE(global, "search dirs:");
for (char **c = ret; *c; c++)
MP_VERBOSE(global, " %s", *c);
MP_VERBOSE(global, "\n");
return ret;
}
char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global, char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global,
const char *filename) const char *filename)
{ {
struct MPOpts *opts = global->opts; struct MPOpts *opts = global->opts;
void *tmp = talloc_new(NULL);
char *res = NULL; char *res = NULL;
if (opts->load_config) { if (opts->load_config) {
if (opts->force_configdir && opts->force_configdir[0]) { for (char **dir = mp_config_dirs(tmp, global); *dir; dir++) {
// Always force the local config dir. char *config_file = talloc_asprintf(tmp, "%s/%s", *dir, filename);
res = mp_find_user_config_file(talloc_ctx, global, filename);
} else {
for (int i = 0; config_lookup_functions[i] != NULL; i++) {
res = config_lookup_functions[i](talloc_ctx, global, filename);
if (!res)
continue;
if (mp_path_exists(res)) if (mp_path_exists(config_file)) {
break; res = talloc_strdup(talloc_ctx, config_file);
break;
talloc_free(res);
res = NULL;
} }
} }
} }
MP_VERBOSE(global, "any config path: '%s' -> '%s'\n", STRNULL(filename),
talloc_free(tmp);
MP_VERBOSE(global, "config path: '%s' -> '%s'\n", STRNULL(filename),
STRNULL(res)); STRNULL(res));
return res; return res;
} }
char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global,
char *mp_find_user_config_file(void *talloc_ctx, struct mpv_global *global, const char *filename)
const char *filename)
{ {
struct MPOpts *opts = global->opts; struct MPOpts *opts = global->opts;
char *res = NULL; char **front = talloc_zero_array(talloc_ctx, char*, MAX_CONFIG_PATHS);
char **ret = front + (MAX_CONFIG_PATHS - 1);
if (opts->load_config) { if (opts->load_config) {
if (opts->force_configdir && opts->force_configdir[0]) { for (char **dir = mp_config_dirs(talloc_ctx, global); *dir; dir++) {
res = mp_path_join(talloc_ctx, bstr0(opts->force_configdir), char *config_file = talloc_asprintf(talloc_ctx, "%s/%s", *dir, filename);
bstr0(filename));
} else {
char *homedir = getenv("MPV_HOME");
char *configdir = NULL;
if (!homedir) { if (!mp_path_exists(config_file))
#ifdef _WIN32 continue;
res = talloc_steal(talloc_ctx, mp_get_win_config_path(filename));
#endif
homedir = getenv("HOME");
configdir = ".mpv";
}
if (!res && homedir) { *(--ret) = config_file;
char *temp = mp_path_join(NULL, bstr0(homedir), bstr0(configdir));
res = mp_path_join(talloc_ctx, bstr0(temp), bstr0(filename));
talloc_free(temp);
}
} }
} }
MP_VERBOSE(global, "user config path: '%s' -> '%s'\n", STRNULL(filename), MP_VERBOSE(global, "config file: '%s'\n", STRNULL(filename));
STRNULL(res));
return res;
}
char *mp_find_global_config_file(void *talloc_ctx, struct mpv_global *global, for (char** c = ret; *c; c++)
const char *filename) MP_VERBOSE(global, " -> '%s'\n", *c);
{
struct MPOpts *opts = global->opts;
char *res = NULL;
if (opts->load_config && !(opts->force_configdir && opts->force_configdir[0])) return ret;
{
if (filename) {
res = mp_path_join(talloc_ctx, bstr0(MPLAYER_CONFDIR), bstr0(filename));
} else {
res = talloc_strdup(talloc_ctx, MPLAYER_CONFDIR);
}
}
MP_VERBOSE(global, "global config path: '%s' -> '%s'\n", STRNULL(filename),
STRNULL(res));
return res;
} }
char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global, char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global,
@@ -152,7 +191,7 @@ char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global,
if (bstr_split_tok(bpath, "/", &prefix, &rest)) { if (bstr_split_tok(bpath, "/", &prefix, &rest)) {
const char *rest0 = rest.start; // ok in this case const char *rest0 = rest.start; // ok in this case
if (bstr_equals0(prefix, "~")) { if (bstr_equals0(prefix, "~")) {
res = mp_find_user_config_file(talloc_ctx, global, rest0); res = mp_find_config_file(talloc_ctx, global, rest0);
} else if (bstr_equals0(prefix, "")) { } else if (bstr_equals0(prefix, "")) {
res = mp_path_join(talloc_ctx, bstr0(getenv("HOME")), rest); res = mp_path_join(talloc_ctx, bstr0(getenv("HOME")), rest);
} }
@@ -281,14 +320,35 @@ bstr mp_split_proto(bstr path, bstr *out_url)
return r; return r;
} }
void mp_mkdirp(const char *dir)
{
void *tmp = talloc_new(NULL);
char *path = talloc_strdup(tmp, dir);
char *cdir = path + 1;
while (cdir) {
cdir = strchr(cdir, '/');
if (cdir)
*cdir = 0;
mkdir(path, 0700);
if (cdir)
*cdir++ = '/';
}
talloc_free(tmp);
}
void mp_mk_config_dir(struct mpv_global *global, char *subdir) void mp_mk_config_dir(struct mpv_global *global, char *subdir)
{ {
void *tmp = talloc_new(NULL); void *tmp = talloc_new(NULL);
char *confdir = mp_find_user_config_file(tmp, global, ""); char *dir = mp_config_dirs(tmp, global)[0];
if (confdir) {
if (subdir) if (dir) {
confdir = mp_path_join(tmp, bstr0(confdir), bstr0(subdir)); dir = talloc_asprintf(tmp, "%s/%s", dir, subdir);
mkdir(confdir, 0777); mp_mkdirp(dir);
} }
talloc_free(tmp); talloc_free(tmp);
} }

View File

@@ -32,13 +32,10 @@ struct mpv_global;
char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global, char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global,
const char *filename); const char *filename);
// Search for the input filename in the global configuration location. // Find all instances of the given config file. Paths are returned in order
char *mp_find_global_config_file(void *talloc_ctx, struct mpv_global *global, // from lowest to highest priority.
const char *filename); char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global,
const char *filename);
// Search for the input filename in the user configuration location.
char *mp_find_user_config_file(void *talloc_ctx, struct mpv_global *global,
const char *filename);
// Normally returns a talloc_strdup'ed copy of the path, except for special // Normally returns a talloc_strdup'ed copy of the path, except for special
// paths starting with '~'. Used to allow the user explicitly reference a // paths starting with '~'. Used to allow the user explicitly reference a
@@ -77,6 +74,7 @@ bool mp_is_url(bstr path);
bstr mp_split_proto(bstr path, bstr *out_url); bstr mp_split_proto(bstr path, bstr *out_url);
void mp_mkdirp(const char *dir);
void mp_mk_config_dir(struct mpv_global *global, char *subdir); void mp_mk_config_dir(struct mpv_global *global, char *subdir);
#endif /* MPLAYER_PATH_H */ #endif /* MPLAYER_PATH_H */

View File

@@ -20,12 +20,11 @@
#include "options/path.h" #include "options/path.h"
#include "osdep/path.h" #include "osdep/path.h"
char *mp_get_macosx_bundled_path(void *talloc_ctx, struct mpv_global *global, char *mp_get_macosx_bundle_dir(void *talloc_ctx)
const char *file)
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *path = [[NSBundle mainBundle] resourcePath]; NSString *path = [[NSBundle mainBundle] resourcePath];
char *rv = mp_path_join(talloc_ctx, bstr0([path UTF8String]), bstr0(file)); char *rv = talloc_strdup(talloc_ctx, [path UTF8String]);
[pool release]; [pool release];
return rv; return rv;
} }

View File

@@ -24,55 +24,48 @@
// Warning: do not use PATH_MAX. Cygwin messed it up. // Warning: do not use PATH_MAX. Cygwin messed it up.
static void get_exe_dir(wchar_t path[MAX_PATH + 1]) char *mp_get_win_exe_dir(void *talloc_ctx)
{ {
int len = (int)GetModuleFileNameW(NULL, path, MAX_PATH); wchar_t w_exedir[MAX_PATH + 1] = {0};
int len = (int)GetModuleFileNameW(NULL, w_exedir, MAX_PATH);
int imax = 0; int imax = 0;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
if (path[i] == '\\') { if (w_exedir[i] == '\\') {
path[i] = '/'; w_exedir[i] = '/';
imax = i; imax = i;
} }
} }
path[imax] = '\0'; w_exedir[imax] = '\0';
return mp_to_utf8(talloc_ctx, w_exedir);
}
char *mp_get_win_exe_subdir(void *talloc_ctx)
{
return talloc_asprintf(talloc_ctx, "%s/mpv", mp_get_win_exe_dir(talloc_ctx));
} }
char *mp_get_win_config_path(const char *filename) char *mp_get_win_app_dir(void *talloc_ctx)
{ {
wchar_t w_appdir[MAX_PATH + 1] = {0}; wchar_t w_appdir[MAX_PATH + 1] = {0};
wchar_t w_exedir[MAX_PATH + 1] = {0};
char *res = NULL;
void *tmp = talloc_new(NULL);
#ifndef __CYGWIN__
if (SHGetFolderPathW(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE, NULL, if (SHGetFolderPathW(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE, NULL,
SHGFP_TYPE_CURRENT, w_appdir) != S_OK) SHGFP_TYPE_CURRENT, w_appdir) != S_OK)
w_appdir[0] = '\0'; return NULL;
#endif
get_exe_dir(w_exedir); return talloc_asprintf(talloc_ctx, "%s/mpv", mp_to_utf8(talloc_ctx, w_appdir));
}
if (filename && filename[0] && w_exedir[0]) {
char *dir = mp_to_utf8(tmp, w_exedir);
char *temp = mp_path_join(tmp, bstr0(dir), bstr0("mpv"));
res = mp_path_join(NULL, bstr0(temp), bstr0(filename)); void mp_add_win_config_dirs(void *talloc_ctx, struct mpv_global *global,
if (!mp_path_exists(res) || mp_path_isdir(res)) { char **dirs, int i)
talloc_free(res); {
res = mp_path_join(NULL, bstr0(dir), bstr0(filename)); if ((dirs[i] = mp_get_win_exe_subdir(talloc_ctx)))
if (!mp_path_exists(res) || mp_path_isdir(res)) { i++;
talloc_free(res); if ((dirs[i] = mp_get_win_exe_dir(talloc_ctx)))
res = NULL; i++;
} if ((dirs[i] = mp_get_win_app_dir(talloc_ctx)))
} i++;
}
if (!res && w_appdir[0]) {
char *dir = mp_to_utf8(tmp, w_appdir);
char *temp = mp_path_join(tmp, bstr0(dir), bstr0("mpv"));
res = mp_path_join(NULL, bstr0(temp), bstr0(filename));
}
talloc_free(tmp);
return res;
} }

View File

@@ -1,12 +1,19 @@
#ifndef OSDEP_PATH_H #ifndef OSDEP_PATH_H
#define OSDEP_PATH_H #define OSDEP_PATH_H
#define MAX_CONFIG_PATHS 32
struct mpv_global; struct mpv_global;
char *mp_get_win_config_path(const char *filename); // Windows config directories
char *mp_get_win_exe_dir(void *talloc_ctx);
char *mp_get_win_exe_subdir(void *talloc_ctx);
char *mp_get_win_app_dir(void *talloc_ctx);
// Returns absolute path of a resource file in a Mac OS X application bundle. void mp_add_win_config_dirs(void *talloc_ctx, struct mpv_global *global,
char *mp_get_macosx_bundled_path(void *talloc_ctx, struct mpv_global *global, char **dirs, int i);
const char *filename);
// Returns Mac OS X application bundle directory.
char *mp_get_macosx_bundle_dir(void *talloc_ctx);
#endif #endif

View File

@@ -71,21 +71,22 @@ bool mp_parse_cfgfiles(struct MPContext *mpctx)
// The #if is a stupid hack to avoid errors if libavfilter is not available. // The #if is a stupid hack to avoid errors if libavfilter is not available.
#if HAVE_LIBAVFILTER && HAVE_ENCODING #if HAVE_LIBAVFILTER && HAVE_ENCODING
conffile = mp_find_config_file(tmp, mpctx->global, "encoding-profiles.conf"); conffile = mp_find_config_file(tmp, mpctx->global, "encoding-profiles.conf");
if (conffile && mp_path_exists(conffile)) if (conffile)
m_config_parse_config_file(mpctx->mconfig, conffile, SECT_ENCODE, 0); m_config_parse_config_file(mpctx->mconfig, conffile, SECT_ENCODE, 0);
#endif #endif
conffile = mp_find_global_config_file(tmp, mpctx->global, "mpv.conf"); // Maintain compatibility with /config
if (conffile && m_config_parse_config_file(conf, conffile, section, 0) < 0) { for (char** cf = mp_find_all_config_files(tmp, mpctx->global, "config"); *cf; cf++) {
r = false; if (m_config_parse_config_file(conf, *cf, section, 0) < 0) {
goto done; r = false;
goto done;
}
} }
mp_mk_config_dir(mpctx->global, NULL); for (char** cf = mp_find_all_config_files(tmp, mpctx->global, "mpv.conf"); *cf; cf++) {
if (!(conffile = mp_find_user_config_file(tmp, mpctx->global, "config"))) if (m_config_parse_config_file(conf, *cf, section, 0) < 0) {
MP_ERR(mpctx, "mp_find_user_config_file(\"config\") problem\n"); r = false;
else if (m_config_parse_config_file(conf, conffile, section, 0) < 0) { goto done;
r = false; }
goto done;
} }
if (encoding) if (encoding)
@@ -134,7 +135,7 @@ static void mp_load_per_file_config(struct MPContext *mpctx)
return; return;
} }
if ((confpath = mp_find_user_config_file(NULL, mpctx->global, name))) { if ((confpath = mp_find_config_file(NULL, mpctx->global, name))) {
try_load_config(mpctx, confpath, FILE_LOCAL_FLAGS); try_load_config(mpctx, confpath, FILE_LOCAL_FLAGS);
talloc_free(confpath); talloc_free(confpath);
@@ -200,9 +201,13 @@ static char *mp_get_playback_resume_config_filename(struct mpv_global *global,
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
conf = talloc_asprintf_append(conf, "%02X", md5[i]); conf = talloc_asprintf_append(conf, "%02X", md5[i]);
conf = talloc_asprintf(tmp, "%s/%s", MP_WATCH_LATER_CONF, conf); res = talloc_asprintf(tmp, MP_WATCH_LATER_CONF "/%s", conf);
res = mp_find_config_file(NULL, global, res);
res = mp_find_user_config_file(NULL, global, conf); if (!res) {
res = mp_find_config_file(tmp, global, MP_WATCH_LATER_CONF);
res = talloc_asprintf(NULL, "%s/%s", res, conf);
}
exit: exit:
talloc_free(tmp); talloc_free(tmp);

View File

@@ -343,7 +343,7 @@ static int script_find_config_file(lua_State *L)
{ {
struct MPContext *mpctx = get_mpctx(L); struct MPContext *mpctx = get_mpctx(L);
const char *s = luaL_checkstring(L, 1); const char *s = luaL_checkstring(L, 1);
char *path = mp_find_user_config_file(NULL, mpctx->global, s); char *path = mp_find_config_file(NULL, mpctx->global, s);
if (path) { if (path) {
lua_pushstring(L, path); lua_pushstring(L, path);
} else { } else {

View File

@@ -167,11 +167,11 @@ void mp_load_scripts(struct MPContext *mpctx)
} }
if (!mpctx->opts->auto_load_scripts) if (!mpctx->opts->auto_load_scripts)
return; return;
// Load ~/.mpv/lua/*
// Load all lua scripts
void *tmp = talloc_new(NULL); void *tmp = talloc_new(NULL);
char *script_path = mp_find_user_config_file(tmp, mpctx->global, "lua"); for (char **luadir = mp_find_all_config_files(tmp, mpctx->global, "lua"); *luadir; luadir++) {
if (script_path) { files = list_script_files(tmp, *luadir);
files = list_script_files(tmp, script_path);
for (int n = 0; files && files[n]; n++) for (int n = 0; files && files[n]; n++)
mp_load_script(mpctx, files[n]); mp_load_script(mpctx, files[n]);
} }

View File

@@ -750,29 +750,24 @@ dvb_config_t *dvb_get_config(stream_t *stream)
} }
void *talloc_ctx = talloc_new(NULL); void *talloc_ctx = talloc_new(NULL);
conf_file = mp_find_user_config_file(talloc_ctx, global, "channels.conf"); conf_file = mp_find_config_file(talloc_ctx, global, "channels.conf");
switch(type) { switch(type) {
case TUNER_TER: case TUNER_TER:
conf_file = mp_find_user_config_file(talloc_ctx, global, "channels.conf.ter"); conf_file = mp_find_config_file(talloc_ctx, global, "channels.conf.ter");
break; break;
case TUNER_CBL: case TUNER_CBL:
conf_file = mp_find_user_config_file(talloc_ctx, global, "channels.conf.cbl"); conf_file = mp_find_config_file(talloc_ctx, global, "channels.conf.cbl");
break; break;
case TUNER_SAT: case TUNER_SAT:
conf_file = mp_find_user_config_file(talloc_ctx, global, "channels.conf.sat"); conf_file = mp_find_config_file(talloc_ctx, global, "channels.conf.sat");
break; break;
case TUNER_ATSC: case TUNER_ATSC:
conf_file = mp_find_user_config_file(talloc_ctx, global, "channels.conf.atsc"); conf_file = mp_find_config_file(talloc_ctx, global, "channels.conf.atsc");
break; break;
} }
if(conf_file && (access(conf_file, F_OK | R_OK) != 0)) { if(conf_file && (access(conf_file, F_OK | R_OK) != 0))
conf_file = mp_find_user_config_file(talloc_ctx, global, "channels.conf"); conf_file = mp_find_config_file(talloc_ctx, global, "channels.conf");
if(conf_file && (access(conf_file, F_OK | R_OK) != 0)) {
conf_file = mp_find_global_config_file(talloc_ctx, global, "channels.conf");
}
}
list = dvb_get_channels(log, conf_file, type); list = dvb_get_channels(log, conf_file, type);
talloc_free(talloc_ctx); talloc_free(talloc_ctx);

View File

@@ -177,7 +177,7 @@ void mp_ass_configure_fonts(ASS_Renderer *priv, struct osd_style_opts *opts,
struct mpv_global *global, struct mp_log *log) struct mpv_global *global, struct mp_log *log)
{ {
void *tmp = talloc_new(NULL); void *tmp = talloc_new(NULL);
char *default_font = mp_find_user_config_file(tmp, global, "subfont.ttf"); char *default_font = mp_find_config_file(tmp, global, "subfont.ttf");
char *config = mp_find_config_file(tmp, global, "fonts.conf"); char *config = mp_find_config_file(tmp, global, "fonts.conf");
if (default_font && !mp_path_exists(default_font)) if (default_font && !mp_path_exists(default_font))
@@ -249,7 +249,7 @@ static void message_callback(int level, const char *format, va_list va, void *ct
ASS_Library *mp_ass_init(struct mpv_global *global, struct mp_log *log) ASS_Library *mp_ass_init(struct mpv_global *global, struct mp_log *log)
{ {
char *path = mp_find_user_config_file(NULL, global, "fonts"); char *path = mp_find_config_file(NULL, global, "fonts");
ASS_Library *priv = ass_library_init(); ASS_Library *priv = ass_library_init();
if (!priv) if (!priv)
abort(); abort();

View File

@@ -242,7 +242,7 @@ struct subfn *find_text_subtitles(struct mpv_global *global, const char *fname)
} }
// Load subtitles in ~/.mpv/sub limiting sub fuzziness // Load subtitles in ~/.mpv/sub limiting sub fuzziness
char *mp_subdir = mp_find_user_config_file(NULL, global, "sub/"); char *mp_subdir = mp_find_config_file(NULL, global, "sub/");
if (mp_subdir) if (mp_subdir)
append_dir_subtitles(global, &slist, &n, bstr0(mp_subdir), fname, 1); append_dir_subtitles(global, &slist, &n, bstr0(mp_subdir), fname, 1);
talloc_free(mp_subdir); talloc_free(mp_subdir);