Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit e8c5427

Browse files
committed
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
1 parent f3a0305 commit e8c5427

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/Support/Path.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
935935
return create_directory(P, IgnoreExisting, Perms);
936936
}
937937

938+
#ifndef __APPLE__
938939
static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
939940
const size_t BufSize = 4096;
940941
char *Buf = new char[BufSize];
@@ -988,6 +989,7 @@ std::error_code copy_file(const Twine &From, int ToFD) {
988989

989990
return EC;
990991
}
992+
#endif
991993

992994
ErrorOr<MD5::MD5Result> md5_contents(int FD) {
993995
MD5 Hash;

lib/Support/Unix/Path.inc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#ifdef __APPLE__
3838
#include <mach-o/dyld.h>
3939
#include <sys/attr.h>
40+
#include <copyfile.h>
4041
#elif defined(__DragonFly__)
4142
#include <sys/mount.h>
4243
#endif
@@ -1113,5 +1114,52 @@ void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) {
11131114

11141115
} // end namespace path
11151116

1117+
namespace fs {
1118+
1119+
#ifdef __APPLE__
1120+
/// This implementation tries to perform an APFS CoW clone of the file,
1121+
/// which can be much faster and uses less space.
1122+
std::error_code copy_file(const Twine &From, const Twine &To) {
1123+
uint32_t Flag = COPYFILE_DATA;
1124+
if (__builtin_available(macos 10.12, *)) {
1125+
bool IsSymlink;
1126+
if (std::error_code Error = is_symlink_file(From, IsSymlink))
1127+
return Error;
1128+
// COPYFILE_CLONE clones the symlink instead of following it
1129+
// and returns EEXISTS if the target file already exists.
1130+
if (!IsSymlink && !exists(To))
1131+
Flag = COPYFILE_CLONE;
1132+
}
1133+
1134+
int Status =
1135+
copyfile(From.str().c_str(), To.str().c_str(), /* State */ NULL, Flag);
1136+
1137+
if (Status == 0)
1138+
return std::error_code();
1139+
return std::error_code(errno, std::generic_category());
1140+
}
1141+
1142+
/// This implementation tries to perform an APFS CoW clone of the file,
1143+
/// which can be much faster and uses less space.
1144+
std::error_code copy_file(const Twine &From, int ToFD) {
1145+
int ReadFD;
1146+
if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
1147+
return EC;
1148+
1149+
uint32_t Flag = COPYFILE_DATA;
1150+
if (__builtin_available(macos 10.12, *))
1151+
Flag = COPYFILE_CLONE;
1152+
1153+
int Status = fcopyfile(ReadFD, ToFD, /*State*/ NULL, Flag);
1154+
1155+
close(ReadFD);
1156+
if (Status == 0)
1157+
return std::error_code();
1158+
return std::error_code(errno, std::generic_category());
1159+
}
1160+
#endif
1161+
1162+
} // end namespace fs
1163+
11161164
} // end namespace sys
11171165
} // end namespace llvm

0 commit comments

Comments
 (0)