os.nim: use the new pathnorm.normalizePath implementation
This commit is contained in:
parent
5b39c7aca9
commit
ce9815bcf5
4 changed files with 67 additions and 85 deletions
|
|
@ -111,6 +111,9 @@ proc enumToString*(enums: openArray[enum]): string =
|
||||||
(default value: true) that can be set to `false` for better Posix
|
(default value: true) that can be set to `false` for better Posix
|
||||||
interoperability. (Bug #9619.)
|
interoperability. (Bug #9619.)
|
||||||
|
|
||||||
|
- `os.joinPath` and `os.normalizePath` handle edge cases like ``"a/b/../../.."``
|
||||||
|
differently.
|
||||||
|
|
||||||
|
|
||||||
### Language additions
|
### Language additions
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -984,6 +984,8 @@ proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [], noNimScr
|
||||||
##
|
##
|
||||||
## Warning: URL-encoded and Unicode attempts at directory traversal are not detected.
|
## Warning: URL-encoded and Unicode attempts at directory traversal are not detected.
|
||||||
## Triple dot is not handled.
|
## Triple dot is not handled.
|
||||||
|
path = pathnorm.normalizePath(path)
|
||||||
|
when false:
|
||||||
let isAbs = isAbsolute(path)
|
let isAbs = isAbsolute(path)
|
||||||
var stack: seq[string] = @[]
|
var stack: seq[string] = @[]
|
||||||
for p in split(path, {DirSep}):
|
for p in split(path, {DirSep}):
|
||||||
|
|
@ -1012,8 +1014,7 @@ proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [], noNimScr
|
||||||
|
|
||||||
proc normalizedPath*(path: string): string {.rtl, extern: "nos$1", tags: [], noNimScript.} =
|
proc normalizedPath*(path: string): string {.rtl, extern: "nos$1", tags: [], noNimScript.} =
|
||||||
## Returns a normalized path for the current OS. See `<#normalizePath>`_
|
## Returns a normalized path for the current OS. See `<#normalizePath>`_
|
||||||
result = path
|
result = pathnorm.normalizePath(path)
|
||||||
normalizePath(result)
|
|
||||||
|
|
||||||
when defined(Windows) and not defined(nimscript):
|
when defined(Windows) and not defined(nimscript):
|
||||||
proc openHandle(path: string, followSymlink=true, writeAccess=false): Handle =
|
proc openHandle(path: string, followSymlink=true, writeAccess=false): Handle =
|
||||||
|
|
|
||||||
|
|
@ -66,12 +66,19 @@ proc addNormalizePath*(x: string; result: var string; state: var int; dirSep = D
|
||||||
if (state shr 1 == 0) and isSlash(x, b):
|
if (state shr 1 == 0) and isSlash(x, b):
|
||||||
result.add dirSep
|
result.add dirSep
|
||||||
state = state or 1
|
state = state or 1
|
||||||
elif result.len > (state and 1) and isDotDot(x, b):
|
elif isDotDot(x, b):
|
||||||
|
if (state shr 1) >= 1:
|
||||||
var d = result.len
|
var d = result.len
|
||||||
# f/..
|
# f/..
|
||||||
while (d-1) > (state and 1) and result[d-1] notin {DirSep, AltSep}:
|
while (d-1) > (state and 1) and result[d-1] notin {DirSep, AltSep}:
|
||||||
dec d
|
dec d
|
||||||
if d > 0: setLen(result, d-1)
|
if d > 0:
|
||||||
|
setLen(result, d-1)
|
||||||
|
dec state, 2
|
||||||
|
else:
|
||||||
|
if result.len > 0 and result[^1] notin {DirSep, AltSep}:
|
||||||
|
result.add dirSep
|
||||||
|
result.add substr(x, b[0], b[1])
|
||||||
elif isDot(x, b):
|
elif isDot(x, b):
|
||||||
discard "discard the dot"
|
discard "discard the dot"
|
||||||
elif b[1] >= b[0]:
|
elif b[1] >= b[0]:
|
||||||
|
|
|
||||||
|
|
@ -190,61 +190,32 @@ block walkDirRec:
|
||||||
removeDir("walkdir_test")
|
removeDir("walkdir_test")
|
||||||
|
|
||||||
block normalizedPath:
|
block normalizedPath:
|
||||||
when defined(posix):
|
|
||||||
block relative:
|
block relative:
|
||||||
doAssert normalizedPath(".") == "."
|
doAssert normalizedPath(".") == ""
|
||||||
doAssert normalizedPath("..") == ".."
|
doAssert normalizedPath("..") == ".."
|
||||||
doAssert normalizedPath("../") == ".."
|
doAssert normalizedPath("../") == ".."
|
||||||
doAssert normalizedPath("../..") == "../.."
|
doAssert normalizedPath("../..") == unixToNativePath"../.."
|
||||||
doAssert normalizedPath("../a/..") == ".."
|
doAssert normalizedPath("../a/..") == ".."
|
||||||
doAssert normalizedPath("../a/../") == ".."
|
doAssert normalizedPath("../a/../") == ".."
|
||||||
doAssert normalizedPath("./") == "."
|
doAssert normalizedPath("./") == ""
|
||||||
|
|
||||||
block absolute:
|
block absolute:
|
||||||
doAssert normalizedPath("/") == "/"
|
doAssert normalizedPath("/") == unixToNativePath"/"
|
||||||
doAssert normalizedPath("/.") == "/"
|
doAssert normalizedPath("/.") == unixToNativePath"/"
|
||||||
doAssert normalizedPath("/..") == "/"
|
doAssert normalizedPath("/..") == unixToNativePath"/.."
|
||||||
doAssert normalizedPath("/../") == "/"
|
doAssert normalizedPath("/../") == unixToNativePath"/.."
|
||||||
doAssert normalizedPath("/../..") == "/"
|
doAssert normalizedPath("/../..") == unixToNativePath"/../.."
|
||||||
doAssert normalizedPath("/../../") == "/"
|
doAssert normalizedPath("/../../") == unixToNativePath"/../.."
|
||||||
doAssert normalizedPath("/../../../") == "/"
|
doAssert normalizedPath("/../../../") == unixToNativePath"/../../.."
|
||||||
doAssert normalizedPath("/a/b/../../foo") == "/foo"
|
doAssert normalizedPath("/a/b/../../foo") == unixToNativePath"/foo"
|
||||||
doAssert normalizedPath("/a/b/../../../foo") == "/foo"
|
doAssert normalizedPath("/a/b/../../../foo") == unixToNativePath"/../foo"
|
||||||
doAssert normalizedPath("/./") == "/"
|
doAssert normalizedPath("/./") == unixToNativePath"/"
|
||||||
doAssert normalizedPath("//") == "/"
|
doAssert normalizedPath("//") == unixToNativePath"/"
|
||||||
doAssert normalizedPath("///") == "/"
|
doAssert normalizedPath("///") == unixToNativePath"/"
|
||||||
doAssert normalizedPath("/a//b") == "/a/b"
|
doAssert normalizedPath("/a//b") == unixToNativePath"/a/b"
|
||||||
doAssert normalizedPath("/a///b") == "/a/b"
|
doAssert normalizedPath("/a///b") == unixToNativePath"/a/b"
|
||||||
doAssert normalizedPath("/a/b/c/..") == "/a/b"
|
doAssert normalizedPath("/a/b/c/..") == unixToNativePath"/a/b"
|
||||||
doAssert normalizedPath("/a/b/c/../") == "/a/b"
|
doAssert normalizedPath("/a/b/c/../") == unixToNativePath"/a/b"
|
||||||
|
|
||||||
else:
|
|
||||||
block relative:
|
|
||||||
doAssert normalizedPath(".") == "."
|
|
||||||
doAssert normalizedPath("..") == ".."
|
|
||||||
doAssert normalizedPath("..\\") == ".."
|
|
||||||
doAssert normalizedPath("..\\..") == "..\\.."
|
|
||||||
doAssert normalizedPath("..\\a\\..") == ".."
|
|
||||||
doAssert normalizedPath("..\\a\\..\\") == ".."
|
|
||||||
doAssert normalizedPath(".\\") == "."
|
|
||||||
|
|
||||||
block absolute:
|
|
||||||
doAssert normalizedPath("\\") == "\\"
|
|
||||||
doAssert normalizedPath("\\.") == "\\"
|
|
||||||
doAssert normalizedPath("\\..") == "\\"
|
|
||||||
doAssert normalizedPath("\\..\\") == "\\"
|
|
||||||
doAssert normalizedPath("\\..\\..") == "\\"
|
|
||||||
doAssert normalizedPath("\\..\\..\\") == "\\"
|
|
||||||
doAssert normalizedPath("\\..\\..\\..\\") == "\\"
|
|
||||||
doAssert normalizedPath("\\a\\b\\..\\..\\foo") == "\\foo"
|
|
||||||
doAssert normalizedPath("\\a\\b\\..\\..\\..\\foo") == "\\foo"
|
|
||||||
doAssert normalizedPath("\\.\\") == "\\"
|
|
||||||
doAssert normalizedPath("\\\\") == "\\"
|
|
||||||
doAssert normalizedPath("\\\\\\") == "\\"
|
|
||||||
doAssert normalizedPath("\\a\\\\b") == "\\a\\b"
|
|
||||||
doAssert normalizedPath("\\a\\\\\\b") == "\\a\\b"
|
|
||||||
doAssert normalizedPath("\\a\\b\\c\\..") == "\\a\\b"
|
|
||||||
doAssert normalizedPath("\\a\\b\\c\\..\\") == "\\a\\b"
|
|
||||||
|
|
||||||
block isHidden:
|
block isHidden:
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue