@@ -638,21 +638,37 @@ bool TryCreateDirectory(const fs::path& p)
638638 return false ;
639639}
640640
641- void FileCommit (FILE* fileout )
641+ bool FileCommit (FILE* file )
642642{
643- fflush (fileout); // harmless if redundantly called
643+ if (fflush (file) != 0 ) { // harmless if redundantly called
644+ LogPrintf (" %s: fflush failed: %d\n " , __func__, errno);
645+ return false ;
646+ }
644647#ifdef WIN32
645- HANDLE hFile = (HANDLE)_get_osfhandle (_fileno (fileout));
646- FlushFileBuffers (hFile);
647- #else
648- #if defined(__linux__) || defined(__NetBSD__)
649- fdatasync (fileno (fileout));
650- #elif defined(__APPLE__) && defined(F_FULLFSYNC)
651- fcntl (fileno (fileout), F_FULLFSYNC, 0 );
648+ HANDLE hFile = (HANDLE)_get_osfhandle (_fileno (file));
649+ if (FlushFileBuffers (hFile) == 0 ) {
650+ LogPrintf (" %s: FlushFileBuffers failed: %d\n " , __func__, GetLastError ());
651+ return false ;
652+ }
652653#else
653- fsync (fileno (fileout));
654- #endif
654+ #if defined(__linux__) || defined(__NetBSD__)
655+ if (fdatasync (fileno (file)) != 0 && errno != EINVAL) { // Ignore EINVAL for filesystems that don't support sync
656+ LogPrintf (" %s: fdatasync failed: %d\n " , __func__, errno);
657+ return false ;
658+ }
659+ #elif defined(__APPLE__) && defined(F_FULLFSYNC)
660+ if (fcntl (fileno (file), F_FULLFSYNC, 0 ) == -1 ) { // Manpage says "value other than -1" is returned on success
661+ LogPrintf (" %s: fcntl F_FULLFSYNC failed: %d\n " , __func__, errno);
662+ return false ;
663+ }
664+ #else
665+ if (fsync (fileno (file)) != 0 && errno != EINVAL) {
666+ LogPrintf (" %s: fsync failed: %d\n " , __func__, errno);
667+ return false ;
668+ }
669+ #endif
655670#endif
671+ return true ;
656672}
657673
658674bool TruncateFile (FILE* file, unsigned int length)
0 commit comments