Skip to content

Commit

Permalink
Revert Added config parameter that allows to bypass checksum validati…
Browse files Browse the repository at this point in the history
…on failure.

Signed-off-by: allexzander <blackslayer4@gmail.com>
  • Loading branch information
allexzander authored and Felix Weilbach (Rebase PR Action) committed Jul 28, 2021
1 parent 76308d5 commit 8122c63
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 73 deletions.
7 changes: 3 additions & 4 deletions src/common/checksums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ ComputeChecksum *ValidateChecksumHeader::prepareStart(const QByteArray &checksum

if (!parseChecksumHeader(checksumHeader, &_expectedChecksumType, &_expectedChecksum)) {
qCWarning(lcChecksums) << "Checksum header malformed:" << checksumHeader;
emit validationFailed(tr("The checksum header is malformed."), {}, {}, _filePath);
emit validationFailed(tr("The checksum header is malformed."));
return nullptr;
}

Expand All @@ -341,7 +341,6 @@ ComputeChecksum *ValidateChecksumHeader::prepareStart(const QByteArray &checksum

void ValidateChecksumHeader::start(const QString &filePath, const QByteArray &checksumHeader)
{
_filePath = filePath;
if (auto calculator = prepareStart(checksumHeader))
calculator->start(filePath);
}
Expand All @@ -356,11 +355,11 @@ void ValidateChecksumHeader::slotChecksumCalculated(const QByteArray &checksumTy
const QByteArray &checksum)
{
if (checksumType != _expectedChecksumType) {
emit validationFailed(tr("The checksum header contained an unknown checksum type '%1'").arg(QString::fromLatin1(_expectedChecksumType)), {}, {}, _filePath);
emit validationFailed(tr("The checksum header contained an unknown checksum type '%1'").arg(QString::fromLatin1(_expectedChecksumType)));
return;
}
if (checksum != _expectedChecksum) {
emit validationFailed(tr("The downloaded file does not match the checksum, it will be resumed. '%1' != '%2'").arg(QString::fromUtf8(_expectedChecksum), QString::fromUtf8(checksum)), checksumType, checksum, _filePath);
emit validationFailed(tr("The downloaded file does not match the checksum, it will be resumed. '%1' != '%2'").arg(QString::fromUtf8(_expectedChecksum), QString::fromUtf8(checksum)));
return;
}
emit validated(checksumType, checksum);
Expand Down
4 changes: 1 addition & 3 deletions src/common/checksums.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class OCSYNC_EXPORT ValidateChecksumHeader : public QObject

signals:
void validated(const QByteArray &checksumType, const QByteArray &checksum);
void validationFailed(const QString &errMsg, const QByteArray &checksumType, const QByteArray &checksum, const QString &filePath);
void validationFailed(const QString &errMsg);

private slots:
void slotChecksumCalculated(const QByteArray &checksumType, const QByteArray &checksum);
Expand All @@ -173,8 +173,6 @@ private slots:

QByteArray _expectedChecksumType;
QByteArray _expectedChecksum;

QString _filePath;
};

/**
Expand Down
6 changes: 0 additions & 6 deletions src/libsync/configfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#define DEFAULT_MAX_LOG_LINES 20000

namespace {
static constexpr char allowChecksumValidationFailC[] = "allowChecksumValidationFail";
static constexpr char showMainDialogAsNormalWindowC[] = "showMainDialogAsNormalWindow";
}

Expand Down Expand Up @@ -892,11 +891,6 @@ void ConfigFile::setMoveToTrash(bool isChecked)
setValue(moveToTrashC, isChecked);
}

bool ConfigFile::allowChecksumValidationFail() const
{
return getValue(allowChecksumValidationFailC, {}, false).toBool();
}

bool ConfigFile::showMainDialogAsNormalWindow() const {
return getValue(showMainDialogAsNormalWindowC, {}, false).toBool();
}
Expand Down
3 changes: 0 additions & 3 deletions src/libsync/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ class OWNCLOUDSYNC_EXPORT ConfigFile
bool moveToTrash() const;
void setMoveToTrash(bool);

/** should we allow checksum validation to fail? set to true to workaround corrupted checksums **/
bool allowChecksumValidationFail() const;

bool showMainDialogAsNormalWindow() const;

static bool setConfDir(const QString &value);
Expand Down
61 changes: 8 additions & 53 deletions src/libsync/propagatedownload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "propagatedownloadencrypted.h"
#include "common/vfs.h"

#include "configfile.h"

#include <QLoggingCategory>
#include <QNetworkAccessManager>
#include <QFileInfo>
Expand All @@ -40,11 +38,6 @@
#include <unistd.h>
#endif

namespace {
constexpr quint16 numChecksumFailuresAllowed = 1;
constexpr char *checksumFailureDbRecordPrefix = "ChecksumValidationFailed_";
}

namespace OCC {

Q_LOGGING_CATEGORY(lcGetJob, "nextcloud.sync.networkjob.get", QtInfoMsg)
Expand Down Expand Up @@ -830,38 +823,8 @@ void PropagateDownloadFile::slotGetFinished()
validator->start(_tmpFile.fileName(), checksumHeader);
}

void PropagateDownloadFile::slotChecksumFail(const QString &errMsg, const QByteArray &checksumType, const QByteArray &checksum, const QString &filePath)
{
if (!checksumType.isEmpty() && !checksum.isEmpty() && !filePath.isEmpty()) {
ConfigFile cfgFile;

if (cfgFile.allowChecksumValidationFail()) {
const auto key = QString(checksumFailureDbRecordPrefix + _item->_fileId);
const QStringList mismatchEntryForFileSplitted = propagator()->_journal->keyValueStoreGet(key).toString().split(":", QString::SkipEmptyParts);
const QByteArray mismatchChecksumForFile = mismatchEntryForFileSplitted.size() > 0 ? mismatchEntryForFileSplitted[0].toUtf8() : QByteArray();
const auto numChecksumMismatchCases = mismatchEntryForFileSplitted.size() > 1 ? mismatchEntryForFileSplitted[1].toInt() : 0;

// format must be CHECKSUM:COUNT
Q_ASSERT(mismatchEntryForFileSplitted.size() != 1);
if (mismatchEntryForFileSplitted.size() == 1) {
qCCritical(lcPropagateDownload) << "mismatchEntryForFile has incorrect format. Should be CHECKSUM:COUNT";
}

if (numChecksumMismatchCases < numChecksumFailuresAllowed || mismatchChecksumForFile != checksum) {
// not enough failures or different checksum this time
qCInfo(lcPropagateDownload) << "Checksum validation has failed" << numChecksumMismatchCases << " times, with previous checksum<" << mismatchChecksumForFile << "> and, current checksum<" << checksum << ">, but, allowChecksumValidationFail is set.Let's give it another try...";
const auto numCasesToSet = mismatchChecksumForFile != checksum ? 1 : numChecksumMismatchCases + 1;
const QString value = QString::fromUtf8(checksum) + QStringLiteral(":") + QString::number(numCasesToSet);
propagator()->_journal->keyValueStoreSet(key, value);
} else {
propagator()->_journal->keyValueStoreDelete(key);
qCInfo(lcPropagateDownload) << "Checksum validation has failed" << numChecksumMismatchCases << " times, but, allowChecksumValidationFail is set, so, let's continue...";
startContentChecksumCompute(checksumType, filePath);
return;
}
}
}

void PropagateDownloadFile::slotChecksumFail(const QString &errMsg)
{
FileSystem::remove(_tmpFile.fileName());
propagator()->_anotherSyncNeeded = true;
done(SyncFileItem::SoftError, errMsg); // tr("The file downloaded with a broken checksum, will be redownloaded."));
Expand Down Expand Up @@ -889,18 +852,6 @@ void PropagateDownloadFile::deleteExistingFolder()
}
}

void PropagateDownloadFile::startContentChecksumCompute(const QByteArray &checksumType, const QString &path)
{
qCInfo(lcPropagateDownload) << "Start checksum compute with checksumType:" << checksumType << " for path:" << path;
// Compute the content checksum.
const auto computeChecksum = new ComputeChecksum(this);
computeChecksum->setChecksumType(checksumType);

connect(computeChecksum, &ComputeChecksum::done,
this, &PropagateDownloadFile::contentChecksumComputed);
computeChecksum->start(path);
}

namespace { // Anonymous namespace for the recall feature
static QString makeRecallFileName(const QString &fn)
{
Expand Down Expand Up @@ -989,9 +940,13 @@ void PropagateDownloadFile::transmissionChecksumValidated(const QByteArray &chec
return contentChecksumComputed(checksumType, checksum);
}

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

propagator()->_journal->keyValueStoreDelete(QString(checksumFailureDbRecordPrefix + _item->_fileId));
connect(computeChecksum, &ComputeChecksum::done,
this, &PropagateDownloadFile::contentChecksumComputed);
computeChecksum->start(_tmpFile.fileName());
}

void PropagateDownloadFile::contentChecksumComputed(const QByteArray &checksumType, const QByteArray &checksum)
Expand Down
4 changes: 1 addition & 3 deletions src/libsync/propagatedownload.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,12 @@ private slots:

void abort(PropagatorJob::AbortType abortType) override;
void slotDownloadProgress(qint64, qint64);
void slotChecksumFail(const QString &errMsg, const QByteArray &checksumType, const QByteArray &checksum, const QString &filePath);
void slotChecksumFail(const QString &errMsg);

private:
void startAfterIsEncryptedIsChecked();
void deleteExistingFolder();

void startContentChecksumCompute(const QByteArray &checksumType, const QString &path);

qint64 _resumeStart;
qint64 _downloadProgress;
QPointer<GETFileJob> _job;
Expand Down
3 changes: 2 additions & 1 deletion test/testchecksumvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ using namespace OCC::Utility;
_successDown = true;
}

void slotDownError(const QString &errMsg, const QByteArray&, const QByteArray&, const QString&) {
void slotDownError(const QString &errMsg)
{
QCOMPARE(_expectedError, errMsg);
_errorSeen = true;
}
Expand Down

0 comments on commit 8122c63

Please sign in to comment.