summaryrefslogtreecommitdiffstats
path: root/lib/filesystem.php
diff options
context:
space:
mode:
authorMichael Gapczynski <GapczynskiM@gmail.com>2011-07-04 16:36:30 -0400
committerMichael Gapczynski <GapczynskiM@gmail.com>2011-07-04 16:36:30 -0400
commit05389827fd8bc70b1092dc2bae1ed0335536cf4c (patch)
tree84dea83406b3b5d5cc0cf270a4e95875f3956300 /lib/filesystem.php
parente24e2d0e16a360753cc0430f2ba75f903c669b14 (diff)
parent53ae56097de2098a2ffd1a8dd1076825bc93305d (diff)
downloadnextcloud-server-05389827fd8bc70b1092dc2bae1ed0335536cf4c.tar.gz
nextcloud-server-05389827fd8bc70b1092dc2bae1ed0335536cf4c.zip
Merge branch 'master' into sharing
Conflicts: .gitignore lib/base.php
Diffstat (limited to 'lib/filesystem.php')
-rw-r--r--lib/filesystem.php231
1 files changed, 173 insertions, 58 deletions
diff --git a/lib/filesystem.php b/lib/filesystem.php
index a13363f832d..4116cb93164 100644
--- a/lib/filesystem.php
+++ b/lib/filesystem.php
@@ -29,10 +29,18 @@
*
* Hooks provided:
* read(path)
- * write(path)
- * create(path) (when a file is created both, write and create will be emited)
- * delete(path)
- * rename(oldpath,newpath)
+ * write(path, &run)
+ * post_write(path)
+ * create(path, &run) (when a file is created, both create and write will be emited in that order)
+ * post_create(path)
+ * delete(path, &run)
+ * post_delete(path)
+ * rename(oldpath,newpath, &run)
+ * post_rename(oldpath,newpath)
+ * copy(oldpath,newpath, &run) (if the newpath doesn't exists yes, copy, create and write will be emited in that order)
+ * post_rename(oldpath,newpath)
+ *
+ * the &run parameter can be set to false to prevent the operation from occuring
*/
class OC_FILESYSTEM{
static private $storages=array();
@@ -68,6 +76,16 @@ class OC_FILESYSTEM{
}
/**
+ * tear down the filesystem, removing all storage providers
+ */
+ static public function tearDown(){
+ foreach(self::$storages as $mountpoint=>$storage){
+ unset(self::$storages[$mountpoint]);
+ }
+ $fakeRoot='';
+ }
+
+ /**
* create a new storage of a specific type
* @param string type
* @param array arguments
@@ -91,8 +109,10 @@ class OC_FILESYSTEM{
* @return bool
*/
static public function chroot($fakeRoot){
- if($fakeRoot[0]!=='/'){
- $fakeRoot='/'.$fakeRoot;
+ if(!$fakeRoot==''){
+ if($fakeRoot[0]!=='/'){
+ $fakeRoot='/'.$fakeRoot;
+ }
}
self::$fakeRoot=$fakeRoot;
}
@@ -210,14 +230,28 @@ class OC_FILESYSTEM{
static public function mkdir($path){
$parent=substr($path,0,strrpos($path,'/'));
if(self::canWrite($parent) and $storage=self::getStorage($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
- return $storage->mkdir(self::getInternalPath($path));
+ $run=true;
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path, 'run' => &$run));
+ if($run){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path, 'run' => &$run));
+ }
+ if($run){
+ $result=$storage->mkdir(self::getInternalPath($path));
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_create', array( 'path' => $path));
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_write', array( 'path' => $path));
+ return $result;
+ }
}
}
static public function rmdir($path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path));
- return $storage->rmdir(self::getInternalPath($path));
+ $run=true;
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path, 'run' => &$run));
+ if($run){
+ $result=$storage->rmdir(self::getInternalPath($path));
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_delete', array( 'path' => $path));
+ return $result;
+ }
}
}
static public function opendir($path){
@@ -307,55 +341,94 @@ class OC_FILESYSTEM{
}
static public function file_put_contents($path,$data){
if(self::canWrite($path) and $storage=self::getStorage($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
- if(!self::file_exists($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
+ $run=true;
+ $exists=self::file_exists($path);
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path, 'run' => &$run));
+ }
+ if($run){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path, 'run' => &$run));
+ }
+ if($run){
+ $result=$storage->file_put_contents(self::getInternalPath($path),$data);
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_create', array( 'path' => $path));
+ }
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_write', array( 'path' => $path));
+ return $result;
}
- return $storage->file_put_contents(self::getInternalPath($path),$data);
}
}
static public function unlink($path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path));
- return $storage->unlink(self::getInternalPath($path));
+ $run=true;
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path, 'run' => &$run));
+ if($run){
+ $result=$storage->unlink(self::getInternalPath($path));
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_delete', array( 'path' => $path));
+ return $result;
+ }
}
}
static public function rename($path1,$path2){
if(self::canWrite($path1) and self::canWrite($path2)){
- $mp1=self::getMountPoint($path1);
- $mp2=self::getMountPoint($path2);
- if($mp1==$mp2){
- if($storage=self::getStorage($path1)){
- return $storage->rename(self::getInternalPath($path1),self::getInternalPath($path2));
+ $run=true;
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'rename', array( 'oldpath' => $path1 ,'newpath'=>$path2, 'run' => &$run));
+ if($run){
+ $mp1=self::getMountPoint($path1);
+ $mp2=self::getMountPoint($path2);
+ if($mp1==$mp2){
+ if($storage=self::getStorage($path1)){
+ $result=$storage->rename(self::getInternalPath($path1),self::getInternalPath($path2));
+ }
+ }elseif($storage1=self::getStorage($path1) and $storage2=self::getStorage($path2)){
+ $tmpFile=$storage1->toTmpFile(self::getInternalPath($path1));
+ $result=$storage2->fromTmpFile(self::getInternalPath($path2));
+ $storage1->unlink(self::getInternalPath($path1));
}
- }elseif($storage1=self::getStorage($path1) and $storage2=self::getStorage($path2)){
- $tmpFile=$storage1->toTmpFile(self::getInternalPath($path1));
- $result=$storage2->fromTmpFile(self::getInternalPath($path2));
- $storage1->unlink(self::getInternalPath($path1));
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_rename', array( 'oldpath' => $path1, 'newpath'=>$path2));
return $result;
}
- OC_HOOK::emit( 'OC_FILESYSTEM', 'rename', array( 'oldpath' => $path1 ,'newpath'=>$path2));
}
}
static public function copy($path1,$path2){
if(self::canRead($path1) and self::canWrite($path2)){
- $mp1=self::getMountPoint($path1);
- $mp2=self::getMountPoint($path2);
- if($mp1==$mp2){
- if($storage=self::getStorage($path1)){
- return $storage->copy(self::getInternalPath($path1),self::getInternalPath($path2));
+ $run=true;
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'copy', array( 'oldpath' => $path1 ,'newpath'=>$path2, 'run' => &$run));
+ $exists=self::file_exists($path2);
+ if($run and !$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path2, 'run' => &$run));
+ }
+ if($run){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path2, 'run' => &$run));
+ }
+ if($run){
+ $mp1=self::getMountPoint($path1);
+ $mp2=self::getMountPoint($path2);
+ if($mp1==$mp2){
+ if($storage=self::getStorage($path1)){
+ $result=$storage->copy(self::getInternalPath($path1),self::getInternalPath($path2));
+ }
+ }elseif($storage1=self::getStorage($path1) and $storage2=self::getStorage($path2)){
+ $tmpFile=$storage1->toTmpFile(self::getInternalPath($path1));
+ $result=$storage2->fromTmpFile(self::getInternalPath($path2));
+ }
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_copy', array( 'oldpath' => $path1 ,'newpath'=>$path2));
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_create', array( 'path' => $path));
}
- }elseif($storage1=self::getStorage($path1) and $storage2=self::getStorage($path2)){
- $tmpFile=$storage1->toTmpFile(self::getInternalPath($path1));
- return $storage2->fromTmpFile(self::getInternalPath($path2));
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_write', array( 'path' => $path));
+ return $result;
}
- OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path2));
}
}
static public function fopen($path,$mode){
$allowed=((strpos($path,'r')===false and strpos($path,'r+')!==false and self::canRead) or self::canWrite($path));
if($allowed){
if($storage=self::getStorage($path)){
+ $run=true;
+ $exists=self::file_exists($path);
+ $write=false;
switch($mode){
case 'r':
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
@@ -365,21 +438,34 @@ class OC_FILESYSTEM{
case 'x+':
case 'a+':
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
- OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
- if(!self::file_exists($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
- }
+ $write=true;
break;
case 'w':
case 'x':
case 'a':
- OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
- if(!self::file_exists($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
- }
+ $write=true;
break;
}
- return $storage->fopen(self::getInternalPath($path),$mode);
+ if($write){
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
+ }
+ if($run){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path, 'run' => &$run));
+ }
+ }
+ if($run){
+ $result=$storage->fopen(self::getInternalPath($path),$mode);
+ if($write){
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_create', array( 'path' => $path));
+ }
+ if($run){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_write', array( 'path' => $path));
+ }
+ }
+ return $result;
+ }
}
}
}
@@ -391,20 +477,44 @@ class OC_FILESYSTEM{
}
static public function fromTmpFile($tmpFile,$path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
- if(!self::file_exists($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
+ $run=true;
+ $exists=self::file_exists($path);
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path, 'run' => &$run));
+ }
+ if($run){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path, 'run' => &$run));
+ }
+ if($run){
+ $result=$storage->fromTmpFile($tmpFile,self::getInternalPath($path));
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_create', array( 'path' => $path));
+ }
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_write', array( 'path' => $path));
+ return $result;
}
- return $storage->fromTmpFile($tmpFile,self::getInternalPath($path));
}
}
static public function fromUploadedFile($tmpFile,$path){
+ error_log('upload');
if(self::canWrite($path) and $storage=self::getStorage($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
- if(!self::file_exists($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
+ $run=true;
+ $exists=self::file_exists($path);
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path, 'run' => &$run));
+ }
+ if($run){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path, 'run' => &$run));
+ }
+ error_log('upload2');
+ if($run){
+ $result=$storage->fromUploadedFile($tmpFile,self::getInternalPath($path));
+ if(!$exists){
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_create', array( 'path' => $path));
+ }
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'post_write', array( 'path' => $path));
+ return $result;
}
- return $storage->fromUploadedFile($tmpFile,self::getInternalPath($path));
}
}
static public function getMimeType($path){
@@ -414,8 +524,11 @@ class OC_FILESYSTEM{
}
static public function delTree($path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
- OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path));
- return $storage->delTree(self::getInternalPath($path));
+ $run=true;
+ OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path, 'run' => &$run));
+ if($run){
+ return $storage->delTree(self::getInternalPath($path));
+ }
}
}
static public function find($path){
@@ -463,10 +576,12 @@ class OC_FILESYSTEM{
$fakeRootLength=strlen(self::$fakeRoot);
foreach(self::$storages as $mountpoint=>$storage){
$results=$storage->search($query);
- foreach($results as $result){
- $file=str_replace('//','/',$mountpoint.$result);
- $file=substr($file,$fakeRootLength);
- $files[]=$file;
+ if(is_array($results)){
+ foreach($results as $result){
+ $file=str_replace('//','/',$mountpoint.$result);
+ $file=substr($file,$fakeRootLength);
+ $files[]=$file;
+ }
}
}
return $files;