updater.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Cache;
  9. use OCP\Util;
  10. /**
  11. * listen to filesystem hooks and change the cache accordingly
  12. */
  13. class Updater {
  14. /**
  15. * resolve a path to a storage and internal path
  16. *
  17. * @param string $path the relative path
  18. * @return array consisting of the storage and the internal path
  19. */
  20. static public function resolvePath($path) {
  21. $view = \OC\Files\Filesystem::getView();
  22. return $view->resolvePath($path);
  23. }
  24. /**
  25. * perform a write update
  26. *
  27. * @param string $path the relative path of the file
  28. */
  29. static public function writeUpdate($path) {
  30. /**
  31. * @var \OC\Files\Storage\Storage $storage
  32. * @var string $internalPath
  33. */
  34. list($storage, $internalPath) = self::resolvePath($path);
  35. if ($storage) {
  36. $cache = $storage->getCache($internalPath);
  37. $scanner = $storage->getScanner($internalPath);
  38. $scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
  39. $cache->correctFolderSize($internalPath);
  40. self::correctFolder($path, $storage->filemtime($internalPath));
  41. }
  42. }
  43. /**
  44. * perform a delete update
  45. *
  46. * @param string $path the relative path of the file
  47. */
  48. static public function deleteUpdate($path) {
  49. /**
  50. * @var \OC\Files\Storage\Storage $storage
  51. * @var string $internalPath
  52. */
  53. list($storage, $internalPath) = self::resolvePath($path);
  54. if ($storage) {
  55. $cache = $storage->getCache($internalPath);
  56. $cache->remove($internalPath);
  57. $cache->correctFolderSize($internalPath);
  58. self::correctFolder($path, time());
  59. }
  60. }
  61. /**
  62. * preform a rename update
  63. *
  64. * @param string $from the relative path of the source file
  65. * @param string $to the relative path of the target file
  66. */
  67. static public function renameUpdate($from, $to) {
  68. /**
  69. * @var \OC\Files\Storage\Storage $storageFrom
  70. * @var \OC\Files\Storage\Storage $storageTo
  71. * @var string $internalFrom
  72. * @var string $internalTo
  73. */
  74. list($storageFrom, $internalFrom) = self::resolvePath($from);
  75. list($storageTo, $internalTo) = self::resolvePath($to);
  76. if ($storageFrom && $storageTo) {
  77. if ($storageFrom === $storageTo) {
  78. $cache = $storageFrom->getCache($internalFrom);
  79. $cache->move($internalFrom, $internalTo);
  80. $cache->correctFolderSize($internalFrom);
  81. $cache->correctFolderSize($internalTo);
  82. self::correctFolder($from, time());
  83. self::correctFolder($to, time());
  84. } else {
  85. self::deleteUpdate($from);
  86. self::writeUpdate($to);
  87. }
  88. }
  89. }
  90. /**
  91. * Update the mtime and ETag of all parent folders
  92. *
  93. * @param string $path
  94. * @param string $time
  95. */
  96. static public function correctFolder($path, $time) {
  97. if ($path !== '' && $path !== '/') {
  98. $parent = dirname($path);
  99. if ($parent === '.' || $parent === '\\') {
  100. $parent = '';
  101. }
  102. /**
  103. * @var \OC\Files\Storage\Storage $storage
  104. * @var string $internalPath
  105. */
  106. list($storage, $internalPath) = self::resolvePath($parent);
  107. if ($storage) {
  108. $cache = $storage->getCache();
  109. $id = $cache->getId($internalPath);
  110. if ($id !== -1) {
  111. $cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
  112. self::correctFolder($parent, $time);
  113. } else {
  114. Util::writeLog('core', 'Path not in cache: '.$internalPath, Util::ERROR);
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * @param array $params
  121. */
  122. static public function writeHook($params) {
  123. self::writeUpdate($params['path']);
  124. }
  125. /**
  126. * @param array $params
  127. */
  128. static public function touchHook($params) {
  129. $path = $params['path'];
  130. list($storage, $internalPath) = self::resolvePath($path);
  131. $cache = $storage->getCache();
  132. $id = $cache->getId($internalPath);
  133. if ($id !== -1) {
  134. $cache->update($id, array('etag' => $storage->getETag($internalPath)));
  135. }
  136. self::writeUpdate($path);
  137. }
  138. /**
  139. * @param array $params
  140. */
  141. static public function renameHook($params) {
  142. self::renameUpdate($params['oldpath'], $params['newpath']);
  143. }
  144. /**
  145. * @param array $params
  146. */
  147. static public function deleteHook($params) {
  148. self::deleteUpdate($params['path']);
  149. }
  150. }