# HG changeset patch # User Tom Fredrik Blenning Klaussen # Date 1345841839 -7200 # Node ID 9463c0c2296918baf946e2d7d3beabfdc4a37c96 # Parent 0114be0b5ad40e0b082adcac7be8be148d781cf9 Refactor computeHash into separate function. diff -r 0114be0b5ad4 -r 9463c0c22969 FileDBLink.cpp --- a/FileDBLink.cpp Fri Aug 24 22:56:43 2012 +0200 +++ b/FileDBLink.cpp Fri Aug 24 22:57:19 2012 +0200 @@ -35,12 +35,15 @@ addFile(fileinfo.absoluteFilePath(), fileinfo.size(), fileinfo.lastModified()); } -void FileDBLink::addFile(const QString& path, qint64 size, const QDateTime& lastModified) +QByteArray FileDBLink::computeHash(const QString& path, QCryptographicHash::Algorithm algorithm) { - QCryptographicHash hash( QCryptographicHash::Sha1 ); + const static uint buffersize = 8192; + QCryptographicHash hash(algorithm); QFile file(path); if ( file.open( QIODevice::ReadOnly ) ) { - hash.addData( file.readAll() ); + while(!file.atEnd()){ + hash.addData(file.read(buffersize)); + } } else { QString errorMsg = path + ": " + file.errorString(); @@ -51,10 +54,14 @@ default: throw IOException(errorMsg); } - } - - addFile(path, size, lastModified, hash.result()); + } + return hash.result(); +} + +void FileDBLink::addFile(const QString& path, qint64 size, const QDateTime& lastModified) +{ + addFile(path, size, lastModified, computeHash(path)); } void FileDBLink::updateFile(const QFileInfo& fileinfo) @@ -64,23 +71,7 @@ void FileDBLink::updateFile(const QString& path, qint64 size, const QDateTime& lastModified) { - QCryptographicHash hash( QCryptographicHash::Sha1 ); - QFile file(path); - if ( file.open( QIODevice::ReadOnly ) ) { - hash.addData( file.readAll() ); - } - else { - QString errorMsg = path + ": " + file.errorString(); - - switch (file.error()) { - case QFile::PermissionsError: - throw PermissionException(errorMsg); - default: - throw IOException(errorMsg); - } - } - - updateFile(path, size, lastModified, hash.result()); + updateFile(path, size, lastModified, computeHash(path)); } const QList > FileDBLink::sortOn(const QString& prefix, SORTORDER order, bool extended) diff -r 0114be0b5ad4 -r 9463c0c22969 FileDBLink.hpp --- a/FileDBLink.hpp Fri Aug 24 22:56:43 2012 +0200 +++ b/FileDBLink.hpp Fri Aug 24 22:57:19 2012 +0200 @@ -89,6 +89,7 @@ static QSharedPointer computedValue(const QSharedPointer& info, const QList >&); + static QByteArray computeHash(const QString& path, QCryptographicHash::Algorithm = QCryptographicHash::Sha1); public: enum DBStatus { NONE = 0, MTIME_DIFFERENT, SAME};