]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix error reporting in filesystem::copy [PR99290]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 28 Apr 2022 12:06:31 +0000 (13:06 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 14 Jun 2022 19:20:03 +0000 (20:20 +0100)
The recursive calls to filesystem::copy should stop if any of them
reports an error.

libstdc++-v3/ChangeLog:

PR libstdc++/99290
* src/c++17/fs_ops.cc (fs::copy): Pass error_code to
directory_iterator constructor, and check on each iteration.
* src/filesystem/ops.cc (fs::copy): Likewise.
* testsuite/27_io/filesystem/operations/copy.cc: Check for
errors during recursion.
* testsuite/experimental/filesystem/operations/copy.cc:
Likewise.

(cherry picked from commit 4e117418fb71f508c479e0144500f4da9cc92520)

libstdc++-v3/src/c++17/fs_ops.cc
libstdc++-v3/src/filesystem/ops.cc
libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc
libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc

index 008195c2d31dbabf42974d9eb459377248eb6c57..5231f67561e0f9040bc6dbce051ccf879ee48fbd 100644 (file)
@@ -404,8 +404,12 @@ fs::copy(const path& from, const path& to, copy_options options,
       // set an unused bit in options to disable further recursion
       if (!is_set(options, copy_options::recursive))
        options |= static_cast<copy_options>(4096);
-      for (const directory_entry& x : directory_iterator(from))
-       copy(x.path(), to/x.path().filename(), options, ec);
+      for (const directory_entry& x : directory_iterator(from, ec))
+       {
+         copy(x.path(), to/x.path().filename(), options, ec);
+         if (ec)
+           return;
+       }
     }
   // _GLIBCXX_RESOLVE_LIB_DEFECTS
   // 2683. filesystem::copy() says "no effects"
index 321eb3a5cba7e183787386f3e39e72566a7f82b3..e7b145f8d79e8dad864c149278fe75029247b4a4 100644 (file)
@@ -344,8 +344,12 @@ fs::copy(const path& from, const path& to, copy_options options,
       // set an unused bit in options to disable further recursion
       if (!is_set(options, copy_options::recursive))
        options |= static_cast<copy_options>(4096);
-      for (const directory_entry& x : directory_iterator(from))
-       copy(x.path(), to/x.path().filename(), options, ec);
+      for (const directory_entry& x : directory_iterator(from, ec))
+       {
+         copy(x.path(), to/x.path().filename(), options, ec);
+         if (ec)
+           return;
+       }
     }
   // _GLIBCXX_RESOLVE_LIB_DEFECTS
   // 2683. filesystem::copy() says "no effects"
index f7aac8a302fa003e3f04c65f57f75b6529d0418b..cbd8c8ea03e35767fc23a70af57cee9e87caac37 100644 (file)
@@ -194,6 +194,34 @@ test05()
   VERIFY( !ec );  // Previous value should be cleared (LWG 2683)
 }
 
+void
+test_pr99290()
+{
+  auto dir = __gnu_test::nonexistent_path();
+  auto source = dir/"source";
+  auto dest = dir/"dest";
+  create_directories(source/"emptydir");
+  create_directories(dest/"emptydir");
+  std::ofstream{source/"file"} << 'a';
+  std::ofstream{dest/"file"} << 'b';
+  // PR libstdc++/99290
+  // std::filesystem::copy does not always report errors for recursion
+  std::error_code ec;
+  copy(source, dest, ec);
+  VERIFY( ec == std::errc::file_exists );
+
+#if __cpp_exceptions
+  try {
+    copy(source, dest);
+    VERIFY( false );
+  } catch (const fs::filesystem_error& e) {
+    VERIFY( e.code() == std::errc::file_exists );
+  }
+#endif
+
+  remove_all(dir);
+}
+
 int
 main()
 {
@@ -202,4 +230,5 @@ main()
   test03();
   test04();
   test05();
+  test_pr99290();
 }
index e1e6d1dcc1510c53a9f4b49f8521c167e58500dd..75f7e0d8297f8bd91292bafc4aa8f4b360033b15 100644 (file)
@@ -190,6 +190,34 @@ test05()
   VERIFY( !ec );  // Previous value should be cleared (LWG 2683)
 }
 
+void
+test_pr99290()
+{
+  auto dir = __gnu_test::nonexistent_path();
+  auto source = dir/"source";
+  auto dest = dir/"dest";
+  create_directories(source/"emptydir");
+  create_directories(dest/"emptydir");
+  std::ofstream{source/"file"} << 'a';
+  std::ofstream{dest/"file"} << 'b';
+  // PR libstdc++/99290
+  // std::filesystem::copy does not always report errors for recursion
+  std::error_code ec;
+  copy(source, dest, ec);
+  VERIFY( ec == std::errc::file_exists );
+
+#if __cpp_exceptions
+  try {
+    copy(source, dest);
+    VERIFY( false );
+  } catch (const fs::filesystem_error& e) {
+    VERIFY( e.code() == std::errc::file_exists );
+  }
+#endif
+
+  remove_all(dir);
+}
+
 int
 main()
 {
@@ -198,4 +226,5 @@ main()
   test03();
   test04();
   test05();
+  test_pr99290();
 }