From 96511be84692cb37ce6ea7313ef5de6624eb3dd7 Mon Sep 17 00:00:00 2001 From: Guodong Jin Date: Mon, 15 Apr 2024 18:19:07 +0800 Subject: [PATCH] add win sync --- src/common/file_system/local_file_system.cpp | 25 +++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/common/file_system/local_file_system.cpp b/src/common/file_system/local_file_system.cpp index e572087854e..5cdab200e09 100644 --- a/src/common/file_system/local_file_system.cpp +++ b/src/common/file_system/local_file_system.cpp @@ -212,13 +212,13 @@ void LocalFileSystem::readFromFile(FileInfo& fileInfo, void* buffer, uint64_t nu throw IOException( stringFormat("Cannot read from file: {} handle: {} " "numBytesRead: {} numBytesToRead: {} position: {}. Error {}: {}", - fileInfo->path, (intptr_t)localFileInfo.handle, numBytesRead, numBytes, position, + fileInfo.path, (intptr_t)localFileInfo.handle, numBytesRead, numBytes, position, error, std::system_category().message(error))); } - if (numBytesRead != numBytes && fileInfo->getFileSize() != position + numBytesRead) { + if (numBytesRead != numBytes && fileInfo.getFileSize() != position + numBytesRead) { throw IOException(stringFormat("Cannot read from file: {} handle: {} " "numBytesRead: {} numBytesToRead: {} position: {}", - fileInfo->path, (intptr_t)localFileInfo.handle, numBytesRead, numBytes, position)); + fileInfo.path, (intptr_t)localFileInfo.handle, numBytesRead, numBytes, position)); } #else auto numBytesRead = pread(localFileInfo.fd, buffer, numBytes, position); @@ -237,7 +237,7 @@ int64_t LocalFileSystem::readFile(FileInfo& fileInfo, void* buf, size_t nbyte) c auto& localFileInfo = ku_dynamic_cast(fileInfo); #if defined(_WIN32) DWORD numBytesRead; - ReadFile((HANDLE)localFileInfo->handle, buf, nbyte, &numBytesRead, nullptr); + ReadFile((HANDLE)localFileInfo.handle, buf, nbyte, &numBytesRead, nullptr); return numBytesRead; #else return read(localFileInfo.fd, buf, nbyte); @@ -265,7 +265,7 @@ void LocalFileSystem::writeFile(FileInfo& fileInfo, const uint8_t* buffer, uint6 throw IOException( stringFormat("Cannot write to file. path: {} handle: {} offsetToWrite: {} " "numBytesToWrite: {} numBytesWritten: {}. Error {}: {}.", - fileInfo->path, (intptr_t)localFileInfo.handle, offset, numBytesToWrite, + fileInfo.path, (intptr_t)localFileInfo.handle, offset, numBytesToWrite, numBytesWritten, error, std::system_category().message(error))); } #else @@ -289,9 +289,16 @@ void LocalFileSystem::writeFile(FileInfo& fileInfo, const uint8_t* buffer, uint6 void LocalFileSystem::syncFile(const FileInfo& fileInfo) const { auto& localFileInfo = ku_dynamic_cast(fileInfo); - if (fsync(localFileInfo.fd)) { +#if defined(_WIN32) + // Note that `FlushFileBuffers` returns 0 when fail, while `fsync` returns 0 when succeed. + if (FlushFileBuffers(localFileInfo.handle) == 0) { throw IOException(stringFormat("Failed to sync file {}.", fileInfo.path)); } +#else + if (fsync(localFileInfo.fd) != 0) { + throw IOException(stringFormat("Failed to sync file {}.", fileInfo.path)); + } +#endif } int64_t LocalFileSystem::seek(FileInfo& fileInfo, uint64_t offset, int whence) const { @@ -319,14 +326,14 @@ void LocalFileSystem::truncate(FileInfo& fileInfo, uint64_t size) const { auto error = GetLastError(); throw IOException(stringFormat("Cannot set file pointer for file: {} handle: {} " "new position: {}. Error {}: {}", - fileInfo->path, (intptr_t)localFileInfo.handle, size, error, + fileInfo.path, (intptr_t)localFileInfo.handle, size, error, std::system_category().message(error))); } if (!SetEndOfFile((HANDLE)localFileInfo.handle)) { auto error = GetLastError(); throw IOException(stringFormat("Cannot truncate file: {} handle: {} " "size: {}. Error {}: {}", - fileInfo->path, (intptr_t)localFileInfo.handle, size, error, + fileInfo.path, (intptr_t)localFileInfo.handle, size, error, std::system_category().message(error))); } #else @@ -346,7 +353,7 @@ uint64_t LocalFileSystem::getFileSize(const FileInfo& fileInfo) const { if (!GetFileSizeEx((HANDLE)localFileInfo.handle, &size)) { auto error = GetLastError(); throw IOException(stringFormat("Cannot read size of file. path: {} - Error {}: {}", - fileInfo->path, error, systemErrMessage(error))); + fileInfo.path, error, systemErrMessage(error))); } return size.QuadPart; #else