Skip to content

Commit

Permalink
chore: function for checking if files are identical
Browse files Browse the repository at this point in the history
See mendersoftware/mender-update-orchestrator#90

Ticket: MEN-7171

Signed-off-by: Daniel Skinstad Drabitzius <daniel.drabitzius@northern.tech>
  • Loading branch information
danielskinstad committed Apr 29, 2024
1 parent 973a8cf commit fd30ace
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/common/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ error::Error DeleteRecursively(const string &path);

expected::ExpectedBool IsExecutable(const string &path, const bool warn = true);

expected::ExpectedBool AreFilesIdentical(const string &file_one, const string &file_two);

expected::ExpectedUnorderedSet<string> ListFiles(const string &in, function<bool(string)> matcher);

error::Error CreateDirectory(const string &path);
Expand Down
40 changes: 40 additions & 0 deletions src/common/path/platform/c++17/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <common/io.hpp>
#include <common/path.hpp>

#include <filesystem>
#include <functional>
#include <iterator>
#include <string>
#include <unordered_set>

Expand All @@ -26,6 +29,7 @@ namespace path {

using namespace std;
namespace fs = std::filesystem;
namespace io = mender::common::io;

unordered_map<Perms, fs::perms> perm_map = {
{Perms::Owner_exec, fs::perms::owner_exec},
Expand Down Expand Up @@ -112,6 +116,42 @@ expected::ExpectedBool IsExecutable(const string &file_path, const bool warn) {
}
}

expected::ExpectedBool AreFilesIdentical(const string &file_one, const string &file_two) {
auto file_one_expected_stream = io::OpenIfstream(file_one);
if (!file_one_expected_stream) {
return expected::unexpected(file_one_expected_stream.error());
}
auto file_two_expected_stream = io::OpenIfstream(file_two);
if (!file_two_expected_stream) {
return expected::unexpected(file_two_expected_stream.error());
}

auto &file_one_stream = file_one_expected_stream.value();
auto &file_two_stream = file_two_expected_stream.value();

// Compare file sizes
file_one_stream.seekg(0, ios::end);
file_two_stream.seekg(0, ios::end);
if (file_one_stream.tellg() != file_two_stream.tellg()) {
return false;
}
file_one_stream.seekg(0, ios::beg);
file_two_stream.seekg(0, ios::beg);

string file_one_contents {
istreambuf_iterator<char>(file_one_stream), istreambuf_iterator<char>()};
string file_two_contents {
istreambuf_iterator<char>(file_two_stream), istreambuf_iterator<char>()};

size_t file_one_hash = std::hash<string> {}(file_one_contents);
size_t file_two_hash = std::hash<string> {}(file_two_contents);

if (file_one_hash == file_two_hash) {
return true;
}
return false;
}

error::Error Permissions(const string &file_path, const vector<Perms> perms) {
if (perms.size() == 0) {
return error::NoError;
Expand Down

0 comments on commit fd30ace

Please sign in to comment.