Skip to content

Commit ef4e37b

Browse files
committed
add active suffix in the individual storage plugins
Signed-off-by: Davide Faconti <[email protected]>
1 parent 32da16e commit ef4e37b

File tree

3 files changed

+11
-29
lines changed

3 files changed

+11
-29
lines changed

rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp

+1-26
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ std::string strip_parent_path(const std::string & relative_path)
4747
{
4848
return fs::path(relative_path).filename().generic_string();
4949
}
50-
51-
std::string remove_active_from_filename(const std::string& relative_path)
52-
{
53-
fs::path path(relative_path);
54-
auto filename = path.filename().string();
55-
auto active_pos = filename.find(".active");
56-
if(active_pos != std::string::npos) {
57-
filename.replace(active_pos, 7, "");
58-
}
59-
auto new_path = path.parent_path() / filename;
60-
return new_path.string();
61-
}
62-
6350
} // namespace
6451

6552
SequentialWriter::SequentialWriter(
@@ -203,11 +190,7 @@ void SequentialWriter::close()
203190
}
204191

205192
if (storage_) {
206-
// when the storage_ is closed, rename the file to remove ".active" suffix
207-
std::string active_path = storage_->get_relative_file_path();
208-
std::string final_path = remove_active_from_filename(active_path);
209193
storage_.reset(); // Destroy storage before calling WRITE_SPLIT callback to make sure that
210-
fs::rename(active_path, final_path);
211194
// bag file was closed before callback call.
212195
}
213196
if (!metadata_.relative_file_paths.empty()) {
@@ -326,7 +309,7 @@ std::string SequentialWriter::format_storage_uri(
326309
// SequentialWriter is opened with a relative path.
327310
std::stringstream storage_file_name;
328311
storage_file_name << fs::path(base_folder).filename().generic_string() << "_" <<
329-
storage_count << ".active";
312+
storage_count;
330313

331314
return (fs::path(base_folder) / storage_file_name.str()).generic_string();
332315
}
@@ -343,15 +326,7 @@ void SequentialWriter::switch_to_next_storage()
343326
storage_options_.uri = format_storage_uri(
344327
base_folder_,
345328
metadata_.relative_file_paths.size());
346-
347-
// when the storage_ is closed, rename the file to remove ".active" suffix
348-
std::string active_path = storage_->get_relative_file_path();
349-
std::string final_path = remove_active_from_filename(active_path);
350-
351329
storage_ = storage_factory_->open_read_write(storage_options_);
352-
353-
std::filesystem::rename(active_path, final_path);
354-
355330
if (!storage_) {
356331
std::stringstream errmsg;
357332
errmsg << "Failed to rollover bagfile to new file: \"" << storage_options_.uri << "\"!";

rosbag2_storage_mcap/src/mcap_storage.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include <algorithm>
4444
#include <cstring>
45+
#include <filesystem>
4546
#include <memory>
4647
#include <mutex>
4748
#include <optional>
@@ -316,6 +317,7 @@ MCAPStorage::~MCAPStorage()
316317
}
317318
if (mcap_writer_) {
318319
mcap_writer_->close();
320+
std::filesystem::rename(relative_path_ + ".active", relative_path_);
319321
}
320322
}
321323

@@ -400,7 +402,7 @@ void MCAPStorage::open_impl(const std::string & uri, const std::string & preset_
400402
YAML::convert<McapWriterOptions>::decode(yaml_node, options);
401403
}
402404

403-
auto status = mcap_writer_->open(relative_path_, options);
405+
auto status = mcap_writer_->open(relative_path_ + ".active", options);
404406
if (!status.ok()) {
405407
throw std::runtime_error(status.message);
406408
}

rosbag2_storage_sqlite3/src/rosbag2_storage_sqlite3/sqlite_storage.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ SqliteStorage::~SqliteStorage()
157157
if (active_transaction_) {
158158
commit_transaction();
159159
}
160+
if (is_read_write(storage_mode_) && database_) {
161+
std::filesystem::rename(
162+
relative_path_ + ".active", relative_path_);
163+
}
160164
}
161165

162166
SqliteStorage::PresetProfile SqliteStorage::parse_preset_profile(const std::string & profile_string)
@@ -210,9 +214,10 @@ void SqliteStorage::open(
210214
"Failed to read from bag: File '" + relative_path_ + "' does not exist!");
211215
}
212216
}
213-
217+
std::string path = is_read_write(io_flag) ? (relative_path_ + ".active") : relative_path_;
218+
214219
try {
215-
database_ = std::make_unique<SqliteWrapper>(relative_path_, io_flag, std::move(pragmas));
220+
database_ = std::make_unique<SqliteWrapper>(path, io_flag, std::move(pragmas));
216221
} catch (const SqliteException & e) {
217222
throw std::runtime_error("Failed to setup storage. Error: " + std::string(e.what()));
218223
}

0 commit comments

Comments
 (0)