I have a program that accepts two file names as arguments: it reads the first file in order to create the second file. How can I ensure that the program won’t overwrite the first file?
Restrictions:
- The method must keep working when the file system supports (soft or hard) links
- File permissions are fixed and it is only required that the first file is readable and the second file writeable
- It should preferably be platform-neutral (although Linux is the primary target)
On linux, open both files, and use
fstatto check ifst_ino(edit:) andst_devare the same.openwill follow symbolic links. Don’t usestatdirectly, to prevent race conditions.