public function testNewUser() { $user1 = $this->getUniqueID('user_'); $this->userBackend->createUser($user1, ''); $this->loginAsUser($user1); Filesystem::mkdir('/folder'); Filesystem::mkdir('/folder/subfolder'); Filesystem::file_put_contents('/foo.txt', 'asd'); Filesystem::file_put_contents('/folder/bar.txt', 'fgh'); Filesystem::file_put_contents('/folder/subfolder/qwerty.txt', 'jkl'); $files = array('/foo.txt', '/folder/bar.txt', '/folder/subfolder', '/folder/subfolder/qwerty.txt'); $originalEtags = $this->getEtags($files); $scanner = new \OC\Files\Utils\Scanner($user1, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger()); $scanner->backgroundScan('/'); $newEtags = $this->getEtags($files); // loop over array and use assertSame over assertEquals to prevent false positives foreach ($originalEtags as $file => $originalEtag) { $this->assertSame($originalEtag, $newEtags[$file]); } }
public function testNewUser() { $user1 = uniqid('user_'); $this->userBackend->createUser($user1, ''); \OC_Util::tearDownFS(); \OC_User::setUserId($user1); \OC_Util::setupFS($user1); Filesystem::mkdir('/folder'); Filesystem::mkdir('/folder/subfolder'); Filesystem::file_put_contents('/foo.txt', 'asd'); Filesystem::file_put_contents('/folder/bar.txt', 'fgh'); Filesystem::file_put_contents('/folder/subfolder/qwerty.txt', 'jkl'); $files = array('/foo.txt', '/folder/bar.txt', '/folder/subfolder', '/folder/subfolder/qwerty.txt'); $originalEtags = $this->getEtags($files); $scanner = new \OC\Files\Utils\Scanner($user1); $scanner->backgroundScan('/'); $newEtags = $this->getEtags($files); // loop over array and use assertSame over assertEquals to prevent false positives foreach ($originalEtags as $file => $originalEtag) { $this->assertSame($originalEtag, $newEtags[$file]); } }
} } else { $users = array(OC_User::getUser()); } $eventSource = \OC::$server->createEventSource(); $listener = new ScanListener($eventSource); foreach ($users as $user) { $eventSource->send('user', $user); $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection()); $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFile', array($listener, 'file')); $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFolder', array($listener, 'folder')); try { if ($force) { $scanner->scan($dir); } else { $scanner->backgroundScan($dir); } } catch (\Exception $e) { $eventSource->send('error', get_class($e) . ': ' . $e->getMessage()); } } $eventSource->send('done', $listener->getCount()); $eventSource->close(); class ScanListener { private $fileCount = 0; private $lastCount = 0; /** * @var \OCP\IEventSource event source to pass events to */ private $eventSource;
protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false) { $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger()); # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception # printout and count if ($verbose) { $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFile', function ($path) use($output) { $output->writeln("\tFile <info>{$path}</info>"); $this->filesCounter += 1; if ($this->hasBeenInterrupted()) { throw new \Exception('ctrl-c'); } }); $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFolder', function ($path) use($output) { $output->writeln("\tFolder <info>{$path}</info>"); $this->foldersCounter += 1; if ($this->hasBeenInterrupted()) { throw new \Exception('ctrl-c'); } }); $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use($output) { $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); }); # count only } else { $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFile', function () use($output) { $this->filesCounter += 1; if ($this->hasBeenInterrupted()) { throw new \Exception('ctrl-c'); } }); $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFolder', function () use($output) { $this->foldersCounter += 1; if ($this->hasBeenInterrupted()) { throw new \Exception('ctrl-c'); } }); } $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFile', function ($path) use($output) { $this->checkScanWarning($path, $output); }); $scanner->listen('\\OC\\Files\\Utils\\Scanner', 'scanFolder', function ($path) use($output) { $this->checkScanWarning($path, $output); }); try { if ($backgroundScan) { $scanner->backgroundScan($path); } else { $scanner->scan($path); } } catch (ForbiddenException $e) { $output->writeln("<error>Home storage for user {$user} not writable</error>"); $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); } catch (\Exception $e) { if ($e->getMessage() !== 'ctrl-c') { $output->writeln('<error>Exception while scanning: ' . $e->getMessage() . "\n" . $e->getTraceAsString() . '</error>'); } return; } }