Don't open directories as files
This commit is contained in:
parent
1073f9ec5a
commit
14bbfa360c
1 changed files with 18 additions and 0 deletions
|
|
@ -270,12 +270,30 @@ const
|
||||||
# we always use binary here as for Nim the OS line ending
|
# we always use binary here as for Nim the OS line ending
|
||||||
# should not be translated.
|
# should not be translated.
|
||||||
|
|
||||||
|
when defined(posix):
|
||||||
|
type
|
||||||
|
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
|
||||||
|
|
||||||
|
Stat {.importc: "struct stat",
|
||||||
|
header: "<sys/stat.h>", final, pure.} = object ## struct stat
|
||||||
|
st_mode: Mode ## Mode of file
|
||||||
|
|
||||||
|
proc S_ISDIR(m: Mode): bool {.importc, header: "<sys/stat.h>".}
|
||||||
|
## Test for a directory.
|
||||||
|
|
||||||
|
proc fstat(a1: cint, a2: var Stat): cint {.importc, header: "<sys/stat.h>".}
|
||||||
|
|
||||||
proc open(f: var File, filename: string,
|
proc open(f: var File, filename: string,
|
||||||
mode: FileMode = fmRead,
|
mode: FileMode = fmRead,
|
||||||
bufSize: int = -1): bool =
|
bufSize: int = -1): bool =
|
||||||
var p: pointer = fopen(filename, FormatOpen[mode])
|
var p: pointer = fopen(filename, FormatOpen[mode])
|
||||||
if p != nil:
|
if p != nil:
|
||||||
|
when defined(posix):
|
||||||
|
var f2 = cast[File](p)
|
||||||
|
var res: Stat
|
||||||
|
if fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode):
|
||||||
|
close(f2)
|
||||||
|
return
|
||||||
result = true
|
result = true
|
||||||
f = cast[File](p)
|
f = cast[File](p)
|
||||||
if bufSize > 0 and bufSize <= high(cint).int:
|
if bufSize > 0 and bufSize <= high(cint).int:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue