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_