Merge remote-tracking branch 'nearby/main' into apply-upstream

# Conflicts:
#	MODULE.bazel
#	connections/implementation/mediums/ble_v2.cc
#	internal/platform/implementation/BUILD
This commit is contained in:
kidfromjupiter
2026-02-04 19:12:40 +00:00
1278 changed files with 930060 additions and 211365 deletions
@@ -63,6 +63,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",
+14 -11
View File
@@ -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);
}
@@ -82,7 +85,7 @@ Exception IOFile::Close() {
return {Exception::kSuccess};
}
Exception IOFile::Write(const ByteArray& data) {
Exception IOFile::Write(absl::string_view data) {
if (!file_.is_open()) {
return {Exception::kIo};
}
@@ -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;
@@ -44,18 +43,19 @@ class IOFile final : public api::InputFile, public api::OutputFile {
std::int64_t GetTotalSize() const override { return total_size_; }
Exception Close() override;
Exception Write(const ByteArray& data) override;
Exception Write(absl::string_view data) override;
absl::Time GetLastModifiedTime() const override;
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,16 +14,16 @@
#include "internal/platform/implementation/shared/file.h"
#include <cstring>
#include <cstdint>
#include <fstream>
#include <memory>
#include <ostream>
#include <string>
#include "file/util/temp_path.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
namespace nearby {
namespace shared {
@@ -40,11 +40,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 +58,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 +102,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 +110,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());
@@ -123,25 +119,25 @@ TEST_F(FileTest, IOFile_CloseInput) {
TEST_F(FileTest, IOFile_NonExistentPathOutput) {
auto io_file = shared::IOFile::CreateOutputFile("/not/a/valid/path.txt");
ByteArray bytes("a", 1);
absl::string_view bytes("a", 1);
EXPECT_TRUE(io_file->Write(bytes).Raised(Exception::kIo));
}
TEST_F(FileTest, IOFile_Write) {
auto io_file_output = shared::IOFile::CreateOutputFile(path_);
ByteArray bytes1("a");
ByteArray bytes2("bc");
absl::string_view bytes1("a");
absl::string_view bytes2("bc");
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");
}
TEST_F(FileTest, IOFile_CloseOutput) {
auto io_file = shared::IOFile::CreateOutputFile(path_);
io_file->Close();
ByteArray bytes("a");
absl::string_view bytes("a");
EXPECT_EQ(io_file->Write(bytes), Exception{Exception::kIo});
}
@@ -29,6 +29,7 @@ class ABSL_LOCKABLE Mutex : public api::Mutex {
void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() override;
void Unlock() ABSL_UNLOCK_FUNCTION() override;
void AssertHeld() const ABSL_ASSERT_EXCLUSIVE_LOCK() override {}
private:
friend class ConditionVariable;