Cache.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Cache;
  8. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  9. use OC\DB\Exceptions\DbalException;
  10. use OC\DB\QueryBuilder\Sharded\ShardDefinition;
  11. use OC\Files\Search\SearchComparison;
  12. use OC\Files\Search\SearchQuery;
  13. use OC\Files\Storage\Wrapper\Encryption;
  14. use OC\SystemConfig;
  15. use OCP\DB\QueryBuilder\IQueryBuilder;
  16. use OCP\EventDispatcher\IEventDispatcher;
  17. use OCP\Files\Cache\CacheEntryInsertedEvent;
  18. use OCP\Files\Cache\CacheEntryRemovedEvent;
  19. use OCP\Files\Cache\CacheEntryUpdatedEvent;
  20. use OCP\Files\Cache\CacheInsertEvent;
  21. use OCP\Files\Cache\CacheUpdateEvent;
  22. use OCP\Files\Cache\ICache;
  23. use OCP\Files\Cache\ICacheEntry;
  24. use OCP\Files\FileInfo;
  25. use OCP\Files\IMimeTypeLoader;
  26. use OCP\Files\Search\ISearchComparison;
  27. use OCP\Files\Search\ISearchOperator;
  28. use OCP\Files\Search\ISearchQuery;
  29. use OCP\Files\Storage\IStorage;
  30. use OCP\FilesMetadata\IFilesMetadataManager;
  31. use OCP\IDBConnection;
  32. use OCP\Util;
  33. use Psr\Log\LoggerInterface;
  34. /**
  35. * Metadata cache for a storage
  36. *
  37. * The cache stores the metadata for all files and folders in a storage and is kept up to date through the following mechanisms:
  38. *
  39. * - Scanner: scans the storage and updates the cache where needed
  40. * - Watcher: checks for changes made to the filesystem outside of the Nextcloud instance and rescans files and folder when a change is detected
  41. * - Updater: listens to changes made to the filesystem inside of the Nextcloud instance and updates the cache where needed
  42. * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater
  43. */
  44. class Cache implements ICache {
  45. use MoveFromCacheTrait {
  46. MoveFromCacheTrait::moveFromCache as moveFromCacheFallback;
  47. }
  48. /**
  49. * @var array partial data for the cache
  50. */
  51. protected array $partial = [];
  52. protected string $storageId;
  53. protected Storage $storageCache;
  54. protected IMimeTypeLoader$mimetypeLoader;
  55. protected IDBConnection $connection;
  56. protected SystemConfig $systemConfig;
  57. protected LoggerInterface $logger;
  58. protected QuerySearchHelper $querySearchHelper;
  59. protected IEventDispatcher $eventDispatcher;
  60. protected IFilesMetadataManager $metadataManager;
  61. public function __construct(
  62. private IStorage $storage,
  63. // this constructor is used in to many pleases to easily do proper di
  64. // so instead we group it all together
  65. ?CacheDependencies $dependencies = null,
  66. ) {
  67. $this->storageId = $storage->getId();
  68. if (strlen($this->storageId) > 64) {
  69. $this->storageId = md5($this->storageId);
  70. }
  71. if (!$dependencies) {
  72. $dependencies = \OC::$server->get(CacheDependencies::class);
  73. }
  74. $this->storageCache = new Storage($this->storage, true, $dependencies->getConnection());
  75. $this->mimetypeLoader = $dependencies->getMimeTypeLoader();
  76. $this->connection = $dependencies->getConnection();
  77. $this->systemConfig = $dependencies->getSystemConfig();
  78. $this->logger = $dependencies->getLogger();
  79. $this->querySearchHelper = $dependencies->getQuerySearchHelper();
  80. $this->eventDispatcher = $dependencies->getEventDispatcher();
  81. $this->metadataManager = $dependencies->getMetadataManager();
  82. }
  83. protected function getQueryBuilder() {
  84. return new CacheQueryBuilder(
  85. $this->connection->getQueryBuilder(),
  86. $this->metadataManager,
  87. );
  88. }
  89. public function getStorageCache(): Storage {
  90. return $this->storageCache;
  91. }
  92. /**
  93. * Get the numeric storage id for this cache's storage
  94. *
  95. * @return int
  96. */
  97. public function getNumericStorageId() {
  98. return $this->storageCache->getNumericId();
  99. }
  100. /**
  101. * get the stored metadata of a file or folder
  102. *
  103. * @param string | int $file either the path of a file or folder or the file id for a file or folder
  104. * @return ICacheEntry|false the cache entry as array or false if the file is not found in the cache
  105. */
  106. public function get($file) {
  107. $query = $this->getQueryBuilder();
  108. $query->selectFileCache();
  109. $metadataQuery = $query->selectMetadata();
  110. if (is_string($file) || $file == '') {
  111. // normalize file
  112. $file = $this->normalize($file);
  113. $query->wherePath($file);
  114. } else { //file id
  115. $query->whereFileId($file);
  116. }
  117. $query->whereStorageId($this->getNumericStorageId());
  118. $result = $query->execute();
  119. $data = $result->fetch();
  120. $result->closeCursor();
  121. //merge partial data
  122. if (!$data && is_string($file) && isset($this->partial[$file])) {
  123. return $this->partial[$file];
  124. } elseif (!$data) {
  125. return $data;
  126. } else {
  127. $data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
  128. return self::cacheEntryFromData($data, $this->mimetypeLoader);
  129. }
  130. }
  131. /**
  132. * Create a CacheEntry from database row
  133. *
  134. * @param array $data
  135. * @param IMimeTypeLoader $mimetypeLoader
  136. * @return CacheEntry
  137. */
  138. public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) {
  139. //fix types
  140. $data['name'] = (string)$data['name'];
  141. $data['path'] = (string)$data['path'];
  142. $data['fileid'] = (int)$data['fileid'];
  143. $data['parent'] = (int)$data['parent'];
  144. $data['size'] = Util::numericToNumber($data['size']);
  145. $data['unencrypted_size'] = Util::numericToNumber($data['unencrypted_size'] ?? 0);
  146. $data['mtime'] = (int)$data['mtime'];
  147. $data['storage_mtime'] = (int)$data['storage_mtime'];
  148. $data['encryptedVersion'] = (int)$data['encrypted'];
  149. $data['encrypted'] = (bool)$data['encrypted'];
  150. $data['storage_id'] = $data['storage'];
  151. $data['storage'] = (int)$data['storage'];
  152. $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']);
  153. $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']);
  154. if ($data['storage_mtime'] == 0) {
  155. $data['storage_mtime'] = $data['mtime'];
  156. }
  157. $data['permissions'] = (int)$data['permissions'];
  158. if (isset($data['creation_time'])) {
  159. $data['creation_time'] = (int)$data['creation_time'];
  160. }
  161. if (isset($data['upload_time'])) {
  162. $data['upload_time'] = (int)$data['upload_time'];
  163. }
  164. return new CacheEntry($data);
  165. }
  166. /**
  167. * get the metadata of all files stored in $folder
  168. *
  169. * @param string $folder
  170. * @return ICacheEntry[]
  171. */
  172. public function getFolderContents($folder) {
  173. $fileId = $this->getId($folder);
  174. return $this->getFolderContentsById($fileId);
  175. }
  176. /**
  177. * get the metadata of all files stored in $folder
  178. *
  179. * @param int $fileId the file id of the folder
  180. * @return ICacheEntry[]
  181. */
  182. public function getFolderContentsById($fileId) {
  183. if ($fileId > -1) {
  184. $query = $this->getQueryBuilder();
  185. $query->selectFileCache()
  186. ->whereParent($fileId)
  187. ->whereStorageId($this->getNumericStorageId())
  188. ->orderBy('name', 'ASC');
  189. $metadataQuery = $query->selectMetadata();
  190. $result = $query->execute();
  191. $files = $result->fetchAll();
  192. $result->closeCursor();
  193. return array_map(function (array $data) use ($metadataQuery) {
  194. $data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
  195. return self::cacheEntryFromData($data, $this->mimetypeLoader);
  196. }, $files);
  197. }
  198. return [];
  199. }
  200. /**
  201. * insert or update meta data for a file or folder
  202. *
  203. * @param string $file
  204. * @param array $data
  205. *
  206. * @return int file id
  207. * @throws \RuntimeException
  208. */
  209. public function put($file, array $data) {
  210. if (($id = $this->getId($file)) > -1) {
  211. $this->update($id, $data);
  212. return $id;
  213. } else {
  214. return $this->insert($file, $data);
  215. }
  216. }
  217. /**
  218. * insert meta data for a new file or folder
  219. *
  220. * @param string $file
  221. * @param array $data
  222. *
  223. * @return int file id
  224. * @throws \RuntimeException
  225. */
  226. public function insert($file, array $data) {
  227. // normalize file
  228. $file = $this->normalize($file);
  229. if (isset($this->partial[$file])) { //add any saved partial data
  230. $data = array_merge($this->partial[$file], $data);
  231. unset($this->partial[$file]);
  232. }
  233. $requiredFields = ['size', 'mtime', 'mimetype'];
  234. foreach ($requiredFields as $field) {
  235. if (!isset($data[$field])) { //data not complete save as partial and return
  236. $this->partial[$file] = $data;
  237. return -1;
  238. }
  239. }
  240. $data['path'] = $file;
  241. if (!isset($data['parent'])) {
  242. $data['parent'] = $this->getParentId($file);
  243. }
  244. if ($data['parent'] === -1 && $file !== '') {
  245. throw new \Exception('Parent folder not in filecache for ' . $file);
  246. }
  247. $data['name'] = basename($file);
  248. [$values, $extensionValues] = $this->normalizeData($data);
  249. $storageId = $this->getNumericStorageId();
  250. $values['storage'] = $storageId;
  251. try {
  252. $builder = $this->connection->getQueryBuilder();
  253. $builder->insert('filecache');
  254. foreach ($values as $column => $value) {
  255. $builder->setValue($column, $builder->createNamedParameter($value));
  256. }
  257. if ($builder->execute()) {
  258. $fileId = $builder->getLastInsertId();
  259. if (count($extensionValues)) {
  260. $query = $this->getQueryBuilder();
  261. $query->insert('filecache_extended');
  262. $query->hintShardKey('storage', $storageId);
  263. $query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
  264. foreach ($extensionValues as $column => $value) {
  265. $query->setValue($column, $query->createNamedParameter($value));
  266. }
  267. $query->execute();
  268. }
  269. $event = new CacheEntryInsertedEvent($this->storage, $file, $fileId, $storageId);
  270. $this->eventDispatcher->dispatch(CacheInsertEvent::class, $event);
  271. $this->eventDispatcher->dispatchTyped($event);
  272. return $fileId;
  273. }
  274. } catch (UniqueConstraintViolationException $e) {
  275. // entry exists already
  276. if ($this->connection->inTransaction()) {
  277. $this->connection->commit();
  278. $this->connection->beginTransaction();
  279. }
  280. }
  281. // The file was created in the mean time
  282. if (($id = $this->getId($file)) > -1) {
  283. $this->update($id, $data);
  284. return $id;
  285. } else {
  286. throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
  287. }
  288. }
  289. /**
  290. * update the metadata of an existing file or folder in the cache
  291. *
  292. * @param int $id the fileid of the existing file or folder
  293. * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged
  294. */
  295. public function update($id, array $data) {
  296. if (isset($data['path'])) {
  297. // normalize path
  298. $data['path'] = $this->normalize($data['path']);
  299. }
  300. if (isset($data['name'])) {
  301. // normalize path
  302. $data['name'] = $this->normalize($data['name']);
  303. }
  304. [$values, $extensionValues] = $this->normalizeData($data);
  305. if (count($values)) {
  306. $query = $this->getQueryBuilder();
  307. $query->update('filecache')
  308. ->whereFileId($id)
  309. ->whereStorageId($this->getNumericStorageId())
  310. ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) {
  311. return $query->expr()->orX(
  312. $query->expr()->neq($key, $query->createNamedParameter($value)),
  313. $query->expr()->isNull($key)
  314. );
  315. }, array_keys($values), array_values($values))));
  316. foreach ($values as $key => $value) {
  317. $query->set($key, $query->createNamedParameter($value));
  318. }
  319. $query->execute();
  320. }
  321. if (count($extensionValues)) {
  322. try {
  323. $query = $this->getQueryBuilder();
  324. $query->insert('filecache_extended');
  325. $query->hintShardKey('storage', $this->getNumericStorageId());
  326. $query->setValue('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT));
  327. foreach ($extensionValues as $column => $value) {
  328. $query->setValue($column, $query->createNamedParameter($value));
  329. }
  330. $query->execute();
  331. } catch (UniqueConstraintViolationException $e) {
  332. $query = $this->getQueryBuilder();
  333. $query->update('filecache_extended')
  334. ->whereFileId($id)
  335. ->hintShardKey('storage', $this->getNumericStorageId())
  336. ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) {
  337. return $query->expr()->orX(
  338. $query->expr()->neq($key, $query->createNamedParameter($value)),
  339. $query->expr()->isNull($key)
  340. );
  341. }, array_keys($extensionValues), array_values($extensionValues))));
  342. foreach ($extensionValues as $key => $value) {
  343. $query->set($key, $query->createNamedParameter($value));
  344. }
  345. $query->execute();
  346. }
  347. }
  348. $path = $this->getPathById($id);
  349. // path can still be null if the file doesn't exist
  350. if ($path !== null) {
  351. $event = new CacheEntryUpdatedEvent($this->storage, $path, $id, $this->getNumericStorageId());
  352. $this->eventDispatcher->dispatch(CacheUpdateEvent::class, $event);
  353. $this->eventDispatcher->dispatchTyped($event);
  354. }
  355. }
  356. /**
  357. * extract query parts and params array from data array
  358. *
  359. * @param array $data
  360. * @return array
  361. */
  362. protected function normalizeData(array $data): array {
  363. $fields = [
  364. 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted',
  365. 'etag', 'permissions', 'checksum', 'storage', 'unencrypted_size'];
  366. $extensionFields = ['metadata_etag', 'creation_time', 'upload_time'];
  367. $doNotCopyStorageMTime = false;
  368. if (array_key_exists('mtime', $data) && $data['mtime'] === null) {
  369. // this horrific magic tells it to not copy storage_mtime to mtime
  370. unset($data['mtime']);
  371. $doNotCopyStorageMTime = true;
  372. }
  373. $params = [];
  374. $extensionParams = [];
  375. foreach ($data as $name => $value) {
  376. if (in_array($name, $fields)) {
  377. if ($name === 'path') {
  378. $params['path_hash'] = md5($value);
  379. } elseif ($name === 'mimetype') {
  380. $params['mimepart'] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/')));
  381. $value = $this->mimetypeLoader->getId($value);
  382. } elseif ($name === 'storage_mtime') {
  383. if (!$doNotCopyStorageMTime && !isset($data['mtime'])) {
  384. $params['mtime'] = $value;
  385. }
  386. } elseif ($name === 'encrypted') {
  387. if (isset($data['encryptedVersion'])) {
  388. $value = $data['encryptedVersion'];
  389. } else {
  390. // Boolean to integer conversion
  391. $value = $value ? 1 : 0;
  392. }
  393. }
  394. $params[$name] = $value;
  395. }
  396. if (in_array($name, $extensionFields)) {
  397. $extensionParams[$name] = $value;
  398. }
  399. }
  400. return [$params, array_filter($extensionParams)];
  401. }
  402. /**
  403. * get the file id for a file
  404. *
  405. * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file
  406. *
  407. * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing
  408. *
  409. * @param string $file
  410. * @return int
  411. */
  412. public function getId($file) {
  413. // normalize file
  414. $file = $this->normalize($file);
  415. $query = $this->getQueryBuilder();
  416. $query->select('fileid')
  417. ->from('filecache')
  418. ->whereStorageId($this->getNumericStorageId())
  419. ->wherePath($file);
  420. $result = $query->execute();
  421. $id = $result->fetchOne();
  422. $result->closeCursor();
  423. return $id === false ? -1 : (int)$id;
  424. }
  425. /**
  426. * get the id of the parent folder of a file
  427. *
  428. * @param string $file
  429. * @return int
  430. */
  431. public function getParentId($file) {
  432. if ($file === '') {
  433. return -1;
  434. } else {
  435. $parent = $this->getParentPath($file);
  436. return (int)$this->getId($parent);
  437. }
  438. }
  439. private function getParentPath($path) {
  440. $parent = dirname($path);
  441. if ($parent === '.') {
  442. $parent = '';
  443. }
  444. return $parent;
  445. }
  446. /**
  447. * check if a file is available in the cache
  448. *
  449. * @param string $file
  450. * @return bool
  451. */
  452. public function inCache($file) {
  453. return $this->getId($file) != -1;
  454. }
  455. /**
  456. * remove a file or folder from the cache
  457. *
  458. * when removing a folder from the cache all files and folders inside the folder will be removed as well
  459. *
  460. * @param string $file
  461. */
  462. public function remove($file) {
  463. $entry = $this->get($file);
  464. if ($entry instanceof ICacheEntry) {
  465. $query = $this->getQueryBuilder();
  466. $query->delete('filecache')
  467. ->whereStorageId($this->getNumericStorageId())
  468. ->whereFileId($entry->getId());
  469. $query->execute();
  470. $query = $this->getQueryBuilder();
  471. $query->delete('filecache_extended')
  472. ->whereFileId($entry->getId())
  473. ->hintShardKey('storage', $this->getNumericStorageId());
  474. $query->execute();
  475. if ($entry->getMimeType() == FileInfo::MIMETYPE_FOLDER) {
  476. $this->removeChildren($entry);
  477. }
  478. $this->eventDispatcher->dispatchTyped(new CacheEntryRemovedEvent($this->storage, $entry->getPath(), $entry->getId(), $this->getNumericStorageId()));
  479. }
  480. }
  481. /**
  482. * Remove all children of a folder
  483. *
  484. * @param ICacheEntry $entry the cache entry of the folder to remove the children of
  485. * @throws \OC\DatabaseException
  486. */
  487. private function removeChildren(ICacheEntry $entry) {
  488. $parentIds = [$entry->getId()];
  489. $queue = [$entry->getId()];
  490. $deletedIds = [];
  491. $deletedPaths = [];
  492. // we walk depth first through the file tree, removing all filecache_extended attributes while we walk
  493. // and collecting all folder ids to later use to delete the filecache entries
  494. while ($entryId = array_pop($queue)) {
  495. $children = $this->getFolderContentsById($entryId);
  496. $childIds = array_map(function (ICacheEntry $cacheEntry) {
  497. return $cacheEntry->getId();
  498. }, $children);
  499. $childPaths = array_map(function (ICacheEntry $cacheEntry) {
  500. return $cacheEntry->getPath();
  501. }, $children);
  502. foreach ($childIds as $childId) {
  503. $deletedIds[] = $childId;
  504. }
  505. foreach ($childPaths as $childPath) {
  506. $deletedPaths[] = $childPath;
  507. }
  508. $query = $this->getQueryBuilder();
  509. $query->delete('filecache_extended')
  510. ->where($query->expr()->in('fileid', $query->createParameter('childIds')))
  511. ->hintShardKey('storage', $this->getNumericStorageId());
  512. foreach (array_chunk($childIds, 1000) as $childIdChunk) {
  513. $query->setParameter('childIds', $childIdChunk, IQueryBuilder::PARAM_INT_ARRAY);
  514. $query->execute();
  515. }
  516. /** @var ICacheEntry[] $childFolders */
  517. $childFolders = [];
  518. foreach ($children as $child) {
  519. if ($child->getMimeType() == FileInfo::MIMETYPE_FOLDER) {
  520. $childFolders[] = $child;
  521. }
  522. }
  523. foreach ($childFolders as $folder) {
  524. $parentIds[] = $folder->getId();
  525. $queue[] = $folder->getId();
  526. }
  527. }
  528. $query = $this->getQueryBuilder();
  529. $query->delete('filecache')
  530. ->whereStorageId($this->getNumericStorageId())
  531. ->whereParentInParameter('parentIds');
  532. // Sorting before chunking allows the db to find the entries close to each
  533. // other in the index
  534. sort($parentIds, SORT_NUMERIC);
  535. foreach (array_chunk($parentIds, 1000) as $parentIdChunk) {
  536. $query->setParameter('parentIds', $parentIdChunk, IQueryBuilder::PARAM_INT_ARRAY);
  537. $query->execute();
  538. }
  539. foreach (array_combine($deletedIds, $deletedPaths) as $fileId => $filePath) {
  540. $cacheEntryRemovedEvent = new CacheEntryRemovedEvent(
  541. $this->storage,
  542. $filePath,
  543. $fileId,
  544. $this->getNumericStorageId()
  545. );
  546. $this->eventDispatcher->dispatchTyped($cacheEntryRemovedEvent);
  547. }
  548. }
  549. /**
  550. * Move a file or folder in the cache
  551. *
  552. * @param string $source
  553. * @param string $target
  554. */
  555. public function move($source, $target) {
  556. $this->moveFromCache($this, $source, $target);
  557. }
  558. /**
  559. * Get the storage id and path needed for a move
  560. *
  561. * @param string $path
  562. * @return array [$storageId, $internalPath]
  563. */
  564. protected function getMoveInfo($path) {
  565. return [$this->getNumericStorageId(), $path];
  566. }
  567. protected function hasEncryptionWrapper(): bool {
  568. return $this->storage->instanceOfStorage(Encryption::class);
  569. }
  570. /**
  571. * Move a file or folder in the cache
  572. *
  573. * @param ICache $sourceCache
  574. * @param string $sourcePath
  575. * @param string $targetPath
  576. * @throws \OC\DatabaseException
  577. * @throws \Exception if the given storages have an invalid id
  578. */
  579. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  580. if ($sourceCache instanceof Cache) {
  581. // normalize source and target
  582. $sourcePath = $this->normalize($sourcePath);
  583. $targetPath = $this->normalize($targetPath);
  584. $sourceData = $sourceCache->get($sourcePath);
  585. if (!$sourceData) {
  586. throw new \Exception('Invalid source storage path: ' . $sourcePath);
  587. }
  588. $shardDefinition = $this->connection->getShardDefinition('filecache');
  589. if (
  590. $shardDefinition &&
  591. $shardDefinition->getShardForKey($sourceCache->getNumericStorageId()) !== $shardDefinition->getShardForKey($this->getNumericStorageId())
  592. ) {
  593. $this->moveFromStorageSharded($shardDefinition, $sourceCache, $sourceData, $targetPath);
  594. return;
  595. }
  596. $sourceId = $sourceData['fileid'];
  597. $newParentId = $this->getParentId($targetPath);
  598. [$sourceStorageId, $sourcePath] = $sourceCache->getMoveInfo($sourcePath);
  599. [$targetStorageId, $targetPath] = $this->getMoveInfo($targetPath);
  600. if (is_null($sourceStorageId) || $sourceStorageId === false) {
  601. throw new \Exception('Invalid source storage id: ' . $sourceStorageId);
  602. }
  603. if (is_null($targetStorageId) || $targetStorageId === false) {
  604. throw new \Exception('Invalid target storage id: ' . $targetStorageId);
  605. }
  606. if ($sourceData['mimetype'] === 'httpd/unix-directory') {
  607. //update all child entries
  608. $sourceLength = mb_strlen($sourcePath);
  609. $childIds = $this->getChildIds($sourceStorageId, $sourcePath);
  610. $childChunks = array_chunk($childIds, 1000);
  611. $query = $this->getQueryBuilder();
  612. $fun = $query->func();
  613. $newPathFunction = $fun->concat(
  614. $query->createNamedParameter($targetPath),
  615. $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash
  616. );
  617. $query->update('filecache')
  618. ->set('path_hash', $fun->md5($newPathFunction))
  619. ->set('path', $newPathFunction)
  620. ->whereStorageId($sourceStorageId)
  621. ->andWhere($query->expr()->in('fileid', $query->createParameter('files')));
  622. if ($sourceStorageId !== $targetStorageId) {
  623. $query->set('storage', $query->createNamedParameter($targetStorageId), IQueryBuilder::PARAM_INT);
  624. }
  625. // when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
  626. if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
  627. $query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
  628. }
  629. // Retry transaction in case of RetryableException like deadlocks.
  630. // Retry up to 4 times because we should receive up to 4 concurrent requests from the frontend
  631. $retryLimit = 4;
  632. for ($i = 1; $i <= $retryLimit; $i++) {
  633. try {
  634. $this->connection->beginTransaction();
  635. foreach ($childChunks as $chunk) {
  636. $query->setParameter('files', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
  637. $query->executeStatement();
  638. }
  639. break;
  640. } catch (\OC\DatabaseException $e) {
  641. $this->connection->rollBack();
  642. throw $e;
  643. } catch (DbalException $e) {
  644. $this->connection->rollBack();
  645. if (!$e->isRetryable()) {
  646. throw $e;
  647. }
  648. // Simply throw if we already retried 4 times.
  649. if ($i === $retryLimit) {
  650. throw $e;
  651. }
  652. // Sleep a bit to give some time to the other transaction to finish.
  653. usleep(100 * 1000 * $i);
  654. }
  655. }
  656. } else {
  657. $this->connection->beginTransaction();
  658. }
  659. $query = $this->getQueryBuilder();
  660. $query->update('filecache')
  661. ->set('path', $query->createNamedParameter($targetPath))
  662. ->set('path_hash', $query->createNamedParameter(md5($targetPath)))
  663. ->set('name', $query->createNamedParameter(basename($targetPath)))
  664. ->set('parent', $query->createNamedParameter($newParentId, IQueryBuilder::PARAM_INT))
  665. ->whereStorageId($sourceStorageId)
  666. ->whereFileId($sourceId);
  667. if ($sourceStorageId !== $targetStorageId) {
  668. $query->set('storage', $query->createNamedParameter($targetStorageId), IQueryBuilder::PARAM_INT);
  669. }
  670. // when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
  671. if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
  672. $query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
  673. }
  674. $query->execute();
  675. $this->connection->commit();
  676. if ($sourceCache->getNumericStorageId() !== $this->getNumericStorageId()) {
  677. $this->eventDispatcher->dispatchTyped(new CacheEntryRemovedEvent($this->storage, $sourcePath, $sourceId, $sourceCache->getNumericStorageId()));
  678. $event = new CacheEntryInsertedEvent($this->storage, $targetPath, $sourceId, $this->getNumericStorageId());
  679. $this->eventDispatcher->dispatch(CacheInsertEvent::class, $event);
  680. $this->eventDispatcher->dispatchTyped($event);
  681. } else {
  682. $event = new CacheEntryUpdatedEvent($this->storage, $targetPath, $sourceId, $this->getNumericStorageId());
  683. $this->eventDispatcher->dispatch(CacheUpdateEvent::class, $event);
  684. $this->eventDispatcher->dispatchTyped($event);
  685. }
  686. } else {
  687. $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
  688. }
  689. }
  690. private function getChildIds(int $storageId, string $path): array {
  691. $query = $this->connection->getQueryBuilder();
  692. $query->select('fileid')
  693. ->from('filecache')
  694. ->where($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
  695. ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($path) . '/%')));
  696. return $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  697. }
  698. /**
  699. * remove all entries for files that are stored on the storage from the cache
  700. */
  701. public function clear() {
  702. $query = $this->getQueryBuilder();
  703. $query->delete('filecache')
  704. ->whereStorageId($this->getNumericStorageId());
  705. $query->execute();
  706. $query = $this->connection->getQueryBuilder();
  707. $query->delete('storages')
  708. ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
  709. $query->execute();
  710. }
  711. /**
  712. * Get the scan status of a file
  713. *
  714. * - Cache::NOT_FOUND: File is not in the cache
  715. * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known
  716. * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned
  717. * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned
  718. *
  719. * @param string $file
  720. *
  721. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  722. */
  723. public function getStatus($file) {
  724. // normalize file
  725. $file = $this->normalize($file);
  726. $query = $this->getQueryBuilder();
  727. $query->select('size')
  728. ->from('filecache')
  729. ->whereStorageId($this->getNumericStorageId())
  730. ->wherePath($file);
  731. $result = $query->execute();
  732. $size = $result->fetchOne();
  733. $result->closeCursor();
  734. if ($size !== false) {
  735. if ((int)$size === -1) {
  736. return self::SHALLOW;
  737. } else {
  738. return self::COMPLETE;
  739. }
  740. } else {
  741. if (isset($this->partial[$file])) {
  742. return self::PARTIAL;
  743. } else {
  744. return self::NOT_FOUND;
  745. }
  746. }
  747. }
  748. /**
  749. * search for files matching $pattern
  750. *
  751. * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
  752. * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
  753. */
  754. public function search($pattern) {
  755. $operator = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', $pattern);
  756. return $this->searchQuery(new SearchQuery($operator, 0, 0, [], null));
  757. }
  758. /**
  759. * search for files by mimetype
  760. *
  761. * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
  762. * where it will search for all mimetypes in the group ('image/*')
  763. * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
  764. */
  765. public function searchByMime($mimetype) {
  766. if (!str_contains($mimetype, '/')) {
  767. $operator = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%');
  768. } else {
  769. $operator = new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype);
  770. }
  771. return $this->searchQuery(new SearchQuery($operator, 0, 0, [], null));
  772. }
  773. public function searchQuery(ISearchQuery $query) {
  774. return current($this->querySearchHelper->searchInCaches($query, [$this]));
  775. }
  776. /**
  777. * Re-calculate the folder size and the size of all parent folders
  778. *
  779. * @param string|boolean $path
  780. * @param array $data (optional) meta data of the folder
  781. */
  782. public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
  783. $this->calculateFolderSize($path, $data);
  784. if ($path !== '') {
  785. $parent = dirname($path);
  786. if ($parent === '.' || $parent === '/') {
  787. $parent = '';
  788. }
  789. if ($isBackgroundScan) {
  790. $parentData = $this->get($parent);
  791. if ($parentData['size'] !== -1 && $this->getIncompleteChildrenCount($parentData['fileid']) === 0) {
  792. $this->correctFolderSize($parent, $parentData, $isBackgroundScan);
  793. }
  794. } else {
  795. $this->correctFolderSize($parent);
  796. }
  797. }
  798. }
  799. /**
  800. * get the incomplete count that shares parent $folder
  801. *
  802. * @param int $fileId the file id of the folder
  803. * @return int
  804. */
  805. public function getIncompleteChildrenCount($fileId) {
  806. if ($fileId > -1) {
  807. $query = $this->getQueryBuilder();
  808. $query->select($query->func()->count())
  809. ->from('filecache')
  810. ->whereParent($fileId)
  811. ->whereStorageId($this->getNumericStorageId())
  812. ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
  813. $result = $query->execute();
  814. $size = (int)$result->fetchOne();
  815. $result->closeCursor();
  816. return $size;
  817. }
  818. return -1;
  819. }
  820. /**
  821. * calculate the size of a folder and set it in the cache
  822. *
  823. * @param string $path
  824. * @param array|null|ICacheEntry $entry (optional) meta data of the folder
  825. * @return int|float
  826. */
  827. public function calculateFolderSize($path, $entry = null) {
  828. return $this->calculateFolderSizeInner($path, $entry);
  829. }
  830. /**
  831. * inner function because we can't add new params to the public function without breaking any child classes
  832. *
  833. * @param string $path
  834. * @param array|null|ICacheEntry $entry (optional) meta data of the folder
  835. * @param bool $ignoreUnknown don't mark the folder size as unknown if any of it's children are unknown
  836. * @return int|float
  837. */
  838. protected function calculateFolderSizeInner(string $path, $entry = null, bool $ignoreUnknown = false) {
  839. $totalSize = 0;
  840. if (is_null($entry) || !isset($entry['fileid'])) {
  841. $entry = $this->get($path);
  842. }
  843. if (isset($entry['mimetype']) && $entry['mimetype'] === FileInfo::MIMETYPE_FOLDER) {
  844. $id = $entry['fileid'];
  845. $query = $this->getQueryBuilder();
  846. $query->select('size', 'unencrypted_size')
  847. ->from('filecache')
  848. ->whereStorageId($this->getNumericStorageId())
  849. ->whereParent($id);
  850. if ($ignoreUnknown) {
  851. $query->andWhere($query->expr()->gte('size', $query->createNamedParameter(0)));
  852. }
  853. $result = $query->execute();
  854. $rows = $result->fetchAll();
  855. $result->closeCursor();
  856. if ($rows) {
  857. $sizes = array_map(function (array $row) {
  858. return Util::numericToNumber($row['size']);
  859. }, $rows);
  860. $unencryptedOnlySizes = array_map(function (array $row) {
  861. return Util::numericToNumber($row['unencrypted_size']);
  862. }, $rows);
  863. $unencryptedSizes = array_map(function (array $row) {
  864. return Util::numericToNumber(($row['unencrypted_size'] > 0) ? $row['unencrypted_size'] : $row['size']);
  865. }, $rows);
  866. $sum = array_sum($sizes);
  867. $min = min($sizes);
  868. $unencryptedSum = array_sum($unencryptedSizes);
  869. $unencryptedMin = min($unencryptedSizes);
  870. $unencryptedMax = max($unencryptedOnlySizes);
  871. $sum = 0 + $sum;
  872. $min = 0 + $min;
  873. if ($min === -1) {
  874. $totalSize = $min;
  875. } else {
  876. $totalSize = $sum;
  877. }
  878. if ($unencryptedMin === -1 || $min === -1) {
  879. $unencryptedTotal = $unencryptedMin;
  880. } else {
  881. $unencryptedTotal = $unencryptedSum;
  882. }
  883. } else {
  884. $totalSize = 0;
  885. $unencryptedTotal = 0;
  886. $unencryptedMax = 0;
  887. }
  888. // only set unencrypted size for a folder if any child entries have it set, or the folder is empty
  889. $shouldWriteUnEncryptedSize = $unencryptedMax > 0 || $totalSize === 0 || $entry['unencrypted_size'] > 0;
  890. if ($entry['size'] !== $totalSize || ($entry['unencrypted_size'] !== $unencryptedTotal && $shouldWriteUnEncryptedSize)) {
  891. if ($shouldWriteUnEncryptedSize) {
  892. // if all children have an unencrypted size of 0, just set the folder unencrypted size to 0 instead of summing the sizes
  893. if ($unencryptedMax === 0) {
  894. $unencryptedTotal = 0;
  895. }
  896. $this->update($id, [
  897. 'size' => $totalSize,
  898. 'unencrypted_size' => $unencryptedTotal,
  899. ]);
  900. } else {
  901. $this->update($id, [
  902. 'size' => $totalSize,
  903. ]);
  904. }
  905. }
  906. }
  907. return $totalSize;
  908. }
  909. /**
  910. * get all file ids on the files on the storage
  911. *
  912. * @return int[]
  913. */
  914. public function getAll() {
  915. $query = $this->getQueryBuilder();
  916. $query->select('fileid')
  917. ->from('filecache')
  918. ->whereStorageId($this->getNumericStorageId());
  919. $result = $query->execute();
  920. $files = $result->fetchAll(\PDO::FETCH_COLUMN);
  921. $result->closeCursor();
  922. return array_map(function ($id) {
  923. return (int)$id;
  924. }, $files);
  925. }
  926. /**
  927. * find a folder in the cache which has not been fully scanned
  928. *
  929. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  930. * use the one with the highest id gives the best result with the background scanner, since that is most
  931. * likely the folder where we stopped scanning previously
  932. *
  933. * @return string|false the path of the folder or false when no folder matched
  934. */
  935. public function getIncomplete() {
  936. // we select the fileid here first instead of directly selecting the path since this helps mariadb/mysql
  937. // to use the correct index.
  938. // The overhead of this should be minimal since the cost of selecting the path by id should be much lower
  939. // than the cost of finding an item with size < 0
  940. $query = $this->getQueryBuilder();
  941. $query->select('fileid')
  942. ->from('filecache')
  943. ->whereStorageId($this->getNumericStorageId())
  944. ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
  945. ->orderBy('fileid', 'DESC')
  946. ->setMaxResults(1);
  947. $result = $query->execute();
  948. $id = $result->fetchOne();
  949. $result->closeCursor();
  950. if ($id === false) {
  951. return false;
  952. }
  953. $path = $this->getPathById($id);
  954. return $path ?? false;
  955. }
  956. /**
  957. * get the path of a file on this storage by it's file id
  958. *
  959. * @param int $id the file id of the file or folder to search
  960. * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache
  961. */
  962. public function getPathById($id) {
  963. $query = $this->getQueryBuilder();
  964. $query->select('path')
  965. ->from('filecache')
  966. ->whereStorageId($this->getNumericStorageId())
  967. ->whereFileId($id);
  968. $result = $query->execute();
  969. $path = $result->fetchOne();
  970. $result->closeCursor();
  971. if ($path === false) {
  972. return null;
  973. }
  974. return (string)$path;
  975. }
  976. /**
  977. * get the storage id of the storage for a file and the internal path of the file
  978. * unlike getPathById this does not limit the search to files on this storage and
  979. * instead does a global search in the cache table
  980. *
  981. * @param int $id
  982. * @return array first element holding the storage id, second the path
  983. * @deprecated 17.0.0 use getPathById() instead
  984. */
  985. public static function getById($id) {
  986. $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  987. $query->select('path', 'storage')
  988. ->from('filecache')
  989. ->where($query->expr()->eq('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  990. $result = $query->execute();
  991. $row = $result->fetch();
  992. $result->closeCursor();
  993. if ($row) {
  994. $numericId = $row['storage'];
  995. $path = $row['path'];
  996. } else {
  997. return null;
  998. }
  999. if ($id = Storage::getStorageId($numericId)) {
  1000. return [$id, $path];
  1001. } else {
  1002. return null;
  1003. }
  1004. }
  1005. /**
  1006. * normalize the given path
  1007. *
  1008. * @param string $path
  1009. * @return string
  1010. */
  1011. public function normalize($path) {
  1012. return trim(\OC_Util::normalizeUnicode($path), '/');
  1013. }
  1014. /**
  1015. * Copy a file or folder in the cache
  1016. *
  1017. * @param ICache $sourceCache
  1018. * @param ICacheEntry $sourceEntry
  1019. * @param string $targetPath
  1020. * @return int fileId of copied entry
  1021. */
  1022. public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int {
  1023. if ($sourceEntry->getId() < 0) {
  1024. throw new \RuntimeException('Invalid source cache entry on copyFromCache');
  1025. }
  1026. $data = $this->cacheEntryToArray($sourceEntry);
  1027. // when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
  1028. if ($sourceCache instanceof Cache && $sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
  1029. $data['encrypted'] = 0;
  1030. }
  1031. $fileId = $this->put($targetPath, $data);
  1032. if ($fileId <= 0) {
  1033. throw new \RuntimeException('Failed to copy to ' . $targetPath . ' from cache with source data ' . json_encode($data) . ' ');
  1034. }
  1035. if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
  1036. $folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
  1037. foreach ($folderContent as $subEntry) {
  1038. $subTargetPath = $targetPath . '/' . $subEntry->getName();
  1039. $this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
  1040. }
  1041. }
  1042. return $fileId;
  1043. }
  1044. private function cacheEntryToArray(ICacheEntry $entry): array {
  1045. return [
  1046. 'size' => $entry->getSize(),
  1047. 'mtime' => $entry->getMTime(),
  1048. 'storage_mtime' => $entry->getStorageMTime(),
  1049. 'mimetype' => $entry->getMimeType(),
  1050. 'mimepart' => $entry->getMimePart(),
  1051. 'etag' => $entry->getEtag(),
  1052. 'permissions' => $entry->getPermissions(),
  1053. 'encrypted' => $entry->isEncrypted(),
  1054. 'creation_time' => $entry->getCreationTime(),
  1055. 'upload_time' => $entry->getUploadTime(),
  1056. 'metadata_etag' => $entry->getMetadataEtag(),
  1057. ];
  1058. }
  1059. public function getQueryFilterForStorage(): ISearchOperator {
  1060. return new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'storage', $this->getNumericStorageId());
  1061. }
  1062. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
  1063. if ($rawEntry->getStorageId() === $this->getNumericStorageId()) {
  1064. return $rawEntry;
  1065. } else {
  1066. return null;
  1067. }
  1068. }
  1069. private function moveFromStorageSharded(ShardDefinition $shardDefinition, ICache $sourceCache, ICacheEntry $sourceEntry, $targetPath): void {
  1070. if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
  1071. $fileIds = $this->getChildIds($sourceCache->getNumericStorageId(), $sourceEntry->getPath());
  1072. } else {
  1073. $fileIds = [];
  1074. }
  1075. $fileIds[] = $sourceEntry->getId();
  1076. $helper = $this->connection->getCrossShardMoveHelper();
  1077. $sourceConnection = $helper->getConnection($shardDefinition, $sourceCache->getNumericStorageId());
  1078. $targetConnection = $helper->getConnection($shardDefinition, $this->getNumericStorageId());
  1079. $cacheItems = $helper->loadItems($sourceConnection, 'filecache', 'fileid', $fileIds);
  1080. $extendedItems = $helper->loadItems($sourceConnection, 'filecache_extended', 'fileid', $fileIds);
  1081. $metadataItems = $helper->loadItems($sourceConnection, 'files_metadata', 'file_id', $fileIds);
  1082. // when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
  1083. $removeEncryptedFlag = ($sourceCache instanceof Cache && $sourceCache->hasEncryptionWrapper()) && !$this->hasEncryptionWrapper();
  1084. $sourcePathLength = strlen($sourceEntry->getPath());
  1085. foreach ($cacheItems as &$cacheItem) {
  1086. if ($cacheItem['path'] === $sourceEntry->getPath()) {
  1087. $cacheItem['path'] = $targetPath;
  1088. $cacheItem['parent'] = $this->getParentId($targetPath);
  1089. $cacheItem['name'] = basename($cacheItem['path']);
  1090. } else {
  1091. $cacheItem['path'] = $targetPath . '/' . substr($cacheItem['path'], $sourcePathLength + 1); // +1 for the leading slash
  1092. }
  1093. $cacheItem['path_hash'] = md5($cacheItem['path']);
  1094. $cacheItem['storage'] = $this->getNumericStorageId();
  1095. if ($removeEncryptedFlag) {
  1096. $cacheItem['encrypted'] = 0;
  1097. }
  1098. }
  1099. $targetConnection->beginTransaction();
  1100. try {
  1101. $helper->saveItems($targetConnection, 'filecache', $cacheItems);
  1102. $helper->saveItems($targetConnection, 'filecache_extended', $extendedItems);
  1103. $helper->saveItems($targetConnection, 'files_metadata', $metadataItems);
  1104. } catch (\Exception $e) {
  1105. $targetConnection->rollback();
  1106. throw $e;
  1107. }
  1108. $sourceConnection->beginTransaction();
  1109. try {
  1110. $helper->deleteItems($sourceConnection, 'filecache', 'fileid', $fileIds);
  1111. $helper->deleteItems($sourceConnection, 'filecache_extended', 'fileid', $fileIds);
  1112. $helper->deleteItems($sourceConnection, 'files_metadata', 'file_id', $fileIds);
  1113. } catch (\Exception $e) {
  1114. $targetConnection->rollback();
  1115. $sourceConnection->rollBack();
  1116. throw $e;
  1117. }
  1118. try {
  1119. $sourceConnection->commit();
  1120. } catch (\Exception $e) {
  1121. $targetConnection->rollback();
  1122. throw $e;
  1123. }
  1124. $targetConnection->commit();
  1125. }
  1126. }