Re: File Permissions - NSFileManager Swift
Re: File Permissions - NSFileManager Swift
- Subject: Re: File Permissions - NSFileManager Swift
- From: Arved von Brasch <email@hidden>
- Date: Tue, 30 Jun 2015 23:26:42 +1000
> On 30 Jun 2015, at 02:29, Jens Alfke <email@hidden> wrote:
>
> It always helps people answer questions if you post the exact error you get.
>
> Generally when there are mysteriously un-deletable files, it has something to do with filesystem extended attributes like ‘immutable’. You can display these by using “ls -l@“.
>
> —Jens
Thank you for replying. I was reporting the error I got back from the NSError, which doesn’t give much detail. Here’s the result of ls -l@:
drwxrwxrwx+ 2 arved staff 68 30 Jun 15:13 Test.app
From the Get Info window, there appears to be custom Access Control that NSFileManager.setAttributes() doesn’t alter. I don’t see anything else in the list of attributes that would alter the custom access. So, how do I change the permissions on the application (or any generic file) to have permission to delete it?
Here’s a test application that is barebones what I’m trying to do:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let store: NSURL
var item: NSURL?
override init() {
store = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DesktopDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)[0] as! NSURL
super.init()
}
@IBAction func addAppPackage(sender: AnyObject) {
let op = NSOpenPanel()
op.treatsFilePackagesAsDirectories = false
op.allowsMultipleSelection = false
op.canChooseDirectories = false
op.canChooseFiles = true
op.canCreateDirectories = false
op.resolvesAliases = true
op.allowedFileTypes = ["public.executable"]
op.prompt = "Add Application"
op.beginSheetModalForWindow(NSApp.mainWindow!!, completionHandler: { returnCode in
if returnCode == NSModalResponseOK {
op.orderOut(sender)
var error: NSError? = nil
for pathURL in op.URLs {
self.item = self.store.URLByAppendingPathComponent(pathURL.lastPathComponent)
NSFileManager.defaultManager().copyItemAtURL(pathURL as! NSURL, toURL: self.item!, error: &error)
}
if error != nil {
NSApp.presentError(error!, modalForWindow: NSApp.mainWindow!!, delegate: nil, didPresentSelector: nil, contextInfo: nil)
}
}
})
}
@IBAction func removeAppPackage(sender: AnyObject) {
let alert = NSAlert()
alert.messageText = "Delete Application"
alert.informativeText = "Are you sure?"
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Delete")
alert.alertStyle = NSAlertStyle.CriticalAlertStyle
alert.beginSheetModalForWindow(NSApp.mainWindow!!, completionHandler: { returnCode in
if returnCode == NSAlertSecondButtonReturn {
var error: NSError? = nil
NSFileManager.defaultManager().setAttributes([NSFilePosixPermissions: 0o777], ofItemAtPath: self.item!.path!, error: &error)
NSFileManager.defaultManager().removeItemAtPath(self.item!.path!, error: &error)
if error != nil {
alert.window.orderOut(sender)
NSApp.presentError(error!, modalForWindow: NSApp.mainWindow!!, delegate: nil, didPresentSelector: nil, contextInfo: nil) // Error - Don’t have permission to remove file
}
}
})
}
}
_______________________________________________
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