Fix the mode being used in the implementation of open in the Fcntl portability header
authorChristopher Dykes <cdykes@fb.com>
Wed, 3 Aug 2016 22:09:29 +0000 (15:09 -0700)
committerFacebook Github Bot 4 <facebook-github-bot-4-bot@fb.com>
Wed, 3 Aug 2016 22:23:49 +0000 (15:23 -0700)
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

index be0a428eafe72d66f8a3e64dbe09c5de679bc6a3..e02a767c12f76336232dfe5ed44c7386c5d4d07f 100755 (executable)
@@ -18,6 +18,7 @@
 
 #ifdef _WIN32
 #include <folly/portability/Sockets.h>
+#include <folly/portability/SysStat.h>
 #include <folly/portability/Windows.h>
 
 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;
 }