From 4d3ef9a82448da91335059c8c0ae14e46ecbd18a Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Sun, 29 Apr 2012 16:11:17 +0200
Subject: improve rescanning folders

---
 lib/filecache.php | 60 ++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 35 insertions(+), 25 deletions(-)

(limited to 'lib/filecache.php')

diff --git a/lib/filecache.php b/lib/filecache.php
index 431470696a6..fce9785f745 100644
--- a/lib/filecache.php
+++ b/lib/filecache.php
@@ -522,30 +522,27 @@ class OC_FileCache{
 			$view=new OC_FilesystemView(($root=='/')?'':$root);
 		}
 		self::scanFile($path,$root);
-		if(self::inCache($path)){
-			self::updateFolder($path,$root);
-		}else{
-			$dh=$view->opendir($path.'/');
-			$totalSize=0;
-			if($dh){
-				while (($filename = readdir($dh)) !== false) {
-					if($filename != '.' and $filename != '..'){
-						$file=$path.'/'.$filename;
-						if($view->is_dir($file.'/')){
-							self::scan($file,$eventSource,$count,$root);
-						}else{
-							$totalSize+=self::scanFile($file,$root);
-							$count++;
-							if($count>$lastSend+25 and $eventSource){
-								$lastSend=$count;
-								$eventSource->send('scanning',array('file'=>$path,'count'=>$count));
-							}
+		$dh=$view->opendir($path.'/');
+		$totalSize=0;
+		if($dh){
+			while (($filename = readdir($dh)) !== false) {
+				if($filename != '.' and $filename != '..'){
+					$file=$path.'/'.$filename;
+					if($view->is_dir($file.'/')){
+						self::scan($file,$eventSource,$count,$root);
+					}else{
+						$totalSize+=self::scanFile($file,$root);
+						$count++;
+						if($count>$lastSend+25 and $eventSource){
+							$lastSend=$count;
+							$eventSource->send('scanning',array('file'=>$path,'count'=>$count));
 						}
 					}
 				}
 			}
-			self::increaseSize($view->getRoot().$path,$totalSize);
 		}
+		self::cleanFolder($path,$root);
+		self::increaseSize($view->getRoot().$path,$totalSize);
 	}
 
 	/**
@@ -668,7 +665,26 @@ class OC_FileCache{
 				}
 			}
 		}
+		
+		self::cleanFolder($path,$root);
+		
+		//update the folder last, so we can calculate the size correctly
+		if(!$root){//filesystem hooks are only valid for the default root
+			OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$path));
+		}else{
+			self::fileSystemWatcherWrite(array('path'=>$path),$root);
+		}
+	}
 
+	/**
+	 * delete non existing files from the cache
+	 */
+	private static function cleanFolder($path,$root=''){
+		if(!$root){
+			$view=OC_Filesystem::getView();
+		}else{
+			$view=new OC_FilesystemView(($root=='/')?'':$root);
+		}
 		//check for removed files, not using getFolderContent to prevent loops
 		$parent=self::getFileId($view->getRoot().$path);
 		$query=OC_DB::prepare('SELECT name FROM *PREFIX*fscache WHERE parent=?');
@@ -683,12 +699,6 @@ class OC_FileCache{
 				}
 			}
 		}
-		//update the folder last, so we can calculate the size correctly
-		if(!$root){//filesystem hooks are only valid for the default root
-			OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$path));
-		}else{
-			self::fileSystemWatcherWrite(array('path'=>$path),$root);
-		}
 	}
 
 	/**
-- 
cgit v1.2.3