1
0

Updater.php 6.5 KB

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