Skip to content

Commit 72812b2

Browse files
committed
chore(Scanner): Use modern syntax and APIs
Signed-off-by: Louis Chmn <louis@chmn.me>
1 parent 60d71a9 commit 72812b2

7 files changed

Lines changed: 94 additions & 38 deletions

File tree

apps/files/lib/BackgroundJob/ScanFiles.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
namespace OCA\Files\BackgroundJob;
1010

11+
use OC\Files\SetupManager;
1112
use OC\Files\Utils\Scanner;
1213
use OCP\AppFramework\Utility\ITimeFactory;
1314
use OCP\BackgroundJob\TimedJob;
1415
use OCP\DB\QueryBuilder\IQueryBuilder;
1516
use OCP\EventDispatcher\IEventDispatcher;
1617
use OCP\IConfig;
1718
use OCP\IDBConnection;
19+
use OCP\IUserManager;
1820
use Psr\Log\LoggerInterface;
1921

2022
/**
@@ -33,6 +35,8 @@ public function __construct(
3335
private LoggerInterface $logger,
3436
private IDBConnection $connection,
3537
ITimeFactory $time,
38+
private readonly SetupManager $setupManager,
39+
private readonly IUserManager $userManager,
3640
) {
3741
parent::__construct($time);
3842
// Run once per 10 minutes
@@ -42,10 +46,11 @@ public function __construct(
4246
protected function runScanner(string $user): void {
4347
try {
4448
$scanner = new Scanner(
45-
$user,
49+
$this->userManager->get($user),
4650
null,
4751
$this->dispatcher,
48-
$this->logger
52+
$this->logger,
53+
$this->setupManager,
4954
);
5055
$scanner->backgroundScan('');
5156
} catch (\Exception $e) {

apps/files/lib/Command/Scan.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OC\Core\Command\InterruptedException;
1212
use OC\DB\Connection;
1313
use OC\DB\ConnectionAdapter;
14+
use OC\Files\SetupManager;
1415
use OC\Files\Storage\Wrapper\Jail;
1516
use OC\Files\Utils\Scanner;
1617
use OC\FilesMetadata\FilesMetadataManager;
@@ -49,6 +50,7 @@ public function __construct(
4950
private FilesMetadataManager $filesMetadataManager,
5051
private IEventDispatcher $eventDispatcher,
5152
private LoggerInterface $logger,
53+
private SetupManager $setupManager,
5254
) {
5355
parent::__construct();
5456
}
@@ -111,10 +113,11 @@ protected function scanFiles(
111113
): void {
112114
$connection = $this->reconnectToDatabase($output);
113115
$scanner = new Scanner(
114-
$user,
116+
$this->userManager->get($user),
115117
new ConnectionAdapter($connection),
116-
Server::get(IEventDispatcher::class),
117-
Server::get(LoggerInterface::class)
118+
$this->eventDispatcher,
119+
$this->logger,
120+
$this->setupManager,
118121
);
119122

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

apps/files/lib/Command/ScanAppData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\Core\Command\InterruptedException;
1111
use OC\DB\Connection;
1212
use OC\DB\ConnectionAdapter;
13+
use OC\Files\SetupManager;
1314
use OC\Files\Utils\Scanner;
1415
use OC\ForbiddenException;
1516
use OC\Preview\Storage\StorageFactory;
@@ -60,6 +61,7 @@ protected function getScanner(OutputInterface $output): Scanner {
6061
new ConnectionAdapter($connection),
6162
Server::get(IEventDispatcher::class),
6263
Server::get(LoggerInterface::class),
64+
Server::get(SetupManager::class),
6365
);
6466
}
6567

apps/files/tests/BackgroundJob/ScanFilesTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OCA\Files\Tests\BackgroundJob;
1010

1111
use OC\Files\Mount\MountPoint;
12+
use OC\Files\SetupManager;
1213
use OC\Files\Storage\Temporary;
1314
use OCA\Files\BackgroundJob\ScanFiles;
1415
use OCP\AppFramework\Utility\ITimeFactory;
@@ -17,6 +18,7 @@
1718
use OCP\IConfig;
1819
use OCP\IDBConnection;
1920
use OCP\IUser;
21+
use OCP\IUserManager;
2022
use OCP\Server;
2123
use Psr\Log\LoggerInterface;
2224
use Test\TestCase;
@@ -51,7 +53,9 @@ protected function setUp(): void {
5153
$dispatcher,
5254
$logger,
5355
$connection,
54-
$this->createMock(ITimeFactory::class)
56+
$this->createMock(ITimeFactory::class),
57+
$this->createMock(SetupManager::class),
58+
$this->createMock(IUserManager::class),
5559
])
5660
->onlyMethods(['runScanner'])
5761
->getMock();

lib/private/Files/Utils/Scanner.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\Files\Cache\Cache;
1111
use OC\Files\Filesystem;
1212
use OC\Files\Mount\MountPoint;
13+
use OC\Files\SetupManager;
1314
use OC\Files\Storage\FailedStorage;
1415
use OC\Files\Storage\Home;
1516
use OC\ForbiddenException;
@@ -29,6 +30,7 @@
2930
use OCP\Files\Storage\IStorage;
3031
use OCP\Files\StorageNotAvailableException;
3132
use OCP\IDBConnection;
33+
use OCP\IUser;
3234
use OCP\Lock\ILockingProvider;
3335
use OCP\Lock\LockedException;
3436
use OCP\Server;
@@ -57,10 +59,11 @@ class Scanner extends PublicEmitter {
5759
protected int $entriesToCommit = 0;
5860

5961
public function __construct(
60-
private ?string $user,
61-
protected ?IDBConnection $db,
62-
private IEventDispatcher $dispatcher,
63-
protected LoggerInterface $logger,
62+
private readonly ?IUser $user,
63+
protected readonly ?IDBConnection $db,
64+
private readonly IEventDispatcher $eventDispatcher,
65+
protected readonly LoggerInterface $logger,
66+
private readonly SetupManager $setupManager,
6467
) {
6568
// when DB locking is used, no DB transactions will be used
6669
$this->useTransaction = !(Server::get(ILockingProvider::class) instanceof DBLockingProvider);
@@ -74,8 +77,11 @@ public function __construct(
7477
*/
7578
protected function getMounts($dir) {
7679
//TODO: move to the node based fileapi once that's done
77-
\OC_Util::tearDownFS();
78-
\OC_Util::setupFS($this->user);
80+
$this->setupManager->tearDown();
81+
82+
if ($this->user !== null) {
83+
$this->setupManager->setupForUser($this->user);
84+
}
7985

8086
$mountManager = Filesystem::getMountManager();
8187
$mounts = $mountManager->findIn($dir);
@@ -88,37 +94,32 @@ protected function getMounts($dir) {
8894

8995
/**
9096
* attach listeners to the scanner
91-
*
92-
* @param MountPoint $mount
9397
*/
94-
protected function attachListener($mount) {
98+
protected function attachListener(MountPoint $mount) {
9599
/** @var \OC\Files\Cache\Scanner $scanner */
96100
$scanner = $mount->getStorage()->getScanner();
97101
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount): void {
98102
$this->emit('\OC\Files\Utils\Scanner', 'scanFile', [$mount->getMountPoint() . $path]);
99-
$this->dispatcher->dispatchTyped(new BeforeFileScannedEvent($mount->getMountPoint() . $path));
103+
$this->eventDispatcher->dispatchTyped(new BeforeFileScannedEvent($mount->getMountPoint() . $path));
100104
});
101105
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount): void {
102106
$this->emit('\OC\Files\Utils\Scanner', 'scanFolder', [$mount->getMountPoint() . $path]);
103-
$this->dispatcher->dispatchTyped(new BeforeFolderScannedEvent($mount->getMountPoint() . $path));
107+
$this->eventDispatcher->dispatchTyped(new BeforeFolderScannedEvent($mount->getMountPoint() . $path));
104108
});
105109
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount): void {
106110
$this->emit('\OC\Files\Utils\Scanner', 'postScanFile', [$mount->getMountPoint() . $path]);
107-
$this->dispatcher->dispatchTyped(new FileScannedEvent($mount->getMountPoint() . $path));
111+
$this->eventDispatcher->dispatchTyped(new FileScannedEvent($mount->getMountPoint() . $path));
108112
});
109113
$scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount): void {
110114
$this->emit('\OC\Files\Utils\Scanner', 'postScanFolder', [$mount->getMountPoint() . $path]);
111-
$this->dispatcher->dispatchTyped(new FolderScannedEvent($mount->getMountPoint() . $path));
115+
$this->eventDispatcher->dispatchTyped(new FolderScannedEvent($mount->getMountPoint() . $path));
112116
});
113117
$scanner->listen('\OC\Files\Cache\Scanner', 'normalizedNameMismatch', function ($path) use ($mount): void {
114118
$this->emit('\OC\Files\Utils\Scanner', 'normalizedNameMismatch', [$path]);
115119
});
116120
}
117121

