summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2020-02-06 14:06:53 +0100
committerwm4 <wm4@nowhere>2020-02-06 14:10:40 +0100
commit31acec543819cf56d63dddae59507858b1229b75 (patch)
tree6895aee82a7e151cd46b62946db3a4db32cbcfe1
parent1293c40623bc718b0b1a238a86d681e1f0ee5278 (diff)
downloadmpv-31acec543819cf56d63dddae59507858b1229b75.tar.bz2
mpv-31acec543819cf56d63dddae59507858b1229b75.tar.xz
path: change win32 semantics for joining drive-relative paths
win32 is a cursed abomination which has "drive letters" at the root of the filesystem namespace for no reason. This requires special handling beyond tolerating the idiotic "\" path separator. Even more cursed is the fact that a path starting with a drive letter can be a relative path. For example, "c:billsucks" is actually a relative path to the current working directory of the C drive. So for example if the current working directory is "c:/windowsphone", then "c:billsucks" would reference "c:/windowsphone/billsucks". You should realize that win32 is a ridiculous satanic trash fire by the point you realize that win32 has at least 26 current working directories, one for each drive letter. Anyway, the actual problem is that mpv's mp_path_join() function would return a relative path if an absolute relative path is joined with a drive-relative path. This should never happen; I bet it breaks a lot of assumptions (maybe even some security or safety relevant ones, but probably not). Since relative drive paths are such a fucked up shit idea, don't try to support them "properly", and just solve the problem at hand. The solution produces a path that should be invalid on win32. Joining two relative paths still behaves the same; this is probably OK (maybe). The change isn't very minimal due to me rewriting parts of it without strict need, but I don't care. Note that the Python os.path.join() function (after which the mpv function was apparently modeled) has the same problem.
-rw-r--r--options/path.c26
-rw-r--r--test/paths.c2
2 files changed, 14 insertions, 14 deletions
diff --git a/options/path.c b/options/path.c
index a8e39c3310..a17d8b4325 100644
--- a/options/path.c
+++ b/options/path.c
@@ -256,28 +256,28 @@ char *mp_splitext(const char *path, bstr *root)
char *mp_path_join_bstr(void *talloc_ctx, struct bstr p1, struct bstr p2)
{
- bool test;
if (p1.len == 0)
return bstrdup0(talloc_ctx, p2);
if (p2.len == 0)
return bstrdup0(talloc_ctx, p1);
+ bool is_absolute = strchr(mp_path_separators, p2.start[0]);
#if HAVE_DOS_PATHS
- test = (p2.len >= 2 && p2.start[1] == ':')
- || p2.start[0] == '\\' || p2.start[0] == '/';
-#else
- test = p2.start[0] == '/';
+ // 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 /.
+ if (p2.len >= 3 && p2.start[1] == ':' &&
+ strchr(mp_path_separators, p2.start[2]))
+ is_absolute = true;
#endif
- if (test)
- return bstrdup0(talloc_ctx, p2); // absolute path
+ if (is_absolute)
+ return bstrdup0(talloc_ctx, p2);
- bool have_separator;
- int endchar1 = p1.start[p1.len - 1];
+ bool have_separator = strchr(mp_path_separators, p1.start[p1.len - 1]);
#if HAVE_DOS_PATHS
- have_separator = endchar1 == '/' || endchar1 == '\\'
- || (p1.len == 2 && endchar1 == ':'); // "X:" only
-#else
- have_separator = endchar1 == '/';
+ // "X:" only => path relative to "X:" current working directory.
+ if (p1.len == 2 && p1.start[1] == ':')
+ have_separator = true;
#endif
return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),
diff --git a/test/paths.c b/test/paths.c
index 5ec2eb8859..434a7996ad 100644
--- a/test/paths.c
+++ b/test/paths.c
@@ -36,7 +36,7 @@ static void run(struct test_ctx *ctx)
TEST_JOIN("c:\\a", "c:\\b", "c:\\b");
TEST_JOIN("c:/a", "c:/b", "c:/b");
// Note: drive-relative paths are not always supported "properly"
- TEST_JOIN("c:/a", "d:b", "d:b");
+ TEST_JOIN("c:/a", "d:b", "c:/a/d:b");
TEST_JOIN("c:a", "b", "c:a/b");
TEST_JOIN("c:", "b", "c:b");
#endif