Scanner.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Utils;
  29. use OC\Files\Cache\Cache;
  30. use OC\Files\Filesystem;
  31. use OC\ForbiddenException;
  32. use OC\Hooks\PublicEmitter;
  33. use OC\Lock\DBLockingProvider;
  34. use OCA\Files_Sharing\SharedStorage;
  35. use OCP\Files\NotFoundException;
  36. use OCP\Files\Storage\IStorage;
  37. use OCP\Files\StorageNotAvailableException;
  38. use OCP\ILogger;
  39. use OC\Files\Storage\FailedStorage;
  40. /**
  41. * Class Scanner
  42. *
  43. * Hooks available in scope \OC\Utils\Scanner
  44. * - scanFile(string $absolutePath)
  45. * - scanFolder(string $absolutePath)
  46. *
  47. * @package OC\Files\Utils
  48. */
  49. class Scanner extends PublicEmitter {
  50. const MAX_ENTRIES_TO_COMMIT = 10000;
  51. /**
  52. * @var string $user
  53. */
  54. private $user;
  55. /**
  56. * @var \OCP\IDBConnection
  57. */
  58. protected $db;
  59. /**
  60. * @var ILogger
  61. */
  62. protected $logger;
  63. /**
  64. * Whether to use a DB transaction
  65. *
  66. * @var bool
  67. */
  68. protected $useTransaction;
  69. /**
  70. * Number of entries scanned to commit
  71. *
  72. * @var int
  73. */
  74. protected $entriesToCommit;
  75. /**
  76. * @param string $user
  77. * @param \OCP\IDBConnection $db
  78. * @param ILogger $logger
  79. */
  80. public function __construct($user, $db, ILogger $logger) {
  81. $this->logger = $logger;
  82. $this->user = $user;
  83. $this->db = $db;
  84. // when DB locking is used, no DB transactions will be used
  85. $this->useTransaction = !(\OC::$server->getLockingProvider() instanceof DBLockingProvider);
  86. }
  87. /**
  88. * get all storages for $dir
  89. *
  90. * @param string $dir
  91. * @return \OC\Files\Mount\MountPoint[]
  92. */
  93. protected function getMounts($dir) {
  94. //TODO: move to the node based fileapi once that's done
  95. \OC_Util::tearDownFS();
  96. \OC_Util::setupFS($this->user);
  97. $mountManager = Filesystem::getMountManager();
  98. $mounts = $mountManager->findIn($dir);
  99. $mounts[] = $mountManager->find($dir);
  100. $mounts = array_reverse($mounts); //start with the mount of $dir
  101. return $mounts;
  102. }
  103. /**
  104. * attach listeners to the scanner
  105. *
  106. * @param \OC\Files\Mount\MountPoint $mount
  107. */
  108. protected function attachListener($mount) {
  109. $scanner = $mount->getStorage()->getScanner();
  110. $emitter = $this;
  111. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
  112. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
  113. });
  114. $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
  115. $emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
  116. });
  117. $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFile', function ($path) use ($mount, $emitter) {
  118. $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFile', array($mount->getMountPoint() . $path));
  119. });
  120. $scanner->listen('\OC\Files\Cache\Scanner', 'postScanFolder', function ($path) use ($mount, $emitter) {
  121. $emitter->emit('\OC\Files\Utils\Scanner', 'postScanFolder', array($mount->getMountPoint() . $path));
  122. });
  123. }
  124. /**
  125. * @param string $dir
  126. */
  127. public function backgroundScan($dir) {
  128. $mounts = $this->getMounts($dir);
  129. foreach ($mounts as $mount) {
  130. $storage = $mount->getStorage();
  131. if (is_null($storage)) {
  132. continue;
  133. }
  134. // don't bother scanning failed storages (shortcut for same result)
  135. if ($storage->instanceOfStorage(FailedStorage::class)) {
  136. continue;
  137. }
  138. // don't scan received local shares, these can be scanned when scanning the owner's storage
  139. if ($storage->instanceOfStorage(SharedStorage::class)) {
  140. continue;
  141. }
  142. $scanner = $storage->getScanner();
  143. $this->attachListener($mount);
  144. $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
  145. $this->triggerPropagator($storage, $path);
  146. });
  147. $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
  148. $this->triggerPropagator($storage, $path);
  149. });
  150. $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
  151. $this->triggerPropagator($storage, $path);
  152. });
  153. $propagator = $storage->getPropagator();
  154. $propagator->beginBatch();
  155. $scanner->backgroundScan();
  156. $propagator->commitBatch();
  157. }
  158. }
  159. /**
  160. * @param string $dir
  161. * @param $recursive
  162. * @param callable|null $mountFilter
  163. * @throws ForbiddenException
  164. * @throws NotFoundException
  165. */
  166. public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECURSIVE, callable $mountFilter = null) {
  167. if (!Filesystem::isValidPath($dir)) {
  168. throw new \InvalidArgumentException('Invalid path to scan');
  169. }
  170. $mounts = $this->getMounts($dir);
  171. foreach ($mounts as $mount) {
  172. if ($mountFilter && !$mountFilter($mount)) {
  173. continue;
  174. }
  175. $storage = $mount->getStorage();
  176. if (is_null($storage)) {
  177. continue;
  178. }
  179. // don't bother scanning failed storages (shortcut for same result)
  180. if ($storage->instanceOfStorage(FailedStorage::class)) {
  181. continue;
  182. }
  183. // if the home storage isn't writable then the scanner is run as the wrong user
  184. if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and
  185. (!$storage->isCreatable('') or !$storage->isCreatable('files'))
  186. ) {
  187. if ($storage->file_exists('') or $storage->getCache()->inCache('')) {
  188. throw new ForbiddenException();
  189. } else {// if the root exists in neither the cache nor the storage the user isn't setup yet
  190. break;
  191. }
  192. }
  193. // don't scan received local shares, these can be scanned when scanning the owner's storage
  194. if ($storage->instanceOfStorage(SharedStorage::class)) {
  195. continue;
  196. }
  197. $relativePath = $mount->getInternalPath($dir);
  198. $scanner = $storage->getScanner();
  199. $scanner->setUseTransactions(false);
  200. $this->attachListener($mount);
  201. $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) {
  202. $this->postProcessEntry($storage, $path);
  203. });
  204. $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) {
  205. $this->postProcessEntry($storage, $path);
  206. });
  207. $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) {
  208. $this->postProcessEntry($storage, $path);
  209. });
  210. if (!$storage->file_exists($relativePath)) {
  211. throw new NotFoundException($dir);
  212. }
  213. if ($this->useTransaction) {
  214. $this->db->beginTransaction();
  215. }
  216. try {
  217. $propagator = $storage->getPropagator();
  218. $propagator->beginBatch();
  219. $scanner->scan($relativePath, $recursive, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
  220. $cache = $storage->getCache();
  221. if ($cache instanceof Cache) {
  222. // only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner
  223. $cache->correctFolderSize($relativePath);
  224. }
  225. $propagator->commitBatch();
  226. } catch (StorageNotAvailableException $e) {
  227. $this->logger->error('Storage ' . $storage->getId() . ' not available');
  228. $this->logger->logException($e);
  229. $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]);
  230. }
  231. if ($this->useTransaction) {
  232. $this->db->commit();
  233. }
  234. }
  235. }
  236. private function triggerPropagator(IStorage $storage, $internalPath) {
  237. $storage->getPropagator()->propagateChange($internalPath, time());
  238. }
  239. private function postProcessEntry(IStorage $storage, $internalPath) {
  240. $this->triggerPropagator($storage, $internalPath);
  241. if ($this->useTransaction) {
  242. $this->entriesToCommit++;
  243. if ($this->entriesToCommit >= self::MAX_ENTRIES_TO_COMMIT) {
  244. $propagator = $storage->getPropagator();
  245. $this->entriesToCommit = 0;
  246. $this->db->commit();
  247. $propagator->commitBatch();
  248. $this->db->beginTransaction();
  249. $propagator->beginBatch();
  250. }
  251. }
  252. }
  253. }