mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Update platform to remove file size from CreateInputFile API.
PiperOrigin-RevId: 846311268
This commit is contained in:
committed by
Copybara-Service
parent
b88fbc6636
commit
a590b2a3e2
+1
-11
@@ -156,15 +156,6 @@ typedef struct NcContext {
|
||||
|
||||
absl::NoDestructor<absl::flat_hash_map<NC_INSTANCE, NcContext>> kNcContextMap;
|
||||
|
||||
int64_t getFileSize(const char* filename) {
|
||||
struct stat file_status;
|
||||
if (stat(filename, &file_status) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return file_status.st_size;
|
||||
}
|
||||
|
||||
int convertStringToInt(absl::string_view data) {
|
||||
if (data.size() != 4) {
|
||||
return 0;
|
||||
@@ -729,8 +720,7 @@ void NcSendPayload(NC_INSTANCE instance, size_t endpoint_ids_size,
|
||||
payload->content.file.file_name);
|
||||
}
|
||||
|
||||
nearby::InputFile input_file(full_file_name,
|
||||
getFileSize(full_file_name.c_str()));
|
||||
nearby::InputFile input_file(full_file_name);
|
||||
cpp_payload =
|
||||
::nearby::connections::Payload(payload->id, std::move(input_file));
|
||||
} else if (payload->type == NC_PAYLOAD_TYPE_STREAM) {
|
||||
|
||||
@@ -431,7 +431,7 @@ ErrorOr<std::unique_ptr<InternalPayload>> CreateIncomingInternalPayload(
|
||||
return {Error(OperationResultCode::IO_FILE_OPENING_ERROR)};
|
||||
}
|
||||
return {std::make_unique<IncomingFileInternalPayload>(
|
||||
Payload(payload_id, InputFile(payload_id, total_size)),
|
||||
Payload(payload_id, InputFile(payload_id)),
|
||||
std::move(output_file), last_modified_time, total_size)};
|
||||
} else {
|
||||
OutputFile output_file(file_path);
|
||||
@@ -441,7 +441,7 @@ ErrorOr<std::unique_ptr<InternalPayload>> CreateIncomingInternalPayload(
|
||||
}
|
||||
return {std::make_unique<IncomingFileInternalPayload>(
|
||||
Payload(payload_id, parent_folder, file_name,
|
||||
InputFile(file_path, total_size)),
|
||||
InputFile(file_path)),
|
||||
std::move(output_file), last_modified_time, total_size)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ TEST(InternalPayloadFactoryTest, CanCreateInternalPayloadFromStreamPayload) {
|
||||
|
||||
TEST(InternalPayloadFactoryTest, CanCreateInternalPayloadFromFilePayload) {
|
||||
Payload::Id payload_id = Payload::GenerateId();
|
||||
InputFile inputFile(payload_id, 512);
|
||||
InputFile inputFile(payload_id);
|
||||
ErrorOr<std::unique_ptr<InternalPayload>> result =
|
||||
CreateOutgoingInternalPayload(Payload{payload_id, std::move(inputFile)});
|
||||
ASSERT_FALSE(result.has_error());
|
||||
@@ -231,7 +231,7 @@ TEST(InternalPayloadFactoryTest,
|
||||
size_t size_after_skip = contents.size() - kOffset;
|
||||
Payload::Id payload_id = Payload::GenerateId();
|
||||
CreateFileWithContents(payload_id, contents);
|
||||
InputFile inputFile(payload_id, contents.size());
|
||||
InputFile inputFile(payload_id);
|
||||
ErrorOr<std::unique_ptr<InternalPayload>> internal_payload_result =
|
||||
CreateOutgoingInternalPayload(Payload{payload_id, std::move(inputFile)});
|
||||
ASSERT_FALSE(internal_payload_result.has_error());
|
||||
|
||||
@@ -58,7 +58,7 @@ TEST(PayloadTest, SupportsFileType) {
|
||||
outputFile.Write(test_data);
|
||||
outputFile.Close();
|
||||
|
||||
InputFile file(payload_id, 100);
|
||||
InputFile file(payload_id);
|
||||
InputStream& stream = file.GetInputStream();
|
||||
|
||||
Payload payload(payload_id, std::move(file));
|
||||
@@ -74,7 +74,7 @@ TEST(PayloadTest, SupportsFileType) {
|
||||
|
||||
TEST(PayloadTest, SupportsMultiDotNamedFileType) {
|
||||
constexpr char expected[] = "this.is.a.multidot.file";
|
||||
InputFile file(expected, 0);
|
||||
InputFile file(expected);
|
||||
|
||||
Payload payload(/*parent_folder=*/"", expected, std::move(file));
|
||||
|
||||
@@ -87,7 +87,7 @@ TEST(PayloadTest,
|
||||
"test_folder.here\\this.is.a.multidot.backslash.folder.separated.file";
|
||||
constexpr char expected[] =
|
||||
"this.is.a.multidot.backslash.folder.separated.file";
|
||||
InputFile file(file_name, 0);
|
||||
InputFile file(file_name);
|
||||
|
||||
Payload payload(/*parent_folder=*/"", file_name, std::move(file));
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ using ::nearby::connections::Payload;
|
||||
std::string path = [self.fileURL.path cStringUsingEncoding:[NSString defaultCStringEncoding]];
|
||||
std::string folder = [self.parentFolder cStringUsingEncoding:[NSString defaultCStringEncoding]];
|
||||
std::string name = [self.fileName cStringUsingEncoding:[NSString defaultCStringEncoding]];
|
||||
return Payload(self.identifier, folder, name, InputFile(path, self.totalSize.longLongValue));
|
||||
return Payload(self.identifier, folder, name, InputFile(path));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -17,13 +17,13 @@
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/platform/payload_id.h"
|
||||
|
||||
namespace nearby {
|
||||
|
||||
InputFile::InputFile(PayloadId id, std::int64_t size)
|
||||
: impl_(Platform::CreateInputFile(id, size)) {}
|
||||
InputFile::InputFile(std::string file_path, std::int64_t size)
|
||||
: impl_(Platform::CreateInputFile(file_path, size)) {}
|
||||
InputFile::InputFile(PayloadId id) : impl_(Platform::CreateInputFile(id)) {}
|
||||
InputFile::InputFile(std::string file_path)
|
||||
: impl_(Platform::CreateInputFile(file_path)) {}
|
||||
InputFile::~InputFile() = default;
|
||||
InputFile::InputFile(InputFile&& other) noexcept = default;
|
||||
InputFile& InputFile::operator=(InputFile&& other) = default;
|
||||
|
||||
@@ -35,8 +35,8 @@ namespace nearby {
|
||||
class InputFile final {
|
||||
public:
|
||||
using Platform = api::ImplementationPlatform;
|
||||
InputFile(PayloadId payload_id, std::int64_t size);
|
||||
InputFile(std::string file_path, std::int64_t size);
|
||||
explicit InputFile(PayloadId payload_id);
|
||||
explicit InputFile(std::string file_path);
|
||||
~InputFile();
|
||||
InputFile(InputFile&&) noexcept;
|
||||
InputFile& operator=(InputFile&&);
|
||||
|
||||
@@ -50,7 +50,7 @@ TEST_F(FileTest, ConstructorDestructorWorks) {
|
||||
output_file.Close();
|
||||
|
||||
// Create an input file and read from it.
|
||||
InputFile input_file(file_path.ToString(), data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_bytes = input_file.Read(data.size());
|
||||
ASSERT_TRUE(read_bytes.ok());
|
||||
EXPECT_EQ(read_bytes.result(), ByteArray(data));
|
||||
@@ -68,7 +68,7 @@ TEST_F(FileTest, SimpleWriteRead) {
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
// Read from file.
|
||||
InputFile input_file(file_path.ToString(), data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_data = input_file.Read(data.size());
|
||||
EXPECT_TRUE(read_data.ok());
|
||||
EXPECT_EQ(std::string(read_data.result()), data);
|
||||
@@ -86,7 +86,7 @@ TEST_F(FileTest, WriteThenCloseThenRead) {
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
// Re-open and read.
|
||||
InputFile input_file(file_path.ToString(), data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_data = input_file.Read(data.size());
|
||||
EXPECT_TRUE(read_data.ok());
|
||||
EXPECT_EQ(std::string(read_data.result()), data);
|
||||
@@ -102,7 +102,7 @@ TEST_F(FileTest, ReadEmptyFile) {
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
// Read from empty file.
|
||||
InputFile input_file(file_path.ToString(), 0);
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_data = input_file.Read(1024);
|
||||
EXPECT_TRUE(read_data.ok());
|
||||
EXPECT_TRUE(read_data.result().Empty());
|
||||
@@ -118,7 +118,7 @@ TEST_F(FileTest, ReadExactly) {
|
||||
EXPECT_TRUE(output_file.Write(ByteArray(data)).Ok());
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
InputFile input_file(file_path.ToString(), data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_data =
|
||||
input_file.GetInputStream().ReadExactly(data.size());
|
||||
EXPECT_TRUE(read_data.ok());
|
||||
@@ -135,7 +135,7 @@ TEST_F(FileTest, ReadTooMuch) {
|
||||
EXPECT_TRUE(output_file.Write(ByteArray(data)).Ok());
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
InputFile input_file(file_path.ToString(), data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_data = input_file.Read(data.size() * 2);
|
||||
EXPECT_TRUE(read_data.ok());
|
||||
EXPECT_EQ(std::string(read_data.result()), data);
|
||||
@@ -153,7 +153,7 @@ TEST_F(FileTest, Skip) {
|
||||
EXPECT_TRUE(output_file.Write(ByteArray(full_data)).Ok());
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
InputFile input_file(file_path.ToString(), full_data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<size_t> skipped_bytes = input_file.Skip(data_to_skip.size());
|
||||
EXPECT_TRUE(skipped_bytes.ok());
|
||||
EXPECT_EQ(skipped_bytes.result(), data_to_skip.size());
|
||||
@@ -176,7 +176,7 @@ TEST_F(FileTest, MultipleWrites) {
|
||||
EXPECT_TRUE(output_file.Write(ByteArray(data2)).Ok());
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
InputFile input_file(file_path.ToString(), full_data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_data = input_file.Read(full_data.size());
|
||||
EXPECT_TRUE(read_data.ok());
|
||||
EXPECT_EQ(std::string(read_data.result()), full_data);
|
||||
@@ -190,7 +190,7 @@ TEST_F(FileTest, CloseTwice) {
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
InputFile input_file(file_path.ToString(), 0);
|
||||
InputFile input_file(file_path.ToString());
|
||||
EXPECT_TRUE(input_file.Close().Ok());
|
||||
EXPECT_TRUE(input_file.Close().Ok());
|
||||
}
|
||||
@@ -208,7 +208,7 @@ TEST_F(FileTest, WriteLargeFile) {
|
||||
EXPECT_TRUE(output_file.Write(ByteArray(large_data)).Ok());
|
||||
EXPECT_TRUE(output_file.Close().Ok());
|
||||
|
||||
InputFile input_file(file_path.ToString(), large_data.size());
|
||||
InputFile input_file(file_path.ToString());
|
||||
ExceptionOr<ByteArray> read_data =
|
||||
input_file.GetInputStream().ReadExactly(large_data.size());
|
||||
EXPECT_TRUE(read_data.ok());
|
||||
|
||||
@@ -257,7 +257,7 @@ void GNCEnsureFileAtPath(std::string path) {
|
||||
[data writeToFile:path atomically:YES];
|
||||
|
||||
auto input_file =
|
||||
nearby::api::ImplementationPlatform::CreateInputFile(path.UTF8String, data.length);
|
||||
nearby::api::ImplementationPlatform::CreateInputFile(path.UTF8String);
|
||||
|
||||
XCTAssertNotEqual(input_file.get(), nullptr);
|
||||
XCTAssertEqual(input_file->GetTotalSize(), data.length);
|
||||
@@ -325,7 +325,7 @@ void GNCEnsureFileAtPath(std::string path) {
|
||||
}
|
||||
|
||||
- (void)testCreateInputFileWithPayloadID {
|
||||
auto input_file = nearby::api::ImplementationPlatform::CreateInputFile(1234, 0);
|
||||
auto input_file = nearby::api::ImplementationPlatform::CreateInputFile(1234);
|
||||
XCTAssertEqual(input_file.get(), nullptr);
|
||||
}
|
||||
|
||||
|
||||
@@ -126,13 +126,11 @@ std::unique_ptr<ConditionVariable> ImplementationPlatform::CreateConditionVariab
|
||||
}
|
||||
|
||||
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::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(PayloadId payload_id) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(const std::string& file_path,
|
||||
size_t size) {
|
||||
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(const std::string& file_path) {
|
||||
return shared::IOFile::CreateInputFile(file_path);
|
||||
}
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ std::unique_ptr<AtomicBoolean> ImplementationPlatform::CreateAtomicBoolean(
|
||||
|
||||
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) {
|
||||
PayloadId payload_id) {
|
||||
std::string parent_folder("");
|
||||
std::string file_name(std::to_string(payload_id));
|
||||
return shared::IOFile::CreateInputFile(
|
||||
@@ -147,7 +147,7 @@ std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
|
||||
}
|
||||
|
||||
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
|
||||
const std::string& file_path, size_t size) {
|
||||
const std::string& file_path) {
|
||||
return shared::IOFile::CreateInputFile(file_path);
|
||||
}
|
||||
|
||||
|
||||
@@ -106,9 +106,9 @@ class ImplementationPlatform {
|
||||
static std::unique_ptr<ConditionVariable> CreateConditionVariable(
|
||||
Mutex* mutex);
|
||||
|
||||
static std::unique_ptr<InputFile> CreateInputFile(PayloadId, std::int64_t);
|
||||
static std::unique_ptr<InputFile> CreateInputFile(PayloadId);
|
||||
|
||||
static std::unique_ptr<InputFile> CreateInputFile(const std::string&, size_t);
|
||||
static std::unique_ptr<InputFile> CreateInputFile(const std::string&);
|
||||
|
||||
static std::unique_ptr<OutputFile> CreateOutputFile(PayloadId);
|
||||
|
||||
|
||||
@@ -314,7 +314,7 @@ void BluetoothAdapter::RestoreRadioNameIfNecessary() {
|
||||
nearby::api::ImplementationPlatform::GetAppDataPath(settings_path);
|
||||
|
||||
auto settings_file =
|
||||
nearby::api::ImplementationPlatform::CreateInputFile(full_path, 0);
|
||||
nearby::api::ImplementationPlatform::CreateInputFile(full_path);
|
||||
if (settings_file == nullptr) {
|
||||
LOG(ERROR) << __func__ << ": Failed to create input file.";
|
||||
return;
|
||||
|
||||
@@ -192,13 +192,13 @@ ImplementationPlatform::CreateConditionVariable(Mutex* mutex) {
|
||||
|
||||
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) {
|
||||
PayloadId payload_id) {
|
||||
std::string file_name(std::to_string(payload_id));
|
||||
return windows::IOFile::CreateInputFile(GetDownloadPath(file_name));
|
||||
}
|
||||
|
||||
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
|
||||
const std::string& file_path, size_t size) {
|
||||
const std::string& file_path) {
|
||||
return windows::IOFile::CreateInputFile(file_path);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,6 @@ Payload ConvertToPayload(NcPayload payload) {
|
||||
NcPayload ConvertToServicePayload(Payload payload) {
|
||||
switch (payload.content.type) {
|
||||
case PayloadContent::Type::kFile: {
|
||||
int64_t file_size = payload.content.file_payload.size;
|
||||
std::string file_path = payload.content.file_payload.file_path.ToString();
|
||||
std::string file_name =
|
||||
payload.content.file_payload.file_path.GetFileName().ToString();
|
||||
@@ -64,7 +63,7 @@ NcPayload ConvertToServicePayload(Payload payload) {
|
||||
std::replace(parent_folder.begin(), parent_folder.end(), '\\', '/');
|
||||
VLOG(1) << __func__ << ": NC Payload file_path=" << file_path
|
||||
<< ", parent_folder = " << parent_folder;
|
||||
nearby::InputFile input_file(file_path, file_size);
|
||||
nearby::InputFile input_file(file_path);
|
||||
NcPayload nc_payload(payload.id, parent_folder, file_name,
|
||||
std::move(input_file));
|
||||
return nc_payload;
|
||||
|
||||
@@ -33,7 +33,7 @@ TEST(NearbyConnectionSharingServicePayloadTest, ConvertBytesToPayload) {
|
||||
|
||||
TEST(NearbyConnectionSharingServicePayloadTest, ConvertFileToPayload) {
|
||||
Payload payload = ConvertToPayload(
|
||||
NcPayload(1234, nearby::InputFile("/为甚么/tmp/test.txt", /*size=*/100)));
|
||||
NcPayload(1234, nearby::InputFile("/为甚么/tmp/test.txt")));
|
||||
EXPECT_THAT(payload.id, Eq(1234LL));
|
||||
EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user