Use native Window API for file access.

PiperOrigin-RevId: 791371696
This commit is contained in:
Francis Tsui
2025-08-05 14:32:42 -07:00
committed by Copybara-Service
parent 7472c810d3
commit e66394d4f7
7 changed files with 289 additions and 86 deletions
@@ -14,10 +14,11 @@
#include "internal/platform/implementation/windows/file.h"
#include <fileapi.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <ios>
#include <limits>
#include <memory>
#include <string>
@@ -28,8 +29,7 @@
#include "internal/platform/implementation/windows/string_utils.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace windows {
namespace nearby::windows {
// InputFile
std::unique_ptr<IOFile> IOFile::CreateInputFile(absl::string_view file_path,
@@ -39,91 +39,93 @@ std::unique_ptr<IOFile> IOFile::CreateInputFile(absl::string_view file_path,
IOFile::IOFile(absl::string_view file_path, size_t size) : path_(file_path) {
// Always open input file path as wide string on Windows platform.
std::wstring wide_path =
string_utils::StringToWideString(std::string(file_path));
file_.open(wide_path, std::ios::binary | std::ios::in | std::ios::ate);
total_size_ = file_.tellg();
if (total_size_ == -1) {
// Unsure why it consistently returns -1 when the file size exceeds 2GB. If
// obtaining the file size through tellg fails, use the size provided
// in the parameters.
total_size_ = size;
std::wstring wide_path = string_utils::StringToWideString(path_);
file_ = ::CreateFileW(wide_path.data(), GENERIC_READ, FILE_SHARE_READ,
/*lpSecurityAttributes=*/nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
/*hTemplateFile=*/nullptr);
if (file_ == INVALID_HANDLE_VALUE) {
LOG(ERROR) << "Failed to open input file: " << file_path
<< " with error: " << ::GetLastError();
return;
}
file_.seekg(0);
LARGE_INTEGER file_size;
if (::GetFileSizeEx(file_, &file_size) == 0) {
LOG(ERROR) << "Failed to get file size: " << file_path
<< " with error: " << ::GetLastError();
return;
}
total_size_ = file_size.QuadPart;
}
std::unique_ptr<IOFile> IOFile::CreateOutputFile(absl::string_view path) {
return std::unique_ptr<IOFile>(new IOFile(path));
}
IOFile::IOFile(absl::string_view file_path)
: file_(), path_(file_path), total_size_(0) {
IOFile::IOFile(absl::string_view file_path) : path_(file_path), total_size_(0) {
// Always open input file path as wide string on Windows platform.
std::wstring wide_path = string_utils::StringToWideString(path_);
file_.open(wide_path, std::ios::binary | std::ios::out);
}
ExceptionOr<ByteArray> IOFile::Read(std::int64_t size) {
try {
if (!file_.is_open()) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
if (file_.peek() == EOF) {
return ExceptionOr<ByteArray>{ByteArray{}};
}
if (!file_.good()) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
if (buffer_.size() < size) {
buffer_.resize(size);
}
file_.read(buffer_.data(), static_cast<ptrdiff_t>(size));
auto num_bytes_read = file_.gcount();
if (num_bytes_read == 0) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
return ExceptionOr<ByteArray>(ByteArray(buffer_.data(), num_bytes_read));
} catch (...) {
LOG(ERROR) << "Fail to read";
return ExceptionOr<ByteArray>{Exception::kIo};
file_ = ::CreateFileW(wide_path.data(), GENERIC_WRITE, /*dwShareMode=*/0,
/*lpSecurityAttributes=*/nullptr, CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
/*hTemplateFile=*/nullptr);
if (file_ == INVALID_HANDLE_VALUE) {
LOG(ERROR) << "Failed to open output file: " << file_path
<< " with error: " << ::GetLastError();
return;
}
}
ExceptionOr<ByteArray> IOFile::Read(std::int64_t size) {
if (file_ == INVALID_HANDLE_VALUE) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
// ReadFile API only supports int32_t size.
if (size > std::numeric_limits<std::uint32_t>::max()) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
if (buffer_.size() < size) {
buffer_.resize(size);
}
DWORD bytes_read = 0;
if (::ReadFile(file_, buffer_.data(), size, &bytes_read,
/*lpOverlapped=*/nullptr) == 0) {
LOG(ERROR) << "Failed to read file: " << path_
<< " with error: " << ::GetLastError();
return ExceptionOr<ByteArray>{Exception::kIo};
}
if (bytes_read == 0) {
return ExceptionOr<ByteArray>{ByteArray{}};
}
return ExceptionOr<ByteArray>(ByteArray(buffer_.data(), bytes_read));
}
Exception IOFile::Close() {
if (file_.is_open()) {
file_.close();
if (file_ != INVALID_HANDLE_VALUE) {
::CloseHandle(file_);
file_ = INVALID_HANDLE_VALUE;
}
return {Exception::kSuccess};
}
Exception IOFile::Write(const ByteArray& data) {
try {
if (!file_.is_open()) {
return {Exception::kIo};
}
if (!file_.good()) {
return {Exception::kIo};
}
file_.write(data.data(), data.size());
return {file_.good() ? Exception::kSuccess : Exception::kIo};
} catch (...) {
LOG(ERROR) << "Fail to write";
if (file_ == INVALID_HANDLE_VALUE) {
return {Exception::kIo};
}
// WriteFile API only supports int32_t size.
if (data.size() > std::numeric_limits<std::uint32_t>::max()) {
return {Exception::kIo};
}
DWORD bytes_written = 0;
if (::WriteFile(file_, data.data(), data.size(), &bytes_written,
/*lpOverlapped=*/nullptr) == 0 ||
bytes_written != data.size()) {
LOG(ERROR) << "Failed to write file: " << path_
<< " with error: " << ::GetLastError();
return {Exception::kIo};
}
return {Exception::kSuccess};
}
Exception IOFile::Flush() {
file_.flush();
return {file_.good() ? Exception::kSuccess : Exception::kIo};
}
} // namespace windows
} // namespace nearby
} // namespace nearby::windows