1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-03 14:14:11 +00:00

Fix mimetype retrieval and make code a bit more readable.

This commit is contained in:
King_DuckZ 2016-01-22 09:45:20 +00:00
parent 913cdbd59d
commit 9ee89b815a
3 changed files with 38 additions and 12 deletions

View file

@ -47,12 +47,27 @@ namespace mchlib {
namespace { namespace {
typedef std::vector<FileRecordData>::iterator FileEntryIt; typedef std::vector<FileRecordData>::iterator FileEntryIt;
void append_to_vec (std::vector<char>& parDest, const HashType& parHash, const std::string& parString) {
const auto old_size = parDest.size();
parDest.resize(old_size + sizeof(HashType) + parString.size());
std::copy(parHash.byte_data, parHash.byte_data + sizeof(HashType), parDest.begin() + old_size);
std::copy(parString.begin(), parString.end(), parDest.begin() + old_size + sizeof(HashType));
}
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);
}
void hash_dir (FileEntryIt parEntry, FileEntryIt parBegin, FileEntryIt parEnd, const PathName& parCurrDir, std::function<void(std::size_t)> parNextItemCallback, bool parIgnoreErrors, MimeType& parMime) { void hash_dir (FileEntryIt parEntry, FileEntryIt parBegin, FileEntryIt parEnd, const PathName& parCurrDir, std::function<void(std::size_t)> parNextItemCallback, bool parIgnoreErrors, MimeType& parMime) {
assert(parEntry != parEnd); assert(parEntry != parEnd);
assert(parEntry->is_directory); assert(parEntry->is_directory);
FileRecordData& curr_entry = *parEntry; FileRecordData& curr_entry = *parEntry;
auto& curr_entry_it = parEntry; auto& curr_entry_it = parEntry;
curr_entry.mime_full = parMime.analyze(curr_entry.abs_path);
//Build a blob with the hashes and filenames of every directory that //Build a blob with the hashes and filenames of every directory that
//is a direct child of current entry //is a direct child of current entry
{ {
@ -73,23 +88,17 @@ namespace mchlib {
#if defined(INDEXER_VERBOSE) #if defined(INDEXER_VERBOSE)
std::cout << "Making initial hash for " << parCurrDir << "...\n"; std::cout << "Making initial hash for " << parCurrDir << "...\n";
#endif #endif
curr_entry.mime_full = parMime.analyze(curr_entry.abs_path);
while (parEnd != it_entry and it_entry->level == curr_entry_it->level + 1 and parCurrDir == PathName(it_entry->abs_path).pop_right()) { while (parEnd != it_entry and it_entry->level == curr_entry_it->level + 1 and parCurrDir == PathName(it_entry->abs_path).pop_right()) {
PathName curr_subdir(it_entry->abs_path); PathName curr_subdir(it_entry->abs_path);
const std::string relpath = make_relative_path(parCurrDir, curr_subdir).path();
std::cout << "Adding " << relpath << " to blob\n";
if (it_entry->is_directory) { if (it_entry->is_directory) {
hash_dir(it_entry, parBegin, parEnd, curr_subdir, parNextItemCallback, parIgnoreErrors, parMime); hash_dir(it_entry, parBegin, parEnd, curr_subdir, parNextItemCallback, parIgnoreErrors, parMime);
append_to_vec(dir_blob, it_entry->hash, relpath);
std::string relpath = make_relative_path(parCurrDir, curr_subdir).path();
const auto old_size = dir_blob.size();
dir_blob.resize(old_size + sizeof(HashType) + relpath.size());
std::copy(it_entry->hash.byte_data, it_entry->hash.byte_data + sizeof(HashType), dir_blob.begin() + old_size);
std::copy(relpath.begin(), relpath.end(), dir_blob.begin() + old_size + sizeof(HashType));
} }
else { else {
std::string relpath = make_relative_path(parCurrDir, curr_subdir).path(); append_to_vec(dir_blob, relpath);
const auto old_size = dir_blob.size();
dir_blob.resize(old_size + relpath.size());
std::copy(relpath.begin(), relpath.end(), dir_blob.begin() + old_size);
} }
++it_entry; ++it_entry;
} }

View file

@ -1,3 +1,20 @@
/* Copyright 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "mimetype.hpp" #include "mimetype.hpp"
#include <magic.h> #include <magic.h>
#include <ciso646> #include <ciso646>

View file

@ -1,4 +1,4 @@
/* Copyright 2015, Michele Santullo /* Copyright 2016, Michele Santullo
* This file is part of "dindexer". * This file is part of "dindexer".
* *
* "dindexer" is free software: you can redistribute it and/or modify * "dindexer" is free software: you can redistribute it and/or modify