Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::filesystem for temporary directory location and deletion #10664

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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