diff options
author | Peter Melnichenko <mpeterval@gmail.com> | 2016-10-28 15:26:35 +0300 |
---|---|---|
committer | Peter Melnichenko <mpeterval@gmail.com> | 2016-10-28 15:37:15 +0300 |
commit | 51aa62b855241a12866d3d008e7019689c24d130 (patch) | |
tree | b8a113f81539b93d90a827b872f4258cfd5f9371 | |
parent | 50b5755d3e9a2c6b2040d2d167accd02208ec687 (diff) | |
download | luarocks-51aa62b855241a12866d3d008e7019689c24d130.tar.gz luarocks-51aa62b855241a12866d3d008e7019689c24d130.tar.bz2 luarocks-51aa62b855241a12866d3d008e7019689c24d130.zip |
Fix absolute path detection on Windows
Do not use "^[%.a-zA-Z]?:?[\\/]" as a pattern when checking if
a path is absolute on Windows. Check if there is a
directory separator after an optional drive letter instead.
-rw-r--r-- | src/luarocks/fs/win32.lua | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 14804713..cfc28d35 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
@@ -33,7 +33,24 @@ function win32.quiet_stderr(cmd) | |||
33 | return cmd.." 2> NUL" | 33 | return cmd.." 2> NUL" |
34 | end | 34 | end |
35 | 35 | ||
36 | local drive_letter = "[%.a-zA-Z]?:?[\\/]" | 36 | -- Split path into root and the rest. |
37 | -- Root part consists of an optional drive letter (e.g. "C:") | ||
38 | -- and an optional directory separator. | ||
39 | local function split_root(path) | ||
40 | local root = "" | ||
41 | |||
42 | if path:match("^.:") then | ||
43 | root = path:sub(1, 2) | ||
44 | path = path:sub(3) | ||
45 | end | ||
46 | |||
47 | if path:match("^[\\/]") then | ||
48 | root = path:sub(1, 1) | ||
49 | path = path:sub(2) | ||
50 | end | ||
51 | |||
52 | return root, path | ||
53 | end | ||
37 | 54 | ||
38 | --- Quote argument for shell processing. Fixes paths on Windows. | 55 | --- Quote argument for shell processing. Fixes paths on Windows. |
39 | -- Adds double quotes and escapes. | 56 | -- Adds double quotes and escapes. |
@@ -41,8 +58,9 @@ local drive_letter = "[%.a-zA-Z]?:?[\\/]" | |||
41 | -- @return string: Quoted argument. | 58 | -- @return string: Quoted argument. |
42 | function win32.Q(arg) | 59 | function win32.Q(arg) |
43 | assert(type(arg) == "string") | 60 | assert(type(arg) == "string") |
44 | -- Quote DIR for Windows | 61 | -- Use Windows-specific directory separator for paths. |
45 | if arg:match("^"..drive_letter) then | 62 | -- Paths should be converted to absolute by now. |
63 | if split_root(arg) ~= "" then | ||
46 | arg = arg:gsub("/", "\\") | 64 | arg = arg:gsub("/", "\\") |
47 | end | 65 | end |
48 | if arg == "\\" then | 66 | if arg == "\\" then |
@@ -62,8 +80,9 @@ end | |||
62 | -- @return string: Quoted argument. | 80 | -- @return string: Quoted argument. |
63 | function win32.Qb(arg) | 81 | function win32.Qb(arg) |
64 | assert(type(arg) == "string") | 82 | assert(type(arg) == "string") |
65 | -- Quote DIR for Windows | 83 | -- Use Windows-specific directory separator for paths. |
66 | if arg:match("^"..drive_letter) then | 84 | -- Paths should be converted to absolute by now. |
85 | if split_root(arg) ~= "" then | ||
67 | arg = arg:gsub("/", "\\") | 86 | arg = arg:gsub("/", "\\") |
68 | end | 87 | end |
69 | if arg == "\\" then | 88 | if arg == "\\" then |
@@ -88,10 +107,14 @@ function win32.absolute_name(pathname, relative_to) | |||
88 | assert(type(relative_to) == "string" or not relative_to) | 107 | assert(type(relative_to) == "string" or not relative_to) |
89 | 108 | ||
90 | relative_to = relative_to or fs.current_dir() | 109 | relative_to = relative_to or fs.current_dir() |
91 | if pathname:match("^"..drive_letter) then | 110 | local root, rest = split_root(pathname) |
111 | if root:match("[\\/]$") then | ||
112 | -- It's an absolute path already. | ||
92 | return pathname | 113 | return pathname |
93 | else | 114 | else |
94 | return relative_to .. "/" .. pathname | 115 | -- It's a relative path, join it with base path. |
116 | -- This drops drive letter from paths like "C:foo". | ||
117 | return relative_to .. "/" .. rest | ||
95 | end | 118 | end |
96 | end | 119 | end |
97 | 120 | ||
@@ -100,7 +123,7 @@ end | |||
100 | -- @param pathname string: pathname to use. | 123 | -- @param pathname string: pathname to use. |
101 | -- @return string: The root of the given pathname. | 124 | -- @return string: The root of the given pathname. |
102 | function win32.root_of(pathname) | 125 | function win32.root_of(pathname) |
103 | return (fs.absolute_name(pathname):match("^("..drive_letter..")")) | 126 | return (split_root(fs.absolute_name(pathname))) |
104 | end | 127 | end |
105 | 128 | ||
106 | --- Create a wrapper to make a script executable from the command-line. | 129 | --- Create a wrapper to make a script executable from the command-line. |