2 ms·
As far as I am aware on Linux there is no way to stat and unlink in one syscall. And a second stat will create a second race condition. If you don’t want the fi
by IgorPartola 5y ago
As far as I am aware on Linux there is no way to stat and unlink in one syscall. And a second stat will create a second race condition. If you don’t want the file to exist why are you questioning whether you have permission or not? Just unlink the file via rm, if possible.
- SkittyDog 5y agoI believe you are correct about the lack of an atomic stat/unlink operation. Depending on how you implement the two steps, your potential errors are different. `rm -f` handles some errors in a different way than the stat/rm approach. One can fail where the other would not. Any given unhandled error may or may not be a problem, depending on the nature of the error, how the rest of the script is structured, and what your requirements are. There's nothing wrong with the stat/rm approach--it may be the better way to go.
- tzs 5y ago> As far as I am aware on Linux there is no way to stat and unlink in one syscall. That just means you have to do a little more work. 1. Become root. 2. Fork until the process table is full. 3. Kill all other processes. After each kill, fork until the process table is again full. 4. When all processes other than the init process are yours, you can do the stat and unlink without worrying about race conditions since there is no one else to have a race with. (Assuming the file you want to stat and unlink isn't some file that your init process is interested in, and assuming it isn't on a network drive). The side effects of this are annoying to deal with though and are probably worse than whatever problems an unhandled race condition would cause. Another approach would be to create a new user, chown the directory containing the file to that user, become that user, chmod the directory to 0300, kill any process that has that file open, do your stat and unlink, chown and chmod the directory back to what they were, and delete the earlier created user.
- julian_sark 5y agoThat's trying WAY too hard. I just reboot in single user mode. (Or, if anyone wants to get pedantic and yes, this is ruining the punchline, but alas: shutdown -h now ; then reboot from the USB stick and mount the partition ; rm file.txt).