From 2d71997762e0c02a03ea0d94f3c1f79a5f3e84e0 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 2 May 2017 19:28:13 +0200 Subject: Detect opining of directory as file on FreeBSD --- libbutl/fdstream.cxx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libbutl/fdstream.cxx b/libbutl/fdstream.cxx index 946118d..a2d5b09 100644 --- a/libbutl/fdstream.cxx +++ b/libbutl/fdstream.cxx @@ -9,8 +9,8 @@ # include // close(), read(), write(), lseek(), dup(), pipe(), // ssize_t, STD*_FILENO # include // writev(), iovec -# include // S_I* -# include // off_t +# include // stat(), S_I* +# include // stat, off_t #else # include @@ -676,6 +676,18 @@ namespace butl of |= O_LARGEFILE; #endif + // Unlike other platforms, FreeBSD allows opening a directory as a file + // which will cause all kinds of problems upstream (e.g., cpfile()). So we + // detect and diagnose this. + // +#ifdef __FreeBSD__ + { + struct stat s; + if (stat (f, &s) == 0 && S_ISDIR (s.st_mode)) + throw_ios_failure (EISDIR); + } +#endif + int fd (open (f, of | O_CLOEXEC, pf)); #else -- cgit v1.1