Implement Unix file regularity check (#20448) (#20628)

* Implement Unix file regularity check

* update std/dirs also
This commit is contained in:
Andrey Makarov 2022-10-25 08:42:47 +03:00 • committed by GitHub
commit 8ed2431db0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 107 additions and 45 deletions

View file

@ -1095,6 +1095,8 @@ type
creationTime*: times.Time ## Time file was created. Not supported on all systems!
blockSize*: int ## Preferred I/O block size for this object.
## In some filesystems, this may vary from file to file.
isRegular*: bool ## Is file regular? (on Unix some "files"
## can be non-regular like FIFOs, devices)
template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
## Transforms the native file info structure into the one nim uses.
@ -1155,14 +1157,14 @@ template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
checkAndIncludeMode(S_IWOTH, fpOthersWrite)
checkAndIncludeMode(S_IXOTH, fpOthersExec)
formalInfo.kind =
(formalInfo.kind, formalInfo.isRegular) =
if S_ISDIR(rawInfo.st_mode):
pcDir
(pcDir, true)
elif S_ISLNK(rawInfo.st_mode):
assert(path != "") # symlinks can't occur for file handles
getSymlinkFileKind(path)
else:
pcFile
(pcFile, S_ISREG(rawInfo.st_mode))
when defined(js):
when not declared(FileHandle):
@ -1215,7 +1217,8 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo {.noWeirdTarget.
##
## When `followSymlink` is true (default), symlinks are followed and the
## information retrieved is information related to the symlink's target.
## Otherwise, information on the symlink itself is retrieved.
## Otherwise, information on the symlink itself is retrieved (however,
## field `isRegular` is still determined from the target on Unix).
##
## If the information cannot be retrieved, such as when the path doesn't
## exist, or when permission restrictions prevent the program from retrieving