From 117b13d099d7b6ca8e5824a39be28d64a7f995d7 Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Tue, 8 Oct 2024 11:43:29 -0700 Subject: [PATCH] introduce copy file utility PiperOrigin-RevId: 683707504 --- internal/base/files.cc | 10 ++++++++++ internal/base/files.h | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/internal/base/files.cc b/internal/base/files.cc index c3e3b12a..b0302848 100644 --- a/internal/base/files.cc +++ b/internal/base/files.cc @@ -105,4 +105,14 @@ bool CreateHardLink(const std::filesystem::path& target, return true; } +bool CopyFileSafely(const std::filesystem::path& old_path, + const std::filesystem::path& new_path) { + std::error_code error_code; + std::filesystem::copy(old_path, new_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 501e8752..40b1a976 100644 --- a/internal/base/files.h +++ b/internal/base/files.h @@ -56,6 +56,11 @@ bool CreateDirectories(const std::filesystem::path& path); bool CreateHardLink(const std::filesystem::path& target, const std::filesystem::path& link_path); +// Copies the file at old_path to new_path. +// Returns true on success. +bool CopyFileSafely(const std::filesystem::path& old_path, + const std::filesystem::path& new_path); + } // namespace nearby::sharing #endif // THIRD_PARTY_NEARBY_INTERNAL_BASE_FILES_H_