2004-07-23 16:35:20 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2010-01-30 23:24:23 +00:00
|
|
|
*
|
2017-06-12 14:41:21 +00:00
|
|
|
* Get path to config dir/file.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-01-30 23:24:23 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2010-01-30 23:24:23 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-12 14:41:21 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2010-01-30 23:24:23 +00:00
|
|
|
*
|
2017-06-12 14:41:21 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2004-07-23 16:35:20 +00:00
|
|
|
*/
|
2007-08-28 11:20:24 +00:00
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
#include <assert.h>
|
2007-08-28 11:20:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-02-23 14:18:09 +00:00
|
|
|
#include <stdbool.h>
|
2012-02-03 07:05:11 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
core: add playback resume feature (manual/opt-in)
A "watch later" command is now mapped to Shift+Q. This quits the player
and stores the playback state in a config file in ~/.mpv/watch_later/.
When calling the player with the same file again, playback is resumed
at that time position.
It's also possible to make mpv save playback state always on quit with
the --save-position-on-quit option. Likewise, resuming can be disabled
with the --no-resume-playback option.
This also attempts to save some playback parameters, like fullscreen
state or track selection. This will unconditionally override config
settings and command line options (which is probably not what you would
expect, but in general nobody will really care about this). Some things
are not backed up, because that would cause various problems. Additional
subtitle files, video filters, etc. are not stored because that would be
too hard and fragile. Volume/mute state are not stored because it would
mess up if the system mixer is used, or if the system mixer was
readjusted in the meantime.
Basically, the tradeoff between perfect state restoration and
complexity/fragility makes it not worth to attempt to implement
it perfectly, even if the result is a little bit inconsistent.
2013-05-05 17:37:29 +00:00
|
|
|
#include <errno.h>
|
2014-02-14 12:52:59 +00:00
|
|
|
|
2010-02-18 09:29:05 +00:00
|
|
|
#include "config.h"
|
2014-02-14 12:52:59 +00:00
|
|
|
|
2014-06-26 16:31:28 +00:00
|
|
|
#include "common/common.h"
|
2013-12-21 19:45:19 +00:00
|
|
|
#include "common/global.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-02-14 12:52:59 +00:00
|
|
|
#include "options/options.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/path.h"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2012-12-09 14:05:21 +00:00
|
|
|
#include "osdep/io.h"
|
2013-09-18 16:42:18 +00:00
|
|
|
#include "osdep/path.h"
|
2018-12-31 17:31:57 +00:00
|
|
|
#include "misc/ctype.h"
|
2008-02-24 12:41:51 +00:00
|
|
|
|
2015-05-09 11:50:47 +00:00
|
|
|
// In order of decreasing priority: the first has highest priority.
|
2015-05-01 19:13:44 +00:00
|
|
|
static const mp_get_platform_path_cb path_resolvers[] = {
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_COCOA
|
2015-05-01 19:13:44 +00:00
|
|
|
mp_get_platform_path_osx,
|
2012-12-09 14:05:21 +00:00
|
|
|
#endif
|
2015-05-01 19:13:44 +00:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
mp_get_platform_path_unix,
|
|
|
|
#endif
|
2017-06-27 11:50:58 +00:00
|
|
|
#if HAVE_UWP
|
|
|
|
mp_get_platform_path_uwp,
|
|
|
|
#elif defined(_WIN32)
|
2015-05-01 19:13:44 +00:00
|
|
|
mp_get_platform_path_win,
|
|
|
|
#endif
|
|
|
|
};
|
2012-12-09 14:05:21 +00:00
|
|
|
|
2015-05-09 11:50:47 +00:00
|
|
|
// from highest (most preferred) to lowest priority
|
|
|
|
static const char *const config_dirs[] = {
|
|
|
|
"home",
|
|
|
|
"old_home",
|
|
|
|
"osxbundle",
|
|
|
|
"global",
|
|
|
|
};
|
|
|
|
|
2018-05-21 13:49:19 +00:00
|
|
|
void mp_init_paths(struct mpv_global *global, struct MPOpts *opts)
|
|
|
|
{
|
|
|
|
TA_FREEP(&global->configdir);
|
|
|
|
|
|
|
|
const char *force_configdir = getenv("MPV_HOME");
|
|
|
|
if (opts->force_configdir && opts->force_configdir[0])
|
|
|
|
force_configdir = opts->force_configdir;
|
|
|
|
if (!opts->load_config)
|
|
|
|
force_configdir = "";
|
|
|
|
|
|
|
|
global->configdir = talloc_strdup(global, force_configdir);
|
|
|
|
}
|
|
|
|
|
2015-05-09 11:50:47 +00:00
|
|
|
// Return a platform specific path using a path type as defined in osdep/path.h.
|
|
|
|
// Keep in mind that the only way to free the return value is freeing talloc_ctx
|
|
|
|
// (or its children), as this function can return a statically allocated string.
|
|
|
|
static const char *mp_get_platform_path(void *talloc_ctx,
|
|
|
|
struct mpv_global *global,
|
|
|
|
const char *type)
|
2015-05-01 19:13:44 +00:00
|
|
|
{
|
2015-05-09 11:50:47 +00:00
|
|
|
assert(talloc_ctx);
|
|
|
|
|
2018-05-21 13:49:19 +00:00
|
|
|
if (global->configdir) {
|
2015-05-09 11:50:47 +00:00
|
|
|
for (int n = 0; n < MP_ARRAY_SIZE(config_dirs); n++) {
|
|
|
|
if (strcmp(config_dirs[n], type) == 0)
|
2018-05-21 13:49:19 +00:00
|
|
|
return (n == 0 && global->configdir[0]) ? global->configdir : NULL;
|
2015-05-09 11:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 19:13:44 +00:00
|
|
|
for (int n = 0; n < MP_ARRAY_SIZE(path_resolvers); n++) {
|
|
|
|
const char *path = path_resolvers[n](talloc_ctx, type);
|
|
|
|
if (path && path[0])
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
return NULL;
|
2012-12-09 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 14:21:44 +00:00
|
|
|
char *mp_find_user_config_file(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
char *res = (char *)mp_get_platform_path(tmp, global, config_dirs[0]);
|
|
|
|
if (res)
|
|
|
|
res = mp_path_join(talloc_ctx, res, filename);
|
|
|
|
talloc_free(tmp);
|
2017-09-28 09:53:57 +00:00
|
|
|
MP_DBG(global, "config path: '%s' -> '%s'\n", filename, res ? res : "-");
|
2015-05-09 14:21:44 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-09 11:50:47 +00:00
|
|
|
static char **mp_find_all_config_files_limited(void *talloc_ctx,
|
|
|
|
struct mpv_global *global,
|
|
|
|
int max_files,
|
|
|
|
const char *filename)
|
2012-12-09 14:05:21 +00:00
|
|
|
{
|
2015-05-09 11:50:47 +00:00
|
|
|
char **ret = talloc_array(talloc_ctx, char*, 2); // 2 preallocated
|
2014-06-26 16:31:28 +00:00
|
|
|
int num_ret = 0;
|
2014-06-18 23:55:40 +00:00
|
|
|
|
2015-05-09 11:50:47 +00:00
|
|
|
for (int i = 0; i < MP_ARRAY_SIZE(config_dirs); i++) {
|
|
|
|
const char *dir = mp_get_platform_path(ret, global, config_dirs[i]);
|
2015-02-15 13:28:49 +00:00
|
|
|
bstr s = bstr0(filename);
|
2015-05-09 11:50:47 +00:00
|
|
|
while (dir && num_ret < max_files && s.len) {
|
2015-02-15 13:28:49 +00:00
|
|
|
bstr fn;
|
|
|
|
bstr_split_tok(s, "|", &fn, &s);
|
|
|
|
|
2015-05-09 14:25:53 +00:00
|
|
|
char *file = mp_path_join_bstr(ret, bstr0(dir), fn);
|
2015-05-09 14:43:22 +00:00
|
|
|
if (mp_path_exists(file)) {
|
2017-09-28 09:53:57 +00:00
|
|
|
MP_DBG(global, "config path: '%.*s' -> '%s'\n",
|
|
|
|
BSTR_P(fn), file);
|
2015-05-09 11:50:47 +00:00
|
|
|
MP_TARRAY_APPEND(NULL, ret, num_ret, file);
|
2015-05-09 14:43:22 +00:00
|
|
|
} else {
|
2017-09-28 09:53:57 +00:00
|
|
|
MP_DBG(global, "config path: '%.*s' -/-> '%s'\n",
|
|
|
|
BSTR_P(fn), file);
|
2015-05-09 14:43:22 +00:00
|
|
|
}
|
2015-02-15 13:28:49 +00:00
|
|
|
}
|
2012-12-09 14:05:21 +00:00
|
|
|
}
|
2015-05-09 11:50:47 +00:00
|
|
|
|
|
|
|
MP_TARRAY_GROW(NULL, ret, num_ret);
|
|
|
|
ret[num_ret] = NULL;
|
2014-02-25 20:19:18 +00:00
|
|
|
|
2014-06-26 16:31:28 +00:00
|
|
|
for (int n = 0; n < num_ret / 2; n++)
|
|
|
|
MPSWAP(char*, ret[n], ret[num_ret - n - 1]);
|
2014-06-18 23:55:40 +00:00
|
|
|
return ret;
|
2012-12-09 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 11:50:47 +00:00
|
|
|
char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
return mp_find_all_config_files_limited(talloc_ctx, global, 64, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
char **l = mp_find_all_config_files_limited(talloc_ctx, global, 1, filename);
|
|
|
|
char *r = l && l[0] ? talloc_steal(talloc_ctx, l[0]) : NULL;
|
|
|
|
talloc_free(l);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *path)
|
2013-12-14 18:50:00 +00:00
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
return NULL;
|
2014-02-25 20:19:18 +00:00
|
|
|
char *res = NULL;
|
2013-12-14 18:50:00 +00:00
|
|
|
bstr bpath = bstr0(path);
|
|
|
|
if (bstr_eatstart0(&bpath, "~")) {
|
|
|
|
// parse to "~" <prefix> "/" <rest>
|
|
|
|
bstr prefix, rest;
|
|
|
|
if (bstr_split_tok(bpath, "/", &prefix, &rest)) {
|
|
|
|
const char *rest0 = rest.start; // ok in this case
|
2013-12-21 19:45:19 +00:00
|
|
|
if (bstr_equals0(prefix, "~")) {
|
2014-06-18 23:55:40 +00:00
|
|
|
res = mp_find_config_file(talloc_ctx, global, rest0);
|
2016-09-29 14:44:01 +00:00
|
|
|
if (!res) {
|
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
const char *p = mp_get_platform_path(tmp, global, "home");
|
|
|
|
res = mp_path_join_bstr(talloc_ctx, bstr0(p), rest);
|
|
|
|
talloc_free(tmp);
|
|
|
|
}
|
2013-12-21 19:45:19 +00:00
|
|
|
} else if (bstr_equals0(prefix, "")) {
|
2017-07-06 14:26:24 +00:00
|
|
|
char *home = getenv("HOME");
|
|
|
|
if (!home)
|
|
|
|
home = getenv("USERPROFILE");
|
|
|
|
res = mp_path_join_bstr(talloc_ctx, bstr0(home), rest);
|
2015-05-03 12:52:11 +00:00
|
|
|
} else if (bstr_eatstart0(&prefix, "~")) {
|
2015-05-09 11:50:47 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
2015-05-01 19:27:33 +00:00
|
|
|
char type[80];
|
|
|
|
snprintf(type, sizeof(type), "%.*s", BSTR_P(prefix));
|
2015-05-09 11:50:47 +00:00
|
|
|
const char *p = mp_get_platform_path(tmp, global, type);
|
2015-05-09 13:26:47 +00:00
|
|
|
res = mp_path_join_bstr(talloc_ctx, bstr0(p), rest);
|
2015-05-09 11:50:47 +00:00
|
|
|
talloc_free(tmp);
|
2013-12-21 19:45:19 +00:00
|
|
|
}
|
2013-12-14 18:50:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 20:19:18 +00:00
|
|
|
if (!res)
|
|
|
|
res = talloc_strdup(talloc_ctx, path);
|
2017-09-28 09:53:57 +00:00
|
|
|
MP_DBG(global, "user path: '%s' -> '%s'\n", path, res);
|
2014-02-25 20:19:18 +00:00
|
|
|
return res;
|
2013-12-14 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 14:18:09 +00:00
|
|
|
char *mp_basename(const char *path)
|
2010-11-16 21:06:52 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
s = strrchr(path, '\\');
|
|
|
|
if (s)
|
|
|
|
path = s + 1;
|
|
|
|
s = strrchr(path, ':');
|
|
|
|
if (s)
|
|
|
|
path = s + 1;
|
|
|
|
#endif
|
|
|
|
s = strrchr(path, '/');
|
2011-02-23 14:18:09 +00:00
|
|
|
return s ? s + 1 : (char *)path;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr mp_dirname(const char *path)
|
|
|
|
{
|
2012-12-10 20:18:46 +00:00
|
|
|
struct bstr ret = {
|
|
|
|
(uint8_t *)path, mp_basename(path) - path
|
|
|
|
};
|
2011-02-23 14:18:09 +00:00
|
|
|
if (ret.len == 0)
|
2012-07-28 21:47:42 +00:00
|
|
|
return bstr0(".");
|
2011-02-23 14:18:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:40:55 +00:00
|
|
|
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
static const char mp_path_separators[] = "\\/";
|
|
|
|
#else
|
|
|
|
static const char mp_path_separators[] = "/";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Mutates path and removes a trailing '/' (or '\' on Windows)
|
|
|
|
void mp_path_strip_trailing_separator(char *path)
|
|
|
|
{
|
|
|
|
size_t len = strlen(path);
|
|
|
|
if (len > 0 && strchr(mp_path_separators, path[len - 1]))
|
|
|
|
path[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
2013-07-08 18:34:26 +00:00
|
|
|
char *mp_splitext(const char *path, bstr *root)
|
|
|
|
{
|
|
|
|
assert(path);
|
|
|
|
const char *split = strrchr(path, '.');
|
2013-12-22 22:04:19 +00:00
|
|
|
if (!split || !split[1] || strchr(split, '/'))
|
|
|
|
return NULL;
|
2013-07-08 18:34:26 +00:00
|
|
|
if (root)
|
2013-12-22 22:04:19 +00:00
|
|
|
*root = (bstr){(char *)path, split - path};
|
|
|
|
return (char *)split + 1;
|
2013-07-08 18:34:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:14:35 +00:00
|
|
|
bool mp_path_is_absolute(struct bstr path)
|
2011-02-23 14:18:09 +00:00
|
|
|
{
|
2020-02-06 13:14:35 +00:00
|
|
|
if (path.len && strchr(mp_path_separators, path.start[0]))
|
|
|
|
return true;
|
2011-02-23 14:18:09 +00:00
|
|
|
|
|
|
|
#if HAVE_DOS_PATHS
|
2020-02-06 13:06:53 +00:00
|
|
|
// Note: "X:filename" is a path relative to the current working directory
|
|
|
|
// of drive X, and thus is not an absolute path. It needs to be
|
|
|
|
// followed by \ or /.
|
2020-02-06 13:14:35 +00:00
|
|
|
if (path.len >= 3 && path.start[1] == ':' &&
|
|
|
|
strchr(mp_path_separators, path.start[2]))
|
|
|
|
return true;
|
2011-02-23 14:18:09 +00:00
|
|
|
#endif
|
2020-02-06 13:14:35 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *mp_path_join_bstr(void *talloc_ctx, struct bstr p1, struct bstr p2)
|
|
|
|
{
|
|
|
|
if (p1.len == 0)
|
|
|
|
return bstrdup0(talloc_ctx, p2);
|
|
|
|
if (p2.len == 0)
|
|
|
|
return bstrdup0(talloc_ctx, p1);
|
|
|
|
|
|
|
|
if (mp_path_is_absolute(p2))
|
2020-02-06 13:06:53 +00:00
|
|
|
return bstrdup0(talloc_ctx, p2);
|
2011-02-23 14:18:09 +00:00
|
|
|
|
2020-02-06 13:06:53 +00:00
|
|
|
bool have_separator = strchr(mp_path_separators, p1.start[p1.len - 1]);
|
2011-02-23 14:18:09 +00:00
|
|
|
#if HAVE_DOS_PATHS
|
2020-02-06 13:06:53 +00:00
|
|
|
// "X:" only => path relative to "X:" current working directory.
|
|
|
|
if (p1.len == 2 && p1.start[1] == ':')
|
|
|
|
have_separator = true;
|
2011-02-23 14:18:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),
|
|
|
|
have_separator ? "" : "/", BSTR_P(p2));
|
2010-11-16 21:06:52 +00:00
|
|
|
}
|
2012-02-03 07:05:11 +00:00
|
|
|
|
2015-05-09 13:26:47 +00:00
|
|
|
char *mp_path_join(void *talloc_ctx, const char *p1, const char *p2)
|
|
|
|
{
|
|
|
|
return mp_path_join_bstr(talloc_ctx, bstr0(p1), bstr0(p2));
|
|
|
|
}
|
|
|
|
|
core: add playback resume feature (manual/opt-in)
A "watch later" command is now mapped to Shift+Q. This quits the player
and stores the playback state in a config file in ~/.mpv/watch_later/.
When calling the player with the same file again, playback is resumed
at that time position.
It's also possible to make mpv save playback state always on quit with
the --save-position-on-quit option. Likewise, resuming can be disabled
with the --no-resume-playback option.
This also attempts to save some playback parameters, like fullscreen
state or track selection. This will unconditionally override config
settings and command line options (which is probably not what you would
expect, but in general nobody will really care about this). Some things
are not backed up, because that would cause various problems. Additional
subtitle files, video filters, etc. are not stored because that would be
too hard and fragile. Volume/mute state are not stored because it would
mess up if the system mixer is used, or if the system mixer was
readjusted in the meantime.
Basically, the tradeoff between perfect state restoration and
complexity/fragility makes it not worth to attempt to implement
it perfectly, even if the result is a little bit inconsistent.
2013-05-05 17:37:29 +00:00
|
|
|
char *mp_getcwd(void *talloc_ctx)
|
|
|
|
{
|
2015-09-11 21:01:12 +00:00
|
|
|
char *e_wd = getenv("PWD");
|
|
|
|
if (e_wd)
|
|
|
|
return talloc_strdup(talloc_ctx, e_wd);
|
|
|
|
|
core: add playback resume feature (manual/opt-in)
A "watch later" command is now mapped to Shift+Q. This quits the player
and stores the playback state in a config file in ~/.mpv/watch_later/.
When calling the player with the same file again, playback is resumed
at that time position.
It's also possible to make mpv save playback state always on quit with
the --save-position-on-quit option. Likewise, resuming can be disabled
with the --no-resume-playback option.
This also attempts to save some playback parameters, like fullscreen
state or track selection. This will unconditionally override config
settings and command line options (which is probably not what you would
expect, but in general nobody will really care about this). Some things
are not backed up, because that would cause various problems. Additional
subtitle files, video filters, etc. are not stored because that would be
too hard and fragile. Volume/mute state are not stored because it would
mess up if the system mixer is used, or if the system mixer was
readjusted in the meantime.
Basically, the tradeoff between perfect state restoration and
complexity/fragility makes it not worth to attempt to implement
it perfectly, even if the result is a little bit inconsistent.
2013-05-05 17:37:29 +00:00
|
|
|
char *wd = talloc_array(talloc_ctx, char, 20);
|
|
|
|
while (getcwd(wd, talloc_get_size(wd)) == NULL) {
|
|
|
|
if (errno != ERANGE) {
|
|
|
|
talloc_free(wd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wd = talloc_realloc(talloc_ctx, wd, char, talloc_get_size(wd) * 2);
|
|
|
|
}
|
|
|
|
return wd;
|
|
|
|
}
|
|
|
|
|
2012-02-03 07:05:11 +00:00
|
|
|
bool mp_path_exists(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2014-10-17 19:46:08 +00:00
|
|
|
return path && stat(path, &st) == 0;
|
2012-02-03 07:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_path_isdir(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2014-10-17 19:46:08 +00:00
|
|
|
return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
|
2012-02-03 07:05:11 +00:00
|
|
|
}
|
2013-09-04 12:04:35 +00:00
|
|
|
|
|
|
|
// Return false if it's considered a normal local filesystem path.
|
|
|
|
bool mp_is_url(bstr path)
|
|
|
|
{
|
2013-09-04 14:28:02 +00:00
|
|
|
int proto = bstr_find0(path, "://");
|
2013-12-22 22:05:12 +00:00
|
|
|
if (proto < 1)
|
2013-09-04 14:28:02 +00:00
|
|
|
return false;
|
2018-12-31 17:31:57 +00:00
|
|
|
// Per RFC3986, the first character of the protocol must be alphabetic.
|
|
|
|
// The rest must be alphanumeric plus -, + and .
|
2013-09-04 14:28:02 +00:00
|
|
|
for (int i = 0; i < proto; i++) {
|
|
|
|
unsigned char c = path.start[i];
|
2018-12-31 17:31:57 +00:00
|
|
|
if ((i == 0 && !mp_isalpha(c)) ||
|
|
|
|
(!mp_isalnum(c) && c != '.' && c != '-' && c != '+'))
|
|
|
|
{
|
2013-09-04 14:28:02 +00:00
|
|
|
return false;
|
2018-12-31 17:31:57 +00:00
|
|
|
}
|
2013-09-04 14:28:02 +00:00
|
|
|
}
|
|
|
|
return true;
|
2013-09-04 12:04:35 +00:00
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-12-22 22:05:37 +00:00
|
|
|
// Return the protocol part of path, e.g. "http" if path is "http://...".
|
|
|
|
// On success, out_url (if not NULL) is set to the part after the "://".
|
|
|
|
bstr mp_split_proto(bstr path, bstr *out_url)
|
|
|
|
{
|
|
|
|
if (!mp_is_url(path))
|
|
|
|
return (bstr){0};
|
|
|
|
bstr r;
|
|
|
|
bstr_split_tok(path, "://", &r, out_url ? out_url : &(bstr){0});
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-06-18 23:55:40 +00:00
|
|
|
void mp_mkdirp(const char *dir)
|
|
|
|
{
|
2014-06-26 17:13:37 +00:00
|
|
|
char *path = talloc_strdup(NULL, dir);
|
2014-06-18 23:55:40 +00:00
|
|
|
char *cdir = path + 1;
|
|
|
|
|
|
|
|
while (cdir) {
|
|
|
|
cdir = strchr(cdir, '/');
|
|
|
|
if (cdir)
|
|
|
|
*cdir = 0;
|
|
|
|
|
|
|
|
mkdir(path, 0700);
|
|
|
|
|
|
|
|
if (cdir)
|
|
|
|
*cdir++ = '/';
|
|
|
|
}
|
|
|
|
|
2014-06-26 17:13:37 +00:00
|
|
|
talloc_free(path);
|
2014-06-18 23:55:40 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
void mp_mk_config_dir(struct mpv_global *global, char *subdir)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2015-05-09 14:21:44 +00:00
|
|
|
char *dir = mp_find_user_config_file(NULL, global, subdir);
|
|
|
|
if (dir)
|
2014-06-18 23:55:40 +00:00
|
|
|
mp_mkdirp(dir);
|
2015-05-09 14:21:44 +00:00
|
|
|
talloc_free(dir);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|