From 4b1448c39242df3ed36dd7a6d6d8b1c2abc34ec2 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Wed, 20 Oct 2021 13:25:53 -0400 Subject: [PATCH 1/3] If the tmp2 and final files are in the same filesystem, rename() instead of copy() into place. --- 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..b6c5f93c4 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(tmp_2_filename.c_str(), &tmp2_stat); + if (rc == 0) + rc = ::stat(final_filename.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 From 8d5d3359e4961bb493970bea4d2c127443676a0b Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Thu, 21 Oct 2021 01:08:25 -0400 Subject: [PATCH 2/3] Final file doesn't exist yet, need to stat the parent directory --- src/plotter_disk.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plotter_disk.hpp b/src/plotter_disk.hpp index b6c5f93c4..e7b044114 100644 --- a/src/plotter_disk.hpp +++ b/src/plotter_disk.hpp @@ -375,7 +375,7 @@ class DiskPlotter { int rc; rc = ::stat(tmp_2_filename.c_str(), &tmp2_stat); if (rc == 0) - rc = ::stat(final_filename.c_str(), &final_stat); + rc = ::stat(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); From 14e0b5ad43cb94b93351ec344a1cd449319dacb5 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Thu, 21 Oct 2021 22:47:12 -0400 Subject: [PATCH 3/3] gcc is able to convert that value for stat(2), but Windows needs a reinterpret_cast. Add that. --- src/plotter_disk.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plotter_disk.hpp b/src/plotter_disk.hpp index e7b044114..0e9bd4e0f 100644 --- a/src/plotter_disk.hpp +++ b/src/plotter_disk.hpp @@ -373,9 +373,9 @@ class DiskPlotter { std::error_code ec; struct stat tmp2_stat, final_stat; int rc; - rc = ::stat(tmp_2_filename.c_str(), &tmp2_stat); + rc = ::stat(reinterpret_cast(tmp_2_filename.c_str()), &tmp2_stat); if (rc == 0) - rc = ::stat(final_filename.parent_path().c_str(), &final_stat); + 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);