1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Ignore empty history files on start

This fixes an esoteric issue of enabling chat log encryption and then restarting before adding any messages
This commit is contained in:
Dubslow 2015-01-19 21:49:41 -06:00
parent b1180a2038
commit f645faff51
No known key found for this signature in database
GPG Key ID: 3DB8E05315C220AA
2 changed files with 13 additions and 10 deletions

View File

@ -106,7 +106,7 @@ QByteArray Core::getSaltFromFile(QString filename)
QFile file(filename); QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) if (!file.open(QIODevice::ReadOnly))
{ {
qWarning() << "Core: encrypted history file doesn't exist"; qWarning() << "Core: file" << filename << "doesn't exist";
return QByteArray(); return QByteArray();
} }
QByteArray data = file.read(tox_pass_encryption_extra_length()); QByteArray data = file.read(tox_pass_encryption_extra_length());
@ -172,9 +172,11 @@ bool Core::loadEncryptedSave(QByteArray& data)
void Core::checkEncryptedHistory() void Core::checkEncryptedHistory()
{ {
QByteArray salt = getSaltFromFile(HistoryKeeper::getHistoryPath()); QString path = HistoryKeeper::getHistoryPath();
bool exists = QFile::exists(path);
if (salt.size() == 0) QByteArray salt = getSaltFromFile(path);
if (exists && salt.size() == 0)
{ // maybe we should handle this better { // maybe we should handle this better
Widget::getInstance()->showWarningMsgBox(tr("Encrypted History"), tr("No encrypted history file found, or it was corrupted.\nHistory will be disabled!")); Widget::getInstance()->showWarningMsgBox(tr("Encrypted History"), tr("No encrypted history file found, or it was corrupted.\nHistory will be disabled!"));
Settings::getInstance().setEncryptLogs(false); Settings::getInstance().setEncryptLogs(false);
@ -188,7 +190,7 @@ void Core::checkEncryptedHistory()
if (pwsaltedkeys[ptHistory]) if (pwsaltedkeys[ptHistory])
{ {
if (HistoryKeeper::checkPassword()) if (!exists || HistoryKeeper::checkPassword())
return; return;
dialogtxt = tr("The stored chat log password failed. Please try another:", "used only when pw set before load() doesn't work"); dialogtxt = tr("The stored chat log password failed. Please try another:", "used only when pw set before load() doesn't work");
} }
@ -198,8 +200,11 @@ void Core::checkEncryptedHistory()
if (pwsaltedkeys[ptMain]) if (pwsaltedkeys[ptMain])
{ {
useOtherPassword(ptHistory); useOtherPassword(ptHistory);
if (HistoryKeeper::checkPassword()) if (!exists || HistoryKeeper::checkPassword())
{
qDebug() << "Core: using main password for history";
return; return;
}
clearPassword(ptHistory); clearPassword(ptHistory);
} }
@ -218,7 +223,7 @@ void Core::checkEncryptedHistory()
else else
setPassword(pw, ptHistory, reinterpret_cast<uint8_t*>(salt.data())); setPassword(pw, ptHistory, reinterpret_cast<uint8_t*>(salt.data()));
error = !HistoryKeeper::checkPassword(); error = exists && !HistoryKeeper::checkPassword();
dialogtxt = a + " " + b; dialogtxt = a + " " + b;
} while (error); } while (error);
} }

View File

@ -73,8 +73,6 @@ QSqlQuery EncryptedDb::exec(const QString &query)
bool EncryptedDb::pullFileContent(const QString &fname, QByteArray &buf) bool EncryptedDb::pullFileContent(const QString &fname, QByteArray &buf)
{ {
qDebug() << "EncryptedDb::pullFileContent()";
QFile dbFile(fname); QFile dbFile(fname);
if (!dbFile.open(QIODevice::ReadOnly)) if (!dbFile.open(QIODevice::ReadOnly))
{ {
@ -87,13 +85,13 @@ bool EncryptedDb::pullFileContent(const QString &fname, QByteArray &buf)
while (!dbFile.atEnd()) while (!dbFile.atEnd())
{ {
QByteArray encrChunk = dbFile.read(encryptedChunkSize); QByteArray encrChunk = dbFile.read(encryptedChunkSize);
qDebug() << "got chunk:" << encrChunk.size(); qDebug() << "EncryptedDb::pullFileContent: got chunk:" << encrChunk.size();
buf = Core::getInstance()->decryptData(encrChunk, Core::ptHistory); buf = Core::getInstance()->decryptData(encrChunk, Core::ptHistory);
if (buf.size() > 0) if (buf.size() > 0)
{ {
fileContent += buf; fileContent += buf;
} else { } else {
qWarning() << "Encrypted history log is corrupted: can't decrypt, will be deleted"; qWarning() << "EncryptedDb::pullFileContent: Encrypted history log is corrupted: can't decrypt, will be deleted";
buf = QByteArray(); buf = QByteArray();
return false; return false;
} }