nextcloud/lib/filecache.php

460 lines
13 KiB
PHP
Raw Normal View History

2011-11-09 21:41:57 +04:00
<?php
/**
* @author Robin Appelman
* @copyright 2011 Robin Appelman icewind1991@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* provide caching for filesystem info in the database
*
* not used by OC_Filesystem for reading filesystem info,
* instread apps should use OC_FileCache::get where possible
*
* It will try to keep the data up to date but changes from outside ownCloud can invalidate the cache
*/
class OC_FileCache{
/**
* get the filesystem info from the cache
* @param string path
* @param string root (optional)
2011-11-09 21:41:57 +04:00
* @return array
*
2012-06-15 19:05:27 +04:00
* returns an associative array with the following keys:
2011-11-09 21:41:57 +04:00
* - size
* - mtime
* - ctime
* - mimetype
* - encrypted
* - versioned
2011-11-09 21:41:57 +04:00
*/
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);
}
}
return OC_FileCache_Cached::get($path,$root);
2011-11-09 21:41:57 +04:00
}
/**
* put filesystem info in the cache
* @param string $path
* @param array data
* @param string root (optional)
2011-11-09 21:41:57 +04:00
*
* $data is an assiciative array in the same format as returned by get
*/
public static function put($path,$data,$root=false){
if($root===false){
$root=OC_Filesystem::getRoot();
}
$path=$root.$path;
$parent=self::getParentId($path);
$id=self::getId($path,'');
if(isset(OC_FileCache_Cached::$savedData[$path])){
$data=array_merge(OC_FileCache_Cached::$savedData[$path],$data);
unset(OC_FileCache_Cached::$savedData[$path]);
}
if($id!=-1){
self::update($id,$data);
return;
}
2012-06-21 20:07:56 +04:00
if(!isset($data['size']) or !isset($data['mtime'])){//save incomplete data for the next time we write it
OC_FileCache_Cached::$savedData[$path]=$data;
return;
}
if(!isset($data['encrypted'])){
$data['encrypted']=false;
}
if(!isset($data['versioned'])){
$data['versioned']=false;
}
2011-11-09 21:41:57 +04:00
$mimePart=dirname($data['mimetype']);
2012-05-04 01:16:18 +04:00
$data['size']=(int)$data['size'];
$data['ctime']=(int)$data['mtime'];
$data['writable']=(int)$data['writable'];
$data['encrypted']=(int)$data['encrypted'];
$data['versioned']=(int)$data['versioned'];
2012-01-31 19:12:49 +04:00
$user=OC_User::getUser();
2012-05-04 01:16:18 +04:00
$query=OC_DB::prepare('INSERT INTO *PREFIX*fscache(parent, name, path, path_hash, size, mtime, ctime, mimetype, mimepart,`user`,writable,encrypted,versioned) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)');
2012-03-30 20:12:33 +04:00
$result=$query->execute(array($parent,basename($path),$path,md5($path),$data['size'],$data['mtime'],$data['ctime'],$data['mimetype'],$mimePart,$user,$data['writable'],$data['encrypted'],$data['versioned']));
if(OC_DB::isError($result)){
OC_Log::write('files','error while writing file('.$path.') to cache',OC_Log::ERROR);
}
2012-07-22 05:24:34 +04:00
if($cache=OC_Cache::getUserCache(true)){
$cache->remove('fileid/'.$path);//ensure we don't have -1 cached
}
2011-11-09 21:41:57 +04:00
}
/**
* update filesystem info of a file
* @param int $id
* @param array $data
*/
private static function update($id,$data){
$arguments=array();
$queryParts=array();
2012-02-05 17:04:10 +04:00
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) $arguments[] = 0;
else $arguments[] = $data[$attribute];
$queryParts[]=$attribute.'=?';
}
}
if(isset($data['mimetype'])){
$arguments[]=dirname($data['mimetype']);
$queryParts[]='mimepart=?';
}
$arguments[]=$id;
$sql = 'UPDATE *PREFIX*fscache SET '.implode(' , ',$queryParts).' WHERE id=?';
$query=OC_DB::prepare($sql);
$result=$query->execute($arguments);
if(OC_DB::isError($result)){
OC_Log::write('files','error while updating file('.$id.') in cache',OC_Log::ERROR);
}
2011-11-09 21:41:57 +04:00
}
/**
* register a file move in the cache
* @param string oldPath
* @param string newPath
* @param string root (optional)
2011-11-09 21:41:57 +04:00
*/
public static function move($oldPath,$newPath,$root=false){
if($root===false){
$root=OC_Filesystem::getRoot();
}
$oldPath=$root.$oldPath;
$newPath=$root.$newPath;
2011-11-09 21:41:57 +04:00
$newParent=self::getParentId($newPath);
2012-03-30 20:12:33 +04:00
$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)));
2012-07-22 05:24:34 +04:00
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$oldPath)){
$cache->set('fileid/'.$newPath,$cache->get('fileid/'.$oldPath));
$cache->remove('fileid/'.$oldPath);
}
$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()){
$old=$row['path'];
$new=$newPath.substr($old,$oldLength);
$updateQuery->execute(array($new,md5($new),md5($old)));
2012-07-22 05:24:34 +04:00
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$old)){
$cache->set('fileid/'.$new,$cache->get('fileid/'.$old));
$cache->remove('fileid/'.$old);
}
}
2011-11-09 21:41:57 +04:00
}
/**
* delete info from the cache
* @param string path
* @param string root (optional)
2011-11-09 21:41:57 +04:00
*/
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=?');
$query->execute(array(md5($root.$path)));
//delete everything inside the folder
$query=OC_DB::prepare('DELETE FROM *PREFIX*fscache WHERE path LIKE ?');
$query->execute(array($root.$path.'/%'));
2012-07-22 05:24:34 +04:00
OC_Cache::remove('fileid/'.$root.$path);
2011-11-09 21:41:57 +04:00
}
/**
* return array of filenames matching the querty
* @param string $query
2012-01-31 19:12:49 +04:00
* @param boolean $returnData
* @param string root (optional)
2011-11-09 21:41:57 +04:00
* @return array of filepaths
*/
public static function search($search,$returnData=false,$root=false){
if($root===false){
$root=OC_Filesystem::getRoot();
}
2012-01-31 19:12:49 +04:00
$rootLen=strlen($root);
if(!$returnData){
2012-05-04 01:16:18 +04:00
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE name LIKE ? AND `user`=?');
2012-01-31 19:12:49 +04:00
}else{
2012-05-04 01:16:18 +04:00
$query=OC_DB::prepare('SELECT * FROM *PREFIX*fscache WHERE name LIKE ? AND `user`=?');
2012-01-31 19:12:49 +04:00
}
$result=$query->execute(array("%$search%",OC_User::getUser()));
2011-11-09 21:41:57 +04:00
$names=array();
while($row=$result->fetchRow()){
2012-01-31 19:12:49 +04:00
if(!$returnData){
$names[]=substr($row['path'],$rootLen);
}else{
$row['path']=substr($row['path'],$rootLen);
$names[]=$row;
}
2011-11-09 21:41:57 +04:00
}
return $names;
}
/**
* get all files and folders in a folder
* @param string path
* @param string root (optional)
2011-11-09 21:41:57 +04:00
* @return array
*
* returns an array of assiciative arrays with the following keys:
* - name
* - size
* - mtime
* - ctime
* - mimetype
* - encrypted
* - versioned
2011-11-09 21:41:57 +04:00
*/
public static function getFolderContent($path,$root=false,$mimetype_filter=''){
if(OC_FileCache_Update::hasUpdated($path,$root,true)){
OC_FileCache_Update::updateFolder($path,$root);
2011-11-09 21:41:57 +04:00
}
return OC_FileCache_Cached::getFolderContent($path,$root,$mimetype_filter);
2011-11-09 21:41:57 +04:00
}
/**
* check if a file or folder is in the cache
* @param string $path
* @param string root (optional)
2011-11-09 21:41:57 +04:00
* @return bool
*/
public static function inCache($path,$root=false){
return self::getId($path,$root)!=-1;
2011-11-09 21:41:57 +04:00
}
/**
* get the file id as used in the cache
* @param string path
* @param string root (optional)
2011-11-09 21:41:57 +04:00
* @return int
*/
public static function getId($path,$root=false){
if($root===false){
$root=OC_Filesystem::getRoot();
}
2012-07-22 05:24:34 +04:00
$fullPath=$root.$path;
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=?');
2012-07-22 05:24:34 +04:00
$result=$query->execute(array(md5($fullPath)));
if(OC_DB::isError($result)){
OC_Log::write('files','error while getting file id of '.$path,OC_Log::ERROR);
return -1;
}
$result=$result->fetchRow();
2011-11-09 21:41:57 +04:00
if(is_array($result)){
2012-07-22 05:24:34 +04:00
$id=$result['id'];
2011-11-09 21:41:57 +04:00
}else{
2012-07-22 05:24:34 +04:00
$id=-1;
}
if($cache=OC_Cache::getUserCache(true)){
$cache->set('fileid/'.$fullPath,$id);
2011-11-09 21:41:57 +04:00
}
2012-07-22 05:24:34 +04:00
return $id;
2011-11-09 21:41:57 +04:00
}
/**
* get the file path from the id, relative to the home folder of the user
* @param int id
* @param string user (optional)
* @return string
*/
public static function getPath($id,$user=''){
if(!$user){
$user=OC_User::getUser();
}
2012-05-04 01:16:18 +04:00
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id=? AND `user`=?');
$result=$query->execute(array($id,$user));
$row=$result->fetchRow();
$path=$row['path'];
$root='/'.$user.'/files';
if(substr($path,0,strlen($root))!=$root){
return false;
}
return substr($path,strlen($root));
}
2011-11-09 21:41:57 +04:00
/**
* get the file id of the parent folder, taking into account '/' has no parent
* @param string $path
* @return int
*/
private static function getParentId($path){
if($path=='/'){
return -1;
}else{
return self::getId(dirname($path),'');
}
}
2011-11-09 21:41:57 +04:00
/**
* adjust the size of the parent folders
* @param string $path
* @param int $sizeDiff
* @param string root (optinal)
2011-11-09 21:41:57 +04:00
*/
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
2011-11-09 21:41:57 +04:00
$query=OC_DB::prepare('UPDATE *PREFIX*fscache SET size=size+? WHERE id=?');
$query->execute(array($sizeDiff,$id));
$id=self::getParentId($path);
2011-11-09 21:41:57 +04:00
$path=dirname($path);
}
}
/**
* recursively scan the filesystem and fill the cache
* @param string $path
* @param OC_EventSource $enventSource (optional)
* @param int count (optional)
* @param string root (optionak)
2011-11-09 21:41:57 +04:00
*/
public static function scan($path,$eventSource=false,&$count=0,$root=false){
if($eventSource){
$eventSource->send('scanning',array('file'=>$path,'count'=>$count));
}
$lastSend=$count;
if($root===false){
$view=OC_Filesystem::getView();
}else{
$view=new OC_FilesystemView($root);
2011-11-09 21:41:57 +04:00
}
self::scanFile($path,$root);
2012-04-29 18:11:17 +04:00
$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));
}
2011-11-09 21:41:57 +04:00
}
}
}
}
OC_FileCache_Update::cleanFolder($path,$root);
self::increaseSize($path,$totalSize,$root);
}
/**
* scan a single file
* @param string path
* @param string root (optional)
* @return int size of the scanned file
*/
public static function scanFile($path,$root=false){
if($root===false){
$view=OC_Filesystem::getView();
}else{
$view=new OC_FilesystemView($root);
}
if(!$view->is_readable($path)) return; //cant read, nothing we can do
2012-04-25 02:10:02 +04:00
clearstatcache();
$mimetype=$view->getMimeType($path);
$stat=$view->stat($path);
if($mimetype=='httpd/unix-directory'){
$writable=$view->is_writable($path.'/');
}else{
$writable=$view->is_writable($path);
}
$stat['mimetype']=$mimetype;
2012-02-05 17:04:10 +04:00
$stat['writable']=$writable;
if($path=='/'){
$path='';
}
self::put($path,$stat,$root);
return $stat['size'];
2011-11-09 21:41:57 +04:00
}
/**
2012-03-15 03:09:06 +04:00
* find files by mimetype
2011-11-09 21:41:57 +04:00
* @param string $part1
* @param string $part2 (optional)
2012-02-08 20:29:54 +04:00
* @param string root (optional)
2011-11-09 21:41:57 +04:00
* @return array of file paths
*
* $part1 and $part2 together form the complete mimetype.
* e.g. searchByMime('text','plain')
*
* seccond mimetype part can be ommited
* e.g. searchByMime('audio')
*/
public static function searchByMime($part1,$part2=null,$root=false){
if($root===false){
2012-02-08 20:29:54 +04:00
$root=OC_Filesystem::getRoot();
}
2012-03-09 19:47:53 +04:00
$rootLen=strlen($root);
$root .= '%';
2012-02-08 20:29:54 +04:00
$user=OC_User::getUser();
if(!$part2){
2012-05-04 01:16:18 +04:00
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE mimepart=? AND `user`=? AND path LIKE ?');
2012-03-09 19:47:53 +04:00
$result=$query->execute(array($part1,$user, $root));
2011-11-09 21:41:57 +04:00
}else{
2012-05-04 01:16:18 +04:00
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE mimetype=? AND `user`=? AND path LIKE ? ');
2012-03-09 19:45:57 +04:00
$result=$query->execute(array($part1.'/'.$part2,$user, $root));
2011-11-09 21:41:57 +04:00
}
$names=array();
while($row=$result->fetchRow()){
2012-02-08 20:29:54 +04:00
$names[]=substr($row['path'],$rootLen);
2011-11-09 21:41:57 +04:00
}
return $names;
}
2012-03-30 21:44:38 +04:00
/**
* clean old pre-path_hash entries
*/
public static function clean(){
$query=OC_DB::prepare('DELETE FROM *PREFIX*fscache WHERE LENGTH(path_hash)<30');
$query->execute();
}
2011-11-09 21:41:57 +04:00
}
//watch for changes and try to keep the cache up to date
OC_Hook::connect('OC_Filesystem','post_write','OC_FileCache_Update','fileSystemWatcherWrite');
OC_Hook::connect('OC_Filesystem','post_delete','OC_FileCache_Update','fileSystemWatcherDelete');
OC_Hook::connect('OC_Filesystem','post_rename','OC_FileCache_Update','fileSystemWatcherRename');