From ab00092c6c5fa79fe40403db25d0f3e8123c61e6 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Thu, 3 Oct 2024 12:35:56 -0700 Subject: [PATCH] Create hard link for latest log files. PiperOrigin-RevId: 681986911 --- Package.swift | 1 + internal/base/BUILD | 24 +++++++++++-------- internal/base/files.cc | 10 ++++++++ internal/base/files.h | 5 ++++ internal/base/files_test.cc | 48 +++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 internal/base/files_test.cc diff --git a/Package.swift b/Package.swift index 12f8cec2..8d06fd0d 100644 --- a/Package.swift +++ b/Package.swift @@ -493,6 +493,7 @@ let package = Package( "connections/status_test.cc", "connections/payload_test.cc", "internal/base/bluetooth_address_test.cc", + "internal/base/files_test.cc", "internal/crypto/ed25519_unittest.cc", "internal/crypto_cros/aead_unittest.cc", "internal/crypto_cros/ec_private_key_unittest.cc", diff --git a/internal/base/BUILD b/internal/base/BUILD index 5077a12c..689613f9 100644 --- a/internal/base/BUILD +++ b/internal/base/BUILD @@ -21,9 +21,6 @@ cc_library( hdrs = [ "observer_list.h", ], - copts = [ - "-Ithird_party", - ], visibility = [ "//fastpair:__subpackages__", "//internal/account:__subpackages__", @@ -46,9 +43,6 @@ cc_library( hdrs = [ "bluetooth_address.h", ], - copts = [ - "-Ithird_party", - ], visibility = [ "//fastpair:__subpackages__", "//internal:__subpackages__", @@ -76,10 +70,6 @@ cc_test( srcs = [ "bluetooth_address_test.cc", ], - copts = [ - "-Ithird_party", - ], - shard_count = 8, deps = [ ":bluetooth_address", "@com_github_protobuf_matchers//protobuf-matchers", @@ -88,3 +78,17 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "files_test", + size = "small", + timeout = "short", + srcs = [ + "files_test.cc", + ], + deps = [ + ":files", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/internal/base/files.cc b/internal/base/files.cc index f2056fb8..c3e3b12a 100644 --- a/internal/base/files.cc +++ b/internal/base/files.cc @@ -95,4 +95,14 @@ bool CreateDirectories(const std::filesystem::path& path) { return true; } +bool CreateHardLink(const std::filesystem::path& target, + const std::filesystem::path& link_path) { + std::error_code error_code; + std::filesystem::create_hard_link(target, link_path, error_code); + if (error_code) { + return false; + } + return true; +} + } // namespace nearby::sharing diff --git a/internal/base/files.h b/internal/base/files.h index f6c4ff5c..501e8752 100644 --- a/internal/base/files.h +++ b/internal/base/files.h @@ -51,6 +51,11 @@ bool Rename(const std::filesystem::path& old_path, // Returns true on success. bool CreateDirectories(const std::filesystem::path& path); +// Creates a hard link to target at link_path. +// Returns true on success. +bool CreateHardLink(const std::filesystem::path& target, + const std::filesystem::path& link_path); + } // namespace nearby::sharing #endif // THIRD_PARTY_NEARBY_INTERNAL_BASE_FILES_H_ diff --git a/internal/base/files_test.cc b/internal/base/files_test.cc new file mode 100644 index 00000000..7a8e1530 --- /dev/null +++ b/internal/base/files_test.cc @@ -0,0 +1,48 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "internal/base/files.h" + +#include +#include // NOLINT +#include +#include +#include + +#include "gtest/gtest.h" + +namespace nearby::sharing { +namespace { + +TEST(FilesTest, CreateHardLinkSuccess) { + std::filesystem::path temp_dir = testing::TempDir(); + std::filesystem::path target = temp_dir / "target"; + RemoveFile(target); + std::ofstream ofstream(target, std::ios::app); + ASSERT_EQ(ofstream.rdstate(), std::ios_base::goodbit); + ofstream << "Hello world"; + ofstream.flush(); + std::optional size = GetFileSize(target); + ASSERT_TRUE(size.has_value()); + EXPECT_EQ(size.value(), 11); + std::filesystem::path link_path = temp_dir / "link_path"; + EXPECT_TRUE(CreateHardLink(target, link_path)); + EXPECT_TRUE(FileExists(link_path)); + EXPECT_EQ(GetFileSize(link_path), 11); + RemoveFile(link_path); + RemoveFile(target); +} + +} // namespace +} // namespace nearby::sharing