Completed
Pull Request — master (#195)
by Victor
10:00 queued 08:41
created

AbstractScanner::fwrite()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10.8579

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 12
cts 21
cp 0.5714
rs 6.7272
cc 7
eloc 21
nc 11
nop 1
crap 10.8579
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\AppConfig;
27
use OCA\Files_Antivirus\IScannable;
28
use OCA\Files_Antivirus\Status;
29
use OCP\ILogger;
30
31
abstract class AbstractScanner {
32
	
33
	/**
34
	 * Scan result
35
	 * @var Status
36
	 */
37
	protected $status;
38
39
	/**
40
	 * If scanning was done part by part
41
	 * the first detected infected part is stored here
42
	 * @var Status
43
	 */
44
	protected $infectedStatus;
45
46
	/** @var  int */
47
	protected $byteCount;
48
49
	/** @var  resource */
50
	protected $writeHandle;
51
52
	/** @var AppConfig */
53
	protected $appConfig;
54
55
	/** @var  ILogger */
56
	protected $logger;
57
58
	/** @var string */
59
	protected $lastChunk;
60
61
	/** @var bool */
62
	protected $isLogUsed = false;
63
64
	/** @var bool */
65
	protected $isAborted = false;
66
67
	/**
68
	 * Close used resources
69
	 */
70
	abstract protected function shutdownScanner();
71
72 9
	public function __construct(AppConfig $config, ILogger $logger){
73 9
		$this->appConfig = $config;
74 9
		$this->logger = $logger;
75 9
	}
76
77 2
	public function getStatus(){
78 2
		if ($this->infectedStatus instanceof Status){
79
			return $this->infectedStatus;
80
		}
81 2
		if ($this->status instanceof Status){
82 2
			return $this->status;
83
		}
84
		return new Status();
85
	}
86
87
	/**
88
	 * Synchronous scan
89
	 * @param IScannable $item
90
	 * @return Status
91
	 */
92 2
	public function scan(IScannable $item) {
93 2
		$this->initScanner();
94
95 2
		while (false !== ($chunk = $item->fread())) {
96 2
			$this->writeChunk($chunk);
97
		}
98
		
99 2
		$this->shutdownScanner();
100 2
		return $this->getStatus();
101
	}
102
	
103
	/**
104
	 * Async scan - new portion of data is available
105
	 * @param string $data
106
	 */
107
	public function onAsyncData($data){
108
		$this->writeChunk($data);
109
	}
110
	
111
	/**
112
	 * Async scan - resource is closed
113
	 * @return Status
114
	 */
115
	public function completeAsyncScan(){
116
		$this->shutdownScanner();
117
		return $this->getStatus();
118
	}
119
	
120
	/**
121
	 * Open write handle. etc
122
	 */
123 4
	public function initScanner(){
124 4
		$this->byteCount = 0;
125 4
		if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){
126
			$this->infectedStatus = clone $this->status;
127
		}
128 4
		$this->status = new Status();
129 4
	}
130
131
	/**
132
	 * @param string $chunk
133
	 */
134 2
	protected function writeChunk($chunk){
135 2
		$this->fwrite(
136 2
			$this->prepareChunk($chunk)
137
		);
138 2
	}
139
140
	/**
141
	 * @param string $data
142
	 */
143 2
	protected final function fwrite($data){
144 2
		if ($this->isAborted){
145
			return;
146
		}
147
148 2
		$dataLength = strlen($data);
149 2
		$streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength());
150 2
		if ($this->byteCount + $dataLength > $streamSizeLimit){
151 2
			$this->logger->debug(
152 2
				'reinit scanner',
153 2
				['app' => 'files_antivirus']
154
			);
155 2
			$this->shutdownScanner();
156 2
			$isReopenSuccessful = $this->retry();
157
		} else {
158
			$isReopenSuccessful = true;
159
		}
160
161 2
		if (!$isReopenSuccessful || !$this->writeRaw($data)){
162
			if (!$this->isLogUsed) {
163
				$this->isLogUsed = true;
164
				$this->logger->warning(
165
					'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings',
166
					['app' => 'files_antivirus']
167
				);
168
			}
169
			// retry on error
170
			$isRetrySuccessful = $this->retry() && $this->writeRaw($data);
171
			$this->isAborted = !$isRetrySuccessful;
172
		}
173 2
	}
174
175
	/**
176
	 * @return bool
177
	 */
178 2
	protected function retry(){
179 2
		$this->initScanner();
180 2
		if (!is_null($this->lastChunk)) {
181
			return $this->writeRaw($this->lastChunk);
182
		}
183 2
		return true;
184
	}
185
186
	/**
187
	 * @param $data
188
	 * @return bool
189
	 */
190 2
	protected function writeRaw($data){
191 2
		$dataLength = strlen($data);
192 2
		$bytesWritten = @fwrite($this->getWriteHandle(), $data);
193 2
		if ($bytesWritten === $dataLength){
194 2
			$this->byteCount += $bytesWritten;
195 2
			$this->lastChunk = $data;
196 2
			return true;
197
		}
198
		return false;
199
	}
200
201
	/**
202
	 * Get a resource to write data into
203
	 * @return resource
204
	 */
205 6
	protected function getWriteHandle(){
206 6
		return $this->writeHandle;
207
	}
208
209
	/**
210
	 * Prepare chunk (if required)
211
	 * @param string $data
212
	 * @return string
213
	 */
214 2
	protected function prepareChunk($data){
215 2
		return $data;
216
	}
217
}
218