Skip to content

Commit

Permalink
add win sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ray6080 committed Apr 15, 2024
1 parent cb13c5b commit 96511be
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/common/file_system/local_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -237,7 +237,7 @@ int64_t LocalFileSystem::readFile(FileInfo& fileInfo, void* buf, size_t nbyte) c
auto& localFileInfo = ku_dynamic_cast<const FileInfo&, const LocalFileInfo&>(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);
Expand Down Expand Up @@ -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
Expand All @@ -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<const FileInfo&, const LocalFileInfo&>(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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 96511be

Please sign in to comment.