Completed
Pull Request — master (#138)
by Victor
02:27
created

BackgroundScanner::tearDownFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 3
nc 1
nop 0
crap 2
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 OC\Files\Filesystem;
12
use OCP\IL10N;
13
use OCP\Files\IRootFolder;
14
use OCP\IUser;
15
use OCP\IUserSession;
16
17
class BackgroundScanner {
18
19
	const BATCH_SIZE = 10;
20
21
	/** @var IRootFolder */
22
	protected $rootFolder;
23
24
	/** @var \OCP\Files\Folder[] */
25
	protected $userFolders;
26
27
	/**
28
	 * @var ScannerFactory
29
	 */
30
	private $scannerFactory;
31
32
	
33
	/**
34
	 * @var IL10N
35
	 */
36
	private $l10n;
37
38
	/** @var string */
39
	protected $currentFilesystemUser;
40
41
	/** @var \OCP\IUserSession */
42
	protected $userSession;
43
44
	/**
45
	 * A constructor
46
	 *
47
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
48
	 * @param IL10N $l10n
49
	 * @param IRootFolder $rootFolder
50
	 * @param IUserSession $userSession
51
	 */
52 1
	public function __construct(ScannerFactory $scannerFactory,
53
								IL10N $l10n,
54
								IRootFolder $rootFolder,
55
								IUserSession $userSession
56
	){
57 1
		$this->rootFolder = $rootFolder;
58 1
		$this->scannerFactory = $scannerFactory;
59 1
		$this->l10n = $l10n;
60 1
		$this->userSession = $userSession;
61 1
	}
62
	
63
	/**
64
	 * Background scanner main job
65
	 * @return null
66
	 */
67 1
	public function run(){
68
		// locate files that are not checked yet
69 1
		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
70
		try {
71 1
			$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
72
73 1
			$concat = \OC::$server->getDatabaseConnection()->getDatabasePlatform()->getConcatExpression(
74 1
				"'/'", 'mnt.user_id'
75
			);
76 1
			$lastConcat = \OC::$server->getDatabaseConnection()->getDatabasePlatform()->getConcatExpression(
77 1
				' ',
78 1
				$qb->expr()->literal('/')
79
			);
80
81 1
			$qb->select(['fc.fileid, mnt.user_id'])
82 1
				->from('filecache', 'fc')
83 1
				->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
84 1
				->innerJoin(
85 1
					'fc',
86 1
					'mounts',
87 1
					'mnt',
88 1
					$qb->expr()->andX(
89 1
						$qb->expr()->eq('fc.storage', 'mnt.storage_id'),
90 1
						$qb->expr()->eq('mnt.mount_point', $concat) . $lastConcat
91
					)
92
				)
93 1
				->where(
94 1
					$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
95
				)
96 1
				->andWhere(
97 1
					$qb->expr()->orX(
98 1
						$qb->expr()->isNull('fa.fileid'),
99 1
						$qb->expr()->gt('fc.mtime', 'fa.check_time')
100
					)
101
				)
102 1
				->andWhere(
103 1
					$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
104
				)
105 1
				->andWhere(
106 1
					$qb->expr()->neq('fc.size', $qb->expr()->literal('0'))
107
				)
108
			;
109 1
			$result = $qb->execute();
110 1
		} catch(\Exception $e) {
111 1
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
112 1
			return;
113
		}
114
115
		$cnt = 0;
116
		while (($row = $result->fetch()) && $cnt < self::BATCH_SIZE) {
117
			try {
118
				$fileId = $row['fileid'];
119
				$userId = $row['user_id'];
120
121
				/** @var IUser $owner */
122
				$owner = \OC::$server->getUserManager()->get($userId);
123
				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...
124
					continue;
125
				}
126
				$this->initFilesystemForUser($owner);
127
				$view = Filesystem::getView();
128
				$path = $view->getPath($fileId);
129
				if (!is_null($path)) {
130
					$item = new Item($this->l10n, $view, $path, $fileId);
131
					$scanner = $this->scannerFactory->getScanner();
132
					$status = $scanner->scan($item);
133
					$status->dispatch($item, true);
134
				}
135
				// increased only for successfully scanned files
136
				$cnt = $cnt + 1;
137
			} catch (\Exception $e){
138
				\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
139
			}
140
		}
141
		$this->tearDownFilesystem();
142
	}
143
144
145
	/**
146
	 * @param \OCP\IUser $user
147
	 * @return \OCP\Files\Folder
148
	 */
149
	protected function getUserFolder(IUser $user) {
150
		if (!isset($this->userFolders[$user->getUID()])) {
151
			$userFolder = $this->rootFolder->getUserFolder($user->getUID());
152
			$this->userFolders[$user->getUID()] = $userFolder;
153
		}
154
		return $this->userFolders[$user->getUID()];
155
	}
156
157
	/**
158
	 * @param IUser $user
159
	 */
160
	protected function initFilesystemForUser(IUser $user) {
161
		if ($this->currentFilesystemUser !== $user->getUID()) {
162
			if ($this->currentFilesystemUser !== '') {
163
				$this->tearDownFilesystem();
164
			}
165
			Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files');
166
			$this->userSession->setUser($user);
167
			$this->currentFilesystemUser = $user->getUID();
168
			Filesystem::initMountPoints($user->getUID());
169
		}
170
	}
171
172
	/**
173
	 *
174
	 */
175
	protected function tearDownFilesystem(){
176
		$this->userSession->setUser(null);
177
		\OC_Util::tearDownFS();
178
	}
179
180
	/**
181
	 * @deprecated since  v8.0.0
182
	 */
183
	public static function check(){
184
	}
185
}
186