Updater.php 7.9 KB

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