]> git.ipfire.org Git - thirdparty/ccache.git/commit
Improve TemporaryFile implementation for Windows (#736)
authorNicholas Hutchinson <nshutchinson@gmail.com>
Mon, 28 Dec 2020 14:22:40 +0000 (14:22 +0000)
committerGitHub <noreply@github.com>
Mon, 28 Dec 2020 14:22:40 +0000 (15:22 +0100)
commiteead7e9d63992684ce7290b09c05410485323096
treeddf44ca283f8732a17261f08169f042462603492
parentda54bff4e2231a9b4619cb3299edda769fa3b2ba
Improve TemporaryFile implementation for Windows (#736)

On Windows, multiple ccache process could race each other to create,
rename and delete temporary files, because they would attempt to
generate the same sequence of temporary file names
(`tmp.cpp_stdout.iG2Kb7`, `tmp.cpp_stdout.P1kAlM`,
`tmp.cpp_stdout.FzP5tM`, ...).

This is because ccache used mingw-w64's [implementation of mkstemp][1],
which uses `rand()` to generate temporary file names, and ccache was
never seeding the thread-local PRNG used by `rand()`.

Replace ccache's use of `mkstemp()` on Windows with an implementation
based on OpenBSD. This allows us to sidestep mingw-w64's problematic
implementation, and allows us to build using MSVC again. (MSVC's C
standard library does not provide `mkstemp()`.)

Example errors:

- Some ccache process is in the process of deleting a temporary file:

      ccache: error: Failed to create temporary file for C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.FzP5tM: Access is denied.

- Some ccache process has destination file open, so it can't be overwritten:

      ccache: error: failed to rename C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.iG2Kb7 to C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.iG2Kb7.ii: Access is denied.

- Source file has been deleted by some other ccache process:

      ccache: error: failed to rename C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.P1kAlM to C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.P1kAlM.ii: The system cannot find the file specified.

[1]: https://github.com/mirror/mingw-w64/blob/v8.0.0/mingw-w64-crt/misc/mkstemp.c
LICENSE.adoc
src/TemporaryFile.cpp
src/third_party/CMakeLists.txt
src/third_party/win32/mktemp.c [new file with mode: 0644]
src/third_party/win32/mktemp.h [new file with mode: 0644]
unittest/CMakeLists.txt
unittest/test_bsdmkstemp.cpp [new file with mode: 0644]