Re: Security scoped bookmarks example?
Re: Security scoped bookmarks example?
- Subject: Re: Security scoped bookmarks example?
- From: Gabriel Zachmann via Cocoa-dev <email@hidden>
- Date: Wed, 4 Mar 2020 15:12:09 +0100
I am still having problems in my screensaver to access files that are stored on
an external disk.
The disk is just a simple hard disk connected to my Macbook via USB.
(It is an important use case, since a lot of people like to keep there huge
image collections on external hard disks.)
I can access the files just fine in Finder.
My screensaver can also access them during the first run, when the users points
it to the directory (i.e., while it is running in preview mode in System
Preferences).
However, next time it gets invoked it cannot access them any more, although I
am using security-scoped bookmarks (SSB).
Details follow. Code excerpts below.
The first time the screensaver runs, the user points it to a specific directory
via System Preferences / Screen Saver Options.
I get the directory from an NSOpenPanel, and store this as a SSB in the user
preferences.
Also, I create a list of all image files in the directory, and sub-directories,
using Spotlight.
I store that as well in the user preferences.
In the next invocation (where it runs as a plugin of legacyScreenSaver),
I get the SSB, resolve it, load the list of images files (basically a list of
paths),
and try to display them.
Problem is that loading the image fails.
Here is the relevant code excerpt from the function -animateOneFrame:
// load new image from disk
NSURL * url = [NSURL fileURLWithPath: [self absolutePathFor: filename_]
isDirectory: NO];
// filename_ is one of the paths from the list of images under the user
selected directory
// at this point, url is a proper URL, I have checked
[url startAccessingSecurityScopedResource];
CGImageSourceRef sourceRef = CGImageSourceCreateWithURL( (CFURLRef) url,
NULL );
// sourceRef is not NULL, I checked
CGImageSourceStatus imgstatus = CGImageSourceGetStatus( sourceRef );
if ( imgstatus != kCGImageStatusComplete )
// this happens with each and every file on an external disk!
.. log imgstatus ..
[url stopAccessingSecurityScopedResource];
return;
}
Loading the image with NSImage like this:
[url startAccessingSecurityScopedResource];
NSImage * image = [[NSImage alloc] initWithContentsOfURL: url];
also fails, I always get NULL.
I also tried this (thanks, Steve!):
NSError * error;
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingFromURL: url
error: & error];
if ( ! fileHandle )
{
[self logMessage: [NSString stringWithFormat: @"Complete path: %@ ,
%@", [url path], error]
asError: YES];
[url stopAccessingSecurityScopedResource];
return;
}
Interestingly, I get this error message:
Complete path: /Volumes/cgvr/test2/backs_detail.jpg , Error
Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file
“backs_detail.jpg” in the folder “test2”."
UserInfo={NSFilePath=/Volumes/cgvr/test2/backs_detail.jpg,
NSUnderlyingError=0x600002c56c70 {Error Domain=NSPOSIXErrorDomain Code=1
"Operation not permitted"}}
It talks about "saving" even though I used fileHandleForReadingFromURL :-(
I create the SSB like this:
directoryBookmark_ = [dir bookmarkDataWithOptions:
NSURLBookmarkCreationWithSecurityScope |
NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
includingResourceValuesForKeys: nil
relativeToURL: nil
error: &systemError];
I tried it also *without* the
NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess,
but there was no difference in the error message.
legacyScreenSaver (the screen saver engine invoking my screen saver) has this
entitlement:
<key>com.apple.security.temporary-exception.files.absolute-path.read-only</key>
<array>
<string>/</string>
</array>
It also has the entitlement com.apple.security.files.user-selected.read-only .
Is the problem that the files I am trying to read are outside / ?
But I thought, I can read all files in folders for which I am getting a
bookmark via openPanel? (no matter which directory they are ..)
If you want, you can find the complete source code here:
https://owncloud.informatik.uni-bremen.de/index.php/s/H7wHqwdgwYwqwy9
<https://owncloud.informatik.uni-bremen.de/index.php/s/H7wHqwdgwYwqwy9>
I should mention that everything works fine when the folder of images is in my
home
(which is, of course, on my local disk).
I will appreciate all kinds of hints and pointers very much.
Confused, Gabriel.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden