path: expand internal path selection API to allow for additional types

Currently, nothing new is actually implemented but the idea is simply to
just pass a type string all the way up from mp_find_user_file down to
actually getting the platform path. This allows for selecting different
directories besides the user's native config directory. See the next
commit for an implementation.
This commit is contained in:
Dudemanguy 2022-11-07 18:35:43 -06:00
parent baa9d56481
commit 5158b85b21
3 changed files with 16 additions and 15 deletions

View File

@ -102,15 +102,15 @@ static const char *mp_get_platform_path(void *talloc_ctx,
return NULL;
}
char *mp_find_user_config_file(void *talloc_ctx, struct mpv_global *global,
const char *filename)
char *mp_find_user_file(void *talloc_ctx, struct mpv_global *global,
const char *type, const char *filename)
{
void *tmp = talloc_new(NULL);
char *res = (char *)mp_get_platform_path(tmp, global, config_dirs[0]);
char *res = (char *)mp_get_platform_path(tmp, global, type);
if (res)
res = mp_path_join(talloc_ctx, res, filename);
talloc_free(tmp);
MP_DBG(global, "config path: '%s' -> '%s'\n", filename, res ? res : "-");
MP_DBG(global, "path: '%s' -> '%s'\n", filename, res ? res : "-");
return res;
}
@ -378,9 +378,9 @@ void mp_mkdirp(const char *dir)
talloc_free(path);
}
void mp_mk_config_dir(struct mpv_global *global, char *subdir)
void mp_mk_user_dir(struct mpv_global *global, const char *type, char *subdir)
{
char *dir = mp_find_user_config_file(NULL, global, subdir);
char *dir = mp_find_user_file(NULL, global, type, subdir);
if (dir)
mp_mkdirp(dir);
talloc_free(dir);

View File

@ -34,11 +34,12 @@ void mp_init_paths(struct mpv_global *global, struct MPOpts *opts);
char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global,
const char *filename);
// Like mp_find_config_file(), but search only the local writable user config
// dir. Also, this returns a result even if the file does not exist. Calling
// it with filename="" is equivalent to retrieving the user config dir.
char *mp_find_user_config_file(void *talloc_ctx, struct mpv_global *global,
const char *filename);
// Search for local writable user files within a specific kind of user dir
// as documented in osdep/path.h. This returns a result even if the file does
// not exist. Calling it with filename="" is equivalent to retrieving the path
// to the dir.
char *mp_find_user_file(void *talloc_ctx, struct mpv_global *global,
const char *type, const char *filename);
// Find all instances of the given config file. Paths are returned in order
// from lowest to highest priority. filename can contain multiple names
@ -90,6 +91,6 @@ bool mp_is_url(bstr path);
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_user_dir(struct mpv_global *global, const char *type, char *subdir);
#endif /* MPLAYER_PATH_H */

View File

@ -63,7 +63,7 @@ void mp_parse_cfgfiles(struct MPContext *mpctx)
{
struct MPOpts *opts = mpctx->opts;
mp_mk_config_dir(mpctx->global, "");
mp_mk_user_dir(mpctx->global, "home", "");
char *p1 = mp_get_user_path(NULL, mpctx->global, "~~home/");
char *p2 = mp_get_user_path(NULL, mpctx->global, "~~old_home/");
@ -226,7 +226,7 @@ static char *mp_get_playback_resume_config_filename(struct MPContext *mpctx,
if (!mpctx->cached_watch_later_configdir) {
mpctx->cached_watch_later_configdir =
mp_find_user_config_file(mpctx, mpctx->global, MP_WATCH_LATER_CONF);
mp_find_user_file(mpctx, mpctx->global, "home", MP_WATCH_LATER_CONF);
}
if (mpctx->cached_watch_later_configdir)
@ -292,7 +292,7 @@ void mp_write_watch_later_conf(struct MPContext *mpctx)
if (!conffile)
goto exit;
mp_mk_config_dir(mpctx->global, mpctx->cached_watch_later_configdir);
mp_mk_user_dir(mpctx->global, "home", mpctx->cached_watch_later_configdir);
MP_INFO(mpctx, "Saving state.\n");