Filesystem Utilities: Add Checksum calculation methods.

This commit is contained in:
Klaas Freitag 2015-05-12 15:48:13 +02:00
parent b36ff1ed1d
commit 67d38bc87b
5 changed files with 116 additions and 1 deletions

View File

@ -165,13 +165,13 @@ endif()
find_package(Sphinx)
find_package(PdfLatex)
find_package(SQLite3 3.8.0 REQUIRED)
# On some OS, we want to use our own, not the system sqlite
if (USE_OUR_OWN_SQLITE3)
include_directories(BEFORE ${SQLITE3_INCLUDE_DIR})
endif()
find_package(ZLIB)
configure_file(config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

View File

@ -19,6 +19,8 @@
#cmakedefine APPLICATION_EXECUTABLE "@APPLICATION_EXECUTABLE@"
#cmakedefine APPLICATION_UPDATE_URL "@APPLICATION_UPDATE_URL@"
#cmakedefine ZLIB_FOUND @ZLIB_FOUND@
#cmakedefine SYSCONFDIR "@SYSCONFDIR@"
#cmakedefine DATADIR "@DATADIR@"

View File

@ -141,6 +141,11 @@ if(NEON_FOUND)
endif()
endif()
if(ZLIB_FOUND)
list(APPEND libsync_LINK_TARGETS ${ZLIB_LIBRARIES})
include_directories(${ZLIB_INCLUDE_DIRS})
endif(ZLIB_FOUND)
add_library(${synclib_NAME} SHARED ${libsync_SRCS} ${syncMoc})
GENERATE_EXPORT_HEADER( ${synclib_NAME}
BASE_NAME ${synclib_NAME}

View File

@ -11,6 +11,7 @@
* for more details.
*/
#include "config.h"
#include "filesystem.h"
#include "utility.h"
@ -18,6 +19,13 @@
#include <QFileInfo>
#include <QCoreApplication>
#include <QDebug>
#include <QCryptographicHash>
#include <QFuture>
#include <qtconcurrentrun.h>
#ifdef ZLIB_FOUND
#include <zlib.h>
#endif
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <qabstractfileengine.h>
@ -394,4 +402,90 @@ QString FileSystem::fileSystemForPath(const QString & path)
}
#endif
QByteArray FileSystem::calcMd5Worker( const QString& filename )
{
QByteArray arr;
QCryptographicHash crypto( QCryptographicHash::Md5 );
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
QByteArray data;
while (!file.atEnd()) {
data = file.read(1024*1024*10);
crypto.addData(data);
}
arr = crypto.result().toHex();
}
return arr;
}
QByteArray FileSystem::calcSha1Worker( const QString& filename )
{
QByteArray arr;
QCryptographicHash crypto( QCryptographicHash::Sha1 );
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
QByteArray data;
while (!file.atEnd()) {
data = file.read(1024*1024*10);
crypto.addData(data);
}
arr = crypto.result().toHex();
}
return arr;
}
#ifdef ZLIB_FOUND
QByteArray FileSystem::calcAdler32Worker( const QString& filename )
{
unsigned int adler = adler32(0L, Z_NULL, 0);
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
QByteArray data;
while (!file.atEnd()) {
data = file.read(1024*1024*10);
adler = adler32(adler, (const Bytef*) data.data(), data.size());
}
}
return QString::number( adler, 16 ).toUtf8();
}
#endif
QByteArray FileSystem::calcMd5( const QString& fileName )
{
QFuture<QByteArray> f1 = QtConcurrent::run(calcMd5Worker, fileName );
f1.waitForFinished();
const QByteArray md5 = f1.result();
return md5;
}
QByteArray FileSystem::calcSha1( const QString& fileName )
{
QFuture<QByteArray> f1 = QtConcurrent::run(calcSha1Worker, fileName );
f1.waitForFinished();
const QByteArray sha1 = f1.result();
return sha1;
}
#ifdef ZLIB_FOUND
QByteArray FileSystem::calcAdler32( const QString& fileName )
{
QFuture<QByteArray> f1 = QtConcurrent::run(calcAdler32Worker, fileName );
f1.waitForFinished();
const QByteArray checksum = f1.result();
return checksum;
}
#endif
} // namespace OCC

View File

@ -121,4 +121,18 @@ bool openAndSeekFileSharedRead(QFile* file, QString* error, qint64 seek);
QString fileSystemForPath(const QString & path);
#endif
/**
* Calculate the checksum of a file in a worker thread. Each function waits
* until the calculation is finished.
*/
QByteArray calcMd5( const QString& fileName );
QByteArray calcSha1( const QString& fileName );
QByteArray calcAdler32( const QString& fileName );
#ifdef ZLIB_FOUND
QByteArray calcAdler32Worker( const QString& filename );
#endif
QByteArray calcSha1Worker( const QString& filename );
QByteArray calcMd5Worker( const QString& filename );
}}