Skip to content

Commit

Permalink
Use random numbers to create temp filenames.
Browse files Browse the repository at this point in the history
Close #2474
  • Loading branch information
abellgithub committed May 7, 2019
1 parent e6f679d commit 1a5c121
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
28 changes: 21 additions & 7 deletions vendor/arbiter/arbiter.cpp
Expand Up @@ -431,7 +431,7 @@ std::unique_ptr<fs::LocalHandle> Arbiter::getLocalHandle(

const auto ext(getExtension(path));
const std::string basename(
crypto::encodeAsHex(crypto::sha256(stripExtension(path))) +
std::to_string(util::randomNumber()) +
(ext.size() ? "." + ext : ""));
tempEndpoint.put(basename, getBinary(path));
localHandle.reset(
Expand Down Expand Up @@ -702,9 +702,7 @@ std::unique_ptr<fs::LocalHandle> Endpoint::getLocalHandle(
{
const std::string tmp(fs::getTempPath());
const auto ext(Arbiter::getExtension(subpath));
const std::string basename(
crypto::encodeAsHex(crypto::sha256(Arbiter::stripExtension(
prefixedRoot() + subpath))) +
const std::string basename(std::to_string(util::randomNumber()) +
(ext.size() ? "." + ext : ""));

const std::string local(tmp + basename);
Expand Down Expand Up @@ -4727,6 +4725,8 @@ int64_t Time::asUnix() const

#include <algorithm>
#include <cctype>
#include <random>
#include <mutex>

#ifdef ARBITER_CUSTOM_NAMESPACE
namespace ARBITER_CUSTOM_NAMESPACE
Expand All @@ -4738,6 +4738,20 @@ namespace arbiter
namespace util
{

namespace
{
std::mutex randomMutex;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned long long> distribution;
}

uint64_t randomNumber()
{
std::lock_guard<std::mutex> lock(randomMutex);
return distribution(gen);
}

std::string stripPostfixing(const std::string path)
{
std::string stripped(path);
Expand All @@ -4763,11 +4777,11 @@ std::string getBasename(const std::string fullPath)

// Now do the real slash searching.
std::size_t pos(stripped.rfind('/'));

// Maybe windows
if (pos == std::string::npos)
if (pos == std::string::npos)
pos = stripped.rfind('\\');

if (pos != std::string::npos)
{
const std::string sub(stripped.substr(pos + 1));
Expand Down
10 changes: 8 additions & 2 deletions vendor/arbiter/arbiter.hpp
Expand Up @@ -4379,6 +4379,12 @@ namespace arbiter
/** General utilities. */
namespace util
{
/**
Returns a random number based on a random device if one exists on
the system.
*/
ARBITER_DLL uint64_t randomNumber();

/** Returns @p path, less any trailing glob indicators (one or two
* asterisks) as well as any possible trailing slash.
*/
Expand Down Expand Up @@ -4463,7 +4469,7 @@ namespace util
#ifdef ARBITER_WINDOWS
sep = "\\";
#else
sep = "/";
sep = "/";
#endif
}
else if (next.empty() && currentIsDir)
Expand All @@ -4475,7 +4481,7 @@ namespace util
#ifdef ARBITER_WINDOWS
sep = "\\";
#else
sep = "/";
sep = "/";
#endif

}
Expand Down

0 comments on commit 1a5c121

Please sign in to comment.