Skip to content

Commit 199dd39

Browse files
Add typed events for the filesystem/scanner
Signed-off-by: Christoph Wurst <[email protected]>
1 parent 2077c74 commit 199dd39

15 files changed

Lines changed: 505 additions & 46 deletions

File tree

apps/files/lib/BackgroundJob/ScanFiles.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
namespace OCA\Files\BackgroundJob;
2525

2626
use OC\Files\Utils\Scanner;
27+
use OCP\EventDispatcher\IEventDispatcher;
2728
use OCP\IConfig;
28-
use OCP\IDBConnection;
2929
use OCP\ILogger;
3030
use OCP\IUser;
3131
use OCP\IUserManager;
@@ -41,39 +41,31 @@ class ScanFiles extends \OC\BackgroundJob\TimedJob {
4141
private $config;
4242
/** @var IUserManager */
4343
private $userManager;
44-
/** @var IDBConnection */
45-
private $dbConnection;
44+
/** @var IEventDispatcher */
45+
private $dispatcher;
4646
/** @var ILogger */
4747
private $logger;
48+
4849
/** Amount of users that should get scanned per execution */
4950
const USERS_PER_SESSION = 500;
5051

5152
/**
5253
* @param IConfig|null $config
5354
* @param IUserManager|null $userManager
54-
* @param IDBConnection|null $dbConnection
55+
* @param IEventDispatcher|null $dispatcher
5556
* @param ILogger|null $logger
5657
*/
5758
public function __construct(IConfig $config = null,
5859
IUserManager $userManager = null,
59-
IDBConnection $dbConnection = null,
60+
IEventDispatcher $dispatcher = null,
6061
ILogger $logger = null) {
6162
// Run once per 10 minutes
6263
$this->setInterval(60 * 10);
6364

64-
if (is_null($userManager) || is_null($config)) {
65-
$this->fixDIForJobs();
66-
} else {
67-
$this->config = $config;
68-
$this->userManager = $userManager;
69-
$this->logger = $logger;
70-
}
71-
}
72-
73-
protected function fixDIForJobs() {
74-
$this->config = \OC::$server->getConfig();
75-
$this->userManager = \OC::$server->getUserManager();
76-
$this->logger = \OC::$server->getLogger();
65+
$this->config = $config ?? \OC::$server->getConfig();
66+
$this->userManager = $userManager ?? \OC::$server->getUserManager();
67+
$this->dispatcher = $dispatcher ?? \OC::$server->query(IEventDispatcher::class);
68+
$this->logger = $logger ?? \OC::$server->getLogger();
7769
}
7870

7971
/**
@@ -83,7 +75,8 @@ protected function runScanner(IUser $user) {
8375
try {
8476
$scanner = new Scanner(
8577
$user->getUID(),
86-
$this->dbConnection,
78+
null,
79+
$this->dispatcher,
8780
$this->logger
8881
);
8982
$scanner->backgroundScan('');
@@ -101,7 +94,7 @@ protected function run($argument) {
10194
if ($this->config->getSystemValueBool('files_no_background_scan', false)) {
10295
return;
10396
}
104-
97+
10598
$offset = $this->config->getAppValue('files', 'cronjob_scan_files', 0);
10699
$users = $this->userManager->search('', self::USERS_PER_SESSION, $offset);
107100
if (!count($users)) {

apps/files/lib/Command/Scan.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OC\Core\Command\Base;
3737
use OC\Core\Command\InterruptedException;
3838
use OC\ForbiddenException;
39+
use OCP\EventDispatcher\IEventDispatcher;
3940
use OCP\Files\Mount\IMountPoint;
4041
use OCP\Files\NotFoundException;
4142
use OCP\Files\StorageNotAvailableException;
@@ -114,7 +115,7 @@ public function checkScanWarning($fullPath, OutputInterface $output) {
114115

115116
protected function scanFiles($user, $path, OutputInterface $output, $backgroundScan = false, $recursive = true, $homeOnly = false) {
116117
$connection = $this->reconnectToDatabase($output);
117-
$scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger());
118+
$scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
118119

119120
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
120121

apps/files/lib/Command/ScanAppData.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OC\Core\Command\Base;
3131
use OC\Core\Command\InterruptedException;
3232
use OC\ForbiddenException;
33+
use OCP\EventDispatcher\IEventDispatcher;
3334
use OCP\Files\IRootFolder;
3435
use OCP\Files\NotFoundException;
3536
use OCP\Files\StorageNotAvailableException;
@@ -85,7 +86,7 @@ protected function scanFiles(OutputInterface $output) {
8586
}
8687

8788
$connection = $this->reconnectToDatabase($output);
88-
$scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->getLogger());
89+
$scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
8990

9091
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
9192
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {

lib/composer/composer/autoload_classmap.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@
220220
'OCP\\Files\\Config\\IUserMountCache' => $baseDir . '/lib/public/Files/Config/IUserMountCache.php',
221221
'OCP\\Files\\EmptyFileNameException' => $baseDir . '/lib/public/Files/EmptyFileNameException.php',
222222
'OCP\\Files\\EntityTooLargeException' => $baseDir . '/lib/public/Files/EntityTooLargeException.php',
223+
'OCP\\Files\\Events\\BeforeFileScannedEvent' => $baseDir . '/lib/public/Files/Events/BeforeFileScannedEvent.php',
224+
'OCP\\Files\\Events\\BeforeFolderScannedEvent' => $baseDir . '/lib/public/Files/Events/BeforeFolderScannedEvent.php',
225+
'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php',
226+
'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php',
227+
'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php',
228+
'OCP\\Files\\Events\\NodeAddedToCache' => $baseDir . '/lib/public/Files/Events/NodeAddedToCache.php',
229+
'OCP\\Files\\Events\\NodeRemovedFromCache' => $baseDir . '/lib/public/Files/Events/NodeRemovedFromCache.php',
223230
'OCP\\Files\\File' => $baseDir . '/lib/public/Files/File.php',
224231
'OCP\\Files\\FileInfo' => $baseDir . '/lib/public/Files/FileInfo.php',
225232
'OCP\\Files\\FileNameTooLongException' => $baseDir . '/lib/public/Files/FileNameTooLongException.php',

lib/composer/composer/autoload_static.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
249249
'OCP\\Files\\Config\\IUserMountCache' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IUserMountCache.php',
250250
'OCP\\Files\\EmptyFileNameException' => __DIR__ . '/../../..' . '/lib/public/Files/EmptyFileNameException.php',
251251
'OCP\\Files\\EntityTooLargeException' => __DIR__ . '/../../..' . '/lib/public/Files/EntityTooLargeException.php',
252+
'OCP\\Files\\Events\\BeforeFileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeFileScannedEvent.php',
253+
'OCP\\Files\\Events\\BeforeFolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeFolderScannedEvent.php',
254+
'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php',
255+
'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php',
256+
'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php',
257+
'OCP\\Files\\Events\\NodeAddedToCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToCache.php',
258+
'OCP\\Files\\Events\\NodeRemovedFromCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeRemovedFromCache.php',
252259
'OCP\\Files\\File' => __DIR__ . '/../../..' . '/lib/public/Files/File.php',
253260
'OCP\\Files\\FileInfo' => __DIR__ . '/../../..' . '/lib/public/Files/FileInfo.php',
254261
'OCP\\Files\\FileNameTooLongException' => __DIR__ . '/../../..' . '/lib/public/Files/FileNameTooLongException.php',

lib/private/Files/Utils/Scanner.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@
3636
use OC\Hooks\PublicEmitter;
3737
use OC\Lock\DBLockingProvider;
3838
use OCA\Files_Sharing\SharedStorage;
39+
use OCP\EventDispatcher\IEventDispatcher;
40+
use OCP\Files\Events\BeforeFileScannedEvent;
41+
use OCP\Files\Events\BeforeFolderScannedEvent;
42+
use OCP\Files\Events\NodeAddedToCache;
43+
use OCP\Files\Events\FileCacheUpdated;
44+
use OCP\Files\Events\NodeRemovedFromCache;
45+
use OCP\Files\Events\FileScannedEvent;
46+
use OCP\Files\Events\FolderScannedEvent;
3947
use OCP\Files\NotFoundException;
4048
use OCP\Files\Storage\IStorage;
4149
use OCP\Files\StorageNotAvailableException;
50+
use OCP\IDBConnection;
4251
use OCP\ILogger;
4352

4453
/**
@@ -53,19 +62,16 @@
5362
class Scanner extends PublicEmitter {
5463
const MAX_ENTRIES_TO_COMMIT = 10000;
5564

56-
/**
57-
* @var string $user
58-
*/
65+
/** @var string $user */
5966
private $user;
6067

61-
/**
62-
* @var \OCP\IDBConnection
63-
*/
68+
/** @var IDBConnection */
6469
protected $db;
6570

66-
/**
67-
* @var ILogger
68-
*/
71+
/** @var IEventDispatcher */
72+
private $dispatcher;
73+
74+
/** @var ILogger */
6975
protected $logger;
7076

7177
/**
@@ -84,13 +90,15 @@ class Scanner extends PublicEmitter {
8490

8591
/**
8692
* @param string $user
87-
* @param \OCP\IDBConnection $db
93+
* @param IDBConnection|null $db
94+
* @param IEventDispatcher $dispatcher
8895
* @param ILogger $logger
8996
*/
90-
public function __construct($user, $db, ILogger $logger) {
91-
$this->logger = $logger;
97+
public function __construct($user, $db, IEventDispatcher $dispatcher, ILogger $logger) {
9298
$this->user = $user;
9399
$this->db = $db;
100+
$this->dispatcher = $dispatcher;
101+
$this->logger = $logger;
94102
// when DB locking is used, no DB transactions will be used
95103
$this->useTransaction = !(\OC::$server->getLockingProvider() instanceof DBLockingProvider);
96104
}
@@ -121,18 +129,21 @@ protected function getMounts($dir) {
121129
*/
122130
protected function attachListener($mount) {
123131
$scanner = $mount->getStorage()->getScanner();
124-
$emitter = $this;
125-
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
126-
$emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
132+
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount) {
133+
$this->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
134+
$this->dispatcher->dispatchTyped(new BeforeFileScannedEvent($mount->getMountPoint() . $path));
127135
});
128-
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
129-
$emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
136+
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount) {
137+
$this->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
138+
$this->dispatcher->dispatchTyped(new BeforeFolderScannedEvent($mount->getMountPoint() . $path));
130139
});
131-
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) {
132-
$emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
140+
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount) {
141+
$this->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
142+
$this->dispatcher->dispatchTyped(new FileScannedEvent($mount->getMountPoint() . $path));
133143
});
134-
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) {
135-
$emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
144+
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount) {
145+
$this->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
146+
$this->dispatcher->dispatchTyped(new FolderScannedEvent($mount->getMountPoint() . $path));
136147
});
137148
}
138149

