The right way to use start/stop AccessingSecurityScopedResource in swift-cpp interop

Hi, so I have this case where I would like the user to pick a folder where they want to create a file/folder using UIDocumentPicker/Browser and I make the file using open() in cpp and use its fd to read/write to the file. Now, the first thing is I have to call startAccessingSecurityScopedResource() on the directory url, then I make the file, get its fd(file descriptor) and I leave this makefile() function.

Every startAccessingSecurityScopedResource() needs to be matched with a stopstartAccessingSecurityScopedResource().

So my question is do I 'have' to call stopAccessingSecurityScopedResource() 'just before' calling close() on the fd.

Or is it fine to call it after I have made the fd i.e., at the end of the makefile() function?

In the tests I did it seems that once the fd is opened, even if stopAccessingSecurityScopedResource() is called on it(the directory), I can continue to read/write from the fd until I close() the fd?

Answered by DTS Engineer in 783370022

Permissions are resolved when you call open, so doing this should be fine:

start accessing
fd = open
stop accessing
work with fd

It is possible for access to the file to be revoked out from under a file descriptor, but it’s very rare. On macOS, for example, you’ll see this if the user force unmounts the volume on which the file resides.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Permissions are resolved when you call open, so doing this should be fine:

start accessing
fd = open
stop accessing
work with fd

It is possible for access to the file to be revoked out from under a file descriptor, but it’s very rare. On macOS, for example, you’ll see this if the user force unmounts the volume on which the file resides.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The right way to use start/stop AccessingSecurityScopedResource in swift-cpp interop
 
 
Q