From 9b66635c65d58b66c229f446e750a3bddf2f8b6f Mon Sep 17 00:00:00 2001 From: aditya Date: Mon, 19 Dec 2022 00:16:34 -0800 Subject: [PATCH 1/7] Add Changes to enable searching for .msg files in sub-directories Signed-off-by: aditya --- .../src/message_definition_cache.cpp | 54 ++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/rosbag2_storage_mcap/src/message_definition_cache.cpp b/rosbag2_storage_mcap/src/message_definition_cache.cpp index 5cfbebac81..72b589b5b1 100644 --- a/rosbag2_storage_mcap/src/message_definition_cache.cpp +++ b/rosbag2_storage_mcap/src/message_definition_cache.cpp @@ -44,6 +44,12 @@ static const std::unordered_set PRIMITIVE_TYPES{ "bool", "byte", "char", "float32", "float64", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "string"}; +#ifdef _WIN32 +static const std::string SEPARATOR = "\\"; +#else +static const std::string SEPARATOR = "/"; +#endif + static std::set parse_msg_dependencies(const std::string & text, const std::string & package_context) { @@ -119,6 +125,23 @@ static std::string delimiter(const DefinitionIdentifier & definition_identifier) return result; } +static std::vector split_string(const std::string& str, + const std::string& delimiter = "\n") { + std::vector strings; + std::string::size_type pos = 0; + std::string::size_type prev = 0; + + while ((pos = str.find(delimiter, prev)) != std::string::npos) { + strings.push_back(str.substr(prev, pos - prev)); + prev = pos + delimiter.size(); + } + + // Get the last substring (or only, if delimiter is not found) + strings.push_back(str.substr(prev)); + + return strings; +} + MessageSpec::MessageSpec(Format format, std::string text, const std::string & package_context) : dependencies(parse_dependencies(format, text, package_context)) , text(std::move(text)) @@ -139,15 +162,34 @@ const MessageSpec & MessageDefinitionCache::load_message_spec( throw std::invalid_argument("Invalid package resource name: " + definition_identifier.package_resource_name); } - std::string package = match[1]; - std::string share_dir = ament_index_cpp::get_package_share_directory(package); - std::ifstream file{share_dir + "/msg/" + match[2].str() + - extension_for_format(definition_identifier.format)}; - if (!file.good()) { + const std::string package = match[1].str(); + const std::string filename = match[2].str() + extension_for_format(definition_identifier.format); + + // Get the package share directory, or throw a PackageNotFoundError + const std::string share_dir = ament_index_cpp::get_package_share_directory(package); + + // Get the rosidl_interfaces index contents for this package + std::string index_contents; + if (!ament_index_cpp::get_resource("rosidl_interfaces", package, index_contents)) { throw DefinitionNotFoundError(definition_identifier.package_resource_name); } - std::string contents{std::istreambuf_iterator(file), {}}; + // Find the first line that ends with the filename we're looking for + const auto lines = split_string(index_contents); + const auto it = std::find_if(lines.begin(), lines.end(), [&filename](const std::string& line) { + return line.size() >= filename.size() && + line.compare(line.size() - filename.size(), filename.size(), filename) == 0; + }); + if (it == lines.end()) { + throw DefinitionNotFoundError(definition_identifier.package_resource_name); + } + + // Read the file + const std::string full_path = share_dir + SEPARATOR + *it; + std::ifstream file{full_path}; + + const std::string contents{std::istreambuf_iterator(file), {}}; + const MessageSpec & spec = msg_specs_by_definition_identifier_ .emplace(definition_identifier, From bc3f14d4ab0c2901fc8e925bc5a6dc87fb6838b1 Mon Sep 17 00:00:00 2001 From: aditya Date: Wed, 28 Dec 2022 18:57:40 -0800 Subject: [PATCH 2/7] Fix CPP Lint Signed-off-by: aditya --- rosbag2_storage_mcap/src/message_definition_cache.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rosbag2_storage_mcap/src/message_definition_cache.cpp b/rosbag2_storage_mcap/src/message_definition_cache.cpp index 72b589b5b1..be24755a4c 100644 --- a/rosbag2_storage_mcap/src/message_definition_cache.cpp +++ b/rosbag2_storage_mcap/src/message_definition_cache.cpp @@ -45,9 +45,9 @@ static const std::unordered_set PRIMITIVE_TYPES{ "int16", "uint16", "int32", "uint32", "int64", "uint64", "string"}; #ifdef _WIN32 -static const std::string SEPARATOR = "\\"; +static const char SEPARATOR[] = "\\"; #else -static const std::string SEPARATOR = "/"; +static const char SEPARATOR[] = "/"; #endif static std::set parse_msg_dependencies(const std::string & text, From fdf9cb933f26423aa33d5dbb008a1e09f30d5892 Mon Sep 17 00:00:00 2001 From: aditya Date: Wed, 28 Dec 2022 20:21:19 -0800 Subject: [PATCH 3/7] Fix Clang failures Signed-off-by: aditya --- rosbag2_storage_mcap/src/message_definition_cache.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rosbag2_storage_mcap/src/message_definition_cache.cpp b/rosbag2_storage_mcap/src/message_definition_cache.cpp index be24755a4c..10a76bc775 100644 --- a/rosbag2_storage_mcap/src/message_definition_cache.cpp +++ b/rosbag2_storage_mcap/src/message_definition_cache.cpp @@ -125,8 +125,9 @@ static std::string delimiter(const DefinitionIdentifier & definition_identifier) return result; } -static std::vector split_string(const std::string& str, - const std::string& delimiter = "\n") { +static std::vector split_string(const std::string & str, + const std::string & delimiter = "\n") +{ std::vector strings; std::string::size_type pos = 0; std::string::size_type prev = 0; @@ -176,7 +177,7 @@ const MessageSpec & MessageDefinitionCache::load_message_spec( // Find the first line that ends with the filename we're looking for const auto lines = split_string(index_contents); - const auto it = std::find_if(lines.begin(), lines.end(), [&filename](const std::string& line) { + const auto it = std::find_if(lines.begin(), lines.end(), [&filename](const std::string & line) { return line.size() >= filename.size() && line.compare(line.size() - filename.size(), filename.size(), filename) == 0; }); From c8dd8aa47dec95d3a7afb8431a089174de97de3d Mon Sep 17 00:00:00 2001 From: aditya Date: Thu, 5 Jan 2023 14:40:08 -0800 Subject: [PATCH 4/7] Fix for matching to correct message definitions Signed-off-by: aditya --- .../src/message_definition_cache.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rosbag2_storage_mcap/src/message_definition_cache.cpp b/rosbag2_storage_mcap/src/message_definition_cache.cpp index 10a76bc775..44572a2d48 100644 --- a/rosbag2_storage_mcap/src/message_definition_cache.cpp +++ b/rosbag2_storage_mcap/src/message_definition_cache.cpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace rosbag2_storage_mcap::internal { @@ -44,12 +45,6 @@ static const std::unordered_set PRIMITIVE_TYPES{ "bool", "byte", "char", "float32", "float64", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "string"}; -#ifdef _WIN32 -static const char SEPARATOR[] = "\\"; -#else -static const char SEPARATOR[] = "/"; -#endif - static std::set parse_msg_dependencies(const std::string & text, const std::string & package_context) { @@ -178,15 +173,15 @@ const MessageSpec & MessageDefinitionCache::load_message_spec( // Find the first line that ends with the filename we're looking for const auto lines = split_string(index_contents); const auto it = std::find_if(lines.begin(), lines.end(), [&filename](const std::string & line) { - return line.size() >= filename.size() && - line.compare(line.size() - filename.size(), filename.size(), filename) == 0; + std::filesystem::path filePath(line); + return filePath.filename() == filename; }); if (it == lines.end()) { throw DefinitionNotFoundError(definition_identifier.package_resource_name); } // Read the file - const std::string full_path = share_dir + SEPARATOR + *it; + const std::string full_path = share_dir + std::filesystem::path::preferred_separator + *it; std::ifstream file{full_path}; const std::string contents{std::istreambuf_iterator(file), {}}; From 548c64f6089a4e85632c767ab1ef1ecf14f544b6 Mon Sep 17 00:00:00 2001 From: aditya Date: Mon, 30 Jan 2023 17:48:03 -0800 Subject: [PATCH 5/7] change sha hash --- mcap_vendor/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcap_vendor/CMakeLists.txt b/mcap_vendor/CMakeLists.txt index 6de89d2053..5c8dfbd260 100644 --- a/mcap_vendor/CMakeLists.txt +++ b/mcap_vendor/CMakeLists.txt @@ -33,7 +33,7 @@ macro(build_mcap_vendor) include(FetchContent) fetchcontent_declare(mcap URL https://github.com/foxglove/mcap/archive/refs/tags/releases/cpp/v0.8.0.tar.gz - URL_HASH SHA1=b44637791da2c9c1cec61a3ba6994f1ef63a228c # v0.8.0 + URL_HASH SHA1=5f229666c13280af14eeb0485da5100bad606184 # v0.8.0 ) fetchcontent_makeavailable(mcap) From 35e40bd32ba627525fb99663759aa9b3ea4fb724 Mon Sep 17 00:00:00 2001 From: aditya Date: Mon, 30 Jan 2023 18:29:40 -0800 Subject: [PATCH 6/7] Revert "change sha hash" This reverts commit 548c64f6089a4e85632c767ab1ef1ecf14f544b6. --- mcap_vendor/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcap_vendor/CMakeLists.txt b/mcap_vendor/CMakeLists.txt index 5c8dfbd260..6de89d2053 100644 --- a/mcap_vendor/CMakeLists.txt +++ b/mcap_vendor/CMakeLists.txt @@ -33,7 +33,7 @@ macro(build_mcap_vendor) include(FetchContent) fetchcontent_declare(mcap URL https://github.com/foxglove/mcap/archive/refs/tags/releases/cpp/v0.8.0.tar.gz - URL_HASH SHA1=5f229666c13280af14eeb0485da5100bad606184 # v0.8.0 + URL_HASH SHA1=b44637791da2c9c1cec61a3ba6994f1ef63a228c # v0.8.0 ) fetchcontent_makeavailable(mcap) From a39144e531fb2fd11e2df0ceea1d9d27d126c5b9 Mon Sep 17 00:00:00 2001 From: aditya Date: Mon, 30 Jan 2023 18:48:59 -0800 Subject: [PATCH 7/7] remove hash --- mcap_vendor/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/mcap_vendor/CMakeLists.txt b/mcap_vendor/CMakeLists.txt index 6de89d2053..d981fdb2a4 100644 --- a/mcap_vendor/CMakeLists.txt +++ b/mcap_vendor/CMakeLists.txt @@ -33,7 +33,6 @@ macro(build_mcap_vendor) include(FetchContent) fetchcontent_declare(mcap URL https://github.com/foxglove/mcap/archive/refs/tags/releases/cpp/v0.8.0.tar.gz - URL_HASH SHA1=b44637791da2c9c1cec61a3ba6994f1ef63a228c # v0.8.0 ) fetchcontent_makeavailable(mcap)