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.

261 lines
6.9 KiB

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