[gcc r14-12167] libstdc++: Fix error reporting for filesystem::rename on Windows [PR122726]
Jonathan Wakely
redi@gcc.gnu.org
Tue Nov 18 12:48:00 GMT 2025
https://gcc.gnu.org/g:19da7880d50eee3ae8f71c353871af961e8ca801
commit r14-12167-g19da7880d50eee3ae8f71c353871af961e8ca801
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Mon Nov 17 21:01:33 2025 +0000
libstdc++: Fix error reporting for filesystem::rename on Windows [PR122726]
Use the __last_system_error() function and the system_category to
convert the Windows error to a generic one.
libstdc++-v3/ChangeLog:
PR libstdc++/122726
* src/filesystem/ops-common.h [_GLIBCXX_FILESYSTEM_IS_WINDOWS]
(rename): Use __last_system_error to set errno accurately.
* testsuite/27_io/filesystem/operations/rename.cc: Test
error_code matches errc::no_such_file_or_directory.
(cherry picked from commit 77278e0292cd23f0d1ddbb5e6e18064e119bdd6f)
Diff:
---
libstdc++-v3/src/filesystem/ops-common.h | 5 +----
libstdc++-v3/testsuite/27_io/filesystem/operations/rename.cc | 10 ++++++++++
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/libstdc++-v3/src/filesystem/ops-common.h b/libstdc++-v3/src/filesystem/ops-common.h
index 1be8d754d6fa..05d59d503e9d 100644
--- a/libstdc++-v3/src/filesystem/ops-common.h
+++ b/libstdc++-v3/src/filesystem/ops-common.h
@@ -159,10 +159,7 @@ namespace __gnu_posix
if (MoveFileExW(oldname, newname,
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
return 0;
- if (GetLastError() == ERROR_ACCESS_DENIED)
- errno = EACCES;
- else
- errno = EIO;
+ errno = std::__last_system_error().default_error_condition().value();
return -1;
}
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/rename.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/rename.cc
index 9ee53d367497..c1e657bf3682 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/operations/rename.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/rename.cc
@@ -170,10 +170,20 @@ test_directories()
fs::remove_all(dir, ec);
}
+void
+test_pr122726()
+{
+ std::error_code ec;
+ const auto nonesuch = __gnu_test::nonexistent_path();
+ fs::rename(nonesuch, "new-name", ec);
+ VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
+}
+
int
main()
{
test01();
test_symlinks();
test_directories();
+ test_pr122726();
}
More information about the Libstdc++-cvs
mailing list