From a4303f9207ac3ee950be29d6ad6245ccdb63d2ea Mon Sep 17 00:00:00 2001 From: Steffen Schuemann Date: Wed, 2 Jan 2019 19:59:27 +0100 Subject: [PATCH] #4: Added test code, fixed missing error_code propagation, and error handling in fs::copy and fs::remove_all. --- filesystem.h | 15 ++++++++++----- test/filesystem_test.cpp | 3 +++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/filesystem.h b/filesystem.h index 0172263..86cbda7 100644 --- a/filesystem.h +++ b/filesystem.h @@ -104,7 +104,7 @@ #define LWG_2937_BEHAVIOUR // ghc::filesystem version in decimal (major * 10000 + minor * 100 + patch) -#define GHC_FILESYSTEM_VERSION 10004L +#define GHC_FILESYSTEM_VERSION 10005L namespace ghc { namespace filesystem { @@ -2812,8 +2812,11 @@ inline void copy(const path& from, const path& to, copy_options options, std::er return; } } - for (const directory_entry& x : directory_iterator(from)) { - copy(x.path(), to / x.path().filename(), options | static_cast(0x8000)); + for (const directory_entry& x : directory_iterator(from, ec)) { + copy(x.path(), to / x.path().filename(), options | static_cast(0x8000), ec); + if(ec) { + return; + } } } return; @@ -3685,7 +3688,7 @@ inline uintmax_t remove_all(const path& p, std::error_code& ec) noexcept ec = detail::make_error_code(detail::portable_error::not_supported); return static_cast(-1); } - for (const directory_entry& de : directory_iterator(p)) { + for (const directory_entry& de : directory_iterator(p, ec)) { if (!de.is_symlink() && de.is_directory()) { count += remove_all(de.path(), ec); if (ec) { @@ -3700,7 +3703,9 @@ inline uintmax_t remove_all(const path& p, std::error_code& ec) noexcept ++count; } } - remove(p, ec); + if(!ec) { + remove(p, ec); + } if (ec) { return static_cast(-1); } diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index 31ae827..d720cee 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -1019,6 +1019,7 @@ TEST_CASE("30.10.13 class directory_iterator", "[filesystem][directory_iterator] CHECK(!iter->is_directory()); CHECK(iter->file_size() == 1234); CHECK(++iter == fs::directory_iterator()); + CHECK_THROWS_AS(fs::directory_iterator(t.path() / "non-existing"), fs::filesystem_error); } if (is_symlink_creation_supported()) { TemporaryDirectory t; @@ -2008,6 +2009,8 @@ TEST_CASE("30.10.15.31 remove_all", "[filesystem][operations][fs.op.remove_all]" fs::create_directories("dir1/dir1b"); generateFile("dir1/dir1a/f1"); generateFile("dir1/dir1b/f2"); + CHECK_NOTHROW(fs::remove_all("dir1/non-existing", ec)); + CHECK(ec); CHECK(fs::remove_all("dir1") == 5); CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator()); }