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 r12-4070] libstdc++: Add utility for creating std::error_code from OS errors
Date: Fri,  1 Oct 2021 19:39:05 +0000 (GMT)	[thread overview]
Message-ID: <20211001193905.29F763857C65@sourceware.org> (raw)

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

commit r12-4070-gd71476c9df931f3ca674941f1942b03eabea010d
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Feb 10 18:00:00 2021 +0000

    libstdc++: Add utility for creating std::error_code from OS errors
    
    This adds a helper function to encapsulate obtaining an error code for
    errors from OS calls. For Windows we want to use GetLastError() and the
    system error category, but otherwise just use errno and the generic
    error category.
    
    This should not be used to replace existing uses of
    ec.assign(errno, generic_category()) because in those cases we really do
    want to get the value of errno, not a system-specific error. Only the
    cases that currently use GetLastError() are replace by this new
    function.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            * src/filesystem/ops-common.h (last_error): New helper function.
            (filesystem::do_space): Use last_error().
            * src/c++17/fs_ops.cc (fs::absolute, fs::create_hard_link)
            (fs::equivalent, fs::remove, fs::temp_directory_path): Use
            last_error().
            * src/filesystem/ops.cc (fs::create_hard_link)
            (fs::remove, fs::temp_directory_path): Likewise.

Diff:
---
 libstdc++-v3/src/c++17/fs_ops.cc         | 10 +++++-----
 libstdc++-v3/src/filesystem/ops-common.h | 16 ++++++++++++++--
 libstdc++-v3/src/filesystem/ops.cc       |  4 ++--
 3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc
index 2eac9977785..4f3715bbbec 100644
--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -113,7 +113,7 @@ fs::absolute(const path& p, error_code& ec)
   while (len > buf.size());
 
   if (len == 0)
-    ec.assign((int)GetLastError(), std::system_category());
+    ec = __last_system_error();
   else
     {
       buf.resize(len);
@@ -682,7 +682,7 @@ fs::create_hard_link(const path& to, const path& new_hard_link,
   if (CreateHardLinkW(new_hard_link.c_str(), to.c_str(), NULL))
     ec.clear();
   else
-    ec.assign((int)GetLastError(), system_category());
+    ec = __last_system_error();
 #else
   ec = std::make_error_code(std::errc::not_supported);
 #endif
@@ -874,12 +874,12 @@ fs::equivalent(const path& p1, const path& p2, error_code& ec) noexcept
       if (!h1 || !h2)
 	{
 	  if (!h1 && !h2)
-	    ec.assign((int)GetLastError(), system_category());
+	    ec = __last_system_error();
 	  return false;
 	}
       if (!h1.get_info() || !h2.get_info())
 	{
-	  ec.assign((int)GetLastError(), system_category());
+	  ec = __last_system_error();
 	  return false;
 	}
       return h1.info.dwVolumeSerialNumber == h2.info.dwVolumeSerialNumber
@@ -1255,7 +1255,7 @@ fs::remove(const path& p, error_code& ec) noexcept
 	  return true;
 	}
       else if (!ec)
-	ec.assign((int)GetLastError(), system_category());
+	ec = __last_system_error();
     }
   else if (status_known(st))
     ec.clear();
diff --git a/libstdc++-v3/src/filesystem/ops-common.h b/libstdc++-v3/src/filesystem/ops-common.h
index bf26c06b7b5..e999e11b422 100644
--- a/libstdc++-v3/src/filesystem/ops-common.h
+++ b/libstdc++-v3/src/filesystem/ops-common.h
@@ -57,6 +57,18 @@
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  // Get the last OS error (for POSIX this is just errno).
+  inline error_code
+  __last_system_error() noexcept
+  {
+#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
+    return {::GetLastError(), std::system_category()};
+#else
+    return {errno, std::generic_category()};
+#endif
+  }
+
 namespace filesystem
 {
 namespace __gnu_posix
@@ -558,7 +570,7 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
 	ec.clear();
       }
     else
-      ec.assign((int)GetLastError(), std::system_category());
+      ec = std::last_system_error();
 #else
     ec = std::make_error_code(std::errc::not_supported);
 #endif
@@ -583,7 +595,7 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
       } while (len > buf.size());
 
     if (len == 0)
-      ec.assign((int)GetLastError(), std::system_category());
+      ec = __last_system_error();
     else
       ec.clear();
 
diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc
index b0a0f15e98f..cc7117b0cd1 100644
--- a/libstdc++-v3/src/filesystem/ops.cc
+++ b/libstdc++-v3/src/filesystem/ops.cc
@@ -590,7 +590,7 @@ fs::create_hard_link(const path& to, const path& new_hard_link,
   if (CreateHardLinkW(new_hard_link.c_str(), to.c_str(), NULL))
     ec.clear();
   else
-    ec.assign((int)GetLastError(), system_category());
+    ec = __last_system_error();
 #else
   ec = std::make_error_code(std::errc::not_supported);
 #endif
@@ -1062,7 +1062,7 @@ fs::remove(const path& p, error_code& ec) noexcept
 	  return true;
 	}
       else if (!ec)
-	ec.assign((int)GetLastError(), system_category());
+	ec = __last_system_error();
     }
   else if (status_known(st))
     ec.clear();


                 reply	other threads:[~2021-10-01 19:39 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=20211001193905.29F763857C65@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).