Skip to content

Commit

Permalink
Fix problems in path normalisation algorithm (luarocks#1541)
Browse files Browse the repository at this point in the history
Current implementation of path normalisation contains various flaws:

- Trailing `.` and `..` at the end of path don't get normalised,
- Path `/` turns into an empty string after normalisation.

This patch changes implementation of normalisation to fix these.
  • Loading branch information
AlgebraicWolf authored Oct 30, 2023
1 parent bb9d7ed commit b8a2710
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 8 additions & 0 deletions spec/dir_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ describe("luarocks.dir #unit", function()
it("strips unneeded /../ and /./", function()
assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt"))
assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt"))
assert.are.same("/some/dir", dir.normalize("/../../../some/./foo/bar/.././../dir/./some/subdir/../.."))
assert.are.same("/some/dir", dir.normalize("/../../../some/./foo/bar/.././../dir/./."))
end)
it("respects relative paths", function()
assert.are.same(".", dir.normalize("."))
assert.are.same("boo", dir.normalize("./boo"))
assert.are.same("/boo", dir.normalize("/./boo"))
assert.are.same("../../../../boo", dir.normalize("../../../hello/world/../../../boo"))
end)
it("respects root directory", function()
assert.are.same("/", dir.normalize("/"))
assert.are.same("/", dir.normalize("/////"))
assert.are.same("/", dir.normalize("/a/b/.././../c/./../../"))
end)
end)

end)
11 changes: 7 additions & 4 deletions src/luarocks/core/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function dir.normalize(name)
if pathname:match("^.:") then
drive, pathname = pathname:match("^(.:)(.*)$")
end
pathname = pathname .. "/"
for piece in pathname:gmatch("(.-)/") do
if piece == ".." then
local prev = pieces[#pieces]
Expand All @@ -75,11 +76,13 @@ function dir.normalize(name)
table.insert(pieces, piece)
end
end
local basename = pathname:match("[^/]+$")
if basename then
table.insert(pieces, basename)
if #pieces == 0 then
pathname = drive .. "."
elseif #pieces == 1 and pieces[1] == "" then
pathname = drive .. "/"
else
pathname = drive .. table.concat(pieces, "/")
end
pathname = drive .. table.concat(pieces, "/")
if protocol ~= "file" then pathname = protocol .."://"..pathname end
return pathname
end
Expand Down

0 comments on commit b8a2710

Please sign in to comment.