27 #include <boost/date_time/c_local_time_adjustor.hpp> 28 #include <boost/date_time/posix_time/posix_time.hpp> 29 #include <boost/date_time/posix_time/posix_time_io.hpp> 30 #include <boost/date_time/local_time_adjustor.hpp> 31 #include <boost/filesystem.hpp> 33 #include <aws/core/utils/logging/LogMacros.h> 44 constexpr
int kMaxErrStrSize = 256;
45 constexpr
char kRosBagFileFormat[] =
"%Y-%m-%d-%H-%M-%S";
59 wordexp_t wordexp_result{};
61 int expand_result =
wordexp_ros(dir.c_str(), &wordexp_result, 0);
63 if (0 == expand_result && 1 == wordexp_result.we_wordc) {
64 expanded_dir = *(wordexp_result.we_wordv);
66 std::array<char, kMaxErrStrSize> err_msg{};
67 (void) strerror_r(errno, err_msg.data(), err_msg.max_size());
68 AWS_LOGSTREAM_ERROR(__func__,
"Failed to expand write directory " << expanded_dir <<
" with error " << err_msg.data());
69 wordfree(&wordexp_result);
73 if (!boost::filesystem::exists(expanded_dir)) {
74 AWS_LOGSTREAM_INFO(__func__,
"Provided write directory " << expanded_dir <<
" doesn't exist, creating.");
75 boost::filesystem::create_directories(expanded_dir);
76 if (!boost::filesystem::exists(expanded_dir)) {
77 AWS_LOGSTREAM_ERROR(__func__,
"Failed to create write directory " << expanded_dir);
78 wordfree(&wordexp_result);
83 wordfree(&wordexp_result);
85 int writeable_result = access(expanded_dir.c_str(), W_OK);
86 if (0 != writeable_result) {
87 AWS_LOGSTREAM_WARN(__func__,
"Provided write directory " << expanded_dir <<
" is not writeable");
90 return boost::filesystem::is_directory(expanded_dir) && 0 == writeable_result;
95 const int result = unlink(file_path.c_str());
97 AWS_LOGSTREAM_INFO(__func__,
"Deleted file " << file_path);
100 std::array<char, kMaxErrStrSize> err_msg{};
101 (void) strerror_r(errno, err_msg.data(), err_msg.max_size());
102 if (errno == ENOENT) {
103 AWS_LOGSTREAM_WARN(__func__,
"Failed to delete file: " << file_path <<
' ' << err_msg.data());
106 AWS_LOGSTREAM_ERROR(__func__,
"Failed to delete file: " << file_path <<
' ' << err_msg.data());
112 using local_adj = boost::date_time::c_local_adjustor<boost::posix_time::ptime>;
114 const boost::posix_time::ptime utc_now = boost::posix_time::second_clock::universal_time();
115 const boost::posix_time::ptime now = local_adj::utc_to_local(utc_now);
116 return now - utc_now;
122 std::string file_name = file_path;
123 size_t index = file_path.find_last_of(
'/');
124 if (index != std::string::npos) {
125 file_name = file_path.substr(index+1);
129 std::string bag_name = file_name;
130 index = file_path.find_last_of(
'_');
131 if (index != std::string::npos) {
132 bag_name = file_name.substr(0, index);
136 std::string ts_unparsed = bag_name;
137 index = file_path.find_last_of(
'.');
138 if (index != std::string::npos) {
139 ts_unparsed = bag_name.substr(0, index);
143 std::regex time_stamp_regex(R
"([0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2})"); 145 auto ts_begin = std::sregex_iterator(ts_unparsed.begin(), ts_unparsed.end(), time_stamp_regex);
146 auto ts_end = std::sregex_iterator();
147 while (ts_begin != ts_end) {
152 AWS_LOGSTREAM_WARN(__func__,
"Could not find timestamp in rosbag filename via regex");
155 std::string time_stamp = match.str(0);
158 auto input_facet =
new boost::posix_time::time_input_facet(kRosBagFileFormat);
159 std::stringstream ss;
160 ss.imbue(std::locale(ss.getloc(), input_facet));
162 boost::posix_time::ptime pt;
165 if (pt == boost::posix_time::ptime()) {
166 AWS_LOGSTREAM_WARN(__func__,
"Parsing rosbag file timestamp failed");
175 }
catch (std::exception& e) {
176 AWS_LOGSTREAM_WARN(__func__,
"Parsing rosbag file timestamp threw exception: " << e.what());
183 std::vector<std::string> ros_bags_to_upload;
184 using boost::filesystem::directory_iterator;
185 boost::filesystem::path ros_bag_search_path(search_directory);
186 for (
auto dir_entry = directory_iterator(ros_bag_search_path); dir_entry != directory_iterator(); dir_entry++) {
187 if (boost::filesystem::is_directory(dir_entry->path())) {
190 if (dir_entry->path().extension().string() ==
".bag") {
192 ros_bag.
open(dir_entry->path().string());
194 if (select_file(view_rosbag)){
195 ros_bags_to_upload.push_back(dir_entry->path().string());
196 AWS_LOG_INFO(__func__,
"Adding bag: [%s] to list of bag files to upload.", dir_entry->path().string().c_str());
201 return ros_bags_to_upload;
void open(std::string const &filename, uint32_t mode=bagmode::Read)
bool ExpandAndCreateDir(const std::string &dir, std::string &expanded_dir)
ros::Time GetRosBagStartTime(const std::string &file_path)
Get the time a rosbag started.
int wordexp_ros(const char *words, wordexp_t *pwordexp, int flags)
std::vector< std::string > GetRosbagsToUpload(const std::string &write_directory, const std::function< bool(rosbag::View &)> &select_file)
Aws::Rosbag::RecorderErrorCode DeleteFile(const std::string &file_path)
delete a file
static Time fromBoost(const boost::posix_time::ptime &t)
boost::posix_time::time_duration GetUTCOffset()