Skip to content

[libcxx] Correct and clean-up filesystem operations error_code paths #88341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions libcxx/src/filesystem/operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ void __copy(const path& from, const path& to, copy_options options, error_code*
return err.report(errc::function_not_supported);
}

if (ec)
ec->clear();

if (is_symlink(f)) {
if (bool(copy_options::skip_symlinks & options)) {
// do nothing
Expand Down Expand Up @@ -166,15 +163,15 @@ void __copy(const path& from, const path& to, copy_options options, error_code*
return;
}
error_code m_ec2;
for (; it != directory_iterator(); it.increment(m_ec2)) {
if (m_ec2) {
return err.report(m_ec2);
}
for (; !m_ec2 && it != directory_iterator(); it.increment(m_ec2)) {
__copy(it->path(), to / it->path().filename(), options | copy_options::__in_recursive_copy, ec);
if (ec && *ec) {
return;
}
}
if (m_ec2) {
return err.report(m_ec2);
}
}
}

Expand Down Expand Up @@ -936,23 +933,28 @@ path __weakly_canonical(const path& p, error_code* ec) {
--PP;
vector<string_view_t> DNEParts;

error_code m_ec;
while (PP.State != PathParser::PS_BeforeBegin) {
tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));
error_code m_ec;
file_status st = __status(tmp, &m_ec);
if (!status_known(st)) {
return err.report(m_ec);
} else if (exists(st)) {
result = __canonical(tmp, ec);
result = __canonical(tmp, &m_ec);
if (m_ec) {
return err.report(m_ec);
}
break;
}
DNEParts.push_back(*PP);
--PP;
}
if (PP.State == PathParser::PS_BeforeBegin)
result = __canonical("", ec);
if (ec)
ec->clear();
if (PP.State == PathParser::PS_BeforeBegin) {
result = __canonical("", &m_ec);
if (m_ec) {
return err.report(m_ec);
}
}
if (DNEParts.empty())
return result;
for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,23 @@ int main(int, char**) {
fs::path p = TC.input;
fs::path expect = TC.expect;
expect.make_preferred();
const fs::path output = fs::weakly_canonical(p);

TEST_REQUIRE(PathEq(output, expect),
TEST_WRITE_CONCATENATED(
"Input: ", TC.input.string(), "\nExpected: ", expect.string(), "\nOutput: ", output.string()));
{
const fs::path output = fs::weakly_canonical(p);
TEST_REQUIRE(PathEq(output, expect),
TEST_WRITE_CONCATENATED(
"Input: ", TC.input.string(), "\nExpected: ", expect.string(), "\nOutput: ", output.string()));
}

// Test the error_code variant
{
std::error_code ec;
const fs::path output_c = fs::weakly_canonical(p, ec);

TEST_REQUIRE(PathEq(output_c, expect),
TEST_WRITE_CONCATENATED(
"Input: ", TC.input.string(), "\nExpected: ", expect.string(), "\nOutput: ", output_c.string()));
}
}
return 0;
}
Loading