Added collector
This commit is contained in:
parent
cf1d3ff933
commit
b69fb262ad
2 changed files with 81 additions and 1 deletions
56
collector.hpp
Normal file
56
collector.hpp
Normal file
|
@ -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 <filesystem>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
|
// for system()
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
26
test.cpp
26
test.cpp
|
@ -7,9 +7,9 @@
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "collector.hpp"
|
||||||
#include "monitor.hpp"
|
#include "monitor.hpp"
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "collector.hpp"
|
|
||||||
#define BOOST_TEST_MODULE Collector test
|
#define BOOST_TEST_MODULE Collector test
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
@ -40,3 +40,27 @@ BOOST_AUTO_TEST_CASE(monitor_test)
|
||||||
watch_result.path.generic_string()));
|
watch_result.path.generic_string()));
|
||||||
std::filesystem::remove(test_file);
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue