From: Robin Appelman Date: Mon, 18 Apr 2011 10:02:45 +0000 (+0200) Subject: replace filesystem observer with the more general OC_HOOK X-Git-Tag: v3.0~267^2~558^2~64^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b8e1788ed0c3da2cd70b9d2bc50c5273b8a65ff7;p=nextcloud-server.git replace filesystem observer with the more general OC_HOOK --- diff --git a/lib/fileobserver.php b/lib/fileobserver.php deleted file mode 100644 index ac9f2513c7b..00000000000 --- a/lib/fileobserver.php +++ /dev/null @@ -1,82 +0,0 @@ -. -*/ - -define('OC_FILEACTION_WRITE',2); -define('OC_FILEACTION_READ',4); -define('OC_FILEACTION_DELETE',8); -define('OC_FILEACTION_CREATE',16); -define('OC_FILEACTION_RENAME',32); - -/** - * base class for file observers - */ -class OC_FILEOBSERVER{ - private $mask; - - public function __construct($arguments){} - - public function __get($name){ - switch($name){ - case 'mask': - return $this->mask; - } - } - - public function notify($path,$action,$storage){} -} - -/** - * observer that makes automatic backups - */ -class OC_FILEOBSERVER_BACKUP extends OC_FILEOBSERVER{ - private $storage; - - public function __construct($arguments){ - $this->mask=OC_FILEACTION_WRITE+OC_FILEACTION_DELETE+OC_FILEACTION_CREATE+OC_FILEACTION_RENAME; - $this->storage=$arguments['storage']; - } - - public function notify($path,$action,$storage){ - switch($action){ - case OC_FILEACTION_DELETE: - if($storage->is_dir($path)){ - $this->storage->delTree($path); - }else{ - $this->storage->unlink($path); - } - break; - case OC_FILEACTION_CREATE: - if($storage->is_dir($path)){ - $this->storage->mkdir($path); - break; - } - case OC_FILEACTION_WRITE: - $tmpFile=$storage->toTmpFile($path); - $this->storage->fromTmpFile($tmpFile,$path); - break; - case OC_FILEACTION_RENAME: - list($source,$target)=explode('->',$path); - $this->storage->rename($source,$target); - } - } -} -?> \ No newline at end of file diff --git a/lib/filestorage.php b/lib/filestorage.php index e6305a51bac..c20d6d7b3e0 100644 --- a/lib/filestorage.php +++ b/lib/filestorage.php @@ -22,29 +22,15 @@ /** * Privde a common interface to all different storage options + * + * Hooks provided: + * read(path) + * write(path) + * create(path) + * delete(path) + * rename(oldpath,newpath) */ class OC_FILESTORAGE{ - private $observers=array(); - /** - * add an observer to the list - * @param OC_FILEOBERSER observer - */ - public function addObserver($observer){ - $this->observers[]=$observer; - } - /** - * notify the observers about an action - * @param int action a combination of OC_FILEACTION_WRITE and OC_FILEACTION_READ - * @param string path relative path of the file - */ - public function notifyObservers($path,$action){ - foreach($this->observers as $observer){ - if($observer->mask & $action){ - $observer->notify($path,$action,$this); - } - } - } - public function __construct($parameters){} public function mkdir($path){} public function rmdir($path){} @@ -99,14 +85,14 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ } public function rmdir($path){ if($return=rmdir($this->datadir.$path)){ - $this->notifyObservers($path,OC_FILEACTION_DELETE); + OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $path)); $this->clearFolderSizeCache($path); } return $return; } public function opendir($path){ if($return=opendir($this->datadir.$path)){ - $this->notifyObservers($path,OC_FILEACTION_READ); + OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path)); } return $return; } @@ -144,7 +130,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ } public function readfile($path){ if($return=readfile($this->datadir.$path)){ - $this->notifyObservers($path,OC_FILEACTION_READ); + OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path)); } return $return; } @@ -159,26 +145,26 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ } public function file_get_contents($path){ if($return=file_get_contents($this->datadir.$path)){ - $this->notifyObservers($path,OC_FILEACTION_READ); + OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path)); } return $return; } public function file_put_contents($path,$data){ if($return=file_put_contents($this->datadir.$path,$data)){ - $this->notifyObservers($path,OC_FILEACTION_WRITE); + OC_HOOK::emit( "OC_FILESYSTEM", "write", array( 'path' => $path)); $this->clearFolderSizeCache($path); } } public function unlink($path){ if($return=unlink($this->datadir.$path)){ - $this->notifyObservers($path,OC_FILEACTION_DELETE); + OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $path)); $this->clearFolderSizeCache($path); } return $return; } public function rename($path1,$path2){ if($return=rename($this->datadir.$path1,$this->datadir.$path2)){ - $this->notifyObservers($path1.'->'.$path2,OC_FILEACTION_RENAME); + OC_HOOK::emit( "OC_FILESYSTEM", "rename", array( 'oldpath' => $path1, 'newpath' => $path2)); } return $return; } @@ -191,7 +177,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ $path2.=$source; } if($return=copy($this->datadir.$path1,$this->datadir.$path2)){ - $this->notifyObservers($path2,OC_FILEACTION_CREATE); + OC_HOOK::emit( "OC_FILESYSTEM", "create", array( 'path' => $path2)); $this->clearFolderSizeCache($path); } return $return; @@ -200,19 +186,20 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ if($return=fopen($this->datadir.$path,$mode)){ switch($mode){ case 'r': - $this->notifyObservers($path,OC_FILEACTION_READ); + OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path)); break; case 'r+': case 'w+': case 'x+': case 'a+': - $this->notifyObservers($path,OC_FILEACTION_READ | OC_FILEACTION_WRITE); + OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path)); + OC_HOOK::emit( "OC_FILESYSTEM", "write", array( 'path' => $path)); $this->clearFolderSizeCache($path); break; case 'w': case 'x': case 'a': - $this->notifyObservers($path,OC_FILEACTION_WRITE); + OC_HOOK::emit( "OC_FILESYSTEM", "write", array( 'path' => $path)); $this->clearFolderSizeCache($path); break; } @@ -375,7 +362,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ $fileStats = stat($this->datadir.$path); if(copy($this->datadir.$path,$filename)){ touch($filename, $fileStats['mtime'], $fileStats['atime']); - $this->notifyObservers($path,OC_FILEACTION_READ); + OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path)); return $filename; }else{ return false; @@ -386,7 +373,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ $fileStats = stat($tmpFile); if(rename($tmpFile,$this->datadir.$path)){ touch($this->datadir.$path, $fileStats['mtime'], $fileStats['atime']); - $this->notifyObservers($path,OC_FILEACTION_CREATE); + OC_HOOK::emit( "OC_FILESYSTEM", "create", array( 'path' => $path)); $this->clearFolderSizeCache($path); return true; }else{ @@ -398,7 +385,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ $fileStats = stat($tmpFile); if(move_uploaded_file($tmpFile,$this->datadir.$path)){ touch($this->datadir.$path, $fileStats['mtime'], $fileStats['atime']); - $this->notifyObservers($path,OC_FILEACTION_CREATE); + OC_HOOK::emit( "OC_FILESYSTEM", "create", array( 'path' => $path)); $this->clearFolderSizeCache($path); return true; }else{ @@ -415,7 +402,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ if ($item == '.' || $item == '..') continue; if(is_file($dir.'/'.$item)){ if(unlink($dir.'/'.$item)){ - $this->notifyObservers($dir.'/'.$item,OC_FILEACTION_DELETE); + OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $dir.'/'.$item)); $this->clearFolderSizeCache($path); } }elseif(is_dir($dir.'/'.$item)){ @@ -425,7 +412,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ } } if($return=rmdir($dir)){ - $this->notifyObservers($dir,OC_FILEACTION_DELETE); + OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $dir)); $this->clearFolderSizeCache($path); } return $return; @@ -463,7 +450,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ public function hash($type,$path,$raw){ if($return=hash_file($type,$this->datadir.$path,$raw)){ - $this->notifyObservers($path,OC_FILEACTION_READ); + OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path)); } return $return; }