Completed
Pull Request — master (#195)
by Victor
14:02 queued 12:49
created

AbstractScanner::getWriteHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 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 4
	public function getStatus(){
69 4
		if ($this->infectedStatus instanceof Status){
70
			return $this->infectedStatus;
71
		}
72 4
		if ($this->status instanceof Status){
73 4
			return $this->status;
74
		}
75
		return new Status();
76
	}
77
78
	/**
79
	 * Synchronous scan
80
	 * @param IScannable $item
81
	 * @return Status
82
	 */
83 2
	public function scan(IScannable $item) {
84 2
		$this->initScanner();
85
86 2
		while (false !== ($chunk = $item->fread())) {
87 2
			$this->writeChunk($chunk);
88
		}
89
		
90 2
		$this->shutdownScanner();
91 2
		return $this->getStatus();
92
	}
93
	
94
	/**
95
	 * Async scan - new portion of data is available
96
	 * @param string $data
97
	 */
98 2
	public function onAsyncData($data){
99 2
		$this->writeChunk($data);
100 2
	}
101
	
102
	/**
103
	 * Async scan - resource is closed
104
	 * @return Status
105
	 */
106 2
	public function completeAsyncScan(){
107 2
		$this->shutdownScanner();
108 2
		return $this->getStatus();
109
	}
110
	
111
	/**
112
	 * Open write handle. etc
113
	 */
114 4
	public function initScanner(){
115 4
		$this->byteCount = 0;
116 4
		if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){
117
			$this->infectedStatus = clone $this->status;
118
		}
119 4
		$this->status = new Status();
120 4
	}
121
122
	/**
123
	 * @param string $chunk
124
	 */
125 4
	protected function writeChunk($chunk){
126 4
		$this->fwrite(
127 4
			$this->prepareChunk($chunk)
128
		);
129 4
	}
130
131
	/**
132
	 * @param string $data
133
	 */
134 4
	protected final function fwrite($data){
135 4
		if ($this->isAborted){
136
			return;
137
		}
138
139 4
		$dataLength = strlen($data);
140 4
		$streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength());
141 4
		if ($this->byteCount + $dataLength > $streamSizeLimit){
142 3
			\OC::$server->getLogger()->debug(
143 3
				'reinit scanner',
144 3
				['app' => 'files_antivirus']
145
			);
146 3
			$this->shutdownScanner();
147 3
			$isReopenSuccessful = $this->retry();
148
		} else {
149 2
			$isReopenSuccessful = true;
150
		}
151
152 4
		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 4
	}
165
166
	/**
167
	 * @return bool
168
	 */
169 3
	protected function retry(){
170 3
		$this->initScanner();
171 3
		if (!is_null($this->lastChunk)) {
172 1
			return $this->writeRaw($this->lastChunk);
173
		}
174 2
		return true;
175
	}
176
177
	/**
178
	 * @param $data
179
	 * @return bool
180
	 */
181 4
	protected function writeRaw($data){
182 4
		$dataLength = strlen($data);
183 4
		$bytesWritten = @fwrite($this->getWriteHandle(), $data);
184 4
		if ($bytesWritten === $dataLength){
185 4
			$this->byteCount += $bytesWritten;
186 4
			$this->lastChunk = $data;
187 4
			return true;
188
		}
189
		return false;
190
	}
191
192
	/**
193
	 * Get a resource to write data into
194
	 * @return resource
195
	 */
196 4
	protected function getWriteHandle(){
197 4
		return $this->writeHandle;
198
	}
199
200
	/**
201
	 * Prepare chunk (if required)
202
	 * @param string $data
203
	 * @return string
204
	 */
205 2
	protected function prepareChunk($data){
206 2
		return $data;
207
	}
208
}
209