...
This creates issues when trying to write portable code, or when trying to implement alternative behavior.
If the file named by new exists at the time of the call to rename(), the result is implementation-defined.
- What to do in an application targeted at only POSIX or only Windows if the "other" behavior is desired (i.e. non-clobbering on POSIX, clobbering on Windows). For non-clobbering on POSIX you use access() and only call rename() if the file does not exist. For clobbering on Windows you call remove() before rename(). In both cases there is a TOCTOU so the part about needing a secure directory applies.
New File Removed
New File Removed
Occasionally, a programmer might want to implements a behavior other than the implementation-defined behavior for rename()
on that platform. If the desired behavior is to ensure that any file referenced by new
is removed, Windows programmers must write additional code.
Non-Compliant Code Example (Windows)
...
Code Block | ||
---|---|---|
| ||
char const *old = /* ... */; char const *new = /* ... */; if (rename(old, new) != 0) { /* Handle error condition */ } |
New File Not Removed
If the desired behavior is to ensure that any file referenced by new
is not removed, POSIX programmers must write additional code.
Non-Compliant Code Example (POSIX)
...