如何在 Mac OS X 上操作期间防止磁盘弹出?

发布于 2024-09-19 06:08:56 字数 532 浏览 7 评论 0原文

我有一个长时间运行的任务,该任务在已安装的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2024-09-26 06:08:57

您需要使用磁盘仲裁< /a> API,更具体地说是 DARegisterDiskUnmountApprovalCallback。

您可以通过 DADisk.h

当回调被调用时,您可以决定是否要阻止卸载。举一个人为的例子:

DADissenterRef myUnmountApprovalCallback(DADiskRef disk, void *context)
{
    DADissenterRef result = NULL; // NULL means approval
    if (stillWorking) {
        // This is released by the caller, according to the docs
        result = DADissenterCreate(kCFAllocatorDefault, kDAReturnBusy, CFSTR("<Your App> is busy writing to this device. Please cancel the operation first.");
    }
    return result;
}

正如评论中所指出的,这并不能阻止任何人直接拔掉插头,但它会给您显式卸载的通知。

You'll need to use the Disk Arbitration API, more specifically the DARegisterDiskUnmountApprovalCallback.

You can create a DADiskRef via the functions avaliable in DADisk.h

When the callback is called, you can then decide whether you want to block the unmount or not. For a contrived example:

DADissenterRef myUnmountApprovalCallback(DADiskRef disk, void *context)
{
    DADissenterRef result = NULL; // NULL means approval
    if (stillWorking) {
        // This is released by the caller, according to the docs
        result = DADissenterCreate(kCFAllocatorDefault, kDAReturnBusy, CFSTR("<Your App> is busy writing to this device. Please cancel the operation first.");
    }
    return result;
}

As noted in the comments, this doesn't prevent anyone from just pulling the plug, but it will give you notification of explicit unmounts.

三生池水覆流年 2024-09-26 06:08:57

您正在寻找磁盘仲裁(或 DiskArb)框架 API。

You are looking for the Disk Arbitration (or DiskArb) framework APIs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文