diff --git a/internal/platform/implementation/apple/platform.mm b/internal/platform/implementation/apple/platform.mm index 2280f513..32ecb7e3 100644 --- a/internal/platform/implementation/apple/platform.mm +++ b/internal/platform/implementation/apple/platform.mm @@ -133,7 +133,7 @@ std::unique_ptr ImplementationPlatform::CreateInputFile(PayloadId pay std::unique_ptr ImplementationPlatform::CreateInputFile(const std::string& file_path, size_t size) { - return shared::IOFile::CreateInputFile(file_path, size); + return shared::IOFile::CreateInputFile(file_path); } ABSL_DEPRECATED("This interface will be deleted in the near future.") diff --git a/internal/platform/implementation/g3/platform.cc b/internal/platform/implementation/g3/platform.cc index 6c3a60d7..f62a27cb 100644 --- a/internal/platform/implementation/g3/platform.cc +++ b/internal/platform/implementation/g3/platform.cc @@ -143,12 +143,12 @@ std::unique_ptr ImplementationPlatform::CreateInputFile( std::string parent_folder(""); std::string file_name(std::to_string(payload_id)); return shared::IOFile::CreateInputFile( - GetDownloadPath(parent_folder, file_name), total_size); + GetDownloadPath(parent_folder, file_name)); } std::unique_ptr ImplementationPlatform::CreateInputFile( const std::string& file_path, size_t size) { - return shared::IOFile::CreateInputFile(file_path, size); + return shared::IOFile::CreateInputFile(file_path); } ABSL_DEPRECATED("This interface will be deleted in the near future.") diff --git a/internal/platform/implementation/shared/BUILD b/internal/platform/implementation/shared/BUILD index ce871842..5ea0f48a 100644 --- a/internal/platform/implementation/shared/BUILD +++ b/internal/platform/implementation/shared/BUILD @@ -62,6 +62,8 @@ cc_library( hdrs = ["file.h"], visibility = ["//internal/platform/implementation:__subpackages__"], deps = [ + "//internal/base:file_path", + "//internal/base:files", "//internal/platform:base", "//internal/platform/implementation:types", "@com_google_absl//absl/memory", diff --git a/internal/platform/implementation/shared/file.cc b/internal/platform/implementation/shared/file.cc index 40ec0294..5f1ea845 100644 --- a/internal/platform/implementation/shared/file.cc +++ b/internal/platform/implementation/shared/file.cc @@ -23,6 +23,8 @@ #include "absl/strings/string_view.h" #include "absl/time/clock.h" #include "absl/time/time.h" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/platform/exception.h" namespace nearby { @@ -30,24 +32,25 @@ namespace shared { // InputFile std::unique_ptr IOFile::CreateInputFile( - const absl::string_view file_path, size_t size) { - return absl::WrapUnique(new IOFile(file_path, size)); + const absl::string_view file_path) { + auto file = absl::WrapUnique(new IOFile(file_path)); + file->OpenForRead(); + return file; } -IOFile::IOFile(const absl::string_view file_path, size_t size) - : file_(std::string(file_path.data(), file_path.size()), - std::ios::binary | std::ios::in | std::ios::ate), - path_(file_path), - total_size_(size) { +void IOFile::OpenForRead() { + file_ = std::fstream(path_, std::ios::binary | std::ios::in | std::ios::ate); + total_size_ = Files::GetFileSize(FilePath(path_)).value_or(0); file_.seekg(0); } std::unique_ptr IOFile::CreateOutputFile(const absl::string_view path) { - return std::unique_ptr(new IOFile(path)); + auto file = absl::WrapUnique(new IOFile(path)); + file->OpenForWrite(); + return file; } -IOFile::IOFile(const absl::string_view file_path) - : file_(), path_(file_path), total_size_(0) { +void IOFile::OpenForWrite() { file_.open(path_, std::ios::binary | std::ios::out); } diff --git a/internal/platform/implementation/shared/file.h b/internal/platform/implementation/shared/file.h index 0923b462..fb3db59d 100644 --- a/internal/platform/implementation/shared/file.h +++ b/internal/platform/implementation/shared/file.h @@ -32,10 +32,9 @@ namespace shared { class IOFile final : public api::InputFile, public api::OutputFile { public: - static std::unique_ptr CreateInputFile( - const absl::string_view file_path, size_t size); + static std::unique_ptr CreateInputFile(absl::string_view file_path); - static std::unique_ptr CreateOutputFile(const absl::string_view path); + static std::unique_ptr CreateOutputFile(absl::string_view path); ExceptionOr Read(std::int64_t size) override; @@ -50,12 +49,13 @@ class IOFile final : public api::InputFile, public api::OutputFile { void SetLastModifiedTime(absl::Time last_modified_time) override; private: - explicit IOFile(const absl::string_view file_path, size_t size); - explicit IOFile(const absl::string_view file_path); + explicit IOFile(absl::string_view file_path) : path_(file_path) {}; + void OpenForRead(); + void OpenForWrite(); + const std::string path_; std::fstream file_; - std::string path_; - std::int64_t total_size_; + std::int64_t total_size_ = 0; }; } // namespace shared diff --git a/internal/platform/implementation/shared/file_test.cc b/internal/platform/implementation/shared/file_test.cc index eb65d5c1..c8597ddd 100644 --- a/internal/platform/implementation/shared/file_test.cc +++ b/internal/platform/implementation/shared/file_test.cc @@ -14,10 +14,8 @@ #include "internal/platform/implementation/shared/file.h" -#include #include #include -#include #include #include "file/util/temp_path.h" @@ -40,11 +38,8 @@ class FileTest : public ::testing::Test { void WriteToFile(absl::string_view text) { file_ << text; file_.flush(); - size_ += text.size(); } - size_t GetSize() const { return size_; } - void AssertEquals(const ExceptionOr& bytes, const std::string& expected) { EXPECT_TRUE(bytes.ok()); @@ -61,44 +56,43 @@ class FileTest : public ::testing::Test { std::unique_ptr temp_path_; std::string path_; std::fstream file_; - size_t size_ = 0; }; TEST_F(FileTest, IOFile_NonExistentPathInput) { auto io_file = - shared::IOFile::CreateInputFile("/not/a/valid/path.txt", GetSize()); + shared::IOFile::CreateInputFile("/not/a/valid/path.txt"); ExceptionOr read_result = io_file->Read(kMaxSize); EXPECT_FALSE(read_result.ok()); EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo)); } TEST_F(FileTest, IOFile_GetFilePath) { - auto io_file = shared::IOFile::CreateInputFile(path_, GetSize()); + auto io_file = shared::IOFile::CreateInputFile(path_); EXPECT_EQ(io_file->GetFilePath(), path_); } TEST_F(FileTest, IOFile_EmptyFileEOF) { - auto io_file = shared::IOFile::CreateInputFile(path_, GetSize()); + auto io_file = shared::IOFile::CreateInputFile(path_); AssertEmpty(io_file->Read(kMaxSize)); } TEST_F(FileTest, IOFile_ReadWorks) { WriteToFile("abc"); - auto io_file = shared::IOFile::CreateInputFile(path_, GetSize()); + auto io_file = shared::IOFile::CreateInputFile(path_); io_file->Read(kMaxSize); SUCCEED(); } TEST_F(FileTest, IOFile_ReadUntilEOF) { WriteToFile("abc"); - auto io_file = shared::IOFile::CreateInputFile(path_, GetSize()); + auto io_file = shared::IOFile::CreateInputFile(path_); AssertEquals(io_file->Read(kMaxSize), "abc"); AssertEmpty(io_file->Read(kMaxSize)); } TEST_F(FileTest, IOFile_ReadWithSize) { WriteToFile("abc"); - auto io_file = shared::IOFile::CreateInputFile(path_, GetSize()); + auto io_file = shared::IOFile::CreateInputFile(path_); AssertEquals(io_file->Read(2), "ab"); AssertEquals(io_file->Read(1), "c"); AssertEmpty(io_file->Read(kMaxSize)); @@ -106,7 +100,7 @@ TEST_F(FileTest, IOFile_ReadWithSize) { TEST_F(FileTest, IOFile_GetTotalSize) { WriteToFile("abc"); - auto io_file = shared::IOFile::CreateInputFile(path_, GetSize()); + auto io_file = shared::IOFile::CreateInputFile(path_); EXPECT_EQ(io_file->GetTotalSize(), 3); AssertEquals(io_file->Read(1), "a"); EXPECT_EQ(io_file->GetTotalSize(), 3); @@ -114,7 +108,7 @@ TEST_F(FileTest, IOFile_GetTotalSize) { TEST_F(FileTest, IOFile_CloseInput) { WriteToFile("abc"); - auto io_file = shared::IOFile::CreateInputFile(path_, GetSize()); + auto io_file = shared::IOFile::CreateInputFile(path_); io_file->Close(); ExceptionOr read_result = io_file->Read(kMaxSize); EXPECT_FALSE(read_result.ok()); @@ -134,7 +128,7 @@ TEST_F(FileTest, IOFile_Write) { EXPECT_EQ(io_file_output->Write(bytes1), Exception{Exception::kSuccess}); EXPECT_EQ(io_file_output->Write(bytes2), Exception{Exception::kSuccess}); auto io_file_input = - shared::IOFile::CreateInputFile(io_file_output->GetFilePath(), GetSize()); + shared::IOFile::CreateInputFile(io_file_output->GetFilePath()); AssertEquals(io_file_input->Read(kMaxSize), "abc"); } diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 048f6116..ed1973c5 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -210,6 +210,7 @@ cc_library( "bluetooth_pairing.cc", "executor.cc", "file.cc", + "file.h", "file_path.cc", "http_loader.cc", "nearby_client_socket.cc", @@ -245,7 +246,6 @@ cc_library( "bluetooth_classic_server_socket.h", "bluetooth_classic_socket.h", "bluetooth_pairing.h", - "file.h", "file_path.h", "http_loader.h", "nearby_client_socket.h", diff --git a/internal/platform/implementation/windows/file.cc b/internal/platform/implementation/windows/file.cc index 4cca12de..7af1019c 100644 --- a/internal/platform/implementation/windows/file.cc +++ b/internal/platform/implementation/windows/file.cc @@ -87,12 +87,13 @@ void TimeToFiletime(absl::Time time, FILETIME* file_time) { } // namespace // InputFile -std::unique_ptr IOFile::CreateInputFile(absl::string_view file_path, - size_t size) { - return absl::WrapUnique(new IOFile(file_path, size)); +std::unique_ptr IOFile::CreateInputFile(absl::string_view file_path) { + auto file = absl::WrapUnique(new IOFile(file_path)); + file->OpenForRead(); + return file; } -IOFile::IOFile(absl::string_view file_path, size_t size) : path_(file_path) { +void IOFile::OpenForRead() { // Always open input file path as wide string on Windows platform. std::wstring wide_path = string_utils::StringToWideString(path_); file_ = ::CreateFileW(wide_path.data(), GENERIC_READ, FILE_SHARE_READ, @@ -100,14 +101,14 @@ IOFile::IOFile(absl::string_view file_path, size_t size) : path_(file_path) { FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, /*hTemplateFile=*/nullptr); if (file_ == INVALID_HANDLE_VALUE) { - LOG(ERROR) << "Failed to open input file: " << file_path + LOG(ERROR) << "Failed to open input file: " << path_ << " with error: " << ::GetLastError(); return; } LARGE_INTEGER file_size; if (::GetFileSizeEx(file_, &file_size) == 0) { - LOG(ERROR) << "Failed to get file size: " << file_path + LOG(ERROR) << "Failed to get file size: " << path_ << " with error: " << ::GetLastError(); return; } @@ -115,18 +116,20 @@ IOFile::IOFile(absl::string_view file_path, size_t size) : path_(file_path) { } std::unique_ptr IOFile::CreateOutputFile(absl::string_view path) { - return std::unique_ptr(new IOFile(path)); + auto file = absl::WrapUnique(new IOFile(path)); + file->OpenForWrite(); + return file; } -IOFile::IOFile(absl::string_view file_path) : path_(file_path), total_size_(0) { - // Always open input file path as wide string on Windows platform. +void IOFile::OpenForWrite() { + // Always open output file path as wide string on Windows platform. std::wstring wide_path = string_utils::StringToWideString(path_); 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 + LOG(ERROR) << "Failed to open output file: " << path_ << " with error: " << ::GetLastError(); return; } @@ -207,7 +210,7 @@ void IOFile::SetLastModifiedTime(absl::Time last_modified_time) { TimeToFiletime(last_modified_time, &last_write_time); // Set both creation time and last write time. if (::SetFileTime(file_, /*lpCreationTime=*/&last_write_time, - /*lpLastAccessTime=*/nullptr, &last_write_time) == 0) { + /*lpLastAccessTime=*/nullptr, &last_write_time) == 0) { LOG(ERROR) << "Failed to set file last write time: " << path_ << " with error: " << ::GetLastError(); } diff --git a/internal/platform/implementation/windows/file.h b/internal/platform/implementation/windows/file.h index 7ee1280e..56b157e4 100644 --- a/internal/platform/implementation/windows/file.h +++ b/internal/platform/implementation/windows/file.h @@ -33,8 +33,7 @@ namespace nearby::windows { class IOFile final : public api::InputFile, public api::OutputFile { public: - static std::unique_ptr CreateInputFile(absl::string_view file_path, - size_t size); + static std::unique_ptr CreateInputFile(absl::string_view file_path); static std::unique_ptr CreateOutputFile(absl::string_view path); @@ -54,11 +53,12 @@ class IOFile final : public api::InputFile, public api::OutputFile { void SetLastModifiedTime(absl::Time last_modified_time) override; private: - explicit IOFile(absl::string_view file_path, size_t size); - explicit IOFile(absl::string_view file_path); + explicit IOFile(absl::string_view file_path) : path_(file_path) {} + void OpenForRead(); + void OpenForWrite(); + const std::string path_; HANDLE file_ = INVALID_HANDLE_VALUE; - std::string path_; std::string buffer_; std::int64_t total_size_ = 0; }; diff --git a/internal/platform/implementation/windows/file_test.cc b/internal/platform/implementation/windows/file_test.cc index 72826a88..e281e57b 100644 --- a/internal/platform/implementation/windows/file_test.cc +++ b/internal/platform/implementation/windows/file_test.cc @@ -91,7 +91,7 @@ std::string CreateTempFile(absl::string_view prefix, size_t size) { TEST(IOFileTest, InputFileNonexistentPathHasZeroSize) { std::unique_ptr input_file = - IOFile::CreateInputFile(/*file_path=*/"", /*size=*/0); + IOFile::CreateInputFile(/*file_path=*/""); ASSERT_NE(input_file, nullptr); EXPECT_EQ(input_file->GetTotalSize(), 0); @@ -99,7 +99,7 @@ TEST(IOFileTest, InputFileNonexistentPathHasZeroSize) { TEST(IOFileTest, InputFileNonexistentPathFailsToRead) { std::unique_ptr input_file = - IOFile::CreateInputFile(/*file_path=*/"", /*size=*/0); + IOFile::CreateInputFile(/*file_path=*/""); ASSERT_NE(input_file, nullptr); ExceptionOr read_result = input_file->Read(/*size=*/1); @@ -110,7 +110,7 @@ TEST(IOFileTest, InputFileNonexistentPathFailsToRead) { TEST(IOFileTest, InputFileNonexistentPathCloseSucceeds) { std::unique_ptr input_file = - IOFile::CreateInputFile(/*file_path=*/"", /*size=*/0); + IOFile::CreateInputFile(/*file_path=*/""); ASSERT_NE(input_file, nullptr); ExceptionOr close_result = input_file->Close(); @@ -124,7 +124,7 @@ TEST(IOFileTest, InputFileLargeFileSize) { ASSERT_FALSE(temp_file.empty()); std::unique_ptr input_file = - IOFile::CreateInputFile(temp_file, /*size=*/0); + IOFile::CreateInputFile(temp_file); ASSERT_NE(input_file, nullptr); EXPECT_EQ(input_file->GetTotalSize(), kLargeFileSize); @@ -137,7 +137,7 @@ TEST(IOFileTest, InputFileReadToEnd) { ASSERT_FALSE(temp_file.empty()); std::unique_ptr input_file = - IOFile::CreateInputFile(temp_file, /*size=*/0); + IOFile::CreateInputFile(temp_file); ASSERT_NE(input_file, nullptr); EXPECT_EQ(input_file->GetTotalSize(), kFileSize); @@ -180,7 +180,7 @@ TEST(IOFileTest, OutputFileWrite) { EXPECT_TRUE(output_file->Close().Ok()); std::unique_ptr input_file = - IOFile::CreateInputFile(temp_file, /*size=*/0); + IOFile::CreateInputFile(temp_file); ExceptionOr read_result = input_file->Read(10); EXPECT_TRUE(read_result.ok()); EXPECT_EQ(read_result.result(), ByteArray("test1test2")); @@ -197,7 +197,7 @@ TEST(IOFileTest, GetSetModifiedTime) { output_file->Close(); std::unique_ptr input_file = - IOFile::CreateInputFile(temp_file, /*size=*/0); + IOFile::CreateInputFile(temp_file); EXPECT_EQ(input_file->GetLastModifiedTime(), absl::FromUnixSeconds(1234567890)); diff --git a/internal/platform/implementation/windows/platform.cc b/internal/platform/implementation/windows/platform.cc index 04a67ac6..2270831e 100644 --- a/internal/platform/implementation/windows/platform.cc +++ b/internal/platform/implementation/windows/platform.cc @@ -194,13 +194,12 @@ ABSL_DEPRECATED("This interface will be deleted in the near future.") std::unique_ptr ImplementationPlatform::CreateInputFile( PayloadId payload_id, std::int64_t total_size) { std::string file_name(std::to_string(payload_id)); - return windows::IOFile::CreateInputFile(GetDownloadPath(file_name), - total_size); + return windows::IOFile::CreateInputFile(GetDownloadPath(file_name)); } std::unique_ptr ImplementationPlatform::CreateInputFile( const std::string& file_path, size_t size) { - return windows::IOFile::CreateInputFile(file_path, size); + return windows::IOFile::CreateInputFile(file_path); } ABSL_DEPRECATED("This interface will be deleted in the near future.")