Completed
Push — closes-181 ( 0a3609 )
by Victor
08:40
created

AbstractScanner   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 180
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
shutdownScanner() 0 1 ?
A getStatus() 0 9 3
A scan() 0 10 2
A onAsyncData() 0 3 1
A completeAsyncScan() 0 4 1
A initScanner() 0 7 3
A writeChunk() 0 5 1
C fwrite() 0 31 7
A retry() 0 7 2
A writeRaw() 0 10 2
A getWriteHandle() 0 3 1
A prepareChunk() 0 3 1
1
<?php
2
3
/**
4
* ownCloud - files_antivirus
5
*
6
* @author Manuel Deglado
7
* @copyright 2012 Manuel Deglado [email protected]
8
*
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
* License as published by the Free Software Foundation; either
12
* version 3 of the License, or any later version.
13
*
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
*
19
* You should have received a copy of the GNU Affero General Public
20
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
*
22
*/
23
24
namespace OCA\Files_Antivirus\Scanner;
25
26
use OCA\Files_Antivirus\IScannable;
27
use OCA\Files_Antivirus\Status;
28
29
abstract class AbstractScanner {
30
	
31
	/**
32
	 * Scan result
33
	 * @var Status
34
	 */
35
	protected $status;
36
37
	/**
38
	 * If scanning was done part by part
39
	 * the first detected infected part is stored here
40
	 * @var Status
41
	 */
42
	protected $infectedStatus;
43
44
	/** @var  int */
45
	protected $byteCount;
46
47
	/** @var  resource */
48
	protected $writeHandle;
49
50
	/** @var \OCA\Files_Antivirus\AppConfig */
51
	protected $appConfig;
52
53
	/** @var string */
54
	protected $lastChunk;
55
56
	/** @var bool */
57
	protected $isLogUsed = false;
58
59
	/** @var bool */
60
	protected $isAborted = false;
61
62
	/**
63
	 * Close used resources
64
	 */
65
	abstract protected function shutdownScanner();
66
67
68
	public function getStatus(){
69
		if ($this->infectedStatus instanceof Status){
70
			return $this->infectedStatus;
71
		}
72
		if ($this->status instanceof Status){
73
			return $this->status;
74
		}
75
		return new Status();
76
	}
77
78
	/**
79
	 * Synchronous scan
80
	 * @param IScannable $item
81
	 * @return Status
82
	 */
83
	public function scan(IScannable $item) {
84
		$this->initScanner();
85
86
		while (false !== ($chunk = $item->fread())) {
87
			$this->writeChunk($chunk);
88
		}
89
		
90
		$this->shutdownScanner();
91
		return $this->getStatus();
92
	}
93
	
94
	/**
95
	 * Async scan - new portion of data is available
96
	 * @param string $data
97
	 */
98
	public function onAsyncData($data){
99
		$this->writeChunk($data);
100
	}
101
	
102
	/**
103
	 * Async scan - resource is closed
104
	 * @return Status
105
	 */
106
	public function completeAsyncScan(){
107
		$this->shutdownScanner();
108
		return $this->getStatus();
109
	}
110
	
111
	/**
112
	 * Open write handle. etc
113
	 */
114
	public function initScanner(){
115
		$this->byteCount = 0;
116
		if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){
117
			$this->infectedStatus = clone $this->status;
118
		}
119
		$this->status = new Status();
120
	}
121
122
	/**
123
	 * @param string $chunk
124
	 */
125
	protected function writeChunk($chunk){
126
		$this->fwrite(
127
			$this->prepareChunk($chunk)
128
		);
129
	}
130
131
	/**
132
	 * @param string $data
133
	 */
134
	protected final function fwrite($data){
135
		if ($this->isAborted){
136
			return;
137
		}
138
139
		$dataLength = strlen($data);
140
		$streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength());
141
		if ($this->byteCount + $dataLength > $streamSizeLimit){
142
			\OC::$server->getLogger()->debug(
143
				'reinit scanner',
144
				['app' => 'files_antivirus']
145
			);
146
			$this->shutdownScanner();
147
			$isReopenSuccessful = $this->retry();
148
		} else {
149
			$isReopenSuccessful = true;
150
		}
151
152
		if (!$isReopenSuccessful || !$this->writeRaw($data)){
153
			if (!$this->isLogUsed) {
154
				$this->isLogUsed = true;
155
				\OC::$server->getLogger()->warning(
156
					'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings',
157
					['app' => 'files_antivirus']
158
				);
159
			}
160
			// retry on error
161
			$isRetrySuccessful = $this->retry() && $this->writeRaw($data);
162
			$this->isAborted = !$isRetrySuccessful;
163
		}
164
	}
165
166
	/**
167
	 * @return bool
168
	 */
169
	protected function retry(){
170
		$this->initScanner();
171
		if (!is_null($this->lastChunk)) {
172
			return $this->writeRaw($this->lastChunk);
173
		}
174
		return true;
175
	}
176
177
	/**
178
	 * @param $data
179
	 * @return bool
180
	 */
181
	protected function writeRaw($data){
182
		$dataLength = strlen($data);
183
		$bytesWritten = @fwrite($this->getWriteHandle(), $data);
184
		if ($bytesWritten === $dataLength){
185
			$this->byteCount += $bytesWritten;
186
			$this->lastChunk = $data;
187
			return true;
188
		}
189
		return false;
190
	}
191
192
	/**
193
	 * Get a resource to write data into
194
	 * @return resource
195
	 */
196
	protected function getWriteHandle(){
197
		return $this->writeHandle;
198
	}
199
200
	/**
201
	 * Prepare chunk (if required)
202
	 * @param string $data
203
	 * @return string
204
	 */
205
	protected function prepareChunk($data){
206
		return $data;
207
	}
208
}
209