1
0

Updater.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\DeadlockException;
  9. use OC\Files\FileInfo;
  10. use OCP\Files\Cache\ICacheEntry;
  11. use OCP\Files\Cache\IUpdater;
  12. use OCP\Files\Storage\IStorage;
  13. use Psr\Log\LoggerInterface;
  14. /**
  15. * Update the cache and propagate changes
  16. *
  17. */
  18. class Updater implements IUpdater {
  19. /**
  20. * @var bool
  21. */
  22. protected $enabled = true;
  23. /**
  24. * @var \OC\Files\Storage\Storage
  25. */
  26. protected $storage;
  27. /**
  28. * @var \OC\Files\Cache\Propagator
  29. */
  30. protected $propagator;
  31. /**
  32. * @var Scanner
  33. */
  34. protected $scanner;
  35. /**
  36. * @var Cache
  37. */
  38. protected $cache;
  39. private LoggerInterface $logger;
  40. /**
  41. * @param \OC\Files\Storage\Storage $storage
  42. */
  43. public function __construct(\OC\Files\Storage\Storage $storage) {
  44. $this->storage = $storage;
  45. $this->propagator = $storage->getPropagator();
  46. $this->scanner = $storage->getScanner();
  47. $this->cache = $storage->getCache();
  48. $this->logger = \OC::$server->get(LoggerInterface::class);
  49. }
  50. /**
  51. * Disable updating the cache through this updater
  52. */
  53. public function disable() {
  54. $this->enabled = false;
  55. }
  56. /**
  57. * Re-enable the updating of the cache through this updater
  58. */
  59. public function enable() {
  60. $this->enabled = true;
  61. }
  62. /**
  63. * Get the propagator for etags and mtime for the view the updater works on
  64. *
  65. * @return Propagator
  66. */
  67. public function getPropagator() {
  68. return $this->propagator;
  69. }
  70. /**
  71. * Propagate etag and mtime changes for the parent folders of $path up to the root of the filesystem
  72. *
  73. * @param string $path the path of the file to propagate the changes for
  74. * @param int|null $time the timestamp to set as mtime for the parent folders, if left out the current time is used
  75. */
  76. public function propagate($path, $time = null) {
  77. if (Scanner::isPartialFile($path)) {
  78. return;
  79. }
  80. $this->propagator->propagateChange($path, $time);
  81. }
  82. /**
  83. * Update the cache for $path and update the size, etag and mtime of the parent folders
  84. *
  85. * @param string $path
  86. * @param int $time
  87. */
  88. public function update($path, $time = null, ?int $sizeDifference = null) {
  89. if (!$this->enabled or Scanner::isPartialFile($path)) {
  90. return;
  91. }
  92. if (is_null($time)) {
  93. $time = time();
  94. }
  95. $data = $this->scanner->scan($path, Scanner::SCAN_SHALLOW, -1, false);
  96. if (isset($data['oldSize']) && isset($data['size'])) {
  97. $sizeDifference = $data['size'] - $data['oldSize'];
  98. }
  99. // encryption is a pita and touches the cache itself
  100. if (isset($data['encrypted']) && (bool)$data['encrypted']) {
  101. $sizeDifference = null;
  102. }
  103. // scanner didn't provide size info, fallback to full size calculation
  104. if ($this->cache instanceof Cache && $sizeDifference === null) {
  105. $this->cache->correctFolderSize($path, $data);
  106. }
  107. $this->correctParentStorageMtime($path);
  108. $this->propagator->propagateChange($path, $time, $sizeDifference ?? 0);
  109. }
  110. /**
  111. * Remove $path from the cache and update the size, etag and mtime of the parent folders
  112. *
  113. * @param string $path
  114. */
  115. public function remove($path) {
  116. if (!$this->enabled or Scanner::isPartialFile($path)) {
  117. return;
  118. }
  119. $parent = dirname($path);
  120. if ($parent === '.') {
  121. $parent = '';
  122. }
  123. $entry = $this->cache->get($path);
  124. $this->cache->remove($path);
  125. $this->correctParentStorageMtime($path);
  126. if ($entry instanceof ICacheEntry) {
  127. $this->propagator->propagateChange($path, time(), -$entry->getSize());
  128. } else {
  129. $this->propagator->propagateChange($path, time());
  130. if ($this->cache instanceof Cache) {
  131. $this->cache->correctFolderSize($parent);
  132. }
  133. }
  134. }
  135. /**
  136. * Rename a file or folder in the cache and update the size, etag and mtime of the parent folders
  137. *
  138. * @param IStorage $sourceStorage
  139. * @param string $source
  140. * @param string $target
  141. */
  142. public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
  143. if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
  144. return;
  145. }
  146. $time = time();
  147. $sourceCache = $sourceStorage->getCache();
  148. $sourceUpdater = $sourceStorage->getUpdater();
  149. $sourcePropagator = $sourceStorage->getPropagator();
  150. $sourceInfo = $sourceCache->get($source);
  151. if ($sourceInfo !== false) {
  152. if ($this->cache->inCache($target)) {
  153. $this->cache->remove($target);
  154. }
  155. if ($sourceStorage === $this->storage) {
  156. $this->cache->move($source, $target);
  157. } else {
  158. $this->cache->moveFromCache($sourceCache, $source, $target);
  159. }
  160. $sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
  161. $targetExtension = pathinfo($target, PATHINFO_EXTENSION);
  162. $targetIsTrash = preg_match("/d\d+/", $targetExtension);
  163. if ($sourceExtension !== $targetExtension && $sourceInfo->getMimeType() !== FileInfo::MIMETYPE_FOLDER && !$targetIsTrash) {
  164. // handle mime type change
  165. $mimeType = $this->storage->getMimeType($target);
  166. $fileId = $this->cache->getId($target);
  167. $this->cache->update($fileId, ['mimetype' => $mimeType]);
  168. }
  169. }
  170. if ($sourceCache instanceof Cache) {
  171. $sourceCache->correctFolderSize($source);
  172. }
  173. if ($this->cache instanceof Cache) {
  174. $this->cache->correctFolderSize($target);
  175. }
  176. if ($sourceUpdater instanceof Updater) {
  177. $sourceUpdater->correctParentStorageMtime($source);
  178. }
  179. $this->correctParentStorageMtime($target);
  180. $this->updateStorageMTimeOnly($target);
  181. $sourcePropagator->propagateChange($source, $time);
  182. $this->propagator->propagateChange($target, $time);
  183. }
  184. private function updateStorageMTimeOnly($internalPath) {
  185. $fileId = $this->cache->getId($internalPath);
  186. if ($fileId !== -1) {
  187. $mtime = $this->storage->filemtime($internalPath);
  188. if ($mtime !== false) {
  189. $this->cache->update(
  190. $fileId, [
  191. 'mtime' => null, // this magic tells it to not overwrite mtime
  192. 'storage_mtime' => $mtime
  193. ]
  194. );
  195. }
  196. }
  197. }
  198. /**
  199. * update the storage_mtime of the direct parent in the cache to the mtime from the storage
  200. *
  201. * @param string $internalPath
  202. */
  203. private function correctParentStorageMtime($internalPath) {
  204. $parentId = $this->cache->getParentId($internalPath);
  205. $parent = dirname($internalPath);
  206. if ($parentId != -1) {
  207. $mtime = $this->storage->filemtime($parent);
  208. if ($mtime !== false) {
  209. try {
  210. $this->cache->update($parentId, ['storage_mtime' => $mtime]);
  211. } catch (DeadlockException $e) {
  212. // ignore the failure.
  213. // with failures concurrent updates, someone else would have already done it.
  214. // in the worst case the `storage_mtime` isn't updated, which should at most only trigger an extra rescan
  215. $this->logger->warning('Error while updating parent storage_mtime, should be safe to ignore', ['exception' => $e]);
  216. }
  217. }
  218. }
  219. }
  220. }