Skip to content

Commit

Permalink
use std::filesystem for temp directory location/deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
vuule committed Apr 14, 2022
1 parent 03e84ef commit c3b82b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cpp/benchmarks/text/subword.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>

#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
Expand All @@ -29,7 +30,7 @@

static std::string create_hash_vocab_file()
{
std::string dir_template("/tmp");
std::string dir_template{std::filesystem::temp_directory_path().string()};
if (const char* env_p = std::getenv("WORKSPACE")) dir_template = env_p;
std::string hash_file = dir_template + "/hash_vocab.txt";
// create a fake hashed vocab text file for this test
Expand Down
20 changes: 7 additions & 13 deletions cpp/include/cudf_test/file_utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <string>

#include <ftw.h>
Expand All @@ -34,29 +35,22 @@ class temp_directory {
public:
temp_directory(const std::string& base_name)
{
std::string dir_template("/tmp");
if (const char* env_p = std::getenv("WORKSPACE")) dir_template = env_p;
std::string dir_template{std::filesystem::temp_directory_path().string()};
if (auto env_p = std::getenv("WORKSPACE")) dir_template = env_p;

dir_template += "/" + base_name + ".XXXXXX";
auto const tmpdirptr = mkdtemp(const_cast<char*>(dir_template.data()));
if (tmpdirptr == nullptr) CUDF_FAIL("Temporary directory creation failure: " + dir_template);
_path = dir_template + "/";
}
CUDF_EXPECTS(tmpdirptr != nullptr, "Temporary directory creation failure: " + dir_template);

static int rm_files(const char* pathname, const struct stat* sbuf, int type, struct FTW* ftwb)
{
return std::remove(pathname);
_path = dir_template + "/";
}

temp_directory& operator=(temp_directory const&) = delete;
temp_directory(temp_directory const&) = delete;
temp_directory& operator=(temp_directory&&) = default;
temp_directory(temp_directory&&) = default;

~temp_directory()
{
// TODO: should use std::filesystem instead, once C++17 support added
nftw(_path.c_str(), rm_files, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
}
~temp_directory() { std::filesystem::remove_all(std::filesystem::path{_path}); }

/**
* @brief Returns the path of the temporary directory
Expand Down

0 comments on commit c3b82b5

Please sign in to comment.