42 #include "console_bridge/console.h" 45 using std::priority_queue;
56 Bag::Bag() : encryptor_loader_(
"rosbag_storage",
"rosbag::EncryptorBase")
67 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES 79 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES 113 throw BagException((format(
"Unknown mode: %1%") % (
int) mode).str());
117 seek(0, std::ios::end);
160 seek(0, std::ios::end);
211 (format(
"Unknown compression type: %i") % compression).str());
219 throw BagException(
"Cannot set encryption plugin after chunks are written");
228 string version = string(
"#ROSBAG V") +
VERSION + string(
"\n");
230 CONSOLE_BRIDGE_logDebug(
"Writing VERSION [%llu]: %s", (
unsigned long long)
file_.
getOffset(), version.c_str());
242 char logtypename[100];
243 int version_major, version_minor;
244 #if defined(_MSC_VER) 245 if (sscanf_s(version_line.c_str(),
"#ROS%s V%d.%d", logtypename,
sizeof(logtypename), &version_major, &version_minor) != 3)
247 if (sscanf(version_line.c_str(),
"#ROS%99s V%d.%d", logtypename, &version_major, &version_minor) != 3)
251 version_ = version_major * 100 + version_minor;
253 CONSOLE_BRIDGE_logDebug(
"Read VERSION: version=%d",
version_);
271 seek(0, std::ios::end);
308 for (
unsigned int i = 0; i < chunk_info.connection_counts.size(); i++)
327 seek(0, std::ios::end);
339 multiset<IndexEntry>
const& index = i->second;
340 IndexEntry const& first_entry = *index.begin();
342 CONSOLE_BRIDGE_logDebug(
"Reading message definition for connection %d at %llu", i->first, (
unsigned long long) first_entry.
chunk_pos);
356 CONSOLE_BRIDGE_logDebug(
"Writing FILE_HEADER [%llu]: index_pos=%llu connection_count=%d chunk_count=%d",
370 uint32_t data_len = 0;
373 write((
char*) &header_len, 4);
374 write((
char*) header_buffer.get(), header_len);
375 write((
char*) &data_len, 4);
380 padding.resize(data_len,
' ');
399 if (index_data_pos_ == 0)
406 std::string encryptor_plugin_name;
408 if (!encryptor_plugin_name.empty()) {
414 CONSOLE_BRIDGE_logDebug(
"Read FILE_HEADER: index_pos=%llu connection_count=%d chunk_count=%d",
418 seek(data_size, std::ios::cur);
466 seek(end_of_chunk_pos);
479 switch (compression) {
488 CONSOLE_BRIDGE_logDebug(
"Writing CHUNK [%llu]: compression=%s compressed=%d uncompressed=%d",
520 uint32_t connection_id = i->first;
521 multiset<IndexEntry>
const& index = i->second;
524 uint32_t index_size = index.size();
534 CONSOLE_BRIDGE_logDebug(
"Writing INDEX_DATA: connection=%d ver=%d count=%d", connection_id,
INDEX_VERSION, index_size);
538 write((
char*) &e.time.sec, 4);
539 write((
char*) &e.time.nsec, 4);
540 write((
char*) &e.offset, 4);
542 CONSOLE_BRIDGE_logDebug(
" - %d.%d: %d", e.time.sec, e.time.nsec, e.offset);
557 uint32_t index_version;
564 CONSOLE_BRIDGE_logDebug(
"Read INDEX_DATA: ver=%d topic=%s count=%d", index_version, topic.c_str(), count);
566 if (index_version != 0)
567 throw BagFormatException((format(
"Unsupported INDEX_DATA version: %1%") % index_version).str());
569 uint32_t connection_id;
574 CONSOLE_BRIDGE_logDebug(
"Creating connection: id=%d topic=%s", connection_id, topic.c_str());
576 connection_info->id = connection_id;
577 connection_info->topic = topic;
583 connection_id = topic_conn_id_iter->second;
587 for (uint32_t i = 0; i < count; i++) {
591 read((
char*) &sec, 4);
592 read((
char*) &nsec, 4);
597 CONSOLE_BRIDGE_logDebug(
" - %d.%d: %llu", sec, nsec, (
unsigned long long) index_entry.
chunk_pos);
604 connection_index.insert(connection_index.end(), index_entry);
619 uint32_t index_version;
620 uint32_t connection_id;
626 CONSOLE_BRIDGE_logDebug(
"Read INDEX_DATA: ver=%d connection=%d count=%d", index_version, connection_id, count);
628 if (index_version != 1)
629 throw BagFormatException((format(
"Unsupported INDEX_DATA version: %1%") % index_version).str());
635 for (uint32_t i = 0; i < count; i++) {
640 read((
char*) &sec, 4);
641 read((
char*) &nsec, 4);
645 CONSOLE_BRIDGE_logDebug(
" - %d.%d: %llu+%d", sec, nsec, (
unsigned long long) index_entry.
chunk_pos, index_entry.
offset);
652 connection_index.insert(connection_index.end(), index_entry);
667 CONSOLE_BRIDGE_logDebug(
"Writing CONNECTION [%llu:%d]: topic=%s id=%d",
715 map<uint32_t, ConnectionInfo*>::iterator key =
connections_.find(
id);
718 connection_info->
id = id;
719 connection_info->
topic = topic;
720 connection_info->
header = boost::make_shared<M_string>();
721 for (M_string::const_iterator i = connection_header.
getValues()->begin(); i != connection_header.
getValues()->end(); i++)
722 (*connection_info->
header)[i->first] = i->second;
723 connection_info->
msg_def = (*connection_info->
header)[
"message_definition"];
725 connection_info->
md5sum = (*connection_info->
header)[
"md5sum"];
728 CONSOLE_BRIDGE_logDebug(
"Read CONNECTION: topic=%s id=%d", topic.c_str(), id);
754 CONSOLE_BRIDGE_logDebug(
"Creating connection: topic=%s md5sum=%s datatype=%s", topic.c_str(), md5sum.c_str(), datatype.c_str());
756 connection_info->id = id;
757 connection_info->topic = topic;
763 connection_info =
connections_[topic_conn_id_iter->second];
765 connection_info->
msg_def = message_definition;
766 connection_info->
datatype = datatype;
767 connection_info->
md5sum = md5sum;
768 connection_info->
header = boost::make_shared<ros::M_string>();
770 (*connection_info->
header)[
"md5sum"] = connection_info->
md5sum;
771 (*connection_info->
header)[
"message_definition"] = connection_info->
msg_def;
773 CONSOLE_BRIDGE_logDebug(
"Read MSG_DEF: topic=%s md5sum=%s datatype=%s", topic.c_str(), md5sum.c_str(), datatype.c_str());
808 CONSOLE_BRIDGE_logDebug(
"readMessageDataRecord: offset=%llu", (
unsigned long long) offset);
860 CONSOLE_BRIDGE_logDebug(
"lz4 compressed_size: %d uncompressed_size: %d",
912 uint32_t chunk_connection_count = chunk_info.connection_counts.size();
920 CONSOLE_BRIDGE_logDebug(
"Writing CHUNK_INFO [%llu]: ver=%d pos=%llu start=%d.%d end=%d.%d",
922 chunk_info.start_time.sec, chunk_info.start_time.nsec,
923 chunk_info.end_time.sec, chunk_info.end_time.nsec);
930 for (map<uint32_t, uint32_t>::const_iterator i = chunk_info.connection_counts.begin(); i != chunk_info.connection_counts.end(); i++) {
931 uint32_t connection_id = i->first;
932 uint32_t count = i->second;
934 write((
char*) &connection_id, 4);
935 write((
char*) &count, 4);
937 CONSOLE_BRIDGE_logDebug(
" - %d: %d", connection_id, count);
953 uint32_t chunk_info_version;
963 uint32_t chunk_connection_count = 0;
966 CONSOLE_BRIDGE_logDebug(
"Read CHUNK_INFO: chunk_pos=%llu connection_count=%d start=%d.%d end=%d.%d",
967 (
unsigned long long) chunk_info.
pos, chunk_connection_count,
972 for (uint32_t i = 0; i < chunk_connection_count; i ++) {
973 uint32_t connection_id, connection_count;
974 read((
char*) &connection_id, 4);
975 read((
char*) &connection_count, 4);
977 CONSOLE_BRIDGE_logDebug(
" %d: %d messages", connection_id, connection_count);
997 write((
char*) &header_len, 4);
998 write((
char*) header_buffer.get(), header_len);
1002 write((
char*) &data_len, 4);
1007 uint32_t header_len;
1010 uint32_t offset = buf.
getSize();
1014 memcpy(buf.
getData() + offset, &header_len, 4);
1016 memcpy(buf.
getData() + offset, header_buffer.get(), header_len);
1020 uint32_t offset = buf.
getSize();
1024 memcpy(buf.
getData() + offset, &data_len, 4);
1033 uint8_t* ptr = start;
1036 uint32_t header_len;
1037 memcpy(&header_len, ptr, 4);
1042 bool parsed = header.
parse(ptr, header_len, error_msg);
1048 memcpy(&data_size, ptr, 4);
1051 bytes_read = ptr - start;
1056 total_bytes_read = 0;
1059 CONSOLE_BRIDGE_logDebug(
"reading header from buffer: offset=%d", offset);
1060 uint32_t bytes_read;
1063 offset += bytes_read;
1064 total_bytes_read += bytes_read;
1076 uint32_t header_len;
1077 read((
char*) &header_len, 4);
1093 read((
char*) &data_size, 4);
1097 M_string::const_iterator
Bag::checkField(
M_string const& fields,
string const& field,
unsigned int min_len,
unsigned int max_len,
bool required)
const {
1098 M_string::const_iterator fitr = fields.find(field);
1099 if (fitr == fields.end()) {
1103 else if ((fitr->second.size() < min_len) || (fitr->second.size() > max_len))
1104 throw BagFormatException((format(
"Field '%1%' is wrong size (%2% bytes)") % field % (uint32_t) fitr->second.size()).str());
1109 bool Bag::readField(
M_string const& fields,
string const& field_name,
bool required,
string& data)
const {
1110 return readField(fields, field_name, 1, UINT_MAX, required, data);
1113 bool Bag::readField(
M_string const& fields,
string const& field_name,
unsigned int min_len,
unsigned int max_len,
bool required,
string& data)
const {
1114 M_string::const_iterator fitr =
checkField(fields, field_name, min_len, max_len, required);
1115 if (fitr == fields.end())
1118 data = fitr->second;
1123 uint64_t packed_time;
1124 if (!
readField(fields, field_name, required, &packed_time))
1127 uint64_t bitmask = (1LL << 33) - 1;
1128 data.
sec = (uint32_t) (packed_time & bitmask);
1129 data.
nsec = (uint32_t) (packed_time >> 32);
1135 uint64_t packed_time = (((uint64_t) field->
nsec) << 32) + field->
sec;
static const std::string COMPRESSION_BZ2
void openRead(std::string const &filename)
open file for reading
static const std::string END_TIME_FIELD_NAME
void startWritingChunk(ros::Time time)
static const std::string CHUNK_COUNT_FIELD_NAME
void read(void *ptr, size_t size)
read size bytes from the file into ptr
static const std::string COMPRESSION_FIELD_NAME
ros::M_string::const_iterator checkField(ros::M_string const &fields, std::string const &field, unsigned int min_len, unsigned int max_len, bool required) const
std::map< std::string, uint32_t > topic_connection_ids_
uint32_t getMajorVersion() const
Get the major-version of the open bag file.
BagMode getMode() const
Get the mode the bag is in.
Bag & operator=(Bag &&other)
void decompressLz4Chunk(ChunkHeader const &chunk_header) const
uint64_t pos
absolute byte offset of chunk record in bag file
void openRead(std::string const &filename)
static const std::string VER_FIELD_NAME
CompressionType getCompression() const
Get the compression method to use for writing chunks.
static const std::string COMPRESSION_LZ4
bool isOpen() const
return true if file is open for reading or writing
void open(std::string const &filename, uint32_t mode=bagmode::Read)
Open a bag file.
void writeConnectionRecord(ConnectionInfo const *connection_info, const bool encrypt)
std::map< uint32_t, ConnectionInfo * > connections_
bool readField(ros::M_string const &fields, std::string const &field_name, bool required, T *data) const
std::string toHeaderString(T const *field) const
static const std::string CONNECTION_FIELD_NAME
const Time TIME_MIN(0, 1)
void startReadingVersion102()
void write(std::string const &s)
boost::shared_ptr< T > createInstance(const std::string &lookup_name)
std::map< ros::M_string, uint32_t > header_connection_ids_
static const std::string INDEX_POS_FIELD_NAME
void setChunkThreshold(uint32_t chunk_threshold)
Set the threshold for creating new chunks.
ros::Time time
timestamp of the message
void readTopicIndexRecord102()
std::map< uint32_t, uint32_t > connection_counts
number of messages in each connection stored in the chunk
Buffer header_buffer_
reusable buffer in which to assemble the record header before writing to file
static const unsigned char OP_CONNECTION
void openWrite(std::string const &filename)
uint32_t getMinorVersion() const
Get the minor-version of the open bag file.
Base class for rosbag exceptions.
bool truncate(uint64_t length)
void readMessageDataHeaderFromBuffer(Buffer &buffer, uint32_t offset, ros::Header &header, uint32_t &data_size, uint32_t &bytes_read) const
void seek(uint64_t offset, int origin=std::ios_base::beg)
seek to given offset from origin
void writeConnectionRecords()
static const std::string DEF_FIELD_NAME
static const std::string ENCRYPTOR_FIELD_NAME
void openAppend(std::string const &filename)
void close()
close the file
void seek(uint64_t pos, int origin=std::ios_base::beg) const
void swap(Bag &a, Bag &b)
void appendDataLengthToBuffer(Buffer &buf, uint32_t data_len)
std::string getFileName() const
return path of currently open file
BagMode
The possible modes to open a bag in.
uint64_t getOffset() const
return current offset from the beginning of the file
void setWriteMode(CompressionType type)
static const unsigned char OP_MSG_DATA
void close()
Close the bag file.
uint32_t getCompressedBytesIn() const
return the number of bytes written to current compressed stream
static const unsigned char OP_CHUNK
void readChunkHeader(ChunkHeader &chunk_header) const
static const std::string VERSION
Buffer decompress_buffer_
reusable buffer to decompress chunks into
uint64_t getSize() const
Get the current size of the bag file (a lower bound)
void appendConnectionRecordToBuffer(Buffer &buf, ConnectionInfo const *connection_info)
std::map< std::string, std::string > M_string
void readMessageDefinitionRecord102()
void readHeaderFromBuffer(Buffer &buffer, uint32_t offset, ros::Header &header, uint32_t &data_size, uint32_t &bytes_read) const
void writeFileHeaderRecord()
static const std::string CHUNK_POS_FIELD_NAME
uint64_t decompressed_chunk_
position of decompressed chunk
void writeHeader(ros::M_string const &fields)
static const std::string START_TIME_FIELD_NAME
uint32_t getChunkOffset() const
Exception thrown on problems reading the bag index.
static const uint32_t CHUNK_INFO_VERSION
boost::shared_ptr< rosbag::EncryptorBase > encryptor_
Exception thrown when on IO problems.
void appendHeaderToBuffer(Buffer &buf, ros::M_string const &fields)
void setCompression(CompressionType compression)
Set the compression method to use for writing chunks.
void writeChunkHeader(CompressionType compression, uint32_t compressed_size, uint32_t uncompressed_size)
static const std::string OP_FIELD_NAME
uint64_t file_header_pos_
Buffer record_buffer_
reusable buffer in which to assemble the record data before writing to file
static const std::string TYPE_FIELD_NAME
uint32_t readMessageDataSize(IndexEntry const &index_entry) const
ROSTIME_DECL const Time TIME_MAX
Buffer chunk_buffer_
reusable buffer to read chunk into
ChunkInfo curr_chunk_info_
static const std::string COUNT_FIELD_NAME
void decompressChunk(uint64_t chunk_pos) const
std::map< uint32_t, std::multiset< IndexEntry > > connection_indexes_
uint64_t chunk_pos
absolute byte offset of the chunk record containing the message
static const unsigned char OP_FILE_HEADER
static const unsigned char OP_INDEX_DATA
uint32_t connection_count_
void readConnectionIndexRecord200()
void readChunkInfoRecord()
void decompressRawChunk(ChunkHeader const &chunk_header) const
void openWrite(std::string const &filename)
open file for writing
uint32_t offset
relative byte offset of the message record (either definition or data) in the chunk ...
uint32_t chunk_threshold_
std::vector< ChunkInfo > chunks_
boost::shared_ptr< ros::M_string > header
void startReadingVersion200()
void read(char *b, std::streamsize n) const
bool readHeader(ros::Header &header) const
void writeDataLength(uint32_t data_len)
uint32_t getChunkThreshold() const
Get the threshold for creating new chunks.
bool isOp(ros::M_string &fields, uint8_t reqOp) const
CompressionType compression_
void decompress(CompressionType compression, uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
std::map< uint32_t, std::multiset< IndexEntry > > curr_chunk_connection_indexes_
static const std::string CONNECTION_COUNT_FIELD_NAME
static const uint32_t FILE_HEADER_LENGTH
uint64_t curr_chunk_data_pos_
static const std::string MD5_FIELD_NAME
Buffer outgoing_chunk_buffer_
reusable buffer to read chunk into
std::string getFileName() const
Get the filename of the bag.
static const unsigned char OP_CHUNK_INFO
bool readDataLength(uint32_t &data_size) const
pluginlib::ClassLoader< rosbag::EncryptorBase > encryptor_loader_
static const std::string SIZE_FIELD_NAME
void readConnectionRecord()
static const std::string COMPRESSION_NONE
ros::Time end_time
latest timestamp of a message in the chunk
void readMessageDataRecord102(uint64_t offset, ros::Header &header) const
void setEncryptorPlugin(const std::string &plugin_name, const std::string &plugin_param=std::string())
Set encryptor of the bag file.
void writeChunkInfoRecords()
static const std::string TOPIC_FIELD_NAME
void openReadWrite(std::string const &filename)
open file for reading & writing
static const unsigned char OP_MSG_DEF
void setSize(uint32_t size)
void write(std::string const &topic, ros::MessageEvent< T > const &event)
Write a message into the bag file.
void readFileHeaderRecord()
static const uint32_t INDEX_VERSION
ros::Header readMessageDataHeader(IndexEntry const &index_entry)
void decompressBz2Chunk(ChunkHeader const &chunk_header) const
ros::Time start_time
earliest timestamp of a message in the chunk