From ae57953a7c5d57bac5abbe25df3e7a3423edbb02 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Wed, 5 Jun 2024 23:59:39 +0200 Subject: [PATCH] add active suffix in the individual storage plugins --- .../rosbag2_cpp/writers/sequential_writer.cpp | 27 +------------------ rosbag2_storage_mcap/src/mcap_storage.cpp | 4 ++- .../sqlite_storage.cpp | 9 +++++-- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp b/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp index 3001db9ac..a058a2073 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp @@ -47,19 +47,6 @@ std::string strip_parent_path(const std::string & relative_path) { return fs::path(relative_path).filename().generic_string(); } - -std::string remove_active_from_filename(const std::string& relative_path) -{ - fs::path path(relative_path); - auto filename = path.filename().string(); - auto active_pos = filename.find(".active"); - if(active_pos != std::string::npos) { - filename.replace(active_pos, 7, ""); - } - auto new_path = path.parent_path() / filename; - return new_path.string(); -} - } // namespace SequentialWriter::SequentialWriter( @@ -203,11 +190,7 @@ void SequentialWriter::close() } if (storage_) { - // when the storage_ is closed, rename the file to remove ".active" suffix - std::string active_path = storage_->get_relative_file_path(); - std::string final_path = remove_active_from_filename(active_path); storage_.reset(); // Destroy storage before calling WRITE_SPLIT callback to make sure that - fs::rename(active_path, final_path); // bag file was closed before callback call. } if (!metadata_.relative_file_paths.empty()) { @@ -326,7 +309,7 @@ std::string SequentialWriter::format_storage_uri( // SequentialWriter is opened with a relative path. std::stringstream storage_file_name; storage_file_name << fs::path(base_folder).filename().generic_string() << "_" << - storage_count << ".active"; + storage_count; return (fs::path(base_folder) / storage_file_name.str()).generic_string(); } @@ -343,15 +326,7 @@ void SequentialWriter::switch_to_next_storage() storage_options_.uri = format_storage_uri( base_folder_, metadata_.relative_file_paths.size()); - - // when the storage_ is closed, rename the file to remove ".active" suffix - std::string active_path = storage_->get_relative_file_path(); - std::string final_path = remove_active_from_filename(active_path); - storage_ = storage_factory_->open_read_write(storage_options_); - - std::filesystem::rename(active_path, final_path); - if (!storage_) { std::stringstream errmsg; errmsg << "Failed to rollover bagfile to new file: \"" << storage_options_.uri << "\"!"; diff --git a/rosbag2_storage_mcap/src/mcap_storage.cpp b/rosbag2_storage_mcap/src/mcap_storage.cpp index cbade144c..ce9f7c9c3 100644 --- a/rosbag2_storage_mcap/src/mcap_storage.cpp +++ b/rosbag2_storage_mcap/src/mcap_storage.cpp @@ -42,6 +42,7 @@ #include #include +#include #include #include #include @@ -316,6 +317,7 @@ MCAPStorage::~MCAPStorage() } if (mcap_writer_) { mcap_writer_->close(); + std::filesystem::rename(relative_path_ + ".active", relative_path_); } } @@ -400,7 +402,7 @@ void MCAPStorage::open_impl(const std::string & uri, const std::string & preset_ YAML::convert::decode(yaml_node, options); } - auto status = mcap_writer_->open(relative_path_, options); + auto status = mcap_writer_->open(relative_path_ + ".active", options); if (!status.ok()) { throw std::runtime_error(status.message); } diff --git a/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_storage.cpp b/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_storage.cpp index 4ce1e98c4..f2c796ab9 100644 --- a/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_storage.cpp +++ b/rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_storage.cpp @@ -157,6 +157,10 @@ SqliteStorage::~SqliteStorage() if (active_transaction_) { commit_transaction(); } + if (is_read_write(storage_mode_) && database_) { + std::filesystem::rename( + relative_path_ + ".active", relative_path_); + } } SqliteStorage::PresetProfile SqliteStorage::parse_preset_profile(const std::string & profile_string) @@ -210,9 +214,10 @@ void SqliteStorage::open( "Failed to read from bag: File '" + relative_path_ + "' does not exist!"); } } - + std::string path = is_read_write(io_flag) ? (relative_path_ + ".active") : relative_path_; + try { - database_ = std::make_unique(relative_path_, io_flag, std::move(pragmas)); + database_ = std::make_unique(path, io_flag, std::move(pragmas)); } catch (const SqliteException & e) { throw std::runtime_error("Failed to setup storage. Error: " + std::string(e.what())); }