Completed
Push — master ( 4fe26b...4d8bf0 )
by
unknown
03:55
created

BackgroundScanner::tearDownFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 0
crap 1
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 Doctrine\DBAL\Platforms\MySqlPlatform;
12
use OC\Files\Filesystem;
13
use OCP\IL10N;
14
use OCP\Files\IRootFolder;
15
use OCP\IUser;
16
use OCP\IUserSession;
17
18
class BackgroundScanner {
19
20
	const BATCH_SIZE = 10;
21
22
	/** @var IRootFolder */
23
	protected $rootFolder;
24
25
	/** @var \OCP\Files\Folder[] */
26
	protected $userFolders;
27
28
	/** @var ScannerFactory */
29
	private $scannerFactory;
30
31
	/** @var IL10N */
32
	private $l10n;
33
34
	/** @var  AppConfig  */
35
	private $appConfig;
36
37
	/** @var string */
38
	protected $currentFilesystemUser;
39
40
	/** @var \OCP\IUserSession */
41
	protected $userSession;
42
43
	/**
44
	 * A constructor
45
	 *
46
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
47
	 * @param IL10N $l10n
48
	 * @param AppConfig $appConfig
49
	 * @param IRootFolder $rootFolder
50
	 * @param IUserSession $userSession
51
	 */
52 2
	public function __construct(ScannerFactory $scannerFactory,
53
								IL10N $l10n,
54
								AppConfig $appConfig,
55
								IRootFolder $rootFolder,
56
								IUserSession $userSession
57
	){
58 2
		$this->rootFolder = $rootFolder;
59 2
		$this->scannerFactory = $scannerFactory;
60 2
		$this->l10n = $l10n;
61 2
		$this->appConfig = $appConfig;
62 2
		$this->userSession = $userSession;
63 2
	}
64
	
65
	/**
66
	 * Background scanner main job
67
	 * @return null
68
	 */
69 1
	public function run(){
70
		// locate files that are not checked yet
71
		try {
72 1
			$result = $this->getFilesForScan();
73 1
		} catch(\Exception $e) {
74
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
75
			return;
76
		}
77
78 1
		$cnt = 0;
79 1
		while (($row = $result->fetch()) && $cnt < self::BATCH_SIZE) {
80
			try {
81
				$fileId = $row['fileid'];
82
				$userId = $row['user_id'];
83
				/** @var IUser $owner */
84
				$owner = \OC::$server->getUserManager()->get($userId);
85
				if (!$owner instanceof IUser){
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
86
					continue;
87
				}
88
				$this->scanOneFile($owner, $fileId);
89
				// increased only for successfully scanned files
90
				$cnt = $cnt + 1;
91
			} catch (\Exception $e){
92
				\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
93
			}
94
		}
95 1
		$this->tearDownFilesystem();
96 1
	}
97
98 2
	protected function getFilesForScan(){
99 2
		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
100
101 2
		$dbConnection = \OC::$server->getDatabaseConnection();
102 2
		$qb = $dbConnection->getQueryBuilder();
103 2
		if ($dbConnection->getDatabasePlatform() instanceof MySqlPlatform) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Platforms\MySqlPlatform does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
104
			$concatFunction = $qb->createFunction(
105
				"CONCAT('/', mnt.user_id, '/')"
106
			);
107
		} else {
108 2
			$concatFunction = $qb->createFunction(
109 2
				"'/' || " . $qb->getColumnName('mnt.user_id') . " || '/'"
110 2
			);
111
		}
112
113 2
		$sizeLimit = intval($this->appConfig->getAvMaxFileSize());
114 2
		if ( $sizeLimit === -1 ){
115 2
			$sizeLimitExpr = $qb->expr()->neq('fc.size', $qb->expr()->literal('0'));
116 2
		} else {
117
			$sizeLimitExpr = $qb->expr()->andX(
118
				$qb->expr()->neq('fc.size', $qb->expr()->literal('0')),
119
				$qb->expr()->lt('fc.size', $qb->expr()->literal((string) $sizeLimit))
120
			);
121
		}
122
123 2
		$qb->select(['fc.fileid', 'mnt.user_id'])
124 2
			->from('filecache', 'fc')
125 2
			->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
126 2
			->innerJoin(
127 2
				'fc',
128 2
				'mounts',
129 2
				'mnt',
130 2
				$qb->expr()->andX(
131 2
					$qb->expr()->eq('fc.storage', 'mnt.storage_id'),
132 2
					$qb->expr()->eq('mnt.mount_point', $concatFunction)
133 2
				)
134 2
			)
135 2
			->where(
136 2
				$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
137 2
			)
138 2
			->andWhere(
139 2
				$qb->expr()->orX(
140 2
					$qb->expr()->isNull('fa.fileid'),
141 2
					$qb->expr()->gt('fc.mtime', 'fa.check_time')
142 2
				)
143 2
			)
144 2
			->andWhere(
145 2
				$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
146 2
			)
147 2
			->andWhere( $sizeLimitExpr )
148
		;
149 2
		return $qb->execute();
150
	}
151
152
	/**
153
	 * @param IUser $owner
154
	 * @param int $fileId
155
	 */
156
	protected function scanOneFile($owner, $fileId){
157
		$this->initFilesystemForUser($owner);
158
		$view = Filesystem::getView();
159
		$path = $view->getPath($fileId);
160
		if (!is_null($path)) {
161
			$item = new Item($this->l10n, $view, $path, $fileId);
162
			$scanner = $this->scannerFactory->getScanner();
163
			$status = $scanner->scan($item);
164
			$status->dispatch($item, true);
165
		}
166
	}
167
168
	/**
169
	 * @param \OCP\IUser $user
170
	 * @return \OCP\Files\Folder
171
	 */
172
	protected function getUserFolder(IUser $user) {
173
		if (!isset($this->userFolders[$user->getUID()])) {
174
			$userFolder = $this->rootFolder->getUserFolder($user->getUID());
175
			$this->userFolders[$user->getUID()] = $userFolder;
176
		}
177
		return $this->userFolders[$user->getUID()];
178
	}
179
180
	/**
181
	 * @param IUser $user
182
	 */
183
	protected function initFilesystemForUser(IUser $user) {
184
		if ($this->currentFilesystemUser !== $user->getUID()) {
185
			if ($this->currentFilesystemUser !== '') {
186
				$this->tearDownFilesystem();
187
			}
188
			Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files');
189
			$this->userSession->setUser($user);
190
			$this->currentFilesystemUser = $user->getUID();
191
			Filesystem::initMountPoints($user->getUID());
192
		}
193
	}
194
195
	/**
196
	 *
197
	 */
198 1
	protected function tearDownFilesystem(){
199 1
		$this->userSession->setUser(null);
200 1
		\OC_Util::tearDownFS();
201 1
	}
202
203
	/**
204
	 * @deprecated since  v8.0.0
205
	 */
206
	public static function check(){
207
	}
208
}
209