Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Two small exception-related fixes #1654

Merged
merged 2 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libaktualizr/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void Config::postUpdateValues() {
if (!tls.server_url_path.empty()) {
try {
tls.server = Utils::readFile(tls.server_url_path, true);
} catch (const boost::filesystem::filesystem_error& e) {
} catch (const std::exception& e) {
LOG_ERROR << "Couldn't read gateway URL: " << e.what();
tls.server = "";
}
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/crypto/p11engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ P11Engine::P11Engine(P11Config config) : config_(std::move(config)), ctx_(config
// an engine which has not been fully initialized
ENGINE_free(engine);
ENGINE_cleanup(); // for openssl < 1.1
throw;
throw exc;
}

ssl_engine_ = engine;
Expand Down
21 changes: 9 additions & 12 deletions src/libaktualizr/storage/sql_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,27 @@ class SQLite3Guard {
// if `rollbackTransaction()` is called explicitely, the changes will be
// rolled back

bool beginTransaction() {
void beginTransaction() {
// Note: transaction cannot be nested and this will fail if another
// transaction was open on the same connection
int ret = exec("BEGIN TRANSACTION;", nullptr, nullptr);
if (ret != SQLITE_OK) {
if (exec("BEGIN TRANSACTION;", nullptr, nullptr) != SQLITE_OK) {
LOG_ERROR << "Can't begin transaction: " << errmsg();
throw SQLException("Transaction error");
}
return ret == SQLITE_OK;
}

bool commitTransaction() {
int ret = exec("COMMIT TRANSACTION;", nullptr, nullptr);
if (ret != SQLITE_OK) {
void commitTransaction() {
if (exec("COMMIT TRANSACTION;", nullptr, nullptr) != SQLITE_OK) {
LOG_ERROR << "Can't commit transaction: " << errmsg();
throw SQLException("Transaction error");
}
return ret == SQLITE_OK;
}

bool rollbackTransaction() {
int ret = exec("ROLLBACK TRANSACTION;", nullptr, nullptr);
if (ret != SQLITE_OK) {
void rollbackTransaction() {
if (exec("ROLLBACK TRANSACTION;", nullptr, nullptr) != SQLITE_OK) {
LOG_ERROR << "Can't rollback transaction: " << errmsg();
throw SQLException("Transaction error");
}
return ret == SQLITE_OK;
}

private:
Expand Down
96 changes: 23 additions & 73 deletions src/libaktualizr/storage/sqlstorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
void SQLStorage::cleanMetaVersion(Uptane::RepositoryType repo, const Uptane::Role& role) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement = db.prepareStatement<int, int, int>(
"SELECT meta FROM meta WHERE (repo=? AND meta_type=? AND version=?);", static_cast<int>(repo), role.ToInt(), -1);
Expand Down Expand Up @@ -161,10 +158,7 @@ void SQLStorage::saveSecondaryInfo(const Uptane::EcuSerial& ecu_serial, const st
key_type_str = key_type_ss.str();
key_type_str.erase(std::remove(key_type_str.begin(), key_type_str.end(), '"'), key_type_str.end());

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement =
db.prepareStatement<std::string>("SELECT count(*) FROM secondary_ecus WHERE serial = ?;", ecu_serial.ToString());
Expand Down Expand Up @@ -195,10 +189,7 @@ void SQLStorage::saveSecondaryInfo(const Uptane::EcuSerial& ecu_serial, const st
void SQLStorage::saveSecondaryData(const Uptane::EcuSerial& ecu_serial, const std::string& data) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement =
db.prepareStatement<std::string>("SELECT count(*) FROM secondary_ecus WHERE serial = ?;", ecu_serial.ToString());
Expand Down Expand Up @@ -317,10 +308,7 @@ void SQLStorage::storeTlsCreds(const std::string& ca, const std::string& cert, c
void SQLStorage::storeTlsCa(const std::string& ca) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement = db.prepareStatement("SELECT count(*) FROM tls_creds;");
if (statement.step() != SQLITE_ROW) {
Expand All @@ -347,10 +335,7 @@ void SQLStorage::storeTlsCa(const std::string& ca) {
void SQLStorage::storeTlsCert(const std::string& cert) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement = db.prepareStatement("SELECT count(*) FROM tls_creds;");
if (statement.step() != SQLITE_ROW) {
Expand All @@ -377,10 +362,8 @@ void SQLStorage::storeTlsCert(const std::string& cert) {
void SQLStorage::storeTlsPkey(const std::string& pkey) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement = db.prepareStatement("SELECT count(*) FROM tls_creds;");
if (statement.step() != SQLITE_ROW) {
LOG_ERROR << "Can't get count of tls_creds table: " << db.errmsg();
Expand All @@ -406,10 +389,8 @@ void SQLStorage::storeTlsPkey(const std::string& pkey) {
bool SQLStorage::loadTlsCreds(std::string* ca, std::string* cert, std::string* pkey) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return false;
}
db.beginTransaction();

auto statement = db.prepareStatement("SELECT ca_cert, client_cert, client_pkey FROM tls_creds LIMIT 1;");

int result = statement.step();
Expand Down Expand Up @@ -535,10 +516,7 @@ bool SQLStorage::loadTlsPkey(std::string* pkey) {
void SQLStorage::storeRoot(const std::string& data, Uptane::RepositoryType repo, Uptane::Version version) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto del_statement =
db.prepareStatement<int, int, int>("DELETE FROM meta WHERE (repo=? AND meta_type=? AND version=?);",
Expand All @@ -564,10 +542,7 @@ void SQLStorage::storeRoot(const std::string& data, Uptane::RepositoryType repo,
void SQLStorage::storeNonRoot(const std::string& data, Uptane::RepositoryType repo, const Uptane::Role role) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto del_statement = db.prepareStatement<int, int>("DELETE FROM meta WHERE (repo=? AND meta_type=?);",
static_cast<int>(repo), role.ToInt());
Expand Down Expand Up @@ -683,10 +658,7 @@ void SQLStorage::clearMetadata() {
void SQLStorage::storeDelegation(const std::string& data, const Uptane::Role role) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement = db.prepareStatement<SQLBlob, std::string>("INSERT OR REPLACE INTO delegations VALUES (?, ?);",
SQLBlob(data), role.ToString());
Expand Down Expand Up @@ -815,10 +787,7 @@ void SQLStorage::clearDeviceId() {
void SQLStorage::storeEcuRegistered() {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement = db.prepareStatement("SELECT count(*) FROM device_info;");
if (statement.step() != SQLITE_ROW) {
Expand Down Expand Up @@ -911,10 +880,7 @@ void SQLStorage::storeEcuSerials(const EcuSerials& serials) {
if (serials.size() >= 1) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

if (db.exec("DELETE FROM ecus;", nullptr, nullptr) != SQLITE_OK) {
LOG_ERROR << "Can't clear ecus: " << db.errmsg();
Expand Down Expand Up @@ -991,10 +957,7 @@ bool SQLStorage::loadEcuSerials(EcuSerials* serials) {
void SQLStorage::clearEcuSerials() {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

if (db.exec("DELETE FROM ecus;", nullptr, nullptr) != SQLITE_OK) {
LOG_ERROR << "Can't clear ECUs: " << db.errmsg();
Expand Down Expand Up @@ -1050,10 +1013,7 @@ void SQLStorage::storeMisconfiguredEcus(const std::vector<MisconfiguredEcu>& ecu
if (ecus.size() >= 1) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

if (db.exec("DELETE FROM misconfigured_ecus;", nullptr, nullptr) != SQLITE_OK) {
LOG_ERROR << "Can't clear misconfigured_ecus: " << db.errmsg();
Expand Down Expand Up @@ -1120,10 +1080,7 @@ void SQLStorage::saveInstalledVersion(const std::string& ecu_serial, const Uptan
InstalledVersionUpdateMode update_mode) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

// either adds a new entry or update the last one's status

Expand Down Expand Up @@ -1662,10 +1619,8 @@ bool SQLStorage::loadReportEvents(Json::Value* report_array, int64_t* id_max) {

void SQLStorage::deleteReportEvents(int64_t id_max) {
SQLite3Guard db = dbConnection();
if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}

db.beginTransaction();

auto statement = db.prepareStatement<int64_t>("DELETE FROM report_events WHERE id <= ?;", id_max);
if (statement.step() != SQLITE_DONE) {
Expand All @@ -1677,10 +1632,8 @@ void SQLStorage::deleteReportEvents(int64_t id_max) {

void SQLStorage::clearInstallationResults() {
SQLite3Guard db = dbConnection();
if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}

db.beginTransaction();

if (db.exec("DELETE FROM device_installation_result;", nullptr, nullptr) != SQLITE_OK) {
LOG_ERROR << "Can't clear device_installation_result: " << db.errmsg();
Expand Down Expand Up @@ -1949,10 +1902,7 @@ std::vector<Uptane::Target> SQLStorage::getTargetFiles() {
void SQLStorage::removeTargetFile(const std::string& target_name) {
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
return;
}
db.beginTransaction();

auto statement =
db.prepareStatement<std::string>("SELECT filename FROM target_images WHERE targetname = ?;", target_name);
Expand All @@ -1974,7 +1924,7 @@ void SQLStorage::removeTargetFile(const std::string& target_name) {
boost::filesystem::remove(images_path_ / filename);
} catch (std::exception& e) {
LOG_ERROR << "Could not remove target file";
throw;
throw e;
}

db.commitTransaction();
Expand Down
10 changes: 6 additions & 4 deletions src/libaktualizr/storage/sqlstorage_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ bool SQLStorageBase::dbMigrateForward(int version_from, int version_to) {

SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
try {
db.beginTransaction();
} catch (const SQLException& e) {
return false;
}

Expand Down Expand Up @@ -212,8 +213,9 @@ bool SQLStorageBase::dbMigrate() {
LOG_INFO << "Bootstraping DB to version " << current_schema_version_;
SQLite3Guard db = dbConnection();

if (!db.beginTransaction()) {
LOG_ERROR << "Can't start transaction: " << db.errmsg();
try {
db.beginTransaction();
} catch (const SQLException& e) {
return false;
}

Expand Down