added auto-update of files being hashed

This commit is contained in:
csoler 2024-03-12 21:39:05 +01:00
parent 5e3434396c
commit 7ef81a37ff
5 changed files with 266 additions and 190 deletions

View File

@ -45,19 +45,25 @@ RsCollection::RsCollection(QObject *parent)
RsCollection::RsCollection(const RsFileTree& ft) RsCollection::RsCollection(const RsFileTree& ft)
{ {
mFileTree = std::unique_ptr<RsFileTree>(new RsFileTree(ft)); mFileTree = std::unique_ptr<RsFileTree>(new RsFileTree(ft));
for(uint64_t i=0;i<mFileTree->numFiles();++i)
mHashes.insert(std::make_pair(mFileTree->fileData(i).hash,i ));
} }
RsCollection::RsCollection(const std::vector<DirDetails>& file_infos,FileSearchFlags flags, QObject *parent) RsCollection::RsCollection(const std::vector<DirDetails>& file_infos,FileSearchFlags flags, QObject *parent)
: QObject(parent), mFileTree(new RsFileTree) : QObject(parent), mFileTree(new RsFileTree)
{ {
if(! ( (flags & RS_FILE_HINTS_LOCAL) || (flags & RS_FILE_HINTS_REMOTE))) if(! ( (flags & RS_FILE_HINTS_LOCAL) || (flags & RS_FILE_HINTS_REMOTE)))
{ {
std::cerr << "(EE) Wrong flags passed to RsCollection constructor. Please fix the code!" << std::endl; std::cerr << "(EE) Wrong flags passed to RsCollection constructor. Please fix the code!" << std::endl;
return ; return ;
} }
for(uint32_t i = 0;i<file_infos.size();++i) for(uint32_t i = 0;i<file_infos.size();++i)
recursAddElements(mFileTree->root(),file_infos[i],flags) ; recursAddElements(mFileTree->root(),file_infos[i],flags) ;
for(uint64_t i=0;i<mFileTree->numFiles();++i)
mHashes.insert(std::make_pair(mFileTree->fileData(i).hash,i ));
} }
RsCollection::~RsCollection() RsCollection::~RsCollection()
@ -67,88 +73,88 @@ RsCollection::~RsCollection()
void RsCollection::downloadFiles() const void RsCollection::downloadFiles() const
{ {
#ifdef TODO_COLLECTION #ifdef TODO_COLLECTION
// print out the element names of all elements that are direct children // print out the element names of all elements that are direct children
// of the outermost element. // of the outermost element.
QDomElement docElem = _xml_doc.documentElement(); QDomElement docElem = _xml_doc.documentElement();
std::vector<ColFileInfo> colFileInfos ; std::vector<ColFileInfo> colFileInfos ;
recursCollectColFileInfos(docElem,colFileInfos,QString(),false) ; recursCollectColFileInfos(docElem,colFileInfos,QString(),false) ;
RsCollectionDialog(_fileName, colFileInfos, false).exec() ; RsCollectionDialog(_fileName, colFileInfos, false).exec() ;
#endif #endif
} }
void RsCollection::autoDownloadFiles() const void RsCollection::autoDownloadFiles() const
{ {
#ifdef TODO_COLLECTION #ifdef TODO_COLLECTION
QDomElement docElem = _xml_doc.documentElement(); QDomElement docElem = _xml_doc.documentElement();
std::vector<ColFileInfo> colFileInfos; std::vector<ColFileInfo> colFileInfos;
recursCollectColFileInfos(docElem,colFileInfos,QString(),false); recursCollectColFileInfos(docElem,colFileInfos,QString(),false);
QString dlDir = QString::fromUtf8(rsFiles->getDownloadDirectory().c_str()); QString dlDir = QString::fromUtf8(rsFiles->getDownloadDirectory().c_str());
foreach(ColFileInfo colFileInfo, colFileInfos) foreach(ColFileInfo colFileInfo, colFileInfos)
{ {
autoDownloadFiles(colFileInfo, dlDir); autoDownloadFiles(colFileInfo, dlDir);
} }
#endif #endif
} }
void RsCollection::autoDownloadFiles(ColFileInfo colFileInfo, QString dlDir) const void RsCollection::autoDownloadFiles(ColFileInfo colFileInfo, QString dlDir) const
{ {
if (!colFileInfo.filename_has_wrong_characters) if (!colFileInfo.filename_has_wrong_characters)
{ {
QString cleanPath = dlDir + colFileInfo.path ; QString cleanPath = dlDir + colFileInfo.path ;
std::cout << "making directory " << cleanPath.toStdString() << std::endl; std::cout << "making directory " << cleanPath.toStdString() << std::endl;
if(!QDir(QApplication::applicationDirPath()).mkpath(cleanPath)) if(!QDir(QApplication::applicationDirPath()).mkpath(cleanPath))
std::cerr << "Unable to make path: " + cleanPath.toStdString() << std::endl; std::cerr << "Unable to make path: " + cleanPath.toStdString() << std::endl;
if (colFileInfo.type==DIR_TYPE_FILE) if (colFileInfo.type==DIR_TYPE_FILE)
rsFiles->FileRequest(colFileInfo.name.toUtf8().constData(), rsFiles->FileRequest(colFileInfo.name.toUtf8().constData(),
RsFileHash(colFileInfo.hash.toStdString()), RsFileHash(colFileInfo.hash.toStdString()),
colFileInfo.size, colFileInfo.size,
cleanPath.toUtf8().constData(), cleanPath.toUtf8().constData(),
RS_FILE_REQ_ANONYMOUS_ROUTING, RS_FILE_REQ_ANONYMOUS_ROUTING,
std::list<RsPeerId>()); std::list<RsPeerId>());
} }
foreach(ColFileInfo colFileInfoChild, colFileInfo.children) foreach(ColFileInfo colFileInfoChild, colFileInfo.children)
{ {
autoDownloadFiles(colFileInfoChild, dlDir); autoDownloadFiles(colFileInfoChild, dlDir);
} }
} }
static QString purifyFileName(const QString& input,bool& bad) static QString purifyFileName(const QString& input,bool& bad)
{ {
static const QString bad_chars = "/\\\"*:?<>|" ; static const QString bad_chars = "/\\\"*:?<>|" ;
bad = false ; bad = false ;
QString output = input ; QString output = input ;
for(int i=0;i<output.length();++i) for(int i=0;i<output.length();++i)
for(int j=0;j<bad_chars.length();++j) for(int j=0;j<bad_chars.length();++j)
if(output[i] == bad_chars[j]) if(output[i] == bad_chars[j])
{ {
output[i] = '_' ; output[i] = '_' ;
bad = true ; bad = true ;
} }
return output ; return output ;
} }
void RsCollection::merge_in(const QString& fname,uint64_t size,const RsFileHash& hash) void RsCollection::merge_in(const QString& fname,uint64_t size,const RsFileHash& hash)
{ {
mFileTree->addFile(mFileTree->root(),fname.toStdString(),hash,size); mFileTree->addFile(mFileTree->root(),fname.toStdString(),hash,size);
#ifdef TO_REMOVE #ifdef TO_REMOVE
ColFileInfo info ; ColFileInfo info ;
info.type = DIR_TYPE_FILE ; info.type = DIR_TYPE_FILE ;
info.name = fname ; info.name = fname ;
info.size = size ; info.size = size ;
info.hash = QString::fromStdString(hash.toStdString()) ; info.hash = QString::fromStdString(hash.toStdString()) ;
recursAddElements(_xml_doc,info,_root) ; recursAddElements(_xml_doc,info,_root) ;
#endif #endif
} }
void RsCollection::merge_in(const RsFileTree& tree) void RsCollection::merge_in(const RsFileTree& tree)
@ -175,56 +181,56 @@ void RsCollection::recursMergeTree(RsFileTree::DirIndex parent,const RsFileTree&
#ifdef TO_REMOVE #ifdef TO_REMOVE
void RsCollection::recursCollectColFileInfos(const QDomElement& e,std::vector<ColFileInfo>& colFileInfos,const QString& current_path, bool bad_chars_in_parent) const void RsCollection::recursCollectColFileInfos(const QDomElement& e,std::vector<ColFileInfo>& colFileInfos,const QString& current_path, bool bad_chars_in_parent) const
{ {
QDomNode n = e.firstChild() ; QDomNode n = e.firstChild() ;
#ifdef COLLECTION_DEBUG #ifdef COLLECTION_DEBUG
std::cerr << "Parsing element " << e.tagName().toStdString() << std::endl; std::cerr << "Parsing element " << e.tagName().toStdString() << std::endl;
#endif #endif
while(!n.isNull()) while(!n.isNull())
{ {
QDomElement ee = n.toElement(); // try to convert the node to an element. QDomElement ee = n.toElement(); // try to convert the node to an element.
#ifdef COLLECTION_DEBUG #ifdef COLLECTION_DEBUG
std::cerr << " Seeing child " << ee.tagName().toStdString() << std::endl; std::cerr << " Seeing child " << ee.tagName().toStdString() << std::endl;
#endif #endif
if(ee.tagName() == QString("File")) if(ee.tagName() == QString("File"))
{ {
ColFileInfo newChild ; ColFileInfo newChild ;
newChild.hash = ee.attribute(QString("sha1")) ; newChild.hash = ee.attribute(QString("sha1")) ;
bool bad_chars_detected = false ; bool bad_chars_detected = false ;
newChild.name = purifyFileName(ee.attribute(QString("name")), bad_chars_detected) ; newChild.name = purifyFileName(ee.attribute(QString("name")), bad_chars_detected) ;
newChild.filename_has_wrong_characters = bad_chars_detected || bad_chars_in_parent ; newChild.filename_has_wrong_characters = bad_chars_detected || bad_chars_in_parent ;
newChild.size = ee.attribute(QString("size")).toULongLong() ; newChild.size = ee.attribute(QString("size")).toULongLong() ;
newChild.path = current_path ; newChild.path = current_path ;
newChild.type = DIR_TYPE_FILE ; newChild.type = DIR_TYPE_FILE ;
colFileInfos.push_back(newChild) ; colFileInfos.push_back(newChild) ;
} }
else if(ee.tagName() == QString("Directory")) else if(ee.tagName() == QString("Directory"))
{ {
ColFileInfo newParent ; ColFileInfo newParent ;
bool bad_chars_detected = false ; bool bad_chars_detected = false ;
QString cleanDirName = purifyFileName(ee.attribute(QString("name")),bad_chars_detected) ; QString cleanDirName = purifyFileName(ee.attribute(QString("name")),bad_chars_detected) ;
newParent.name=cleanDirName; newParent.name=cleanDirName;
newParent.filename_has_wrong_characters = bad_chars_detected || bad_chars_in_parent ; newParent.filename_has_wrong_characters = bad_chars_detected || bad_chars_in_parent ;
newParent.size = 0; newParent.size = 0;
newParent.path = current_path ; newParent.path = current_path ;
newParent.type = DIR_TYPE_DIR ; newParent.type = DIR_TYPE_DIR ;
recursCollectColFileInfos(ee,newParent.children,current_path + "/" + cleanDirName, bad_chars_in_parent || bad_chars_detected) ; recursCollectColFileInfos(ee,newParent.children,current_path + "/" + cleanDirName, bad_chars_in_parent || bad_chars_detected) ;
uint32_t size = newParent.children.size(); uint32_t size = newParent.children.size();
for(uint32_t i=0;i<size;++i) for(uint32_t i=0;i<size;++i)
{ {
const ColFileInfo &colFileInfo = newParent.children[i]; const ColFileInfo &colFileInfo = newParent.children[i];
newParent.size +=colFileInfo.size ; newParent.size +=colFileInfo.size ;
} }
colFileInfos.push_back(newParent) ; colFileInfos.push_back(newParent) ;
} }
n = n.nextSibling() ; n = n.nextSibling() ;
} }
} }
#endif #endif
@ -234,82 +240,82 @@ void RsCollection::recursAddElements(RsFileTree::DirIndex parent, const DirDetai
if (dd.type == DIR_TYPE_FILE) if (dd.type == DIR_TYPE_FILE)
mFileTree->addFile(parent,dd.name,dd.hash,dd.size); mFileTree->addFile(parent,dd.name,dd.hash,dd.size);
else if (dd.type == DIR_TYPE_DIR) else if (dd.type == DIR_TYPE_DIR)
{ {
RsFileTree::DirIndex new_dir_index = mFileTree->addDirectory(parent,dd.name); RsFileTree::DirIndex new_dir_index = mFileTree->addDirectory(parent,dd.name);
for(uint32_t i=0;i<dd.children.size();++i) for(uint32_t i=0;i<dd.children.size();++i)
{ {
if (!dd.children[i].ref) if (!dd.children[i].ref)
continue; continue;
DirDetails subDirDetails; DirDetails subDirDetails;
if (!rsFiles->RequestDirDetails(dd.children[i].ref, subDirDetails, flags)) if (!rsFiles->RequestDirDetails(dd.children[i].ref, subDirDetails, flags))
continue; continue;
recursAddElements(new_dir_index,subDirDetails,flags) ; recursAddElements(new_dir_index,subDirDetails,flags) ;
} }
} }
} }
#ifdef TO_REMOVE #ifdef TO_REMOVE
void RsCollection::recursAddElements(QDomDocument& doc,const ColFileInfo& colFileInfo,QDomElement& e) const void RsCollection::recursAddElements(QDomDocument& doc,const ColFileInfo& colFileInfo,QDomElement& e) const
{ {
if (colFileInfo.type == DIR_TYPE_FILE) if (colFileInfo.type == DIR_TYPE_FILE)
{ {
QDomElement f = doc.createElement("File") ; QDomElement f = doc.createElement("File") ;
f.setAttribute(QString("name"),colFileInfo.name) ; f.setAttribute(QString("name"),colFileInfo.name) ;
f.setAttribute(QString("sha1"),colFileInfo.hash) ; f.setAttribute(QString("sha1"),colFileInfo.hash) ;
f.setAttribute(QString("size"),QString::number(colFileInfo.size)) ; f.setAttribute(QString("size"),QString::number(colFileInfo.size)) ;
e.appendChild(f) ; e.appendChild(f) ;
} }
else if (colFileInfo.type == DIR_TYPE_DIR) else if (colFileInfo.type == DIR_TYPE_DIR)
{ {
QDomElement d = doc.createElement("Directory") ; QDomElement d = doc.createElement("Directory") ;
d.setAttribute(QString("name"),colFileInfo.name) ; d.setAttribute(QString("name"),colFileInfo.name) ;
for (std::vector<ColFileInfo>::const_iterator it = colFileInfo.children.begin(); it != colFileInfo.children.end(); ++it) for (std::vector<ColFileInfo>::const_iterator it = colFileInfo.children.begin(); it != colFileInfo.children.end(); ++it)
recursAddElements(doc,(*it),d) ; recursAddElements(doc,(*it),d) ;
e.appendChild(d) ; e.appendChild(d) ;
} }
} }
void RsCollection::recursAddElements( void RsCollection::recursAddElements(
QDomDocument& doc, const RsFileTree& ft, uint32_t index, QDomDocument& doc, const RsFileTree& ft, uint32_t index,
QDomElement& e ) const QDomElement& e ) const
{ {
std::vector<uint64_t> subdirs; std::vector<uint64_t> subdirs;
std::vector<RsFileTree::FileData> subfiles ; std::vector<RsFileTree::FileData> subfiles ;
std::string name; std::string name;
if(!ft.getDirectoryContent(name, subdirs, subfiles, index)) return; if(!ft.getDirectoryContent(name, subdirs, subfiles, index)) return;
QDomElement d = doc.createElement("Directory") ; QDomElement d = doc.createElement("Directory") ;
d.setAttribute(QString("name"),QString::fromUtf8(name.c_str())) ; d.setAttribute(QString("name"),QString::fromUtf8(name.c_str())) ;
e.appendChild(d) ; e.appendChild(d) ;
for (uint32_t i=0;i<subdirs.size();++i) for (uint32_t i=0;i<subdirs.size();++i)
recursAddElements(doc,ft,subdirs[i],d) ; recursAddElements(doc,ft,subdirs[i],d) ;
for(uint32_t i=0;i<subfiles.size();++i) for(uint32_t i=0;i<subfiles.size();++i)
{ {
QDomElement f = doc.createElement("File") ; QDomElement f = doc.createElement("File") ;
f.setAttribute(QString("name"),QString::fromUtf8(subfiles[i].name.c_str())) ; f.setAttribute(QString("name"),QString::fromUtf8(subfiles[i].name.c_str())) ;
f.setAttribute(QString("sha1"),QString::fromStdString(subfiles[i].hash.toStdString())) ; f.setAttribute(QString("sha1"),QString::fromStdString(subfiles[i].hash.toStdString())) ;
f.setAttribute(QString("size"),QString::number(subfiles[i].size)) ; f.setAttribute(QString("size"),QString::number(subfiles[i].size)) ;
d.appendChild(f) ; d.appendChild(f) ;
} }
} }
static void showErrorBox(const QString& fileName, const QString& error) static void showErrorBox(const QString& fileName, const QString& error)
{ {
QMessageBox mb(QMessageBox::Warning, QObject::tr("Failed to process collection file"), QObject::tr("The collection file %1 could not be opened.\nReported error is: \n\n%2").arg(fileName).arg(error), QMessageBox::Ok); QMessageBox mb(QMessageBox::Warning, QObject::tr("Failed to process collection file"), QObject::tr("The collection file %1 could not be opened.\nReported error is: \n\n%2").arg(fileName).arg(error), QMessageBox::Ok);
mb.exec(); mb.exec();
} }
#endif #endif
@ -441,25 +447,25 @@ bool RsCollection::checkFile(const QString& fileName, RsCollectionErrorCode& err
#ifdef TO_REMOVE #ifdef TO_REMOVE
bool RsCollection::load(QWidget *parent) bool RsCollection::load(QWidget *parent)
{ {
QString fileName; QString fileName;
if (!misc::getOpenFileName(parent, RshareSettings::LASTDIR_EXTRAFILE, QApplication::translate("RsCollectionFile", "Open collection file"), QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")", fileName)) if (!misc::getOpenFileName(parent, RshareSettings::LASTDIR_EXTRAFILE, QApplication::translate("RsCollectionFile", "Open collection file"), QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")", fileName))
return false; return false;
std::cerr << "Got file name: " << fileName.toStdString() << std::endl; std::cerr << "Got file name: " << fileName.toStdString() << std::endl;
return load(fileName, true); return load(fileName, true);
} }
#endif #endif
bool RsCollection::save(const QString& fileName) const bool RsCollection::save(const QString& fileName) const
{ {
QFile file(fileName); QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) if (!file.open(QIODevice::WriteOnly))
{ {
std::cerr << "Cannot write to file " << fileName.toStdString() << " !!" << std::endl; std::cerr << "Cannot write to file " << fileName.toStdString() << " !!" << std::endl;
return false; return false;
} }
QDomDocument xml_doc ; QDomDocument xml_doc ;
QDomElement root = xml_doc.createElement("RsCollection"); QDomElement root = xml_doc.createElement("RsCollection");
@ -472,12 +478,12 @@ bool RsCollection::save(const QString& fileName) const
xml_doc.appendChild(root); xml_doc.appendChild(root);
QTextStream stream(&file) ; QTextStream stream(&file) ;
stream.setCodec("UTF-8") ; stream.setCodec("UTF-8") ;
stream << xml_doc.toString() ; stream << xml_doc.toString() ;
file.close(); file.close();
return true; return true;
} }
bool RsCollection::recursParseXml(QDomDocument& doc,const QDomNode& e,const RsFileTree::DirIndex parent) bool RsCollection::recursParseXml(QDomDocument& doc,const QDomNode& e,const RsFileTree::DirIndex parent)
@ -545,6 +551,10 @@ bool RsCollection::recursParseXml(QDomDocument& doc,const QDomNode& e,const RsFi
n = n.nextSibling() ; n = n.nextSibling() ;
} }
mHashes.clear();
for(uint64_t i=0;i<mFileTree->numFiles();++i)
mHashes.insert(std::make_pair(mFileTree->fileData(i).hash,i ));
return true; return true;
} }
bool RsCollection::recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFileTree::DirData& dd) const bool RsCollection::recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFileTree::DirData& dd) const
@ -581,16 +591,16 @@ bool RsCollection::recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFi
#ifdef TO_REMOVE #ifdef TO_REMOVE
bool RsCollection::save(QWidget *parent) const bool RsCollection::save(QWidget *parent) const
{ {
QString fileName; QString fileName;
if(!misc::getSaveFileName(parent, RshareSettings::LASTDIR_EXTRAFILE, QApplication::translate("RsCollectionFile", "Create collection file"), QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")", fileName)) if(!misc::getSaveFileName(parent, RshareSettings::LASTDIR_EXTRAFILE, QApplication::translate("RsCollectionFile", "Create collection file"), QApplication::translate("RsCollectionFile", "Collection files") + " (*." + RsCollection::ExtensionString + ")", fileName))
return false; return false;
if (!fileName.endsWith("." + RsCollection::ExtensionString)) if (!fileName.endsWith("." + RsCollection::ExtensionString))
fileName += "." + RsCollection::ExtensionString ; fileName += "." + RsCollection::ExtensionString ;
std::cerr << "Got file name: " << fileName.toStdString() << std::endl; std::cerr << "Got file name: " << fileName.toStdString() << std::endl;
return save(fileName); return save(fileName);
} }
@ -606,38 +616,59 @@ qulonglong RsCollection::size()
return mFileTree->totalFileSize(); return mFileTree->totalFileSize();
#ifdef TO_REMOVE #ifdef TO_REMOVE
QDomElement docElem = _xml_doc.documentElement(); QDomElement docElem = _xml_doc.documentElement();
std::vector<ColFileInfo> colFileInfos; std::vector<ColFileInfo> colFileInfos;
recursCollectColFileInfos(docElem, colFileInfos, QString(),false); recursCollectColFileInfos(docElem, colFileInfos, QString(),false);
uint64_t size = 0; uint64_t size = 0;
for (uint32_t i = 0; i < colFileInfos.size(); ++i) { for (uint32_t i = 0; i < colFileInfos.size(); ++i) {
size += colFileInfos[i].size; size += colFileInfos[i].size;
} }
return size; return size;
#endif #endif
} }
bool RsCollection::isCollectionFile(const QString &fileName) bool RsCollection::isCollectionFile(const QString &fileName)
{ {
QString ext = QFileInfo(fileName).suffix().toLower(); QString ext = QFileInfo(fileName).suffix().toLower();
return (ext == RsCollection::ExtensionString); return (ext == RsCollection::ExtensionString);
} }
void RsCollection::updateHashes(const std::map<RsFileHash,RsFileHash>& old_to_new_hashes)
{
for(auto it:old_to_new_hashes)
{
auto fit = mHashes.find(it.first);
if(fit == mHashes.end())
{
RsErr() << "Could not find hash " << it.first << " in RsCollection list of hashes. This is a bug." ;
return ;
}
const auto& fd(mFileTree->fileData(fit->second));
if(fd.hash != it.first)
{
RsErr() << "Mismatch hash for file " << fd.name << " (found " << fd.hash << " instead of " << it.first << ") in RsCollection list of hashes. This is a bug." ;
return ;
}
mFileTree->updateFile(fit->second,fd.name,it.second,fd.size);
}
}
#ifdef TO_REMOVE #ifdef TO_REMOVE
void RsCollection::saveColl(std::vector<ColFileInfo> colFileInfos, const QString &fileName) void RsCollection::saveColl(std::vector<ColFileInfo> colFileInfos, const QString &fileName)
{ {
QDomElement root = _xml_doc.elementsByTagName("RsCollection").at(0).toElement(); QDomElement root = _xml_doc.elementsByTagName("RsCollection").at(0).toElement();
while (root.childNodes().count()>0) root.removeChild(root.firstChild()); while (root.childNodes().count()>0) root.removeChild(root.firstChild());
for(uint32_t i = 0;i<colFileInfos.size();++i) for(uint32_t i = 0;i<colFileInfos.size();++i)
recursAddElements(_xml_doc,colFileInfos[i],root) ; recursAddElements(_xml_doc,colFileInfos[i],root) ;
_saved=save(fileName); _saved=save(fileName);
} }
#endif #endif

