From 14bbfa360cb5e956761ad2ac415c68b421ac28a7 Mon Sep 17 00:00:00 2001 From: def Date: Fri, 11 Mar 2016 17:02:56 +0100 Subject: [PATCH 1/4] Don't open directories as files --- lib/system/sysio.nim | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 78c7b1ca1..df69b0913 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -270,12 +270,30 @@ const # we always use binary here as for Nim the OS line ending # should not be translated. +when defined(posix): + type + Mode {.importc: "mode_t", header: "".} = cint + + Stat {.importc: "struct stat", + header: "", final, pure.} = object ## struct stat + st_mode: Mode ## Mode of file + + proc S_ISDIR(m: Mode): bool {.importc, header: "".} + ## Test for a directory. + + proc fstat(a1: cint, a2: var Stat): cint {.importc, header: "".} proc open(f: var File, filename: string, mode: FileMode = fmRead, bufSize: int = -1): bool = var p: pointer = fopen(filename, FormatOpen[mode]) 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 f = cast[File](p) if bufSize > 0 and bufSize <= high(cint).int: From 7fa3d58167579bd493a026cbea5c815935df4ea6 Mon Sep 17 00:00:00 2001 From: def Date: Wed, 16 Mar 2016 13:04:36 +0100 Subject: [PATCH 2/4] Don't check opened file for directory in nimscript --- lib/system/sysio.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index df69b0913..512bf1f51 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -270,7 +270,7 @@ const # we always use binary here as for Nim the OS line ending # should not be translated. -when defined(posix): +when defined(posix) and not defined(nimscript): type Mode {.importc: "mode_t", header: "".} = cint @@ -288,7 +288,7 @@ proc open(f: var File, filename: string, bufSize: int = -1): bool = var p: pointer = fopen(filename, FormatOpen[mode]) if p != nil: - when defined(posix): + when defined(posix) and not defined(nimscript): var f2 = cast[File](p) var res: Stat if fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode): From d5c332ffa8b0774593c3548167f85d91388b23b1 Mon Sep 17 00:00:00 2001 From: def Date: Thu, 17 Mar 2016 22:22:52 +0100 Subject: [PATCH 3/4] Comment to explain not opening directories in open() --- lib/system/sysio.nim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 512bf1f51..4ced51c96 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -289,6 +289,9 @@ proc open(f: var File, filename: string, var p: pointer = fopen(filename, FormatOpen[mode]) if p != nil: when defined(posix) and not defined(nimscript): + # How `fopen` handles opening a directory is not specified in ISO C and + # POSIX. We do not want to handle directories as regular files that can + # be opened. var f2 = cast[File](p) var res: Stat if fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode): From dfba0bdcafb69fe408048924d86d33bf6718cffa Mon Sep 17 00:00:00 2001 From: def Date: Fri, 18 Mar 2016 19:09:27 +0100 Subject: [PATCH 4/4] Explicitly return false --- lib/system/sysio.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 4ced51c96..d0bba6775 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -296,7 +296,7 @@ proc open(f: var File, filename: string, var res: Stat if fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode): close(f2) - return + return false result = true f = cast[File](p) if bufSize > 0 and bufSize <= high(cint).int: