summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libcxx/src/experimental/filesystem/operations.cpp33
-rw-r--r--libcxx/test/std/experimental/filesystem/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp8
2 files changed, 27 insertions, 14 deletions
diff --git a/libcxx/src/experimental/filesystem/operations.cpp b/libcxx/src/experimental/filesystem/operations.cpp
index 6c7e4cf2488..4ca7727d06c 100644
--- a/libcxx/src/experimental/filesystem/operations.cpp
+++ b/libcxx/src/experimental/filesystem/operations.cpp
@@ -886,23 +886,28 @@ path __system_complete(const path& p, std::error_code *ec) {
return absolute(p, current_path());
}
-path __temp_directory_path(std::error_code *ec) {
- const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
- const char* ret = nullptr;
- for (auto & ep : env_paths) {
- if ((ret = std::getenv(ep)))
- break;
- }
- path p(ret ? ret : "/tmp");
- std::error_code m_ec;
- if (is_directory(p, m_ec)) {
- if (ec) ec->clear();
- return p;
- }
+path __temp_directory_path(std::error_code* ec) {
+ const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
+ const char* ret = nullptr;
+
+ for (auto& ep : env_paths)
+ if ((ret = std::getenv(ep)))
+ break;
+ if (ret == nullptr)
+ ret = "/tmp";
+
+ path p(ret);
+ std::error_code m_ec;
+ if (!exists(p, m_ec) || !is_directory(p, m_ec)) {
if (!m_ec || m_ec == make_error_code(errc::no_such_file_or_directory))
- m_ec = make_error_code(errc::not_a_directory);
+ m_ec = make_error_code(errc::not_a_directory);
set_or_throw(m_ec, ec, "temp_directory_path");
return {};
+ }
+
+ if (ec)
+ ec->clear();
+ return p;
}
// An absolute path is composed according to the table in [fs.op.absolute].
diff --git a/libcxx/test/std/experimental/filesystem/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp b/libcxx/test/std/experimental/filesystem/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp
index 148564e6196..021dd7fc816 100644
--- a/libcxx/test/std/experimental/filesystem/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp
+++ b/libcxx/test/std/experimental/filesystem/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp
@@ -97,6 +97,14 @@ TEST_CASE(basic_tests)
TEST_CHECK(ec == std::make_error_code(std::errc::permission_denied));
TEST_CHECK(ret == "");
+ // Set the env variable to point to a non-existent dir
+ PutEnv(TC.name, TC.p / "does_not_exist");
+ ec = GetTestEC();
+ ret = temp_directory_path(ec);
+ TEST_CHECK(ec != GetTestEC());
+ TEST_CHECK(ec);
+ TEST_CHECK(ret == "");
+
// Finally erase this env variable
UnsetEnv(TC.name);
}
OpenPOWER on IntegriCloud