View File

@ -103,6 +103,7 @@ public:
static bool isCollectionFile(const QString& fileName); static bool isCollectionFile(const QString& fileName);
void updateHashes(const std::map<RsFileHash,RsFileHash>& old_to_new_hashes);
private: private:
bool recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFileTree::DirData& dd) const; bool recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFileTree::DirData& dd) const;
@ -130,6 +131,8 @@ private:
void autoDownloadFiles(ColFileInfo colFileInfo, QString dlDir) const ; void autoDownloadFiles(ColFileInfo colFileInfo, QString dlDir) const ;
std::unique_ptr<RsFileTree> mFileTree; std::unique_ptr<RsFileTree> mFileTree;
std::map<RsFileHash,RsFileTree::FileIndex> mHashes; // used to efficiently update files being hashed
#ifdef TO_REMOVE #ifdef TO_REMOVE
QDomDocument _xml_doc ; QDomDocument _xml_doc ;
QString _fileName ; QString _fileName ;

View File

@ -23,6 +23,7 @@
#include "RsCollection.h" #include "RsCollection.h"
#include "util/misc.h" #include "util/misc.h"
#include "util/rsdir.h"
#include <QCheckBox> #include <QCheckBox>
#include <QDateTime> #include <QDateTime>
@ -738,7 +739,7 @@ void RsCollectionDialog::addSelectionRecursive()
addSelection(true); addSelection(true);
} }
static void recursBuildFileTree(const QString& path,RsFileTree& tree,RsFileTree::DirIndex dir_index,bool recursive) static void recursBuildFileTree(const QString& path,RsFileTree& tree,RsFileTree::DirIndex dir_index,bool recursive,QSet<QString>& paths_to_hash)
{ {
QFileInfo fileInfo = path; QFileInfo fileInfo = path;
@ -746,17 +747,28 @@ static void recursBuildFileTree(const QString& path,RsFileTree& tree,RsFileTree:
{ {
auto di = tree.addDirectory(dir_index,fileInfo.fileName().toUtf8().constData()); auto di = tree.addDirectory(dir_index,fileInfo.fileName().toUtf8().constData());
QDir dirParent = fileInfo.absoluteFilePath(); if(recursive)
dirParent.setFilter(QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot); {
QFileInfoList childrenList = dirParent.entryInfoList(); QDir dirParent = fileInfo.absoluteFilePath();
dirParent.setFilter(QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot);
QFileInfoList childrenList = dirParent.entryInfoList();
for(QFileInfo f:childrenList) for(QFileInfo f:childrenList)
recursBuildFileTree(f.absoluteFilePath(),tree,di,recursive); recursBuildFileTree(f.absoluteFilePath(),tree,di,recursive,paths_to_hash);
}
} }
else else
{ {
#warning TODO: compute the hash // Here we use a temporary hash that serves two purposes:
tree.addFile(dir_index,fileInfo.fileName().toUtf8().constData(),RsFileHash(),fileInfo.size()); // 1 - identify the file in the RsFileTree of the collection so that we can update its hash when calculated
// 2 - mark the file as being processed
// The hash s is computed to be the hash of the path of the file. The collection must take care of multiple instances.
Sha1CheckSum s = RsDirUtil::sha1sum((uint8_t*)(fileInfo.filePath().toUtf8().constData()),fileInfo.filePath().toUtf8().size());
tree.addFile(dir_index,fileInfo.fileName().toUtf8().constData(),s,fileInfo.size());
paths_to_hash.insert(fileInfo.filePath().toUtf8().constData());
} }
} }
/** /**
@ -768,7 +780,6 @@ static void recursBuildFileTree(const QString& path,RsFileTree& tree,RsFileTree:
*/ */
void RsCollectionDialog::addSelection(bool recursive) void RsCollectionDialog::addSelection(bool recursive)
{ {
QStringList fileToHash;
QMap<QString, QString > dirToAdd; QMap<QString, QString > dirToAdd;
int count=0;//to not scan all items on list .count() int count=0;//to not scan all items on list .count()
@ -776,11 +787,13 @@ void RsCollectionDialog::addSelection(bool recursive)
mCollectionModel->preMods(); mCollectionModel->preMods();
QSet<QString> paths_to_hash; // sha1sum of the paths to hash
foreach (QModelIndex index, milSelectionList) foreach (QModelIndex index, milSelectionList)
if(index.column()==0) //Get only FileName if(index.column()==0) //Get only FileName
{ {
RsFileTree tree; RsFileTree tree;
recursBuildFileTree(_dirModel->filePath(_tree_proxyModel->mapToSource(index)),tree,tree.root(),recursive); recursBuildFileTree(_dirModel->filePath(_tree_proxyModel->mapToSource(index)),tree,tree.root(),recursive,paths_to_hash);
mCollection->merge_in(tree); mCollection->merge_in(tree);
@ -852,9 +865,18 @@ void RsCollectionDialog::addSelection(bool recursive)
// Process Files once all done // Process Files once all done
ui._hashBox->addAttachments(fileToHash,RS_FILE_REQ_ANONYMOUS_ROUTING /*, 0*/); ui._hashBox->addAttachments(fileToHash,RS_FILE_REQ_ANONYMOUS_ROUTING /*, 0*/);
#endif #endif
// std::map<Sha1CheckSum,QString> paths_and_hashes;
// for(auto path:paths_to_hash)
// paths_and_hashes.insert(std::make_pair(RsDirUtil::sha1sum((uint8_t*)path.toUtf8().constData(),path.toUtf8().size()),path));
// mCollectionModel->addFilesToHash(paths_and_hashes);
mCollectionModel->postMods(); mCollectionModel->postMods();
ui._hashBox->addAttachments(QStringList(paths_to_hash.begin(),paths_to_hash.end()),RS_FILE_REQ_ANONYMOUS_ROUTING /*, 0*/);
} }
#ifdef TO_REMOVE
/** /**
* @brief RsCollectionDialog::addAllChild: Add children to RsCollection * @brief RsCollectionDialog::addAllChild: Add children to RsCollection
* @param fileInfoParent: Parent's QFileInfo to scan * @param fileInfoParent: Parent's QFileInfo to scan
@ -910,6 +932,7 @@ bool RsCollectionDialog::addAllChild(QFileInfo &fileInfoParent
} }
return true; return true;
} }
#endif
/** /**
* @brief RsCollectionDialog::remove: Remove selected Items in RSCollection * @brief RsCollectionDialog::remove: Remove selected Items in RSCollection
@ -1186,6 +1209,7 @@ void RsCollectionDialog::makeDir()
*/ */
void RsCollectionDialog::fileHashingFinished(QList<HashedFile> hashedFiles) void RsCollectionDialog::fileHashingFinished(QList<HashedFile> hashedFiles)
{ {
#ifdef TO_REMOVE
std::cerr << "RsCollectionDialog::fileHashingFinished() started." << std::endl; std::cerr << "RsCollectionDialog::fileHashingFinished() started." << std::endl;
QString message; QString message;
@ -1211,8 +1235,26 @@ void RsCollectionDialog::fileHashingFinished(QList<HashedFile> hashedFiles)
_newColFileInfos.push_back(colFileInfo); _newColFileInfos.push_back(colFileInfo);
#endif #endif
} }
#endif
// build a map of old-hash to new-hash for the hashed files, so that it can be passed to the mCollection for update
std::cerr << "RsCollectionDialog::fileHashingFinished message : " << message.toStdString() << std::endl; std::map<RsFileHash,RsFileHash> old_to_new_hashes;
for(auto f:hashedFiles)
{
auto it = mFilesBeingHashed.find(f.filepath);
if(it == mFilesBeingHashed.end())
{
RsErr() << "Could not find hash-ID correspondence for path " << f.filepath.toUtf8().constData() << ". This is a bug." << std::endl;
continue;
}
old_to_new_hashes.insert(std::make_pair(it->second,f.hash));
mFilesBeingHashed.erase(it);
}
mCollectionModel->preMods();
mCollection->updateHashes(old_to_new_hashes);
mCollectionModel->postMods();
updateList(); updateList();
} }

View File

@ -18,6 +18,7 @@
* * * *
*******************************************************************************/ *******************************************************************************/
#include <set>
#include "ui_RsCollectionDialog.h" #include "ui_RsCollectionDialog.h"
#include "RsCollection.h" #include "RsCollection.h"
#include "RsCollectionModel.h" #include "RsCollectionModel.h"
@ -90,12 +91,12 @@ private:
bool addChild(QTreeWidgetItem *parent, const std::vector<ColFileInfo> &child); bool addChild(QTreeWidgetItem *parent, const std::vector<ColFileInfo> &child);
bool removeItem(QTreeWidgetItem *item, bool &removeOnlyFile) ; bool removeItem(QTreeWidgetItem *item, bool &removeOnlyFile) ;
void saveChild(QTreeWidgetItem *parentItem, ColFileInfo *parentInfo = NULL); void saveChild(QTreeWidgetItem *parentItem, ColFileInfo *parentInfo = NULL);
#endif
void addSelection(bool recursive) ;
bool addAllChild(QFileInfo &fileInfoParent bool addAllChild(QFileInfo &fileInfoParent
, QMap<QString, QString > &dirToAdd , QMap<QString, QString > &dirToAdd
, QStringList &fileToHash , QStringList &fileToHash
, int &count); , int &count);
#endif
void addSelection(bool recursive) ;
Ui::RsCollectionDialog ui; Ui::RsCollectionDialog ui;
QString _fileName ; QString _fileName ;
@ -110,4 +111,6 @@ private:
RsCollectionModel *mCollectionModel; RsCollectionModel *mCollectionModel;
RsCollection *mCollection; RsCollection *mCollection;
std::map<QString,RsFileHash> mFilesBeingHashed; // map of file path vs. temporary ID used for the file while hashing
}; };

