Completed
Push — master ( 587ec5...6da211 )
by Morris
8s
created

Local::shutdownScanner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2014 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
10
namespace OCA\Files_Antivirus\Scanner;
11
12
use OCA\Files_Antivirus\AppConfig;
13
14
class Local extends \OCA\Files_Antivirus\Scanner{
15
	
16
	/**
17
	 * @var string
18
	 */
19
	protected $avPath;
20
	
21
	/**
22
	 * STDIN and STDOUT descriptors
23
	 * @var array of resources
24
	 */
25
	private $pipes = array();
26
	
27
	/**
28
	 * Process handle
29
	 * @var resource
30
	 */
31
	private $process;
32
	
33 2
	public function __construct(AppConfig $config){
34 2
		$this->appConfig = $config;
35
		// get the path to the executable
36 2
		$this->avPath = escapeshellcmd($this->appConfig->getAvPath());
37
38
		// check that the executable is available
39 2
		if (!file_exists($this->avPath)) {
40
			throw new \RuntimeException('The antivirus executable could not be found at '.$this->avPath);
41
		}
42 2
	}
43
	
44 2
	public function initScanner(){
45 2
		parent::initScanner();
46
		
47
		// using 2>&1 to grab the full command-line output.
48 2
		$cmd = $this->avPath . " " . $this->appConfig->getCmdline() ." - 2>&1";
49
		$descriptorSpec = array(
50 2
			0 => array("pipe","r"), // STDIN
51 2
			1 => array("pipe","w")  // STDOUT
52 2
		);
53
		
54 2
		$this->process = proc_open($cmd, $descriptorSpec, $this->pipes);
55 2
		if (!is_resource($this->process)) {
56
			throw new \RuntimeException('Error starting process');
57
		}
58 2
	}
59
	
60 2
	protected function shutdownScanner(){
61 2
		fclose($this->pipes[0]);
62 2
		$output = stream_get_contents($this->pipes[1]);
63 2
		fclose($this->pipes[1]);
64
		
65 2
		$result = proc_close($this->process);
66
		
67 2
		\OCP\Util::writeLog('files_antivirus', 'Exit code :: ' . $result . ' Response :: ' . $output, \OCP\Util::DEBUG);
68 2
		$this->status->parseResponse($output, $result);
69 2
	}
70
	
71 2
	protected function getWriteHandle(){
72 2
		return $this->pipes[0];
73
	}
74
	
75 2
	protected function prepareChunk($data){
76 2
		return $data;
77
	}
78
}
79