A modern Swift interface for copyfile. The main advantage over NSFileManager is that this supports APFS sparse file copying (even across different mount points) and also provides a callback for progress indication. You can also use other features of "copyfile" and "fcopyfile" without having to write your own C wrapper calls.
try await CopyManager.default.copyItem(at: srcURL, to: dstURL, flags: [.all, .recursive, .clone, .dataSparse])
let startTime = Date()
for try await progress in CopyManager.default.copyItemProgress(at: srcURL, to: dstURL, flags: [.all, .recursive, .clone, .dataSparse]) {
let elapsed = Date().timeIntervalSince(startTime)
let speed = Double(progress.bytesCopied) / elapsed / 1048576
print("\(speed) MB/s (copied \(progress.bytesCopied) bytes)")
}
This project is licensed under the Apache 2.0 license. It includes code licensed under Apple Public Source License.