ysf / anewer

anewer appends lines from stdin to a file if they don't already exist in the file. This is a rust version of https://github.com/tomnomnom/anew
https://github.com/ysf/anewer
GNU General Public License v3.0
57 stars 4 forks source link

Implicitly check for file existence #8

Closed stoeckmann closed 1 year ago

stoeckmann commented 2 years ago

Checking if a file exists by using its pathname and later on opening the file by using its pathname again leads to a TOCTOU race condition.

Just try to open the file. If it fails because the file does not exist then we move on without error output. If it exists but cannot be open, e.g. because read permissions are missing, then error out. Otherwise there is no further need to check for availability because it has been already opened.

The file could also be opened with read/write permission so it does not have to be opened writable again. But this should be a different commit, if it is even desired behavior.