From a697b05dd7e3a4c3e1db169e49931ce59f1ef3ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Sch=C3=BCmann?= Date: Sun, 23 May 2021 10:53:58 +0200 Subject: [PATCH] refs #121, allow fs::remove on read-only entries in windows too --- include/ghc/filesystem.hpp | 7 +++++++ test/filesystem_test.cpp | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index bc55b51..c2fd72b 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -4649,6 +4649,13 @@ GHC_INLINE bool remove(const path& p, std::error_code& ec) noexcept } ec = detail::make_system_error(error); } + else if(attr & FILE_ATTRIBUTE_READONLY) { + auto new_attr = attr & ~FILE_ATTRIBUTE_READONLY; + if(!SetFileAttributesW(cstr, new_attr)) { + auto error = ::GetLastError(); + ec = detail::make_system_error(error); + } + } if (!ec) { if (attr & FILE_ATTRIBUTE_DIRECTORY) { if (!RemoveDirectoryW(cstr)) { diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index 4528c05..ee61c6f 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -2889,3 +2889,18 @@ TEST_CASE("Windows: path namespace handling", "[filesystem][path][fs.path.win.na WARN("Windows specific tests are empty on non-Windows systems."); #endif } + +TEST_CASE("Windows: Deletion of Read-only Files", "[filesystem][fs.win][fs.win.remove]") +{ +#ifdef GHC_OS_WINDOWS + TemporaryDirectory t(TempOpt::change_path); + std::error_code ec; + generateFile("foo", 512); + auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write; + CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove)); + CHECK_NOTHROW(fs::remove("foo")); + CHECK(!fs::exists("foo")); +#else + WARN("Windows specific tests are empty on non-Windows systems."); +#endif +}