118-
/**
119-
* @param string $dir
120-
*/
121-
public function backgroundScan($dir) {
122+
public function backgroundScan(string $dir) {
122123
$mounts = $this->getMounts($dir);
123124
foreach ($mounts as $mount) {
124125
try {
@@ -157,13 +158,10 @@ public function backgroundScan($dir) {
157158
}
158159

159160
/**
160-
* @param string $dir
161-
* @param $recursive
162-
* @param callable|null $mountFilter
163161
* @throws ForbiddenException
164162
* @throws NotFoundException
165163
*/
166-
public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECURSIVE, ?callable $mountFilter = null) {
164+
public function scan(string $dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECURSIVE, ?callable $mountFilter = null) {
167165
if (!Filesystem::isValidPath($dir)) {
168166
throw new \InvalidArgumentException('Invalid path to scan');
169167
}
@@ -219,18 +217,18 @@ public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECUR
219217

220218
$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage): void {
221219
$this->postProcessEntry($storage, $path);
222-
$this->dispatcher->dispatchTyped(new NodeRemovedFromCache($storage, $path));
220+
$this->eventDispatcher->dispatchTyped(new NodeRemovedFromCache($storage, $path));
223221
});
224222
$scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage): void {
225223
$this->postProcessEntry($storage, $path);
226-
$this->dispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
224+
$this->eventDispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
227225
});
228226
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path, $storageId, $data, $fileId) use ($storage): void {
229227
$this->postProcessEntry($storage, $path);
230228
if ($fileId) {
231-
$this->dispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
229+
$this->eventDispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
232230
} else {
233-
$this->dispatcher->dispatchTyped(new NodeAddedToCache($storage, $path));
231+
$this->eventDispatcher->dispatchTyped(new NodeAddedToCache($storage, $path));
234232
}
235233
});
236234

