Skip to content

Commit

Permalink
fix #8225 isHidden was broken on posix
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jul 14, 2018
1 parent 9b98add commit 47f6193
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1688,13 +1688,28 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo =
rawToFormalFileInfo(rawInfo, path, result)

proc isHidden*(path: string): bool =
## Determines whether a given path is hidden or not. Returns false if the
## 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 '..' ' ."
## Determines whether a given path is hidden or not.
## On Windows, a path is hidden if it exists and it's 'hidden' attribute is set.
## On posix, a hidden path starts with `.` (see examples).
## reference: https://en.wikipedia.org/wiki/Hidden_file_and_hidden_directory
## NOTE: on OSX, in addition to the "dotfile" behaviour, existing paths with the
## "Invisible" attribute are hidden in Finder, although not in ls
## NOTE: the behavior of certain corner cases (see examples) ".foo/" is
## unspecified, subject to change (can be avoided using `normalizePath`)
runnableExamples:
when defined(posix):
doAssert ".foo".isHidden
doAssert ".foo.ext".isHidden
doAssert "bar/.foo.ext".isHidden
doAssert(not "bar".isHidden)
doAssert(not ".".isHidden)
doAssert(not "..".isHidden)
doAssert(not ".foo/bar".isHidden)
doAssert(not "".isHidden)
# TODO: decide on these:
# doAssert ".foo/".isHidden # un-specified
# doAssert ".foo/.".isHidden # un-specified

when defined(Windows):
when useWinUnicode:
wrapUnary(attributes, getFileAttributesW, path)
Expand All @@ -1703,14 +1718,8 @@ proc isHidden*(path: string): bool =
if attributes != -1'i32:
result = (attributes and FILE_ATTRIBUTE_HIDDEN) != 0'i32
else:
if fileExists(path):
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] != '.')
let fileName = extractFilename(path)
result = len(fileName) >= 2 and fileName[0] == '.' and fileName != ".."

{.pop.}

Expand Down

0 comments on commit 47f6193

Please sign in to comment.