Skip to content
Closed
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
17 changes: 3 additions & 14 deletions include/rcpputils/filesystem_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define RCPPUTILS__FILESYSTEM_HELPER_HPP_

#include <cstdint>
#include <filesystem>
#include <string>
#include <vector>

Expand Down Expand Up @@ -259,18 +260,6 @@ RCPPUTILS_PUBLIC uint64_t file_size(const path & p);
*/
RCPPUTILS_PUBLIC bool exists(const path & path_to_check);


/**
* \brief Get a path to a location in the temporary directory, if it's available.
*
* This does not create any directories.
* On Windows, this uses "GetTempPathA"
* On non-Windows, this prefers the environment variable TMPDIR, falling back to /tmp
*
* \return A path to a directory for storing temporary files and directories.
*/
RCPPUTILS_PUBLIC path temp_directory_path();

/**
* \brief Construct a uniquely named temporary directory, in "parent", with format base_nameXXXXXX
*
Expand All @@ -284,9 +273,9 @@ RCPPUTILS_PUBLIC path temp_directory_path();
*
* \throws std::system_error If any OS APIs do not succeed.
*/
RCPPUTILS_PUBLIC path create_temp_directory(
RCPPUTILS_PUBLIC std::filesystem::path create_temp_directory(
const std::string & base_name,
const path & parent_path = temp_directory_path());
const std::filesystem::path & parent_path = std::filesystem::temp_directory_path());

/**
* \brief Return current working directory.
Expand Down
35 changes: 7 additions & 28 deletions src/filesystem_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,36 +284,15 @@ bool exists(const path & path_to_check)
return path_to_check.exists();
}

path temp_directory_path()
{
#ifdef _WIN32
#ifdef UNICODE
#error "rcpputils::fs does not support Unicode paths"
#endif
TCHAR temp_path[MAX_PATH];
DWORD size = GetTempPathA(MAX_PATH, temp_path);
if (size > MAX_PATH || size == 0) {
std::error_code ec(static_cast<int>(GetLastError()), std::system_category());
throw std::system_error(ec, "cannot get temporary directory path");
}
temp_path[size] = '\0';
#else
const char * temp_path = NULL;
const char * ret_str = rcutils_get_env("TMPDIR", &temp_path);
if (NULL != ret_str || *temp_path == '\0') {
temp_path = "/tmp";
}
#endif
return path(temp_path);
}

path create_temp_directory(const std::string & base_name, const path & parent_path)
std::filesystem::path create_temp_directory(
const std::string & base_name,
const std::filesystem::path & parent_path)
{
const auto template_path = base_name + "XXXXXX";
std::string full_template_str = (parent_path / template_path).string();
if (!create_directories(parent_path)) {
std::error_code ec{errno, std::system_category()};
errno = 0;
std::error_code ec;
std::filesystem::create_directories(parent_path, ec);
if (ec) {
throw std::system_error(ec, "could not create the parent directory");
}

Expand All @@ -335,7 +314,7 @@ path create_temp_directory(const std::string & base_name, const path & parent_pa
errno = 0;
throw std::system_error(ec, "could not format or create the temp directory");
}
const path final_path{dir_name};
const std::filesystem::path final_path{dir_name};
#endif

return final_path;
Expand Down
22 changes: 16 additions & 6 deletions test/test_filesystem_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ TEST(TestFilesystemHelper, filesystem_manipulation)
EXPECT_TRUE(rcpputils::fs::remove(dir));
EXPECT_FALSE(rcpputils::fs::exists(file));
EXPECT_FALSE(rcpputils::fs::exists(dir));
auto temp_dir = rcpputils::fs::temp_directory_path();
auto temp_dir_std = std::filesystem::temp_directory_path();
rcpputils::fs::path temp_dir = rcpputils::fs::path(temp_dir_std.c_str());
temp_dir = temp_dir / "rcpputils" / "test_folder";
EXPECT_FALSE(rcpputils::fs::exists(temp_dir));
EXPECT_TRUE(rcpputils::fs::create_directories(temp_dir));
Expand Down Expand Up @@ -452,7 +453,9 @@ TEST(TestFilesystemHelper, create_temp_directory)
{
const std::string basename = "test_base_name";

const auto tmpdir1 = rcpputils::fs::create_temp_directory(basename);
const auto tmpdir1_std = rcpputils::fs::create_temp_directory(basename);
rcpputils::fs::path tmpdir1(tmpdir1_std.c_str());

EXPECT_TRUE(tmpdir1.exists());
EXPECT_TRUE(tmpdir1.is_directory());

Expand All @@ -464,7 +467,8 @@ TEST(TestFilesystemHelper, create_temp_directory)
EXPECT_TRUE(rcpputils::fs::exists(tmp_file));
EXPECT_TRUE(rcpputils::fs::is_regular_file(tmp_file));

const auto tmpdir2 = rcpputils::fs::create_temp_directory(basename);
const auto tmpdir2_std = rcpputils::fs::create_temp_directory(basename);
rcpputils::fs::path tmpdir2(tmpdir2_std.c_str());
EXPECT_TRUE(tmpdir2.exists());
EXPECT_TRUE(tmpdir2.is_directory());

Expand All @@ -486,7 +490,11 @@ TEST(TestFilesystemHelper, create_temp_directory)
// newly created paths
{
const auto new_relative = rcpputils::fs::current_path() / "child1" / "child2";
const auto tmpdir = rcpputils::fs::create_temp_directory("base_name", new_relative);
const auto tmpdir_std = rcpputils::fs::create_temp_directory(
"base_name",
std::filesystem::path(new_relative.string()));
rcpputils::fs::path tmpdir(tmpdir_std.c_str());

EXPECT_TRUE(tmpdir.exists());
EXPECT_TRUE(tmpdir.is_directory());
EXPECT_TRUE(rcpputils::fs::remove_all(tmpdir));
Expand All @@ -499,11 +507,13 @@ TEST(TestFilesystemHelper, create_temp_directory)
EXPECT_EQ(tmpdir_emptybase.filename().string().size(), 6u);

// Empty path doesn't exist and cannot be created
EXPECT_THROW(rcpputils::fs::create_temp_directory("basename", path()), std::system_error);
EXPECT_THROW(rcpputils::fs::create_temp_directory("basename", std::filesystem::path()),
std::system_error);

// With the template string XXXXXX already in the name, it will still be there, the unique
// portion is appended to the end.
const auto tmpdir_template_in_name = rcpputils::fs::create_temp_directory("base_XXXXXX");
const auto tmpdir_template_in_name_std = rcpputils::fs::create_temp_directory("base_XXXXXX");
rcpputils::fs::path tmpdir_template_in_name(tmpdir_template_in_name_std.c_str());
EXPECT_TRUE(tmpdir_template_in_name.exists());
EXPECT_TRUE(tmpdir_template_in_name.is_directory());
// On Linux, it will not replace the base_name Xs, only the final 6 that the function appends.
Expand Down