Skip to content

Commit

Permalink
If tmp2 and final files are in same filesystem, rename() not copy() (#…
Browse files Browse the repository at this point in the history
…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 <cross+chia@distal.com>
  • Loading branch information
cross and Chris Ross committed Jan 29, 2022
1 parent b1973a3 commit 0502f34
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/plotter_disk.hpp
Expand Up @@ -23,6 +23,7 @@

#include <math.h>
#include <stdio.h>
#include <sys/stat.h>

#include <algorithm>
#include <fstream>
Expand Down Expand Up @@ -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<const char *>(tmp_2_filename.c_str()), &tmp2_stat);
if (rc == 0)
rc = ::stat(reinterpret_cast<const char *>(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
Expand Down

0 comments on commit 0502f34

Please sign in to comment.