From 4e0a5b3a1086177f917296c53191107c395f5b34 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Wed, 4 May 2016 16:45:53 -0700 Subject: [PATCH] Create the fcntl.h portability header Summary: Windows has it, but all it does is define a few constants. Reviewed By: yfeldblum Differential Revision: D3002161 fb-gh-sync-id: a719faf391125522b2cb21471a56e8a9fe894225 fbshipit-source-id: a719faf391125522b2cb21471a56e8a9fe894225 --- folly/Makefile.am | 2 + folly/portability/Fcntl.cpp | 95 +++++++++++++++++++++++++++++++++++++ folly/portability/Fcntl.h | 53 +++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100755 folly/portability/Fcntl.cpp create mode 100755 folly/portability/Fcntl.h diff --git a/folly/Makefile.am b/folly/Makefile.am index f8c616b6..69887388 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -278,6 +278,7 @@ nobase_follyinclude_HEADERS = \ portability/Constexpr.h \ portability/Dirent.h \ portability/Environment.h \ + portability/Fcntl.h \ portability/GFlags.h \ portability/IOVec.h \ portability/Libgen.h \ @@ -426,6 +427,7 @@ libfolly_la_SOURCES = \ MemoryMapping.cpp \ portability/Dirent.cpp \ portability/Environment.cpp \ + portability/Fcntl.cpp \ portability/Libgen.cpp \ portability/Malloc.cpp \ portability/Memory.cpp \ diff --git a/folly/portability/Fcntl.cpp b/folly/portability/Fcntl.cpp new file mode 100755 index 00000000..be0a428e --- /dev/null +++ b/folly/portability/Fcntl.cpp @@ -0,0 +1,95 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#ifdef _WIN32 +#include +#include + +namespace folly { +namespace portability { +namespace fcntl { +int creat(char const* fn, int pm) { return _creat(fn, pm); } + +int fcntl(int fd, int cmd, ...) { + va_list args; + int res = -1; + va_start(args, cmd); + switch (cmd) { + case F_GETFD: { + HANDLE h = (HANDLE)_get_osfhandle(fd); + if (h != INVALID_HANDLE_VALUE) { + DWORD flags; + if (GetHandleInformation(h, &flags)) { + res = flags & HANDLE_FLAG_INHERIT; + } + } + break; + } + case F_SETFD: { + int flags = va_arg(args, int); + HANDLE h = (HANDLE)_get_osfhandle(fd); + if (h != INVALID_HANDLE_VALUE) { + if (SetHandleInformation( + h, HANDLE_FLAG_INHERIT, (DWORD)(flags & FD_CLOEXEC))) { + res = 0; + } + } + break; + } + case F_GETFL: { + // No idea how to get the IO blocking mode, so return 0. + res = 0; + break; + } + 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); + } + } else { + res = 0; + } + } + break; + } + } + va_end(args); + return res; +} + +int open(char const* fn, int of, int pm) { + int fh; + errno_t res = _sopen_s(&fh, fn, of, _SH_DENYNO, pm); + return res ? -1 : fh; +} + +int posix_fallocate(int fd, off_t offset, off_t len) { + // We'll pretend we always have enough space. We + // can't exactly pre-allocate on windows anyways. + return 0; +} +} +} +} +#endif diff --git a/folly/portability/Fcntl.h b/folly/portability/Fcntl.h new file mode 100755 index 00000000..ba97dbb2 --- /dev/null +++ b/folly/portability/Fcntl.h @@ -0,0 +1,53 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#ifdef _WIN32 +#include + +// I have no idea what the normal values for these are, +// and really don't care what they are. They're only used +// within fcntl, so it's not an issue. +#define FD_CLOEXEC HANDLE_FLAG_INHERIT +#define O_NONBLOCK 1 +#define F_GETFD 1 +#define F_SETFD 2 +#define F_GETFL 3 +#define F_SETFL 4 + +#ifdef HAVE_POSIX_FALLOCATE +#undef HAVE_POSIX_FALLOCATE +#endif +#define HAVE_POSIX_FALLOCATE 1 + +// See portability/Unistd.h for why these need to be in a namespace +// rather then extern "C". +namespace folly { +namespace portability { +namespace fcntl { +int creat(char const* fn, int pm); +int fcntl(int fd, int cmd, ...); +int posix_fallocate(int fd, off_t offset, off_t len); +int open(char const* fn, int of, int pm = 0); +} +} +} + +/* using override */ using namespace folly::portability::fcntl; +#endif -- 2.34.1