PR71337 fix filesystem::temp_directory_path error handling

PR libstdc++/71337
	* src/filesystem/ops.cc (temp_directory_path): Pass error_code
	argument to other filesystem operations.
	* testsuite/experimental/filesystem/operations/temp_directory_path.cc:
	Add testcase for inaccessible directory.

From-SVN: r241487
This commit is contained in:
Jonathan Wakely 2016-10-24 17:45:51 +01:00 committed by Jonathan Wakely
parent bb52a7e324
commit 6daff2d946
3 changed files with 61 additions and 4 deletions

View File

@ -1,5 +1,11 @@
2016-10-24 Jonathan Wakely <jwakely@redhat.com> 2016-10-24 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/71337
* src/filesystem/ops.cc (temp_directory_path): Pass error_code
argument to other filesystem operations.
* testsuite/experimental/filesystem/operations/temp_directory_path.cc:
Add testcase for inaccessible directory.
* src/filesystem/dir.cc (open_dir): Return same value for errors * src/filesystem/dir.cc (open_dir): Return same value for errors
whether ignored or not. whether ignored or not.
(_Dir::advance(error_code*, directory_options)): Return false on (_Dir::advance(error_code*, directory_options)): Return false on

View File

@ -1428,12 +1428,17 @@ fs::path fs::temp_directory_path(error_code& ec)
for (auto e = env; tmpdir == nullptr && *e != nullptr; ++e) for (auto e = env; tmpdir == nullptr && *e != nullptr; ++e)
tmpdir = ::getenv(*e); tmpdir = ::getenv(*e);
path p = tmpdir ? tmpdir : "/tmp"; path p = tmpdir ? tmpdir : "/tmp";
if (exists(p) && is_directory(p)) auto st = status(p, ec);
if (!ec)
{ {
ec.clear(); if (is_directory(st))
return p; {
ec.clear();
return p;
}
else
ec = std::make_error_code(std::errc::not_a_directory);
} }
ec = std::make_error_code(std::errc::not_a_directory);
return {}; return {};
#endif #endif
} }

View File

@ -45,6 +45,7 @@ test01()
std::error_code ec; std::error_code ec;
fs::path p1 = fs::temp_directory_path(ec); fs::path p1 = fs::temp_directory_path(ec);
VERIFY( !ec );
VERIFY( exists(p1) ); VERIFY( exists(p1) );
fs::path p2 = fs::temp_directory_path(); fs::path p2 = fs::temp_directory_path();
@ -62,6 +63,7 @@ test02()
std::error_code ec; std::error_code ec;
fs::path p = fs::temp_directory_path(ec); fs::path p = fs::temp_directory_path(ec);
VERIFY( ec ); VERIFY( ec );
VERIFY( p == fs::path() );
std::error_code ec2; std::error_code ec2;
try { try {
@ -72,10 +74,54 @@ test02()
VERIFY( ec2 == ec ); VERIFY( ec2 == ec );
} }
void
test03()
{
auto p = __gnu_test::nonexistent_path();
create_directories(p/"tmp");
permissions(p, fs::perms::none);
setenv("TMPDIR", (p/"tmp").c_str(), 1);
std::error_code ec;
auto r = fs::temp_directory_path(ec); // libstdc++/PR71337
VERIFY( ec == std::make_error_code(std::errc::permission_denied) );
VERIFY( r == fs::path() );
std::error_code ec2;
try {
fs::temp_directory_path();
} catch (const fs::filesystem_error& e) {
ec2 = e.code();
}
VERIFY( ec2 == ec );
permissions(p, fs::perms::owner_all, ec);
remove_all(p, ec);
}
void
test04()
{
__gnu_test::scoped_file f;
setenv("TMPDIR", f.path.c_str(), 1);
std::error_code ec;
auto r = fs::temp_directory_path(ec);
VERIFY( ec == std::make_error_code(std::errc::not_a_directory) );
VERIFY( r == fs::path() );
std::error_code ec2;
try {
fs::temp_directory_path();
} catch (const fs::filesystem_error& e) {
ec2 = e.code();
}
VERIFY( ec2 == ec );
}
int int
main() main()
{ {
test01(); test01();
test02(); test02();
test03();
test04();
} }