updater.php 6.3 KB

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