SqliteDb: use proper destroy instead of unlink

GitOrigin-RevId: 73d2d992c362c7b0c6d6f28adef56c416ec8c158
This commit is contained in:
Arseny Smirnov 2018-06-07 22:56:03 +03:00
parent a72494d721
commit 03b674ab1c
2 changed files with 12 additions and 5 deletions

View File

@ -51,7 +51,7 @@ Status SqliteDb::init(CSlice path, bool *was_created) {
// from older database
bool is_db_exists = stat(path).is_ok();
if (!is_db_exists) {
destroy(path).ignore();
TRY_STATUS(destroy(path));
}
if (was_created != nullptr) {
@ -178,7 +178,7 @@ Status SqliteDb::change_key(CSlice path, const DbKey &new_db_key, const DbKey &o
// Encrypt
PerfWarningTimer timer("Encrypt sqlite database", 0.1);
auto tmp_path = path.str() + ".ecnrypted";
unlink(tmp_path).ignore();
TRY_STATUS(destroy(tmp_path));
// make shure that database is not empty
TRY_STATUS(db.exec("CREATE TABLE IF NOT EXISTS encryption_dummy_table(id INT PRIMARY KEY)"));
@ -194,7 +194,7 @@ Status SqliteDb::change_key(CSlice path, const DbKey &new_db_key, const DbKey &o
// Dectypt
PerfWarningTimer timer("Decrypt sqlite database", 0.1);
auto tmp_path = path.str() + ".ecnrypted";
unlink(tmp_path).ignore();
TRY_STATUS(destroy(tmp_path));
//NB: not really safe
TRY_STATUS(db.exec(PSLICE() << "ATTACH DATABASE '" << tmp_path << "' AS decrypted KEY ''"));

View File

@ -11,6 +11,7 @@
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/port/path.h"
#include "td/utils/port/Stat.h"
namespace td {
namespace detail {
@ -18,8 +19,14 @@ Status RawSqliteDb::last_error(sqlite3 *db) {
return Status::Error(Slice(sqlite3_errmsg(db)));
}
Status RawSqliteDb::destroy(Slice path) {
with_db_path(path, [](auto path) { unlink(path).ignore(); });
return Status::OK();
Status error;
with_db_path(path, [&](auto path) {
unlink(path).ignore();
if (!stat(path).is_error()) {
error = Status::Error(PSLICE() << "Failed to delete " << tag("path", path));
}
});
return error;
}
Status RawSqliteDb::last_error() {
//If database was corrupted, try to delete it.