Completed
Pull Request — stable8.2 (#145)
by Victor
02:18
created

BackgroundScanner::initFS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Copyright (c) 2012 Bart Visscher <[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
namespace OCA\Files_Antivirus;
10
11
use OCP\IUserManager;
12
use OCP\IL10N;
13
14
use OCA\Files_Antivirus\Item;
15
16
class BackgroundScanner {
17
18
	/** @var ScannerFactory */
19
	private $scannerFactory;
20
	
21
	/** @var IUserManager */
22
	private $userManager;
23
24
	/** @var IL10N */
25
	private $l10n;
26
27
	/** @var  AppConfig  */
28
	private $appConfig;
29
30
	/** @var string */
31
	protected $currentFilesystemUser;
32
33
	/** @var \OCP\IUserSession */
34
	protected $userSession;
35
36
	/**
37
	 * A constructor
38
	 *
39
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
40
	 * @param AppConfig $appConfig
41
	 * @param IUserManager $userManager
42
	 * @param IL10N $l10n
43
	 */
44
	public function __construct(ScannerFactory $scannerFactory, AppConfig $appConfig, IUserManager $userManager, IL10N $l10n){
45
		$this->scannerFactory = $scannerFactory;
46
		$this->userManager = $userManager;
47
		$this->l10n = $l10n;
48
		$this->appConfig = $appConfig;
49
	}
50
	
51
	/**
52
	 * Background scanner main job
53
	 * @return null
54
	 */
55
	public function run(){
56
		if (!$this->initFS()) {
57
			return;
58
		}
59
		// locate files that are not checked yet
60
		$dirMimetypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
61
		$sql = 'SELECT `*PREFIX*filecache`.`fileid`, `*PREFIX*storages`.*'
62
			.' FROM `*PREFIX*filecache`'
63
			.' LEFT JOIN `*PREFIX*files_antivirus` ON `*PREFIX*files_antivirus`.`fileid` = `*PREFIX*filecache`.`fileid`'
64
			.' JOIN `*PREFIX*storages` ON `*PREFIX*storages`.`numeric_id` = `*PREFIX*filecache`.`storage`'
65
			.' WHERE `mimetype` != ?'
66
			.' AND (`*PREFIX*storages`.`id` LIKE ? OR `*PREFIX*storages`.`id` LIKE ?)'
67
			.' AND (`*PREFIX*files_antivirus`.`fileid` IS NULL OR `mtime` > `check_time`)'
68
			.' AND `path` LIKE ?'
69
			.' AND `*PREFIX*filecache`.`size` != 0';
70
		$stmt = \OCP\DB::prepare($sql, 5);
71
		try {
72
			$result = $stmt->execute(array($dirMimetypeId, 'local::%', 'home::%', 'files/%'));
73
			if (\OCP\DB::isError($result)) {
74
				\OCP\Util::writeLog('files_antivirus', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
75
				return;
76
			}
77
		} catch(\Exception $e) {
78
			\OCP\Util::writeLog('files_antivirus', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
79
			return;
80
		}
81
	
82
		$view = new \OC\Files\View('/');
83
		while ($row = $result->fetchRow()) {
84
			$path = $view->getPath($row['fileid']);
85
			if (!is_null($path)) {
86
				$item = new Item($this->l10n, $view, $path, $row['fileid']);
87
				$scanner = $this->scannerFactory->getScanner();
88
				$status = $scanner->scan($item);					
89
				$status->dispatch($item, true);
90
			}
91
		}
92
		\OC_Util::tearDownFS();
93
	}
94
	
95
	/**
96
	 * A hack to access files and views. Better than before.
97
	 */
98
	protected function initFS(){
99
		//Need any valid user to mount FS
100
		$results = $this->userManager->search('', 2, 0);
101
		$anyUser = array_pop($results);
102
		if (is_null($anyUser)) {
103
			\OCP\Util::writeLog('files_antivirus', "Failed to setup file system", \OCP\Util::ERROR);
104
			return false;
105
		}
106
		\OC_Util::tearDownFS();
107
		\OC_Util::setupFS($anyUser->getUID());
108
		return true;
109
	}
110
111
	
112
	/**
113
	 * @deprecated 
114
	 */
115
	public static function check(){
116
		return \OCA\Files_Antivirus\Cron\Task::run();
117
	}
118
}
119