从沙盒应用程序更新系统文件

Update a system file from sandboxed application

我正在开发一个更新文件 /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist 的应用程序。此文件归 root 所有。我目前正在使用 /usr/libexec/authopen 工具更新它,运行 使用 NSTaskNSPipe 更新该工具。这是代码:

func saveAs(fname:String) {
    // Create a dictory to write back out, and replace the knownNetworks and the preferredOrder
    let d = NSMutableDictionary(dictionary:airport_preferences)
    d[KnownNetworks] = networks
    d[PreferredOrder] = preferred_order

    let tempFileName = NSTemporaryDirectory() + "/preferences.new"
    d.writeToFile(tempFileName,atomically:true)

    let data =  NSData(contentsOfFile: tempFileName)
    let task = NSTask()
    task.launchPath = "/usr/libexec/authopen"
    task.arguments = ["-c","-w",fname]
    let pipe = NSPipe()
    task.standardInput = pipe
    task.launch()
    let old_signal = signal(SIGPIPE,SIG_IGN)
    pipe.fileHandleForWriting.writeData(data!)
    pipe.fileHandleForWriting.closeFile()
    task.waitUntilExit()
    signal(SIGPIPE,old_signal)
    do {
        try NSFileManager.defaultManager().removeItemAtPath(tempFileName)
    } catch let error as NSError {
        print(error.description)
    }
}

要移动到 App Sandbox,我可以让用户 select 在浏览器中打开文件。这并不难,因为我可以预先定位 NSOpenPanelMac App Sandboxing- Updating files outside the sandbox 描述了如何更新沙箱外的文件,但不是 root 拥有的文件。

当我 运行 我的代码在沙箱下时,我得到这个错误:

tempfile: /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist length: 339710
arguments: Optional(["-c", "-w", "/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist"])
2015-09-26 11:46:07.699 WiFi Editor[94175:2887221] An uncaught exception was raised
2015-09-26 11:46:07.700 WiFi Editor[94175:2887221] Couldn't posix_spawn: error 1
2015-09-26 11:46:07.703 WiFi Editor[94175:2887221] (

(该代码中仍然包含我的调试打印语句。)

那么如何从沙箱更新系统文件呢?

恐怕对于沙盒应用来说,这是根本不可能的事情。不禁止用户将文本粘贴到终端 window。