Scanner.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 OCA\Files_Sharing\SharedStorage;
  33. use OCP\Files\Storage\IStorage;
  34. use OCP\Files\StorageNotAvailableException;
  35. use OCP\ILogger;
  36. /**
  37. * Class Scanner
  38. *
  39. * Hooks available in scope \OC\Utils\Scanner
  40. * - scanFile(string $absolutePath)
  41. * - scanFolder(string $absolutePath)
  42. *
  43. * @package OC\Files\Utils
  44. */
  45. class Scanner extends PublicEmitter {
  46. /**
  47. * @var string $user
  48. */
  49. private $user;
  50. /**
  51. * @var \OCP\IDBConnection
  52. */
  53. protected $db;
  54. /**
  55. * @var ILogger
  56. */
  57. protected $logger;
  58. /**
  59. * @param string $user
  60. * @param \OCP\IDBConnection $db
  61. * @param ILogger $logger
  62. */
  63. public function __construct($user, $db, ILogger $logger) {
  64. $this->logger = $logger;
  65. $this->user = $user;
  66. $this->db = $db;
  67. }
  68. /**
  69. * get all storages for $dir
  70. *
  71. * @param string $dir
  72. * @return \OC\Files\Mount\MountPoint[]
  73. */
  74. protected function getMounts($dir) {
  75. //TODO: move to the node based fileapi once that's done
  76. \OC_Util::tearDownFS();
  77. \OC_Util::setupFS($this->user);
  78. $mountManager = Filesystem::getMountManager();
  79. $mounts = $mountManager->findIn($dir);
  80. $mounts[] = $mountManager->find($dir);
  81. $mounts = array_reverse($mounts); //start with the mount of $dir
  82. return $mounts;
  83. }
  84. /**
  85. * attach listeners to the scanner
  86. *
  87. * @param \OC\Files\Mount\MountPoint $mount
  88. */
  89. protected function attachListener($mount) {
  90. $scanner = $mount->getStorage()->getScanner();
  91. $emitter = $this;
  92. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
  93. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
  94. });
  95. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
  96. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
  97. });
  98. $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) {
  99. $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
  100. });
  101. $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) {
  102. $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
  103. });
  104. }
  105. /**
  106. * @param string $dir
  107. */
  108. public function backgroundScan($dir) {
  109. $mounts = $this->getMounts($dir);
  110. foreach ($mounts as $mount) {
  111. $storage = $mount->getStorage();
  112. if (is_null($storage)) {
  113. continue;
  114. }
  115. // don't bother scanning failed storages (shortcut for same result)
  116. if ($storage->instanceOfStorage('OC\Files\Storage\FailedStorage')) {
  117. continue;
  118. }
  119. // don't scan the root storage
  120. if ($storage->instanceOfStorage('\OC\Files\Storage\Local') && $mount->getMountPoint() === '/') {
  121. continue;
  122. }
  123. // don't scan received local shares, these can be scanned when scanning the owner's storage
  124. if ($storage->instanceOfStorage(SharedStorage::class)) {
  125. continue;
  126. }
  127. $scanner = $storage->getScanner();
  128. $this->attachListener($mount);
  129. $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
  130. $this->triggerPropagator($storage, $path);
  131. });
  132. $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
  133. $this->triggerPropagator($storage, $path);
  134. });
  135. $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
  136. $this->triggerPropagator($storage, $path);
  137. });
  138. $propagator = $storage->getPropagator();
  139. $propagator->beginBatch();
  140. $scanner->backgroundScan();
  141. $propagator->commitBatch();
  142. }
  143. }
  144. /**
  145. * @param string $dir
  146. * @throws \OC\ForbiddenException
  147. */
  148. public function scan($dir = '') {
  149. if (!Filesystem::isValidPath($dir)) {
  150. throw new \InvalidArgumentException('Invalid path to scan');
  151. }
  152. $mounts = $this->getMounts($dir);
  153. foreach ($mounts as $mount) {
  154. $storage = $mount->getStorage();
  155. if (is_null($storage)) {
  156. continue;
  157. }
  158. // don't bother scanning failed storages (shortcut for same result)
  159. if ($storage->instanceOfStorage('OC\Files\Storage\FailedStorage')) {
  160. continue;
  161. }
  162. // if the home storage isn't writable then the scanner is run as the wrong user
  163. if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and
  164. (!$storage->isCreatable('') or !$storage->isCreatable('files'))
  165. ) {
  166. if ($storage->file_exists('') or $storage->getCache()->inCache('')) {
  167. throw new ForbiddenException();
  168. } else {// if the root exists in neither the cache nor the storage the user isn't setup yet
  169. break;
  170. }
  171. }
  172. // don't scan received local shares, these can be scanned when scanning the owner's storage
  173. if ($storage->instanceOfStorage(SharedStorage::class)) {
  174. continue;
  175. }
  176. $relativePath = $mount->getInternalPath($dir);
  177. $scanner = $storage->getScanner();
  178. $scanner->setUseTransactions(false);
  179. $this->attachListener($mount);
  180. $isDbLocking = \OC::$server->getLockingProvider() instanceof DBLockingProvider;
  181. $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
  182. $this->triggerPropagator($storage, $path);
  183. });
  184. $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
  185. $this->triggerPropagator($storage, $path);
  186. });
  187. $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
  188. $this->triggerPropagator($storage, $path);
  189. });
  190. if (!$isDbLocking) {
  191. $this->db->beginTransaction();
  192. }
  193. try {
  194. $propagator = $storage->getPropagator();
  195. $propagator->beginBatch();
  196. $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
  197. $cache = $storage->getCache();
  198. if ($cache instanceof Cache) {
  199. // only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner
  200. $cache->correctFolderSize($relativePath);
  201. }
  202. $propagator->commitBatch();
  203. } catch (StorageNotAvailableException $e) {
  204. $this->logger->error('Storage ' . $storage->getId() . ' not available');
  205. $this->logger->logException($e);
  206. $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
  207. }
  208. if (!$isDbLocking) {
  209. $this->db->commit();
  210. }
  211. }
  212. }
  213. private function triggerPropagator(IStorage $storage, $internalPath) {
  214. $storage->getPropagator()->propagateChange($internalPath, time());
  215. }
  216. }