Created
January 25, 2017 01:05
-
-
Save banjun/841e663a5680355b311eb4980bd51353 to your computer and use it in GitHub Desktop.
Is your device using Apple File System (APFS)?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the original code by apple and its license | |
// https://developer.apple.com/library/prerelease/content/samplecode/APFSCloning/Listings/clonefile_main_c.html#//apple_ref/doc/uid/TP40017341-clonefile_main_c-DontLinkElementID_5 | |
NSLog("%@", "\(ProcessInfo().operatingSystemVersion)") | |
/* Initailize state */ | |
let s = copyfile_state_alloc(); | |
let srcPath = NSHomeDirectory() + "/tmp/test.txt" | |
let copyPath = NSHomeDirectory() + "/tmp/test-copy.txt" | |
_ = try? FileManager.default.removeItem(atPath: srcPath) | |
_ = try? FileManager.default.removeItem(atPath: copyPath) | |
NSLog("--------------------------------\n") | |
NSLog("%@", "srcPath: \(try? String(contentsOf: URL(fileURLWithPath: srcPath)))") | |
NSLog("%@", "copyPath: \(try? String(contentsOf: URL(fileURLWithPath: copyPath)))") | |
try! "1234567890".write(to: URL(fileURLWithPath: srcPath), atomically: true, encoding: .utf8) | |
NSLog("--------------------------------\n") | |
NSLog("%@", "srcPath: \(try? String(contentsOf: URL(fileURLWithPath: srcPath)))") | |
NSLog("%@", "copyPath: \(try? String(contentsOf: URL(fileURLWithPath: copyPath)))") | |
/* Copy the file | |
(but clone if available) */ | |
srcPath.withCString { src in | |
copyPath.withCString { dst in | |
if (copyfile(src, dst, s, UInt32(COPYFILE_CLONE)) < 0) { | |
perror("Error in copyfile()"); | |
exit(1); | |
} | |
} | |
} | |
/* Was the file cloned ? */ | |
var was_cloned: Bool = false | |
if ((copyfile_state_get(s, UInt32(COPYFILE_STATE_WAS_CLONED), &was_cloned) == 0) && was_cloned) { | |
NSLog("File was cloned\n"); | |
} else { | |
NSLog("File was copied and not cloned\n"); | |
} | |
NSLog("--------------------------------\n") | |
NSLog("%@", "srcPath: \(try? String(contentsOf: URL(fileURLWithPath: srcPath)))") | |
NSLog("%@", "copyPath: \(try? String(contentsOf: URL(fileURLWithPath: copyPath)))") | |
/* Release the state variable */ | |
copyfile_state_free(s); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment