libstdc++: Improve exceptions thrown from fs::temp_directory_path
Currently the throwing overload of fs::temp_directory_path() will discard the path that was obtained from the environment. When it fails because the path doesn't resolve to a directory you get an unhelpful error like: filesystem error: temp_directory_path: Not a directory It would be better to also print the path in that case, e.g. filesystem error: temp_directory_path: Not a directory [/home/bob/tmp] libstdc++-v3/ChangeLog: * src/c++17/fs_ops.cc (fs::temp_directory_path()): Include path in exception. (fs::temp_directory_path(error_code&)): Rearrange to more closely match the structure of the first overload. * src/filesystem/ops.cc (fs::temp_directory_path): Likewise. * testsuite/27_io/filesystem/operations/temp_directory_path.cc: Check that exception contains the path. * testsuite/experimental/filesystem/operations/temp_directory_path.cc: Likewise.
This commit is contained in:
parent
6c96b14a19
commit
1eef21ccfa
|
@ -1571,18 +1571,29 @@ fs::path
|
|||
fs::temp_directory_path()
|
||||
{
|
||||
error_code ec;
|
||||
path tmp = temp_directory_path(ec);
|
||||
path p = fs::get_temp_directory_from_env(ec);
|
||||
if (!ec)
|
||||
{
|
||||
auto st = status(p, ec);
|
||||
if (!ec && !is_directory(st))
|
||||
ec = std::make_error_code(std::errc::not_a_directory);
|
||||
}
|
||||
if (ec)
|
||||
{
|
||||
if (p.empty())
|
||||
_GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec));
|
||||
return tmp;
|
||||
else
|
||||
_GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", p, ec));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
fs::path
|
||||
fs::temp_directory_path(error_code& ec)
|
||||
{
|
||||
path p = fs::get_temp_directory_from_env(ec);
|
||||
if (ec)
|
||||
return p;
|
||||
if (!ec)
|
||||
{
|
||||
auto st = status(p, ec);
|
||||
if (ec)
|
||||
p.clear();
|
||||
|
@ -1591,6 +1602,7 @@ fs::temp_directory_path(error_code& ec)
|
|||
p.clear();
|
||||
ec = std::make_error_code(std::errc::not_a_directory);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
|
|
@ -1326,18 +1326,24 @@ fs::path
|
|||
fs::temp_directory_path()
|
||||
{
|
||||
error_code ec;
|
||||
path tmp = temp_directory_path(ec);
|
||||
if (ec.value())
|
||||
_GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec));
|
||||
return tmp;
|
||||
path p = fs::get_temp_directory_from_env(ec);
|
||||
if (!ec)
|
||||
{
|
||||
auto st = status(p, ec);
|
||||
if (!ec && !is_directory(st))
|
||||
ec = std::make_error_code(std::errc::not_a_directory);
|
||||
}
|
||||
if (ec)
|
||||
_GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", p, ec));
|
||||
return p;
|
||||
}
|
||||
|
||||
fs::path
|
||||
fs::temp_directory_path(error_code& ec)
|
||||
{
|
||||
path p = fs::get_temp_directory_from_env(ec);
|
||||
if (ec)
|
||||
return p;
|
||||
if (!ec)
|
||||
{
|
||||
auto st = status(p, ec);
|
||||
if (ec)
|
||||
p.clear();
|
||||
|
@ -1346,5 +1352,6 @@ fs::temp_directory_path(error_code& ec)
|
|||
p.clear();
|
||||
ec = std::make_error_code(std::errc::not_a_directory);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
|
|
@ -140,12 +140,17 @@ test04()
|
|||
VERIFY( r == fs::path() );
|
||||
|
||||
std::error_code ec2;
|
||||
std::string failed_path;
|
||||
try {
|
||||
fs::temp_directory_path();
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
ec2 = e.code();
|
||||
// On Windows the returned path will be in preferred form, i.e. using L'\\'
|
||||
// and will have a trailing slash, so compare generic forms.
|
||||
failed_path = e.path1().generic_string();
|
||||
}
|
||||
VERIFY( ec2 == ec );
|
||||
VERIFY( failed_path.find(f.path.generic_string()) != std::string::npos );
|
||||
}
|
||||
|
||||
int
|
||||
|
|
|
@ -141,12 +141,17 @@ test04()
|
|||
VERIFY( r == fs::path() );
|
||||
|
||||
std::error_code ec2;
|
||||
std::string failed_path;
|
||||
try {
|
||||
fs::temp_directory_path();
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
ec2 = e.code();
|
||||
// On Windows the returned path will be in preferred form, i.e. using L'\\'
|
||||
// and will have a trailing slash, so compare generic forms.
|
||||
failed_path = e.path1().generic_string();
|
||||
}
|
||||
VERIFY( ec2 == ec );
|
||||
VERIFY( failed_path.find(f.path.generic_string()) != std::string::npos );
|
||||
}
|
||||
|
||||
int
|
||||
|
|
Loading…
Reference in New Issue