Completed
Pull Request — master (#195)
by Victor
16:47 queued 06:49
created

AbstractScanner::shutdownScanner()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
nc 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\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
	public function __construct(AppConfig $config, ILogger $logger){
73
		$this->appConfig = $config;
74
		$this->logger = $logger;
75
	}
76
77
	public function getStatus(){
78
		if ($this->infectedStatus instanceof Status){
79
			return $this->infectedStatus;
80
		}
81
		if ($this->status instanceof Status){
82
			return $this->status;
83
		}
84
		return new Status();
85
	}
86
87
	/**
88
	 * Synchronous scan
89
	 * @param IScannable $item
90
	 * @return Status
91
	 */
92
	public function scan(IScannable $item) {
93
		$this->initScanner();
94
95
		while (false !== ($chunk = $item->fread())) {
96
			$this->writeChunk($chunk);
97
		}
98
		
99
		$this->shutdownScanner();
100
		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
	public function initScanner(){
124
		$this->byteCount = 0;
125
		if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){
126
			$this->infectedStatus = clone $this->status;
127
		}
128
		$this->status = new Status();
129
	}
130
131
	/**
132
	 * @param string $chunk
133
	 */
134
	protected function writeChunk($chunk){
135
		$this->fwrite(
136
			$this->prepareChunk($chunk)
137
		);
138
	}
139
140
	/**
141
	 * @param string $data
142
	 */
143
	protected final function fwrite($data){
144
		if ($this->isAborted){
145
			return;
146
		}
147
148
		$dataLength = strlen($data);
149
		$streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength());
150
		if ($this->byteCount + $dataLength > $streamSizeLimit){
151
			$this->logger->debug(
152
				'reinit scanner',
153
				['app' => 'files_antivirus']
154
			);
155
			$this->shutdownScanner();
156
			$isReopenSuccessful = $this->retry();
157
		} else {
158
			$isReopenSuccessful = true;
159
		}
160
161
		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
	}
174
175
	/**
176
	 * @return bool
177
	 */
178
	protected function retry(){
179
		$this->initScanner();
180
		if (!is_null($this->lastChunk)) {
181
			return $this->writeRaw($this->lastChunk);
182
		}
183
		return true;
184
	}
185
186
	/**
187
	 * @param $data
188
	 * @return bool
189
	 */
190
	protected function writeRaw($data){
191
		$dataLength = strlen($data);
192
		$bytesWritten = @fwrite($this->getWriteHandle(), $data);
193
		if ($bytesWritten === $dataLength){
194
			$this->byteCount += $bytesWritten;
195
			$this->lastChunk = $data;
196
			return true;
197
		}
198
		return false;
199
	}
200
201
	/**
202
	 * Get a resource to write data into
203
	 * @return resource
204
	 */
205
	protected function getWriteHandle(){
206
		return $this->writeHandle;
207
	}
208
209
	/**
210
	 * Prepare chunk (if required)
211
	 * @param string $data
212
	 * @return string
213
	 */
214
	protected function prepareChunk($data){
215
		return $data;
216
	}
217
}
218