From b237bd4d0a631b0962d25df8baf01b68a3a3c390 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 | 52 +++++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 3 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 5997451769ba1..71cb42ef98b75 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -298,8 +298,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: @@ -338,6 +341,62 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", result = $r c_free(cast[pointer](r)) +proc normalizePath*(path: var string, separator=DirSep) {.rtl, extern: "nos$1", tags: [].} = + ## Normalize a path. + ## + ## Consecutive directory separators 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] == separator + var stack: seq[string] = @[] + for p in split(path, {separator}): + 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 = separator & join(stack, $separator) + elif stack.len > 0: + path = join(stack, $separator) + else: + path = "." + +proc normalizedPath*(path: string, separator=DirSep): string {.rtl, extern: "nos$1", tags: [].} = + ## Returns a normalized path for the current OS. See `<#normalizePath>`_ + result = path + normalizePath(result, separator) + +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.len == 0 or 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, writeAccess=false): Handle = var flags = FILE_FLAG_BACKUP_SEMANTICS or FILE_ATTRIBUTE_NORMAL diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index e6fbb0e51264b..7645392bbe55b 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -43,6 +43,7 @@ true true true true + ''' """ # test os path creation, iteration, and deletion @@ -138,4 +139,53 @@ let tm = fromUnix(0) + 100.microseconds writeFile("a", "") setLastModificationTime("a", tm) echo getLastModificationTime("a") == tm -removeFile("a") \ No newline at end of file +removeFile("a") + +block normalizedPath: + doAssert normalizedPath("/a/b/../../../foo", '/') == "/foo" + doAssert normalizedPath("\\a\\b\\..\\..\\..\\foo", '\\') == "\\foo" + +when defined(Linux) or defined(osx): + + 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("/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 absolutePath: + block root: + let cwd = getCurrentDir() + setCurrentDir("/") + doAssert absolutePath("foo") == "/foo" + doAssert absolutePath("/") == "/" + setCurrentDir(cwd) + + block tmp: + let cwd = getCurrentDir() + setCurrentDir("/tmp") + doAssert absolutePath("foo") == "/tmp/foo" + doAssert absolutePath("/") == "/" + setCurrentDir(cwd)