Skip to content

Commit

Permalink
add active suffix in the individual storage plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Jun 5, 2024
1 parent 32da16e commit ae57953
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
27 changes: 1 addition & 26 deletions rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();
}
Expand All @@ -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 << "\"!";
Expand Down
4 changes: 3 additions & 1 deletion rosbag2_storage_mcap/src/mcap_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include <algorithm>
#include <cstring>
#include <filesystem>
#include <memory>
#include <mutex>
#include <optional>
Expand Down Expand Up @@ -316,6 +317,7 @@ MCAPStorage::~MCAPStorage()
}
if (mcap_writer_) {
mcap_writer_->close();
std::filesystem::rename(relative_path_ + ".active", relative_path_);
}
}

Expand Down Expand Up @@ -400,7 +402,7 @@ void MCAPStorage::open_impl(const std::string & uri, const std::string & preset_
YAML::convert<McapWriterOptions>::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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<SqliteWrapper>(relative_path_, io_flag, std::move(pragmas));
database_ = std::make_unique<SqliteWrapper>(path, io_flag, std::move(pragmas));
} catch (const SqliteException & e) {
throw std::runtime_error("Failed to setup storage. Error: " + std::string(e.what()));
}
Expand Down

0 comments on commit ae57953

Please sign in to comment.