Merge pull request #1461 from Varriount/os/add-hiddenFile
Add isHidden procedure to os.nim
This commit is contained in:
commit
3cdc328952
1 changed files with 29 additions and 5 deletions
|
|
@ -365,6 +365,7 @@ when defined(windows):
|
||||||
template getFilename(f: expr): expr = $f.cFilename
|
template getFilename(f: expr): expr = $f.cFilename
|
||||||
|
|
||||||
proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} =
|
proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} =
|
||||||
|
# Note - takes advantage of null delimiter in the cstring
|
||||||
const dot = ord('.')
|
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)
|
f.cFileName[1].int == dot and f.cFileName[2].int == 0)
|
||||||
|
|
@ -1861,12 +1862,12 @@ proc expandTilde*(path: string): string =
|
||||||
|
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
type
|
type
|
||||||
DeviceId = int32
|
DeviceId* = int32
|
||||||
FileId = int64
|
FileId* = int64
|
||||||
else:
|
else:
|
||||||
type
|
type
|
||||||
DeviceId = TDev
|
DeviceId* = TDev
|
||||||
FileId = TIno
|
FileId* = TIno
|
||||||
|
|
||||||
type
|
type
|
||||||
FileInfo* = object
|
FileInfo* = object
|
||||||
|
|
@ -1909,6 +1910,7 @@ template rawToFormalFileInfo(rawInfo, formalInfo): expr =
|
||||||
if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT) != 0'i32:
|
if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT) != 0'i32:
|
||||||
formalInfo.kind = succ(result.kind)
|
formalInfo.kind = succ(result.kind)
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
template checkAndIncludeMode(rawMode, formalMode: expr) =
|
template checkAndIncludeMode(rawMode, formalMode: expr) =
|
||||||
if (rawInfo.st_mode and rawMode) != 0'i32:
|
if (rawInfo.st_mode and rawMode) != 0'i32:
|
||||||
|
|
@ -1995,4 +1997,26 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo =
|
||||||
osError(osLastError())
|
osError(osLastError())
|
||||||
rawToFormalFileInfo(rawInfo, result)
|
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. 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:
|
||||||
|
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] != '.')
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue