1
0

scanner.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Olivier Paroz <github@oparoz.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Utils;
  27. use OC\Files\Cache\Cache;
  28. use OC\Files\Filesystem;
  29. use OC\ForbiddenException;
  30. use OC\Hooks\PublicEmitter;
  31. use OC\Lock\DBLockingProvider;
  32. use OCP\Files\Storage\IStorage;
  33. use OCP\Files\StorageNotAvailableException;
  34. use OCP\ILogger;
  35. /**
  36. * Class Scanner
  37. *
  38. * Hooks available in scope \OC\Utils\Scanner
  39. * - scanFile(string $absolutePath)
  40. * - scanFolder(string $absolutePath)
  41. *
  42. * @package OC\Files\Utils
  43. */
  44. class Scanner extends PublicEmitter {
  45. /**
  46. * @var string $user
  47. */
  48. private $user;
  49. /**
  50. * @var \OCP\IDBConnection
  51. */
  52. protected $db;
  53. /**
  54. * @var ILogger
  55. */
  56. protected $logger;
  57. /**
  58. * @param string $user
  59. * @param \OCP\IDBConnection $db
  60. * @param ILogger $logger
  61. */
  62. public function __construct($user, $db, ILogger $logger) {
  63. $this->logger = $logger;
  64. $this->user = $user;
  65. $this->db = $db;
  66. }
  67. /**
  68. * get all storages for $dir
  69. *
  70. * @param string $dir
  71. * @return \OC\Files\Mount\MountPoint[]
  72. */
  73. protected function getMounts($dir) {
  74. //TODO: move to the node based fileapi once that's done
  75. \OC_Util::tearDownFS();
  76. \OC_Util::setupFS($this->user);
  77. $mountManager = Filesystem::getMountManager();
  78. $mounts = $mountManager->findIn($dir);
  79. $mounts[] = $mountManager->find($dir);
  80. $mounts = array_reverse($mounts); //start with the mount of $dir
  81. return $mounts;
  82. }
  83. /**
  84. * attach listeners to the scanner
  85. *
  86. * @param \OC\Files\Mount\MountPoint $mount
  87. */
  88. protected function attachListener($mount) {
  89. $scanner = $mount->getStorage()->getScanner();
  90. $emitter = $this;
  91. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
  92. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
  93. });
  94. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
  95. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
  96. });
  97. $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) {
  98. $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
  99. });
  100. $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) {
  101. $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
  102. });
  103. }
  104. /**
  105. * @param string $dir
  106. */
  107. public function backgroundScan($dir) {
  108. $mounts = $this->getMounts($dir);
  109. foreach ($mounts as $mount) {
  110. if (is_null($mount->getStorage())) {
  111. continue;
  112. }
  113. // don't scan the root storage
  114. if ($mount->getStorage()->instanceOfStorage('\OC\Files\Storage\Local') && $mount->getMountPoint() === '/') {
  115. continue;
  116. }
  117. $scanner = $mount->getStorage()->getScanner();
  118. $this->attachListener($mount);
  119. $scanner->backgroundScan();
  120. }
  121. }
  122. /**
  123. * @param string $dir
  124. * @throws \OC\ForbiddenException
  125. */
  126. public function scan($dir = '') {
  127. if (!Filesystem::isValidPath($dir)) {
  128. throw new \InvalidArgumentException('Invalid path to scan');
  129. }
  130. $mounts = $this->getMounts($dir);
  131. foreach ($mounts as $mount) {
  132. if (is_null($mount->getStorage())) {
  133. continue;
  134. }
  135. $storage = $mount->getStorage();
  136. // if the home storage isn't writable then the scanner is run as the wrong user
  137. if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and
  138. (!$storage->isCreatable('') or !$storage->isCreatable('files'))
  139. ) {
  140. if ($storage->file_exists('') or $storage->getCache()->inCache('')) {
  141. throw new ForbiddenException();
  142. } else {// if the root exists in neither the cache nor the storage the user isn't setup yet
  143. break;
  144. }
  145. }
  146. $relativePath = $mount->getInternalPath($dir);
  147. $scanner = $storage->getScanner();
  148. $scanner->setUseTransactions(false);
  149. $this->attachListener($mount);
  150. $isDbLocking = \OC::$server->getLockingProvider() instanceof DBLockingProvider;
  151. $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
  152. $this->triggerPropagator($storage, $path);
  153. });
  154. $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
  155. $this->triggerPropagator($storage, $path);
  156. });
  157. $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
  158. $this->triggerPropagator($storage, $path);
  159. });
  160. if (!$isDbLocking) {
  161. $this->db->beginTransaction();
  162. }
  163. try {
  164. $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
  165. $cache = $storage->getCache();
  166. if ($cache instanceof Cache) {
  167. // only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner
  168. $cache->correctFolderSize($relativePath);
  169. }
  170. } catch (StorageNotAvailableException $e) {
  171. $this->logger->error('Storage ' . $storage->getId() . ' not available');
  172. $this->logger->logException($e);
  173. $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
  174. }
  175. if (!$isDbLocking) {
  176. $this->db->commit();
  177. }
  178. }
  179. }
  180. private function triggerPropagator(IStorage $storage, $internalPath) {
  181. $storage->getPropagator()->propagateChange($internalPath, time());
  182. }
  183. }