comparison 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
comparison
equal deleted inserted replaced
12:0114be0b5ad4 13:9463c0c22969
33 void FileDBLink::addFile(const QFileInfo& fileinfo) 33 void FileDBLink::addFile(const QFileInfo& fileinfo)
34 { 34 {
35 addFile(fileinfo.absoluteFilePath(), fileinfo.size(), fileinfo.lastModified()); 35 addFile(fileinfo.absoluteFilePath(), fileinfo.size(), fileinfo.lastModified());
36 } 36 }
37 37
38 void FileDBLink::addFile(const QString& path, qint64 size, const QDateTime& lastModified) 38 QByteArray FileDBLink::computeHash(const QString& path, QCryptographicHash::Algorithm algorithm)
39 { 39 {
40 QCryptographicHash hash( QCryptographicHash::Sha1 ); 40 const static uint buffersize = 8192;
41 QCryptographicHash hash(algorithm);
41 QFile file(path); 42 QFile file(path);
42 if ( file.open( QIODevice::ReadOnly ) ) { 43 if ( file.open( QIODevice::ReadOnly ) ) {
43 hash.addData( file.readAll() ); 44 while(!file.atEnd()){
45 hash.addData(file.read(buffersize));
46 }
44 } 47 }
45 else { 48 else {
46 QString errorMsg = path + ": " + file.errorString(); 49 QString errorMsg = path + ": " + file.errorString();
47 50
48 switch (file.error()) { 51 switch (file.error()) {
49 case QFile::PermissionsError: 52 case QFile::PermissionsError:
50 throw PermissionException(errorMsg); 53 throw PermissionException(errorMsg);
51 default: 54 default:
52 throw IOException(errorMsg); 55 throw IOException(errorMsg);
53 } 56 }
54 } 57 }
55 58 return hash.result();
56 addFile(path, size, lastModified, hash.result()); 59 }
57 60
61
62 void FileDBLink::addFile(const QString& path, qint64 size, const QDateTime& lastModified)
63 {
64 addFile(path, size, lastModified, computeHash(path));
58 } 65 }
59 66
60 void FileDBLink::updateFile(const QFileInfo& fileinfo) 67 void FileDBLink::updateFile(const QFileInfo& fileinfo)
61 { 68 {
62 updateFile(fileinfo.absoluteFilePath(), fileinfo.size(), fileinfo.lastModified()); 69 updateFile(fileinfo.absoluteFilePath(), fileinfo.size(), fileinfo.lastModified());
63 } 70 }
64 71
65 void FileDBLink::updateFile(const QString& path, qint64 size, const QDateTime& lastModified) 72 void FileDBLink::updateFile(const QString& path, qint64 size, const QDateTime& lastModified)
66 { 73 {
67 QCryptographicHash hash( QCryptographicHash::Sha1 ); 74 updateFile(path, size, lastModified, computeHash(path));
68 QFile file(path);
69 if ( file.open( QIODevice::ReadOnly ) ) {
70 hash.addData( file.readAll() );
71 }
72 else {
73 QString errorMsg = path + ": " + file.errorString();
74
75 switch (file.error()) {
76 case QFile::PermissionsError:
77 throw PermissionException(errorMsg);
78 default:
79 throw IOException(errorMsg);
80 }
81 }
82
83 updateFile(path, size, lastModified, hash.result());
84 } 75 }
85 76
86 const QList<QSharedPointer<FileDBLink::DBInfo> > FileDBLink::sortOn(const QString& prefix, SORTORDER order, bool extended) 77 const QList<QSharedPointer<FileDBLink::DBInfo> > FileDBLink::sortOn(const QString& prefix, SORTORDER order, bool extended)
87 { 78 {
88 QList<QSharedPointer<DBInfo> > list = (extended) ? computedValues(prefix) : values(prefix); 79 QList<QSharedPointer<DBInfo> > list = (extended) ? computedValues(prefix) : values(prefix);