]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add utility for creating std::error_code from OS errors
authorJonathan Wakely <jwakely@redhat.com>
Wed, 10 Feb 2021 18:00:00 +0000 (18:00 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 4 Oct 2023 11:10:03 +0000 (12:10 +0100)
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.

(cherry picked from commit d71476c9df931f3ca674941f1942b03eabea010d)

libstdc++-v3/src/c++17/fs_ops.cc
libstdc++-v3/src/filesystem/ops-common.h
libstdc++-v3/src/filesystem/ops.cc

index 4155b4d64b9c01b77079caab8205211a9de1731d..4e2990d476d318aa1cfcb825ee89d4e5bb95abdf 100644 (file)
@@ -117,7 +117,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);
@@ -690,7 +690,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
@@ -882,12 +882,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
@@ -1263,7 +1263,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();
index 54bafff2a9cf14795ada3c0282bcc250b9a73b85..bf1017ea4418a7b98de234179d9076e2a0670512 100644 (file)
 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
@@ -557,7 +569,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
@@ -582,7 +594,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();
 
index b7df8d99b6cfbca740296f7588eb776a7d41007a..6616d81cf776b168b091f5d37e6402cb8cf378e1 100644 (file)
@@ -598,7 +598,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
@@ -1070,7 +1070,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();