async part

This commit is contained in:
Green Sky 2023-10-25 17:00:35 +02:00
parent 150bbe05f7
commit 12df9de1b4
No known key found for this signature in database
6 changed files with 91 additions and 19 deletions

View File

@ -1,25 +1,57 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
add_library(solanaceae_clamav
# TODO: seperate interface lib
add_library(solanaceae_clamav_interface INTERFACE
./solanaceae/clamav/clamav_module_interface.hpp
)
target_include_directories(solanaceae_clamav_interface INTERFACE .)
target_compile_features(solanaceae_clamav_interface INTERFACE cxx_std_17)
########################################
add_library(solanaceae_clamav
./solanaceae/clamav/clamav_module.hpp
./solanaceae/clamav/clamav_module.cpp
)
target_include_directories(solanaceae_clamav PUBLIC .)
target_compile_features(solanaceae_clamav PUBLIC cxx_std_17)
target_link_libraries(solanaceae_clamav PUBLIC
solanaceae_clamav_interface
EXT_SOL::libclamav
solanaceae_util
)
add_executable(solanaceae_clamav_test
./solanaceae/clamav/test_exe.cpp
########################################
add_library(solanaceae_clamav_async_interface INTERFACE
./solanaceae/clamav/clamav_module_async_interface.hpp
)
target_include_directories(solanaceae_clamav_test PUBLIC .)
target_compile_features(solanaceae_clamav_test PUBLIC cxx_std_17)
target_link_libraries(solanaceae_clamav_test PUBLIC
solanaceae_clamav
#solanaceae_util
target_include_directories(solanaceae_clamav_async_interface INTERFACE .)
target_compile_features(solanaceae_clamav_async_interface INTERFACE cxx_std_17)
########################################
add_library(solanaceae_clamav_async_wrapper
./solanaceae/clamav/clamav_module_async_wrapper.hpp
./solanaceae/clamav/clamav_module_async_wrapper.cpp
)
target_link_libraries(solanaceae_clamav_async_wrapper PUBLIC
solanaceae_clamav_interface
solanaceae_clamav_async_interface
)
########################################
if (SOLANACEAE_CLAMAV_STANDALONE)
add_executable(solanaceae_clamav_test
./solanaceae/clamav/test_exe.cpp
)
target_include_directories(solanaceae_clamav_test PUBLIC .)
target_compile_features(solanaceae_clamav_test PUBLIC cxx_std_17)
target_link_libraries(solanaceae_clamav_test PUBLIC
solanaceae_clamav
#solanaceae_util
)
endif()

View File

@ -18,7 +18,7 @@ class ClamAVModule : public ClamAVModuleInterface {
ClamAVModule(ConfigModelI& conf);
~ClamAVModule(void);
ScanResult scanFilePath(std::string_view path);
ScanResult scanFilePath(std::string_view path) override;
};

View File

@ -0,0 +1,14 @@
#pragma once
#include "./clamav_module_interface.hpp"
#include <future>
// non-blocking
struct ClamAVModuleAsyncInterface {
using ScanResult = ClamAVModuleInterface::ScanResult;
virtual std::future<ScanResult> scanFilePath(std::string_view path) = 0;
};

View File

@ -0,0 +1,17 @@
#include "./clamav_module_async_wrapper.hpp"
ClamAVModuleAsyncWrapper::ClamAVModuleAsyncWrapper(ClamAVModuleInterface& cavmi) : _cavmi(cavmi) {
}
std::future<ClamAVModuleAsyncWrapper::ScanResult> ClamAVModuleAsyncWrapper::scanFilePath(std::string_view path) {
return std::async(
std::launch::async,
[this](std::string inner_path) {
// TODO: optimize, clamav supports multi threading
std::lock_guard lock_guard{_cavmi_mutex};
return _cavmi.scanFilePath(inner_path);
},
std::string{path} // copy
);
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "./clamav_module_async_interface.hpp"
#include <mutex>
class ClamAVModuleAsyncWrapper : public ClamAVModuleAsyncInterface {
ClamAVModuleInterface& _cavmi;
std::mutex _cavmi_mutex;
public:
ClamAVModuleAsyncWrapper(ClamAVModuleInterface& cavmi);
std::future<ScanResult> scanFilePath(std::string_view path) override;
};

View File

@ -2,7 +2,6 @@
#include <string_view>
#include <string>
#include <future>
// blocking
struct ClamAVModuleInterface {
@ -15,11 +14,3 @@ struct ClamAVModuleInterface {
};
// non-blocking
struct ClamAVModuleAsyncInterface {
using ScanResult = ClamAVModuleInterface::ScanResult;
virtual std::promise<ScanResult> scanFilePath(std::string_view path) = 0;
};