Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
guard against invalid database files
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer committed Apr 8, 2015
1 parent 7d58a41 commit 4b66c49
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
19 changes: 16 additions & 3 deletions platform/default/sqlite_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion src/mbgl/util/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdexcept>

#include <unistd.h>

namespace mbgl {
namespace util {
Expand All @@ -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");
}
}

}
}
9 changes: 9 additions & 0 deletions src/mbgl/util/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}

Expand Down
46 changes: 45 additions & 1 deletion test/storage/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;

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

0 comments on commit 4b66c49

Please sign in to comment.