diff --git a/include/chia/copy.h b/include/chia/copy.h index d5054b4a..d4b9dee6 100644 --- a/include/chia/copy.h +++ b/include/chia/copy.h @@ -17,10 +17,26 @@ #include #include +#ifdef __linux__ + #include + namespace fs = std::filesystem; +#endif inline -uint64_t copy_file(const std::string& src_path, const std::string& dst_path) +uint64_t copy_file(const std::string& src_path, const std::string& dst_path, const std::string& dst2_path) { + #ifdef __linux__ + const uint64_t plotsize = 109000000000 // rough plot size placeholder, later pass real size through + fs::space_info tmp = fs::space(dst_path); + if(tmp.available < plotsize) { + fs::space_info tmp2 = fs::space(dst_path2); + if(tmp2.available < plotsize) { + throw std::runtime_error("All destinations have run out of disk space"); + } + dst_path = dst2_path; + throw std::runtime_error("Destination does not have enough available disk space left, switching to alternate destination"); + } + #endif FILE* src = fopen(src_path.c_str(), "rb"); if(!src) { throw std::runtime_error("fopen() failed"); @@ -49,19 +65,21 @@ uint64_t copy_file(const std::string& src_path, const std::string& dst_path) } inline -uint64_t final_copy(const std::string& src_path, const std::string& dst_path) +uint64_t final_copy(const std::string& src_path, const std::string& dst_path, const std::string& dst2_path) { if(src_path == dst_path) { return 0; } const std::string tmp_dst_path = dst_path + ".tmp"; + const std::string tmp_dst2_path = dst2_path + ".tmp"; uint64_t total_bytes = 0; if(rename(src_path.c_str(), tmp_dst_path.c_str())) { // try manual copy - total_bytes = copy_file(src_path, tmp_dst_path); + total_bytes = copy_file(src_path, tmp_dst_path, tmp_dst2_path); } remove(src_path.c_str()); rename(tmp_dst_path.c_str(), dst_path.c_str()); + rename(tmp_dst2_path.c_str(), dst2_path.c_str()); return total_bytes; }