Skip to content

Commit c18724d

Browse files
committed
fix issue #1597
Signed-off-by: Davide Faconti <[email protected]>
1 parent e01e1ef commit c18724d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ 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+
5063
} // namespace
5164

5265
SequentialWriter::SequentialWriter(
@@ -190,7 +203,11 @@ void SequentialWriter::close()
190203
}
191204

192205
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);
193209
storage_.reset(); // Destroy storage before calling WRITE_SPLIT callback to make sure that
210+
fs::rename(active_path, final_path);
194211
// bag file was closed before callback call.
195212
}
196213
if (!metadata_.relative_file_paths.empty()) {
@@ -309,7 +326,7 @@ std::string SequentialWriter::format_storage_uri(
309326
// SequentialWriter is opened with a relative path.
310327
std::stringstream storage_file_name;
311328
storage_file_name << fs::path(base_folder).filename().generic_string() << "_" <<
312-
storage_count;
329+
storage_count << ".active";
313330

314331
return (fs::path(base_folder) / storage_file_name.str()).generic_string();
315332
}
@@ -326,7 +343,15 @@ void SequentialWriter::switch_to_next_storage()
326343
storage_options_.uri = format_storage_uri(
327344
base_folder_,
328345
metadata_.relative_file_paths.size());
346+
347+
// when the storage_ is closed, rename the file to remove ".inactive" suffix
348+
std::string active_path = storage_->get_relative_file_path();
349+
std::string final_path = remove_active_from_filename(active_path);
350+
329351
storage_ = storage_factory_->open_read_write(storage_options_);
352+
353+
std::filesystem::rename(active_path, final_path);
354+
330355
if (!storage_) {
331356
std::stringstream errmsg;
332357
errmsg << "Failed to rollover bagfile to new file: \"" << storage_options_.uri << "\"!";

0 commit comments

Comments
 (0)