Files
spark/src/lib/utils/TempDir.h
T

53 lines
1.2 KiB
C++

#pragma once
#include "lib/os/os.h"
#include <boost/filesystem.hpp>
#include <sstream>
#include <stdlib.h>
#include <string>
namespace bts {
namespace utils {
class TempDir {
public:
TempDir() : path_() {
// stolen from TempFile
std::string temp;
char *temp_cstr = getenv("TEMP");
if (temp_cstr != NULL)
temp = temp_cstr;
bts::os::normalize_path(temp);
if (!bts::os::is_windows() && temp.empty())
temp = "/tmp";
path_ = boost::filesystem::path(temp);
try {
path_ /=
boost::filesystem::unique_path("bts-utest-%%%%-%%%%-%%%%-%%%%");
} catch (const std::exception &) {
std::ostringstream digits;
digits << "bts-utest";
for (int i = 0; i < 21; i++) {
if (i % 5 == 0)
digits << "-";
else
digits << rand() % 9;
}
path_ /= boost::filesystem::path(digits.str());
}
boost::filesystem::create_directory(path_);
}
std::string name() { return path_.string(); }
~TempDir() { boost::filesystem::remove_all(path_); }
private:
boost::filesystem::path path_;
};
} // namespace utils
} // namespace bts