From a0f8eed7f6e9a75451d0b7e7436e998ffeaea112 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Wed, 3 Aug 2016 15:09:29 -0700 Subject: [PATCH] Fix the mode being used in the implementation of open in the Fcntl portability header Summary: The mode parameter to `open` under MSVC is not the same as the mode parameter everywhere else, so we need to do a bit of translation. Reviewed By: yfeldblum Differential Revision: D3651218 fbshipit-source-id: 80df1e15f34b8d66533256107d8c9218f757fde2 --- folly/portability/Fcntl.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/folly/portability/Fcntl.cpp b/folly/portability/Fcntl.cpp index be0a428e..e02a767c 100755 --- a/folly/portability/Fcntl.cpp +++ b/folly/portability/Fcntl.cpp @@ -18,6 +18,7 @@ #ifdef _WIN32 #include +#include #include namespace folly { @@ -80,7 +81,17 @@ int fcntl(int fd, int cmd, ...) { int open(char const* fn, int of, int pm) { int fh; - errno_t res = _sopen_s(&fh, fn, of, _SH_DENYNO, pm); + int realMode = _S_IREAD; + if ((of & _O_RDWR) == _O_RDWR) { + realMode = _S_IREAD | _S_IWRITE; + } else if ((of & _O_WRONLY) == _O_WRONLY) { + realMode = _S_IWRITE; + } else if ((of & _O_RDONLY) != _O_RDONLY) { + // One of these needs to be present, just fail if + // none are. + return -1; + } + errno_t res = _sopen_s(&fh, fn, of, _SH_DENYNO, realMode); return res ? -1 : fh; } -- 2.34.1