From e52961f468dc34ec37a66bee4418cbd8aee7964b Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 7 Aug 2024 15:10:40 +0200 Subject: [PATCH] chore: delete tmp folders on startup --- .../storage/include/storage/storage.hpp | 3 ++- libraries/core_libs/storage/src/storage.cpp | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index cc18c163e9..4f33fa06c5 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -208,8 +208,9 @@ class DbStorage : public std::enable_shared_from_this { const std::string& new_col_name, bool move_data = false); // For removal of LOG.old.* files in the database - void removeOldLogFiles() const; + void removeTempFiles() const; void removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const; + void deleteTmpDirectories(const std::string& path) const; uint32_t getMajorVersion() const; std::unique_ptr getColumnIterator(const Column& c); diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index a6793c432b..0ef686f139 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -49,7 +49,7 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc LOG_OBJECTS_CREATE("DBS"); fs::create_directories(db_path_); - removeOldLogFiles(); + removeTempFiles(); rocksdb::Options options; options.create_missing_column_families = true; @@ -94,10 +94,11 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc } } -void DbStorage::removeOldLogFiles() const { +void DbStorage::removeTempFiles() const { const std::regex filePattern("LOG\\.old\\.\\d+"); removeFilesWithPattern(db_path_, filePattern); removeFilesWithPattern(state_db_path_, filePattern); + deleteTmpDirectories(path_); } void DbStorage::removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const { @@ -118,6 +119,22 @@ void DbStorage::removeFilesWithPattern(const std::string& directory, const std:: } } +void DbStorage::deleteTmpDirectories(const std::string& path) const { + try { + for (const auto& entry : fs::directory_iterator(path)) { + if (entry.is_directory()) { + std::string dirName = entry.path().filename().string(); + if (dirName.size() >= 4 && dirName.substr(dirName.size() - 4) == ".tmp") { + fs::remove_all(entry.path()); + LOG(log_dg_) << "Deleted: " << entry.path() << std::endl; + } + } + } + } catch (const fs::filesystem_error& e) { + LOG(log_er_) << "Error: " << e.what() << std::endl; + } +} + void DbStorage::updateDbVersions() { saveStatusField(StatusDbField::DbMajorVersion, TARAXA_DB_MAJOR_VERSION); saveStatusField(StatusDbField::DbMinorVersion, TARAXA_DB_MINOR_VERSION);