public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r9-9669] libstdc++: Fix create_directories to resolve symlinks [PR101510]
Date: Wed, 11 Aug 2021 22:50:18 +0000 (GMT)	[thread overview]
Message-ID: <20210811225018.EE8F53865C28@sourceware.org> (raw)

https://gcc.gnu.org/g:f8ac77533951d79d4e6a65841aa30293b2f11fdd

commit r9-9669-gf8ac77533951d79d4e6a65841aa30293b2f11fdd
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jul 20 18:15:48 2021 +0100

    libstdc++: Fix create_directories to resolve symlinks [PR101510]
    
    When filesystem__create_directories checks to see if the path already
    exists and resolves 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:
            Check symlink to existing directory.
            * testsuite/27_io/filesystem/operations/create_directory.cc: Do
            not test with symlinks on Windows.
            * testsuite/experimental/filesystem/operations/create_directories.cc:
            Check symlink to existing directory.
            * testsuite/experimental/filesystem/operations/create_directory.cc:
            Do not test with symlinks on Windows.
    
    (cherry picked from commit 124eaa50e0a34f5f89572c1aa812c50979da58fc)

Diff:
---
 libstdc++-v3/src/c++17/fs_ops.cc                   |  2 +-
 libstdc++-v3/src/filesystem/ops.cc                 |  2 +-
 .../filesystem/operations/create_directories.cc    | 23 ++++++++++++++++++++++
 .../filesystem/operations/create_directory.cc      |  4 ++++
 .../filesystem/operations/create_directories.cc    | 23 ++++++++++++++++++++++
 .../filesystem/operations/create_directory.cc      |  4 ++++
 6 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc
index 0796853187d..f37d5497075 100644
--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -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))
diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc
index 4859b6e7fa6..ea2693622de 100644
--- a/libstdc++-v3/src/filesystem/ops.cc
+++ b/libstdc++-v3/src/filesystem/ops.cc
@@ -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))
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directories.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directories.cc
index c4411dfc1e7..7dd9c209b40 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directories.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directories.cc
@@ -146,10 +146,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();
 }
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directory.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directory.cc
index 86c57ed90db..9555280e8c6 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directory.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/create_directory.cc
@@ -71,6 +71,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";
@@ -81,6 +84,7 @@ test01()
   VERIFY( !ec );
   b = fs::create_directory(link);
   VERIFY( !b );
+#endif
 
   remove_all(p, ec);
 }
diff --git a/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc
index b6909b630d4..6cfa7800860 100644
--- a/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc
+++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc
@@ -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();
 }
diff --git a/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directory.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directory.cc
index 9ad32558662..20701ae4fcc 100644
--- a/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directory.cc
+++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directory.cc
@@ -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);
 }


                 reply	other threads:[~2021-08-11 22:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210811225018.EE8F53865C28@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).