Hi, can you please tell me what is the expected behaviour of such C++ snippet? ~~~…
- User
Hi, can you please tell me what is the expected behaviour of such C++ snippet?
codestd::vector<FileDiff> compare_directories(const fs::path &dir1, const fs::path &dir2) { std::vector<FileDiff> differences; std::error_code ec; // For no-throw overloads fs::recursive_directory_iterator it1(dir1, ec), end1; fs::recursive_directory_iterator it2(dir2, ec), end2; auto advance_it = [&](fs::recursive_directory_iterator &it, const fs::recursive_directory_iterator &end) { while (it != end && !it->is_regular_file()) { ++it; } }; advance_it(it1, end1); advance_it(it2, end2); while (it1 != end1 || it2 != end2) { // Compare paths bool end1_reached = it1 == end1; bool end2_reached = it2 == end2; fs::path path1 = end1_reached ? fs::path() : it1->path(); fs::path path2 = end2_reached ? fs::path() : it2->path(); if (!end1_reached && (end2_reached || path1 < path2)) { // Path1 is either a file not present in dir2, or path2 has reached the end. differences.push_back({path1, fs::path(), "", ""}); ++it1; } else if (!end2_reached && (end1_reached || path2 < path1)) { // Path2 is either a file not present in dir1, or path1 has reached the end. differences.push_back({fs::path(), path2, "", ""}); ++it2; } else { // Both paths are equal, compare file content by hashing if (fs::file_size(path1, ec) != fs::file_size(path2, ec)) { differences.push_back({path1, path2, "Size differs", ""}); } else { std::string hash1 = hash_file(path1); std::string hash2 = hash_file(path2); if (hash1 != hash2) { differences.push_back({path1, path2, hash1, hash2}); } } ++it1; ++it2; } advance_it(it1, end1); advance_it(it2, end2); } return differences; } - Assistant
- User
This code is doubling the checks, namely it returns twice the same file that is missing in both folder. Do you manage to find such error?
- Assistant