Merge pull request #307 from hunbernd/fix/sqlcipher-version

Sqlcipher version fix
This commit is contained in:
Cyril Soler 2016-05-04 15:32:17 -04:00
commit 40f8313236
3 changed files with 72 additions and 3 deletions

View File

@ -35,10 +35,54 @@
#include <sqlcipher/sqlite3.h>
#endif
std::string RsServer::getSQLCipherVersion()
{
sqlite3* mDb;
std::string versionstring("");
const char* version;
int rc = sqlite3_open_v2("", &mDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , NULL); //create DB in a temp file
if(rc){
std::cerr << "Can't open database, Error code: " << sqlite3_errmsg(mDb)
<< std::endl;
sqlite3_close(mDb);
mDb = NULL;
return "";
}
std::string sqlQuery = "PRAGMA cipher_version;";
sqlite3_stmt* stmt = NULL;
rc = sqlite3_prepare_v2(mDb, sqlQuery.c_str(), sqlQuery.length(), &stmt, NULL);
if (rc == SQLITE_OK) {
rc = sqlite3_step(stmt);
switch (rc) {
case SQLITE_ROW:
version = (const char *)sqlite3_column_text(stmt, 0); //not needed to free
versionstring.append(version);
break;
case SQLITE_DONE:
break;
default:
std::cerr << "RetroDb::tableExists(): Error executing statement (code: " << rc << ")"
<< std::endl;
break;
}
}
if (stmt) {
sqlite3_finalize(stmt);
}
sqlite3_close(mDb); // no-op if mDb is NULL (https://www.sqlite.org/c3ref/close.html)
return versionstring;
}
void RsServer::getLibraries(std::list<RsLibraryInfo> &libraries)
{
libraries.push_back(RsLibraryInfo("bzip2", BZ2_bzlibVersion()));
libraries.push_back(RsLibraryInfo("OpenSSL", SSLeay_version(SSLEAY_VERSION)));
libraries.push_back(RsLibraryInfo("SQLCipher", SQLITE_VERSION));
libraries.push_back(RsLibraryInfo("SQLite", SQLITE_VERSION));
#ifndef NO_SQLCIPHER
libraries.push_back(RsLibraryInfo("SQLCipher", getSQLCipherVersion()));
#endif
libraries.push_back(RsLibraryInfo("Zlib", ZLIB_VERSION));
}

View File

@ -139,6 +139,8 @@ class RsServer: public RsControl, public RsTickingThread
private:
std::string getSQLCipherVersion();
// The real Server Parts.
//filedexserver *server;

View File

@ -67,7 +67,6 @@ RetroDb::RetroDb(const std::string &dbPath, int flags, const std::string& key) :
return;
}
}
#endif
char *err = NULL;
rc = sqlite3_exec(mDb, "PRAGMA cipher_migrate;", NULL, NULL, &err);
@ -81,8 +80,32 @@ RetroDb::RetroDb(const std::string &dbPath, int flags, const std::string& key) :
std::cerr << std::endl;
sqlite3_free(err);
}
}
//Test DB for correct sqlcipher version
if (sqlite3_exec(mDb, "PRAGMA user_version;", NULL, NULL, NULL) != SQLITE_OK)
{
std::cerr << "RetroDb::RetroDb(): Failed to open database: " << dbPath << std::endl << "Trying with settings for sqlcipher version 3...";
//Reopening the database with correct settings
rc = sqlite3_close(mDb);
mDb = NULL;
if(!rc)
rc = sqlite3_open_v2(dbPath.c_str(), &mDb, flags, NULL);
if(!rc && !mKey.empty())
rc = sqlite3_key(mDb, mKey.c_str(), mKey.size());
if(!rc)
rc = sqlite3_exec(mDb, "PRAGMA kdf_iter = 64000;", NULL, NULL, NULL);
if (!rc && (sqlite3_exec(mDb, "PRAGMA user_version;", NULL, NULL, NULL) == SQLITE_OK))
{
std::cerr << "\tSuccess" << std::endl;
} else {
std::cerr << "\tFailed, giving up" << std::endl;
sqlite3_close(mDb);
mDb = NULL;
return;
}
}
#endif
}
RetroDb::~RetroDb(){