Learning Swift on macOS, struggling with acl_free
It’s like you went out of your way to choose one of weirdest dark’n’dusty corners of the ecosystem. Why couldn’t you just create a SwiftUI app like everyone else (-:
Anyway, the reason you’re crashing is that your pointers don’t line up. It’s especially challenging because of the weird way that the ACL API is constructed in C. And your attempts to fix that have run across a common pitfall: Ampersand (&
) doesn’t do what you think it does. See The Peril of the Ampersand for the full backstory.
Here’s some code that works:
import Foundation
func main() throws {
let u = URL(fileURLWithPath: "/Users/quinn")
let r = try u.resourceValues(forKeys: [.fileSecurityKey])
let s = r.fileSecurity!
var aclQ: acl_t? = nil
let success = CFFileSecurityCopyAccessControlList(s, &aclQ)
guard success, let acl = aclQ else { fatalError() }
if let c = acl_to_text(acl, nil) {
print(String(cString: c))
acl_free(c)
// prints:
// !#acl 1
// group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF0000000C:everyone:12:deny:delete
}
acl_free(UnsafeMutableRawPointer(acl))
}
try main()
There are a number of subtleties here:
-
let acl = aclQ
— I’m doing this so that acl
is non-optional throughout the rest of the function. That just makes everything easier.
-
acl_free(UnsafeMutableRawPointer(acl))
— In C, acl_free
is ‘generic’: It can free a variety of different data structures returned by the ACL API. In Swift that means it gets imported as taking an UnsafeMutableRawPointer
. However, the type of acl
is acl_t
[1]. So, you have to apply a manual conversion.
-
acl_free(c)
— Note how you don’t need the conversion here. That’s because c
is of type UnsafeMutablePointer<CChar>
, and Swift has a built-in conversion from that to UnsafeMutableRawPointer
.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] This is actually a type alias for OpaquePointer
, which is the source of ongoing grief in the Swift/C interop story. There have been various efforts to fix this but none of them have stuck. Indeed, it just came up again on Swift Forums.