mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-21 14:55:24 +00:00
os: always use pcLinkToDir as kind for links to directories
Only the OS-specific case in walkDir() handled that correctly. Adapt the general case and handle it in getFileInfo().
This commit is contained in:
@@ -865,6 +865,20 @@ type
|
|||||||
|
|
||||||
{.deprecated: [TPathComponent: PathComponent].}
|
{.deprecated: [TPathComponent: PathComponent].}
|
||||||
|
|
||||||
|
|
||||||
|
when defined(posix):
|
||||||
|
proc getSymlinkFileKind(path: string): PathComponent =
|
||||||
|
# Helper function.
|
||||||
|
var s: Stat
|
||||||
|
assert(path != "")
|
||||||
|
if stat(path, s) < 0'i32:
|
||||||
|
raiseOSError(osLastError())
|
||||||
|
if S_ISDIR(s.st_mode):
|
||||||
|
result = pcLinkToDir
|
||||||
|
else:
|
||||||
|
result = pcLinkToFile
|
||||||
|
|
||||||
|
|
||||||
proc staticWalkDir(dir: string; relative: bool): seq[
|
proc staticWalkDir(dir: string; relative: bool): seq[
|
||||||
tuple[kind: PathComponent, path: string]] =
|
tuple[kind: PathComponent, path: string]] =
|
||||||
discard
|
discard
|
||||||
@@ -931,13 +945,15 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
|||||||
if x.d_type == DT_DIR: k = pcDir
|
if x.d_type == DT_DIR: k = pcDir
|
||||||
if x.d_type == DT_LNK:
|
if x.d_type == DT_LNK:
|
||||||
if dirExists(y): k = pcLinkToDir
|
if dirExists(y): k = pcLinkToDir
|
||||||
else: k = succ(k)
|
else: k = pcLinkToFile
|
||||||
yield (k, y)
|
yield (k, y)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if lstat(y, s) < 0'i32: break
|
if lstat(y, s) < 0'i32: break
|
||||||
if S_ISDIR(s.st_mode): k = pcDir
|
if S_ISDIR(s.st_mode):
|
||||||
if S_ISLNK(s.st_mode): k = succ(k)
|
k = pcDir
|
||||||
|
elif S_ISLNK(s.st_mode):
|
||||||
|
k = getSymlinkFileKind(y)
|
||||||
yield (k, y)
|
yield (k, y)
|
||||||
|
|
||||||
iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {.
|
iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {.
|
||||||
@@ -1502,7 +1518,7 @@ type
|
|||||||
lastWriteTime*: Time # Time file was last modified/written to.
|
lastWriteTime*: Time # Time file was last modified/written to.
|
||||||
creationTime*: Time # Time file was created. Not supported on all systems!
|
creationTime*: Time # Time file was created. Not supported on all systems!
|
||||||
|
|
||||||
template rawToFormalFileInfo(rawInfo, formalInfo): untyped =
|
template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
||||||
## Transforms the native file info structure into the one nim uses.
|
## Transforms the native file info structure into the one nim uses.
|
||||||
## 'rawInfo' is either a 'TBY_HANDLE_FILE_INFORMATION' structure on Windows,
|
## 'rawInfo' is either a 'TBY_HANDLE_FILE_INFORMATION' structure on Windows,
|
||||||
## or a 'Stat' structure on posix
|
## or a 'Stat' structure on posix
|
||||||
@@ -1557,8 +1573,11 @@ template rawToFormalFileInfo(rawInfo, formalInfo): untyped =
|
|||||||
checkAndIncludeMode(S_IXOTH, fpOthersExec)
|
checkAndIncludeMode(S_IXOTH, fpOthersExec)
|
||||||
|
|
||||||
formalInfo.kind = pcFile
|
formalInfo.kind = pcFile
|
||||||
if S_ISDIR(rawInfo.st_mode): formalInfo.kind = pcDir
|
if S_ISDIR(rawInfo.st_mode):
|
||||||
if S_ISLNK(rawInfo.st_mode): formalInfo.kind.inc()
|
formalInfo.kind = pcDir
|
||||||
|
elif S_ISLNK(rawInfo.st_mode):
|
||||||
|
assert(path != "") # symlinks can't occur for file handles
|
||||||
|
formalInfo.kind = getSymlinkFileKind(path)
|
||||||
|
|
||||||
proc getFileInfo*(handle: FileHandle): FileInfo =
|
proc getFileInfo*(handle: FileHandle): FileInfo =
|
||||||
## Retrieves file information for the file object represented by the given
|
## Retrieves file information for the file object represented by the given
|
||||||
@@ -1574,12 +1593,12 @@ proc getFileInfo*(handle: FileHandle): FileInfo =
|
|||||||
var realHandle = get_osfhandle(handle)
|
var realHandle = get_osfhandle(handle)
|
||||||
if getFileInformationByHandle(realHandle, addr rawInfo) == 0:
|
if getFileInformationByHandle(realHandle, addr rawInfo) == 0:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
rawToFormalFileInfo(rawInfo, result)
|
rawToFormalFileInfo(rawInfo, "", result)
|
||||||
else:
|
else:
|
||||||
var rawInfo: Stat
|
var rawInfo: Stat
|
||||||
if fstat(handle, rawInfo) < 0'i32:
|
if fstat(handle, rawInfo) < 0'i32:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
rawToFormalFileInfo(rawInfo, result)
|
rawToFormalFileInfo(rawInfo, "", result)
|
||||||
|
|
||||||
proc getFileInfo*(file: File): FileInfo =
|
proc getFileInfo*(file: File): FileInfo =
|
||||||
if file.isNil:
|
if file.isNil:
|
||||||
@@ -1608,7 +1627,7 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo =
|
|||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
if getFileInformationByHandle(handle, addr rawInfo) == 0:
|
if getFileInformationByHandle(handle, addr rawInfo) == 0:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
rawToFormalFileInfo(rawInfo, result)
|
rawToFormalFileInfo(rawInfo, path, result)
|
||||||
discard closeHandle(handle)
|
discard closeHandle(handle)
|
||||||
else:
|
else:
|
||||||
var rawInfo: Stat
|
var rawInfo: Stat
|
||||||
@@ -1618,7 +1637,7 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo =
|
|||||||
else:
|
else:
|
||||||
if lstat(path, rawInfo) < 0'i32:
|
if lstat(path, rawInfo) < 0'i32:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
rawToFormalFileInfo(rawInfo, result)
|
rawToFormalFileInfo(rawInfo, path, result)
|
||||||
|
|
||||||
proc isHidden*(path: string): bool =
|
proc isHidden*(path: string): bool =
|
||||||
## Determines whether a given path is hidden or not. Returns false if the
|
## Determines whether a given path is hidden or not. Returns false if the
|
||||||
|
|||||||
Reference in New Issue
Block a user