View File

@ -252,9 +252,9 @@ bool RsCollectionModel::setData(const QModelIndex& index,const QVariant& value,i
if(role==Qt::CheckStateRole && convertInternalIdToIndex(index.internalId(), e)) if(role==Qt::CheckStateRole && convertInternalIdToIndex(index.internalId(), e))
{ {
//#ifdef DEBUG_COLLECTION_MODEL #ifdef DEBUG_COLLECTION_MODEL
std::cerr << "Setting check state of item " << index << " to " << value.toBool() << std::endl; std::cerr << "Setting check state of item " << index << " to " << value.toBool() << std::endl;
//#endif #endif
RsFileTree::DirIndex dir_index ; RsFileTree::DirIndex dir_index ;
if(e.is_file) if(e.is_file)
@ -370,7 +370,6 @@ QVariant RsCollectionModel::checkStateRole(const EntryIndex& i,int col) const
{ {
if(i.is_file) if(i.is_file)
{ {
std::cerr<< "entry is file, checkstate = " << (int)mFileInfos[i.index].is_checked << std::endl;
if(mFileInfos[i.index].is_checked) if(mFileInfos[i.index].is_checked)
return QVariant(Qt::Checked); return QVariant(Qt::Checked);
else else
@ -378,8 +377,6 @@ QVariant RsCollectionModel::checkStateRole(const EntryIndex& i,int col) const
} }
else else
{ {
std::cerr<< "entry is dir, checkstate = " << (int)mDirInfos[i.index].check_state << std::endl;
switch(mDirInfos[i.index].check_state) switch(mDirInfos[i.index].check_state)
{ {
case SELECTED: return QVariant::fromValue((int)Qt::Checked); case SELECTED: return QVariant::fromValue((int)Qt::Checked);