diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c2d9178..dbe79f1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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() + diff --git a/src/solanaceae/clamav/clamav_module.hpp b/src/solanaceae/clamav/clamav_module.hpp index 00dbc99..ea3c45b 100644 --- a/src/solanaceae/clamav/clamav_module.hpp +++ b/src/solanaceae/clamav/clamav_module.hpp @@ -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; }; diff --git a/src/solanaceae/clamav/clamav_module_async_interface.hpp b/src/solanaceae/clamav/clamav_module_async_interface.hpp new file mode 100644 index 0000000..018deb3 --- /dev/null +++ b/src/solanaceae/clamav/clamav_module_async_interface.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "./clamav_module_interface.hpp" + +#include + +// non-blocking +struct ClamAVModuleAsyncInterface { + using ScanResult = ClamAVModuleInterface::ScanResult; + + virtual std::future scanFilePath(std::string_view path) = 0; + +}; + diff --git a/src/solanaceae/clamav/clamav_module_async_wrapper.cpp b/src/solanaceae/clamav/clamav_module_async_wrapper.cpp new file mode 100644 index 0000000..7fd9e3f --- /dev/null +++ b/src/solanaceae/clamav/clamav_module_async_wrapper.cpp @@ -0,0 +1,17 @@ +#include "./clamav_module_async_wrapper.hpp" + +ClamAVModuleAsyncWrapper::ClamAVModuleAsyncWrapper(ClamAVModuleInterface& cavmi) : _cavmi(cavmi) { +} + +std::future 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 + ); +} + diff --git a/src/solanaceae/clamav/clamav_module_async_wrapper.hpp b/src/solanaceae/clamav/clamav_module_async_wrapper.hpp new file mode 100644 index 0000000..4752eed --- /dev/null +++ b/src/solanaceae/clamav/clamav_module_async_wrapper.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "./clamav_module_async_interface.hpp" + +#include + +class ClamAVModuleAsyncWrapper : public ClamAVModuleAsyncInterface { + ClamAVModuleInterface& _cavmi; + std::mutex _cavmi_mutex; + + public: + + ClamAVModuleAsyncWrapper(ClamAVModuleInterface& cavmi); + + std::future scanFilePath(std::string_view path) override; + +}; + diff --git a/src/solanaceae/clamav/clamav_module_interface.hpp b/src/solanaceae/clamav/clamav_module_interface.hpp index 7ee2a8c..f40e1d7 100644 --- a/src/solanaceae/clamav/clamav_module_interface.hpp +++ b/src/solanaceae/clamav/clamav_module_interface.hpp @@ -2,7 +2,6 @@ #include #include -#include // blocking struct ClamAVModuleInterface { @@ -15,11 +14,3 @@ struct ClamAVModuleInterface { }; -// non-blocking -struct ClamAVModuleAsyncInterface { - using ScanResult = ClamAVModuleInterface::ScanResult; - - virtual std::promise scanFilePath(std::string_view path) = 0; - -}; -