views:

30

answers:

1

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?

A: 

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

fbrereto