调用FSDeleteObject时的竞争条件 [英] Race condition when calling FSDeleteObject

查看:80
本文介绍了调用FSDeleteObject时的竞争条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了安全保存"操作,其操作如下:

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

  1. 将一些数据保存到临时文件A
  2. A的内容复制到最终目标B
  3. 删除A
  1. Save some data to temporary file A
  2. Copy contents of A to final destination B
  3. Delete A

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

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.

这是一个间歇性问题:通常FSDeleteObject调用可以正常工作.在我收到错误代码的情况下,我想在操作系统完成播放后稍后"安全删除该文件.

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?

推荐答案

这是正在发生的事情:

  1. FSDeleteObject暂时因fBsyErr失败的最常见原因是Spotlight正在对文件建立索引.如果修改文件,将其关闭,然后立即尝试使用FSDeleteObject删除它,则很有可能Spotlight索引器将其打开,您将得到fBsyErr.
  2. 某些第三方防病毒扫描程序也可以触发此问题.当您关闭修改后的文件时,防病毒扫描程序会立即开始检查是否存在病毒.如果仍在尝试删除文件时仍在检查,则FSDeleteObject将失败,并显示fBsyErr.
  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.

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

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

这篇关于调用FSDeleteObject时的竞争条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