mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Create hard link for latest log files.
PiperOrigin-RevId: 681986911
This commit is contained in:
committed by
Copybara-Service
parent
fdd944e98d
commit
ab00092c6c
@@ -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",
|
||||
|
||||
+14
-10
@@ -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",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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_
|
||||
|
||||
@@ -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 <cstdint>
|
||||
#include <filesystem> // NOLINT
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <optional>
|
||||
|
||||
#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<uintmax_t> 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
|
||||
Reference in New Issue
Block a user