]> source.dussan.org Git - nextcloud-server.git/commitdiff
stricter tests for archive backends and make sure we make the tests
authorRobin Appelman <icewind@owncloud.com>
Wed, 28 Mar 2012 21:46:44 +0000 (23:46 +0200)
committerRobin Appelman <icewind@owncloud.com>
Wed, 28 Mar 2012 21:47:44 +0000 (23:47 +0200)
3rdparty/Archive/Tar.php
apps/files_archive/lib/archive.php
apps/files_archive/lib/storage.php
apps/files_archive/lib/tar.php
apps/files_archive/lib/zip.php
apps/files_archive/tests/archive.php

index d69bab6ad7880b43d64abe9e7ec4843ab3d7fb90..d8eae851bdcde846279a7832bbfca0ac9181c7db 100644 (file)
@@ -649,14 +649,14 @@ class Archive_Tar extends PEAR
     // {{{ _error()
     function _error($p_message)
     {
-        $this->error_object = &$this->raiseError($p_message); 
+        $this->error_object = $this->raiseError($p_message);
     }
     // }}}
 
     // {{{ _warning()
     function _warning($p_message)
     {
-        $this->error_object = &$this->raiseError($p_message); 
+        $this->error_object = $this->raiseError($p_message);
     }
     // }}}
 
index 3be3388a3b050f50227b225b8d1a4e4a83c054ef..113f92e9604d1e2e934ea7b9e2bfbc3d2a695b32 100644 (file)
@@ -17,6 +17,15 @@ abstract class OC_Archive{
                switch($ext){
                        case '.zip':
                                return new OC_Archive_ZIP($path);
+                       case '.gz':
+                       case '.bz':
+                       case '.bz2':
+                               if(strpos($path,'.tar.')){
+                                       return new OC_Archive_TAR($path);
+                               }
+                               break;
+                       case '.tgz':
+                               return new OC_Archive_TAR($path);
                }
        }
        
index 72a96ca5a5d7f4b8eead02d621f87818d18accdb..598b85f0dbec2d982860f7da7871e33a6a9f70b0 100644 (file)
@@ -139,4 +139,8 @@ class OC_Filestorage_Archive extends OC_Filestorage_Common{
                }
                self::$enableAutomount=true;
        }
+
+       public function rename($path1,$path2){
+               return $this->archive->rename($path1,$path2);
+       }
 }
