From 90b7e76a24d6915d3bbaff490f9635fe13972c2b Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Wed, 21 May 2025 18:06:04 -0700 Subject: [PATCH] Remove std::filesystem from nearby/sharing. PiperOrigin-RevId: 761743090 --- internal/base/file_path.cc | 12 +++ internal/base/file_path.h | 16 +++ internal/base/file_path_test.cc | 21 ++++ internal/base/files.cc | 11 ++ internal/base/files.h | 6 ++ internal/platform/device_info_impl.cc | 9 +- sharing/BUILD | 14 ++- sharing/attachment_container_test.cc | 5 +- sharing/fake_nearby_connections_manager.cc | 11 +- sharing/fake_nearby_connections_manager.h | 13 ++- sharing/file_attachment.cc | 15 ++- sharing/file_attachment.h | 10 +- sharing/incoming_share_session.cc | 9 +- sharing/incoming_share_session_test.cc | 10 +- sharing/nearby_connections_manager.h | 4 +- sharing/nearby_connections_manager_impl.cc | 16 ++- sharing/nearby_connections_manager_impl.h | 23 ++-- .../nearby_connections_manager_impl_test.cc | 100 ++++++++---------- sharing/nearby_connections_service.cc | 9 +- sharing/nearby_connections_types.h | 16 ++- sharing/nearby_file_handler.cc | 12 +-- sharing/nearby_file_handler.h | 6 +- sharing/nearby_file_handler_test.cc | 15 ++- sharing/nearby_sharing_service_impl.cc | 8 +- sharing/nearby_sharing_service_impl_test.cc | 42 ++++---- sharing/nearby_sharing_settings.cc | 2 - sharing/nearby_sharing_util.cc | 5 +- sharing/nearby_sharing_util.h | 6 +- sharing/outgoing_share_session.cc | 6 +- sharing/outgoing_share_session.h | 4 +- sharing/outgoing_share_session_test.cc | 10 +- 31 files changed, 236 insertions(+), 210 deletions(-) diff --git a/internal/base/file_path.cc b/internal/base/file_path.cc index 856db0b5..a3434c24 100644 --- a/internal/base/file_path.cc +++ b/internal/base/file_path.cc @@ -36,11 +36,23 @@ std::wstring FilePath::ToWideString() const { return path_.wstring(); } +bool FilePath::IsEmpty() const { + return path_.empty(); +} + FilePath& FilePath::append(const FilePath& subpath) { path_ /= subpath.path_; return *this; } +FilePath FilePath::GetFileName() const { + return FromPath(path_.filename()); +} + +FilePath FilePath::GetExtension() const { + return FromPath(path_.extension()); +} + FilePath FilePath::GetParentPath() const { return FilePath(path_.parent_path().wstring()); } diff --git a/internal/base/file_path.h b/internal/base/file_path.h index f156a39f..7ee4040b 100644 --- a/internal/base/file_path.h +++ b/internal/base/file_path.h @@ -48,9 +48,19 @@ class FilePath { // Returns the path as a unicode string. std::wstring ToWideString() const; + // Returns true if the path is empty. + bool IsEmpty() const; + // Appends the given `subpath` to this path using a path separator.. FilePath& append(const FilePath& subpath); + // Returns the last component of this path. + FilePath GetFileName() const; + + // Returns the extension of the last component of this path. + // The return extension includes the "." prefix. + FilePath GetExtension() const; + // Returns the path of the parent directory of this path. FilePath GetParentPath() const; @@ -59,6 +69,12 @@ class FilePath { friend auto operator<=>(const FilePath& lhs, const FilePath& rhs) = default; + // Hash function for absl containers. + template + friend H AbslHashValue(H h, const FilePath& path) { + return H::combine(std::move(h), path.path_); + } + private: std::filesystem::path path_; }; diff --git a/internal/base/file_path_test.cc b/internal/base/file_path_test.cc index 6fd1ab6a..c81a0cd2 100644 --- a/internal/base/file_path_test.cc +++ b/internal/base/file_path_test.cc @@ -69,6 +69,27 @@ TEST(FilePathTest, FromUnicodeLinuxToWideString) { EXPECT_EQ(path.ToWideString(), L"/usr/local/home/奥巴马/Documents"); } +TEST(FilePathTest, IsEmptySuccess) { + FilePath path; + EXPECT_TRUE(path.IsEmpty()); + FilePath path2("/usr/local/home/奥巴马/Documents/test.pdf"); + EXPECT_FALSE(path2.IsEmpty()); +} + +TEST(FilePathTest, GetExtensionEmpty) { + FilePath path("/usr/local/home/test/Documents/test."); + EXPECT_EQ(path.GetExtension().ToString(), "."); +} + +TEST(FilePathTest, GetExtensionSuccess) { + FilePath path("/usr/local/home/test/Documents/test.奥巴马"); + EXPECT_EQ(path.GetExtension().ToString(), ".奥巴马"); +} +TEST(FilePathTest, GetFileNameSuccess) { + FilePath path("/usr/local/home/test/奥巴马.pdf"); + EXPECT_EQ(path.GetFileName().ToString(), "奥巴马.pdf"); +} + TEST(FilePathTest, AppendSuccess) { FilePath path("/usr/local/home/奥巴马/Documents"); FilePath sub_path("贝拉克/temp"); diff --git a/internal/base/files.cc b/internal/base/files.cc index 1ddb553e..1685597d 100644 --- a/internal/base/files.cc +++ b/internal/base/files.cc @@ -14,6 +14,7 @@ #include "internal/base/files.h" +#include #include #include // NOLINT(build/c++17) #include @@ -130,4 +131,14 @@ bool CopyFileSafely(const std::filesystem::path& old_path, return true; } +std::optional GetAvailableDiskSpaceInBytes(const FilePath& path) { + std::error_code error_code; + std::filesystem::space_info space_info = + std::filesystem::space(path.GetPath(), error_code); + if (error_code.value() == 0) { + return space_info.available; + } + return std::nullopt; +} + } // namespace nearby::sharing diff --git a/internal/base/files.h b/internal/base/files.h index 676d759f..62fe4093 100644 --- a/internal/base/files.h +++ b/internal/base/files.h @@ -15,6 +15,7 @@ #ifndef THIRD_PARTY_NEARBY_INTERNAL_BASE_FILES_H_ #define THIRD_PARTY_NEARBY_INTERNAL_BASE_FILES_H_ +#include #include #include // NOLINT(build/c++17) #include @@ -65,6 +66,11 @@ bool CreateHardLink(const std::filesystem::path& target, bool CopyFileSafely(const std::filesystem::path& old_path, const std::filesystem::path& new_path); +// Returns the available disk space in bytes for the given path. +// Returns nullopt if the path does not exist or if the space cannot be +// determined. +std::optional GetAvailableDiskSpaceInBytes(const FilePath& path); + } // namespace nearby::sharing #endif // THIRD_PARTY_NEARBY_INTERNAL_BASE_FILES_H_ diff --git a/internal/platform/device_info_impl.cc b/internal/platform/device_info_impl.cc index b0abe001..d62711a7 100644 --- a/internal/platform/device_info_impl.cc +++ b/internal/platform/device_info_impl.cc @@ -15,7 +15,6 @@ #include "internal/platform/device_info_impl.h" #include -#include // NOLINT #include #include #include @@ -78,13 +77,7 @@ FilePath DeviceInfoImpl::GetLogPath() const { std::optional DeviceInfoImpl::GetAvailableDiskSpaceInBytes( const FilePath& path) const { - std::error_code error_code; - std::filesystem::space_info space_info = - std::filesystem::space(path.GetPath(), error_code); - if (error_code.value() == 0) { - return space_info.available; - } - return std::nullopt; + return nearby::sharing::GetAvailableDiskSpaceInBytes(path); } bool DeviceInfoImpl::IsScreenLocked() const { diff --git a/sharing/BUILD b/sharing/BUILD index 5cb37901..41d00f59 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -18,10 +18,10 @@ cc_library( name = "connection_types", hdrs = ["nearby_connections_types.h"], deps = [ + "//internal/base:file_path", "//internal/base:files", "//internal/crypto_cros", # buildcleaner: keep "//internal/interop:authentication_status", - "//sharing/common:compatible_u8_string", "@com_google_absl//absl/random", "@com_google_absl//absl/strings:string_view", "@com_google_absl//absl/time", @@ -51,9 +51,9 @@ cc_library( "//sharing:__subpackages__", ], deps = [ + "//internal/base:file_path", "//internal/network:url", "//proto:sharing_enums_cc_proto", - "//sharing/common:compatible_u8_string", "//sharing/common:enum", "//sharing/internal/base", "//sharing/proto:wire_format_cc_proto", @@ -85,11 +85,11 @@ cc_library( ], deps = [ ":connection_types", + "//internal/base:file_path", "//internal/network:url", "//sharing/common:enum", "//sharing/internal/public:logging", "//sharing/proto:enums_cc_proto", - "//sharing/proto:wire_format_cc_proto", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", @@ -224,7 +224,6 @@ cc_library( "//proto:sharing_enums_cc_proto", "//sharing/analytics", "//sharing/certificates", - "//sharing/common:compatible_u8_string", "//sharing/internal/api:platform", "//sharing/internal/public:logging", "//sharing/proto:enums_cc_proto", @@ -318,6 +317,7 @@ cc_library( "//internal/base", "//internal/base:bluetooth_address", "//internal/base:file_path", + "//internal/base:files", "//internal/flags:nearby_flags", "//internal/network:url", "//internal/platform:base", @@ -328,7 +328,6 @@ cc_library( "//sharing/analytics", "//sharing/certificates", "//sharing/common", - "//sharing/common:compatible_u8_string", "//sharing/common:enum", "//sharing/contacts", "//sharing/fast_initiation:nearby_fast_initiation", @@ -380,6 +379,7 @@ cc_library( ":transfer_metadata", ":types", "//internal/base", + "//internal/base:file_path", "//internal/platform:types", "//sharing/common:enum", "//sharing/internal/api:platform", @@ -533,6 +533,8 @@ cc_test( ":connection_types", ":nearby_sharing_service", ":types", + "//internal/base:file_path", + "//internal/base:files", "//internal/flags:nearby_flags", "//internal/platform/implementation/g3", # fixdeps: keep "//internal/test", @@ -788,6 +790,7 @@ cc_test( deps = [ ":attachment_compare", ":attachments", + "//internal/base:file_path", "//proto:sharing_enums_cc_proto", "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_googletest//:gtest_main", @@ -820,6 +823,7 @@ cc_test( ":transfer_metadata_matchers", ":types", "//internal/analytics:mock_event_logger", + "//internal/base:file_path", "//internal/network:url", "//internal/platform/implementation/g3", # fixdeps: keep "//internal/test", diff --git a/sharing/attachment_container_test.cc b/sharing/attachment_container_test.cc index 12e5bbc1..f37577a1 100644 --- a/sharing/attachment_container_test.cc +++ b/sharing/attachment_container_test.cc @@ -15,13 +15,13 @@ #include "sharing/attachment_container.h" #include -#include // NOLINT #include #include #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" +#include "internal/base/file_path.h" #include "proto/sharing_enums.pb.h" #include "sharing/attachment_compare.h" // IWYU pragma: keep #include "sharing/file_attachment.h" @@ -62,8 +62,7 @@ class AttachmentContainerTest : public ::testing::Test { nearby::sharing::service::proto::WifiCredentialsMetadata::WPA_PSK, "somepassword", true, /*batch_id=*/99707L, AttachmentSourceType::ATTACHMENT_SOURCE_PASTE) { - file1_.set_file_path( - std::filesystem::u8path("/usr/local/tmp/someFileName.jpg")); + file1_.set_file_path(FilePath{"/usr/local/tmp/someFileName.jpg"}); } TextAttachment text1_; diff --git a/sharing/fake_nearby_connections_manager.cc b/sharing/fake_nearby_connections_manager.cc index c7b6a755..36075a2c 100644 --- a/sharing/fake_nearby_connections_manager.cc +++ b/sharing/fake_nearby_connections_manager.cc @@ -16,7 +16,6 @@ #include -#include // NOLINT(build/c++17) #include #include #include @@ -30,6 +29,7 @@ #include "absl/container/flat_hash_set.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" +#include "internal/base/file_path.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/internal/public/logging.h" #include "sharing/nearby_connections_manager.h" @@ -316,20 +316,19 @@ void FakeNearbyConnectionsManager::SetCustomSavePath( custom_save_path_ = custom_save_path; } -absl::flat_hash_set +absl::flat_hash_set FakeNearbyConnectionsManager::GetAndClearUnknownFilePathsToDelete() { - absl::flat_hash_set file_paths_to_delete = - file_paths_to_delete_; + absl::flat_hash_set file_paths_to_delete = file_paths_to_delete_; file_paths_to_delete_.clear(); return file_paths_to_delete; } -absl::flat_hash_set +absl::flat_hash_set FakeNearbyConnectionsManager::GetUnknownFilePathsToDeleteForTesting() { return file_paths_to_delete_; } void FakeNearbyConnectionsManager::AddUnknownFilePathsToDeleteForTesting( - std::filesystem::path file_path) { + FilePath file_path) { file_paths_to_delete_.insert(file_path); } diff --git a/sharing/fake_nearby_connections_manager.h b/sharing/fake_nearby_connections_manager.h index fe46f1a9..2cf787a2 100644 --- a/sharing/fake_nearby_connections_manager.h +++ b/sharing/fake_nearby_connections_manager.h @@ -17,7 +17,6 @@ #include -#include // NOLINT(build/c++17) #include #include #include @@ -31,7 +30,9 @@ #include "absl/container/flat_hash_set.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" +#include "internal/base/file_path.h" #include "sharing/common/nearby_share_enums.h" +#include "sharing/nearby_connection.h" #include "sharing/nearby_connections_manager.h" #include "sharing/nearby_connections_types.h" @@ -74,8 +75,7 @@ class FakeNearbyConnectionsManager : public NearbyConnectionsManager { absl::string_view endpoint_id) override; void UpgradeBandwidth(absl::string_view endpoint_id) override; void SetCustomSavePath(absl::string_view custom_save_path) override; - absl::flat_hash_set - GetAndClearUnknownFilePathsToDelete() override; + absl::flat_hash_set GetAndClearUnknownFilePathsToDelete() override; // Testing methods void SetRawAuthenticationToken(absl::string_view endpoint_id, @@ -134,9 +134,8 @@ class FakeNearbyConnectionsManager : public NearbyConnectionsManager { return !incoming_payloads_.empty(); } - absl::flat_hash_set - GetUnknownFilePathsToDeleteForTesting(); - void AddUnknownFilePathsToDeleteForTesting(std::filesystem::path file_path); + absl::flat_hash_set GetUnknownFilePathsToDeleteForTesting(); + void AddUnknownFilePathsToDeleteForTesting(FilePath file_path); // Add `connection` to list of connections as if it was accepted. void AcceptConnection(std::vector endpoint_info, @@ -183,7 +182,7 @@ class FakeNearbyConnectionsManager : public NearbyConnectionsManager { mutable absl::Mutex incoming_payloads_mutex_; std::map> incoming_payloads_ ABSL_GUARDED_BY(incoming_payloads_mutex_); - absl::flat_hash_set file_paths_to_delete_; + absl::flat_hash_set file_paths_to_delete_; std::string Dump() const override; }; diff --git a/sharing/file_attachment.cc b/sharing/file_attachment.cc index c58be6eb..015101ac 100644 --- a/sharing/file_attachment.cc +++ b/sharing/file_attachment.cc @@ -15,16 +15,15 @@ #include "sharing/file_attachment.h" #include -#include // NOLINT(build/c++17) #include #include #include #include "absl/strings/match.h" #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #include "proto/sharing_enums.pb.h" #include "sharing/attachment.h" -#include "sharing/common/compatible_u8_string.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/internal/base/mime.h" #include "sharing/proto/wire_format.pb.h" @@ -49,8 +48,8 @@ FileAttachment::Type FileAttachmentTypeFromMimeType( return service::proto::FileMetadata::UNKNOWN; } -std::string MimeTypeFromPath(const std::filesystem::path& path) { - std::string extension = path.extension().string(); +std::string MimeTypeFromPath(const FilePath& path) { + std::string extension = path.GetExtension().ToString(); return extension.empty() ? "application/octet-stream" : nearby::utils::GetWellKnownMimeTypeFromExtension( extension.substr(1)); @@ -58,17 +57,15 @@ std::string MimeTypeFromPath(const std::filesystem::path& path) { } // namespace -FileAttachment::FileAttachment(std::filesystem::path file_path, - absl::string_view mime_type, +FileAttachment::FileAttachment(FilePath file_path, absl::string_view mime_type, std::string parent_folder, int32_t batch_id, AttachmentSourceType source_type) : Attachment(Attachment::Family::kFile, /*size=*/0, batch_id, source_type), mime_type_(mime_type.empty() ? MimeTypeFromPath(file_path) : mime_type), type_(FileAttachmentTypeFromMimeType(mime_type_)), - file_path_(std::move(file_path)), parent_folder_(std::move(parent_folder)) { - file_name_ = - GetCompatibleU8String(file_path_.value_or(L"").filename().u8string()); + file_name_ = file_path.GetFileName().ToString(); + file_path_ = std::move(file_path); } FileAttachment::FileAttachment(int64_t id, int64_t size, std::string file_name, diff --git a/sharing/file_attachment.h b/sharing/file_attachment.h index f486101c..b819b291 100644 --- a/sharing/file_attachment.h +++ b/sharing/file_attachment.h @@ -16,12 +16,12 @@ #define THIRD_PARTY_NEARBY_SHARING_FILE_ATTACHMENT_H_ #include -#include // NOLINT(build/c++17) #include #include #include #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #if defined(_WIN32) #if defined(PLATFORM_UNKNOWN) #define UNDEF_PLATFORM_UNKNOWN @@ -49,7 +49,7 @@ class FileAttachment : public Attachment { using Type = nearby::sharing::service::proto::FileMetadata::Type; explicit FileAttachment( - std::filesystem::path file_path, absl::string_view mime_type = "", + FilePath file_path, absl::string_view mime_type = "", std::string parent_folder = "", int32_t batch_id = 0, location::nearby::proto::sharing::AttachmentSourceType source_type = location::nearby::proto::sharing::ATTACHMENT_SOURCE_UNKNOWN); @@ -68,7 +68,7 @@ class FileAttachment : public Attachment { absl::string_view mime_type() const { return mime_type_; } absl::string_view parent_folder() const { return parent_folder_; } Type type() const { return type_; } - const std::optional& file_path() const { + const std::optional& file_path() const { return file_path_; } @@ -76,7 +76,7 @@ class FileAttachment : public Attachment { absl::string_view GetDescription() const override; ShareType GetShareType() const override; - void set_file_path(std::optional path) { + void set_file_path(std::optional path) { file_path_ = std::move(path); } @@ -85,7 +85,7 @@ class FileAttachment : public Attachment { std::string file_name_; std::string mime_type_; Type type_; - std::optional file_path_; + std::optional file_path_; std::string parent_folder_; }; diff --git a/sharing/incoming_share_session.cc b/sharing/incoming_share_session.cc index c6985eb4..157c45c1 100644 --- a/sharing/incoming_share_session.cc +++ b/sharing/incoming_share_session.cc @@ -15,7 +15,6 @@ #include "sharing/incoming_share_session.h" #include -#include // NOLINT #include #include #include @@ -32,7 +31,6 @@ #include "internal/platform/task_runner.h" #include "sharing/analytics/analytics_recorder.h" #include "sharing/attachment_container.h" -#include "sharing/common/compatible_u8_string.h" #include "sharing/constants.h" #include "sharing/file_attachment.h" #include "sharing/internal/public/logging.h" @@ -307,9 +305,8 @@ bool IncomingShareSession::UpdateFilePayloadPaths() { continue; } - auto file_path = incoming_payload->content.file_payload.file.path; - VLOG(1) << __func__ << ": Updated file_path=" - << GetCompatibleU8String(file_path.u8string()); + FilePath file_path = incoming_payload->content.file_payload.file.path; + VLOG(1) << __func__ << ": Updated file_path=" << file_path.ToString(); file.set_file_path(file_path); } return result; @@ -406,7 +403,7 @@ std::vector IncomingShareSession::GetPayloadFilePaths() attachment_payload_map(); for (const auto& file : container.GetFileAttachments()) { if (!file.file_path().has_value()) continue; - FilePath file_path = FilePath::FromPath(*file.file_path()); + FilePath file_path = *file.file_path(); VLOG(1) << __func__ << ": file_path=" << file_path.ToString(); if (attachment_paylod_map.find(file.id()) == attachment_paylod_map.end()) { continue; diff --git a/sharing/incoming_share_session_test.cc b/sharing/incoming_share_session_test.cc index 3b2e28f2..dad7ed41 100644 --- a/sharing/incoming_share_session_test.cc +++ b/sharing/incoming_share_session_test.cc @@ -90,7 +90,7 @@ constexpr absl::string_view kEndpointId = "ABCD"; std::unique_ptr CreateFilePayload(int64_t payload_id, FilePath file_path) { auto file_payload = - std::make_unique(InputFile(file_path.GetPath())); + std::make_unique(InputFile(file_path.ToString())); file_payload->id = payload_id; return file_payload; } @@ -887,10 +887,10 @@ TEST_F(IncomingShareSessionTest, PayloadTransferUpdateCompleteWithSuccess) { EXPECT_THAT(*metadata, HasStatus(TransferMetadata::Status::kComplete)); EXPECT_THAT( session_.attachment_container().GetFileAttachments()[0].file_path(), - Eq(file1_path.GetPath())); + Eq(file1_path)); EXPECT_THAT( session_.attachment_container().GetFileAttachments()[1].file_path(), - Eq(file2_path.GetPath())); + Eq(file2_path)); EXPECT_THAT( session_.attachment_container().GetTextAttachments()[0].text_body(), Eq(text_content1)); @@ -969,10 +969,10 @@ TEST_F(IncomingShareSessionTest, PayloadTransferUpdateCancelled) { EXPECT_THAT(*metadata, HasStatus(TransferMetadata::Status::kCancelled)); EXPECT_THAT( session_.attachment_container().GetFileAttachments()[0].file_path(), - Eq(file1_path.GetPath())); + Eq(file1_path)); EXPECT_THAT( session_.attachment_container().GetFileAttachments()[1].file_path(), - Eq(file2_path.GetPath())); + Eq(file2_path)); EXPECT_THAT( connections_manager_.connection_endpoint_info(kEndpointId).has_value(), IsTrue()); diff --git a/sharing/nearby_connections_manager.h b/sharing/nearby_connections_manager.h index dd41e677..896ca7b3 100644 --- a/sharing/nearby_connections_manager.h +++ b/sharing/nearby_connections_manager.h @@ -17,7 +17,6 @@ #include -#include // NOLINT(build/c++17) #include #include #include @@ -27,6 +26,7 @@ #include "absl/container/flat_hash_set.h" #include "absl/strings/string_view.h" #include "absl/types/span.h" +#include "internal/base/file_path.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/nearby_connection.h" #include "sharing/nearby_connections_types.h" @@ -157,7 +157,7 @@ class NearbyConnectionsManager { virtual void SetCustomSavePath(absl::string_view custom_save_path) = 0; // Gets the file paths to delete and clear the hash set. - virtual absl::flat_hash_set + virtual absl::flat_hash_set GetAndClearUnknownFilePathsToDelete() = 0; // Dump internal state for debugging purposes. diff --git a/sharing/nearby_connections_manager_impl.cc b/sharing/nearby_connections_manager_impl.cc index 262ae3ba..bf27e4db 100644 --- a/sharing/nearby_connections_manager_impl.cc +++ b/sharing/nearby_connections_manager_impl.cc @@ -16,7 +16,6 @@ #include -#include // NOLINT(build/c++17) #include #include #include @@ -28,10 +27,10 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "absl/functional/bind_front.h" -#include "absl/meta/type_traits.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "absl/types/span.h" +#include "internal/base/file_path.h" #include "internal/flags/nearby_flags.h" #include "internal/platform/device_info.h" #include "internal/platform/mutex_lock.h" @@ -825,7 +824,7 @@ void NearbyConnectionsManagerImpl::DeleteUnknownFilePayloadAndCancel( void NearbyConnectionsManagerImpl::ProcessUnknownFilePathsToDelete( PayloadStatus status, PayloadContent::Type type, - const std::filesystem::path& path) { + const FilePath& path) { // Unknown payload comes as kInProgress and kCanceled status with kFile type // from NearbyConnections. Delete it. if ((status == PayloadStatus::kCanceled || @@ -977,13 +976,13 @@ void NearbyConnectionsManagerImpl::SetCustomSavePath( }); } -absl::flat_hash_set +absl::flat_hash_set NearbyConnectionsManagerImpl::GetUnknownFilePathsToDelete() { MutexLock lock(&mutex_); return file_paths_to_delete_; } -absl::flat_hash_set +absl::flat_hash_set NearbyConnectionsManagerImpl::GetAndClearUnknownFilePathsToDelete() { MutexLock lock(&mutex_); auto file_paths_to_delete = std::move(file_paths_to_delete_); @@ -991,20 +990,19 @@ NearbyConnectionsManagerImpl::GetAndClearUnknownFilePathsToDelete() { return file_paths_to_delete; } -absl::flat_hash_set +absl::flat_hash_set NearbyConnectionsManagerImpl::GetUnknownFilePathsToDeleteForTesting() { return GetUnknownFilePathsToDelete(); } void NearbyConnectionsManagerImpl::AddUnknownFilePathsToDeleteForTesting( - std::filesystem::path file_path) { + FilePath file_path) { MutexLock lock(&mutex_); file_paths_to_delete_.insert(file_path); } void NearbyConnectionsManagerImpl::ProcessUnknownFilePathsToDeleteForTesting( - PayloadStatus status, PayloadContent::Type type, - const std::filesystem::path& path) { + PayloadStatus status, PayloadContent::Type type, const FilePath& path) { ProcessUnknownFilePathsToDelete(status, type, path); } diff --git a/sharing/nearby_connections_manager_impl.h b/sharing/nearby_connections_manager_impl.h index 98d5ba4f..82041689 100644 --- a/sharing/nearby_connections_manager_impl.h +++ b/sharing/nearby_connections_manager_impl.h @@ -17,7 +17,6 @@ #include -#include // NOLINT(build/c++17) #include #include #include @@ -27,6 +26,7 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "absl/strings/string_view.h" +#include "internal/base/file_path.h" #include "internal/platform/device_info.h" #include "internal/platform/mutex.h" #include "internal/platform/task_runner.h" @@ -87,20 +87,18 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager { absl::string_view endpoint_id) override; void UpgradeBandwidth(absl::string_view endpoint_id) override; void SetCustomSavePath(absl::string_view custom_save_path) override; - absl::flat_hash_set - GetAndClearUnknownFilePathsToDelete() override; + absl::flat_hash_set GetAndClearUnknownFilePathsToDelete() override; std::string Dump() const override; NearbyConnectionsService* GetNearbyConnectionsService() const { return nearby_connections_service_.get(); } - absl::flat_hash_set - GetUnknownFilePathsToDeleteForTesting(); - void AddUnknownFilePathsToDeleteForTesting(std::filesystem::path file_path); - void ProcessUnknownFilePathsToDeleteForTesting( - PayloadStatus status, PayloadContent::Type type, - const std::filesystem::path& path); + absl::flat_hash_set GetUnknownFilePathsToDeleteForTesting(); + void AddUnknownFilePathsToDeleteForTesting(FilePath file_path); + void ProcessUnknownFilePathsToDeleteForTesting(PayloadStatus status, + PayloadContent::Type type, + const FilePath& path); void OnPayloadTransferUpdateForTesting(absl::string_view endpoint_id, const PayloadTransferUpdate& update); void OnPayloadReceivedForTesting(absl::string_view endpoint_id, @@ -129,9 +127,9 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager { ConnectionsStatus status); void ProcessUnknownFilePathsToDelete(PayloadStatus status, PayloadContent::Type type, - const std::filesystem::path& path); + const FilePath& path); void DeleteUnknownFilePayloadAndCancel(Payload& payload); - absl::flat_hash_set GetUnknownFilePathsToDelete(); + absl::flat_hash_set GetUnknownFilePathsToDelete(); std::optional> GetStatusListenerForId( int64_t payload_id) const ABSL_LOCKS_EXCLUDED(mutex_); @@ -208,8 +206,7 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager { ABSL_GUARDED_BY(mutex_); // A set of file paths to delete. - absl::flat_hash_set file_paths_to_delete_ - ABSL_GUARDED_BY(mutex_); + absl::flat_hash_set file_paths_to_delete_ ABSL_GUARDED_BY(mutex_); }; } // namespace sharing diff --git a/sharing/nearby_connections_manager_impl_test.cc b/sharing/nearby_connections_manager_impl_test.cc index 29f16f4b..a7937e5c 100644 --- a/sharing/nearby_connections_manager_impl_test.cc +++ b/sharing/nearby_connections_manager_impl_test.cc @@ -17,7 +17,6 @@ #include #include -#include // NOLINT(build/c++17) #include #include #include @@ -35,6 +34,8 @@ #include "absl/time/time.h" #include "absl/types/optional.h" #include "absl/types/span.h" +#include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/flags/nearby_flags.h" #include "internal/test/fake_clock.h" #include "internal/test/fake_device_info.h" @@ -86,8 +87,8 @@ constexpr uint8_t kBluetoothMacAddress[] = {0x00, 0x00, 0xe6, 0x88, 0x64, 0x13}; constexpr char kInvalidBluetoothMacAddress[] = {0x07, 0x07, 0x07}; constexpr absl::Duration kSynchronizationTimeOut = absl::Milliseconds(200); -void InitializeTemporaryFile(std::filesystem::path& file) { - std::FILE* output_fp = std::fopen(file.string().c_str(), "wb+"); +void InitializeTemporaryFile(FilePath& file) { + std::FILE* output_fp = std::fopen(file.GetPath().c_str(), "wb+"); ASSERT_NE(output_fp, nullptr); EXPECT_EQ(std::fwrite(kPayload, 1, sizeof(kPayload), output_fp), sizeof(kPayload)); @@ -387,8 +388,7 @@ class NearbyConnectionsManagerImplTest : public testing::Test { const std::vector expected_payload(std::begin(kPayload), std::end(kPayload)); - std::filesystem::path file(std::filesystem::temp_directory_path() / - "file.jpg"); + FilePath file = GetTemporaryDirectory()->append(FilePath("file.jpg")); InitializeTemporaryFile(file); absl::Notification notification; @@ -405,7 +405,7 @@ class NearbyConnectionsManagerImplTest : public testing::Test { FilePayload file_payload = std::move(payload->content.file_payload); std::vector payload_bytes(file_payload.size); std::FILE* payload_fp = - std::fopen(file_payload.file.path.string().c_str(), "rb"); + std::fopen(file_payload.file.path.GetPath().c_str(), "rb"); ASSERT_NE(payload_fp, nullptr); EXPECT_EQ(std::fread(payload_bytes.data(), 1, file_payload.size, payload_fp), @@ -419,7 +419,7 @@ class NearbyConnectionsManagerImplTest : public testing::Test { // Manually setup payload id, because the tested id is not generated from // file name. - auto payload = std::make_unique(InputFile(file)); + auto payload = std::make_unique(InputFile(file.ToString())); payload->id = payload_id; nearby_connections_manager_->Send(kRemoteEndpointId, std::move(payload), @@ -1403,18 +1403,16 @@ TEST_F(NearbyConnectionsManagerImplTest, nearby_connections_manager_->RegisterPayloadStatusListener( kPayloadId3, payload_listener->GetWeakPtr()); - std::filesystem::path file1(std::filesystem::temp_directory_path() / - "file1.jpg"); - std::filesystem::path file2(std::filesystem::temp_directory_path() / - "file2.jpg"); + FilePath file1 = GetTemporaryDirectory()->append(FilePath("file1.jpg")); + FilePath file2 = GetTemporaryDirectory()->append(FilePath("file2.jpg")); InitializeTemporaryFile(file1); InitializeTemporaryFile(file2); - payload_listener_remote.payload_cb(kRemoteEndpointId, - Payload(kPayloadId, InputFile(file1))); - payload_listener_remote.payload_cb(kRemoteEndpointId, - Payload(kPayloadId2, InputFile(file2))); + payload_listener_remote.payload_cb( + kRemoteEndpointId, Payload(kPayloadId, InputFile(file1.ToString()))); + payload_listener_remote.payload_cb( + kRemoteEndpointId, Payload(kPayloadId2, InputFile(file2.ToString()))); const std::vector byte_payload(std::begin(kBytePayload), std::end(kBytePayload)); @@ -1521,12 +1519,11 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingFilePayload) { const std::vector expected_payload(std::begin(kPayload), std::end(kPayload)); - std::filesystem::path file(std::filesystem::temp_directory_path() / - "file.jpg"); + FilePath file = GetTemporaryDirectory()->append(FilePath("file.jpg")); InitializeTemporaryFile(file); - payload_listener_remote.payload_cb(kRemoteEndpointId, - Payload(kPayloadId, InputFile(file))); + payload_listener_remote.payload_cb( + kRemoteEndpointId, Payload(kPayloadId, InputFile(file.ToString()))); absl::Notification payload_notification; EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_)).WillOnce([&]() { @@ -1546,7 +1543,7 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingFilePayload) { ASSERT_TRUE(payload->content.is_file()); std::vector payload_bytes(payload->content.file_payload.size); std::FILE* payload_fp = std::fopen( - payload->content.file_payload.file.path.string().c_str(), "rb"); + payload->content.file_payload.file.path.GetPath().c_str(), "rb"); ASSERT_NE(payload_fp, nullptr); EXPECT_EQ(std::fread(payload_bytes.data(), 1, payload->content.file_payload.size, payload_fp), @@ -1572,12 +1569,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ClearIncomingPayloads) { nearby_connections_manager_->RegisterPayloadStatusListener( kPayloadId, payload_listener->GetWeakPtr()); - std::filesystem::path file(std::filesystem::temp_directory_path() / - "file.jpg"); + FilePath file = GetTemporaryDirectory()->append(FilePath("file.jpg")); InitializeTemporaryFile(file); - payload_listener_remote.payload_cb(kRemoteEndpointId, - Payload(kPayloadId, InputFile(file))); + payload_listener_remote.payload_cb( + kRemoteEndpointId, Payload(kPayloadId, InputFile(file.ToString()))); absl::Notification payload_notification; EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_)).WillOnce([&]() { @@ -1804,13 +1800,13 @@ TEST_F(NearbyConnectionsManagerImplTest, TEST_F(NearbyConnectionsManagerImplTest, UnknownFilePathsToDelete) { nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test1.txt"); + FilePath("test1.txt")); nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test2.txt"); + FilePath("test2.txt")); auto unknown_file_paths = nearby_connections_manager_->GetUnknownFilePathsToDeleteForTesting(); nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test3.txt"); + FilePath("test3.txt")); // Test if we get copy of container. EXPECT_NE(unknown_file_paths.size(), 3); @@ -1821,7 +1817,8 @@ TEST_F(NearbyConnectionsManagerImplTest, UnknownFilePathsToDelete) { unknown_file_paths = nearby_connections_manager_->GetUnknownFilePathsToDeleteForTesting(); EXPECT_THAT(unknown_file_paths, - UnorderedElementsAre("test1.txt", "test2.txt", "test3.txt")); + UnorderedElementsAre(FilePath("test1.txt"), FilePath("test2.txt"), + FilePath("test3.txt"))); nearby_connections_manager_->GetAndClearUnknownFilePathsToDelete(); EXPECT_TRUE( nearby_connections_manager_->GetUnknownFilePathsToDeleteForTesting() @@ -1829,9 +1826,9 @@ TEST_F(NearbyConnectionsManagerImplTest, UnknownFilePathsToDelete) { // Test GetAndClearUnknownFilePathsToDelete nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test1.txt"); + FilePath("test1.txt")); nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test2.txt"); + FilePath("test2.txt")); unknown_file_paths = nearby_connections_manager_->GetAndClearUnknownFilePathsToDelete(); EXPECT_EQ(unknown_file_paths.size(), 2); @@ -1851,16 +1848,15 @@ TEST_F(NearbyConnectionsManagerImplTest, ASSERT_TRUE(OnIncomingConnection(connection_listener_remote, incoming_connection_listener, payload_listener_remote) != nullptr); - std::filesystem::path file(std::filesystem::temp_directory_path() / - "file.jpg"); - payload_listener_remote.payload_cb(kRemoteEndpointId, - Payload(kPayloadId, InputFile(file))); + FilePath file = GetTemporaryDirectory()->append(FilePath("file.jpg")); + payload_listener_remote.payload_cb( + kRemoteEndpointId, Payload(kPayloadId, InputFile(file.ToString()))); nearby_connections_manager_->OnPayloadTransferUpdateForTesting( kRemoteEndpointId, PayloadTransferUpdate(kPayloadId, PayloadStatus::kCanceled, kTotalSize, /*bytes_transferred=*/kTotalSize)); - absl::flat_hash_set unknown_file_paths = + absl::flat_hash_set unknown_file_paths = nearby_connections_manager_->GetUnknownFilePathsToDeleteForTesting(); EXPECT_EQ(unknown_file_paths.size(), 1); nearby_connections_manager_->GetAndClearUnknownFilePathsToDelete(); @@ -1885,10 +1881,9 @@ TEST_F(NearbyConnectionsManagerImplTest, OnPayloadReceivedForUnknownFile) { ASSERT_TRUE(OnIncomingConnection(connection_listener_remote, incoming_connection_listener, payload_listener_remote) != nullptr); - std::filesystem::path file(std::filesystem::temp_directory_path() / - "file.jpg"); - payload_listener_remote.payload_cb(kRemoteEndpointId, - Payload(kPayloadId, InputFile(file))); + FilePath file = GetTemporaryDirectory()->append(FilePath("file.jpg")); + payload_listener_remote.payload_cb( + kRemoteEndpointId, Payload(kPayloadId, InputFile(file.ToString()))); // Flag is on. Add unknown file paths with kCanceled to the list. NearbyFlags::GetInstance().OverrideBoolFlagValue( @@ -1896,13 +1891,12 @@ TEST_F(NearbyConnectionsManagerImplTest, OnPayloadReceivedForUnknownFile) { kDeleteUnexpectedReceivedFileFix, true); nearby_connections_manager_->ClearIncomingPayloads(); - Payload payload(kPayloadId, InputFile(file)); + Payload payload(kPayloadId, InputFile(file.ToString())); nearby_connections_manager_->OnPayloadReceivedForTesting(kRemoteEndpointId, payload); - std::filesystem::path file2(std::filesystem::temp_directory_path() / - "file2.jpg"); - Payload payload2(kPayloadId, InputFile(file2)); + FilePath file2 = GetTemporaryDirectory()->append(FilePath("file2.jpg")); + Payload payload2(kPayloadId, InputFile(file2.ToString())); nearby_connections_manager_->OnPayloadReceivedForTesting(kRemoteEndpointId, payload2); auto unknown_file_paths = @@ -1914,9 +1908,8 @@ TEST_F(NearbyConnectionsManagerImplTest, OnPayloadReceivedForUnknownFile) { std::make_shared>(); nearby_connections_manager_->RegisterPayloadStatusListener( kPayloadId, payload_listener->GetWeakPtr()); - std::filesystem::path file3(std::filesystem::temp_directory_path() / - "file3.jpg"); - Payload payload3(kPayloadId, InputFile(file3)); + FilePath file3 = GetTemporaryDirectory()->append(FilePath("file3.jpg")); + Payload payload3(kPayloadId, InputFile(file3.ToString())); nearby_connections_manager_->OnPayloadReceivedForTesting(kRemoteEndpointId, payload3); unknown_file_paths = @@ -1946,10 +1939,9 @@ TEST_F(NearbyConnectionsManagerImplTest, nearby_connections_manager_->RegisterPayloadStatusListener( kPayloadId, payload_listener->GetWeakPtr()); - std::filesystem::path file(std::filesystem::temp_directory_path() / - "file.jpg"); + FilePath file = GetTemporaryDirectory()->append(FilePath("file.jpg")); - Payload payload(kPayloadId, InputFile(file)); + Payload payload(kPayloadId, InputFile(file.ToString())); nearby_connections_manager_->OnPayloadReceivedForTesting(kRemoteEndpointId, payload); @@ -1978,9 +1970,8 @@ TEST_F(NearbyConnectionsManagerImplTest, payload_notification.Notify(); }); - std::filesystem::path file2(std::filesystem::temp_directory_path() / - "file2.jpg"); - Payload payload2(kPayloadId, InputFile(file2)); + FilePath file2 = GetTemporaryDirectory()->append(FilePath("file2.jpg")); + Payload payload2(kPayloadId, InputFile(file2.ToString())); nearby_connections_manager_->OnPayloadReceivedForTesting(kRemoteEndpointId, payload2); unknown_file_paths = @@ -1994,11 +1985,10 @@ TEST_F(NearbyConnectionsManagerImplTest, } TEST_F(NearbyConnectionsManagerImplTest, ProcessUnknownFilePathsToDelete) { - std::filesystem::path file(std::filesystem::temp_directory_path() / - "file.jpg"); + FilePath file = GetTemporaryDirectory()->append(FilePath("file.jpg")); nearby_connections_manager_->ProcessUnknownFilePathsToDeleteForTesting( PayloadStatus::kCanceled, PayloadContent::Type::kFile, file); - absl::flat_hash_set unknown_file_paths = + absl::flat_hash_set unknown_file_paths = nearby_connections_manager_->GetUnknownFilePathsToDeleteForTesting(); EXPECT_EQ(unknown_file_paths.size(), 1); nearby_connections_manager_->GetAndClearUnknownFilePathsToDelete(); diff --git a/sharing/nearby_connections_service.cc b/sharing/nearby_connections_service.cc index 3bd58d31..96ed757f 100644 --- a/sharing/nearby_connections_service.cc +++ b/sharing/nearby_connections_service.cc @@ -16,14 +16,12 @@ #include #include -#include // NOLINT(build/c++17) #include #include #include #include #include "internal/platform/file.h" -#include "sharing/common/compatible_u8_string.h" #include "sharing/internal/public/logging.h" #include "sharing/nearby_connections_types.h" @@ -58,10 +56,9 @@ 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 = GetCompatibleU8String( - payload.content.file_payload.file.path.u8string()); - std::string file_name = GetCompatibleU8String( - payload.content.file_payload.file.path.filename().u8string()); + std::string file_path = payload.content.file_payload.file.path.ToString(); + std::string file_name = + payload.content.file_payload.file.path.GetFileName().ToString(); std::string parent_folder = payload.content.file_payload.parent_folder; std::replace(parent_folder.begin(), parent_folder.end(), '\\', '/'); VLOG(1) << __func__ << ": NC Payload file_path=" << file_path diff --git a/sharing/nearby_connections_types.h b/sharing/nearby_connections_types.h index 1eaa380d..fd431e6b 100644 --- a/sharing/nearby_connections_types.h +++ b/sharing/nearby_connections_types.h @@ -17,7 +17,6 @@ #include -#include // NOLINT(build/c++17) #include #include #include @@ -28,9 +27,9 @@ #include "absl/random/random.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" +#include "internal/base/file_path.h" #include "internal/base/files.h" #include "internal/interop/authentication_status.h" -#include "sharing/common/compatible_u8_string.h" namespace nearby { namespace sharing { @@ -358,11 +357,10 @@ enum class DistanceInfo { struct InputFile { InputFile() = default; - explicit InputFile(std::string path) { - this->path = std::filesystem::u8path(path); - } + explicit InputFile(absl::string_view file_path) + : path(file_path) {} - std::filesystem::path path; + FilePath path; }; // A simple payload containing raw bytes. @@ -412,10 +410,10 @@ struct Payload { explicit Payload(InputFile file, absl::string_view parent_folder = absl::string_view()) { - id = std::hash()(GetCompatibleU8String(file.path.u8string())); + id = std::hash()(file.path.ToString()); content.type = PayloadContent::Type::kFile; - std::optional size = GetFileSize(file.path); + std::optional size = GetFileSize(file.path.GetPath()); if (size.has_value()) { content.file_payload.size = *size; } @@ -433,7 +431,7 @@ struct Payload { absl::string_view parent_folder = absl::string_view()) : id(id) { content.type = PayloadContent::Type::kFile; - std::optional size = GetFileSize(file.path); + std::optional size = GetFileSize(file.path.GetPath()); if (size.has_value()) { content.file_payload.size = *size; } diff --git a/sharing/nearby_file_handler.cc b/sharing/nearby_file_handler.cc index 123d7905..da3afd35 100644 --- a/sharing/nearby_file_handler.cc +++ b/sharing/nearby_file_handler.cc @@ -15,7 +15,6 @@ #include "sharing/nearby_file_handler.h" #include -#include // NOLINT(build/c++17) #include #include #include @@ -29,7 +28,6 @@ #include "internal/base/file_path.h" #include "internal/base/files.h" #include "internal/platform/task_runner_impl.h" -#include "sharing/common/compatible_u8_string.h" #include "sharing/internal/api/sharing_platform.h" #include "sharing/internal/public/logging.h" @@ -41,13 +39,13 @@ using ::nearby::sharing::api::SharingPlatform; // Called on the FileTaskRunner to actually open the files passed. std::vector DoOpenFiles( - absl::Span file_paths) { + absl::Span file_paths) { std::vector files; for (const auto& file_path : file_paths) { - std::optional size = GetFileSize(file_path); + std::optional size = GetFileSize(file_path.GetPath()); if (!size.has_value()) { - LOG(ERROR) << __func__ << ": Failed to open file. File=" - << GetCompatibleU8String(file_path.u8string()); + LOG(ERROR) << __func__ + << ": Failed to open file. File=" << file_path.ToString(); return {}; } files.push_back({*size, file_path}); @@ -64,7 +62,7 @@ NearbyFileHandler::NearbyFileHandler(SharingPlatform& platform) NearbyFileHandler::~NearbyFileHandler() = default; -void NearbyFileHandler::OpenFiles(std::vector file_paths, +void NearbyFileHandler::OpenFiles(std::vector file_paths, OpenFilesCallback callback) { sequenced_task_runner_->PostTask( [callback = std::move(callback), file_paths = std::move(file_paths)]() { diff --git a/sharing/nearby_file_handler.h b/sharing/nearby_file_handler.h index ff9e9558..b1a2e8ed 100644 --- a/sharing/nearby_file_handler.h +++ b/sharing/nearby_file_handler.h @@ -17,7 +17,6 @@ #include -#include // NOLINT(build/c++17) #include #include #include @@ -36,7 +35,7 @@ class NearbyFileHandler { public: struct FileInfo { uint64_t size; - std::filesystem::path file_path; + FilePath file_path; }; using OpenFilesCallback = std::function)>; @@ -47,8 +46,7 @@ class NearbyFileHandler { // Open the files given in |file_paths| and return the opened files sizes via // |callback|. If any file fails to open, return an empty list. - void OpenFiles(std::vector file_paths, - OpenFilesCallback callback); + void OpenFiles(std::vector file_paths, OpenFilesCallback callback); void DeleteFilesFromDisk(std::vector file_paths, DeleteFilesFromDiskCallback callback); diff --git a/sharing/nearby_file_handler_test.cc b/sharing/nearby_file_handler_test.cc index 775ac06e..5305433b 100644 --- a/sharing/nearby_file_handler_test.cc +++ b/sharing/nearby_file_handler_test.cc @@ -16,7 +16,6 @@ #include #include -#include // NOLINT(build/c++17) #include #include "gtest/gtest.h" @@ -32,8 +31,8 @@ namespace sharing { namespace { using ::nearby::sharing::api::MockSharingPlatform; -bool CreateFile(std::filesystem::path file_path) { - std::FILE* file = std::fopen(file_path.c_str(), "w+"); +bool CreateFile(FilePath& file_path) { + std::FILE* file = std::fopen(file_path.GetPath().c_str(), "w+"); if (file == nullptr) { return false; } @@ -46,8 +45,8 @@ TEST(NearbyFileHandler, OpenFiles) { NearbyFileHandler nearby_file_handler(mock_platform); absl::Notification notification; std::vector result; - std::filesystem::path test_file = - std::filesystem::temp_directory_path() / "nearby_nfh_test_abc.jpg"; + FilePath test_file = + GetTemporaryDirectory()->append(FilePath("nearby_nfh_test_abc.jpg")); ASSERT_TRUE(CreateFile(test_file)); nearby_file_handler.OpenFiles( @@ -59,7 +58,7 @@ TEST(NearbyFileHandler, OpenFiles) { notification.WaitForNotificationWithTimeout(absl::Seconds(1)); EXPECT_EQ(result.size(), 1); - ASSERT_TRUE(RemoveFile(test_file)); + ASSERT_TRUE(RemoveFile(test_file.GetPath())); } TEST(NearbyFileHandler, DeleteAFileFromDisk) { @@ -67,7 +66,7 @@ TEST(NearbyFileHandler, DeleteAFileFromDisk) { NearbyFileHandler nearby_file_handler(mock_platform); FilePath test_file = GetTemporaryDirectory()->append(FilePath("nearby_nfh_test_abc.jpg")); - ASSERT_TRUE(CreateFile(test_file.GetPath())); + ASSERT_TRUE(CreateFile(test_file)); std::vector file_paths; file_paths.push_back(test_file); nearby_file_handler.DeleteFilesFromDisk(file_paths, []() {}); @@ -104,7 +103,7 @@ TEST(NearbyFileHandler, TestCallback) { NearbyFileHandler nearby_file_handler(mock_platform); FilePath test_file = GetTemporaryDirectory()->append(FilePath("nearby_nfh_test_abc.jpg")); - ASSERT_TRUE(CreateFile(test_file.GetPath())); + ASSERT_TRUE(CreateFile(test_file)); std::vector file_paths; file_paths.push_back(test_file); nearby_file_handler.DeleteFilesFromDisk( diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 61fc16e2..137d5fde 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -20,7 +20,6 @@ #include #include #include -#include // NOLINT(build/c++17) #include #include #include @@ -761,7 +760,7 @@ void NearbySharingServiceImpl::SendAttachments( } for (const FileAttachment& attachment : attachment_container->GetFileAttachments()) { - if (!attachment.file_path() || attachment.file_path()->empty()) { + if (!attachment.file_path() || attachment.file_path()->IsEmpty()) { LOG(WARNING) << __func__ << ": Got file attachment without path"; std::move(status_codes_callback)(StatusCodes::kInvalidArgument); return; @@ -2830,8 +2829,7 @@ void NearbySharingServiceImpl::OnReceivedIntroduction( session->session_id(), session->share_target(), /*referrer_package=*/std::nullopt, session->os_type()); - if (IsOutOfStorage(device_info_, - std::filesystem::u8path(settings_->GetCustomSavePath()), + if (IsOutOfStorage(device_info_, FilePath{settings_->GetCustomSavePath()}, session->attachment_container().GetStorageSize())) { Fail(*session, TransferMetadata::Status::kNotEnoughSpace); LOG(WARNING) << __func__ @@ -3134,7 +3132,7 @@ void NearbySharingServiceImpl::RemoveIncomingPayloads( for (auto it = file_paths_to_delete.begin(); it != file_paths_to_delete.end(); ++it) { VLOG(1) << __func__ << ": Has unknown file path to delete."; - files_for_deletion.push_back(FilePath::FromPath(*it)); + files_for_deletion.push_back(*it); } std::vector payload_file_path = session.GetPayloadFilePaths(); files_for_deletion.insert(files_for_deletion.end(), payload_file_path.begin(), diff --git a/sharing/nearby_sharing_service_impl_test.cc b/sharing/nearby_sharing_service_impl_test.cc index 90253268..d44559f1 100644 --- a/sharing/nearby_sharing_service_impl_test.cc +++ b/sharing/nearby_sharing_service_impl_test.cc @@ -20,7 +20,6 @@ #include #include #include -#include // NOLINT(build/c++17) #include #include #include @@ -270,9 +269,9 @@ constexpr absl::Duration kCertificateDownloadDuringDiscoveryPeriod = absl::Seconds(10); std::unique_ptr GetFilePayload(int64_t payload_id) { - std::filesystem::path path = - std::filesystem::temp_directory_path() / absl::StrCat(payload_id); - InputFile input_file{path}; + FilePath path = + GetTemporaryDirectory()->append(FilePath(absl::StrCat(payload_id))); + InputFile input_file{path.ToString()}; return std::make_unique(input_file); } @@ -374,7 +373,7 @@ std::unique_ptr CreateTextAttachments( } std::unique_ptr CreateFileAttachments( - std::vector file_paths) { + std::vector file_paths) { auto attachment_container = std::make_unique(); for (auto& file_path : file_paths) { attachment_container->AddFileAttachment( @@ -1188,7 +1187,7 @@ class NearbySharingServiceImplTest : public testing::Test { fake_nearby_connections_manager_->GetRegisteredPayloadStatusListener( kFilePayloadId); - std::filesystem::path file_path; + FilePath file_path; absl::Notification success_notification; EXPECT_CALL(callback, OnTransferUpdate(testing::_, testing::_, testing::_)) .WillOnce(testing::Invoke([&](const ShareTarget& share_target, @@ -1231,7 +1230,7 @@ class NearbySharingServiceImplTest : public testing::Test { EXPECT_FALSE(fake_nearby_connections_manager_->has_incoming_payloads()); // Remove test file. - std::filesystem::remove(file_path); + RemoveFile(file_path.GetPath()); } void FlushTesting() { @@ -1259,10 +1258,10 @@ class NearbySharingServiceImplTest : public testing::Test { FakeAccountManager& account_manager() { return fake_account_manager_; } - std::filesystem::path CreateTestFile(absl::string_view name, + FilePath CreateTestFile(absl::string_view name, const std::vector& content) { - std::filesystem::path path = std::filesystem::temp_directory_path() / name; - std::FILE* file = std::fopen(path.string().c_str(), "w+"); + FilePath path = GetTemporaryDirectory()->append(FilePath(name)); + std::FILE* file = std::fopen(path.GetPath().c_str(), "w+"); std::fwrite(content.data(), 1, content.size(), file); std::fclose(file); return path; @@ -3470,7 +3469,7 @@ TEST_F(NearbySharingServiceImplTest, SendFileWithEmptyPath) { DiscoverShareTarget(transfer_callback, discovery_callback); ScopedSendSurface s(service_.get(), &transfer_callback); - EXPECT_EQ(SendAttachments(target_id, CreateFileAttachments({""})), + EXPECT_EQ(SendAttachments(target_id, CreateFileAttachments({FilePath{""}})), NearbySharingServiceImpl::StatusCodes::kInvalidArgument); } @@ -3482,7 +3481,7 @@ TEST_P(NearbySharingServiceImplSendFailureTest, SendFilesRemoteFailure) { ScopedSendSurface s(service_.get(), &transfer_callback); std::vector test_data = {'T', 'e', 's', 't'}; - std::filesystem::path path = CreateTestFile("text.txt", test_data); + FilePath path = CreateTestFile("text.txt", test_data); absl::Notification notification; ExpectTransferUpdates(transfer_callback, target_id, @@ -3631,7 +3630,7 @@ TEST_F(NearbySharingServiceImplTest, SendFilesSuccess) { std::vector test_data = {'T', 'e', 's', 't'}; std::string file_name = "test.txt"; - std::filesystem::path path = CreateTestFile(file_name, test_data); + FilePath path = CreateTestFile(file_name, test_data); absl::Notification introduction_notification; ExpectTransferUpdates(transfer_callback, target_id, @@ -3673,8 +3672,8 @@ TEST_F(NearbySharingServiceImplTest, SendFilesSuccess) { // Expect the file payload to be sent in the end. PayloadInfo info = GetWrittenPayload(); ASSERT_TRUE(info.payload->content.is_file()); - std::filesystem::path file = info.payload->content.file_payload.file.path; - ASSERT_TRUE(std::filesystem::exists(file)); + FilePath file = info.payload->content.file_payload.file.path; + ASSERT_TRUE(FileExists(file.GetPath())); } TEST_F(NearbySharingServiceImplTest, SendWifiCredentialsSuccess) { @@ -5028,14 +5027,15 @@ TEST_F(NearbySharingServiceImplTest, NoAdvertisingWhenHidden) { TEST_F(NearbySharingServiceImplTest, RemoveIncomingPayloads) { fake_nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test1.txt"); + FilePath{"test1.txt"}); fake_nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test2.txt"); + FilePath{"test2.txt"}); auto unknown_file_paths_to_delete = fake_nearby_connections_manager_->GetUnknownFilePathsToDeleteForTesting(); EXPECT_EQ(unknown_file_paths_to_delete.size(), 2); - EXPECT_THAT(unknown_file_paths_to_delete, - UnorderedElementsAre("test1.txt", "test2.txt")); + EXPECT_THAT( + unknown_file_paths_to_delete, + UnorderedElementsAre(FilePath("test1.txt"), FilePath("test2.txt"))); nearby::analytics::MockEventLogger mock_event_logger; analytics::AnalyticsRecorder analytics_recorder{/*vendor_id=*/0, &mock_event_logger}; @@ -5054,9 +5054,9 @@ TEST_F(NearbySharingServiceImplTest, RemoveIncomingPayloads) { // Test GetAndClearUnknownFilePathsToDelete fake_nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test1.txt"); + FilePath{"test1.txt"}); fake_nearby_connections_manager_->AddUnknownFilePathsToDeleteForTesting( - "test2.txt"); + FilePath{"test2.txt"}); unknown_file_paths_to_delete = fake_nearby_connections_manager_->GetAndClearUnknownFilePathsToDelete(); EXPECT_EQ(unknown_file_paths_to_delete.size(), 2); diff --git a/sharing/nearby_sharing_settings.cc b/sharing/nearby_sharing_settings.cc index 6e72a9e5..0592b970 100644 --- a/sharing/nearby_sharing_settings.cc +++ b/sharing/nearby_sharing_settings.cc @@ -15,7 +15,6 @@ #include "sharing/nearby_sharing_settings.h" #include -#include // NOLINT(build/c++17) #include #include #include @@ -30,7 +29,6 @@ #include "internal/platform/device_info.h" #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_recorder.h" -#include "sharing/common/compatible_u8_string.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/common/nearby_share_prefs.h" #include "sharing/internal/api/preference_manager.h" diff --git a/sharing/nearby_sharing_util.cc b/sharing/nearby_sharing_util.cc index f08595fb..c92b8428 100644 --- a/sharing/nearby_sharing_util.cc +++ b/sharing/nearby_sharing_util.cc @@ -18,7 +18,6 @@ #include #include #include -#include // NOLINT(build/c++17) #include #include #include @@ -138,10 +137,10 @@ std::string GetDeviceId( return std::string(endpoint_id); } -bool IsOutOfStorage(DeviceInfo& device_info, std::filesystem::path file_path, +bool IsOutOfStorage(DeviceInfo& device_info, FilePath file_path, int64_t storage_required) { std::optional available_storage = - device_info.GetAvailableDiskSpaceInBytes(FilePath::FromPath(file_path)); + device_info.GetAvailableDiskSpaceInBytes(file_path); if (!available_storage.has_value()) { return false; diff --git a/sharing/nearby_sharing_util.h b/sharing/nearby_sharing_util.h index d5ebbcd4..5e0e58ef 100644 --- a/sharing/nearby_sharing_util.h +++ b/sharing/nearby_sharing_util.h @@ -16,7 +16,6 @@ #define THIRD_PARTY_NEARBY_SHARING_NEARBY_SHARING_UTIL_H_ #include -#include // NOLINT(build/c++17) #include #include #include @@ -24,6 +23,7 @@ #include "absl/strings/string_view.h" #include "internal/platform/device_info.h" #include "proto/sharing_enums.pb.h" +#include "internal/base/file_path.h" #include "sharing/advertisement.h" #include "sharing/certificates/nearby_share_decrypted_public_certificate.h" #include "sharing/common/nearby_share_enums.h" @@ -36,8 +36,8 @@ namespace nearby::sharing { // device_info - Nearby Share DeviceInfo // file_path - The path is to store sharing contents. // storage_required - required storage space. -bool IsOutOfStorage(nearby::DeviceInfo& device_info, - std::filesystem::path file_path, int64_t storage_required); +bool IsOutOfStorage(nearby::DeviceInfo& device_info, FilePath file_path, + int64_t storage_required); // Decodes certificate to find MAC address encoded in it. std::optional> GetBluetoothMacAddressFromCertificate( diff --git a/sharing/outgoing_share_session.cc b/sharing/outgoing_share_session.cc index a596c015..186e0b19 100644 --- a/sharing/outgoing_share_session.cc +++ b/sharing/outgoing_share_session.cc @@ -16,7 +16,6 @@ #include #include -#include // NOLINT #include #include #include @@ -28,6 +27,7 @@ #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" +#include "internal/base/file_path.h" #include "internal/platform/clock.h" #include "internal/platform/task_runner.h" #include "sharing/analytics/analytics_recorder.h" @@ -160,8 +160,8 @@ void OutgoingShareSession::OnConnectionDisconnected() { } } -std::vector OutgoingShareSession::GetFilePaths() const { - std::vector file_paths; +std::vector OutgoingShareSession::GetFilePaths() const { + std::vector file_paths; file_paths.reserve(attachment_container().GetFileAttachments().size()); for (const FileAttachment& file_attachment : attachment_container().GetFileAttachments()) { diff --git a/sharing/outgoing_share_session.h b/sharing/outgoing_share_session.h index e314ff1e..4387e63b 100644 --- a/sharing/outgoing_share_session.h +++ b/sharing/outgoing_share_session.h @@ -16,7 +16,6 @@ #define THIRD_PARTY_NEARBY_SHARING_OUTGOING_SHARE_SESSION_H_ #include -#include // NOLINT #include #include #include @@ -27,6 +26,7 @@ #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" +#include "internal/base/file_path.h" #include "internal/platform/clock.h" #include "internal/platform/task_runner.h" #include "sharing/analytics/analytics_recorder.h" @@ -84,7 +84,7 @@ class OutgoingShareSession : public ShareSession { PairedKeyVerificationRunner::PairedKeyVerificationResult result, location::nearby::proto::sharing::OSType share_target_os_type); - std::vector GetFilePaths() const; + std::vector GetFilePaths() const; void CreateTextPayloads(); void CreateWifiCredentialsPayloads(); diff --git a/sharing/outgoing_share_session_test.cc b/sharing/outgoing_share_session_test.cc index f1ce6c0f..ac31d844 100644 --- a/sharing/outgoing_share_session_test.cc +++ b/sharing/outgoing_share_session_test.cc @@ -28,6 +28,7 @@ #include "absl/time/time.h" #include "internal/analytics/mock_event_logger.h" #include "internal/analytics/sharing_log_matchers.h" +#include "internal/base/file_path.h" #include "internal/network/url.h" #include "internal/test/fake_clock.h" #include "internal/test/fake_device_info.h" @@ -95,13 +96,14 @@ class OutgoingShareSessionTest : public ::testing::Test { "A bit of text body", "Some text title", "text/html"), text2_(nearby::sharing::service::proto::TextMetadata::ADDRESS, "A bit of text body 2", "Some text title 2", "text/plain"), - file1_("/usr/local/tmp/someFileName.jpg", "/usr/local/parent"), - file2_("/usr/local/tmp/someFileName2.jpg", "/usr/local/parent2"), + file1_(FilePath("/usr/local/tmp/someFileName.jpg"), /*mime_type=*/"", + /*parent_folder=*/"/usr/local/parent"), + file2_(FilePath("/usr/local/tmp/someFileName2.jpg"), /*mime_type=*/"", + /*parent_folder=*/"/usr/local/parent2"), wifi1_( "GoogleGuest", nearby::sharing::service::proto::WifiCredentialsMetadata::WPA_PSK, - "somepassword", /*is_hidden=*/true) { - } + "somepassword", /*is_hidden=*/true) {} std::unique_ptr CreateDefaultAttachmentContainer() { return std::make_unique(