Skip to content

Commit

Permalink
Merge pull request #4532 from ckamm/content-checksum
Browse files Browse the repository at this point in the history
Enable content checksums #4375
  • Loading branch information
ckamm committed Mar 14, 2016
2 parents 39a95d3 + d6d3502 commit 25baa99
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/libsync/checksums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ bool downloadChecksumEnabled()
return enabled;
}

QByteArray contentChecksumType()
{
static QByteArray type = qgetenv("OWNCLOUD_CONTENT_CHECKSUM_TYPE");
if (!type.isNull()) { // can set to "" to disable checksumming
return type;
}
return "SHA1";
}

ComputeChecksum::ComputeChecksum(QObject* parent)
: QObject(parent)
{
Expand Down
4 changes: 4 additions & 0 deletions src/libsync/checksums.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ bool uploadChecksumEnabled();
/// Checks OWNCLOUD_DISABLE_CHECKSUM_DOWNLOAD
bool downloadChecksumEnabled();

/// Checks OWNCLOUD_CONTENT_CHECKSUM_TYPE (default: SHA1)
QByteArray contentChecksumType();


/**
* Computes the checksum of a file.
* \ingroup libsync
Expand Down
39 changes: 32 additions & 7 deletions src/libsync/propagatedownload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,11 @@ void PropagateDownloadFileQNAM::slotGetFinished()
}

// Do checksum validation for the download. If there is no checksum header, the validator
// will also emit the validated() signal to continue the flow in slot downloadFinished()
// will also emit the validated() signal to continue the flow in slot transmissionChecksumValidated()
// as this is (still) also correct.
ValidateChecksumHeader *validator = new ValidateChecksumHeader(this);
connect(validator, SIGNAL(validated(QByteArray,QByteArray)),
SLOT(downloadFinished(QByteArray,QByteArray)));
SLOT(transmissionChecksumValidated(QByteArray,QByteArray)));
connect(validator, SIGNAL(validationFailed(QString)),
SLOT(slotChecksumFail(QString)));
auto checksumHeader = job->reply()->rawHeader(checkSumHeaderC);
Expand Down Expand Up @@ -649,13 +649,38 @@ static void preserveGroupOwnership(const QString& fileName, const QFileInfo& fi)
}
} // end namespace

void PropagateDownloadFileQNAM::downloadFinished(const QByteArray& transportChecksumType,
const QByteArray& transportChecksum)

void PropagateDownloadFileQNAM::transmissionChecksumValidated(const QByteArray &checksumType, const QByteArray &checksum)
{
// by default, reuse the transport checksum as content checksum
_item->_contentChecksum = transportChecksum;
_item->_contentChecksumType = transportChecksumType;
const auto theContentChecksumType = contentChecksumType();

// Reuse transmission checksum as content checksum.
//
// We could do this more aggressively and accept both MD5 and SHA1
// instead of insisting on the exactly correct checksum type.
if (theContentChecksumType == checksumType || theContentChecksumType.isEmpty()) {
return contentChecksumComputed(checksumType, checksum);
}

// Compute the content checksum.
auto computeChecksum = new ComputeChecksum(this);
computeChecksum->setChecksumType(theContentChecksumType);

connect(computeChecksum, SIGNAL(done(QByteArray,QByteArray)),
SLOT(contentChecksumComputed(QByteArray,QByteArray)));
computeChecksum->start(_tmpFile.fileName());
}

void PropagateDownloadFileQNAM::contentChecksumComputed(const QByteArray &checksumType, const QByteArray &checksum)
{
_item->_contentChecksum = checksum;
_item->_contentChecksumType = checksumType;

downloadFinished();
}

void PropagateDownloadFileQNAM::downloadFinished()
{
QString fn = _propagator->getFilePath(_item->_file);

// In case of file name clash, report an error
Expand Down
5 changes: 3 additions & 2 deletions src/libsync/propagatedownload.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ class PropagateDownloadFileQNAM : public PropagateItemJob {
private slots:
void slotGetFinished();
void abort() Q_DECL_OVERRIDE;
void downloadFinished(const QByteArray& transportChecksumType = QByteArray(),
const QByteArray &transportChecksum = QByteArray());
void transmissionChecksumValidated(const QByteArray& checksumType, const QByteArray& checksum);
void contentChecksumComputed(const QByteArray& checksumType, const QByteArray& checksum);
void downloadFinished();
void slotDownloadProgress(qint64,qint64);
void slotChecksumFail( const QString& errMsg );

Expand Down
13 changes: 4 additions & 9 deletions src/libsync/propagateupload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,23 +219,18 @@ void PropagateUploadFileQNAM::slotComputeContentChecksum()

_stopWatch.start();

QByteArray contentChecksumType;
// We currently only do content checksums for the particular .eml case
// This should be done more generally in the future!
if (filePath.endsWith(QLatin1String(".eml"), Qt::CaseInsensitive)) {
contentChecksumType = "MD5";
}
QByteArray checksumType = contentChecksumType();

// Maybe the discovery already computed the checksum?
if (_item->_contentChecksumType == contentChecksumType
if (_item->_contentChecksumType == checksumType
&& !_item->_contentChecksum.isEmpty()) {
slotComputeTransmissionChecksum(contentChecksumType, _item->_contentChecksum);
slotComputeTransmissionChecksum(checksumType, _item->_contentChecksum);
return;
}

// Compute the content checksum.
auto computeChecksum = new ComputeChecksum(this);
computeChecksum->setChecksumType(contentChecksumType);
computeChecksum->setChecksumType(checksumType);

connect(computeChecksum, SIGNAL(done(QByteArray,QByteArray)),
SLOT(slotComputeTransmissionChecksum(QByteArray,QByteArray)));
Expand Down

0 comments on commit 25baa99

Please sign in to comment.