diff options
Diffstat (limited to 'common/storage_lmdb.cpp')
-rw-r--r-- | common/storage_lmdb.cpp | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index cc8b28d..ae4bfba 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -437,6 +437,43 @@ void Storage::Transaction::abort() | |||
437 | d->transaction = nullptr; | 437 | d->transaction = nullptr; |
438 | } | 438 | } |
439 | 439 | ||
440 | //Ensure that we opened the correct database by comparing the expected identifier with the one | ||
441 | //we write to the database on first open. | ||
442 | static bool ensureCorrectDb(Storage::NamedDatabase &database, const QByteArray &db, bool readOnly) | ||
443 | { | ||
444 | bool openedTheWrongDatabase = false; | ||
445 | auto count = database.scan("__internal_dbname", [db, &openedTheWrongDatabase](const QByteArray &key, const QByteArray &value) ->bool { | ||
446 | if (value != db) { | ||
447 | Warning() << "Opened the wrong database, got " << value << " instead of " << db; | ||
448 | openedTheWrongDatabase = true; | ||
449 | } | ||
450 | return false; | ||
451 | }, | ||
452 | [](const Storage::Error &error) -> bool{ | ||
453 | return false; | ||
454 | }, false); | ||
455 | //This is the first time we open this database in a write transaction, write the db name | ||
456 | if (!count) { | ||
457 | if (!readOnly) { | ||
458 | database.write("__internal_dbname", db); | ||
459 | } | ||
460 | } | ||
461 | return !openedTheWrongDatabase; | ||
462 | } | ||
463 | |||
464 | bool Storage::Transaction::validateNamedDatabases() | ||
465 | { | ||
466 | auto databases = getDatabaseNames(); | ||
467 | for (const auto &dbName : databases) { | ||
468 | auto db = openDatabase(dbName); | ||
469 | if (!db) { | ||
470 | Warning() << "Failed to open the database: " << dbName; | ||
471 | return false; | ||
472 | } | ||
473 | } | ||
474 | return true; | ||
475 | } | ||
476 | |||
440 | Storage::NamedDatabase Storage::Transaction::openDatabase(const QByteArray &db, const std::function<void(const Storage::Error &error)> &errorHandler, bool allowDuplicates) const | 477 | Storage::NamedDatabase Storage::Transaction::openDatabase(const QByteArray &db, const std::function<void(const Storage::Error &error)> &errorHandler, bool allowDuplicates) const |
441 | { | 478 | { |
442 | if (!d) { | 479 | if (!d) { |
@@ -450,7 +487,12 @@ Storage::NamedDatabase Storage::Transaction::openDatabase(const QByteArray &db, | |||
450 | delete p; | 487 | delete p; |
451 | return Storage::NamedDatabase(); | 488 | return Storage::NamedDatabase(); |
452 | } | 489 | } |
453 | return Storage::NamedDatabase(p); | 490 | auto database = Storage::NamedDatabase(p); |
491 | if (!ensureCorrectDb(database, db, d->requestedRead)) { | ||
492 | Warning() << "Failed to open the database"; | ||
493 | return Storage::NamedDatabase(); | ||
494 | } | ||
495 | return database; | ||
454 | } | 496 | } |
455 | 497 | ||
456 | QList<QByteArray> Storage::Transaction::getDatabaseNames() const | 498 | QList<QByteArray> Storage::Transaction::getDatabaseNames() const |