1
0

Updater.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. if ($this->cache instanceof Cache) {
  133. $this->cache->correctFolderSize($parent);
  134. }
  135. }
  136. }
  137. /**
  138. * Rename a file or folder in the cache.
  139. *
  140. * @param IStorage $sourceStorage
  141. * @param string $source
  142. * @param string $target
  143. */
  144. public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
  145. $this->copyOrRenameFromStorage($sourceStorage, $source, $target, function (ICache $sourceCache) use ($sourceStorage, $source, $target) {
  146. // Remove existing cache entry to no reuse the fileId.
  147. if ($this->cache->inCache($target)) {
  148. $this->cache->remove($target);
  149. }
  150. if ($sourceStorage === $this->storage) {
  151. $this->cache->move($source, $target);
  152. } else {
  153. $this->cache->moveFromCache($sourceCache, $source, $target);
  154. }
  155. });
  156. }
  157. /**
  158. * Copy a file or folder in the cache.
  159. */
  160. public function copyFromStorage(IStorage $sourceStorage, string $source, string $target): void {
  161. $this->copyOrRenameFromStorage($sourceStorage, $source, $target, function (ICache $sourceCache, ICacheEntry $sourceInfo) use ($target) {
  162. $parent = dirname($target);
  163. $parentInCache = $this->cache->inCache($parent);
  164. if (!$parentInCache) {
  165. $parentData = $this->scanner->scan($parent, Scanner::SCAN_SHALLOW, -1, false);
  166. $parentInCache = $parentData !== null;
  167. }
  168. if ($parentInCache) {
  169. $this->cache->copyFromCache($sourceCache, $sourceInfo, $target);
  170. }
  171. });
  172. }
  173. /**
  174. * Utility to copy or rename a file or folder in the cache and update the size, etag and mtime of the parent folders
  175. */
  176. private function copyOrRenameFromStorage(IStorage $sourceStorage, string $source, string $target, callable $operation): void {
  177. if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
  178. return;
  179. }
  180. $time = time();
  181. $sourceCache = $sourceStorage->getCache();
  182. $sourceUpdater = $sourceStorage->getUpdater();
  183. $sourcePropagator = $sourceStorage->getPropagator();
  184. $sourceInfo = $sourceCache->get($source);
  185. $sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
  186. $targetExtension = pathinfo($target, PATHINFO_EXTENSION);
  187. $targetIsTrash = preg_match("/^d\d+$/", $targetExtension);
  188. if ($sourceInfo !== false) {
  189. if (!$this->storage->instanceOfStorage(ObjectStoreStorage::class)) {
  190. $operation($sourceCache, $sourceInfo);
  191. }
  192. $isDir = $sourceInfo->getMimeType() === FileInfo::MIMETYPE_FOLDER;
  193. } else {
  194. $isDir = $this->storage->is_dir($target);
  195. }
  196. if ($sourceExtension !== $targetExtension && !$isDir && !$targetIsTrash) {
  197. // handle mime type change
  198. $mimeType = $this->storage->getMimeType($target);
  199. $fileId = $this->cache->getId($target);
  200. $this->cache->update($fileId, ['mimetype' => $mimeType]);
  201. }
  202. if ($sourceCache instanceof Cache) {
  203. $sourceCache->correctFolderSize($source);
  204. }
  205. if ($this->cache instanceof Cache) {
  206. $this->cache->correctFolderSize($target);
  207. }
  208. if ($sourceUpdater instanceof Updater) {
  209. $sourceUpdater->correctParentStorageMtime($source);
  210. }
  211. $this->correctParentStorageMtime($target);
  212. $this->updateStorageMTimeOnly($target);
  213. $sourcePropagator->propagateChange($source, $time);
  214. $this->propagator->propagateChange($target, $time);
  215. }
  216. private function updateStorageMTimeOnly($internalPath) {
  217. $fileId = $this->cache->getId($internalPath);
  218. if ($fileId !== -1) {
  219. $mtime = $this->storage->filemtime($internalPath);
  220. if ($mtime !== false) {
  221. $this->cache->update(
  222. $fileId, [
  223. 'mtime' => null, // this magic tells it to not overwrite mtime
  224. 'storage_mtime' => $mtime
  225. ]
  226. );
  227. }
  228. }
  229. }
  230. /**
  231. * update the storage_mtime of the direct parent in the cache to the mtime from the storage
  232. *
  233. * @param string $internalPath
  234. */
  235. private function correctParentStorageMtime($internalPath) {
  236. $parentId = $this->cache->getParentId($internalPath);
  237. $parent = dirname($internalPath);
  238. if ($parentId != -1) {
  239. $mtime = $this->storage->filemtime($parent);
  240. if ($mtime !== false) {
  241. try {
  242. $this->cache->update($parentId, ['storage_mtime' => $mtime]);
  243. } catch (DeadlockException $e) {
  244. // ignore the failure.
  245. // with failures concurrent updates, someone else would have already done it.
  246. // in the worst case the `storage_mtime` isn't updated, which should at most only trigger an extra rescan
  247. $this->logger->warning('Error while updating parent storage_mtime, should be safe to ignore', ['exception' => $e]);
  248. }
  249. }
  250. }
  251. }
  252. }