Mercurial > dedupe
diff FileDBLink.cpp @ 13:9463c0c22969
Refactor computeHash into separate function.
| author | Tom Fredrik Blenning Klaussen <bfg@sim.no> |
|---|---|
| date | Fri, 24 Aug 2012 22:57:19 +0200 |
| parents | b5943e4bf676 |
| children | 199fc63c60c1 |
line wrap: on
line diff
--- 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<QSharedPointer<FileDBLink::DBInfo> > FileDBLink::sortOn(const QString& prefix, SORTORDER order, bool extended)
