From 464bd7ecd5eda966d5328c88d313fad7498b635a Mon Sep 17 00:00:00 2001 From: Nathan Nguyen Date: Mon, 24 Feb 2020 09:40:57 +1100 Subject: [PATCH] MDL-67504 antivirus_clamav: Add scanning tries option --- lib/antivirus/clamav/classes/scanner.php | 36 +++++++++++-------- .../clamav/lang/en/antivirus_clamav.php | 4 +++ lib/antivirus/clamav/settings.php | 6 ++++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/antivirus/clamav/classes/scanner.php b/lib/antivirus/clamav/classes/scanner.php index 380f60afcc1..624abc4f122 100644 --- a/lib/antivirus/clamav/classes/scanner.php +++ b/lib/antivirus/clamav/classes/scanner.php @@ -72,20 +72,28 @@ class scanner extends \core\antivirus\scanner { // We can do direct stream scanning if unixsocket or tcpsocket running methods are in use, // if not, use default process. - $runningmethod = $this->get_config('runningmethod'); - switch ($runningmethod) { - case 'unixsocket': - case 'tcpsocket': - $return = $this->scan_file_execute_socket($file, $runningmethod); - break; - case 'commandline': - $return = $this->scan_file_execute_commandline($file); - break; - default: - // This should not happen. - debugging('Unknown running method.'); - return self::SCAN_RESULT_ERROR; - } + $maxtries = get_config('antivirus_clamav', 'tries'); + $tries = 0; + do { + $runningmethod = $this->get_config('runningmethod'); + $tries++; + switch ($runningmethod) { + case 'unixsocket': + case 'tcpsocket': + $return = $this->scan_file_execute_socket($file, $runningmethod); + break; + case 'commandline': + $return = $this->scan_file_execute_commandline($file); + break; + default: + // This should not happen. + throw new \coding_exception('Unknown running method.'); + } + } while ($return == self::SCAN_RESULT_ERROR && $tries < $maxtries); + + $notice = get_string('tries_notice', 'antivirus_clamav', + ['tries' => $tries, 'notice' => $this->get_scanning_notice()]); + $this->set_scanning_notice($notice); if ($return === self::SCAN_RESULT_ERROR) { $this->message_admins($this->get_scanning_notice()); diff --git a/lib/antivirus/clamav/lang/en/antivirus_clamav.php b/lib/antivirus/clamav/lang/en/antivirus_clamav.php index 8e8cb1d4c6c..c9eeb6c3bd2 100644 --- a/lib/antivirus/clamav/lang/en/antivirus_clamav.php +++ b/lib/antivirus/clamav/lang/en/antivirus_clamav.php @@ -50,3 +50,7 @@ $string['tcpsockethostdesc'] = 'Domain name of the ClamAV server'; $string['tcpsocketport'] = 'TCP socket port'; $string['tcpsocketportdesc'] = 'The port to use when connecting to ClamAV'; $string['unknownerror'] = 'There was an unknown error with ClamAV.'; +$string['tries'] = 'Scanning attempts'; +$string['tries_desc'] = 'Number of attempts clamav will try when there is an error during scanning process'; +$string['tries_notice'] = 'Clamav scanning has tried {$a->tries} time(s). +{$a->notice}'; diff --git a/lib/antivirus/clamav/settings.php b/lib/antivirus/clamav/settings.php index 855ad7ffe15..96b8dfd08e6 100644 --- a/lib/antivirus/clamav/settings.php +++ b/lib/antivirus/clamav/settings.php @@ -67,4 +67,10 @@ if ($ADMIN->fulltree) { $settings->add(new admin_setting_configselect('antivirus_clamav/clamfailureonupload', new lang_string('clamfailureonupload', 'antivirus_clamav'), new lang_string('configclamfailureonupload', 'antivirus_clamav'), 'donothing', $options)); + + // Number of attempts clamav will try when there is error during a scanning process. + $options = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5); + $settings->add(new admin_setting_configselect('antivirus_clamav/tries', + new lang_string('tries', 'antivirus_clamav'), + new lang_string('tries_desc', 'antivirus_clamav'), 1, $options)); }