Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding db directory creation to rosbag2_cpp #450

Merged
merged 7 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions ros2bag/ros2bag/verb/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from rclpy.qos import InvalidQoSProfileException
from ros2bag.api import convert_yaml_to_qos_profile
from ros2bag.api import create_bag_directory
from ros2bag.api import print_error
from ros2bag.verb import VerbExtension
from ros2cli.node import NODE_NAME_PREFIX
Expand Down Expand Up @@ -115,8 +114,6 @@ def main(self, *, args): # noqa: D102
except (InvalidQoSProfileException, ValueError) as e:
return print_error(str(e))

create_bag_directory(uri)

if args.all:
# NOTE(hidmic): in merged install workspaces on Windows, Python entrypoint lookups
# combined with constrained environments (as imposed by colcon test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ void SequentialCompressionWriter::open(
converter_ = std::make_unique<rosbag2_cpp::Converter>(converter_options, converter_factory_);
}

rcpputils::fs::path db_path(base_folder_);
if (db_path.is_directory()) {
std::stringstream error;
error << "Database directory already exists (" << db_path.string() <<
"), can't overwrite existing database";
throw std::runtime_error{error.str()};
}

bool dir_created = rcpputils::fs::create_directories(db_path);
if (!dir_created) {
std::stringstream error;
error << "Failed to create database directory (" << db_path.string() << ").";
throw std::runtime_error{error.str()};
}

const auto storage_uri = format_storage_uri(base_folder_, 0);
storage_ = storage_factory_->open_read_write(storage_uri, storage_options.storage_id);
if (!storage_) {
Expand Down
15 changes: 15 additions & 0 deletions rosbag2_cpp/src/rosbag2_cpp/writers/sequential_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ void SequentialWriter::open(
converter_ = std::make_unique<Converter>(converter_options, converter_factory_);
}

rcpputils::fs::path db_path(base_folder_);
if (db_path.is_directory()) {
std::stringstream error;
error << "Database direcotory already exists (" << db_path.string() <<
"), can't overwrite existing database";
throw std::runtime_error{error.str()};
}

bool dir_created = rcpputils::fs::create_directories(db_path);
if (!dir_created) {
std::stringstream error;
error << "Failed to create database direcotory (" << db_path.string() << ").";
throw std::runtime_error{error.str()};
}

const auto storage_uri = format_storage_uri(base_folder_, 0);

storage_ = storage_factory_->open_read_write(storage_uri, storage_options.storage_id);
Expand Down
9 changes: 9 additions & 0 deletions rosbag2_cpp/test/rosbag2_cpp/test_sequential_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class SequentialWriterTest : public Test
storage_options_ = rosbag2_cpp::StorageOptions{};
storage_options_.uri = "uri";

rcpputils::fs::path dir(storage_options_.uri);
rcpputils::fs::remove_all(dir);

ON_CALL(*storage_factory_, open_read_write(_, _)).WillByDefault(
DoAll(
Invoke(
Expand All @@ -59,6 +62,12 @@ class SequentialWriterTest : public Test
*storage_factory_, open_read_write(_, _)).Times(AtLeast(0));
}

~SequentialWriterTest()
{
rcpputils::fs::path dir(storage_options_.uri);
rcpputils::fs::remove_all(dir);
}

std::unique_ptr<StrictMock<MockStorageFactory>> storage_factory_;
std::shared_ptr<NiceMock<MockStorage>> storage_;
std::shared_ptr<StrictMock<MockConverterFactory>> converter_factory_;
Expand Down
2 changes: 0 additions & 2 deletions rosbag2_tests/test/rosbag2_tests/test_rosbag2_cpp_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ TEST(TestRosbag2CPPAPI, minimal_writer_example)
auto rosbag_directory = rcpputils::fs::path("test_rosbag2_writer_api_bag");
// in case the bag was previously not cleaned up
rcpputils::fs::remove_all(rosbag_directory);
// See https://github.com/ros2/rosbag2/issues/448
rcpputils::fs::create_directories(rosbag_directory);
Karsten1987 marked this conversation as resolved.
Show resolved Hide resolved

rosbag2_cpp::StorageOptions storage_options;
storage_options.uri = rosbag_directory.string();
Expand Down