[issue8828] Atomic function to rename a file

anatoly techtonik report at bugs.python.org
Fri Dec 23 20:29:06 CET 2011


anatoly techtonik <techtonik at gmail.com> added the comment:

2011/12/23 Charles-François Natali <report at bugs.python.org>

>
> Charles-François Natali <neologix at free.fr> added the comment:
>
> > I propose quite the opposite. rename() should not overwrite existing
> > files by default.
>
> 1. That's not what I understood from:
> > os.rename(overwrite=True) to produce consistent cross-platform
> > behavior.
>
> 2. The above argument on backward incompatible change applies in exactly
> the same way (just exchange Windows for POSIX).
>
> 3. As explained above, it can not be done reliably on POSIX (TOCTTOU race).

os.rename(overwrite=False) by default will do less harm than the opposite,
so I'd say it is a way to go even if it can not be reliably done on POSIX.
But I believe correct file system that supports transactions will make it
possible to do reliably on POSIX too.

Then I believe that having a small chance of overwriting file just created
at exactly the same moment on a POSIX is a small price to pay for function
that does safety check before rename on a platform that doesn't have such
functionality at all. If you need this functionality - you will still do
'if exists ... rename' and the TOCTTOU race will affect you. So, the only
exit is to place a little sign "in some extreme cases Linux suffers from
TOCTTOU problem when renaming files without overwriting, so keep that in
mind".

BUT let me remind you that this bug is about "Atomic rename" which should
rollback in case of errors.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8828>
_______________________________________


More information about the Python-bugs-list mailing list