Updater.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Jagszent <daniel@jagszent.de>
  8. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Cache;
  29. use Doctrine\DBAL\Exception\DeadlockException;
  30. use OC\Files\FileInfo;
  31. use OCP\Files\Cache\ICacheEntry;
  32. use OCP\Files\Cache\IUpdater;
  33. use OCP\Files\Storage\IStorage;
  34. use Psr\Log\LoggerInterface;
  35. /**
  36. * Update the cache and propagate changes
  37. *
  38. */
  39. class Updater implements IUpdater {
  40. /**
  41. * @var bool
  42. */
  43. protected $enabled = true;
  44. /**
  45. * @var \OC\Files\Storage\Storage
  46. */
  47. protected $storage;
  48. /**
  49. * @var \OC\Files\Cache\Propagator
  50. */
  51. protected $propagator;
  52. /**
  53. * @var Scanner
  54. */
  55. protected $scanner;
  56. /**
  57. * @var Cache
  58. */
  59. protected $cache;
  60. private LoggerInterface $logger;
  61. /**
  62. * @param \OC\Files\Storage\Storage $storage
  63. */
  64. public function __construct(\OC\Files\Storage\Storage $storage) {
  65. $this->storage = $storage;
  66. $this->propagator = $storage->getPropagator();
  67. $this->scanner = $storage->getScanner();
  68. $this->cache = $storage->getCache();
  69. $this->logger = \OC::$server->get(LoggerInterface::class);
  70. }
  71. /**
  72. * Disable updating the cache through this updater
  73. */
  74. public function disable() {
  75. $this->enabled = false;
  76. }
  77. /**
  78. * Re-enable the updating of the cache through this updater
  79. */
  80. public function enable() {
  81. $this->enabled = true;
  82. }
  83. /**
  84. * Get the propagator for etags and mtime for the view the updater works on
  85. *
  86. * @return Propagator
  87. */
  88. public function getPropagator() {
  89. return $this->propagator;
  90. }
  91. /**
  92. * Propagate etag and mtime changes for the parent folders of $path up to the root of the filesystem
  93. *
  94. * @param string $path the path of the file to propagate the changes for
  95. * @param int|null $time the timestamp to set as mtime for the parent folders, if left out the current time is used
  96. */
  97. public function propagate($path, $time = null) {
  98. if (Scanner::isPartialFile($path)) {
  99. return;
  100. }
  101. $this->propagator->propagateChange($path, $time);
  102. }
  103. /**
  104. * Update the cache for $path and update the size, etag and mtime of the parent folders
  105. *
  106. * @param string $path
  107. * @param int $time
  108. */
  109. public function update($path, $time = null, ?int $sizeDifference = null) {
  110. if (!$this->enabled or Scanner::isPartialFile($path)) {
  111. return;
  112. }
  113. if (is_null($time)) {
  114. $time = time();
  115. }
  116. $data = $this->scanner->scan($path, Scanner::SCAN_SHALLOW, -1, false);
  117. if (isset($data['oldSize']) && isset($data['size'])) {
  118. $sizeDifference = $data['size'] - $data['oldSize'];
  119. }
  120. // encryption is a pita and touches the cache itself
  121. if (isset($data['encrypted']) && !!$data['encrypted']) {
  122. $sizeDifference = null;
  123. }
  124. // scanner didn't provide size info, fallback to full size calculation
  125. if ($this->cache instanceof Cache && $sizeDifference === null) {
  126. $this->cache->correctFolderSize($path, $data);
  127. }
  128. $this->correctParentStorageMtime($path);
  129. $this->propagator->propagateChange($path, $time, $sizeDifference ?? 0);
  130. }
  131. /**
  132. * Remove $path from the cache and update the size, etag and mtime of the parent folders
  133. *
  134. * @param string $path
  135. */
  136. public function remove($path) {
  137. if (!$this->enabled or Scanner::isPartialFile($path)) {
  138. return;
  139. }
  140. $parent = dirname($path);
  141. if ($parent === '.') {
  142. $parent = '';
  143. }
  144. $entry = $this->cache->get($path);
  145. $this->cache->remove($path);
  146. $this->correctParentStorageMtime($path);
  147. if ($entry instanceof ICacheEntry) {
  148. $this->propagator->propagateChange($path, time(), -$entry->getSize());
  149. } else {
  150. $this->propagator->propagateChange($path, time());
  151. if ($this->cache instanceof Cache) {
  152. $this->cache->correctFolderSize($parent);
  153. }
  154. }
  155. }
  156. /**
  157. * Rename a file or folder in the cache and update the size, etag and mtime of the parent folders
  158. *
  159. * @param IStorage $sourceStorage
  160. * @param string $source
  161. * @param string $target
  162. */
  163. public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
  164. if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
  165. return;
  166. }
  167. $time = time();
  168. $sourceCache = $sourceStorage->getCache();
  169. $sourceUpdater = $sourceStorage->getUpdater();
  170. $sourcePropagator = $sourceStorage->getPropagator();
  171. $sourceInfo = $sourceCache->get($source);
  172. if ($sourceInfo !== false) {
  173. if ($this->cache->inCache($target)) {
  174. $this->cache->remove($target);
  175. }
  176. if ($sourceStorage === $this->storage) {
  177. $this->cache->move($source, $target);
  178. } else {
  179. $this->cache->moveFromCache($sourceCache, $source, $target);
  180. }
  181. $sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
  182. $targetExtension = pathinfo($target, PATHINFO_EXTENSION);
  183. $targetIsTrash = preg_match("/d\d+/", $targetExtension);
  184. if ($sourceExtension !== $targetExtension && $sourceInfo->getMimeType() !== FileInfo::MIMETYPE_FOLDER && !$targetIsTrash) {
  185. // handle mime type change
  186. $mimeType = $this->storage->getMimeType($target);
  187. $fileId = $this->cache->getId($target);
  188. $this->cache->update($fileId, ['mimetype' => $mimeType]);
  189. }
  190. }
  191. if ($sourceCache instanceof Cache) {
  192. $sourceCache->correctFolderSize($source);
  193. }
  194. if ($this->cache instanceof Cache) {
  195. $this->cache->correctFolderSize($target);
  196. }
  197. if ($sourceUpdater instanceof Updater) {
  198. $sourceUpdater->correctParentStorageMtime($source);
  199. }
  200. $this->correctParentStorageMtime($target);
  201. $this->updateStorageMTimeOnly($target);
  202. $sourcePropagator->propagateChange($source, $time);
  203. $this->propagator->propagateChange($target, $time);
  204. }
  205. private function updateStorageMTimeOnly($internalPath) {
  206. $fileId = $this->cache->getId($internalPath);
  207. if ($fileId !== -1) {
  208. $mtime = $this->storage->filemtime($internalPath);
  209. if ($mtime !== false) {
  210. $this->cache->update(
  211. $fileId, [
  212. 'mtime' => null, // this magic tells it to not overwrite mtime
  213. 'storage_mtime' => $mtime
  214. ]
  215. );
  216. }
  217. }
  218. }
  219. /**
  220. * update the storage_mtime of the direct parent in the cache to the mtime from the storage
  221. *
  222. * @param string $internalPath
  223. */
  224. private function correctParentStorageMtime($internalPath) {
  225. $parentId = $this->cache->getParentId($internalPath);
  226. $parent = dirname($internalPath);
  227. if ($parentId != -1) {
  228. $mtime = $this->storage->filemtime($parent);
  229. if ($mtime !== false) {
  230. try {
  231. $this->cache->update($parentId, ['storage_mtime' => $mtime]);
  232. } catch (DeadlockException $e) {
  233. // ignore the failure.
  234. // with failures concurrent updates, someone else would have already done it.
  235. // in the worst case the `storage_mtime` isn't updated, which should at most only trigger an extra rescan
  236. $this->logger->warning("Error while updating parent storage_mtime, should be safe to ignore", ['exception' => $e]);
  237. }
  238. }
  239. }
  240. }
  241. }