Operations with transactional files in OSX

I am trying to do the following:

  • Read file attributes
  • If the attributes meet a specific condition, delete the file.

Now I use NSFileManagerto execute attributesOfItemAtPath:error:, and then removeItemAtPath:error:. I am worried that something will happen between two operations that are not valid for the initial check.

What is the best way to make these two operations atomic?

Edit

The answers still suggest locking files that I tried to learn about. The closest I could find was to set a flag NSFileImmutable. But it looks like any other program may appear, disable it and modify the file. Is there a better way to lock a file?

Edit 2

Someone asked to use a precedent. Let's say I'm trying to sync two folders. Any changes made to files in one folder are reflected in another, and vice versa. If I delete file 1 from folder A, I will also delete file 1 from folder B. But if file 1 in folder B changes before I delete it; then instead of deleting it, I want to synchronize it with folder A

+5
source share
4 answers

You can use forced file lock (kernel lock) to lock the file in question to prevent changes to the file when you work with it. I know that Linux and Solaris support mandatory file locking, but I don’t know if OS X / HFS + is suitable, and if so, how to use it. Hope this helps.

0

, ? , ? . , , .

Cocoa; googled , ; ..

0

, :

fileManager:shouldRemoveItemAtPath:

delete file shouldRemoveItemAtPath: ( YES) ( NO) .

0

It seems to me that you should just go and delete the files that match. It makes no sense to block if you are not worried that some other application will modify the file so that it cannot be deleted. I think about it; You have found a file that matches your deletion criteria. You want to delete it. Does it really matter if it changes in the meantime?

0
source

All Articles