Hello everyone,

I'm sandboxing an NSDocument based application for OS X. In addition to Apple's 
standard Open Recent menu, this app also provides the user a few ways to reopen 
commonly used documents. To make that work under sandboxing, I capture a 
security-scoped bookmark for each such document. Then later, when the users 
wants to reopen the file, I resolve that bookmark data back to a URL. The code 
is something like:

        NSData* bookmark = XXLoadDocumentBookmarkData();
        NSURLBookmarkResolutionOptions ops = 
NSURLBookmarkResolutionWithSecurityScope;
        NSURL* url = [NSURL URLByResolvingBookmarkData:bookmark options:ops 
relativeToURL:nil bookmarkDataIsStale:NULL error:NULL];
        
        [url startAccessingSecurityScopedResource];
        NSDocument* doc = [[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:url display:YES error:NULL];
        [url stopAccessingSecurityScopedResource];

Anyways, that code works just fine: the document is reopened with proper 
read/write access.

The problem is that after some period of time, the reopened documents become 
locked and saving fails. Both autosave and manual save operations fail with the 
error: "you don’t own the file and don't have permission to write to it". In 
the document's titlebar the suffix "— Locked" is shown, and Cocoa prevents me 
from unlocking the document using the popup options. The issue never occurs 
immediately, only after some successful saves and perhaps a few minutes have 
passed. I have discerned no pattern to when or what triggers the locking.

Does anyone have any idea what's going wrong? Why is sandbox access for these 
documents being cut off while they remain open?

I can somewhat workaround the problem by overriding -[NSDocument 
performSynchronousFileAccessUsingBlock:], wrapping super's implementation with 
-[NSURL startAccessingSecurityScopedResource] for the relevant URL. However, 
that feels like a hack and makes me very nervous that other things could be 
broken. Also, while that restores successful saving, it doesn't prevent "— 
Locked" from appearing in the document's titlebar.

Thank you for any ideas or help!

Best,
Martin Wierschin


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to