diff --git a/rosbag2_cpp/src/rosbag2_cpp/reindexer.cpp b/rosbag2_cpp/src/rosbag2_cpp/reindexer.cpp index 5ae804074..5bf6b65f6 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/reindexer.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/reindexer.cpp @@ -165,6 +165,8 @@ void Reindexer::aggregate_metadata( const rosbag2_storage::StorageOptions & storage_options) { std::map temp_topic_info; + std::chrono::time_point latest_log_start = + std::chrono::time_point::min(); // In order to most accurately reconstruct the metadata, we need to // visit each of the contained relative files files in the bag, @@ -191,6 +193,14 @@ void Reindexer::aggregate_metadata( auto temp_metadata = bag_reader->get_metadata(); metadata_.storage_identifier = temp_metadata.storage_identifier; + // try to find the last log for the most complete custom data section + if (latest_log_start < temp_metadata.starting_time) { + latest_log_start = temp_metadata.starting_time; + if (!temp_metadata.custom_data.empty()) { + metadata_.custom_data = temp_metadata.custom_data; + } + } + if (temp_metadata.starting_time < metadata_.starting_time) { metadata_.starting_time = temp_metadata.starting_time; } diff --git a/rosbag2_tests/test/rosbag2_tests/test_reindexer.cpp b/rosbag2_tests/test/rosbag2_tests/test_reindexer.cpp index 4bbc3b9d3..2aa7a84ed 100644 --- a/rosbag2_tests/test/rosbag2_tests/test_reindexer.cpp +++ b/rosbag2_tests/test/rosbag2_tests/test_reindexer.cpp @@ -80,6 +80,7 @@ class ReindexTestFixture : public ParametrizedTemporaryDirectoryFixture rosbag2_storage::StorageOptions storage_options; storage_options.storage_id = GetParam(); storage_options.uri = root_bag_path_.string(); + storage_options.custom_data["name"] = "value"; writer.open(storage_options); rosbag2_storage::TopicMetadata topic; topic.name = "/test_topic"; @@ -132,6 +133,15 @@ TEST_P(ReindexTestFixture, test_multiple_files) { EXPECT_EQ(generated_metadata.storage_identifier, GetParam()); EXPECT_EQ(generated_metadata.version, target_metadata.version); + if (GetParam() != "mcap") { + // Skip check for mcap storage since it doesn't store serialized metadata in bag directly on + // Iron and Humble. Backporting of the relevant #1423 is not possible without updating + // mcap vendor package to the v1.1.0 because we have dependencies from the + // mcap_reader_->metadataIndexes(); API which became available only in the + // https://github.com/foxglove/mcap/pull/902 + EXPECT_EQ(generated_metadata.custom_data, target_metadata.custom_data); + } + for (const auto & gen_rel_path : generated_metadata.relative_file_paths) { EXPECT_TRUE( std::find(