From 220bb16cbee5b91d0bc0fcc6c71560d631295fa5 Mon Sep 17 00:00:00 2001 From: Evan Klitzke Date: Thu, 15 Mar 2018 06:54:11 -0700 Subject: [PATCH] util: Introduce DirectoryCommit commit function to sync a directory --- src/util/system.cpp | 9 +++++++++ src/util/system.h | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/src/util/system.cpp b/src/util/system.cpp index 01c588b0d31..43c6ae54ab5 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -1043,6 +1043,15 @@ bool FileCommit(FILE *file) return true; } +void DirectoryCommit(const fs::path &dirname) +{ +#ifndef WIN32 + FILE* file = fsbridge::fopen(dirname, "r"); + fsync(fileno(file)); + fclose(file); +#endif +} + bool TruncateFile(FILE *file, unsigned int length) { #if defined(WIN32) return _chsize(_fileno(file), length) == 0; diff --git a/src/util/system.h b/src/util/system.h index d2373998dfc..029204f01e6 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -62,6 +62,13 @@ void PrintExceptionContinue(const std::exception *pex, const char* pszThread); * feature analogous to fsync(). */ bool FileCommit(FILE *file); + +/** + * Sync directory contents. This is required on some environments to ensure that + * newly created files are committed to disk. + */ +void DirectoryCommit(const fs::path &dirname); + bool TruncateFile(FILE *file, unsigned int length); int RaiseFileDescriptorLimit(int nMinFD); void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);