From 1c1659fbf94119cf035a644b5d07033d7b98d43f Mon Sep 17 00:00:00 2001 From: Karsten Knese Date: Wed, 8 Jan 2020 11:16:53 -0800 Subject: [PATCH 1/2] remove rosbag2 filesystem helper Signed-off-by: Karsten Knese --- .../rosbag2_compression/zstd_compressor.cpp | 7 +- .../rosbag2_compression/zstd_decompressor.cpp | 6 +- .../test_zstd_compressor.cpp | 37 ++--- rosbag2_cpp/src/rosbag2_cpp/info.cpp | 1 - rosbag2_cpp/src/rosbag2_cpp/writer.cpp | 1 - .../rosbag2_cpp/writers/sequential_writer.cpp | 13 +- rosbag2_cpp/test/rosbag2_cpp/test_info.cpp | 18 +-- .../rosbag2_cpp/test_sequential_writer.cpp | 7 +- rosbag2_storage/CMakeLists.txt | 10 +- .../rosbag2_storage/filesystem_helper.hpp | 148 ------------------ rosbag2_storage/package.xml | 1 + .../src/rosbag2_storage/metadata_io.cpp | 13 +- .../test_filesystem_helper.cpp | 108 ------------- .../test_metadata_serialization.cpp | 1 - .../CMakeLists.txt | 4 +- rosbag2_storage_default_plugins/package.xml | 1 + .../sqlite/sqlite_storage.cpp | 14 +- .../sqlite/storage_test_fixture.hpp | 9 +- .../sqlite/test_sqlite_storage.cpp | 25 ++- .../sqlite/test_sqlite_wrapper.cpp | 5 +- .../test/rosbag2_tests/record_fixture.hpp | 8 +- 21 files changed, 90 insertions(+), 347 deletions(-) delete mode 100644 rosbag2_storage/include/rosbag2_storage/filesystem_helper.hpp delete mode 100644 rosbag2_storage/test/rosbag2_storage/test_filesystem_helper.cpp diff --git a/rosbag2_compression/src/rosbag2_compression/zstd_compressor.cpp b/rosbag2_compression/src/rosbag2_compression/zstd_compressor.cpp index 32d081883d..a411483bfb 100644 --- a/rosbag2_compression/src/rosbag2_compression/zstd_compressor.cpp +++ b/rosbag2_compression/src/rosbag2_compression/zstd_compressor.cpp @@ -18,9 +18,9 @@ #include #include -#include "rosbag2_compression/zstd_compressor.hpp" +#include "rcutils/filesystem.h" -#include "rosbag2_storage/filesystem_helper.hpp" +#include "rosbag2_compression/zstd_compressor.hpp" #include "logging.hpp" @@ -65,8 +65,7 @@ std::vector get_input_buffer(const std::string & uri) throw std::runtime_error{"Error opening file"}; } - const auto decompressed_buffer_length = - rosbag2_storage::FilesystemHelper::get_file_size(uri); + const auto decompressed_buffer_length = rcutils_get_file_size(uri.c_str()); if (decompressed_buffer_length == 0) { fclose(file_pointer); diff --git a/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp b/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp index c2e0b2061f..0d629fa946 100644 --- a/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp +++ b/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp @@ -18,12 +18,12 @@ #include #include +#include "rcutils/filesystem.h" + #include "rcpputils/filesystem_helper.hpp" #include "rosbag2_compression/zstd_decompressor.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" - #include "logging.hpp" namespace @@ -68,7 +68,7 @@ std::vector get_input_buffer(const std::string & uri) throw std::runtime_error{errmsg.str()}; } - const auto compressed_buffer_length = rosbag2_storage::FilesystemHelper::get_file_size(uri); + const auto compressed_buffer_length = rcutils_get_file_size(uri.c_str()); if (compressed_buffer_length == 0) { fclose(file_pointer); diff --git a/rosbag2_compression/test/rosbag2_compression/test_zstd_compressor.cpp b/rosbag2_compression/test/rosbag2_compression/test_zstd_compressor.cpp index f3bb235339..bf21161330 100644 --- a/rosbag2_compression/test/rosbag2_compression/test_zstd_compressor.cpp +++ b/rosbag2_compression/test/rosbag2_compression/test_zstd_compressor.cpp @@ -18,9 +18,14 @@ #include #include "rclcpp/rclcpp.hpp" + +#include "rcpputils/filesystem_helper.hpp" + +#include "rcutils/filesystem.h" + #include "rosbag2_compression/zstd_compressor.hpp" #include "rosbag2_compression/zstd_decompressor.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" + #include "rosbag2_test_common/temporary_directory_fixture.hpp" #include "gmock/gmock.h" @@ -84,28 +89,27 @@ class CompressionHelperFixture : public rosbag2_test_common::TemporaryDirectoryF TEST_F(CompressionHelperFixture, zstd_compress_file_uri) { - const auto uri = rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "file1.txt"}); + const auto uri = (rcpputils::fs::path(temporary_dir_path_) / "file1.txt").string(); create_garbage_file(uri); auto zstd_compressor = rosbag2_compression::ZstdCompressor{}; const auto compressed_uri = zstd_compressor.compress_uri(uri); const auto expected_compressed_uri = uri + "." + zstd_compressor.get_compression_identifier(); - const auto uncompressed_file_size = rosbag2_storage::FilesystemHelper::get_file_size(uri); - const auto compressed_file_size = - rosbag2_storage::FilesystemHelper::get_file_size(compressed_uri); + const auto uncompressed_file_size = rcutils_get_file_size(uri.c_str()); + const auto compressed_file_size = rcutils_get_file_size(compressed_uri.c_str()); EXPECT_NE(compressed_uri, uri); EXPECT_EQ(compressed_uri, expected_compressed_uri); EXPECT_LT(compressed_file_size, uncompressed_file_size); EXPECT_GT(compressed_file_size, 0u); - EXPECT_TRUE(rosbag2_storage::FilesystemHelper::file_exists(compressed_uri)); + EXPECT_TRUE(rcpputils::fs::path(compressed_uri).exists()); } TEST_F(CompressionHelperFixture, zstd_decompress_file_uri) { - const auto uri = rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "file1.txt"}); + const auto uri = (rcpputils::fs::path(temporary_dir_path_) / "file1.txt").string(); create_garbage_file(uri); - const auto initial_file_size = rosbag2_storage::FilesystemHelper::get_file_size(uri); + const auto initial_file_size = rcutils_get_file_size(uri.c_str()); auto zstd_compressor = rosbag2_compression::ZstdCompressor{}; const auto compressed_uri = zstd_compressor.compress_uri(uri); @@ -116,19 +120,18 @@ TEST_F(CompressionHelperFixture, zstd_decompress_file_uri) const auto decompressed_uri = zstd_decompressor.decompress_uri(compressed_uri); const auto expected_decompressed_uri = uri; - const auto decompressed_file_size = - rosbag2_storage::FilesystemHelper::get_file_size(decompressed_uri); + const auto decompressed_file_size = rcutils_get_file_size(decompressed_uri.c_str()); EXPECT_NE(compressed_uri, uri); EXPECT_NE(decompressed_uri, compressed_uri); EXPECT_EQ(uri, expected_decompressed_uri); EXPECT_EQ(initial_file_size, decompressed_file_size); - EXPECT_TRUE(rosbag2_storage::FilesystemHelper::file_exists(decompressed_uri)); + EXPECT_TRUE(rcpputils::fs::path(decompressed_uri).exists()); } TEST_F(CompressionHelperFixture, zstd_decompress_file_contents) { - const auto uri = rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "file2.txt"}); + const auto uri = (rcpputils::fs::path(temporary_dir_path_) / "file2.txt").string(); create_garbage_file(uri); auto compressor = rosbag2_compression::ZstdCompressor{}; @@ -138,10 +141,9 @@ TEST_F(CompressionHelperFixture, zstd_decompress_file_contents) const auto decompressed_uri = decompressor.decompress_uri(compressed_uri); const auto initial_data = read_file(uri); - const auto initial_file_size = rosbag2_storage::FilesystemHelper::get_file_size(uri); + const auto initial_file_size = rcutils_get_file_size(uri.c_str()); const auto decompressed_data = read_file(decompressed_uri); - const auto decompressed_file_size = - rosbag2_storage::FilesystemHelper::get_file_size(decompressed_uri); + const auto decompressed_file_size = rcutils_get_file_size(decompressed_uri.c_str()); EXPECT_EQ( initial_data.size() * sizeof(decltype(initial_data)::value_type), @@ -154,7 +156,7 @@ TEST_F(CompressionHelperFixture, zstd_decompress_file_contents) TEST_F(CompressionHelperFixture, zstd_decompress_fails_on_bad_file) { - const auto uri = rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "file3.txt"}); + const auto uri = (rcpputils::fs::path(temporary_dir_path_) / "file3.txt").string(); create_garbage_file(uri); auto decompressor = rosbag2_compression::ZstdDecompressor{}; @@ -163,8 +165,7 @@ TEST_F(CompressionHelperFixture, zstd_decompress_fails_on_bad_file) TEST_F(CompressionHelperFixture, zstd_decompress_fails_on_bad_uri) { - const auto bad_uri = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "bad_uri.txt"}); + const auto bad_uri = (rcpputils::fs::path(temporary_dir_path_) / "bad_uri.txt").string(); auto decompressor = rosbag2_compression::ZstdDecompressor{}; EXPECT_THROW(decompressor.decompress_uri(bad_uri), std::runtime_error); diff --git a/rosbag2_cpp/src/rosbag2_cpp/info.cpp b/rosbag2_cpp/src/rosbag2_cpp/info.cpp index f627ad08d6..f7009b6e77 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/info.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/info.cpp @@ -18,7 +18,6 @@ #include #include -#include "rosbag2_storage/filesystem_helper.hpp" #include "rosbag2_storage/metadata_io.hpp" #include "rosbag2_storage/storage_factory.hpp" diff --git a/rosbag2_cpp/src/rosbag2_cpp/writer.cpp b/rosbag2_cpp/src/rosbag2_cpp/writer.cpp index 46d1142f8c..639fae6d09 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/writer.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/writer.cpp @@ -25,7 +25,6 @@ #include "rosbag2_cpp/storage_options.hpp" #include "rosbag2_cpp/writer_interfaces/base_writer_interface.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" #include "rosbag2_storage/serialized_bag_message.hpp" #include "rosbag2_storage/topic_metadata.hpp" diff --git a/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp b/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp index e5feda6719..6ca5f1a591 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp @@ -21,11 +21,13 @@ #include #include +#include "rcpputils/filesystem_helper.hpp" + +#include "rcutils/filesystem.h" + #include "rosbag2_cpp/info.hpp" #include "rosbag2_cpp/storage_options.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" - namespace rosbag2_cpp { namespace writers @@ -39,10 +41,9 @@ std::string format_storage_uri(const std::string & base_folder, uint64_t storage // The name of the folder needs to be queried in case // SequentialWriter is opened with a relative path. std::stringstream storage_file_name; - storage_file_name << rosbag2_storage::FilesystemHelper::get_folder_name(base_folder) << - "_" << storage_count; + storage_file_name << rcpputils::fs::path(base_folder).filename().string() << "_" << storage_count; - return rosbag2_storage::FilesystemHelper::concat({base_folder, storage_file_name.str()}); + return (rcpputils::fs::path(base_folder) / storage_file_name.str()).string(); } } // namespace @@ -221,7 +222,7 @@ void SequentialWriter::finalize_metadata() metadata_.bag_size = 0; for (const auto & path : metadata_.relative_file_paths) { - metadata_.bag_size += rosbag2_storage::FilesystemHelper::get_file_size(path); + metadata_.bag_size += rcutils_get_file_size(path.c_str()); } metadata_.topics_with_message_count.clear(); diff --git a/rosbag2_cpp/test/rosbag2_cpp/test_info.cpp b/rosbag2_cpp/test/rosbag2_cpp/test_info.cpp index b3cec5226b..6bb7d627a8 100644 --- a/rosbag2_cpp/test/rosbag2_cpp/test_info.cpp +++ b/rosbag2_cpp/test/rosbag2_cpp/test_info.cpp @@ -19,11 +19,12 @@ #include #include +#include "rcpputils/filesystem_helper.hpp" + #include "rosbag2_cpp/info.hpp" #include "rosbag2_cpp/writer.hpp" #include "rosbag2_storage/bag_metadata.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" #include "rosbag2_storage/metadata_io.hpp" #include "rosbag2_test_common/temporary_directory_fixture.hpp" @@ -56,11 +57,9 @@ TEST_F(TemporaryDirectoryFixture, read_metadata_supports_version_2) { " message_count: 200\n"; { - const auto bagfile_path = rosbag2_storage::FilesystemHelper::concat({ - temporary_dir_path_, rosbag2_storage::MetadataIo::metadata_filename - }); - - std::ofstream fout {bagfile_path}; + std::ofstream fout { + (rcpputils::fs::path(temporary_dir_path_) / rosbag2_storage::MetadataIo::metadata_filename) + .string()}; fout << bagfile; } @@ -127,10 +126,9 @@ TEST_F(TemporaryDirectoryFixture, read_metadata_makes_appropriate_call_to_metada " compression_format: \"zstd\"\n" " compression_mode: \"FILE\"\n"); - std::ofstream fout( - rosbag2_storage::FilesystemHelper::concat({ - temporary_dir_path_, rosbag2_storage::MetadataIo::metadata_filename - })); + std::ofstream fout { + (rcpputils::fs::path(temporary_dir_path_) / rosbag2_storage::MetadataIo::metadata_filename) + .string()}; fout << bagfile; fout.close(); diff --git a/rosbag2_cpp/test/rosbag2_cpp/test_sequential_writer.cpp b/rosbag2_cpp/test/rosbag2_cpp/test_sequential_writer.cpp index a77c46d0c4..5b61f74467 100644 --- a/rosbag2_cpp/test/rosbag2_cpp/test_sequential_writer.cpp +++ b/rosbag2_cpp/test/rosbag2_cpp/test_sequential_writer.cpp @@ -19,11 +19,12 @@ #include #include +#include "rcpputils/filesystem_helper.hpp" + #include "rosbag2_cpp/writers/sequential_writer.hpp" #include "rosbag2_cpp/writer.hpp" #include "rosbag2_storage/bag_metadata.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" #include "rosbag2_storage/topic_metadata.hpp" #include "mock_converter.hpp" @@ -237,8 +238,8 @@ TEST_F(SequentialWriterTest, writer_splits_when_storage_bagfile_size_gt_max_bagf static_cast(expected_splits)) << "Storage should have split bagfile " << (expected_splits - 1); - const auto base_path = rosbag2_storage::FilesystemHelper::concat({ - storage_options_.uri, storage_options_.uri}); + const auto base_path = + (rcpputils::fs::path(storage_options_.uri) / storage_options_.uri).string(); int counter = 0; for (const auto & path : fake_metadata_.relative_file_paths) { std::stringstream ss; diff --git a/rosbag2_storage/CMakeLists.txt b/rosbag2_storage/CMakeLists.txt index f8915b2dce..0fb85e3adb 100644 --- a/rosbag2_storage/CMakeLists.txt +++ b/rosbag2_storage/CMakeLists.txt @@ -22,6 +22,7 @@ endif() find_package(ament_cmake REQUIRED) find_package(pluginlib REQUIRED) +find_package(rcpputils REQUIRED) find_package(rcutils REQUIRED) find_package(yaml_cpp_vendor REQUIRED) @@ -36,6 +37,7 @@ target_include_directories(rosbag2_storage PUBLIC include) ament_target_dependencies( rosbag2_storage pluginlib + rcpputils rcutils yaml_cpp_vendor) @@ -105,14 +107,6 @@ if(BUILD_TESTING) target_link_libraries(test_metadata_serialization rosbag2_storage) ament_target_dependencies(test_metadata_serialization rosbag2_test_common) endif() - - ament_add_gmock(test_filesystem_helper - test/rosbag2_storage/test_filesystem_helper.cpp) - if(TARGET test_filesystem_helper) - target_include_directories(test_filesystem_helper PRIVATE include) - target_link_libraries(test_filesystem_helper rosbag2_storage) - ament_target_dependencies(test_filesystem_helper rosbag2_test_common) - endif() endif() ament_package( diff --git a/rosbag2_storage/include/rosbag2_storage/filesystem_helper.hpp b/rosbag2_storage/include/rosbag2_storage/filesystem_helper.hpp deleted file mode 100644 index a4151e1ac4..0000000000 --- a/rosbag2_storage/include/rosbag2_storage/filesystem_helper.hpp +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright 2018 Open Source Robotics Foundation, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef ROSBAG2_STORAGE__FILESYSTEM_HELPER_HPP_ -#define ROSBAG2_STORAGE__FILESYSTEM_HELPER_HPP_ - -#ifdef _WIN32 -# include -#else -# include -#endif - -#include - -#include -#include -#include - -namespace rosbag2_storage -{ - -class FilesystemHelper -{ -public: -#ifdef _WIN32 - static constexpr const char * const separator = "\\"; -#else - static constexpr const char * const separator = "/"; -#endif - - /** - * Concatenates a list of path parts and separates them with a directory separator - * \param parts - * \return The concatenated string - */ - static std::string concat(std::initializer_list parts) - { - std::stringstream ss; - - auto it = parts.begin(); - for (size_t i = 0; i < parts.size(); ++i) { - if (i != 0) { - ss << separator; - } - ss << it[i]; - } - - return ss.str(); - } - - /** - * Returns the name of the folder identified by a file system path - * \param path The path to a folder - * \return The name of the folder - */ - static std::string get_folder_name(const std::string & path) - { - auto last_separator = path.rfind(separator); - if (last_separator == path.size() - 1) { - auto start = path.rfind(separator, last_separator - 1); - return path.substr(start + 1, last_separator - (start + 1)); - } else { - return path.substr( - last_separator == std::string::npos ? 0 : last_separator + 1, path.length()); - } - } - - /** - * Returns the name of the file identified by a file system path - * \param path The file path - * \return The name of the file or an empty string, if the path ends with a separator - */ - static std::string get_file_name(const std::string & path) - { - auto last_separator = path.rfind(separator); - if (last_separator == path.size() - 1) { - return ""; - } else { - return path.substr( - last_separator == std::string::npos ? 0 : last_separator + 1, path.length()); - } - } - - - /** - * Calculates the size of a directory by summarizing the file size of all files - * Note: This operation is not recursive - * \param directory_path The directory path to calculate the size of - * \return The size of the directory - */ - static size_t calculate_directory_size(const std::string & directory_path) - { - size_t dir_size = 0; -#ifdef _WIN32 - WIN32_FIND_DATA data; - HANDLE handle = FindFirstFile(concat({directory_path, "*"}).c_str(), &data); - if (handle != INVALID_HANDLE_VALUE) { - do { - if (strcmp(data.cFileName, ".") != 0 && strcmp(data.cFileName, "..") != 0) { - dir_size += get_file_size(concat({directory_path, data.cFileName})); - } - } while (FindNextFile(handle, &data)); - FindClose(handle); - } - return dir_size; -#else - DIR * dir; - dirent * entry; - if ((dir = opendir(directory_path.c_str())) != nullptr) { - while ((entry = readdir(dir)) != nullptr) { - if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { - dir_size += get_file_size(concat({directory_path, entry->d_name})); - } - } - closedir(dir); - } - return dir_size; -#endif - } - - static size_t get_file_size(const std::string & file_path) - { - struct stat stat_buffer {}; - int rc = stat(file_path.c_str(), &stat_buffer); - return rc == 0 ? static_cast(stat_buffer.st_size) : 0; - } - - static bool file_exists(const std::string & file_path) - { - struct stat stat_buffer {}; - return stat(file_path.c_str(), &stat_buffer) == 0; - } -}; - -} // namespace rosbag2_storage - -#endif // ROSBAG2_STORAGE__FILESYSTEM_HELPER_HPP_ diff --git a/rosbag2_storage/package.xml b/rosbag2_storage/package.xml index ebb964cc1d..4027b5957e 100644 --- a/rosbag2_storage/package.xml +++ b/rosbag2_storage/package.xml @@ -10,6 +10,7 @@ ament_cmake pluginlib + rcpputils rcutils yaml_cpp_vendor diff --git a/rosbag2_storage/src/rosbag2_storage/metadata_io.cpp b/rosbag2_storage/src/rosbag2_storage/metadata_io.cpp index 5f5c64d36a..95df940aa4 100644 --- a/rosbag2_storage/src/rosbag2_storage/metadata_io.cpp +++ b/rosbag2_storage/src/rosbag2_storage/metadata_io.cpp @@ -18,8 +18,11 @@ #include #include +#include "rcpputils/filesystem_helper.hpp" + +#include "rcutils/filesystem.h" + #include "rosbag2_storage/topic_metadata.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" #ifdef _WIN32 // This is necessary because of a bug in yaml-cpp's cmake @@ -172,7 +175,8 @@ BagMetadata MetadataIo::read_metadata(const std::string & uri) try { YAML::Node yaml_file = YAML::LoadFile(get_metadata_file_name(uri)); auto metadata = yaml_file["rosbag2_bagfile_information"].as(); - metadata.bag_size = FilesystemHelper::calculate_directory_size(uri); + rcutils_allocator_t allocator = rcutils_get_default_allocator(); + metadata.bag_size = rcutils_calculate_directory_size(uri.c_str(), allocator); return metadata; } catch (const YAML::Exception & ex) { throw std::runtime_error(std::string("Exception on parsing info file: ") + ex.what()); @@ -181,15 +185,14 @@ BagMetadata MetadataIo::read_metadata(const std::string & uri) std::string MetadataIo::get_metadata_file_name(const std::string & uri) { - std::string metadata_file = rosbag2_storage::FilesystemHelper::concat({uri, metadata_filename}); + std::string metadata_file = (rcpputils::fs::path(uri) / metadata_filename).string(); return metadata_file; } bool MetadataIo::metadata_file_exists(const std::string & uri) { - return rosbag2_storage::FilesystemHelper::file_exists( - rosbag2_storage::FilesystemHelper::concat({uri, metadata_filename})); + return rcpputils::fs::path(get_metadata_file_name(uri)).exists(); } } // namespace rosbag2_storage diff --git a/rosbag2_storage/test/rosbag2_storage/test_filesystem_helper.cpp b/rosbag2_storage/test/rosbag2_storage/test_filesystem_helper.cpp deleted file mode 100644 index 225350f2a2..0000000000 --- a/rosbag2_storage/test/rosbag2_storage/test_filesystem_helper.cpp +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright 2018, Bosch Software Innovations GmbH. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include - -#include -#include - -#include "rosbag2_storage/filesystem_helper.hpp" -#include "rosbag2_test_common/temporary_directory_fixture.hpp" - -using namespace ::testing; // NOLINT -using namespace rosbag2_storage; // NOLINT -using namespace rosbag2_test_common; // NOLINT - -class FilesystemHelperFixture : public TemporaryDirectoryFixture {}; - -TEST_F(FilesystemHelperFixture, calculate_directory_size_adds_size_of_two_directories) -{ - std::ofstream out(FilesystemHelper::concat({temporary_dir_path_, "file1.txt"})); - out << "test"; - out.close(); - - std::ofstream out2(FilesystemHelper::concat({temporary_dir_path_, "file2.txt"})); - out2 << "something"; - out2.close(); - - size_t size = FilesystemHelper::calculate_directory_size(temporary_dir_path_); - - EXPECT_THAT(size, Eq(13u)); -} - -TEST_F(FilesystemHelperFixture, file_exists_shows_whether_file_exists) -{ - std::ofstream out(FilesystemHelper::concat({temporary_dir_path_, "file1.txt"})); - out << "test"; - out.close(); - - EXPECT_TRUE(FilesystemHelper::file_exists( - FilesystemHelper::concat({temporary_dir_path_, "file1.txt"}))); - EXPECT_FALSE(FilesystemHelper::file_exists( - FilesystemHelper::concat({temporary_dir_path_, "file2.txt"}))); -} - -TEST(FilesystemHelper, concat_joins_string_list_with_directory_separators) -{ - auto sep = std::string(FilesystemHelper::separator); - - auto path = FilesystemHelper::concat({"some", "path", "to", "a", "folder"}); - - EXPECT_THAT(path, Eq("some" + sep + "path" + sep + "to" + sep + "a" + sep + "folder")); -} - -TEST(FilesystemHelper, concat_returns_empty_string_for_empty_list) -{ - auto path = FilesystemHelper::concat({}); - - EXPECT_THAT(path, Eq("")); -} - -TEST(FilesystemHelper, get_folder_name_for_path_ending_with_separator) -{ - auto sep = std::string(FilesystemHelper::separator); - auto path = FilesystemHelper::concat({"some", "path", "to", "a", "folder"}) + sep; - - auto folder_name = FilesystemHelper::get_folder_name(path); - - EXPECT_THAT(folder_name, Eq("folder")); -} - -TEST(FilesystemHelper, get_folder_name_for_path_not_ending_with_separator) -{ - auto path = FilesystemHelper::concat({"some", "path", "to", "a", "folder"}); - - auto folder_name = FilesystemHelper::get_folder_name(path); - - EXPECT_THAT(folder_name, Eq("folder")); -} - -TEST(FilesystemHelper, get_file_name_returns_last_part_of_path) -{ - auto path = FilesystemHelper::concat({"some", "path", "to", "a", "file.txt"}); - - auto folder_name = FilesystemHelper::get_file_name(path); - - EXPECT_THAT(folder_name, Eq("file.txt")); -} - -TEST(FilesystemHelper, get_file_name_returns_empty_string_if_passed_a_path_with_trailing_separator) -{ - auto sep = std::string(FilesystemHelper::separator); - auto path = FilesystemHelper::concat({"some", "path", "to", "a", "folder"}) + sep; - - auto folder_name = FilesystemHelper::get_file_name(path); - - EXPECT_THAT(folder_name, Eq("")); -} diff --git a/rosbag2_storage/test/rosbag2_storage/test_metadata_serialization.cpp b/rosbag2_storage/test/rosbag2_storage/test_metadata_serialization.cpp index 8dcf2b19a7..7eda04d5fa 100644 --- a/rosbag2_storage/test/rosbag2_storage/test_metadata_serialization.cpp +++ b/rosbag2_storage/test/rosbag2_storage/test_metadata_serialization.cpp @@ -27,7 +27,6 @@ #include "rosbag2_storage/bag_metadata.hpp" #include "rosbag2_storage/metadata_io.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" #include "rosbag2_test_common/temporary_directory_fixture.hpp" using namespace ::testing; // NOLINT diff --git a/rosbag2_storage_default_plugins/CMakeLists.txt b/rosbag2_storage_default_plugins/CMakeLists.txt index 51c3332aa7..2cb9239acd 100644 --- a/rosbag2_storage_default_plugins/CMakeLists.txt +++ b/rosbag2_storage_default_plugins/CMakeLists.txt @@ -22,6 +22,7 @@ endif() find_package(ament_cmake REQUIRED) find_package(pluginlib REQUIRED) +find_package(rcpputils REQUIRED) find_package(rcutils REQUIRED) find_package(rosbag2_storage REQUIRED) find_package(sqlite3_vendor REQUIRED) @@ -34,6 +35,7 @@ add_library(${PROJECT_NAME} SHARED ament_target_dependencies(${PROJECT_NAME} rosbag2_storage + rcpputils rcutils SQLite3 pluginlib) @@ -63,7 +65,7 @@ install( ament_export_include_directories(include) ament_export_libraries(${PROJECT_NAME}) -ament_export_dependencies(rosbag2_storage rcutils sqlite3_vendor SQLite3) +ament_export_dependencies(rosbag2_storage rcpputils rcutils sqlite3_vendor SQLite3) if(BUILD_TESTING) find_package(ament_cmake_gmock REQUIRED) diff --git a/rosbag2_storage_default_plugins/package.xml b/rosbag2_storage_default_plugins/package.xml index de3806883d..99e3a43156 100644 --- a/rosbag2_storage_default_plugins/package.xml +++ b/rosbag2_storage_default_plugins/package.xml @@ -10,6 +10,7 @@ ament_cmake pluginlib + rcpputils rcutils rosbag2_storage sqlite3_vendor diff --git a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_storage.cpp b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_storage.cpp index 8b21cb0634..44a6d08d82 100644 --- a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_storage.cpp +++ b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_storage.cpp @@ -25,7 +25,10 @@ #include #include -#include "rosbag2_storage/filesystem_helper.hpp" +#include "rcpputils/filesystem_helper.hpp" + +#include "rcutils/filesystem.h" + #include "rosbag2_storage/metadata_io.hpp" #include "rosbag2_storage/serialized_bag_message.hpp" #include "rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.hpp" @@ -70,7 +73,7 @@ void SqliteStorage::open( relative_path_ = uri + FILE_EXTENSION; // READ_WRITE requires the DB to not exist. - if (rosbag2_storage::FilesystemHelper::file_exists(relative_path_)) { + if (rcpputils::fs::path(relative_path_).exists()) { throw std::runtime_error( "Failed to create bag: File '" + relative_path_ + "' already exists!"); } @@ -78,7 +81,7 @@ void SqliteStorage::open( relative_path_ = uri; // APPEND and READ_ONLY require the DB to exist - if (!rosbag2_storage::FilesystemHelper::file_exists(relative_path_)) { + if (!rcpputils::fs::path(relative_path_).exists()) { throw std::runtime_error( "Failed to read from bag: File '" + relative_path_ + "' does not exist!"); } @@ -154,8 +157,7 @@ std::vector SqliteStorage::get_all_topics_and_ty uint64_t SqliteStorage::get_bagfile_size() const { - return rosbag2_storage::FilesystemHelper::get_file_size( - get_relative_file_path()); + return rcutils_get_file_size(get_relative_file_path().c_str()); } void SqliteStorage::initialize() @@ -284,7 +286,7 @@ rosbag2_storage::BagMetadata SqliteStorage::get_metadata() metadata.starting_time = std::chrono::time_point(std::chrono::nanoseconds(min_time)); metadata.duration = std::chrono::nanoseconds(max_time) - std::chrono::nanoseconds(min_time); - metadata.bag_size = rosbag2_storage::FilesystemHelper::get_file_size(get_relative_file_path()); + metadata.bag_size = rcutils_get_file_size(get_relative_file_path().c_str()); return metadata; } diff --git a/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/storage_test_fixture.hpp b/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/storage_test_fixture.hpp index 29940bd1b9..833effc0d3 100644 --- a/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/storage_test_fixture.hpp +++ b/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/storage_test_fixture.hpp @@ -25,10 +25,15 @@ #include #include +#include "rcpputils/filesystem_helper.hpp" + #include "rcutils/logging_macros.h" #include "rcutils/snprintf.h" + #include "rosbag2_storage/metadata_io.hpp" + #include "rosbag2_storage_default_plugins/sqlite/sqlite_storage.hpp" + #include "rosbag2_test_common/temporary_directory_fixture.hpp" using namespace ::testing; // NOLINT @@ -91,7 +96,7 @@ class StorageTestFixture : public TemporaryDirectoryFixture std::unique_ptr writable_storage = std::make_unique(); - auto db_file = rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag"}); + auto db_file = (rcpputils::fs::path(temporary_dir_path_) / "rosbag").string(); writable_storage->open(db_file); @@ -116,7 +121,7 @@ class StorageTestFixture : public TemporaryDirectoryFixture std::unique_ptr readable_storage = std::make_unique(); - auto db_file = rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag.db3"}); + auto db_file = (rcpputils::fs::path(temporary_dir_path_) / "rosbag.db3").string(); readable_storage->open( db_file, rosbag2_storage::storage_interfaces::IOFlag::READ_ONLY); diff --git a/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_storage.cpp b/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_storage.cpp index cd6e46973f..e86a4c5334 100644 --- a/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_storage.cpp +++ b/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_storage.cpp @@ -20,9 +20,10 @@ #include #include +#include "rcpputils/filesystem_helper.hpp" + #include "rcutils/snprintf.h" -#include "rosbag2_storage/filesystem_helper.hpp" #include "storage_test_fixture.hpp" using namespace ::testing; // NOLINT @@ -78,8 +79,7 @@ TEST_F(StorageTestFixture, has_next_return_false_if_there_are_no_more_messages) std::unique_ptr readable_storage = std::make_unique(); - auto db_filename = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag.db3"}); + auto db_filename = (rcpputils::fs::path(temporary_dir_path_) / "rosbag.db3").string(); readable_storage->open(db_filename); EXPECT_TRUE(readable_storage->has_next()); @@ -99,8 +99,7 @@ TEST_F(StorageTestFixture, get_next_returns_messages_in_timestamp_order) { std::unique_ptr readable_storage = std::make_unique(); - auto db_filename = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag.db3"}); + auto db_filename = (rcpputils::fs::path(temporary_dir_path_) / "rosbag.db3").string(); readable_storage->open(db_filename); EXPECT_TRUE(readable_storage->has_next()); @@ -117,8 +116,7 @@ TEST_F(StorageTestFixture, get_all_topics_and_types_returns_the_correct_vector) std::make_unique(); // extension is omitted since storage is being created; io_flag = READ_WRITE - const auto read_write_filename = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag"}); + const auto read_write_filename = (rcpputils::fs::path(temporary_dir_path_) / "rosbag").string(); writable_storage->open(read_write_filename); writable_storage->create_topic({"topic1", "type1", "rmw1"}); @@ -153,8 +151,7 @@ TEST_F(StorageTestFixture, get_metadata_returns_correct_struct) { write_messages_to_sqlite(messages); const auto readable_storage = std::make_unique(); - const auto db_filename = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag.db3"}); + const auto db_filename = (rcpputils::fs::path(temporary_dir_path_) / "rosbag.db3").string(); readable_storage->open(db_filename, rosbag2_storage::storage_interfaces::IOFlag::READ_ONLY); const auto metadata = readable_storage->get_metadata(); @@ -178,8 +175,7 @@ TEST_F(StorageTestFixture, get_metadata_returns_correct_struct_if_no_messages) { write_messages_to_sqlite({}); const auto readable_storage = std::make_unique(); - const auto db_filename = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag.db3"}); + const auto db_filename = (rcpputils::fs::path(temporary_dir_path_) / "rosbag.db3").string(); readable_storage->open(db_filename, rosbag2_storage::storage_interfaces::IOFlag::READ_ONLY); const auto metadata = readable_storage->get_metadata(); @@ -199,8 +195,7 @@ TEST_F(StorageTestFixture, remove_topics_and_types_returns_the_empty_vector) { std::make_unique(); // extension is omitted since storage is created; io_flag = READ_WRITE - const auto read_write_filename = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag"}); + const auto read_write_filename = (rcpputils::fs::path(temporary_dir_path_) / "rosbag").string(); writable_storage->open(read_write_filename); writable_storage->create_topic({"topic1", "type1", "rmw1"}); @@ -211,7 +206,6 @@ TEST_F(StorageTestFixture, remove_topics_and_types_returns_the_empty_vector) { writable_storage.reset(); // Remove topics - auto readable_storage = std::make_unique(); readable_storage->open(read_only_filename, @@ -231,8 +225,7 @@ TEST_F(StorageTestFixture, get_relative_file_path_returns_db_name_with_ext) { // check that storage::get_relative_file_path returns the relative path to the sqlite3 db // and that uri is handled properly when storage::open is called with different io_flags // READ_WRITE expects uri to not end in extension - const auto read_write_filename = - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "rosbag"}); + const auto read_write_filename = (rcpputils::fs::path(temporary_dir_path_) / "rosbag").string(); const auto storage_filename = read_write_filename + ".db3"; const auto read_write_storage = std::make_unique(); read_write_storage->open(read_write_filename, diff --git a/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_wrapper.cpp b/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_wrapper.cpp index b99853d23d..01d480c1a7 100644 --- a/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_wrapper.cpp +++ b/rosbag2_storage_default_plugins/test/rosbag2_storage_default_plugins/sqlite/test_sqlite_wrapper.cpp @@ -18,9 +18,10 @@ #include #include +#include "rcpputils/filesystem_helper.hpp" + #include "rcutils/types.h" -#include "rosbag2_storage/filesystem_helper.hpp" #include "rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.hpp" #include "storage_test_fixture.hpp" @@ -33,7 +34,7 @@ class SqliteWrapperTestFixture : public StorageTestFixture SqliteWrapperTestFixture() : StorageTestFixture(), db_( - rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "test.db3"}), + (rcpputils::fs::path(temporary_dir_path_) / "test.db3").string(), rosbag2_storage::storage_interfaces::IOFlag::READ_WRITE) {} diff --git a/rosbag2_tests/test/rosbag2_tests/record_fixture.hpp b/rosbag2_tests/test/rosbag2_tests/record_fixture.hpp index 7e03cdff12..3375cd1c05 100644 --- a/rosbag2_tests/test/rosbag2_tests/record_fixture.hpp +++ b/rosbag2_tests/test/rosbag2_tests/record_fixture.hpp @@ -22,9 +22,9 @@ #include #include -#include "rclcpp/rclcpp.hpp" +#include "rcpputils/filesystem_helper.hpp" -#include "rosbag2_storage/filesystem_helper.hpp" +#include "rclcpp/rclcpp.hpp" #include "rosbag2_storage_default_plugins/sqlite/sqlite_storage.hpp" @@ -45,8 +45,8 @@ class RecordFixture : public TemporaryDirectoryFixture public: RecordFixture() { - root_bag_path_ = rosbag2_storage::FilesystemHelper::concat({temporary_dir_path_, "bag"}); - storage_path_ = rosbag2_storage::FilesystemHelper::concat({root_bag_path_, "bag_0"}); + root_bag_path_ = (rcpputils::fs::path(temporary_dir_path_) / "bag").string(); + storage_path_ = (rcpputils::fs::path(root_bag_path_) / "bag_0").string(); database_path_ = storage_path_ + ".db3"; std::cout << "Database " << database_path_ << " in " << temporary_dir_path_ << std::endl; } From 5a32326f32ef10584047bbb2353f89aaa5f3116a Mon Sep 17 00:00:00 2001 From: Karsten Knese Date: Wed, 8 Jan 2020 11:41:57 -0800 Subject: [PATCH 2/2] alpha sort Signed-off-by: Karsten Knese --- .../src/rosbag2_compression/zstd_decompressor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp b/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp index 0d629fa946..05df3c8d9b 100644 --- a/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp +++ b/rosbag2_compression/src/rosbag2_compression/zstd_decompressor.cpp @@ -18,10 +18,10 @@ #include #include -#include "rcutils/filesystem.h" - #include "rcpputils/filesystem_helper.hpp" +#include "rcutils/filesystem.h" + #include "rosbag2_compression/zstd_decompressor.hpp" #include "logging.hpp"