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.
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.