You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Updater.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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) {
  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 (
  118. isset($data['oldSize']) && isset($data['size']) &&
  119. !$data['encrypted'] // encryption is a pita and touches the cache itself
  120. ) {
  121. $sizeDifference = $data['size'] - $data['oldSize'];
  122. } else {
  123. // scanner didn't provide size info, fallback to full size calculation
  124. $sizeDifference = 0;
  125. if ($this->cache instanceof Cache) {
  126. $this->cache->correctFolderSize($path, $data);
  127. }
  128. }
  129. $this->correctParentStorageMtime($path);
  130. $this->propagator->propagateChange($path, $time, $sizeDifference);
  131. }
  132. /**
  133. * Remove $path from the cache and update the size, etag and mtime of the parent folders
  134. *
  135. * @param string $path
  136. */
  137. public function remove($path) {
  138. if (!$this->enabled or Scanner::isPartialFile($path)) {
  139. return;
  140. }
  141. $parent = dirname($path);
  142. if ($parent === '.') {
  143. $parent = '';
  144. }
  145. $entry = $this->cache->get($path);
  146. $this->cache->remove($path);
  147. $this->correctParentStorageMtime($path);
  148. if ($entry instanceof ICacheEntry) {
  149. $this->propagator->propagateChange($path, time(), -$entry->getSize());
  150. } else {
  151. $this->propagator->propagateChange($path, time());
  152. if ($this->cache instanceof Cache) {
  153. $this->cache->correctFolderSize($parent);
  154. }
  155. }
  156. }
  157. /**
  158. * Rename a file or folder in the cache and update the size, etag and mtime of the parent folders
  159. *
  160. * @param IStorage $sourceStorage
  161. * @param string $source
  162. * @param string $target
  163. */
  164. public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
  165. if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
  166. return;
  167. }
  168. $time = time();
  169. $sourceCache = $sourceStorage->getCache();
  170. $sourceUpdater = $sourceStorage->getUpdater();
  171. $sourcePropagator = $sourceStorage->getPropagator();
  172. $sourceInfo = $sourceCache->get($source);
  173. if ($sourceInfo !== false) {
  174. if ($this->cache->inCache($target)) {
  175. $this->cache->remove($target);
  176. }
  177. if ($sourceStorage === $this->storage) {
  178. $this->cache->move($source, $target);
  179. } else {
  180. $this->cache->moveFromCache($sourceCache, $source, $target);
  181. }
  182. $sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
  183. $targetExtension = pathinfo($target, PATHINFO_EXTENSION);
  184. $targetIsTrash = preg_match("/d\d+/", $targetExtension);
  185. if ($sourceExtension !== $targetExtension && $sourceInfo->getMimeType() !== FileInfo::MIMETYPE_FOLDER && !$targetIsTrash) {
  186. // handle mime type change
  187. $mimeType = $this->storage->getMimeType($target);
  188. $fileId = $this->cache->getId($target);
  189. $this->cache->update($fileId, ['mimetype' => $mimeType]);
  190. }
  191. }
  192. if ($sourceCache instanceof Cache) {
  193. $sourceCache->correctFolderSize($source);
  194. }
  195. if ($this->cache instanceof Cache) {
  196. $this->cache->correctFolderSize($target);
  197. }
  198. if ($sourceUpdater instanceof Updater) {
  199. $sourceUpdater->correctParentStorageMtime($source);
  200. }
  201. $this->correctParentStorageMtime($target);
  202. $this->updateStorageMTimeOnly($target);
  203. $sourcePropagator->propagateChange($source, $time);
  204. $this->propagator->propagateChange($target, $time);
  205. }
  206. private function updateStorageMTimeOnly($internalPath) {
  207. $fileId = $this->cache->getId($internalPath);
  208. if ($fileId !== -1) {
  209. $mtime = $this->storage->filemtime($internalPath);
  210. if ($mtime !== false) {
  211. $this->cache->update(
  212. $fileId, [
  213. 'mtime' => null, // this magic tells it to not overwrite mtime
  214. 'storage_mtime' => $mtime
  215. ]
  216. );
  217. }
  218. }
  219. }
  220. /**
  221. * update the storage_mtime of the direct parent in the cache to the mtime from the storage
  222. *
  223. * @param string $internalPath
  224. */
  225. private function correctParentStorageMtime($internalPath) {
  226. $parentId = $this->cache->getParentId($internalPath);
  227. $parent = dirname($internalPath);
  228. if ($parentId != -1) {
  229. $mtime = $this->storage->filemtime($parent);
  230. if ($mtime !== false) {
  231. try {
  232. $this->cache->update($parentId, ['storage_mtime' => $mtime]);
  233. } catch (DeadlockException $e) {
  234. // ignore the failure.
  235. // with failures concurrent updates, someone else would have already done it.
  236. // in the worst case the `storage_mtime` isn't updated, which should at most only trigger an extra rescan
  237. $this->logger->warning("Error while updating parent storage_mtime, should be safe to ignore", ['exception' => $e]);
  238. }
  239. }
  240. }
  241. }
  242. }