changeset 4186:e99d1ad11cfe

fix build
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 14 Sep 2020 19:27:55 +0200
parents b289a1234822
children a8d2f4e65f05
files OrthancFramework/Resources/CMake/BoostConfiguration.cmake OrthancFramework/Sources/SystemToolbox.cpp OrthancFramework/UnitTestsSources/FrameworkTests.cpp
diffstat 3 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Resources/CMake/BoostConfiguration.cmake	Mon Sep 14 18:09:30 2020 +0200
+++ b/OrthancFramework/Resources/CMake/BoostConfiguration.cmake	Mon Sep 14 19:27:55 2020 +0200
@@ -240,7 +240,7 @@
 
 
   ##
-  ## Configuration of boost::filesystem
+  ## Configuration of boost::filesystem and boost::iostreams
   ## 
 
   if (CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
@@ -277,6 +277,10 @@
     endif()
   endif()
 
+  list(APPEND BOOST_SOURCES
+    ${BOOST_NAME}/libs/iostreams/src/file_descriptor.cpp
+    )
+  
 
   ##
   ## Configuration of boost::locale
--- a/OrthancFramework/Sources/SystemToolbox.cpp	Mon Sep 14 18:09:30 2020 +0200
+++ b/OrthancFramework/Sources/SystemToolbox.cpp	Mon Sep 14 19:27:55 2020 +0200
@@ -26,6 +26,7 @@
 
 #if defined(_WIN32)
 #  include <windows.h>
+#  include <fileapi.h>   // For "FlushFileBuffers()"
 #  include <process.h>   // For "_spawnvp()" and "_getpid()"
 #  include <stdlib.h>    // For "environ"
 #else
@@ -307,7 +308,29 @@
     {
       // https://stackoverflow.com/a/23826489/881731
       f.flush();
-      ::fdatasync(f->handle());
+
+      bool success = false;
+
+      /**
+       * "f->handle()" corresponds to "FILE*" (aka "HANDLE") on
+       * Microsoft Windows, and to "int" (file descriptor) on other
+       * systems:
+       * https://github.com/boostorg/iostreams/blob/develop/include/boost/iostreams/detail/file_handle.hpp
+       **/
+      
+#if defined(_WIN32)
+      // https://docs.microsoft.com/fr-fr/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers
+      success = (::FlushFileBuffers(f->handle()) != 0);
+#elif (_POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500)
+      success = (::fdatasync(f->handle()) == 0);
+#else
+      success = (::fsync(f->handle()) == 0);
+#endif
+
+      if (!success)
+      {
+        throw OrthancException(ErrorCode_FileStorageCannotWrite, "Cannot force flush to disk");
+      }
     }
 
     f.close();
--- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Mon Sep 14 18:09:30 2020 +0200
+++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Mon Sep 14 19:27:55 2020 +0200
@@ -603,6 +603,15 @@
 
   std::string u;
   ASSERT_THROW(SystemToolbox::ReadFile(u, path.c_str()), OrthancException);
+
+  {
+    TemporaryFile tmp;
+    std::string s = "Hello";
+    SystemToolbox::WriteFile(s, tmp.GetPath(), true /* call fsync() */);
+    std::string t;
+    SystemToolbox::ReadFile(t, tmp.GetPath());
+    ASSERT_EQ(s, t);
+  }
 }