diff --git a/src/common/util.cpp b/src/common/util.cpp index 7947b460..4d4564b6 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -10,6 +10,7 @@ #endif // Q_OS_ANDROID #include "util.h" +#define SQLITE_DB_MAGIC "SQLite format 3" #ifdef Q_OS_ANDROID static QString documentName(const QString &path) @@ -156,3 +157,21 @@ const QTemporaryDir &Util::tempDir() static QTemporaryDir dir; return dir; } + +bool Util::isSQLiteDB(const QString &path, QString &errorString) +{ + QFile file(path); + char magic[sizeof(SQLITE_DB_MAGIC)]; + + if (!file.open(QFile::ReadOnly)) { + errorString = file.errorString(); + return false; + } + if ((file.read(magic, sizeof(magic)) < (qint64)sizeof(magic)) + || memcmp(SQLITE_DB_MAGIC, magic, sizeof(SQLITE_DB_MAGIC))) { + errorString = "Not a SQLite database file"; + return false; + } + + return true; +} diff --git a/src/common/util.h b/src/common/util.h index b93faa59..a9863fc4 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -16,6 +16,7 @@ namespace Util QString file2name(const QString &path); QString displayName(const QString &path); const QTemporaryDir &tempDir(); + bool isSQLiteDB(const QString &path, QString &errorString); } #endif // UTIL_H diff --git a/src/map/mbtilesmap.cpp b/src/map/mbtilesmap.cpp index 7af7047c..a9cee8e4 100644 --- a/src/map/mbtilesmap.cpp +++ b/src/map/mbtilesmap.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -48,12 +49,15 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent) : Map(fileName, parent), _mapRatio(1.0), _tileRatio(1.0), _scalable(false), _scaledSize(0), _valid(false) { + if (!Util::isSQLiteDB(fileName, _errorString)) + return; + _db = QSqlDatabase::addDatabase("QSQLITE", fileName); _db.setDatabaseName(fileName); _db.setConnectOptions("QSQLITE_OPEN_READONLY"); if (!_db.open()) { - _errorString = "Error opening database file"; + _errorString = _db.lastError().text(); return; } diff --git a/src/map/oruxmap.cpp b/src/map/oruxmap.cpp index f62a9e83..1ecd2071 100644 --- a/src/map/oruxmap.cpp +++ b/src/map/oruxmap.cpp @@ -413,6 +413,12 @@ OruxMap::OruxMap(const QString &fileName, QObject *parent) if (dir.exists("OruxMapsImages.db")) { QString dbFile(dir.absoluteFilePath("OruxMapsImages.db")); + + if (!Util::isSQLiteDB(dbFile, _errorString)) { + _errorString = "OruxMapsImages.db: " + _errorString; + return; + } + _db = QSqlDatabase::addDatabase("QSQLITE", dbFile); _db.setDatabaseName(dbFile); _db.setConnectOptions("QSQLITE_OPEN_READONLY"); diff --git a/src/map/osmdroidmap.cpp b/src/map/osmdroidmap.cpp index 93b4f844..67e7df7e 100644 --- a/src/map/osmdroidmap.cpp +++ b/src/map/osmdroidmap.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -18,12 +19,15 @@ OsmdroidMap::OsmdroidMap(const QString &fileName, QObject *parent) { quint64 z, l = 0, r = 0, t = 0, b = 0; + if (!Util::isSQLiteDB(fileName, _errorString)) + return; + _db = QSqlDatabase::addDatabase("QSQLITE", fileName); _db.setDatabaseName(fileName); _db.setConnectOptions("QSQLITE_OPEN_READONLY"); if (!_db.open()) { - _errorString = "Error opening database file"; + _errorString = _db.lastError().text(); return; } diff --git a/src/map/sqlitemap.cpp b/src/map/sqlitemap.cpp index c03ae9e3..b35dc1d0 100644 --- a/src/map/sqlitemap.cpp +++ b/src/map/sqlitemap.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -16,12 +17,15 @@ SqliteMap::SqliteMap(const QString &fileName, QObject *parent) : Map(fileName, parent), _mapRatio(1.0), _valid(false) { + if (!Util::isSQLiteDB(fileName, _errorString)) + return; + _db = QSqlDatabase::addDatabase("QSQLITE", fileName); _db.setDatabaseName(fileName); _db.setConnectOptions("QSQLITE_OPEN_READONLY"); if (!_db.open()) { - _errorString = "Error opening database file"; + _errorString = _db.lastError().text(); return; }