index 40b314a2abf7a1b4fd646fe2d618422ba6e2e979..a5d54004788f7fe0b6f8a64213ca3795f53b4fc4 100644 (file)
@@ -15,7 +15,6 @@ class OC_Archive_TAR extends OC_Archive{
         * @var Archive_Tar tar
         */
        private $tar=null;
-       private $headers=array();
        private $path;
 
        function __construct($source){
@@ -53,10 +52,17 @@ class OC_Archive_TAR extends OC_Archive{
         * @return bool
         */
        function addFolder($path){
-               if(substr($path,-1)!=='/'){
+               $tmpBase=get_temp_dir().'/';
+               if(substr($path,-1,1)!='/'){
                        $path.='/';
                }
-               return $this->tar->add(array($path));
+               if($this->fileExists($path)){
+                       return false;
+               }
+               mkdir($tmpBase.$path);
+               $result=$this->tar->addModify(array($tmpBase.$path),'',$tmpBase);
+               rmdir($tmpBase.$path);
+               return $result;
        }
        /**
         * add a file to the archive
@@ -76,10 +82,6 @@ class OC_Archive_TAR extends OC_Archive{
                }else{
                        $result=$this->tar->addString($path,$source);
                }
-//             $this->reopen();
-//             var_dump($this->getFiles());
-//             exit();
-               
                return $result;
        }
 
@@ -98,16 +100,13 @@ class OC_Archive_TAR extends OC_Archive{
                unlink($this->path);
                $types=array(null,'gz','bz');
                $this->tar=new Archive_Tar($this->path,$types[self::getTarType($this->path)]);
-               $this->tar->createModify(array($tmp),'',$tmp);
+               $this->tar->createModify(array($tmp),'',$tmp.'/');
        }
 
        private function getHeader($file){
-               if(isset($this->headers[$file])){
-                       return $this->headers[$file];
-               }
                $headers=$this->tar->listContent();
                foreach($headers as $header){
-                       if($file==$header['filename']){
+                       if($file==$header['filename'] or $file.'/'==$header['filename']){
                                return $header;
                        }
                }
@@ -179,6 +178,9 @@ class OC_Archive_TAR extends OC_Archive{
         */
        function extractFile($path,$dest){
                $tmp=OC_Helper::tmpFolder();
+               if(!$this->fileExists($path)){
+                       return false;
+               }
                $success=$this->tar->extractList(array($path),$tmp);
                if($success){
                        rename($tmp.$path,$dest);
@@ -210,6 +212,9 @@ class OC_Archive_TAR extends OC_Archive{
         * @return bool
         */
        function remove($path){
+               if(!$this->fileExists($path)){
+                       return false;
+               }
                //no proper way to delete, extract entire archive, delete file and remake archive
                $tmp=OC_Helper::tmpFolder();
                $this->tar->extract($tmp);
@@ -218,6 +223,7 @@ class OC_Archive_TAR extends OC_Archive{
                unlink($this->path);
                $this->reopen();
                $this->tar->createModify(array($tmp),'',$tmp);
+               return true;
        }
        /**
         * get a file handler
index 16f2273f443294d43301e053e66c591ccf6c8fb1..5a5bc766875ddde02b3d02abac2f2fd825dc274b 100644 (file)
@@ -11,7 +11,6 @@ class OC_Archive_ZIP extends OC_Archive{
         * @var ZipArchive zip
         */
        private $zip=null;
-       private $contents=array();
        private $success=false;
        private $path;
        
@@ -56,7 +55,9 @@ class OC_Archive_ZIP extends OC_Archive{
         * @return bool
         */
        function rename($source,$dest){
-               return $this->zip->renameName($source,$dest);
+               $source=$this->stripPath($source);
+               $dest=$this->stripPath($dest);
+               $this->zip->renameName($source,$dest);
        }
        /**
         * get the uncompressed size of a file in the archive
@@ -99,15 +100,11 @@ class OC_Archive_ZIP extends OC_Archive{
         * @return array
         */
        function getFiles(){
-               if(count($this->contents)){
-                       return $this->contents;
-               }
                $fileCount=$this->zip->numFiles;
                $files=array();
                for($i=0;$i<$fileCount;$i++){
                        $files[]=$this->zip->getNameIndex($i);
                }
-               $this->contents=$files;
                return $files;
        }
        /**
@@ -143,7 +140,7 @@ class OC_Archive_ZIP extends OC_Archive{
         * @return bool
         */
        function fileExists($path){
-               return $this->zip->locateName($path)!==false;
+               return ($this->zip->locateName($path)!==false) or ($this->zip->locateName($path.'/')!==false);
        }
        /**
         * remove a file or folder from the archive
@@ -151,7 +148,11 @@ class OC_Archive_ZIP extends OC_Archive{
         * @return bool
         */
        function remove($path){
-               return $this->zip->deleteName($path);
+               if($this->fileExists($path.'/')){
+                       return $this->zip->deleteName($path.'/');
+               }else{
+                       return $this->zip->deleteName($path);
+               }
        }
        /**
         * get a file handler
@@ -188,4 +189,12 @@ class OC_Archive_ZIP extends OC_Archive{
                        unlink($tmpFile);
                }
        }
+
+       private function stripPath($path){
+               if(substr($path,0,1)=='/'){
+                       return substr($path,1);
+               }else{
+                       return $path;
+               }
+       }
 }
index 2619d91b3b887d3794596f52c538ce1c34e74fe3..9e99466a521c334dfb5890bbb0739852a017a4fd 100644 (file)
@@ -68,6 +68,7 @@ abstract class Test_Archive extends UnitTestCase {
                $this->instance->addFile('lorem.txt',$textFile);
                $this->assertEqual(1,count($this->instance->getFiles()));
                $this->assertTrue($this->instance->fileExists('lorem.txt'));
+               $this->assertFalse($this->instance->fileExists('lorem.txt/'));
                
                $this->assertEqual(file_get_contents($textFile),$this->instance->getFile('lorem.txt'));
                $this->instance->addFile('lorem.txt','foobar');
@@ -94,6 +95,17 @@ abstract class Test_Archive extends UnitTestCase {
                $this->assertTrue($this->instance->fileExists('lorem.txt'));
                $this->assertEqual(file_get_contents($dir.'/lorem.txt'),$this->instance->getFile('lorem.txt'));
        }
+       public function testFolder(){
+               $this->instance=$this->getNew();
+               $this->assertFalse($this->instance->fileExists('/test'));
+               $this->assertFalse($this->instance->fileExists('/test/'));
+               $this->instance->addFolder('/test');
+               $this->assertTrue($this->instance->fileExists('/test'));
+               $this->assertTrue($this->instance->fileExists('/test/'));
+               $this->instance->remove('/test');
+               $this->assertFalse($this->instance->fileExists('/test'));
+               $this->assertFalse($this->instance->fileExists('/test/'));
+       }
        public function testExtract(){
                $dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
                $this->instance=$this->getExisting();
@@ -105,4 +117,17 @@ abstract class Test_Archive extends UnitTestCase {
                $this->assertEqual(file_get_contents($dir.'/lorem.txt'),file_get_contents($tmpDir.'lorem.txt'));
                OC_Helper::rmdirr($tmpDir);
        }
+       public function testMoveRemove(){
+               $dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
+               $textFile=$dir.'/lorem.txt';
+               $this->instance=$this->getNew();
+               $this->instance->addFile('lorem.txt',$textFile);
+               $this->assertFalse($this->instance->fileExists('target.txt'));
+               $this->instance->rename('lorem.txt','target.txt');
+               $this->assertTrue($this->instance->fileExists('target.txt'));
+               $this->assertFalse($this->instance->fileExists('lorem.txt'));
+               $this->assertEqual(file_get_contents($textFile),$this->instance->getFile('target.txt'));
+               $this->instance->remove('target.txt');
+               $this->assertFalse($this->instance->fileExists('target.txt'));
+       }
 }