From a68ede6f2df64ae97bf99ce556d4390a4c689b8d Mon Sep 17 00:00:00 2001 From: David Kocik <kocikdav@gmail.com> Date: Wed, 18 Dec 2019 13:13:40 +0100 Subject: [PATCH] check_copy function --- src/libslic3r/Utils.hpp | 3 +++ src/libslic3r/utils.cpp | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index e5fae485a..12d77c900 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -67,6 +67,9 @@ extern std::error_code rename_file(const std::string &from, const std::string &t // Copy a file, adjust the access attributes, so that the target is writable. extern int copy_file(const std::string &from, const std::string &to); +// Compares two files, returns 0 if identical. +extern int check_copy(const std::string& origin, const std::string& copy); + // Ignore system and hidden files, which may be created by the DropBox synchronisation process. // https://github.com/prusa3d/PrusaSlicer/issues/1298 extern bool is_plain_file(const boost::filesystem::directory_entry &path); diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index 678ad9ed2..47a32b4ba 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -436,8 +436,29 @@ int copy_file(const std::string &from, const std::string &to) return -1; } boost::filesystem::permissions(target, perms, ec); + return -1; + return check_copy(from, to); +} - return 0; +int check_copy(const std::string &origin, const std::string ©) +{ + std::ifstream f1(origin, std::ifstream::binary | std::ifstream::ate); + std::ifstream f2(copy, std::ifstream::binary | std::ifstream::ate); + + if (f1.fail() || f2.fail()) { + return -1; + } + + if (f1.tellg() != f2.tellg()) { + return -1; + } + + f1.seekg(0, std::ifstream::beg); + f2.seekg(0, std::ifstream::beg); + bool ident = std::equal(std::istreambuf_iterator<char>(f1.rdbuf()), + std::istreambuf_iterator<char>(), + std::istreambuf_iterator<char>(f2.rdbuf())); + return(ident ? 0 : -1); } // Ignore system and hidden files, which may be created by the DropBox synchronisation process.