[vlc-commits] common.lua: fix utility resolving dot segments in paths

Pierre Ynard git at videolan.org
Tue Aug 18 17:18:38 CEST 2020


vlc/vlc-3.0 | branch: master | Pierre Ynard <linkfanel at yahoo.fr> | Tue Aug 18 17:02:36 2020 +0200| [60127caa886454acae7fe5b2de272888c3103559] | committer: Pierre Ynard

common.lua: fix utility resolving dot segments in paths

This utility didn't work at all on Windows because it had no support for
'\', it also had glaring bugs and shortcomings, and its implementation
was broken. Lua has no knowledge of the OS and directory separator
currently in use, so this takes the one sensible approach here:
supporting URLs to let the utility operate on unambiguous URL paths
rather than file paths.

(cherry picked from commit 9c4cfda8f4747231b86b893e33ebb015349f729f)
Signed-off-by: Pierre Ynard <linkfanel at yahoo.fr>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=60127caa886454acae7fe5b2de272888c3103559
---

 share/lua/modules/common.lua | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/share/lua/modules/common.lua b/share/lua/modules/common.lua
index 8c49c713e5..388ec412e1 100644
--- a/share/lua/modules/common.lua
+++ b/share/lua/modules/common.lua
@@ -111,8 +111,40 @@ function durationtostring(duration)
 end
 
 -- realpath
+-- this is for URL paths - do not use for file paths as this has
+-- no support for Windows '\' directory separators
 function realpath(path)
-    return string.gsub(string.gsub(string.gsub(string.gsub(path,"/%.%./[^/]+","/"),"/[^/]+/%.%./","/"),"/%./","/"),"//","/")
+    -- detect URLs to extract and process the path component
+    local s, p, qf = string.match(path, "^([a-zA-Z0-9+%-%.]-://[^/]-)(/[^?#]*)(.*)$")
+    if not s then
+        s = ""
+        p = path
+        qf = ""
+    end
+
+    local n
+    repeat
+        p, n = p:gsub("//","/", 1)
+    until n == 0
+
+    repeat
+        p, n = p:gsub("/%./","/", 1)
+    until n == 0
+    p = p:gsub("/%.$", "/", 1)
+
+    -- resolving ".." without an absolute path would be troublesome
+    if p:match("^/") then
+        repeat
+            p, n = p:gsub("^/%.%./","/", 1)
+            if n == 0 then
+                p, n = p:gsub("/[^/]+/%.%./","/", 1)
+            end
+        until n == 0
+        p = p:gsub("^/%.%.$","/", 1)
+        p = p:gsub("/[^/]+/%.%.$","/", 1)
+    end
+
+    return s..p..qf
 end
 
 -- parse the time from a string and return the seconds



More information about the vlc-commits mailing list