| 1 |
-- Helper to resolve symlinks in paths |
| 2 |
|
| 3 |
local sub = string.sub |
| 4 |
local symlinkattributes = lfs.symlinkattributes |
| 5 |
local attributes = lfs.attributes |
| 6 |
local currentdir = lfs.currentdir |
| 7 |
local concat = table.concat |
| 8 |
local move = table.move |
| 9 |
local newtable = lua.newtable |
| 10 |
local setmetatable = setmetatable |
| 11 |
|
| 12 |
-- Windows should just adapt reasonable path conventions like every other operating system. |
| 13 |
-- But some people like to do their own thing, so we need lots of special casing here. |
| 14 |
local windows_style_paths = os.type ~= 'unix' |
| 15 |
|
| 16 |
-- Marker key for elements of result_tree to indicate the path components entry and the file mode |
| 17 |
local path_components, file_mode = {}, {} |
| 18 |
local tree_root |
| 19 |
|
| 20 |
local split_path do |
| 21 |
local l = lpeg |
| 22 |
local separator = windows_style_paths and l.S'/\\' or l.S'/' |
| 23 |
-- We do not allow empty segments here because they get ignored. |
| 24 |
local segment = l.C((1 - separator)^1) |
| 25 |
-- Duplicate and trailing separators are dropped. |
| 26 |
local unc = windows_style_paths and separator * separator * l.Cg(l.P(1)^0 * -1, 'unc') or l.P(false) |
| 27 |
local drive_letter = windows_style_paths and l.Cg(l.R('az', 'AZ') * ':', 'drive') or l.P(false) |
| 28 |
local path_pat = l.Ct(unc + drive_letter^-1 * (l.Cc'' * separator^1)^-1 * (segment * separator^1)^0 * segment^-1 * -1) |
| 29 |
function split_path(path) |
| 30 |
local splitted = path_pat:match(path) |
| 31 |
if not splitted then |
| 32 |
error'Invalid path rejected' |
| 33 |
elseif splitted.unc then |
| 34 |
error'Unsupported UNC path encountered' |
| 35 |
elseif splitted.drive and splitted[1] ~= '' then |
| 36 |
error'Unsupported relative path with drive letter encountered' |
| 37 |
end |
| 38 |
return splitted |
| 39 |
end |
| 40 |
end |
| 41 |
|
| 42 |
local function recombine_path(components) |
| 43 |
local joined = concat(components, '/') |
| 44 |
if components.drive then |
| 45 |
joined = components.drive .. joined |
| 46 |
end |
| 47 |
return joined |
| 48 |
end |
| 49 |
|
| 50 |
local function lookup_split_path_in_tree(components, tree) |
| 51 |
if components[1] == '' then |
| 52 |
if windows_style_paths then |
| 53 |
tree = tree_root[components.drive or tree[path_components].drive] |
| 54 |
else |
| 55 |
tree = tree_root |
| 56 |
end |
| 57 |
end |
| 58 |
for i=1, #components do |
| 59 |
local next_tree = tree[components[i]] |
| 60 |
if not next_tree then |
| 61 |
return nil, string.format("Unable to find %q in %q", components[i], recombine_path(tree[path_components])) |
| 62 |
end |
| 63 |
tree = next_tree |
| 64 |
end |
| 65 |
return tree |
| 66 |
end |
| 67 |
|
| 68 |
local tree_meta |
| 69 |
tree_meta = { |
| 70 |
__index = function(parent, component) |
| 71 |
local parent_components = parent[path_components] |
| 72 |
local depth = #parent_components |
| 73 |
local components = move(parent_components, 1, depth, 1, newtable(depth + 1, 1)) |
| 74 |
components.drive, components[depth + 1] = parent_components.drive, component |
| 75 |
local path = recombine_path(components) |
| 76 |
|
| 77 |
local mode = symlinkattributes(path, 'mode') |
| 78 |
if not mode then |
| 79 |
parent[component] = false |
| 80 |
return false |
| 81 |
end |
| 82 |
if mode == 'link' then |
| 83 |
local target = symlinkattributes(path, 'target') |
| 84 |
local splitted_target = split_path(target) |
| 85 |
local target_tree = lookup_split_path_in_tree(splitted_target, parent) or false |
| 86 |
parent[component] = target_tree |
| 87 |
return target_tree |
| 88 |
end |
| 89 |
|
| 90 |
local child = { |
| 91 |
[path_components] = components, |
| 92 |
[file_mode] = mode, |
| 93 |
} |
| 94 |
if mode == 'directory' then |
| 95 |
setmetatable(child, tree_meta) |
| 96 |
child['.'] = child |
| 97 |
child['..'] = parent |
| 98 |
end |
| 99 |
parent[component] = child |
| 100 |
return child |
| 101 |
end, |
| 102 |
} |
| 103 |
|
| 104 |
-- We assume that the directory structure does not change during our run. |
| 105 |
function build_root_dir(drive) |
| 106 |
local root_dir = setmetatable({ |
| 107 |
[path_components] = {'', drive = drive}, |
| 108 |
[file_mode] = 'directory', -- "If [your root is not a directory] you are having a bad problem and you will not go to space today". |
| 109 |
}, tree_meta) |
| 110 |
root_dir['.'] = root_dir |
| 111 |
root_dir['..'] = root_dir |
| 112 |
return {[''] = root_dir} |
| 113 |
end |
| 114 |
tree_root = windows_style_paths and setmetatable({}, {__index = function(t, drive) |
| 115 |
local root_dir = build_root_dir(drive) |
| 116 |
t[drive] = root_dir |
| 117 |
return root_dir |
| 118 |
end}) or build_root_dir() |
| 119 |
|
| 120 |
local function resolve_path_to_tree(path) |
| 121 |
local splitted = split_path(path) |
| 122 |
if splitted[1] == '' and (not windows_style_paths or splitted.drive) then -- Optimization to avoid currentdir lookup. |
| 123 |
return lookup_split_path_in_tree(splitted, tree_root) |
| 124 |
else |
| 125 |
local splitted_currentdir = split_path(currentdir()) |
| 126 |
local current_tree = assert(lookup_split_path_in_tree(splitted_currentdir, tree_root)) |
| 127 |
return lookup_split_path_in_tree(splitted, current_tree) |
| 128 |
end |
| 129 |
end |
| 130 |
|
| 131 |
local function resolve_path(path) |
| 132 |
local tree, err = resolve_path_to_tree(path) |
| 133 |
if not tree then return tree, err end |
| 134 |
return recombine_path(tree[path_components]), tree[file_mode] |
| 135 |
end |
| 136 |
|
| 137 |
return { |
| 138 |
realpath = resolve_path, |
| 139 |
} |