copy file into .tmp and then rename if correct

This commit is contained in:
David Kocik 2019-12-19 15:36:00 +01:00
parent dafd768b34
commit 31b134bfcc
2 changed files with 34 additions and 18 deletions

View file

@ -65,6 +65,7 @@ extern std::string normalize_utf8_nfc(const char *src);
extern std::error_code rename_file(const std::string &from, const std::string &to); extern std::error_code rename_file(const std::string &from, const std::string &to);
// Copy a file, adjust the access attributes, so that the target is writable. // Copy a file, adjust the access attributes, so that the target is writable.
int copy_file_inner(const std::string &from, const std::string &to);
extern int copy_file(const std::string &from, const std::string &to, const bool with_check = false); extern int copy_file(const std::string &from, const std::string &to, const bool with_check = false);
// Compares two files, returns 0 if identical. // Compares two files, returns 0 if identical.

View file

@ -417,7 +417,7 @@ std::error_code rename_file(const std::string &from, const std::string &to)
#endif #endif
} }
int copy_file(const std::string &from, const std::string &to, const bool with_check) int copy_file_inner(const std::string& from, const std::string& to)
{ {
const boost::filesystem::path source(from); const boost::filesystem::path source(from);
const boost::filesystem::path target(to); const boost::filesystem::path target(to);
@ -436,7 +436,22 @@ int copy_file(const std::string &from, const std::string &to, const bool with_ch
return -1; return -1;
} }
boost::filesystem::permissions(target, perms, ec); boost::filesystem::permissions(target, perms, ec);
return (with_check ? check_copy(from, to) : 0); return 0;
}
int copy_file(const std::string &from, const std::string &to, const bool with_check)
{
std::string to_temp = to + ".tmp";
int ret_val = copy_file_inner(from,to_temp);
if(ret_val == 0 && with_check)
{
ret_val = check_copy(from, to_temp);
if (ret_val == 0)
{
rename_file(to_temp, to);
}
}
return ret_val;
} }
int check_copy(const std::string &origin, const std::string &copy) int check_copy(const std::string &origin, const std::string &copy)