Scanner.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Ari Selseng <ari@selseng.net>
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Jagszent <daniel@jagszent.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Martin Mattel <martin.mattel@diemattels.at>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Owen Winkler <a_github@midnightcircus.com>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Robin McCorkell <robin@mccorkell.me.uk>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. * @author Vincent Petry <vincent@nextcloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OC\Files\Cache;
  37. use Doctrine\DBAL\Exception;
  38. use OC\Files\Storage\Wrapper\Encryption;
  39. use OC\Files\Storage\Wrapper\Jail;
  40. use OC\Hooks\BasicEmitter;
  41. use OCP\Files\Cache\IScanner;
  42. use OCP\Files\ForbiddenException;
  43. use OCP\Files\NotFoundException;
  44. use OCP\Files\Storage\IReliableEtagStorage;
  45. use OCP\IDBConnection;
  46. use OCP\Lock\ILockingProvider;
  47. use Psr\Log\LoggerInterface;
  48. /**
  49. * Class Scanner
  50. *
  51. * Hooks available in scope \OC\Files\Cache\Scanner:
  52. * - scanFile(string $path, string $storageId)
  53. * - scanFolder(string $path, string $storageId)
  54. * - postScanFile(string $path, string $storageId)
  55. * - postScanFolder(string $path, string $storageId)
  56. *
  57. * @package OC\Files\Cache
  58. */
  59. class Scanner extends BasicEmitter implements IScanner {
  60. /**
  61. * @var \OC\Files\Storage\Storage $storage
  62. */
  63. protected $storage;
  64. /**
  65. * @var string $storageId
  66. */
  67. protected $storageId;
  68. /**
  69. * @var \OC\Files\Cache\Cache $cache
  70. */
  71. protected $cache;
  72. /**
  73. * @var boolean $cacheActive If true, perform cache operations, if false, do not affect cache
  74. */
  75. protected $cacheActive;
  76. /**
  77. * @var bool $useTransactions whether to use transactions
  78. */
  79. protected $useTransactions = true;
  80. /**
  81. * @var \OCP\Lock\ILockingProvider
  82. */
  83. protected $lockingProvider;
  84. protected IDBConnection $connection;
  85. public function __construct(\OC\Files\Storage\Storage $storage) {
  86. $this->storage = $storage;
  87. $this->storageId = $this->storage->getId();
  88. $this->cache = $storage->getCache();
  89. $this->cacheActive = !\OC::$server->getConfig()->getSystemValueBool('filesystem_cache_readonly', false);
  90. $this->lockingProvider = \OC::$server->get(ILockingProvider::class);
  91. $this->connection = \OC::$server->get(IDBConnection::class);
  92. }
  93. /**
  94. * Whether to wrap the scanning of a folder in a database transaction
  95. * On default transactions are used
  96. *
  97. * @param bool $useTransactions
  98. */
  99. public function setUseTransactions($useTransactions) {
  100. $this->useTransactions = $useTransactions;
  101. }
  102. /**
  103. * get all the metadata of a file or folder
  104. * *
  105. *
  106. * @param string $path
  107. * @return array|null an array of metadata of the file
  108. */
  109. protected function getData($path) {
  110. $data = $this->storage->getMetaData($path);
  111. if (is_null($data)) {
  112. \OC::$server->get(LoggerInterface::class)->debug("!!! Path '$path' is not accessible or present !!!", ['app' => 'core']);
  113. }
  114. return $data;
  115. }
  116. /**
  117. * scan a single file and store it in the cache
  118. *
  119. * @param string $file
  120. * @param int $reuseExisting
  121. * @param int $parentId
  122. * @param array|null|false $cacheData existing data in the cache for the file to be scanned
  123. * @param bool $lock set to false to disable getting an additional read lock during scanning
  124. * @param null $data the metadata for the file, as returned by the storage
  125. * @return array|null an array of metadata of the scanned file
  126. * @throws \OCP\Lock\LockedException
  127. */
  128. public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true, $data = null) {
  129. if ($file !== '') {
  130. try {
  131. $this->storage->verifyPath(dirname($file), basename($file));
  132. } catch (\Exception $e) {
  133. return null;
  134. }
  135. }
  136. // only proceed if $file is not a partial file, blacklist is handled by the storage
  137. if (!self::isPartialFile($file)) {
  138. // acquire a lock
  139. if ($lock) {
  140. if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  141. $this->storage->acquireLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
  142. }
  143. }
  144. try {
  145. $data = $data ?? $this->getData($file);
  146. } catch (ForbiddenException $e) {
  147. if ($lock) {
  148. if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  149. $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
  150. }
  151. }
  152. return null;
  153. }
  154. try {
  155. if ($data) {
  156. // pre-emit only if it was a file. By that we avoid counting/treating folders as files
  157. if ($data['mimetype'] !== 'httpd/unix-directory') {
  158. $this->emit('\OC\Files\Cache\Scanner', 'scanFile', [$file, $this->storageId]);
  159. \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', ['path' => $file, 'storage' => $this->storageId]);
  160. }
  161. $parent = dirname($file);
  162. if ($parent === '.' || $parent === '/') {
  163. $parent = '';
  164. }
  165. if ($parentId === -1) {
  166. $parentId = $this->cache->getParentId($file);
  167. }
  168. // scan the parent if it's not in the cache (id -1) and the current file is not the root folder
  169. if ($file && $parentId === -1) {
  170. $parentData = $this->scanFile($parent);
  171. if (!$parentData) {
  172. return null;
  173. }
  174. $parentId = $parentData['fileid'];
  175. }
  176. if ($parent) {
  177. $data['parent'] = $parentId;
  178. }
  179. if (is_null($cacheData)) {
  180. /** @var CacheEntry $cacheData */
  181. $cacheData = $this->cache->get($file);
  182. }
  183. if ($cacheData && $reuseExisting && isset($cacheData['fileid'])) {
  184. // prevent empty etag
  185. $etag = empty($cacheData['etag']) ? $data['etag'] : $cacheData['etag'];
  186. $fileId = $cacheData['fileid'];
  187. $data['fileid'] = $fileId;
  188. // only reuse data if the file hasn't explicitly changed
  189. $mtimeUnchanged = isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime'];
  190. // if the folder is marked as unscanned, never reuse etags
  191. if ($mtimeUnchanged && $cacheData['size'] !== -1) {
  192. $data['mtime'] = $cacheData['mtime'];
  193. if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) {
  194. $data['size'] = $cacheData['size'];
  195. }
  196. if ($reuseExisting & self::REUSE_ETAG && !$this->storage->instanceOfStorage(IReliableEtagStorage::class)) {
  197. $data['etag'] = $etag;
  198. }
  199. }
  200. // we only updated unencrypted_size if it's already set
  201. if ($cacheData['unencrypted_size'] === 0) {
  202. unset($data['unencrypted_size']);
  203. }
  204. // Only update metadata that has changed
  205. // i.e. get all the values in $data that are not present in the cache already
  206. $newData = $this->array_diff_assoc_multi($data, $cacheData->getData());
  207. // make it known to the caller that etag has been changed and needs propagation
  208. if (isset($newData['etag'])) {
  209. $data['etag_changed'] = true;
  210. }
  211. } else {
  212. // we only updated unencrypted_size if it's already set
  213. unset($data['unencrypted_size']);
  214. $newData = $data;
  215. $fileId = -1;
  216. }
  217. if (!empty($newData)) {
  218. // Reset the checksum if the data has changed
  219. $newData['checksum'] = '';
  220. $newData['parent'] = $parentId;
  221. $data['fileid'] = $this->addToCache($file, $newData, $fileId);
  222. }
  223. $data['oldSize'] = ($cacheData && isset($cacheData['size'])) ? $cacheData['size'] : 0;
  224. if ($cacheData && isset($cacheData['encrypted'])) {
  225. $data['encrypted'] = $cacheData['encrypted'];
  226. }
  227. // post-emit only if it was a file. By that we avoid counting/treating folders as files
  228. if ($data['mimetype'] !== 'httpd/unix-directory') {
  229. $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', [$file, $this->storageId]);
  230. \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', ['path' => $file, 'storage' => $this->storageId]);
  231. }
  232. } else {
  233. $this->removeFromCache($file);
  234. }
  235. } catch (\Exception $e) {
  236. if ($lock) {
  237. if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  238. $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
  239. }
  240. }
  241. throw $e;
  242. }
  243. // release the acquired lock
  244. if ($lock) {
  245. if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  246. $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
  247. }
  248. }
  249. if ($data && !isset($data['encrypted'])) {
  250. $data['encrypted'] = false;
  251. }
  252. return $data;
  253. }
  254. return null;
  255. }
  256. protected function removeFromCache($path) {
  257. \OC_Hook::emit('Scanner', 'removeFromCache', ['file' => $path]);
  258. $this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', [$path]);
  259. if ($this->cacheActive) {
  260. $this->cache->remove($path);
  261. }
  262. }
  263. /**
  264. * @param string $path
  265. * @param array $data
  266. * @param int $fileId
  267. * @return int the id of the added file
  268. */
  269. protected function addToCache($path, $data, $fileId = -1) {
  270. if (isset($data['scan_permissions'])) {
  271. $data['permissions'] = $data['scan_permissions'];
  272. }
  273. \OC_Hook::emit('Scanner', 'addToCache', ['file' => $path, 'data' => $data]);
  274. $this->emit('\OC\Files\Cache\Scanner', 'addToCache', [$path, $this->storageId, $data, $fileId]);
  275. if ($this->cacheActive) {
  276. if ($fileId !== -1) {
  277. $this->cache->update($fileId, $data);
  278. return $fileId;
  279. } else {
  280. return $this->cache->insert($path, $data);
  281. }
  282. } else {
  283. return -1;
  284. }
  285. }
  286. /**
  287. * @param string $path
  288. * @param array $data
  289. * @param int $fileId
  290. */
  291. protected function updateCache($path, $data, $fileId = -1) {
  292. \OC_Hook::emit('Scanner', 'addToCache', ['file' => $path, 'data' => $data]);
  293. $this->emit('\OC\Files\Cache\Scanner', 'updateCache', [$path, $this->storageId, $data]);
  294. if ($this->cacheActive) {
  295. if ($fileId !== -1) {
  296. $this->cache->update($fileId, $data);
  297. } else {
  298. $this->cache->put($path, $data);
  299. }
  300. }
  301. }
  302. /**
  303. * scan a folder and all it's children
  304. *
  305. * @param string $path
  306. * @param bool $recursive
  307. * @param int $reuse
  308. * @param bool $lock set to false to disable getting an additional read lock during scanning
  309. * @return array|null an array of the meta data of the scanned file or folder
  310. */
  311. public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
  312. if ($reuse === -1) {
  313. $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG;
  314. }
  315. if ($lock) {
  316. if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  317. $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider);
  318. $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
  319. }
  320. }
  321. try {
  322. try {
  323. $data = $this->scanFile($path, $reuse, -1, null, $lock);
  324. if ($data && $data['mimetype'] === 'httpd/unix-directory') {
  325. $size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock, $data['size']);
  326. $data['size'] = $size;
  327. }
  328. } catch (NotFoundException $e) {
  329. $this->removeFromCache($path);
  330. return null;
  331. }
  332. } finally {
  333. if ($lock) {
  334. if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  335. $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
  336. $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider);
  337. }
  338. }
  339. }
  340. return $data;
  341. }
  342. /**
  343. * Compares $array1 against $array2 and returns all the values in $array1 that are not in $array2
  344. * Note this is a one-way check - i.e. we don't care about things that are in $array2 that aren't in $array1
  345. *
  346. * Supports multi-dimensional arrays
  347. * Also checks keys/indexes
  348. * Comparisons are strict just like array_diff_assoc
  349. * Order of keys/values does not matter
  350. *
  351. * @param array $array1
  352. * @param array $array2
  353. * @return array with the differences between $array1 and $array1
  354. * @throws \InvalidArgumentException if $array1 isn't an actual array
  355. *
  356. */
  357. protected function array_diff_assoc_multi(array $array1, array $array2) {
  358. $result = [];
  359. foreach ($array1 as $key => $value) {
  360. // if $array2 doesn't have the same key, that's a result
  361. if (!array_key_exists($key, $array2)) {
  362. $result[$key] = $value;
  363. continue;
  364. }
  365. // if $array2's value for the same key is different, that's a result
  366. if ($array2[$key] !== $value && !is_array($value)) {
  367. $result[$key] = $value;
  368. continue;
  369. }
  370. if (is_array($value)) {
  371. $nestedDiff = $this->array_diff_assoc_multi($value, $array2[$key]);
  372. if (!empty($nestedDiff)) {
  373. $result[$key] = $nestedDiff;
  374. continue;
  375. }
  376. }
  377. }
  378. return $result;
  379. }
  380. /**
  381. * Get the children currently in the cache
  382. *
  383. * @param int $folderId
  384. * @return array[]
  385. */
  386. protected function getExistingChildren($folderId) {
  387. $existingChildren = [];
  388. $children = $this->cache->getFolderContentsById($folderId);
  389. foreach ($children as $child) {
  390. $existingChildren[$child['name']] = $child;
  391. }
  392. return $existingChildren;
  393. }
  394. /**
  395. * scan all the files and folders in a folder
  396. *
  397. * @param string $path
  398. * @param bool|IScanner::SCAN_RECURSIVE_INCOMPLETE $recursive
  399. * @param int $reuse a combination of self::REUSE_*
  400. * @param int $folderId id for the folder to be scanned
  401. * @param bool $lock set to false to disable getting an additional read lock during scanning
  402. * @param int|float $oldSize the size of the folder before (re)scanning the children
  403. * @return int|float the size of the scanned folder or -1 if the size is unknown at this stage
  404. */
  405. protected function scanChildren(string $path, $recursive, int $reuse, int $folderId, bool $lock, int|float $oldSize, &$etagChanged = false) {
  406. if ($reuse === -1) {
  407. $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG;
  408. }
  409. $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', [$path, $this->storageId]);
  410. $size = 0;
  411. $childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size, $etagChanged);
  412. foreach ($childQueue as $child => [$childId, $childSize]) {
  413. // "etag changed" propagates up, but not down, so we pass `false` to the children even if we already know that the etag of the current folder changed
  414. $childEtagChanged = false;
  415. $childSize = $this->scanChildren($child, $recursive, $reuse, $childId, $lock, $childSize, $childEtagChanged);
  416. $etagChanged |= $childEtagChanged;
  417. if ($childSize === -1) {
  418. $size = -1;
  419. } elseif ($size !== -1) {
  420. $size += $childSize;
  421. }
  422. }
  423. // for encrypted storages, we trigger a regular folder size calculation instead of using the calculated size
  424. // to make sure we also updated the unencrypted-size where applicable
  425. if ($this->storage->instanceOfStorage(Encryption::class)) {
  426. $this->cache->calculateFolderSize($path);
  427. } else {
  428. if ($this->cacheActive) {
  429. $updatedData = [];
  430. if ($oldSize !== $size) {
  431. $updatedData['size'] = $size;
  432. }
  433. if ($etagChanged) {
  434. $updatedData['etag'] = uniqid();
  435. }
  436. if ($updatedData) {
  437. $this->cache->update($folderId, $updatedData);
  438. }
  439. }
  440. }
  441. $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', [$path, $this->storageId]);
  442. return $size;
  443. }
  444. /**
  445. * @param bool|IScanner::SCAN_RECURSIVE_INCOMPLETE $recursive
  446. */
  447. private function handleChildren(string $path, $recursive, int $reuse, int $folderId, bool $lock, int|float &$size, bool &$etagChanged): array {
  448. // we put this in it's own function so it cleans up the memory before we start recursing
  449. $existingChildren = $this->getExistingChildren($folderId);
  450. $newChildren = iterator_to_array($this->storage->getDirectoryContent($path));
  451. if (count($existingChildren) === 0 && count($newChildren) === 0) {
  452. // no need to do a transaction
  453. return [];
  454. }
  455. if ($this->useTransactions) {
  456. $this->connection->beginTransaction();
  457. }
  458. $exceptionOccurred = false;
  459. $childQueue = [];
  460. $newChildNames = [];
  461. foreach ($newChildren as $fileMeta) {
  462. $permissions = $fileMeta['scan_permissions'] ?? $fileMeta['permissions'];
  463. if ($permissions === 0) {
  464. continue;
  465. }
  466. $originalFile = $fileMeta['name'];
  467. $file = trim(\OC\Files\Filesystem::normalizePath($originalFile), '/');
  468. if (trim($originalFile, '/') !== $file) {
  469. // encoding mismatch, might require compatibility wrapper
  470. \OC::$server->get(LoggerInterface::class)->debug('Scanner: Skipping non-normalized file name "'. $originalFile . '" in path "' . $path . '".', ['app' => 'core']);
  471. $this->emit('\OC\Files\Cache\Scanner', 'normalizedNameMismatch', [$path ? $path . '/' . $originalFile : $originalFile]);
  472. // skip this entry
  473. continue;
  474. }
  475. $newChildNames[] = $file;
  476. $child = $path ? $path . '/' . $file : $file;
  477. try {
  478. $existingData = $existingChildren[$file] ?? false;
  479. $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock, $fileMeta);
  480. if ($data) {
  481. if ($data['mimetype'] === 'httpd/unix-directory' && $recursive === self::SCAN_RECURSIVE) {
  482. $childQueue[$child] = [$data['fileid'], $data['size']];
  483. } elseif ($data['mimetype'] === 'httpd/unix-directory' && $recursive === self::SCAN_RECURSIVE_INCOMPLETE && $data['size'] === -1) {
  484. // only recurse into folders which aren't fully scanned
  485. $childQueue[$child] = [$data['fileid'], $data['size']];
  486. } elseif ($data['size'] === -1) {
  487. $size = -1;
  488. } elseif ($size !== -1) {
  489. $size += $data['size'];
  490. }
  491. if (isset($data['etag_changed']) && $data['etag_changed']) {
  492. $etagChanged = true;
  493. }
  494. }
  495. } catch (Exception $ex) {
  496. // might happen if inserting duplicate while a scanning
  497. // process is running in parallel
  498. // log and ignore
  499. if ($this->useTransactions) {
  500. $this->connection->rollback();
  501. $this->connection->beginTransaction();
  502. }
  503. \OC::$server->get(LoggerInterface::class)->debug('Exception while scanning file "' . $child . '"', [
  504. 'app' => 'core',
  505. 'exception' => $ex,
  506. ]);
  507. $exceptionOccurred = true;
  508. } catch (\OCP\Lock\LockedException $e) {
  509. if ($this->useTransactions) {
  510. $this->connection->rollback();
  511. }
  512. throw $e;
  513. }
  514. }
  515. $removedChildren = \array_diff(array_keys($existingChildren), $newChildNames);
  516. foreach ($removedChildren as $childName) {
  517. $child = $path ? $path . '/' . $childName : $childName;
  518. $this->removeFromCache($child);
  519. }
  520. if ($this->useTransactions) {
  521. $this->connection->commit();
  522. }
  523. if ($exceptionOccurred) {
  524. // It might happen that the parallel scan process has already
  525. // inserted mimetypes but those weren't available yet inside the transaction
  526. // To make sure to have the updated mime types in such cases,
  527. // we reload them here
  528. \OC::$server->getMimeTypeLoader()->reset();
  529. }
  530. return $childQueue;
  531. }
  532. /**
  533. * check if the file should be ignored when scanning
  534. * NOTE: files with a '.part' extension are ignored as well!
  535. * prevents unfinished put requests to be scanned
  536. *
  537. * @param string $file
  538. * @return boolean
  539. */
  540. public static function isPartialFile($file) {
  541. if (pathinfo($file, PATHINFO_EXTENSION) === 'part') {
  542. return true;
  543. }
  544. if (str_contains($file, '.part/')) {
  545. return true;
  546. }
  547. return false;
  548. }
  549. /**
  550. * walk over any folders that are not fully scanned yet and scan them
  551. */
  552. public function backgroundScan() {
  553. if ($this->storage->instanceOfStorage(Jail::class)) {
  554. // for jail storage wrappers (shares, groupfolders) we run the background scan on the source storage
  555. // this is mainly done because the jail wrapper doesn't implement `getIncomplete` (because it would be inefficient).
  556. //
  557. // Running the scan on the source storage might scan more than "needed", but the unscanned files outside the jail will
  558. // have to be scanned at some point anyway.
  559. $unJailedScanner = $this->storage->getUnjailedStorage()->getScanner();
  560. $unJailedScanner->backgroundScan();
  561. } else {
  562. if (!$this->cache->inCache('')) {
  563. // if the storage isn't in the cache yet, just scan the root completely
  564. $this->runBackgroundScanJob(function () {
  565. $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG);
  566. }, '');
  567. } else {
  568. $lastPath = null;
  569. // find any path marked as unscanned and run the scanner until no more paths are unscanned (or we get stuck)
  570. while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) {
  571. $this->runBackgroundScanJob(function () use ($path) {
  572. $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE);
  573. }, $path);
  574. // FIXME: this won't proceed with the next item, needs revamping of getIncomplete()
  575. // to make this possible
  576. $lastPath = $path;
  577. }
  578. }
  579. }
  580. }
  581. protected function runBackgroundScanJob(callable $callback, $path) {
  582. try {
  583. $callback();
  584. \OC_Hook::emit('Scanner', 'correctFolderSize', ['path' => $path]);
  585. if ($this->cacheActive && $this->cache instanceof Cache) {
  586. $this->cache->correctFolderSize($path, null, true);
  587. }
  588. } catch (\OCP\Files\StorageInvalidException $e) {
  589. // skip unavailable storages
  590. } catch (\OCP\Files\StorageNotAvailableException $e) {
  591. // skip unavailable storages
  592. } catch (\OCP\Files\ForbiddenException $e) {
  593. // skip forbidden storages
  594. } catch (\OCP\Lock\LockedException $e) {
  595. // skip unavailable storages
  596. }
  597. }
  598. /**
  599. * Set whether the cache is affected by scan operations
  600. *
  601. * @param boolean $active The active state of the cache
  602. */
  603. public function setCacheActive($active) {
  604. $this->cacheActive = $active;
  605. }
  606. }