[PATCH] D60802: Implement sys::fs::copy_file using the macOS copyfile(3) API to support APFS clones

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 16:59:35 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL358628: Implement sys::fs::copy_file using the macOS copyfile(3) API (authored by adrian, committed by ).
Herald added a subscriber: kristina.

Changed prior to commit:
  https://reviews.llvm.org/D60802?vs=195479&id=195653#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60802/new/

https://reviews.llvm.org/D60802

Files:
  llvm/trunk/lib/Support/Path.cpp
  llvm/trunk/lib/Support/Unix/Path.inc


Index: llvm/trunk/lib/Support/Unix/Path.inc
===================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc
+++ llvm/trunk/lib/Support/Unix/Path.inc
@@ -37,6 +37,7 @@
 #ifdef __APPLE__
 #include <mach-o/dyld.h>
 #include <sys/attr.h>
+#include <copyfile.h>
 #elif defined(__DragonFly__)
 #include <sys/mount.h>
 #endif
@@ -1113,5 +1114,55 @@
 
 } // 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;
+  bool IsSymlink;
+  if (std::error_code Error = is_symlink_file(From, IsSymlink))
+    return Error;
+
+  if (!IsSymlink)
+    if (__builtin_available(macos 10.12, *))
+      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) {
+  uint32_t Flag = COPYFILE_DATA;
+  bool IsSymlink;
+  if (std::error_code Error = is_symlink_file(From, IsSymlink))
+    return Error;
+
+  int ReadFD;
+  if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
+    return EC;
+
+  if (!IsSymlink)
+    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
Index: llvm/trunk/lib/Support/Path.cpp
===================================================================
--- llvm/trunk/lib/Support/Path.cpp
+++ llvm/trunk/lib/Support/Path.cpp
@@ -935,6 +935,7 @@
   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 @@
 
   return EC;
 }
+#endif
 
 ErrorOr<MD5::MD5Result> md5_contents(int FD) {
   MD5 Hash;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60802.195653.patch
Type: text/x-patch
Size: 2411 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190417/817f1b4e/attachment.bin>


More information about the llvm-commits mailing list