libstdc++: Fix create_directories to resolve symlinks [PR101510]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 20 Jul 2021 17:15:48 +0000 (18:15 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 20 Jul 2021 19:34:47 +0000 (20:34 +0100)
When filesystem__create_directories checks to see if the path already
exists and resovles to a directory, it uses filesystem::symlink_status,
which means it reports an error if the path is a symlink. It should use
filesystem::status, so that the target directory is detected, and no
error is reported.

Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:

PR libstdc++/101510
* src/c++17/fs_ops.cc (fs::create_directories): Use status
instead of symlink_status.
* src/filesystem/ops.cc (fs::create_directories): Likewise.
* testsuite/27_io/filesystem/operations/create_directories.cc:
* testsuite/27_io/filesystem/operations/create_directory.cc: Do
not test with symlinks on Windows.
* testsuite/experimental/filesystem/operations/create_directories.cc:
* testsuite/experimental/filesystem/operations/create_directory.cc:
Do not test with symlinks on Windows.

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

index cec7644..ceaf029 100644 (file)
@@ -496,7 +496,7 @@ fs::create_directories(const path& p, error_code& ec)
       return false;
     }
 
-  file_status st = symlink_status(p, ec);
+  file_status st = status(p, ec);
   if (is_directory(st))
     return false;
   else if (ec && !status_known(st))
index c400376..7c5b164 100644 (file)
@@ -426,7 +426,7 @@ fs::create_directories(const path& p, error_code& ec) noexcept
       return false;
     }
 
-  file_status st = symlink_status(p, ec);
+  file_status st = status(p, ec);
   if (is_directory(st))
     return false;
   else if (ec && !status_known(st))
index 393d6a5..304c145 100644 (file)
@@ -145,10 +145,33 @@ test03()
   remove_all(p);
 }
 
+void
+test04()
+{
+#if defined(__MINGW32__) || defined(__MINGW64__)
+  // no symlinks
+#else
+  // PR libstdc++/101510
+  // create_directories reports an error if the path is a symlink to a dir
+  std::error_code ec = make_error_code(std::errc::invalid_argument);
+  const auto p = __gnu_test::nonexistent_path() / "";
+  fs::create_directories(p/"dir");
+  auto link = p/"link";
+  fs::create_directory_symlink("dir", link);
+  bool created = fs::create_directories(link, ec);
+  VERIFY( !created );
+  VERIFY( !ec );
+  created = fs::create_directories(link);
+  VERIFY( !created );
+  remove_all(p);
+#endif
+}
+
 int
 main()
 {
   test01();
   test02();
   test03();
+  test04();
 }
index 2566214..afddccf 100644 (file)
@@ -70,6 +70,9 @@ test01()
     VERIFY( e.path1() == f );
   }
 
+#if defined(__MINGW32__) || defined(__MINGW64__)
+  // no symlinks
+#else
   // PR libstdc++/101510 create_directory on an existing symlink to a directory
   fs::create_directory(p/"dir");
   auto link = p/"link";
@@ -80,6 +83,7 @@ test01()
   VERIFY( !ec );
   b = fs::create_directory(link);
   VERIFY( !b );
+#endif
 
   remove_all(p, ec);
 }
index fb26400..fc134ad 100644 (file)
@@ -129,10 +129,33 @@ test03()
   remove_all(p);
 }
 
+void
+test04()
+{
+#if defined(__MINGW32__) || defined(__MINGW64__)
+  // no symlinks
+#else
+  // PR libstdc++/101510
+  // create_directories reports an error if the path is a symlink to a dir
+  std::error_code ec = make_error_code(std::errc::invalid_argument);
+  const auto p = __gnu_test::nonexistent_path() / "";
+  fs::create_directories(p/"dir");
+  auto link = p/"link";
+  fs::create_directory_symlink("dir", link);
+  bool created = fs::create_directories(link, ec);
+  VERIFY( !created );
+  VERIFY( !ec );
+  created = fs::create_directories(link);
+  VERIFY( !created );
+  remove_all(p);
+#endif
+}
+
 int
 main()
 {
   test01();
   test02();
   test03();
+  test04();
 }
index 39f95b6..8e36b76 100644 (file)
@@ -69,6 +69,9 @@ test01()
     VERIFY( e.path1() == f );
   }
 
+#if defined(__MINGW32__) || defined(__MINGW64__)
+  // no symlinks
+#else
   // PR libstdc++/101510 create_directory on an existing symlink to a directory
   fs::create_directory(p/"dir");
   auto link = p/"link";
@@ -79,6 +82,7 @@ test01()
   VERIFY( !ec );
   b = fs::create_directory(link);
   VERIFY( !b );
+#endif
 
   remove_all(p, ec);
 }