size_t count = from_st->st_size;
#if defined _GLIBCXX_USE_SENDFILE && ! defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
- off_t offset = 0;
- ssize_t n = ::sendfile(out.fd, in.fd, &offset, count);
- if (n < 0 && errno != ENOSYS && errno != EINVAL)
+ ssize_t n = 0;
+ if (count != 0)
{
- ec.assign(errno, std::generic_category());
- return false;
- }
- if ((size_t)n == count)
- {
- if (!out.close() || !in.close())
+ off_t offset = 0;
+ n = ::sendfile(out.fd, in.fd, &offset, count);
+ if (n < 0 && errno != ENOSYS && errno != EINVAL)
{
ec.assign(errno, std::generic_category());
return false;
}
- ec.clear();
- return true;
+ if ((size_t)n == count)
+ {
+ if (!out.close() || !in.close())
+ {
+ ec.assign(errno, std::generic_category());
+ return false;
+ }
+ ec.clear();
+ return true;
+ }
+ else if (n > 0)
+ count -= n;
}
- else if (n > 0)
- count -= n;
#endif // _GLIBCXX_USE_SENDFILE
using std::ios;
}
#endif
- if (count && !(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())
{
ec.assign(errno, std::generic_category());
--- /dev/null
+// { 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();
+}