filesystem: fix n4100 conformance for temp_directory_path
N4100 states that an error shall be reported if `!exists(p) || !is_directory(p)`. We were missing the first half of the conditional. Invert the error and normal code paths to make the code easier to follow. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@294127 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -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].
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user