@@ -225,12 +236,15 @@ public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECUR
225236

226237
$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
227238
$this->postProcessEntry($storage, $path);
239+
$this->dispatcher->dispatchTyped(new NodeRemovedFromCache($storage, $path));
228240
});
229241
$scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
230242
$this->postProcessEntry($storage, $path);
243+
$this->dispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
231244
});
232245
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
233246
$this->postProcessEntry($storage, $path);
247+
$this->dispatcher->dispatchTyped(new NodeAddedToCache($storage, $path));
234248
});
235249

236250
if (!$storage->file_exists($relativePath)) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2019 Christoph Wurst <[email protected]>
7+
*
8+
* @author 2019 Christoph Wurst <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
namespace OCP\Files\Events;
27+
28+
use OCP\EventDispatcher\Event;
29+
30+
/**
31+
* @since 18.0.0
32+
*/
33+
class BeforeFileScannedEvent extends Event {
34+
35+
/** @var string */
36+
private $absolutePath;
37+
38+
/**
39+
* @param string $absolutePath
40+
*
41+
* @since 18.0.0
42+
*/
43+
public function __construct(string $absolutePath) {
44+
parent::__construct();
45+
$this->absolutePath = $absolutePath;
46+
}
47+
48+
/**
49+
* @return string
50+
* @since 18.0.0
51+
*/
52+
public function getAbsolutePath(): string {
53+
return $this->absolutePath;
54+
}
55+
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2019 Christoph Wurst <[email protected]>
7+
*
8+
* @author 2019 Christoph Wurst <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
namespace OCP\Files\Events;
27+
28+
use OCP\EventDispatcher\Event;
29+
30+
/**
31+
* @since 18.0.0
32+
*/
33+
class BeforeFolderScannedEvent extends Event {
34+
35+
/** @var string */
36+
private $absolutePath;
37+
38+
/**
39+
* @param string $absolutePath
40+
*
41+
* @since 18.0.0
42+
*/
43+
public function __construct(string $absolutePath) {
44+
parent::__construct();
45+
$this->absolutePath = $absolutePath;
46+
}
47+
48+
/**
49+
* @return string
50+
* @since 18.0.0
51+
*/
52+
public function getAbsolutePath(): string {
53+
return $this->absolutePath;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)