update.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /**
  9. * handles updating the filecache according to outside changes
  10. */
  11. class OC_FileCache_Update{
  12. /**
  13. * check if a file or folder is updated outside owncloud
  14. * @param string path
  15. * @param string root (optional)
  16. * @param boolean folder
  17. * @return bool
  18. */
  19. public static function hasUpdated($path,$root=false,$folder=false) {
  20. if($root===false) {
  21. $view=OC_Filesystem::getView();
  22. }else{
  23. $view=new OC_FilesystemView($root);
  24. }
  25. if(!$view->file_exists($path)) {
  26. return false;
  27. }
  28. $cachedData=OC_FileCache_Cached::get($path, $root);
  29. if(isset($cachedData['mtime'])) {
  30. $cachedMTime=$cachedData['mtime'];
  31. if($folder) {
  32. return $view->hasUpdated($path.'/', $cachedMTime);
  33. }else{
  34. return $view->hasUpdated($path, $cachedMTime);
  35. }
  36. }else{//file not in cache, so it has to be updated
  37. if(($path=='/' or $path=='') and $root===false) {//dont auto update the home folder, it will be scanned
  38. return false;
  39. }
  40. return true;
  41. }
  42. }
  43. /**
  44. * delete non existing files from the cache
  45. */
  46. public static function cleanFolder($path,$root=false) {
  47. if($root===false) {
  48. $view=OC_Filesystem::getView();
  49. }else{
  50. $view=new OC_FilesystemView($root);
  51. }
  52. $cachedContent=OC_FileCache_Cached::getFolderContent($path,$root);
  53. foreach($cachedContent as $fileData) {
  54. $path=$fileData['path'];
  55. $file=$view->getRelativePath($path);
  56. if(!$view->file_exists($file)) {
  57. if($root===false) {//filesystem hooks are only valid for the default root
  58. OC_Hook::emit('OC_Filesystem', 'post_delete', array('path'=>$file));
  59. }else{
  60. self::delete($file, $root);
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * update the cache according to changes in the folder
  67. * @param string path
  68. * @param string root (optional)
  69. */
  70. public static function updateFolder($path,$root=false) {
  71. if($root===false) {
  72. $view=OC_Filesystem::getView();
  73. }else{
  74. $view=new OC_FilesystemView($root);
  75. }
  76. $dh=$view->opendir($path.'/');
  77. if($dh) {//check for changed/new files
  78. while (($filename = readdir($dh)) !== false) {
  79. if($filename != '.' and $filename != '..' and $filename != '') {
  80. $file=$path.'/'.$filename;
  81. $isDir=$view->is_dir($file);
  82. if(self::hasUpdated($file, $root, $isDir)) {
  83. if($isDir){
  84. self::updateFolder($file, $root);
  85. }elseif($root===false) {//filesystem hooks are only valid for the default root
  86. OC_Hook::emit('OC_Filesystem', 'post_write', array('path'=>$file));
  87. }else{
  88. self::update($file, $root);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. self::cleanFolder($path, $root);
  95. //update the folder last, so we can calculate the size correctly
  96. if($root===false) {//filesystem hooks are only valid for the default root
  97. OC_Hook::emit('OC_Filesystem', 'post_write', array('path'=>$path));
  98. }else{
  99. self::update($path, $root);
  100. }
  101. }
  102. /**
  103. * called when changes are made to files
  104. * @param array $params
  105. * @param string root (optional)
  106. */
  107. public static function fileSystemWatcherWrite($params) {
  108. $path=$params['path'];
  109. self::update($path);
  110. }
  111. /**
  112. * called when files are deleted
  113. * @param array $params
  114. * @param string root (optional)
  115. */
  116. public static function fileSystemWatcherDelete($params) {
  117. $path=$params['path'];
  118. self::delete($path);
  119. }
  120. /**
  121. * called when files are deleted
  122. * @param array $params
  123. * @param string root (optional)
  124. */
  125. public static function fileSystemWatcherRename($params) {
  126. $oldPath=$params['oldpath'];
  127. $newPath=$params['newpath'];
  128. self::rename($oldPath, $newPath);
  129. }
  130. /**
  131. * update the filecache according to changes to the filesystem
  132. * @param string path
  133. * @param string root (optional)
  134. */
  135. public static function update($path,$root=false) {
  136. if($root===false) {
  137. $view=OC_Filesystem::getView();
  138. }else{
  139. $view=new OC_FilesystemView($root);
  140. }
  141. $mimetype=$view->getMimeType($path);
  142. $size=0;
  143. $cached=OC_FileCache_Cached::get($path,$root);
  144. $cachedSize=isset($cached['size'])?$cached['size']:0;
  145. if($view->is_dir($path.'/')) {
  146. if(OC_FileCache::inCache($path, $root)) {
  147. $cachedContent=OC_FileCache_Cached::getFolderContent($path, $root);
  148. foreach($cachedContent as $file) {
  149. $size+=$file['size'];
  150. }
  151. $mtime=$view->filemtime($path.'/');
  152. $ctime=$view->filectime($path.'/');
  153. $writable=$view->is_writable($path.'/');
  154. OC_FileCache::put($path, array('size'=>$size,'mtime'=>$mtime,'ctime'=>$ctime,'mimetype'=>$mimetype,'writable'=>$writable));
  155. }else{
  156. $count=0;
  157. OC_FileCache::scan($path, null, $count, $root);
  158. return; //increaseSize is already called inside scan
  159. }
  160. }else{
  161. $size=OC_FileCache::scanFile($path, $root);
  162. }
  163. OC_FileCache::increaseSize(dirname($path), $size-$cachedSize, $root);
  164. }
  165. /**
  166. * update the filesystem after a delete has been detected
  167. * @param string path
  168. * @param string root (optional)
  169. */
  170. public static function delete($path,$root=false) {
  171. $cached=OC_FileCache_Cached::get($path, $root);
  172. if(!isset($cached['size'])) {
  173. return;
  174. }
  175. $size=$cached['size'];
  176. OC_FileCache::increaseSize(dirname($path), -$size, $root);
  177. OC_FileCache::delete($path, $root);
  178. }
  179. /**
  180. * update the filesystem after a rename has been detected
  181. * @param string oldPath
  182. * @param string newPath
  183. * @param string root (optional)
  184. */
  185. public static function rename($oldPath,$newPath,$root=false) {
  186. if(!OC_FileCache::inCache($oldPath, $root)) {
  187. return;
  188. }
  189. if($root===false) {
  190. $view=OC_Filesystem::getView();
  191. }else{
  192. $view=new OC_FilesystemView($root);
  193. }
  194. $cached=OC_FileCache_Cached::get($oldPath, $root);
  195. $oldSize=$cached['size'];
  196. OC_FileCache::increaseSize(dirname($oldPath), -$oldSize, $root);
  197. OC_FileCache::increaseSize(dirname($newPath), $oldSize, $root);
  198. OC_FileCache::move($oldPath, $newPath);
  199. }
  200. }