Don't attempt to separately close the underlying file descriptor in the format other...
authorChristopher Dykes <cdykes@fb.com>
Sat, 19 Nov 2016 01:54:36 +0000 (17:54 -0800)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Sat, 19 Nov 2016 02:08:53 +0000 (18:08 -0800)
Summary: Windows automatically closes the underlying file descriptor when you call fclose, however fclose is not a function that can be easily overriden in the portability layer, so choose to just not call `close` on Windows instead.

Reviewed By: yfeldblum

Differential Revision: D4190524

fbshipit-source-id: a68edccd04e63f89c178ade584fa7192845773f8

folly/Portability.h
folly/test/FormatOtherTest.cpp

index 1ec55c105fb17de69681a3f9d25b40bae9da872f..77494f95d8b98d4eff02741ecc4ca5eb54be0bec 100644 (file)
@@ -321,4 +321,10 @@ constexpr auto kIsLinux = true;
 #else
 constexpr auto kIsLinux = false;
 #endif
 #else
 constexpr auto kIsLinux = false;
 #endif
+
+#if defined(_WIN32)
+constexpr auto kIsWindows = true;
+#else
+constexpr auto kIsWindows = false;
+#endif
 }
 }
index 466d3c6594b0175f69e08d40c9c468d5b547853e..2882b7a0dbdd2d2b75db9342b0bd965f2b586ce8 100644 (file)
 
 #include <folly/FBVector.h>
 #include <folly/FileUtil.h>
 
 #include <folly/FBVector.h>
 #include <folly/FileUtil.h>
+#include <folly/Portability.h>
 #include <folly/dynamic.h>
 #include <folly/json.h>
 #include <folly/dynamic.h>
 #include <folly/json.h>
-#include <folly/small_vector.h>
 #include <folly/portability/GTest.h>
 #include <folly/portability/GTest.h>
+#include <folly/small_vector.h>
 
 using namespace folly;
 
 
 using namespace folly;
 
@@ -33,7 +34,13 @@ TEST(FormatOther, file) {
   {
     int fds[2];
     CHECK_ERR(pipe(fds));
   {
     int fds[2];
     CHECK_ERR(pipe(fds));
-    SCOPE_EXIT { closeNoInt(fds[1]); };
+    SCOPE_EXIT {
+      // fclose on Windows automatically closes the underlying
+      // file descriptor.
+      if (!kIsWindows) {
+        closeNoInt(fds[1]);
+      }
+    };
     {
       FILE* fp = fdopen(fds[1], "wb");
       PCHECK(fp);
     {
       FILE* fp = fdopen(fds[1], "wb");
       PCHECK(fp);