Check if path under app-scoped bookmark is writable inside sandboxed app

无人久伴 提交于 2019-12-07 05:19:02

问题


I have an OS X app, which stores an app-scoped bookmark to persist access to certain directories. I am able to write to those directories without any problems, but there is a part in my code where I want to make an extra check to confirm that the path is writable and it fails.

var fileManager: NSFileManager = NSFileManager.defaultManager()
var baseUrl: NSURL = try! NSURL(byResolvingBookmarkData: data, …)
var fileUrl: NSURL = url.URLByAppendingPathComponent("foo.txt")

// Changing this order has no effect, I make only the first start access call,
// the second one is to demonstrate that it's not working.

baseUrl.startAccessingSecurityScopedResource() // true
fileUrl.startAccessingSecurityScopedResource() // false

// File manager confirms that base path is writable, but anything inside
// it – not. This worked perfectly fine before sandboxing the app.

fileManager.isWritableFileAtPath(baseUrl.path!) // true
fileManager.isWritableFileAtPath(fileUrl.path!) // false

// Writing file to the `fileUrl` works anyway, even though
// file manager thinks it's not writable.

writeMyFile(toUrl: fileUrl)

// Here's another interesting observation, is writable starts working
// once the files get created.

fileManager.isWritableFileAtPath(fileUrl.path!) // false
fileManager.createFileAtPath(fileUrl.path!, contents: nil, attributes: nil)
fileManager.isWritableFileAtPath(fileUrl.path!) // true

Am I doing something wrong or is there a problem with file manager inside a sandbox? Is there a decent way to check if subpath is writable, i.e., without creating files?

来源:https://stackoverflow.com/questions/35307603/check-if-path-under-app-scoped-bookmark-is-writable-inside-sandboxed-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!