guard against invalid database files

This commit is contained in:
Konstantin Käfer
2015-04-08 14:24:03 +02:00
parent 7d58a41de5
commit 4b66c490b4
4 changed files with 79 additions and 5 deletions

View File

@@ -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) {

View File

@@ -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");
}
}
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}