From b69fb262ada433f39bb7997f61ac489f38232d67 Mon Sep 17 00:00:00 2001 From: Tobias Schmidl <5060861-schtobia@users.noreply.gitlab.com> Date: Thu, 6 Oct 2022 10:26:45 +0200 Subject: [PATCH] Added collector --- collector.hpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ test.cpp | 26 +++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 collector.hpp diff --git a/collector.hpp b/collector.hpp new file mode 100644 index 0000000..fc77fb1 --- /dev/null +++ b/collector.hpp @@ -0,0 +1,56 @@ +/** + * @file collector.hpp + * Collects file events and acts upon them + * + * @author Tobias Schmidl + * @copyright Copyright © 2022, Tobias Schmidl + * SPDX-License-Identifier: MIT + */ + +#pragma once +#include "monitor.hpp" +#include +#include + +// for system() +#include +#include + +constexpr const char* FILTER = "^Core\\.[^.][^.]*\\..*\\.lz4"; + +namespace collector::collector { + +class Collector +{ + monitor::Monitor _monitor; + std::filesystem::path _target_file; + + public: + Collector(boost::asio::io_service& service, + std::filesystem::path&& root_path, + std::filesystem::path&& target_file) + : _monitor(service, std::move(root_path)) + , _target_file(std::move(target_file)) + { + } + Collector(boost::asio::io_service& service, + const std::filesystem::path& root_path, + const std::filesystem::path& target_file) + : _monitor(service, root_path) + , _target_file(target_file) + { + } + + bool operator()() + { + const auto& fs_event = + _monitor.watch(std::regex(FILTER), + boost::asio::dir_monitor_event::event_type::added); + // big fugly hack + std::string call_command = + std::string("/usr/bin/tar cvf ") + _target_file.generic_string() + + std::string(" ") + _monitor.get_root_path().generic_string(); + return system(call_command.c_str()) == 0; + } +}; +} diff --git a/test.cpp b/test.cpp index 570ef94..3f1a286 100644 --- a/test.cpp +++ b/test.cpp @@ -7,9 +7,9 @@ * SPDX-License-Identifier: MIT */ +#include "collector.hpp" #include "monitor.hpp" #include "version.hpp" -#include "collector.hpp" #define BOOST_TEST_MODULE Collector test #include #include @@ -40,3 +40,27 @@ BOOST_AUTO_TEST_CASE(monitor_test) watch_result.path.generic_string())); std::filesystem::remove(test_file); } + +BOOST_AUTO_TEST_CASE(collector_test) +{ + BOOST_TEST_MESSAGE("Test version " << VERSION); + const std::filesystem::path target_file{ "test.tar" }, + test_input_dir{ "test_input" }; + std::filesystem::remove(target_file); + std::filesystem::remove_all(test_input_dir); + std::filesystem::create_directory(test_input_dir); + const auto create_file_result = std::async([&test_input_dir]() { + std::this_thread::sleep_for(5s); + std::ofstream test_file{ test_input_dir / "should_be_in_it" }; + test_file << "Hello World." << std::endl; + test_file.flush(); + + std::ofstream{ test_input_dir / "Core.TEST.00.lz4" }; + std::ofstream{ test_input_dir / "might_not_be_in_it" }; + }); + Collector collector(service, test_input_dir, target_file); + BOOST_TEST(collector()); + BOOST_TEST(std::filesystem::is_regular_file(target_file)); + std::filesystem::remove(target_file); + std::filesystem::remove_all(test_input_dir); +}