mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Stop passing file size into InputFile c'tor.
PiperOrigin-RevId: 845989622
This commit is contained in:
committed by
Copybara-Service
parent
0fe6eb566f
commit
b88fbc6636
@@ -133,7 +133,7 @@ std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(PayloadId pay
|
||||
|
||||
std::unique_ptr<InputFile> 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.")
|
||||
|
||||
@@ -143,12 +143,12 @@ std::unique_ptr<InputFile> 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<InputFile> 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.")
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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> 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> IOFile::CreateOutputFile(const absl::string_view path) {
|
||||
return std::unique_ptr<IOFile>(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);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,10 +32,9 @@ namespace shared {
|
||||
|
||||
class IOFile final : public api::InputFile, public api::OutputFile {
|
||||
public:
|
||||
static std::unique_ptr<IOFile> CreateInputFile(
|
||||
const absl::string_view file_path, size_t size);
|
||||
static std::unique_ptr<IOFile> CreateInputFile(absl::string_view file_path);
|
||||
|
||||
static std::unique_ptr<IOFile> CreateOutputFile(const absl::string_view path);
|
||||
static std::unique_ptr<IOFile> CreateOutputFile(absl::string_view path);
|
||||
|
||||
ExceptionOr<ByteArray> 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
|
||||
|
||||
@@ -14,10 +14,8 @@
|
||||
|
||||
#include "internal/platform/implementation/shared/file.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#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<ByteArray>& bytes,
|
||||
const std::string& expected) {
|
||||
EXPECT_TRUE(bytes.ok());
|
||||
@@ -61,44 +56,43 @@ class FileTest : public ::testing::Test {
|
||||
std::unique_ptr<TempPath> 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<ByteArray> 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<ByteArray> 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");
|
||||
}
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -87,12 +87,13 @@ void TimeToFiletime(absl::Time time, FILETIME* file_time) {
|
||||
} // namespace
|
||||
|
||||
// InputFile
|
||||
std::unique_ptr<IOFile> IOFile::CreateInputFile(absl::string_view file_path,
|
||||
size_t size) {
|
||||
return absl::WrapUnique(new IOFile(file_path, size));
|
||||
std::unique_ptr<IOFile> 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> IOFile::CreateOutputFile(absl::string_view path) {
|
||||
return std::unique_ptr<IOFile>(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();
|
||||
}
|
||||
|
||||
@@ -33,8 +33,7 @@ namespace nearby::windows {
|
||||
|
||||
class IOFile final : public api::InputFile, public api::OutputFile {
|
||||
public:
|
||||
static std::unique_ptr<IOFile> CreateInputFile(absl::string_view file_path,
|
||||
size_t size);
|
||||
static std::unique_ptr<IOFile> CreateInputFile(absl::string_view file_path);
|
||||
|
||||
static std::unique_ptr<IOFile> 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;
|
||||
};
|
||||
|
||||
@@ -91,7 +91,7 @@ std::string CreateTempFile(absl::string_view prefix, size_t size) {
|
||||
|
||||
TEST(IOFileTest, InputFileNonexistentPathHasZeroSize) {
|
||||
std::unique_ptr<IOFile> 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<IOFile> input_file =
|
||||
IOFile::CreateInputFile(/*file_path=*/"", /*size=*/0);
|
||||
IOFile::CreateInputFile(/*file_path=*/"");
|
||||
ASSERT_NE(input_file, nullptr);
|
||||
|
||||
ExceptionOr<ByteArray> read_result = input_file->Read(/*size=*/1);
|
||||
@@ -110,7 +110,7 @@ TEST(IOFileTest, InputFileNonexistentPathFailsToRead) {
|
||||
|
||||
TEST(IOFileTest, InputFileNonexistentPathCloseSucceeds) {
|
||||
std::unique_ptr<IOFile> input_file =
|
||||
IOFile::CreateInputFile(/*file_path=*/"", /*size=*/0);
|
||||
IOFile::CreateInputFile(/*file_path=*/"");
|
||||
ASSERT_NE(input_file, nullptr);
|
||||
|
||||
ExceptionOr<ByteArray> close_result = input_file->Close();
|
||||
@@ -124,7 +124,7 @@ TEST(IOFileTest, InputFileLargeFileSize) {
|
||||
ASSERT_FALSE(temp_file.empty());
|
||||
|
||||
std::unique_ptr<IOFile> 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<IOFile> 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<IOFile> input_file =
|
||||
IOFile::CreateInputFile(temp_file, /*size=*/0);
|
||||
IOFile::CreateInputFile(temp_file);
|
||||
ExceptionOr<ByteArray> 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<IOFile> input_file =
|
||||
IOFile::CreateInputFile(temp_file, /*size=*/0);
|
||||
IOFile::CreateInputFile(temp_file);
|
||||
EXPECT_EQ(input_file->GetLastModifiedTime(),
|
||||
absl::FromUnixSeconds(1234567890));
|
||||
|
||||
|
||||
@@ -194,13 +194,12 @@ ABSL_DEPRECATED("This interface will be deleted in the near future.")
|
||||
std::unique_ptr<InputFile> 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<InputFile> 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.")
|
||||
|
||||
Reference in New Issue
Block a user