如何在 Mac OS X 上操作期间防止磁盘弹出?
我有一个长时间运行的任务,该任务在已安装的 USB 驱动器上执行一系列文件操作,并且我希望防止用户在发生这种情况时从 Finder(或其他地方)弹出驱动器。有一个“取消”按钮,可以随时结束任务。
我原以为在任务期间保持已安装卷上的文件句柄打开可以解决问题,但它没有起作用。
这是我尝试过的(已删除错误处理):
NSString *tempFilePath = @"/Volumes/myVolume/.myTempFile";
if ([[NSFileManager defaultManager] fileExistsAtPath:tempFilePath] == NO) {
[[NSFileManager defaultManager] createFileAtPath:tempFilePath contents:nil attributes:nil]
}
_tempFile = [NSFileHandle fileHandleForWritingAtPath:tempFilePath];
关于如何确保防止卷弹出,有什么想法吗?
I have a long-running task that performs a series of file operations on mounted USB drives and I want to prevent users from ejecting the drive from Finder (or elsewhere) while this happens. There is a Cancel button that allows the task to be ended at any time.
I had assumed that keeping a file handle open on the mounted volume for the duration of the task would do the trick, but it hasn't worked.
This is what I tried (error handling removed):
NSString *tempFilePath = @"/Volumes/myVolume/.myTempFile";
if ([[NSFileManager defaultManager] fileExistsAtPath:tempFilePath] == NO) {
[[NSFileManager defaultManager] createFileAtPath:tempFilePath contents:nil attributes:nil]
}
_tempFile = [NSFileHandle fileHandleForWritingAtPath:tempFilePath];
Any ideas about what I can do to ensure that the volume is prevented from ejecting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用磁盘仲裁< /a> API,更具体地说是 DARegisterDiskUnmountApprovalCallback。
您可以通过 DADisk.h
当回调被调用时,您可以决定是否要阻止卸载。举一个人为的例子:
正如评论中所指出的,这并不能阻止任何人直接拔掉插头,但它会给您显式卸载的通知。
You'll need to use the Disk Arbitration API, more specifically the DARegisterDiskUnmountApprovalCallback.
You can create a
DADiskRef
via the functions avaliable in DADisk.hWhen the callback is called, you can then decide whether you want to block the unmount or not. For a contrived example:
As noted in the comments, this doesn't prevent anyone from just pulling the plug, but it will give you notification of explicit unmounts.
您正在寻找磁盘仲裁(或 DiskArb)框架 API。
You are looking for the Disk Arbitration (or DiskArb) framework APIs.