mirror of
https://github.com/NixOS/nix
synced 2025-07-05 04:01:47 +02:00
cherry-pick https://gerrit.lix.systems/c/lix/+/2100
Cherry-pick https://gerrit.lix.systems/c/lix/+/2100 This change fixes a potential concurrency failure when accessing random which is not thread safe. Co-authored-by: Lily Ballard <lily@ballards.net>
This commit is contained in:
parent
e22142e11a
commit
6aed9d877c
7 changed files with 35 additions and 37 deletions
|
@ -8,12 +8,12 @@
|
|||
#include "nix/util/util.hh"
|
||||
|
||||
#include <atomic>
|
||||
#include <random>
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <deque>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
|
||||
#include <fcntl.h>
|
||||
|
@ -27,10 +27,6 @@
|
|||
# include <io.h>
|
||||
#endif
|
||||
|
||||
#include "nix/util/strings-inline.hh"
|
||||
|
||||
#include "util-config-private.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
DirectoryIterator::DirectoryIterator(const std::filesystem::path& p) {
|
||||
|
@ -641,6 +637,13 @@ std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
|
|||
return {std::move(fd), tmpl};
|
||||
}
|
||||
|
||||
Path makeTempPath(const Path & root, const Path & suffix)
|
||||
{
|
||||
// start the counter at a random value to minimize issues with preexisting temp paths
|
||||
static std::atomic_uint_fast32_t counter(std::random_device{}());
|
||||
return fmt("%1%%2%-%3%-%4%", root, suffix, getpid(), counter.fetch_add(1, std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
void createSymlink(const Path & target, const Path & link)
|
||||
{
|
||||
try {
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
*/
|
||||
|
||||
#include "nix/util/types.hh"
|
||||
#include "nix/util/error.hh"
|
||||
#include "nix/util/logging.hh"
|
||||
#include "nix/util/file-descriptor.hh"
|
||||
#include "nix/util/file-path.hh"
|
||||
|
||||
|
@ -18,12 +16,8 @@
|
|||
#ifdef _WIN32
|
||||
# include <windef.h>
|
||||
#endif
|
||||
#include <signal.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <optional>
|
||||
|
||||
/**
|
||||
|
@ -335,6 +329,14 @@ Path defaultTempDir();
|
|||
*/
|
||||
bool isExecutableFileAmbient(const std::filesystem::path & exe);
|
||||
|
||||
/**
|
||||
* Return temporary path constructed by appending a suffix to a root path.
|
||||
*
|
||||
* The constructed path looks like `<root><suffix>-<pid>-<unique>`. To create a
|
||||
* path nested in a directory, provide a suffix starting with `/`.
|
||||
*/
|
||||
Path makeTempPath(const Path & root, const Path & suffix = ".tmp");
|
||||
|
||||
/**
|
||||
* Used in various places.
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include <atomic>
|
||||
#include "nix/util/source-accessor.hh"
|
||||
#include "nix/util/archive.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue