mirror of
https://git.mirrors.martin98.com/https://github.com/gulrak/filesystem
synced 2025-06-04 11:13:58 +08:00
[Closes #1] fs::canonical now errors on empty path and fs::weakly_canonical doesn't call canonical with empty path anymore
This commit is contained in:
parent
58610c421d
commit
d097c9535a
19
filesystem.h
19
filesystem.h
@ -2615,6 +2615,10 @@ inline path canonical(const path& p)
|
||||
|
||||
inline path canonical(const path& p, std::error_code& ec)
|
||||
{
|
||||
if(p.empty()) {
|
||||
ec = detail::make_error_code(detail::portable_error::not_found);
|
||||
return path();
|
||||
}
|
||||
path work = p.is_absolute() ? p : absolute(p, ec);
|
||||
path root = work.root_path();
|
||||
path result;
|
||||
@ -3813,9 +3817,14 @@ inline path weakly_canonical(const path& p, std::error_code& ec) noexcept
|
||||
return path();
|
||||
}
|
||||
scan = false;
|
||||
result = canonical(result, ec) / pe;
|
||||
if (ec) {
|
||||
break;
|
||||
if(!result.empty()) {
|
||||
result = canonical(result, ec) / pe;
|
||||
if (ec) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
result /= pe;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3824,7 +3833,9 @@ inline path weakly_canonical(const path& p, std::error_code& ec) noexcept
|
||||
}
|
||||
}
|
||||
if (scan) {
|
||||
result = canonical(result, ec);
|
||||
if(!result.empty()) {
|
||||
result = canonical(result, ec);
|
||||
}
|
||||
}
|
||||
return ec ? path() : result.lexically_normal();
|
||||
}
|
||||
|
@ -1101,7 +1101,7 @@ TEST_CASE("30.10.15.1 absolute", "[filesystem][operations][fs.op.absolute]")
|
||||
|
||||
TEST_CASE("30.10.15.2 canonical", "[filesystem][operations][fs.op.canonical]")
|
||||
{
|
||||
CHECK(fs::canonical("") == fs::current_path());
|
||||
CHECK_THROWS_AS(fs::canonical(""), fs::filesystem_error);
|
||||
CHECK(fs::canonical(fs::current_path()) == fs::current_path());
|
||||
|
||||
CHECK(fs::canonical(".") == fs::current_path());
|
||||
@ -1462,6 +1462,7 @@ TEST_CASE("30.10.15.13 exists", "[filesystem][operations][fs.op.exists]")
|
||||
{
|
||||
TemporaryDirectory t(TempOpt::change_path);
|
||||
std::error_code ec;
|
||||
CHECK(!fs::exists(""));
|
||||
CHECK(!fs::exists("foo"));
|
||||
CHECK(!fs::exists("foo", ec));
|
||||
CHECK(!ec);
|
||||
@ -2130,9 +2131,9 @@ TEST_CASE("30.10.15.38 temporary_directory_path", "[filesystem][operations][fs.o
|
||||
|
||||
TEST_CASE("30.10.15.39 weakly_canonical", "[filesystem][operations][fs.op.weakly_canonical]")
|
||||
{
|
||||
CHECK(fs::weakly_canonical("foo/bar") == fs::current_path() / "foo/bar");
|
||||
CHECK(fs::weakly_canonical("foo/./bar") == fs::current_path() / "foo/bar");
|
||||
CHECK(fs::weakly_canonical("foo/../bar") == fs::current_path() / "bar");
|
||||
CHECK(fs::weakly_canonical("foo/bar") == "foo/bar");
|
||||
CHECK(fs::weakly_canonical("foo/./bar") == "foo/bar");
|
||||
CHECK(fs::weakly_canonical("foo/../bar") == "bar");
|
||||
|
||||
{
|
||||
TemporaryDirectory t(TempOpt::change_path);
|
||||
|
Loading…
x
Reference in New Issue
Block a user