Make std::experimental::filesystem::remove and remove_all return false or 0 if the file doesn't exist

Differential Revision: https://reviews.llvm.org/D41830



git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@322293 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ekaterina Vaartis
2018-01-11 17:04:29 +00:00
parent fa614b847a
commit 45d5893943
3 changed files with 38 additions and 7 deletions

View File

@@ -661,8 +661,10 @@ path __read_symlink(const path& p, std::error_code *ec) {
bool __remove(const path& p, std::error_code *ec) { bool __remove(const path& p, std::error_code *ec) {
if (ec) ec->clear(); if (ec) ec->clear();
if (::remove(p.c_str()) == -1) { if (::remove(p.c_str()) == -1) {
set_or_throw(ec, "remove", p); if (errno != ENOENT)
set_or_throw(ec, "remove", p);
return false; return false;
} }
return true; return true;
@@ -692,13 +694,18 @@ std::uintmax_t remove_all_impl(path const & p, std::error_code& ec)
} // end namespace } // end namespace
std::uintmax_t __remove_all(const path& p, std::error_code *ec) { std::uintmax_t __remove_all(const path& p, std::error_code *ec) {
if (ec) ec->clear();
std::error_code mec; std::error_code mec;
auto count = remove_all_impl(p, mec); auto count = remove_all_impl(p, mec);
if (mec) { if (mec) {
set_or_throw(mec, ec, "remove_all", p); if (mec == errc::no_such_file_or_directory) {
return static_cast<std::uintmax_t>(-1); return 0;
} else {
set_or_throw(mec, ec, "remove_all", p);
return static_cast<std::uintmax_t>(-1);
}
} }
if (ec) ec->clear();
return count; return count;
} }

View File

@@ -61,17 +61,29 @@ TEST_CASE(test_error_reporting)
const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42); const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
permissions(bad_perms_dir, perms::none); permissions(bad_perms_dir, perms::none);
const path testCases[] = { const path testCases[] = {
"",
env.make_env_path("dne"),
non_empty_dir, non_empty_dir,
file_in_bad_dir, file_in_bad_dir,
}; };
for (auto& p : testCases) { for (auto& p : testCases) {
std::error_code ec; std::error_code ec;
TEST_CHECK(!fs::remove(p, ec)); TEST_CHECK(!fs::remove(p, ec));
TEST_CHECK(ec); TEST_CHECK(ec);
TEST_CHECK(checkThrow(p, ec)); TEST_CHECK(checkThrow(p, ec));
} }
// PR#35780
const path testCasesNonexistant[] = {
"",
env.make_env_path("dne")
};
for (auto& p : testCasesNonexistant) {
std::error_code ec;
TEST_CHECK(!fs::remove(p, ec));
TEST_CHECK(!ec);
}
} }
TEST_CASE(basic_remove_test) TEST_CASE(basic_remove_test)

View File

@@ -64,16 +64,28 @@ TEST_CASE(test_error_reporting)
permissions(bad_perms_file, perms::none); permissions(bad_perms_file, perms::none);
const path testCases[] = { const path testCases[] = {
env.make_env_path("dne"),
file_in_bad_dir file_in_bad_dir
}; };
const auto BadRet = static_cast<std::uintmax_t>(-1); const auto BadRet = static_cast<std::uintmax_t>(-1);
for (auto& p : testCases) { for (auto& p : testCases) {
std::error_code ec; std::error_code ec;
TEST_CHECK(fs::remove_all(p, ec) == BadRet); TEST_CHECK(fs::remove_all(p, ec) == BadRet);
TEST_CHECK(ec); TEST_CHECK(ec);
TEST_CHECK(checkThrow(p, ec)); TEST_CHECK(checkThrow(p, ec));
} }
// PR#35780
const path testCasesNonexistant[] = {
"",
env.make_env_path("dne")
};
for (auto &p : testCasesNonexistant) {
std::error_code ec;
TEST_CHECK(fs::remove_all(p) == 0);
TEST_CHECK(!ec);
}
} }
TEST_CASE(basic_remove_all_test) TEST_CASE(basic_remove_all_test)