Skip to content

Commit

Permalink
Use std::filesystem for temporary directory location and deletion (#…
Browse files Browse the repository at this point in the history
…10664)

Addressing a long-standing TODO. Since std::filesystem is available since C++17, use it to recursively delete temporary directories (used for benchmarks, etc.). 
Small step towards portable temp directory/file utilities.

Authors:
  - Vukasin Milovanovic (https://github.com/vuule)

Approvers:
  - Ram (Ramakrishna Prabhu) (https://github.com/rgsl888prabhu)
  - David Wendt (https://github.com/davidwendt)

URL: #10664
  • Loading branch information
vuule authored Apr 15, 2022
1 parent 4e668f2 commit 9e1258d
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 9e1258d

Please sign in to comment.