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
source
share