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

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


vlc | branch: master | Pierre Ynard <linkfanel at yahoo.fr> | Tue Aug 18 17:02:36 2020 +0200| [9c4cfda8f4747231b86b893e33ebb015349f729f] | 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.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=9c4cfda8f4747231b86b893e33ebb015349f729f
---

 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 06db2463b9..4bbb005a62 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