Skip to content

Commit 3afd7da

Browse files
authored
Use O_TMPFILE on Linux (#186)
To avoid race condition, we can first try to open the file using `O_TMPFILE` on Linux systems
1 parent 0def07f commit 3afd7da

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/create.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,16 @@ int create_file() {
204204
int create_file(std::error_code& ec) {
205205
fs::path::string_type path = make_pattern("");
206206

207-
int handle = mkstemp(path.data());
207+
int handle;
208+
#ifdef __linux__
209+
handle = open(path.c_str(), O_RDWR | O_TMPFILE, S_IRUSR | S_IWUSR);
210+
if (handle >= 0) {
211+
ec.clear();
212+
return handle;
213+
}
214+
#endif
215+
216+
handle = mkstemp(path.data());
208217
if (handle == -1) {
209218
ec = std::error_code(errno, std::system_category());
210219
return -1;

0 commit comments

Comments
 (0)