Race condition when calling FSDeleteObject
I have implemented a "safe save" operation that goes something like this:
- Save some data to temporary file
A
- Copy contents of
A
to final destinationB
- 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开发者_StackOverflow社区 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?
Here's what's happening:
- The most common cause of
FSDeleteObject
temporarily failing withfBsyErr
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 usingFSDeleteObject
, it's quite possible that the Spotlight indexer will have it open and you'll getfBsyErr
.- 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 withfBsyErr
.
Each problem has a series of workarounds, the best for both is to use unlink
精彩评论