From c4ac8edce9fc04e9898b12f9935850a9469e5340 Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Wed, 6 Aug 2014 20:42:43 -0400 Subject: Make device and file ID's public. Added the isHidden() proc --- lib/pure/os.nim | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pure/os.nim b/lib/pure/os.nim index a70bfa7f1..37d341787 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1860,12 +1860,12 @@ proc expandTilde*(path: string): string = when defined(Windows): type - DeviceId = int32 - FileId = int64 + DeviceId* = int32 + FileId* = int64 else: type - DeviceId = TDev - FileId = TIno + DeviceId* = TDev + FileId* = TIno type FileInfo* = object @@ -1908,6 +1908,7 @@ template rawToFormalFileInfo(rawInfo, formalInfo): expr = if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT) != 0'i32: formalInfo.kind = succ(result.kind) + else: template checkAndIncludeMode(rawMode, formalMode: expr) = if (rawInfo.st_mode and rawMode) != 0'i32: @@ -1994,4 +1995,21 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo = osError(osLastError()) rawToFormalFileInfo(rawInfo, result) +proc isHidden*(path: string): bool = + ## Determines whether a given path is hidden or not. Returns false if the + ## file doesn't exist. On Windows, a file is hidden if the file's 'hidden' + ## attribute is set. On Unix-like systems, a file is hidden if it starts + ## with a '.' ." + when defined(Windows): + wrapUnary(attributes, getFileAttributesW, path) + if attributes != -1'i32: + result = (attributes and FILE_ATTRIBUTE_HIDDEN) != 0'i32 + else: + result = false + else: + if fileExists(path): + result = (path[0] == '.') + else: + result = false + {.pop.} -- cgit 1.4.1-2-gfad0 From 6dd96abdcc30a0002129f77fd2ac85c1a2edcf13 Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Sun, 10 Aug 2014 18:32:27 -0400 Subject: Made the code actually work, updated the docstring to point out that the given file path must exist *and* be accessible from the current working directory of the running program. --- lib/pure/os.nim | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 37d341787..adde94afc 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -365,8 +365,9 @@ when defined(windows): template getFilename(f: expr): expr = $f.cFilename proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} = + # Note - takes advantage of null delimiter in the cstring const dot = ord('.') - result = f.cFileName[0].int == dot and(f.cFileName[1].int == 0 or + result = f.cFileName[0].int == dot and (f.cFileName[1].int == 0 or f.cFileName[1].int == dot and f.cFileName[2].int == 0) proc existsFile*(filename: string): bool {.rtl, extern: "nos$1", @@ -1997,19 +1998,24 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo = proc isHidden*(path: string): bool = ## Determines whether a given path is hidden or not. Returns false if the - ## file doesn't exist. On Windows, a file is hidden if the file's 'hidden' - ## attribute is set. On Unix-like systems, a file is hidden if it starts - ## with a '.' ." + ## file doesn't exist. The given path must be accessible from the current + ## working directory of the program. + ## + ## On Windows, a file is hidden if the file's 'hidden' attribute is set. + ## On Unix-like systems, a file is hidden if it starts with a '.' (period) + ## and is not *just* '.' or '..' ' ." when defined(Windows): wrapUnary(attributes, getFileAttributesW, path) if attributes != -1'i32: result = (attributes and FILE_ATTRIBUTE_HIDDEN) != 0'i32 - else: - result = false else: if fileExists(path): - result = (path[0] == '.') - else: - result = false + let + fileName = extractFilename(path) + nameLen = len(fileName) + if nameLen == 2: + result = (fileName[0] == '.') and (fileName[1] != '.') + elif nameLen > 2: + result = (fileName[0] == '.') and (fileName[3] != '.') {.pop.} -- cgit 1.4.1-2-gfad0