Ejemplo n.º 1
0
void TestGui::checkDatabase()
{
    CompositeKey key;
    key.addKey(PasswordKey("a"));
    KeePass2Reader reader;
    QScopedPointer<Database> dbSaved(reader.readDatabase(m_tmpFileName, key));
    QVERIFY(dbSaved);
    QVERIFY(!reader.hasError());
    QCOMPARE(dbSaved->metadata()->name(), m_db->metadata()->name());
}
Ejemplo n.º 2
0
/**
 * Saves changes in the current database.
 * If successful, returns true and emits dbSaved() signal;
 * otherwise returns false and (sometimes) emits an appropriate error signal.
 */
Q_INVOKABLE bool PwDatabaseFacade::save() {
    if (!db) {
        LOG("Cannot save - no DB open");
        return false;
    }
    if (isLocked()) {
        LOG("Cannot save - DB is locked");
        return false;
    }

    emit dbAboutToSave();

    // Encrypt and save to memory
    QByteArray outData;
    if (!db->save(outData)) {
        LOG("DB saving failed");
        return false;
    }

    // Copy original DB to temporary timestamped file
    // (but when we create a DB, the original does not exist, and it is ok)
    QFile dbFile(db->getDatabaseFilePath());
    QString bakFileName = makeBackupFilePath(db->getDatabaseFilePath());
    if (dbFile.exists() && !dbFile.copy(bakFileName)) {
        LOG("Failed to backup the original DB: %s", dbFile.errorString().toUtf8().constData());
        emit fileSaveError(tr("Cannot backup database file. Saving cancelled.", "An error message: failed to make a backup copy of the database file."), dbFile.errorString());
        return false;
    }

    // Write the new data directly to the original file
    if (!dbFile.open(QIODevice::WriteOnly)) {
        LOG("Cannot open DB file: '%s' Error: %d. Message: %s",
                dbFile.fileName().toUtf8().constData(),
                dbFile.error(), dbFile.errorString().toUtf8().constData());
        emit fileSaveError(tr("Cannot save database file", "An error message shown when the database file cannot be saved."), dbFile.errorString());
        return false;
    }
    qint64 bytesWritten = dbFile.write(outData);
    if ((dbFile.error() != QFile::NoError) || (bytesWritten != outData.size())) {
        LOG("Cannot write to DB file. Error: %d. Message: %s", dbFile.error(), dbFile.errorString().toUtf8().constData());
        emit fileSaveError(tr("Cannot write to database file", "An error message shown when the database file cannot be written to."), dbFile.errorString());
        return false;
    }
    if (!dbFile.flush()) {
        LOG("Could not flush the DB file. Error: %d. Message: %s", dbFile.error(), dbFile.errorString().toUtf8().constData());
        emit fileSaveError(tr("Error writing to database file", "An error message shown when the database file cannot be written to."), dbFile.errorString());
        dbFile.close();
        // could not flush -> possibly not completely written -> not saved
        return false;
    }
    dbFile.close();

    if (!Settings::instance()->isBackupDatabaseOnSave()) {
        // Backup switched off, so remove the backup file
        // (there are no backup files for newly created DBs)
        QFile bakFile(bakFileName);
        if (bakFile.exists() && !bakFile.remove()) {
            // Could not delete backup. Not critical and nothing we/user can do about it - so just ignore it silently.
            LOG("Could not remove backup file: %s", bakFile.fileName().toUtf8().constData());
        }
    }
    emit dbSaved();
    return true;
}