From 8ec7827cad36741b3082e62c25204a3c296e9450 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 4 May 2016 23:22:15 +0300 Subject: [PATCH 01/10] Move table initialization to install script --- appinfo/app.php | 13 ------------- appinfo/install.php | 8 ++++++++ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/appinfo/app.php b/appinfo/app.php index 304989b4..383d368a 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -32,16 +32,3 @@ \OC::$server->getURLGenerator() ); }); - -$avBinary = \OCP\Config::getAppValue('files_antivirus', 'av_path', ''); -if (empty($avBinary)){ - try { - $ruleMapper = $app->getContainer()->query('RuleMapper'); - $rules = $ruleMapper->findAll(); - if(!count($rules)) { - $ruleMapper->populate(); - } - \OCP\Config::setAppValue('files_antivirus', 'av_path', '/usr/bin/clamscan'); - } catch (\Exception $e) { - } -} diff --git a/appinfo/install.php b/appinfo/install.php index 33edb1d4..e0fa4c12 100644 --- a/appinfo/install.php +++ b/appinfo/install.php @@ -1,3 +1,11 @@ getContainer()->query('RuleMapper'); +$rules = $ruleMapper->findAll(); +if(!count($rules)) { + $ruleMapper->populate(); +} + +\OC::$server->getConfig()->setAppValue('files_antivirus', 'av_path', '/usr/bin/clamscan'); \OC::$server->getJobList()->add('OCA\Files_Antivirus\Cron\Task'); From 7013ad0db7845ddc68317ad14d938af3289f5430 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Thu, 5 May 2016 00:01:56 +0300 Subject: [PATCH 02/10] Fix savig rules --- js/settings.js | 2 +- lib/db/rule.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/js/settings.js b/js/settings.js index 6f2c9d9f..0290fb23 100644 --- a/js/settings.js +++ b/js/settings.js @@ -45,7 +45,7 @@ var antivirusSettings = antivirusSettings || { row = $(node).parent(), data = { id : row.data('id'), - status_type : row.find('.status-type select').val(), + statusType : row.find('.status-type select').val(), match : row.children('.match').text(), description : row.children('.description').text(), status : row.find('.scan-result select').val() diff --git a/lib/db/rule.php b/lib/db/rule.php index 101e5ab7..8f4837cb 100644 --- a/lib/db/rule.php +++ b/lib/db/rule.php @@ -69,13 +69,14 @@ class Rule extends Entity implements JsonSerializable{ * @return array */ public function jsonSerialize() { - return array( + return [ + 'id' => $this->id, 'group_id' => $this->groupId, 'status_type' => $this->statusType, 'result' => $this->result, 'match' => $this->match, 'description' => $this->description, 'status' => $this->status - ); + ]; } } From 84f05986196e6df942b41430567a2b56ef1b440a Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Thu, 5 May 2016 02:12:31 +0300 Subject: [PATCH 03/10] Please codechecker && migrate tests to PSR-4 Fixes --- appinfo/preupdate.php | 4 +- appinfo/update.php | 5 ++- lib/avirwrapper.php | 2 +- lib/backgroundscanner.php | 83 +++++++++++++++++++++++++-------------- lib/notification.php | 51 ++++++++++++------------ lib/status.php | 2 +- 6 files changed, 86 insertions(+), 61 deletions(-) diff --git a/appinfo/preupdate.php b/appinfo/preupdate.php index d8d75e52..b1fc6b25 100644 --- a/appinfo/preupdate.php +++ b/appinfo/preupdate.php @@ -1,6 +1,6 @@ getConfig()->getAppValue('files_antivirus', 'installed_version'); if (version_compare($installedVersion, '0.6', '<')) { $query = OCP\DB::prepare( 'SELECT COUNT(*) AS `count`, `fileid` FROM `*PREFIX*files_antivirus` GROUP BY `fileid` HAVING COUNT(*) > 1' ); $result = $query->execute(); @@ -12,4 +12,4 @@ if (version_compare($installedVersion, '0.6.1', '<') && version_compare($installedVersion, '0.5', '>=')) { $alterQuery = OCP\DB::prepare( 'ALTER TABLE `*PREFIX*files_antivirus_status` RENAME TO `*PREFIX*files_avir_status`' ); $alterQuery->execute(); -} \ No newline at end of file +} diff --git a/appinfo/update.php b/appinfo/update.php index 6ffebfa3..0d7a79c7 100644 --- a/appinfo/update.php +++ b/appinfo/update.php @@ -7,7 +7,7 @@ */ -$installedVersion = \OCP\Config::getAppValue('files_antivirus', 'installed_version'); +$installedVersion = \OC::$server->getConfig()->getAppValue('files_antivirus', 'installed_version'); if (version_compare($installedVersion, '0.5', '<')) { $app = new \OCA\Files_Antivirus\AppInfo\Application(); @@ -21,8 +21,9 @@ $jobs = $jobList->getAll(); foreach ($jobs as $job) { $jobArg = $job->getArgument(); - if($jobArg[0]=='OC_Files_Antivirus_BackgroundScanner') + if($jobArg[0] === 'OC_Files_Antivirus_BackgroundScanner') { $jobList->remove($job); + } } } diff --git a/lib/avirwrapper.php b/lib/avirwrapper.php index 6053b881..961e2af6 100644 --- a/lib/avirwrapper.php +++ b/lib/avirwrapper.php @@ -70,7 +70,7 @@ function ($data) use ($scanner){ }, function () use ($scanner, $path) { $status = $scanner->completeAsyncScan(); - if ($status->getNumericStatus() == \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){ + if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){ //prevent from going to trashbin if (App::isEnabled('files_trashbin')) { \OCA\Files_Trashbin\Storage::preRenameHook([]); diff --git a/lib/backgroundscanner.php b/lib/backgroundscanner.php index 9bf973b0..0495c1f5 100644 --- a/lib/backgroundscanner.php +++ b/lib/backgroundscanner.php @@ -51,50 +51,76 @@ public function run(){ return; } // locate files that are not checked yet - $dirMimetypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); - $sql = 'SELECT `*PREFIX*filecache`.`fileid`, `*PREFIX*storages`.*' - .' FROM `*PREFIX*filecache`' - .' LEFT JOIN `*PREFIX*files_antivirus` ON `*PREFIX*files_antivirus`.`fileid` = `*PREFIX*filecache`.`fileid`' - .' JOIN `*PREFIX*storages` ON `*PREFIX*storages`.`numeric_id` = `*PREFIX*filecache`.`storage`' - .' WHERE `mimetype` != ?' - .' AND (`*PREFIX*storages`.`id` LIKE ? OR `*PREFIX*storages`.`id` LIKE ?)' - .' AND (`*PREFIX*files_antivirus`.`fileid` IS NULL OR `mtime` > `check_time`)' - .' AND `path` LIKE ?' - .' AND `*PREFIX*filecache`.`size` != 0'; - $stmt = \OCP\DB::prepare($sql, 5); + $dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); try { - $result = $stmt->execute(array($dirMimetypeId, 'local::%', 'home::%', 'files/%')); - if (\OCP\DB::isError($result)) { - \OCP\Util::writeLog('files_antivirus', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); - return; - } + $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb->select(['fc.fileid']) + ->from('filecache', 'fc') + ->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid')) + ->innerJoin( + 'fc', + 'storages', + 'ss', + $qb->expr()->andX( + $qb->expr()->eq('fc.storage', 'ss.numeric_id'), + $qb->expr()->orX( + $qb->expr()->like('ss.id', $qb->expr()->literal('local::%')), + $qb->expr()->like('ss.id', $qb->expr()->literal('home::%')) + ) + ) + ) + ->where( + $qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId)) + ) + ->andWhere( + $qb->expr()->orX( + $qb->expr()->isNull('fa.fileid'), + $qb->expr()->gt('fc.mtime', 'fa.check_time') + ) + ) + ->andWhere( + $qb->expr()->like('fc.path', $qb->expr()->literal('files/%')) + ) + ->andWhere( + $qb->expr()->neq('fc.size', $qb->expr()->literal('0')) + ) + ->setMaxResults(5) + ; + $result = $qb->execute(); } catch(\Exception $e) { - \OCP\Util::writeLog('files_antivirus', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); + \OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); return; } - - $view = new \OC\Files\View('/'); - while ($row = $result->fetchRow()) { - $path = $view->getPath($row['fileid']); - if (!is_null($path)) { - $item = new Item($this->l10n, $view, $path, $row['fileid']); - $scanner = $this->scannerFactory->getScanner(); - $status = $scanner->scan($item); - $status->dispatch($item, true); + + + $view = new \OC\Files\View(''); + try { + while ($row = $result->fetch()) { + $path = $view->getPath($row['fileid']); + if (!is_null($path)) { + $item = new Item($this->l10n, $view, $path, $row['fileid']); + $scanner = $this->scannerFactory->getScanner(); + $status = $scanner->scan($item); + $status->dispatch($item, true); + } } + } catch (\Exception $e){ + \OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); } \OC_Util::tearDownFS(); } - + /** * A hack to access files and views. Better than before. + * + * @return bool */ protected function initFS(){ //Need any valid user to mount FS $results = $this->userManager->search('', 2, 0); $anyUser = array_pop($results); if (is_null($anyUser)) { - \OCP\Util::writeLog('files_antivirus', "Failed to setup file system", \OCP\Util::ERROR); + \OC::$server->getLogger()->error("Failed to setup file system", ['app' => 'files_antivirus']); return false; } \OC_Util::tearDownFS(); @@ -102,7 +128,6 @@ protected function initFS(){ return true; } - /** * @deprecated since v8.0.0 */ diff --git a/lib/notification.php b/lib/notification.php index c07c8237..97965cfa 100644 --- a/lib/notification.php +++ b/lib/notification.php @@ -13,34 +13,33 @@ public static function sendMail($path){ if (!\OCP\User::isLoggedIn()){ return; } - $email = \OCP\Config::getUserValue(\OCP\User::getUser(), 'settings', 'email', ''); - if (\OCP\App::isEnabled(user_ldap)){ - $user = \OCP\Config::getUserValue(\OCP\User::getUser(), 'user_ldap', 'displayName', ''); - if (empty($user)) { - $user = \OCP\User::getUser(); - } - } - else { - $user = \OCP\User::getUser(); - }; + $config = \OC::$server->getConfig(); + $user = \OC::$server->getUserSession()->getUser(); + $email = $user->getEMailAddress(); + $displayName = $user->getDisplayName(); + if ( strval($displayName) ==='' ) { + $displayName = $user->getUID(); + } \OCP\Util::writeLog('files_antivirus', 'Email: '.$email, \OCP\Util::DEBUG); if (!empty($email)) { - $defaults = new \OCP\Defaults(); - $tmpl = new \OCP\Template('files_antivirus', 'notification'); - $tmpl->assign('file', $path); - $tmpl->assign('host', \OCP\Util::getServerHost()); - $tmpl->assign('user', \OCP\User::getDisplayName()); - $msg = $tmpl->fetchPage(); - $from = \OCP\Util::getDefaultEmailAddress('security-noreply'); - \OCP\Util::sendMail( - $email, - $user, - \OCP\Util::getL10N('files_antivirus')->t('Malware detected'), - $msg, - $from, - $defaults->getName(), - true - ); + try { + $tmpl = new \OCP\Template('files_antivirus', 'notification'); + $tmpl->assign('file', $path); + $tmpl->assign('host', \OC::$server->getRequest()->getServerHost()); + $tmpl->assign('user', $displayName); + $msg = $tmpl->fetchPage(); + $from = \OCP\Util::getDefaultEmailAddress('security-noreply'); + $mailer = \OC::$server->getMailer(); + $message = $mailer->createMessage(); + $message->setSubject(\OCP\Util::getL10N('files_antivirus')->t('Malware detected')); + $message->setFrom([$from => 'ownCloud Notifier']); + $message->setTo([$email => $displayName]); + $message->setPlainBody($msg); + $message->setHtmlBody($msg); + $mailer->send($message); + } catch (\Exception $e){ + \OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); + } } } } diff --git a/lib/status.php b/lib/status.php index af3a93ca..96013c75 100644 --- a/lib/status.php +++ b/lib/status.php @@ -78,7 +78,7 @@ public function parseResponse($rawResponse, $result = null){ if (preg_match($rule->getMatch(), $rawResponse, $matches)){ $isMatched = true; $this->numericStatus = $rule->getStatus(); - if ($rule->getStatus()==self::SCANRESULT_CLEAN){ + if (intval($rule->getStatus())===self::SCANRESULT_CLEAN){ $this->details = ''; } else { $this->details = isset($matches[1]) ? $matches[1] : 'unknown'; From 68f38c02b8cc6615a3cd9509333a0d2bffa990d1 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 13 Jul 2016 01:06:20 +0300 Subject: [PATCH 04/10] Fix file not found --- appinfo/application.php | 13 ++--- lib/backgroundscanner.php | 108 ++++++++++++++++++++++++++------------ tests/cron/tasktest.php | 26 +++++---- 3 files changed, 95 insertions(+), 52 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index 6d9c2a92..a616496c 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -13,7 +13,6 @@ use OCA\Files_Antivirus\AppConfig; use OCA\Files_Antivirus\Controller\RuleController; use OCA\Files_Antivirus\Controller\SettingsController; -use OCA\Files_Antivirus\Hooks\FilesystemHooks; use OCA\Files_Antivirus\Db\RuleMapper; use OCA\Files_Antivirus\BackgroundScanner; use OCA\Files_Antivirus\ScannerFactory; @@ -57,16 +56,12 @@ public function __construct (array $urlParams = array()) { $container->registerService('BackgroundScanner', function($c) { return new BackgroundScanner( $c->query('ScannerFactory'), - $c->query('ServerContainer')->getUserManager(), - $c->query('L10N') - ); - }); - $container->registerService('FilesystemHooks', function($c) { - return new FilesystemHooks( - $c->query('ServerContainer')->getRootFolder(), - $c->query('AppConfig') + $c->query('L10N'), + $c->getServer()->getRootFolder(), + $c->getServer()->getUserSession() ); }); + $container->registerService('RuleMapper', function($c) { return new RuleMapper( $c->query('ServerContainer')->getDb() diff --git a/lib/backgroundscanner.php b/lib/backgroundscanner.php index 0495c1f5..35e08112 100644 --- a/lib/backgroundscanner.php +++ b/lib/backgroundscanner.php @@ -8,38 +8,57 @@ namespace OCA\Files_Antivirus; -use OCP\IUserManager; +use OC\Files\Filesystem; +use OC\Files\View; use OCP\IL10N; - -use OCA\Files_Antivirus\Item; +use OCP\IUser; +use OCP\Files\IRootFolder; +use OCP\IUserSession; class BackgroundScanner { + const BATCH_SIZE = 10; + + /** @var IRootFolder */ + protected $rootFolder; + + /** @var \OCP\Files\Folder[] */ + protected $userFolders; + /** * @var ScannerFactory */ private $scannerFactory; - - /** - * @var IUserManager - */ - private $userManager; + /** * @var IL10N */ private $l10n; - + + /** @var string */ + protected $currentFilesystemUser; + + /** @var \OCP\IUserSession */ + protected $userSession; + /** * A constructor + * * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory - * @param IUserManager $userManager * @param IL10N $l10n + * @param IRootFolder $rootFolder + * @param IUserSession $userSession */ - public function __construct(ScannerFactory $scannerFactory, IUserManager $userManager, IL10N $l10n){ + public function __construct(ScannerFactory $scannerFactory, + IL10N $l10n, + IRootFolder $rootFolder, + IUserSession $userSession + ){ + $this->rootFolder = $rootFolder; $this->scannerFactory = $scannerFactory; - $this->userManager = $userManager; $this->l10n = $l10n; + $this->userSession = $userSession; } /** @@ -47,9 +66,6 @@ public function __construct(ScannerFactory $scannerFactory, IUserManager $userMa * @return null */ public function run(){ - if (!$this->initFS()) { - return; - } // locate files that are not checked yet $dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); try { @@ -84,7 +100,7 @@ public function run(){ ->andWhere( $qb->expr()->neq('fc.size', $qb->expr()->literal('0')) ) - ->setMaxResults(5) + ->setMaxResults(self::BATCH_SIZE) ; $result = $qb->execute(); } catch(\Exception $e) { @@ -92,13 +108,18 @@ public function run(){ return; } - - $view = new \OC\Files\View(''); try { while ($row = $result->fetch()) { - $path = $view->getPath($row['fileid']); + $fileId = $row['fileid']; + $owner = $this->getOwner($fileId); + if (!$owner){ + continue; + } + $this->initFilesystemForUser($owner); + $view = \OC\Files\Filesystem::getView(); + $path = $view->getPath($fileId); if (!is_null($path)) { - $item = new Item($this->l10n, $view, $path, $row['fileid']); + $item = new Item($this->l10n, $view, $path, $fileId); $scanner = $this->scannerFactory->getScanner(); $status = $scanner->scan($item); $status->dispatch($item, true); @@ -110,22 +131,43 @@ public function run(){ \OC_Util::tearDownFS(); } + protected function getOwner($fileId){ + $mountProviderCollection = \OC::$server->getMountProviderCollection(); + $mountCache = $mountProviderCollection->getMountCache(); + $mounts = $mountCache->getMountsForFileId($fileId); + if (!empty($mounts)) { + $user = $mounts[0]->getUser(); + if ($user instanceof IUser) { + return $user; + } + } + } + /** - * A hack to access files and views. Better than before. - * - * @return bool + * @param \OCP\IUser $user + * @return \OCP\Files\Folder */ - protected function initFS(){ - //Need any valid user to mount FS - $results = $this->userManager->search('', 2, 0); - $anyUser = array_pop($results); - if (is_null($anyUser)) { - \OC::$server->getLogger()->error("Failed to setup file system", ['app' => 'files_antivirus']); - return false; + protected function getUserFolder(IUser $user) { + if (!isset($this->userFolders[$user->getUID()])) { + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); + $this->userFolders[$user->getUID()] = $userFolder; + } + return $this->userFolders[$user->getUID()]; + } + + /** + * @param IUser $user + */ + protected function initFilesystemForUser(IUser $user) { + if ($this->currentFilesystemUser !== $user->getUID()) { + if ($this->currentFilesystemUser !== '') { + Filesystem::tearDown(); + } + Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files'); + $this->userSession->setUser($user); + $this->currentFilesystemUser = $user->getUID(); + \OC\Files\Filesystem::initMountPoints($user->getUID()); } - \OC_Util::tearDownFS(); - \OC_Util::setupFS($anyUser->getUID()); - return true; } /** diff --git a/tests/cron/tasktest.php b/tests/cron/tasktest.php index f3d12225..3c28650c 100644 --- a/tests/cron/tasktest.php +++ b/tests/cron/tasktest.php @@ -6,15 +6,20 @@ * See the COPYING-README file. */ -use \OCA\Files_Antivirus\Db\RuleMapper; -use \OCA\Files_Antivirus\Item; -use \OCA\Files_Antivirus\ScannerFactory; -use \OCA\Files_Antivirus\BackgroundScanner; +use OCA\Files_Antivirus\ScannerFactory; +use OCA\Files_Antivirus\BackgroundScanner; +use OCA\Files_Antivirus\Tests\Testbase; + +/** @var ScannerFactory */ + protected $scannerFactory; + +class Test_Files_Antivirus_Cron_TaskTest extends Testbase { + /** @var ScannerFactory */ + protected $scannerFactory; -class Test_Files_Antivirus_Cron_TaskTest extends \OCA\Files_Antivirus\Tests\Testbase { public function setUp(){ parent::setUp(); - //Bgscanner requires at least one user on the current instance + //Background scanner requires at least one user on the current instance $userManager = $this->application->getContainer()->query('ServerContainer')->getUserManager(); $results = $userManager->search('', 1, 0); @@ -28,12 +33,13 @@ public function setUp(){ } public function testRun(){ - $backgroudScanner = new BackgroundScanner( + $backgroundScanner = new BackgroundScanner( $this->scannerFactory, - $this->container->query('ServerContainer')->getUserManager(), - $this->l10n + $this->l10n, + $this->container->getServer()->getRootFolder(), + $this->container->getServer()->getUserSession() ); - $bgScan = $backgroudScanner->run(); + $bgScan = $backgroundScanner->run(); $this->assertNull($bgScan); } } From 74806a73f0324b4c641ae7a31f0c9afee0da5323 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Tue, 23 Aug 2016 17:43:31 +0300 Subject: [PATCH 05/10] Count only successful scan results in batch --- lib/backgroundscanner.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/backgroundscanner.php b/lib/backgroundscanner.php index 35e08112..27455cd9 100644 --- a/lib/backgroundscanner.php +++ b/lib/backgroundscanner.php @@ -9,10 +9,9 @@ namespace OCA\Files_Antivirus; use OC\Files\Filesystem; -use OC\Files\View; use OCP\IL10N; -use OCP\IUser; use OCP\Files\IRootFolder; +use OCP\IUser; use OCP\IUserSession; class BackgroundScanner { @@ -100,7 +99,6 @@ public function run(){ ->andWhere( $qb->expr()->neq('fc.size', $qb->expr()->literal('0')) ) - ->setMaxResults(self::BATCH_SIZE) ; $result = $qb->execute(); } catch(\Exception $e) { @@ -108,15 +106,16 @@ public function run(){ return; } - try { - while ($row = $result->fetch()) { + $cnt = 0; + while ($row = $result->fetch() && $cnt < self::BATCH_SIZE) { + try { $fileId = $row['fileid']; $owner = $this->getOwner($fileId); if (!$owner){ continue; } $this->initFilesystemForUser($owner); - $view = \OC\Files\Filesystem::getView(); + $view = Filesystem::getView(); $path = $view->getPath($fileId); if (!is_null($path)) { $item = new Item($this->l10n, $view, $path, $fileId); @@ -124,11 +123,13 @@ public function run(){ $status = $scanner->scan($item); $status->dispatch($item, true); } + // increased only for successfully scanned files + $cnt = $cnt + 1; + } catch (\Exception $e){ + \OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); } - } catch (\Exception $e){ - \OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); } - \OC_Util::tearDownFS(); + $this->tearDownFilesystem(); } protected function getOwner($fileId){ @@ -161,15 +162,23 @@ protected function getUserFolder(IUser $user) { protected function initFilesystemForUser(IUser $user) { if ($this->currentFilesystemUser !== $user->getUID()) { if ($this->currentFilesystemUser !== '') { - Filesystem::tearDown(); + $this->tearDownFilesystem(); } Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files'); $this->userSession->setUser($user); $this->currentFilesystemUser = $user->getUID(); - \OC\Files\Filesystem::initMountPoints($user->getUID()); + Filesystem::initMountPoints($user->getUID()); } } + /** + * + */ + protected function tearDownFilesystem(){ + $this->userSession->setUser(null); + \OC_Util::tearDownFS(); + } + /** * @deprecated since v8.0.0 */ From 70f354b022191be1542c8e8117b04cc0b23ee610 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 24 Aug 2016 20:59:26 +0300 Subject: [PATCH 06/10] Make var type more obvious --- lib/backgroundscanner.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/backgroundscanner.php b/lib/backgroundscanner.php index 27455cd9..9a276a1b 100644 --- a/lib/backgroundscanner.php +++ b/lib/backgroundscanner.php @@ -111,7 +111,8 @@ public function run(){ try { $fileId = $row['fileid']; $owner = $this->getOwner($fileId); - if (!$owner){ + /** @var IUser $owner */ + if (!$owner instanceof IUser){ continue; } $this->initFilesystemForUser($owner); @@ -132,6 +133,10 @@ public function run(){ $this->tearDownFilesystem(); } + /** + * @param int $fileId + * @return IUser|null + */ protected function getOwner($fileId){ $mountProviderCollection = \OC::$server->getMountProviderCollection(); $mountCache = $mountProviderCollection->getMountCache(); @@ -142,6 +147,7 @@ protected function getOwner($fileId){ return $user; } } + return null; } /** From 28b5b17865564f3b9722f0fc3a12b000ebf4ed8f Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 24 Aug 2016 23:23:17 +0300 Subject: [PATCH 07/10] Remove illegal merge lefover --- tests/cron/tasktest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/cron/tasktest.php b/tests/cron/tasktest.php index 3c28650c..245d19bb 100644 --- a/tests/cron/tasktest.php +++ b/tests/cron/tasktest.php @@ -10,9 +10,6 @@ use OCA\Files_Antivirus\BackgroundScanner; use OCA\Files_Antivirus\Tests\Testbase; -/** @var ScannerFactory */ - protected $scannerFactory; - class Test_Files_Antivirus_Cron_TaskTest extends Testbase { /** @var ScannerFactory */ protected $scannerFactory; From 9abeb3dc4caadbda3b871540a599efda2ab6bfbe Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 24 Aug 2016 23:44:09 +0300 Subject: [PATCH 08/10] Backport test fix --- tests/db/ruletest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/db/ruletest.php b/tests/db/ruletest.php index c4b01298..2ad34684 100644 --- a/tests/db/ruletest.php +++ b/tests/db/ruletest.php @@ -28,11 +28,14 @@ public function testJsonSerialize(){ 'description' => "", 'status' => \OCA\Files_Antivirus\Status::SCANRESULT_CLEAN ]; - + $rule = Rule::fromParams($data); + $actual = $rule->jsonSerialize(); + $this->assertArrayHasKey('id', $actual); + unset($actual['id']); $this->assertEquals( - $expected, - $rule->jsonSerialize() + $expected, + $actual ); } } From 847c42e108ae827fa592b821234b0a2a601d27ca Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 24 Aug 2016 23:56:46 +0300 Subject: [PATCH 09/10] Update appinfo --- appinfo/info.xml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 6a31508c..1136b10f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -10,13 +10,12 @@ For this app to be effective, the ClamAV virus definitions should be kept up to http://github.com/owncloud/files_antivirus.git AGPL Manuel Delgado, Bart Visscher, thinksilicon.de, Victor Dubiniuk - 0.8.0.2 - 8 + 0.8.1.0 - + 157439 From 8eeb2ae292f2ce402a4a86347a78e1de915b1f6f Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 26 Aug 2016 00:27:18 +0300 Subject: [PATCH 10/10] Fix assignment in condition --- lib/backgroundscanner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/backgroundscanner.php b/lib/backgroundscanner.php index 9a276a1b..a6bbea70 100644 --- a/lib/backgroundscanner.php +++ b/lib/backgroundscanner.php @@ -107,7 +107,7 @@ public function run(){ } $cnt = 0; - while ($row = $result->fetch() && $cnt < self::BATCH_SIZE) { + while (($row = $result->fetch()) && $cnt < self::BATCH_SIZE) { try { $fileId = $row['fileid']; $owner = $this->getOwner($fileId);