我开发的Mac OS X 10.7沙盒应用程序,我想实现文件类似NSDocument
的方式节省:非就地保存在OS X沙盒
- 重写文件的新内容到一个临时文件
- 覆盖原来的文件,临时文件
我遇到的问题是,沙箱否认步骤2中我看到控制台下面一行:
sandboxd: XXXX deny file-write-create /Volumes/Home/sbooth/Test Files/Test
我已经打开此文件进行读取和写入,并且启用了文件系统读取/写入访问权限。我知道NSDocument没有特殊的权利,所以我试图弄清楚我错过了什么。
这里是我现在做的事情(应用程序的这一部分是在C++中,不客观的,C/C++):
FSRef tempFileFSRef;
if(noErr != FSPathMakeRef((const UInt8 *)tempFileName, &tempFileFSRef, NULL))
; // Handle it
CFURLRef destinationDirURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, mURL);
FSRef destinationDirFSRef;
if(!CFURLGetFSRef(destinationDirURL, &destinationDirFSRef))
; // Handle it
CFRelease(destinationDirURL), destinationDirURL = NULL;
CFStringRef destinationName = CFURLCopyLastPathComponent(mURL);
FSRef target;
OSStatus result = FSCopyObjectSync(&tempFileFSRef, &destinationDirFSRef, destinationName, &target, kFSFileOperationOverwrite | kFSFileOperationSkipSourcePermissionErrors);
if(noErr != result)
; // Handle it
的代码工作正常,如果我禁用沙盒。
编辑:附加信息是由费米要求。我使用C STDIO打开文件:
FILE *f = fopen(reinterpret_cast<const char *>(buf), "r");
,并使用fclose
创建临时文件之前关闭。
我的权利是:
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.assets.music.read-write</key>
<true/>
<key>com.apple.security.files.downloads.read-write</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
</dict>
另外值得一提的是,苹果在他们App Sandbox Design Guide,上面写着:
If you are managing documents using any technology other than the NSDocument class, you must convert to using this class. The NSDocument class automatically works with Powerbox. NSDocument also provides support for keeping documents within your sandbox if the user moves them using Finder.
除了在控制台sandboxd线,是'FSCopyObjectSync'返回一个错误? –
它确实会返回一个错误:-54 | permErr – sbooth
这也并不令人意外,但FSMoveObjectSync表现出相同的行为 – sbooth