Created
May 1, 2019 06:33
-
-
Save dreness/08213395aef006720bd1fd9022d2a86e to your computer and use it in GitHub Desktop.
Show something mumble mumble APFS cloned files mumble?
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
import Cocoa | |
// https://cocoa-dev.apple.narkive.com/Ciy40e20/is-cloning-the-same-as-copying-in-apfs#post8 | |
func unclonedSize(of url: URL) throws -> off_t { | |
var list = attrlist(bitmapcount: UInt16(ATTR_BIT_MAP_COUNT), | |
reserved: 0, | |
commonattr: 0, | |
volattr: 0, | |
dirattr: 0, | |
fileattr: 0, | |
forkattr: attrgroup_t(ATTR_CMNEXT_PRIVATESIZE)) | |
let bufsize = 4 + MemoryLayout<off_t>.size | |
let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: bufsize) | |
defer { buf.deallocate() } | |
let err = url.withUnsafeFileSystemRepresentation { getattrlist($0, &list, buf, bufsize, UInt32(FSOPT_ATTR_CMN_EXTENDED)) } | |
if err != 0 { throw POSIXError.Code(rawValue: errno).map { POSIXError($0) } ?? CocoaError(.fileReadUnknown) } | |
let attrsize = buf.withMemoryRebound(to: UInt32.self, capacity: 1) { $0.pointee } | |
if attrsize < bufsize { throw CocoaError(.fileReadUnknown) } | |
var unclonedSize: off_t = 0 | |
_ = withUnsafeMutableBytes(of: &unclonedSize) { memcpy($0.baseAddress!, buf + 4, MemoryLayout<off_t>.size) } | |
return unclonedSize | |
} | |
do { | |
let regular = URL(fileURLWithPath: "/Users/andre/borkens/regular.dmg") | |
let clone = URL(fileURLWithPath: "/Users/andre/borkens/clone.dmg") | |
let cpRegular = URL(fileURLWithPath: "/Users/andre/borkens/cp-regular.dmg") | |
let cloneAppended = URL(fileURLWithPath: "/Users/andre/borkens/clone-appended.dmg") | |
print("Uncloned size of regular.dmg : \(try unclonedSize(of: regular))") | |
print("Uncloned size of clone.dmg : \(try unclonedSize(of: clone))") | |
print("Uncloned size of cpRegular.dmg : \(try unclonedSize(of: cpRegular))") | |
print("Uncloned size of cloneAppended.dmg: \(try unclonedSize(of: cloneAppended))") | |
} catch { | |
print("Error occurred: \(error.localizedDescription)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's try it. In Xcode, make a new macOS playground and use the above Swift code. The test files are described as follows:
echo
and>>
.The playground output is:
Because this is a gist, conclusions are left as an exercise for the reader. I'll even tell you where the answer is: in the man page for getattrlist(2).