1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-22 08:51:08 +01:00

Merge pull request #14832 from NixOS/o-tmpfile-fallback

libutil: Gracefully fall back from unsupported O_TMPFILE
This commit is contained in:
Sergei Zimmerman 2025-12-18 20:39:56 +00:00 committed by GitHub
commit 27006cc8a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -553,17 +553,27 @@ AutoCloseFD createAnonymousTempFile()
{ {
AutoCloseFD fd; AutoCloseFD fd;
#ifdef O_TMPFILE #ifdef O_TMPFILE
fd = ::open(defaultTempDir().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR, S_IWUSR | S_IRUSR); static std::atomic_flag tmpfileUnsupported{};
if (!fd) if (!tmpfileUnsupported.test()) /* Try with O_TMPFILE first. */ {
throw SysError("creating anonymous temporary file"); /* Use O_EXCL, because the file is never supposed to be linked into filesystem. */
#else fd = ::open(defaultTempDir().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR | O_EXCL, S_IWUSR | S_IRUSR);
if (!fd) {
/* Not supported by the filesystem or the kernel. */
if (errno == EOPNOTSUPP || errno == EISDIR)
tmpfileUnsupported.test_and_set(); /* Set flag and fall through to createTempFile. */
else
throw SysError("creating anonymous temporary file");
} else {
return fd; /* Successfully created. */
}
}
#endif
auto [fd2, path] = createTempFile("nix-anonymous"); auto [fd2, path] = createTempFile("nix-anonymous");
if (!fd2) if (!fd2)
throw SysError("creating temporary file '%s'", path); throw SysError("creating temporary file '%s'", path);
fd = std::move(fd2); fd = std::move(fd2);
# ifndef _WIN32 #ifndef _WIN32
unlink(requireCString(path)); /* We only care about the file descriptor. */ unlink(requireCString(path)); /* We only care about the file descriptor. */
# endif
#endif #endif
return fd; return fd;
} }