From f3e0a2a8008be1fbe08ace9b9da1090ee73bd79d Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Tue, 24 Oct 2017 20:30:48 +0100 Subject: [PATCH] Add normalizePath, absolutePath and tests --- lib/pure/os.nim | 63 ++++++++++++++++++++++++++++++++++++++++++-- tests/stdlib/tos.nim | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index a77bee99daa95..487329c2e3733 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -288,8 +288,11 @@ proc setCurrentDir*(newDir: string) {.inline, tags: [].} = proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", tags: [ReadDirEffect].} = - ## Returns the full (`absolute`:idx:) path of the file `filename`, - ## raises OSError in case of an error. + ## Returns the full (`absolute`:idx:) path of an existing file `filename`, + ## raises OSError in case of an error. Follows symlinks. + ## + ## To create absolute paths from any path (existing or not) see + ## `<#absolutePath>`_. when defined(windows): var bufsize = MAX_PATH.int32 when useWinUnicode: @@ -328,6 +331,62 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", result = $r c_free(cast[pointer](r)) +proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} = + ## Normalize an UNIX path. + ## + ## Consecutive slashes (/) are collapsed, including an initial double slash. + ## + ## On relative paths, double dot (..) sequences are collapsed if possible. + ## On absolute paths they are always collapsed. + ## + ## Warning: URL-encoded and Unicode attempts at directory traversal are not detected. + ## Triple dot is not handled. + let is_abs = path[0] == DirSep + var stack: seq[string] = @[] + for p in split(path, {DirSep}): + case p + of "", ".": + continue + of "..": + if stack.len == 0: + if is_abs: + discard # collapse all double dots on absoluta paths + else: + stack.add(p) + elif stack[^1] == "..": + stack.add(p) + else: + discard stack.pop() + else: + stack.add(p) + + if is_abs: + path = DirSep & join(stack, $DirSep) + elif stack.len > 0: + path = join(stack, $DirSep) + else: + path = "." + +proc normalizedPath*(path: string): string {.rtl, extern: "nos$1", tags: [].} = + ## Returns a normalized UNIX path. See `<#normalizePath>`_ + result = path + normalizePath result + +proc makeAbsolutePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} = + ## Generates the normalized, (`absolute`:idx:) version of `path`, based on + ## the current directory. See `<#normalizePath>`_ + if path.isAbsolute == false: + path = joinPath(getCurrentDir(), path) + + if path.len > 1 and path[^1] == DirSep: + path = path[0..^2] + +proc absolutePath*(path: string): string {.rtl, extern: "nos$1", tags: [].} = + ## Returns the normalized, (`absolute`:idx:) version of `path`, based on + ## the current directory. See `<#normalizePath>`_ + result = path + makeAbsolutePath result + when defined(Windows): proc openHandle(path: string, followSymlink=true): Handle = var flags = FILE_FLAG_BACKUP_SEMANTICS or FILE_ATTRIBUTE_NORMAL diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 771dc24562ba8..1ca6f822e86a8 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -42,6 +42,7 @@ Raises true true true + ''' """ # test os path creation, iteration, and deletion @@ -129,3 +130,52 @@ echo fileExists("../dest/a/b/file.txt") echo fileExists("../dest/a/b/c/fileC.txt") removeDir("../dest") + +block normalizedPath: + 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("/./") == "/" + 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" + +when defined(Linux) or defined(osx): + + block absolutePath: + block root: + let cwd = getCurrentDir() + setCurrentDir("/") + doAssert absolutePath("foo") == "/foo" + doAssert absolutePath("") == "/" + #doAssert absolutePath(".") == "/" + doAssert absolutePath("/") == "/" + setCurrentDir(cwd) + + block tmp: + let cwd = getCurrentDir() + setCurrentDir("/tmp") + doAssert absolutePath("foo") == "/tmp/foo" + doAssert absolutePath("") == "/tmp" + #doAssert absolutePath(".") == "/tmp" + doAssert absolutePath("/") == "/" + #doAssert absolutePath("foo/..") == "/tmp/foo" + #doAssert absolutePath("../..") == "/tmp/foo" + setCurrentDir(cwd)