Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update copy.h #633

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions include/chia/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,26 @@
#include <cstdio>
#include <cstdint>

#ifdef __linux__
#include <filesystem>
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");
Expand Down Expand Up @@ -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;
}

Expand Down