Fcntl.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/portability/Fcntl.h>
  17. #ifdef _WIN32
  18. #include <folly/portability/Sockets.h>
  19. #include <folly/portability/SysStat.h>
  20. #include <folly/portability/Windows.h>
  21. namespace folly {
  22. namespace portability {
  23. namespace fcntl {
  24. int creat(char const* fn, int pm) {
  25. return _creat(fn, pm);
  26. }
  27. int fcntl(int fd, int cmd, ...) {
  28. va_list args;
  29. int res = -1;
  30. va_start(args, cmd);
  31. switch (cmd) {
  32. case F_GETFD: {
  33. HANDLE h = (HANDLE)_get_osfhandle(fd);
  34. if (h != INVALID_HANDLE_VALUE) {
  35. DWORD flags;
  36. if (GetHandleInformation(h, &flags)) {
  37. res = int(flags & HANDLE_FLAG_INHERIT);
  38. }
  39. }
  40. break;
  41. }
  42. case F_SETFD: {
  43. int flags = va_arg(args, int);
  44. HANDLE h = (HANDLE)_get_osfhandle(fd);
  45. if (h != INVALID_HANDLE_VALUE) {
  46. if (SetHandleInformation(
  47. h, HANDLE_FLAG_INHERIT, (DWORD)(flags & FD_CLOEXEC))) {
  48. res = 0;
  49. }
  50. }
  51. break;
  52. }
  53. case F_GETFL: {
  54. // No idea how to get the IO blocking mode, so return 0.
  55. res = 0;
  56. break;
  57. }
  58. case F_SETFL: {
  59. int flags = va_arg(args, int);
  60. // If it's not a socket, it's probably a pipe.
  61. if (folly::portability::sockets::is_fh_socket(fd)) {
  62. SOCKET s = (SOCKET)_get_osfhandle(fd);
  63. if (s != INVALID_SOCKET) {
  64. u_long nonBlockingEnabled = (flags & O_NONBLOCK) ? 1 : 0;
  65. res = ioctlsocket(s, FIONBIO, &nonBlockingEnabled);
  66. }
  67. } else {
  68. HANDLE p = (HANDLE)_get_osfhandle(fd);
  69. if (GetFileType(p) == FILE_TYPE_PIPE) {
  70. DWORD newMode = PIPE_READMODE_BYTE;
  71. newMode |= (flags & O_NONBLOCK) ? PIPE_NOWAIT : PIPE_WAIT;
  72. if (SetNamedPipeHandleState(p, &newMode, nullptr, nullptr)) {
  73. res = 0;
  74. }
  75. }
  76. }
  77. break;
  78. }
  79. }
  80. va_end(args);
  81. return res;
  82. }
  83. int open(char const* fn, int of, int pm) {
  84. int fh;
  85. int realMode = _S_IREAD;
  86. if ((of & _O_RDWR) == _O_RDWR) {
  87. realMode = _S_IREAD | _S_IWRITE;
  88. } else if ((of & _O_WRONLY) == _O_WRONLY) {
  89. realMode = _S_IWRITE;
  90. } else if ((of & _O_RDONLY) != _O_RDONLY) {
  91. // One of these needs to be present, just fail if
  92. // none are.
  93. return -1;
  94. }
  95. if (!strcmp(fn, "/dev/null")) {
  96. // Windows doesn't have a /dev/null, but it does have
  97. // NUL, which achieves the same result.
  98. fn = "NUL";
  99. }
  100. errno_t res = _sopen_s(&fh, fn, of, _SH_DENYNO, realMode);
  101. return res ? -1 : fh;
  102. }
  103. int posix_fallocate(int fd, off_t offset, off_t len) {
  104. // We'll pretend we always have enough space. We
  105. // can't exactly pre-allocate on windows anyways.
  106. return 0;
  107. }
  108. } // namespace fcntl
  109. } // namespace portability
  110. } // namespace folly
  111. #endif