Skip to content

Commit

Permalink
Merge pull request #4903 from NREL/4824_Temp_dir_collision
Browse files Browse the repository at this point in the history
Fix #4824 - Use a unique_path in create_temporary_directory to avoid collision
  • Loading branch information
jmarrec authored Jun 29, 2023
2 parents 93b47f4 + 97e9c03 commit 2eccd1a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/utilities/core/FilesystemHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "Path.hpp"
#include "Assert.hpp"

#include <fmt/format.h>

namespace openstudio {
namespace filesystem {
std::vector<char> read(openstudio::filesystem::ifstream& t_file) {
Expand Down Expand Up @@ -140,17 +142,15 @@ namespace filesystem {
static std::atomic<unsigned int> count = 0;
constexpr auto allowed_attempts = 1000;

const auto temp_dir = openstudio::filesystem::temp_directory_path();

int attempts{0};
const auto base_temp_dir = openstudio::filesystem::temp_directory_path();
// std::filesystem::unique_path doesn't exist, if moving to std::filesystem, use emoveBraces(createUUID)
auto upath = boost::filesystem::unique_path();
const auto temp_dir = base_temp_dir / fmt::format("{}-{}-{}-", basename.string(), upath.string(), std::time(nullptr));

while (attempts < allowed_attempts) {
while (count < allowed_attempts) {
// concat number to path basename, without adding a new path element
auto filename = basename;
filename += openstudio::toPath("-" + std::to_string(std::time(nullptr)) + "-" + std::to_string(count++));
auto full_pathname = temp_dir / filename;
// full_path_name = {temp_path}/{base_name}-{count++}

auto full_pathname = temp_dir;
full_pathname += std::to_string(count++);
try {
if (openstudio::filesystem::create_directories(full_pathname)) {
// if the path was created, then we know it was created for us
Expand Down

0 comments on commit 2eccd1a

Please sign in to comment.