diff --git a/3rdparty/Archive/Tar.php b/3rdparty/Archive/Tar.php index d69bab6ad7..d8eae851bd 100644 --- a/3rdparty/Archive/Tar.php +++ b/3rdparty/Archive/Tar.php @@ -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); } // }}} diff --git a/apps/files_archive/lib/archive.php b/apps/files_archive/lib/archive.php index 3be3388a3b..113f92e960 100644 --- a/apps/files_archive/lib/archive.php +++ b/apps/files_archive/lib/archive.php @@ -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); } } diff --git a/apps/files_archive/lib/storage.php b/apps/files_archive/lib/storage.php index 72a96ca5a5..598b85f0db 100644 --- a/apps/files_archive/lib/storage.php +++ b/apps/files_archive/lib/storage.php @@ -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); + } } diff --git a/apps/files_archive/lib/tar.php b/apps/files_archive/lib/tar.php index 40b314a2ab..a5d5400478 100644 --- a/apps/files_archive/lib/tar.php +++ b/apps/files_archive/lib/tar.php @@ -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 diff --git a/apps/files_archive/lib/zip.php b/apps/files_archive/lib/zip.php index 16f2273f44..5a5bc76687 100644 --- a/apps/files_archive/lib/zip.php +++ b/apps/files_archive/lib/zip.php @@ -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; + } + } } diff --git a/apps/files_archive/tests/archive.php b/apps/files_archive/tests/archive.php index 2619d91b3b..9e99466a52 100644 --- a/apps/files_archive/tests/archive.php +++ b/apps/files_archive/tests/archive.php @@ -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')); + } }