diff options
Diffstat (limited to 'lib/filecache.php')
-rw-r--r-- | lib/filecache.php | 142 |
1 files changed, 80 insertions, 62 deletions
diff --git a/lib/filecache.php b/lib/filecache.php index 364b908bcfa..adcf97753ed 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -42,9 +42,9 @@ class OC_FileCache{ * - encrypted * - versioned */ - public static function get($path,$root=false){ - if(OC_FileCache_Update::hasUpdated($path,$root)){ - if($root===false){//filesystem hooks are only valid for the default root + public static function get($path,$root=false) { + if(OC_FileCache_Update::hasUpdated($path,$root)) { + if($root===false) {//filesystem hooks are only valid for the default root OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$path)); }else{ OC_FileCache_Update::update($path,$root); @@ -61,18 +61,18 @@ class OC_FileCache{ * * $data is an assiciative array in the same format as returned by get */ - public static function put($path,$data,$root=false){ - if($root===false){ + public static function put($path,$data,$root=false) { + if($root===false) { $root=OC_Filesystem::getRoot(); } $fullpath=$root.$path; $parent=self::getParentId($fullpath); $id=self::getId($fullpath,''); - if(isset(OC_FileCache_Cached::$savedData[$fullpath])){ + if(isset(OC_FileCache_Cached::$savedData[$fullpath])) { $data=array_merge(OC_FileCache_Cached::$savedData[$fullpath],$data); unset(OC_FileCache_Cached::$savedData[$fullpath]); } - if($id!=-1){ + if($id!=-1) { self::update($id,$data); return; } @@ -84,14 +84,14 @@ class OC_FileCache{ $parent = self::getParentId($fullpath); } - if(!isset($data['size']) or !isset($data['mtime'])){//save incomplete data for the next time we write it + if(!isset($data['size']) or !isset($data['mtime'])) {//save incomplete data for the next time we write it OC_FileCache_Cached::$savedData[$fullpath]=$data; return; } - if(!isset($data['encrypted'])){ + if(!isset($data['encrypted'])) { $data['encrypted']=false; } - if(!isset($data['versioned'])){ + if(!isset($data['versioned'])) { $data['versioned']=false; } $mimePart=dirname($data['mimetype']); @@ -103,11 +103,11 @@ class OC_FileCache{ $user=OC_User::getUser(); $query=OC_DB::prepare('INSERT INTO `*PREFIX*fscache`(`parent`, `name`, `path`, `path_hash`, `size`, `mtime`, `ctime`, `mimetype`, `mimepart`,`user`,`writable`,`encrypted`,`versioned`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'); $result=$query->execute(array($parent,basename($fullpath),$fullpath,md5($fullpath),$data['size'],$data['mtime'],$data['ctime'],$data['mimetype'],$mimePart,$user,$data['writable'],$data['encrypted'],$data['versioned'])); - if(OC_DB::isError($result)){ + if(OC_DB::isError($result)) { OC_Log::write('files','error while writing file('.$fullpath.') to cache',OC_Log::ERROR); } - if($cache=OC_Cache::getUserCache(true)){ + if($cache=OC_Cache::getUserCache(true)) { $cache->remove('fileid/'.$fullpath);//ensure we don't have -1 cached } } @@ -117,13 +117,13 @@ class OC_FileCache{ * @param int $id * @param array $data */ - private static function update($id,$data){ + private static function update($id,$data) { $arguments=array(); $queryParts=array(); - foreach(array('size','mtime','ctime','mimetype','encrypted','versioned','writable') as $attribute){ - if(isset($data[$attribute])){ + foreach(array('size','mtime','ctime','mimetype','encrypted','versioned','writable') as $attribute) { + if(isset($data[$attribute])) { //Convert to int it args are false - if($data[$attribute] === false){ + if($data[$attribute] === false) { $arguments[] = 0; }else{ $arguments[] = $data[$attribute]; @@ -131,7 +131,7 @@ class OC_FileCache{ $queryParts[]='`'.$attribute.'`=?'; } } - if(isset($data['mimetype'])){ + if(isset($data['mimetype'])) { $arguments[]=dirname($data['mimetype']); $queryParts[]='`mimepart`=?'; } @@ -140,7 +140,7 @@ class OC_FileCache{ $sql = 'UPDATE `*PREFIX*fscache` SET '.implode(' , ',$queryParts).' WHERE `id`=?'; $query=OC_DB::prepare($sql); $result=$query->execute($arguments); - if(OC_DB::isError($result)){ + if(OC_DB::isError($result)) { OC_Log::write('files','error while updating file('.$id.') in cache',OC_Log::ERROR); } } @@ -151,17 +151,21 @@ class OC_FileCache{ * @param string newPath * @param string root (optional) */ - public static function move($oldPath,$newPath,$root=false){ - if($root===false){ + public static function move($oldPath,$newPath,$root=false) { + if($root===false) { $root=OC_Filesystem::getRoot(); } + // If replacing an existing file, delete the file + if (self::inCache($newPath, $root)) { + self::delete($newPath, $root); + } $oldPath=$root.$oldPath; $newPath=$root.$newPath; $newParent=self::getParentId($newPath); $query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `parent`=? ,`name`=?, `path`=?, `path_hash`=? WHERE `path_hash`=?'); $query->execute(array($newParent,basename($newPath),$newPath,md5($newPath),md5($oldPath))); - if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$oldPath)){ + if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$oldPath)) { $cache->set('fileid/'.$newPath,$cache->get('fileid/'.$oldPath)); $cache->remove('fileid/'.$oldPath); } @@ -169,12 +173,12 @@ class OC_FileCache{ $query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `path` LIKE ?'); $oldLength=strlen($oldPath); $updateQuery=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `path`=?, `path_hash`=? WHERE `path_hash`=?'); - while($row= $query->execute(array($oldPath.'/%'))->fetchRow()){ + while($row= $query->execute(array($oldPath.'/%'))->fetchRow()) { $old=$row['path']; $new=$newPath.substr($old,$oldLength); $updateQuery->execute(array($new,md5($new),md5($old))); - if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$old)){ + if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$old)) { $cache->set('fileid/'.$new,$cache->get('fileid/'.$old)); $cache->remove('fileid/'.$old); } @@ -186,8 +190,8 @@ class OC_FileCache{ * @param string path * @param string root (optional) */ - public static function delete($path,$root=false){ - if($root===false){ + public static function delete($path,$root=false) { + if($root===false) { $root=OC_Filesystem::getRoot(); } $query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path_hash`=?'); @@ -207,20 +211,20 @@ class OC_FileCache{ * @param string root (optional) * @return array of filepaths */ - public static function search($search,$returnData=false,$root=false){ - if($root===false){ + public static function search($search,$returnData=false,$root=false) { + if($root===false) { $root=OC_Filesystem::getRoot(); } $rootLen=strlen($root); - if(!$returnData){ + if(!$returnData) { $query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `name` LIKE ? AND `user`=?'); }else{ $query=OC_DB::prepare('SELECT * FROM `*PREFIX*fscache` WHERE `name` LIKE ? AND `user`=?'); } $result=$query->execute(array("%$search%",OC_User::getUser())); $names=array(); - while($row=$result->fetchRow()){ - if(!$returnData){ + while($row=$result->fetchRow()) { + if(!$returnData) { $names[]=substr($row['path'],$rootLen); }else{ $row['path']=substr($row['path'],$rootLen); @@ -245,8 +249,8 @@ class OC_FileCache{ * - encrypted * - versioned */ - public static function getFolderContent($path,$root=false,$mimetype_filter=''){ - if(OC_FileCache_Update::hasUpdated($path,$root,true)){ + public static function getFolderContent($path,$root=false,$mimetype_filter='') { + if(OC_FileCache_Update::hasUpdated($path,$root,true)) { OC_FileCache_Update::updateFolder($path,$root); } return OC_FileCache_Cached::getFolderContent($path,$root,$mimetype_filter); @@ -258,7 +262,7 @@ class OC_FileCache{ * @param string root (optional) * @return bool */ - public static function inCache($path,$root=false){ + public static function inCache($path,$root=false) { return self::getId($path,$root)!=-1; } @@ -268,30 +272,30 @@ class OC_FileCache{ * @param string root (optional) * @return int */ - public static function getId($path,$root=false){ - if($root===false){ + public static function getId($path,$root=false) { + if($root===false) { $root=OC_Filesystem::getRoot(); } $fullPath=$root.$path; - if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$fullPath)){ + if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$fullPath)) { return $cache->get('fileid/'.$fullPath); } $query=OC_DB::prepare('SELECT `id` FROM `*PREFIX*fscache` WHERE `path_hash`=?'); $result=$query->execute(array(md5($fullPath))); - if(OC_DB::isError($result)){ + if(OC_DB::isError($result)) { OC_Log::write('files','error while getting file id of '.$path,OC_Log::ERROR); return -1; } $result=$result->fetchRow(); - if(is_array($result)){ + if(is_array($result)) { $id=$result['id']; }else{ $id=-1; } - if($cache=OC_Cache::getUserCache(true)){ + if($cache=OC_Cache::getUserCache(true)) { $cache->set('fileid/'.$fullPath,$id); } @@ -304,8 +308,8 @@ class OC_FileCache{ * @param string user (optional) * @return string */ - public static function getPath($id,$user=''){ - if(!$user){ + public static function getPath($id,$user='') { + if(!$user) { $user=OC_User::getUser(); } $query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `id`=? AND `user`=?'); @@ -313,7 +317,7 @@ class OC_FileCache{ $row=$result->fetchRow(); $path=$row['path']; $root='/'.$user.'/files'; - if(substr($path,0,strlen($root))!=$root){ + if(substr($path,0,strlen($root))!=$root) { return false; } return substr($path,strlen($root)); @@ -324,8 +328,8 @@ class OC_FileCache{ * @param string $path * @return int */ - private static function getParentId($path){ - if($path=='/'){ + private static function getParentId($path) { + if($path=='/') { return -1; }else{ return self::getId(dirname($path),''); @@ -338,10 +342,10 @@ class OC_FileCache{ * @param int $sizeDiff * @param string root (optinal) */ - public static function increaseSize($path,$sizeDiff, $root=false){ + public static function increaseSize($path,$sizeDiff, $root=false) { if($sizeDiff==0) return; $id=self::getId($path,$root); - while($id!=-1){//walk up the filetree increasing the size of all parent folders + while($id!=-1) {//walk up the filetree increasing the size of all parent folders $query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `size`=`size`+? WHERE `id`=?'); $query->execute(array($sizeDiff,$id)); $id=self::getParentId($path); @@ -356,8 +360,8 @@ class OC_FileCache{ * @param int count (optional) * @param string root (optional) */ - public static function scan($path,$eventSource=false,&$count=0,$root=false){ - if($eventSource){ + public static function scan($path,$eventSource=false,&$count=0,$root=false) { + if($eventSource) { $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); } $lastSend=$count; @@ -365,7 +369,7 @@ class OC_FileCache{ if (substr($path, 0, 7) == '/Shared') { return; } - if($root===false){ + if($root===false) { $view=OC_Filesystem::getView(); }else{ $view=new OC_FilesystemView($root); @@ -373,16 +377,16 @@ class OC_FileCache{ self::scanFile($path,$root); $dh=$view->opendir($path.'/'); $totalSize=0; - if($dh){ + if($dh) { while (($filename = readdir($dh)) !== false) { - if($filename != '.' and $filename != '..'){ + if($filename != '.' and $filename != '..') { $file=$path.'/'.$filename; - if($view->is_dir($file.'/')){ + if($view->is_dir($file.'/')) { self::scan($file,$eventSource,$count,$root); }else{ $totalSize+=self::scanFile($file,$root); $count++; - if($count>$lastSend+25 and $eventSource){ + if($count>$lastSend+25 and $eventSource) { $lastSend=$count; $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); } @@ -401,12 +405,12 @@ class OC_FileCache{ * @param string root (optional) * @return int size of the scanned file */ - public static function scanFile($path,$root=false){ + public static function scanFile($path,$root=false) { // NOTE: Ugly hack to prevent shared files from going into the cache (the source already exists somewhere in the cache) if (substr($path, 0, 7) == '/Shared') { return; } - if($root===false){ + if($root===false) { $view=OC_Filesystem::getView(); }else{ $view=new OC_FilesystemView($root); @@ -415,14 +419,14 @@ class OC_FileCache{ clearstatcache(); $mimetype=$view->getMimeType($path); $stat=$view->stat($path); - if($mimetype=='httpd/unix-directory'){ + if($mimetype=='httpd/unix-directory') { $writable=$view->is_writable($path.'/'); }else{ $writable=$view->is_writable($path); } $stat['mimetype']=$mimetype; $stat['writable']=$writable; - if($path=='/'){ + if($path=='/') { $path=''; } self::put($path,$stat,$root); @@ -442,14 +446,14 @@ class OC_FileCache{ * seccond mimetype part can be ommited * e.g. searchByMime('audio') */ - public static function searchByMime($part1,$part2=null,$root=false){ - if($root===false){ + public static function searchByMime($part1,$part2=null,$root=false) { + if($root===false) { $root=OC_Filesystem::getRoot(); } $rootLen=strlen($root); $root .= '%'; $user=OC_User::getUser(); - if(!$part2){ + if(!$part2) { $query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `mimepart`=? AND `user`=? AND `path` LIKE ?'); $result=$query->execute(array($part1,$user, $root)); }else{ @@ -457,7 +461,7 @@ class OC_FileCache{ $result=$query->execute(array($part1.'/'.$part2,$user, $root)); } $names=array(); - while($row=$result->fetchRow()){ + while($row=$result->fetchRow()) { $names[]=substr($row['path'],$rootLen); } return $names; @@ -466,10 +470,24 @@ class OC_FileCache{ /** * clean old pre-path_hash entries */ - public static function clean(){ + public static function clean() { $query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE LENGTH(`path_hash`)<30'); $query->execute(); } + + /** + * clear filecache entries + * @param string user (optonal) + */ + public static function clear($user='') { + if($user) { + $query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE user=?'); + $query->execute(array($user)); + }else{ + $query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache`'); + $query->execute(); + } + } } //watch for changes and try to keep the cache up to date |