Scanner.php 22 KB

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