调用 FSDeleteObject 时的竞争条件

发布于 2024-08-21 02:49:56 字数 465 浏览 4 评论 0原文

我已经实现了一个“安全保存”操作,如下所示:

  1. 将一些数据保存到临时文件 A
  2. A 的内容复制到最终目标 B
  3. 删除 A

我在第 3 步遇到了竞争条件,当尝试使用 删除文件时,Mac OS X 偶尔会返回错误 -47 (fBsyErr) FSDeleteObject。我完全有信心我是唯一修改此文件的人,并怀疑操作系统在我尝试删除该文件时正在执行某些操作(例如后台缓存任务),从而导致错误。

这是一个间歇性问题:通常 FSDeleteObject 调用工作得很好。在我收到错误代码的情况下,当操作系统完成使用该文件时,我希望“在稍后的时间点”安全地删除该文件。

尝试删除这个麻烦的临时文件时最好采取什么措施?

I have implemented a "safe save" operation that goes something like this:

  1. Save some data to temporary file A
  2. Copy contents of A to final destination B
  3. Delete A

I have a race condition at step 3 where Mac OS X will occasionally come back with error -47 (fBsyErr) when trying to delete the file using FSDeleteObject. I am completely confident I am the only one modifying this file and suspect the OS is doing something (e.g., background caching tasks) at the time I try to delete the file, resulting in the error.

This is an intermittent issue: normally the FSDeleteObject call works just fine. In those cases where I get the error code back I'd like to safely delete the file "at a later point in time" when the OS is finished playing with it.

What would be the best course of action to take in trying to delete this troublesome temporary file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

森罗 2024-08-28 02:49:56

这是正在发生的事情:

  1. FSDeleteObject 暂时失败并出现 fBsyErr 的最常见原因是 Spotlight 正在为文件建立索引。如果您修改文件,关闭它,然后立即尝试使用 FSDeleteObject 删除它,则 Spotlight 索引器很可能会打开该文件,并且您会收到 fBsyErr .
  2. 某些第三方防病毒扫描程序也可能引发此问题。当您关闭修改的文件时,防病毒扫描程序会立即开始检查是否有病毒。如果在您尝试删除文件时它仍在检查,FSDeleteObject 将失败并显示 fBsyErr

每个问题都有一系列的解决方法,对两者来说最好的方法是使用 unlink

Here's what's happening:

  1. The most common cause of FSDeleteObject temporarily failing with fBsyErr is that Spotlight is in the process of indexing the file. If you modify a file, close it, and then immediately try to delete it using FSDeleteObject, it's quite possible that the Spotlight indexer will have it open and you'll get fBsyErr.
  2. Some third party anti-virus scanners can also trigger this problem. When you close a modified file, the anti-virus scanner immediately starts to check it for viruses. If it's still checking when you try to delete the file, FSDeleteObject will fail with fBsyErr.

Each problem has a series of workarounds, the best for both is to use unlink

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