首页 > 解决方案 > 如何在沙箱外将数据写入 /usr/libexec/cups/filter/

问题描述

我在沙箱中有一个应用程序(用于 App Store 部署),它需要将一个文件安装到 /usr/libexec/cups/filter/ 中,并将两个文件安装到 /private/etc/cups/ 中。此安装不会定期完成,而是从 UI 执行。在要求“全盘访问”或通过其他方式时这可能吗?

    var inFilePath = Bundle.main.url(forResource: "thnucups", withExtension: nil, subdirectory: "thnuclnt/x86_64-darwin")!

    let inFile: FileHandle? = try! FileHandle(forReadingFrom: inFilePath)

    let data = inFile?.readDataToEndOfFile()

    inFile?.closeFile()

    var destPath = "/usr/libexec/cups/filter/test";

    let outFileUrl = URL(fileURLWithPath: destPath)

    FileManager.default.createFile(atPath: destPath, contents: nil, attributes: nil)

    let outFile: FileHandle? = try! FileHandle(forWritingTo: outFileUrl)

    outFile?.write(data!)

    outFile?.closeFile()

放出文件:文件句柄?= 试试吧!FileHandle(forWritingTo: outFileUrl) 抛出以下异常:

2019-07-29 15:16:46.985094+0200 macOS 的 ezeep 连接器 [33450:488523] 致命错误:“尝试!” 表达式意外引发错误:错误域 = NSCocoaErrorDomain 代码 = 4“文件“测试”不存在。” UserInfo={NSFilePath=/usr/libexec/cups/filter/test, NSUnderlyingError=0x600000c5e610 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}: 文件 /Users/rwelz/Documents/Develop/PROJEKTE/ThinPrint /in GIT/ThinPrint/EZEEP/Xcode/ezeep macOS 连接器/ezeep macOS 连接器/AppDelegate.swift,第 146 行


现在有了:

    let dialog = NSOpenPanel()

    dialog.allowsMultipleSelection = false
    dialog.canCreateDirectories = false
    dialog.canChooseDirectories = true
    dialog.canChooseFiles = false

    dialog.begin
    { (result) -> Void in
        if result == .OK
        {
            let url = dialog.url

            do
            {
                try data?.write(to: outFileUrl)
            }
            catch
            {
                print("Error: Exception caught: " + error.localizedDescription)

            }
        }
    }

我不再收到异常,但由于 /usr/libexec/cups/filter 具有 drwxr-xr-x 根轮(不以 root 身份运行时没有写入访问权限)我得到“您无权保存文件夹过滤器中的文件“test”

我需要以 root 身份运行,这在沙盒中是不允许的。

还有其他想法吗?

关于罗伯特

标签: swiftmacosappstore-sandbox

解决方案


我决定构建一个 pkg 安装程序,它可以从我的沙盒应用程序中的菜单项保存到磁盘。应用程序第一次启动时也会提供保存对话框。然后用户必须打开 pkg 并以管理员身份进行身份验证才能开始安装。在 pkg 的 postflight 脚本中,它向应用程序发送一个苹果事件( osascript -e 'tell application PATH to start_action' 让应用程序知道它可以继续。

实际上,安装程序将一个虚拟 ppd 与 cups 过滤器一起安装到 /Library/Printers/PPDs/Contents/Resources/ 中,我的应用程序会查询 cups 以获取此打印机驱动程序(分别为 ppd)以确保 /usr/libexec/cups/filer /my_filter 已安装。

现在让我们看看苹果在审查我的应用程序时对该解决方案的看法;)


推荐阅读