[vlc-devel] [PATCH 2/4] common.lua: fix utility resolving dot segments in paths

Pierre Ynard linkfanel at yahoo.fr
Tue Aug 11 04:42:04 CEST 2020


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.


diff --git a/share/lua/modules/common.lua b/share/lua/modules/common.lua
index 06db246..4bbb005 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
-- 
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."


More information about the vlc-devel mailing list