From 63c703f20d6e39d420ee5358a146434c8efaabdb Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 8 Mar 2016 18:58:47 +0100 Subject: [PATCH] Fix wrong hashes for directories set_listing must sort by abs_path, because the path received by lower_bound() is an absolute path. Inside hash_dir() the path passed to append_to_vec() is really the basename of the current item. This will become more clear in one of the next commits. --- src/machinery/scantask/hashing.cpp | 26 +++++++++++++++++--------- src/machinery/set_listing.cpp | 6 +++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/machinery/scantask/hashing.cpp b/src/machinery/scantask/hashing.cpp index cbe449f..b709b1b 100644 --- a/src/machinery/scantask/hashing.cpp +++ b/src/machinery/scantask/hashing.cpp @@ -23,17 +23,22 @@ #include #include +//#define INDEXER_VERBOSE + +#if defined(INDEXER_VERBOSE) +# include +#endif + namespace mchlib { namespace { - - void append_to_vec (std::vector& parDest, const TigerHash& parHash, boost::string_ref parString) { + void append_to_vec (std::vector& parDest, const TigerHash& parHash, const std::string& parString) { const auto old_size = parDest.size(); parDest.resize(old_size + sizeof(TigerHash) + parString.size()); std::copy(parHash.byte_data, parHash.byte_data + sizeof(TigerHash), parDest.begin() + old_size); std::copy(parString.begin(), parString.end(), parDest.begin() + old_size + sizeof(TigerHash)); } - void append_to_vec (std::vector& parDest, boost::string_ref parString) { + void append_to_vec (std::vector& parDest, const std::string& parString) { const auto old_size = parDest.size(); parDest.resize(old_size + parString.size()); std::copy(parString.begin(), parString.end(), parDest.begin() + old_size); @@ -46,26 +51,29 @@ namespace mchlib { //is a direct child of current entry std::vector dir_blob; #if defined(INDEXER_VERBOSE) - std::cout << "Making initial hash for " << parCurrDir << "...\n"; + std::cout << "Making initial hash for " << parEntry.abs_path << "...\n"; #endif + PathName curr_dir(parEntry.path); for (auto it = parList.begin(); it != parList.end(); ++it) { assert(PathName(parEntry.abs_path) == PathName(it->abs_path).pop_right()); + PathName curr_path(it->path); + const std::string basename = make_relative_path(curr_dir, curr_path).path(); if (it->is_directory) { auto cd_list = MutableSetListingView(it); assert(boost::empty(cd_list) or cd_list.begin()->abs_path != it->abs_path); hash_dir(*it, cd_list, parIgnoreErrors); - append_to_vec(dir_blob, it->hash, it->path); + append_to_vec(dir_blob, it->hash, basename); } else { - append_to_vec(dir_blob, it->path); + append_to_vec(dir_blob, basename); } } tiger_data(dir_blob, parEntry.hash); #if defined(INDEXER_VERBOSE) - std::cout << "Got intermediate hash for dir " << parCurrDir << + std::cout << "Got intermediate hash for dir " << parEntry.abs_path << ": " << tiger_to_string(parEntry.hash) << ' ' << parEntry.mime_type << '\n'; #endif @@ -74,7 +82,7 @@ namespace mchlib { for (auto it = first_file(parList); it != parList.end(); ++it) { assert(not it->is_directory); #if defined(INDEXER_VERBOSE) - std::cout << "Hashing file " << it->abs_path << "..."; + std::cout << "Hashing file " << it->abs_path << "...\n"; #endif //TODO: notify callback try { @@ -93,7 +101,7 @@ namespace mchlib { } #if defined(INDEXER_VERBOSE) - std::cout << "Final hash for dir " << parCurrDir << " is " << tiger_to_string(parEntry.hash) << '\n'; + std::cout << "Final hash for dir " << parEntry.abs_path << " is " << tiger_to_string(parEntry.hash) << '\n'; #endif parEntry.hash_valid = true; } diff --git a/src/machinery/set_listing.cpp b/src/machinery/set_listing.cpp index e4f4c87..69460ce 100644 --- a/src/machinery/set_listing.cpp +++ b/src/machinery/set_listing.cpp @@ -29,14 +29,14 @@ namespace mchlib { //to be made. struct FileRecordDataForSearch { FileRecordDataForSearch ( const char* parPath, uint16_t parLevel, bool parIsDir) : - path(parPath), + abs_path(parPath), level(parLevel), is_directory(parIsDir) { assert(parPath); } - boost::string_ref path; + boost::string_ref abs_path; uint16_t level; bool is_directory; }; @@ -48,7 +48,7 @@ namespace mchlib { return (l.level < r.level) or (l.level == r.level and l.is_directory and not r.is_directory) - or (l.level == r.level and l.is_directory == r.is_directory and l.path < r.path) + or (l.level == r.level and l.is_directory == r.is_directory and l.abs_path < r.abs_path) //sort by directory - parent first, children later //(level == o.level and is_dir and not o.is_dir)