tests/lib/Files/EtagTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Test\Files;
1010

1111
use OC\Files\Filesystem;
12+
use OC\Files\SetupManager;
1213
use OC\Files\Utils\Scanner;
1314
use OCA\Files_Sharing\AppInfo\Application;
1415
use OCP\EventDispatcher\IEventDispatcher;
@@ -73,7 +74,13 @@ public function testNewUser(): void {
7374
$files = ['/foo.txt', '/folder/bar.txt', '/folder/subfolder', '/folder/subfolder/qwerty.txt'];
7475
$originalEtags = $this->getEtags($files);
7576

76-
$scanner = new Scanner($user1, Server::get(IDBConnection::class), Server::get(IEventDispatcher::class), Server::get(LoggerInterface::class));
77+
$scanner = new Scanner(
78+
Server::get(IUserManager::class)->get($user1),
79+
Server::get(IDBConnection::class),
80+
Server::get(IEventDispatcher::class),
81+
Server::get(LoggerInterface::class),
82+
Server::get(SetupManager::class),
83+
);
7784
$scanner->backgroundScan('/');
7885

7986
$newEtags = $this->getEtags($files);

tests/lib/Files/Utils/ScannerTest.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OC\Files\Filesystem;
1212
use OC\Files\Mount\MountPoint;
13+
use OC\Files\SetupManager;
1314
use OC\Files\Storage\Temporary;
1415
use OC\Files\Utils\Scanner;
1516
use OCP\EventDispatcher\IEventDispatcher;
@@ -77,7 +78,13 @@ public function testReuseExistingRoot(): void {
7778
$storage->file_put_contents('foo.txt', 'qwerty');
7879
$storage->file_put_contents('folder/bar.txt', 'qwerty');
7980

80-
$scanner = new TestScanner('', Server::get(IDBConnection::class), $this->createMock(IEventDispatcher::class), Server::get(LoggerInterface::class));
81+
$scanner = new TestScanner(
82+
Server::get(IUserManager::class)->get(''),
83+
Server::get(IDBConnection::class),
84+
$this->createMock(IEventDispatcher::class),
85+
Server::get(LoggerInterface::class),
86+
Server::get(SetupManager::class),
87+
);
8188
$scanner->addMount($mount);
8289

8390
$scanner->scan('');
@@ -99,7 +106,13 @@ public function testReuseExistingFile(): void {
99106
$storage->file_put_contents('foo.txt', 'qwerty');
100107
$storage->file_put_contents('folder/bar.txt', 'qwerty');
101108

102-
$scanner = new TestScanner('', Server::get(IDBConnection::class), $this->createMock(IEventDispatcher::class), Server::get(LoggerInterface::class));
109+
$scanner = new TestScanner(
110+
Server::get(IUserManager::class)->get(''),
111+
Server::get(IDBConnection::class),
112+
$this->createMock(IEventDispatcher::class),
113+
Server::get(LoggerInterface::class),
114+
Server::get(SetupManager::class),
115+
);
103116
$scanner->addMount($mount);
104117

105118
$scanner->scan('');
@@ -137,7 +150,13 @@ public function testScanSubMount(): void {
137150
$storage->file_put_contents('foo.txt', 'qwerty');
138151
$storage->file_put_contents('folder/bar.txt', 'qwerty');
139152

140-
$scanner = new Scanner($uid, Server::get(IDBConnection::class), Server::get(IEventDispatcher::class), Server::get(LoggerInterface::class));
153+
$scanner = new Scanner(
154+
Server::get(IUserManager::class)->get($uid),
155+
Server::get(IDBConnection::class),
156+
Server::get(IEventDispatcher::class),
157+
Server::get(LoggerInterface::class),
158+
Server::get(SetupManager::class),
159+
);
141160

142161
$this->assertFalse($cache->inCache('folder/bar.txt'));
143162
$scanner->scan('/' . $uid . '/files/foo');
@@ -166,7 +185,13 @@ public function testInvalidPathScanning($invalidPath): void {
166185
$this->expectException(\InvalidArgumentException::class);
167186
$this->expectExceptionMessage('Invalid path to scan');
168187

169-
$scanner = new TestScanner('', Server::get(IDBConnection::class), $this->createMock(IEventDispatcher::class), Server::get(LoggerInterface::class));
188+
$scanner = new TestScanner(
189+
Server::get(IUserManager::class)->get(''),
190+
Server::get(IDBConnection::class),
191+
$this->createMock(IEventDispatcher::class),
192+
Server::get(LoggerInterface::class),
193+
Server::get(SetupManager::class),
194+
);
170195
$scanner->scan($invalidPath);
171196
}
172197

@@ -180,7 +205,13 @@ public function testPropagateEtag(): void {
180205
$storage->file_put_contents('folder/bar.txt', 'qwerty');
181206
$storage->touch('folder/bar.txt', time() - 200);
182207

183-
$scanner = new TestScanner('', Server::get(IDBConnection::class), $this->createMock(IEventDispatcher::class), Server::get(LoggerInterface::class));
208+
$scanner = new TestScanner(
209+
Server::get(IUserManager::class)->get(''),
210+
Server::get(IDBConnection::class),
211+
$this->createMock(IEventDispatcher::class),
212+
Server::get(LoggerInterface::class),
213+
Server::get(SetupManager::class),
214+
);
184215
$scanner->addMount($mount);
185216

186217
$scanner->scan('');
@@ -206,7 +237,13 @@ public function testShallow(): void {
206237
$storage->file_put_contents('folder/bar.txt', 'qwerty');
207238
$storage->file_put_contents('folder/subfolder/foobar.txt', 'qwerty');
208239

209-
$scanner = new TestScanner('', Server::get(IDBConnection::class), $this->createMock(IEventDispatcher::class), Server::get(LoggerInterface::class));
240+
$scanner = new TestScanner(
241+
Server::get(IUserManager::class)->get(''),
242+
Server::get(IDBConnection::class),
243+
$this->createMock(IEventDispatcher::class),
244+
Server::get(LoggerInterface::class),
245+
Server::get(SetupManager::class),
246+
);
210247
$scanner->addMount($mount);
211248

212249
$scanner->scan('', $recusive = false);

0 commit comments

Comments
 (0)