comparison OrthancFramework/Sources/SystemToolbox.cpp @ 4186:e99d1ad11cfe

fix build
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 14 Sep 2020 19:27:55 +0200
parents b289a1234822
children a2f1c97002fe
comparison
equal deleted inserted replaced
4185:b289a1234822 4186:e99d1ad11cfe
24 #include "SystemToolbox.h" 24 #include "SystemToolbox.h"
25 25
26 26
27 #if defined(_WIN32) 27 #if defined(_WIN32)
28 # include <windows.h> 28 # include <windows.h>
29 # include <fileapi.h> // For "FlushFileBuffers()"
29 # include <process.h> // For "_spawnvp()" and "_getpid()" 30 # include <process.h> // For "_spawnvp()" and "_getpid()"
30 # include <stdlib.h> // For "environ" 31 # include <stdlib.h> // For "environ"
31 #else 32 #else
32 # include <unistd.h> // For "execvp()" 33 # include <unistd.h> // For "execvp()"
33 # include <sys/wait.h> // For "waitpid()" 34 # include <sys/wait.h> // For "waitpid()"
305 306
306 if (callFsync) 307 if (callFsync)
307 { 308 {
308 // https://stackoverflow.com/a/23826489/881731 309 // https://stackoverflow.com/a/23826489/881731
309 f.flush(); 310 f.flush();
310 ::fdatasync(f->handle()); 311
312 bool success = false;
313
314 /**
315 * "f->handle()" corresponds to "FILE*" (aka "HANDLE") on
316 * Microsoft Windows, and to "int" (file descriptor) on other
317 * systems:
318 * https://github.com/boostorg/iostreams/blob/develop/include/boost/iostreams/detail/file_handle.hpp
319 **/
320
321 #if defined(_WIN32)
322 // https://docs.microsoft.com/fr-fr/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers
323 success = (::FlushFileBuffers(f->handle()) != 0);
324 #elif (_POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500)
325 success = (::fdatasync(f->handle()) == 0);
326 #else
327 success = (::fsync(f->handle()) == 0);
328 #endif
329
330 if (!success)
331 {
332 throw OrthancException(ErrorCode_FileStorageCannotWrite, "Cannot force flush to disk");
333 }
311 } 334 }
312 335
313 f.close(); 336 f.close();
314 } 337 }
315 338