collector/collector.hpp
2022-10-06 11:22:08 +02:00

56 lines
1.4 KiB
C++

/**
* @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;
}
};
}