1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-20 12:14:55 +00:00

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.
This commit is contained in:
King_DuckZ 2016-03-08 18:58:47 +01:00
parent 7b0189a119
commit 63c703f20d
2 changed files with 20 additions and 12 deletions

View file

@ -23,17 +23,22 @@
#include <boost/range/empty.hpp>
#include <boost/utility/string_ref.hpp>
//#define INDEXER_VERBOSE
#if defined(INDEXER_VERBOSE)
# include <iostream>
#endif
namespace mchlib {
namespace {
void append_to_vec (std::vector<char>& parDest, const TigerHash& parHash, boost::string_ref parString) {
void append_to_vec (std::vector<char>& 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<char>& parDest, boost::string_ref parString) {
void append_to_vec (std::vector<char>& 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<char> 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;
}

View file

@ -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)