summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-04-08 14:24:03 +0200
committerKonstantin Käfer <mail@kkaefer.com>2015-04-08 14:55:47 +0200
commit4b66c490b41e8ea18428828707ed4327734f92dc (patch)
tree2dab5679b594fc1ff7c15a21e91b0f849c6d0b66
parent7d58a41de5dbf1b24b8bad9a2a98c21a7bf75382 (diff)
downloadqtlocation-mapboxgl-4b66c490b41e8ea18428828707ed4327734f92dc.tar.gz
guard against invalid database files
-rw-r--r--platform/default/sqlite_cache.cpp19
-rw-r--r--src/mbgl/util/io.cpp10
-rw-r--r--src/mbgl/util/io.hpp9
-rw-r--r--test/storage/database.cpp46
4 files changed, 79 insertions, 5 deletions
diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp
index 3f95db7c2d..a3114098c8 100644
--- a/platform/default/sqlite_cache.cpp
+++ b/platform/default/sqlite_cache.cpp
@@ -6,6 +6,7 @@
#include <mbgl/util/async_queue.hpp>
#include <mbgl/util/variant.hpp>
#include <mbgl/util/compression.hpp>
+#include <mbgl/util/io.hpp>
#include <mbgl/platform/log.hpp>
#include "sqlite3.hpp"
@@ -169,8 +170,20 @@ void SQLiteCache::createSchema() {
try {
db->exec(sql);
schema = true;
- } catch(mapbox::sqlite::Exception &ex) {
- Log::Error(Event::Database, ex.code, ex.what());
+ } catch (mapbox::sqlite::Exception &ex) {
+
+ if (ex.code == SQLITE_NOTADB) {
+ Log::Warning(Event::Database, "Trashing invalid database");
+ db.reset();
+ try {
+ util::deleteFile(path);
+ } catch (util::IOException& ioEx) {
+ Log::Error(Event::Database, ex.code, ex.what());
+ }
+ db = util::make_unique<Database>(path.c_str(), ReadWrite | Create);
+ } else {
+ Log::Error(Event::Database, ex.code, ex.what());
+ }
// Creating the database table + index failed. That means there may already be one, likely
// with different columsn. Drop it and try to create a new one.
@@ -269,7 +282,7 @@ void SQLiteCache::process(PutAction &action) {
putStmt->run();
} catch (mapbox::sqlite::Exception& ex) {
Log::Error(Event::Database, ex.code, ex.what());
- }
+ }
}
void SQLiteCache::process(RefreshAction &action) {
diff --git a/src/mbgl/util/io.cpp b/src/mbgl/util/io.cpp
index 76f7c35ade..bb4c3595c3 100644
--- a/src/mbgl/util/io.cpp
+++ b/src/mbgl/util/io.cpp
@@ -4,7 +4,8 @@
#include <iostream>
#include <sstream>
#include <fstream>
-#include <stdexcept>
+
+#include <unistd.h>
namespace mbgl {
namespace util {
@@ -30,5 +31,12 @@ std::string read_file(const std::string &filename) {
}
}
+void deleteFile(const std::string& filename) {
+ const int ret = unlink(filename.c_str());
+ if (ret == -1) {
+ throw IOException(errno, "failed to unlink file");
+ }
+}
+
}
}
diff --git a/src/mbgl/util/io.hpp b/src/mbgl/util/io.hpp
index e95fc16d9d..bf15253ee4 100644
--- a/src/mbgl/util/io.hpp
+++ b/src/mbgl/util/io.hpp
@@ -2,13 +2,22 @@
#define MBGL_UTIL_IO
#include <string>
+#include <stdexcept>
namespace mbgl {
namespace util {
+struct IOException : std::runtime_error {
+ inline IOException(int err, const char* msg) : std::runtime_error(msg), code(err) {
+ }
+ const int code = 0;
+};
+
void write_file(const std::string &filename, const std::string &data);
std::string read_file(const std::string &filename);
+void deleteFile(const std::string& filename);
+
}
}
diff --git a/test/storage/database.cpp b/test/storage/database.cpp
index 80cb956194..c60388be4a 100644
--- a/test/storage/database.cpp
+++ b/test/storage/database.cpp
@@ -6,8 +6,9 @@
#include <mbgl/storage/default/sqlite_cache.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
+#include <mbgl/util/io.hpp>
-#include <fcntl.h>
+#include <sys/file.h>
class ScopedTest {
public:
@@ -66,6 +67,12 @@ void deleteFile(const char* name) {
}
}
+
+void writeFile(const char* name, const std::string& data) {
+ mbgl::util::write_file(name, data);
+}
+
+
TEST_F(Storage, DatabaseCreate) {
using namespace mbgl;
@@ -336,3 +343,40 @@ TEST_F(Storage, DatabaseDeleted) {
// Explicitly delete the Cache now.
cache.reset();
}
+
+
+
+TEST_F(Storage, DatabaseInvalid) {
+ using namespace mbgl;
+
+ // Create a locked file.
+ createDir("test/fixtures/database");
+ deleteFile("test/fixtures/database/invalid.db");
+ writeFile("test/fixtures/database/invalid.db", "this is an invalid file");
+
+ auto cache = util::make_unique<SQLiteCache>("test/fixtures/database/invalid.db");
+
+ std::promise<void> promise;
+
+ {
+ // Adds a file.
+ Log::setObserver(util::make_unique<FixtureLogObserver>());
+ promise = {};
+ auto response = std::make_shared<Response>();
+ response->data = "Demo";
+ cache->put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full);
+ cache->get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) {
+ EXPECT_NE(nullptr, res.get());
+ EXPECT_EQ("Demo", res->data);
+ promise.set_value();
+ });
+ promise.get_future().get();
+
+ auto observer = Log::removeObserver();
+ auto flo = dynamic_cast<FixtureLogObserver*>(observer.get());
+ EXPECT_EQ(1ul, flo->count({ EventSeverity::Warning, Event::Database, -1, "Trashing invalid database" }));
+ }
+
+ // Explicitly delete the Cache now.
+ cache.reset();
+}