Fix database connection usage in the files scanner

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2021-01-11 16:38:31 +01:00
parent fdd111924f
commit 3678823748
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8
2 changed files with 22 additions and 18 deletions

View File

@ -35,15 +35,15 @@
namespace OCA\Files\Command; namespace OCA\Files\Command;
use Doctrine\DBAL\Connection;
use OC\Core\Command\Base; use OC\Core\Command\Base;
use OC\Core\Command\InterruptedException; use OC\Core\Command\InterruptedException;
use OC\DB\Connection;
use OC\DB\ConnectionAdapter;
use OC\ForbiddenException; use OC\ForbiddenException;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Mount\IMountPoint; use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\StorageNotAvailableException; use OCP\Files\StorageNotAvailableException;
use OCP\IDBConnection;
use OCP\IUserManager; use OCP\IUserManager;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
@ -118,7 +118,12 @@ class Scan extends Base {
protected function scanFiles($user, $path, OutputInterface $output, $backgroundScan = false, $recursive = true, $homeOnly = false) { protected function scanFiles($user, $path, OutputInterface $output, $backgroundScan = false, $recursive = true, $homeOnly = false) {
$connection = $this->reconnectToDatabase($output); $connection = $this->reconnectToDatabase($output);
$scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger()); $scanner = new \OC\Files\Utils\Scanner(
$user,
new ConnectionAdapter($connection),
\OC::$server->query(IEventDispatcher::class),
\OC::$server->getLogger()
);
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
@ -303,12 +308,9 @@ class Scan extends Base {
return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60); return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60);
} }
/** protected function reconnectToDatabase(OutputInterface $output): Connection {
* @return \OCP\IDBConnection /** @var Connection $connection */
*/ $connection = \OC::$server->get(Connection::class);
protected function reconnectToDatabase(OutputInterface $output) {
/** @var Connection | IDBConnection $connection */
$connection = \OC::$server->getDatabaseConnection();
try { try {
$connection->close(); $connection->close();
} catch (\Exception $ex) { } catch (\Exception $ex) {

View File

@ -29,16 +29,16 @@
namespace OCA\Files\Command; namespace OCA\Files\Command;
use Doctrine\DBAL\Connection;
use OC\Core\Command\Base; use OC\Core\Command\Base;
use OC\Core\Command\InterruptedException; use OC\Core\Command\InterruptedException;
use OC\DB\Connection;
use OC\DB\ConnectionAdapter;
use OC\ForbiddenException; use OC\ForbiddenException;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\StorageNotAvailableException; use OCP\Files\StorageNotAvailableException;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -101,7 +101,12 @@ class ScanAppData extends Base {
} }
$connection = $this->reconnectToDatabase($output); $connection = $this->reconnectToDatabase($output);
$scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger()); $scanner = new \OC\Files\Utils\Scanner(
null,
new ConnectionAdapter($connection),
\OC::$server->query(IEventDispatcher::class),
\OC::$server->getLogger()
);
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
@ -250,12 +255,9 @@ class ScanAppData extends Base {
return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60); return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60);
} }
/** protected function reconnectToDatabase(OutputInterface $output): Connection {
* @return \OCP\IDBConnection /** @var Connection $connection*/
*/ $connection = \OC::$server->get(Connection::class);
protected function reconnectToDatabase(OutputInterface $output) {
/** @var Connection | IDBConnection $connection*/
$connection = \OC::$server->getDatabaseConnection();
try { try {
$connection->close(); $connection->close();
} catch (\Exception $ex) { } catch (\Exception $ex) {