From: Adrian Prantl Date: Thu, 18 Apr 2019 21:22:50 +0000 (+0000) Subject: Implement sys::fs::copy_file using the macOS copyfile(3) API X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e8c5427cd12d1c87e1a7af4bf0fa245f40ceb198;p=llvm Implement sys::fs::copy_file using the macOS copyfile(3) API to support APFS clones. This patch adds a Darwin-specific implementation of llvm::sys::fs::copy_file() that uses the macOS copyfile(3) API to support APFS copy-on-write clones, which should be faster and much more space efficient. https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/APFS_Guide/ToolsandAPIs/ToolsandAPIs.html Differential Revision: https://reviews.llvm.org/D60802 This reapplies 358628 with an additional bugfix handling the case where the destination file already exists. (Caught by the clang testsuite). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358716 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 902df74a68f..9466591c25e 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -935,6 +935,7 @@ std::error_code create_directories(const Twine &Path, bool IgnoreExisting, return create_directory(P, IgnoreExisting, Perms); } +#ifndef __APPLE__ static std::error_code copy_file_internal(int ReadFD, int WriteFD) { const size_t BufSize = 4096; char *Buf = new char[BufSize]; @@ -988,6 +989,7 @@ std::error_code copy_file(const Twine &From, int ToFD) { return EC; } +#endif ErrorOr md5_contents(int FD) { MD5 Hash; diff --git a/lib/Support/Unix/Path.inc b/lib/Support/Unix/Path.inc index 05ccc6cc5e4..fbe3ed11c59 100644 --- a/lib/Support/Unix/Path.inc +++ b/lib/Support/Unix/Path.inc @@ -37,6 +37,7 @@ #ifdef __APPLE__ #include #include +#include #elif defined(__DragonFly__) #include #endif @@ -1113,5 +1114,52 @@ void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl &Result) { } // end namespace path +namespace fs { + +#ifdef __APPLE__ +/// This implementation tries to perform an APFS CoW clone of the file, +/// which can be much faster and uses less space. +std::error_code copy_file(const Twine &From, const Twine &To) { + uint32_t Flag = COPYFILE_DATA; + if (__builtin_available(macos 10.12, *)) { + bool IsSymlink; + if (std::error_code Error = is_symlink_file(From, IsSymlink)) + return Error; + // COPYFILE_CLONE clones the symlink instead of following it + // and returns EEXISTS if the target file already exists. + if (!IsSymlink && !exists(To)) + Flag = COPYFILE_CLONE; + } + + int Status = + copyfile(From.str().c_str(), To.str().c_str(), /* State */ NULL, Flag); + + if (Status == 0) + return std::error_code(); + return std::error_code(errno, std::generic_category()); +} + +/// This implementation tries to perform an APFS CoW clone of the file, +/// which can be much faster and uses less space. +std::error_code copy_file(const Twine &From, int ToFD) { + int ReadFD; + if (std::error_code EC = openFileForRead(From, ReadFD, OF_None)) + return EC; + + uint32_t Flag = COPYFILE_DATA; + if (__builtin_available(macos 10.12, *)) + Flag = COPYFILE_CLONE; + + int Status = fcopyfile(ReadFD, ToFD, /*State*/ NULL, Flag); + + close(ReadFD); + if (Status == 0) + return std::error_code(); + return std::error_code(errno, std::generic_category()); +} +#endif + +} // end namespace fs + } // end namespace sys } // end namespace llvm