From d831937f12fd59f98cdab2401ec15237b19d6a92 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 16 Sep 2024 13:27:52 +0100 Subject: [PATCH] Extract function to create a random filename. --- nano/secure/utility.cpp | 8 ++++++-- nano/secure/utility.hpp | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/nano/secure/utility.cpp b/nano/secure/utility.cpp index ac974d9f5..a2dabd5e3 100644 --- a/nano/secure/utility.cpp +++ b/nano/secure/utility.cpp @@ -47,7 +47,7 @@ std::filesystem::path nano::working_path (nano::networks network) return result; } -std::filesystem::path nano::unique_path (nano::networks network) +std::filesystem::path nano::random_filename () { std::random_device rd; std::mt19937 gen (rd ()); @@ -61,8 +61,12 @@ std::filesystem::path nano::unique_path (nano::networks network) { random_string += hex_chars[dis (gen)]; } + return std::filesystem::path{ random_string }; +} - auto result = working_path (network) / random_string; +std::filesystem::path nano::unique_path (nano::networks network) +{ + auto result = working_path (network) / random_filename (); std::filesystem::create_directories (result); diff --git a/nano/secure/utility.hpp b/nano/secure/utility.hpp index abb70df84..90953eea0 100644 --- a/nano/secure/utility.hpp +++ b/nano/secure/utility.hpp @@ -9,6 +9,8 @@ namespace nano std::filesystem::path app_path (); // OS-specific way of finding a path to a home directory. std::filesystem::path working_path (nano::networks network = nano::network_constants::active_network); +// Construct a random filename +std::filesystem::path random_filename (); // Get a unique path within the home directory, used for testing. // Any directories created at this location will be removed when a test finishes. std::filesystem::path unique_path (nano::networks network = nano::network_constants::active_network);