Does mkstemp open files only if they don't already exist?
lac at openend.se
Tue Sep 1 19:09:10 CEST 2015
In a message of Wed, 02 Sep 2015 00:57:45 +1000, Chris Angelico writes:
>On Wed, Sep 2, 2015 at 12:45 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>> I assume the answer is "Yes", but is it safe to expect that
>> tempfile.mkstemp() will only create a file that doesn't already exist? I
>> presume that there's no chance of it over-writing an existing file (say,
>> due to a race-condition).
>It depends on OS support, but with that, yes, it is guaranteed to be
>safe; the file is opened with an exclusivity flag. Check your system's
>man pages for details, or here:
>O_EXCL|O_CREATE makes an "atomic file creation" operation which will
>fail if another process is doing the same thing. I'm not sure how
>mkstemp() handles that failure, but my guess/expectation is that it
>would pick a different file name and try again.
I remember discussion on the mercurial mailing list about
somebody who had a problem with this in conjunction with a
virus scanner that really wanted to get in and do things
to the poor temp files.
for a very, very long thread about it.
Apparantly, you can get windows to complain that you cannot create a
file because it already exists, and it doesn't go back and try this
again for you.
But at the time I found the discussion puzzling, as my thought
was 'why are these people using mkstemp directly, instead of
tempfile.NamedTemporaryFile which seems to be what they want.
But I found this thread looking for a different problem with a
mercurial repository that we had that was corrupted, a year or so
after the thread was written, so I didn't want to go back and ask them
about it _then_.
Then in the general excitement --"Your raid system is buggy! It is
inserting sludge in your files!" I forgot about this puzzlement.
More information about the Python-list