X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fportability%2FFcntl.cpp;h=5d8e7ec7ed0db8d1dff35346c7e212bc2677ad4a;hb=96ac8f8ff0c9bc9446eede119fdfee0fe7feaba3;hp=e02a767c12f76336232dfe5ed44c7386c5d4d07f;hpb=a0f8eed7f6e9a75451d0b7e7436e998ffeaea112;p=folly.git diff --git a/folly/portability/Fcntl.cpp b/folly/portability/Fcntl.cpp old mode 100755 new mode 100644 index e02a767c..5d8e7ec7 --- a/folly/portability/Fcntl.cpp +++ b/folly/portability/Fcntl.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,9 @@ namespace folly { namespace portability { namespace fcntl { -int creat(char const* fn, int pm) { return _creat(fn, pm); } +int creat(char const* fn, int pm) { + return _creat(fn, pm); +} int fcntl(int fd, int cmd, ...) { va_list args; @@ -36,7 +38,7 @@ int fcntl(int fd, int cmd, ...) { if (h != INVALID_HANDLE_VALUE) { DWORD flags; if (GetHandleInformation(h, &flags)) { - res = flags & HANDLE_FLAG_INHERIT; + res = int(flags & HANDLE_FLAG_INHERIT); } } break; @@ -59,17 +61,21 @@ int fcntl(int fd, int cmd, ...) { } case F_SETFL: { int flags = va_arg(args, int); - if (flags & O_NONBLOCK) { - // If it's not a socket, it's probably a pipe, and - // those are non-blocking by default with Windows. - if (folly::portability::sockets::is_fh_socket(fd)) { - SOCKET s = (SOCKET)_get_osfhandle(fd); - if (s != INVALID_SOCKET) { - u_long nonBlockingEnabled = 1; - res = ioctlsocket(s, FIONBIO, &nonBlockingEnabled); + // If it's not a socket, it's probably a pipe. + if (folly::portability::sockets::is_fh_socket(fd)) { + SOCKET s = (SOCKET)_get_osfhandle(fd); + if (s != INVALID_SOCKET) { + u_long nonBlockingEnabled = (flags & O_NONBLOCK) ? 1 : 0; + res = ioctlsocket(s, FIONBIO, &nonBlockingEnabled); + } + } else { + HANDLE p = (HANDLE)_get_osfhandle(fd); + if (GetFileType(p) == FILE_TYPE_PIPE) { + DWORD newMode = PIPE_READMODE_BYTE; + newMode |= (flags & O_NONBLOCK) ? PIPE_NOWAIT : PIPE_WAIT; + if (SetNamedPipeHandleState(p, &newMode, nullptr, nullptr)) { + res = 0; } - } else { - res = 0; } } break; @@ -91,6 +97,11 @@ int open(char const* fn, int of, int pm) { // none are. return -1; } + if (!strcmp(fn, "/dev/null")) { + // Windows doesn't have a /dev/null, but it does have + // NUL, which achieves the same result. + fn = "NUL"; + } errno_t res = _sopen_s(&fh, fn, of, _SH_DENYNO, realMode); return res ? -1 : fh; }