public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-1570] libstdc++: Make std::filesystem::copy_file work for procfs [PR108178]
@ 2023-06-06 11:37 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-06-06 11:37 UTC (permalink / raw)
To: gcc-cvs, libstdc++-cvs
https://gcc.gnu.org/g:07a0e108247f23fcb919c61595adae143f1ea02a
commit r14-1570-g07a0e108247f23fcb919c61595adae143f1ea02a
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Mar 21 12:29:08 2023 +0000
libstdc++: Make std::filesystem::copy_file work for procfs [PR108178]
The size reported by stat is always zero for some special files such as
those under /proc, which means the current copy_file implementation
thinks there is nothing to copy. Instead of trusting the stat value, try
to read a character from a streambuf and check for EOF.
libstdc++-v3/ChangeLog:
PR libstdc++/108178
* src/filesystem/ops-common.h (do_copy_file): Check for empty
files by trying to read a character.
* testsuite/27_io/filesystem/operations/copy_file_108178.cc:
New test.
Diff:
---
libstdc++-v3/src/filesystem/ops-common.h | 15 ++++++----
.../filesystem/operations/copy_file_108178.cc | 33 ++++++++++++++++++++++
2 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/libstdc++-v3/src/filesystem/ops-common.h b/libstdc++-v3/src/filesystem/ops-common.h
index 7c8d3a694e2..901f166514f 100644
--- a/libstdc++-v3/src/filesystem/ops-common.h
+++ b/libstdc++-v3/src/filesystem/ops-common.h
@@ -618,11 +618,16 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
if (sbout.is_open())
out.fd = -1;
- if (from_st->st_size && !(std::ostream(&sbout) << &sbin))
- {
- ec = std::make_error_code(std::errc::io_error);
- return false;
- }
+ // ostream::operator<<(streambuf*) fails if it extracts no characters,
+ // so don't try to use it for empty files. But from_st->st_size == 0 for
+ // some special files (e.g. procfs, see PR libstdc++/108178) so just try
+ // to read a character to decide whether there is anything to copy or not.
+ if (sbin.sgetc() != char_traits<char>::eof())
+ if (!(std::ostream(&sbout) << &sbin))
+ {
+ ec = std::make_error_code(std::errc::io_error);
+ return false;
+ }
if (!sbout.close() || !sbin.close())
{
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/copy_file_108178.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/copy_file_108178.cc
new file mode 100644
index 00000000000..25135834e21
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/copy_file_108178.cc
@@ -0,0 +1,33 @@
+// { dg-do run { target c++17 } }
+// { dg-require-filesystem-ts "" }
+
+// C++17 30.10.15.4 Copy [fs.op.copy_file]
+
+#include <filesystem>
+#include <fstream>
+#include <unistd.h> // getpid
+#include <testsuite_fs.h>
+#include <testsuite_hooks.h>
+
+namespace fs = std::filesystem;
+
+void
+test_procfs() // PR libstdc++/108178
+{
+ auto pid = ::getpid();
+ std::string from = "/proc/" + std::to_string(pid) + "/status";
+ if (fs::exists(from))
+ {
+ auto to = __gnu_test::nonexistent_path();
+ fs::copy_file(from, to);
+ std::ifstream f(to);
+ VERIFY(f.is_open());
+ VERIFY(f.peek() != std::char_traits<char>::eof());
+ fs::remove(to);
+ }
+}
+
+int main()
+{
+ test_procfs();
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-06-06 11:37 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-06 11:37 [gcc r14-1570] libstdc++: Make std::filesystem::copy_file work for procfs [PR108178] Jonathan Wakely
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).