updater.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /**
  10. * listen to filesystem hooks and change the cache accordingly
  11. */
  12. class Updater {
  13. /**
  14. * resolve a path to a storage and internal path
  15. *
  16. * @param string $path the relative path
  17. * @return array an array consisting of the storage and the internal path
  18. */
  19. static public function resolvePath($path) {
  20. $view = \OC\Files\Filesystem::getView();
  21. return $view->resolvePath($path);
  22. }
  23. /**
  24. * perform a write update
  25. *
  26. * @param string $path the relative path of the file
  27. */
  28. static public function writeUpdate($path) {
  29. /**
  30. * @var \OC\Files\Storage\Storage $storage
  31. * @var string $internalPath
  32. */
  33. list($storage, $internalPath) = self::resolvePath($path);
  34. if ($storage) {
  35. $cache = $storage->getCache($internalPath);
  36. $scanner = $storage->getScanner($internalPath);
  37. $data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
  38. $cache->correctFolderSize($internalPath, $data);
  39. self::correctFolder($path, $storage->filemtime($internalPath));
  40. self::correctParentStorageMtime($storage, $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. $parent = dirname($internalPath);
  56. if ($parent === '.') {
  57. $parent = '';
  58. }
  59. $cache = $storage->getCache($internalPath);
  60. $cache->remove($internalPath);
  61. $cache->correctFolderSize($parent);
  62. self::correctFolder($path, time());
  63. self::correctParentStorageMtime($storage, $internalPath);
  64. }
  65. }
  66. /**
  67. * preform a rename update
  68. *
  69. * @param string $from the relative path of the source file
  70. * @param string $to the relative path of the target file
  71. */
  72. static public function renameUpdate($from, $to) {
  73. /**
  74. * @var \OC\Files\Storage\Storage $storageFrom
  75. * @var \OC\Files\Storage\Storage $storageTo
  76. * @var string $internalFrom
  77. * @var string $internalTo
  78. */
  79. list($storageFrom, $internalFrom) = self::resolvePath($from);
  80. // if it's a moved mountpoint we dont need to do anything
  81. if ($internalFrom === '') {
  82. return;
  83. }
  84. list($storageTo, $internalTo) = self::resolvePath($to);
  85. if ($storageFrom && $storageTo) {
  86. if ($storageFrom === $storageTo) {
  87. $cache = $storageFrom->getCache($internalFrom);
  88. $cache->move($internalFrom, $internalTo);
  89. if (pathinfo($internalFrom, PATHINFO_EXTENSION) !== pathinfo($internalTo, PATHINFO_EXTENSION)) {
  90. // redetect mime type change
  91. $mimeType = $storageTo->getMimeType($internalTo);
  92. $fileId = $storageTo->getCache()->getId($internalTo);
  93. $storageTo->getCache()->update($fileId, array('mimetype' => $mimeType));
  94. }
  95. $cache->correctFolderSize($internalFrom);
  96. $cache->correctFolderSize($internalTo);
  97. self::correctFolder($from, time());
  98. self::correctFolder($to, time());
  99. self::correctParentStorageMtime($storageFrom, $internalFrom);
  100. self::correctParentStorageMtime($storageTo, $internalTo);
  101. } else {
  102. self::deleteUpdate($from);
  103. self::writeUpdate($to);
  104. }
  105. }
  106. }
  107. /**
  108. * get file owner and path
  109. * @param string $filename
  110. * @return string[] with the oweners uid and the owners path
  111. */
  112. private static function getUidAndFilename($filename) {
  113. $uid = \OC\Files\Filesystem::getOwner($filename);
  114. \OC\Files\Filesystem::initMountPoints($uid);
  115. if ($uid != \OCP\User::getUser()) {
  116. $info = \OC\Files\Filesystem::getFileInfo($filename);
  117. if (!$info) {
  118. return array($uid, '/files/' . $filename);
  119. }
  120. $ownerView = new \OC\Files\View('/' . $uid . '/files');
  121. $filename = $ownerView->getPath($info['fileid']);
  122. }
  123. return array($uid, '/files/' . $filename);
  124. }
  125. /**
  126. * Update the mtime and ETag of all parent folders
  127. *
  128. * @param string $path
  129. * @param string $time
  130. */
  131. static public function correctFolder($path, $time) {
  132. if ($path !== '' && $path !== '/') {
  133. list($owner, $realPath) = self::getUidAndFilename(dirname($path));
  134. /**
  135. * @var \OC\Files\Storage\Storage $storage
  136. * @var string $internalPath
  137. */
  138. $view = new \OC\Files\View('/' . $owner);
  139. list($storage, $internalPath) = $view->resolvePath($realPath);
  140. $cache = $storage->getCache();
  141. $id = $cache->getId($internalPath);
  142. while ($id !== -1) {
  143. $cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
  144. if ($realPath !== '') {
  145. $realPath = dirname($realPath);
  146. if ($realPath === DIRECTORY_SEPARATOR) {
  147. $realPath = "";
  148. }
  149. // check storage for parent in case we change the storage in this step
  150. list($storage, $internalPath) = $view->resolvePath($realPath);
  151. $cache = $storage->getCache();
  152. $id = $cache->getId($internalPath);
  153. } else {
  154. $id = -1;
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * update the storage_mtime of the parent
  161. *
  162. * @param \OC\Files\Storage\Storage $storage
  163. * @param string $internalPath
  164. */
  165. static private function correctParentStorageMtime($storage, $internalPath) {
  166. $cache = $storage->getCache();
  167. $parentId = $cache->getParentId($internalPath);
  168. $parent = dirname($internalPath);
  169. if ($parentId != -1) {
  170. $cache->update($parentId, array('storage_mtime' => $storage->filemtime($parent)));
  171. }
  172. }
  173. /**
  174. * @param array $params
  175. */
  176. static public function writeHook($params) {
  177. self::writeUpdate($params['path']);
  178. }
  179. /**
  180. * @param array $params
  181. */
  182. static public function touchHook($params) {
  183. $path = $params['path'];
  184. list($storage, $internalPath) = self::resolvePath($path);
  185. $cache = $storage->getCache();
  186. $id = $cache->getId($internalPath);
  187. if ($id !== -1) {
  188. $cache->update($id, array('etag' => $storage->getETag($internalPath)));
  189. }
  190. self::writeUpdate($path);
  191. }
  192. /**
  193. * @param array $params
  194. */
  195. static public function renameHook($params) {
  196. self::renameUpdate($params['oldpath'], $params['newpath']);
  197. }
  198. /**
  199. * @param array $params
  200. */
  201. static public function deleteHook($params) {
  202. self::deleteUpdate($params['path']);
  203. }
  204. }