From 0502f346b984bf80ca7f5d7c600fe3467483d9dd Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Sat, 29 Jan 2022 17:57:59 -0500 Subject: [PATCH] If tmp2 and final files are in same filesystem, rename() not copy() (#314) * If the tmp2 and final files are in the same filesystem, rename() instead of copy() into place. * Final file doesn't exist yet, need to stat the parent directory * gcc is able to convert that value for stat(2), but Windows needs a reinterpret_cast. Add that. Co-authored-by: Chris Ross --- src/plotter_disk.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/plotter_disk.hpp b/src/plotter_disk.hpp index b848021eb..0e9bd4e0f 100644 --- a/src/plotter_disk.hpp +++ b/src/plotter_disk.hpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -370,7 +371,13 @@ class DiskPlotter { Timer copy; do { std::error_code ec; - if (tmp_2_filename.parent_path() == final_filename.parent_path()) { + struct stat tmp2_stat, final_stat; + int rc; + rc = ::stat(reinterpret_cast(tmp_2_filename.c_str()), &tmp2_stat); + if (rc == 0) + rc = ::stat(reinterpret_cast(final_filename.parent_path().c_str()), &final_stat); + if ((rc == 0 && tmp2_stat.st_dev == final_stat.st_dev) || + tmp_2_filename.parent_path() == final_filename.parent_path()) { fs::rename(tmp_2_filename, final_filename, ec); if (ec.value() != 0) { std::cout << "Could not rename " << tmp_2_filename << " to " << final_filename