stricter tests for archive backends and make sure we make the tests

This commit is contained in:
Robin Appelman 2012-03-28 23:46:44 +02:00
parent c26e003462
commit 1b6fe4f65e
6 changed files with 75 additions and 22 deletions

View File

@ -649,14 +649,14 @@ class Archive_Tar extends PEAR
// {{{ _error() // {{{ _error()
function _error($p_message) function _error($p_message)
{ {
$this->error_object = &$this->raiseError($p_message); $this->error_object = $this->raiseError($p_message);
} }
// }}} // }}}
// {{{ _warning() // {{{ _warning()
function _warning($p_message) function _warning($p_message)
{ {
$this->error_object = &$this->raiseError($p_message); $this->error_object = $this->raiseError($p_message);
} }
// }}} // }}}

View File

@ -17,6 +17,15 @@ abstract class OC_Archive{
switch($ext){ switch($ext){
case '.zip': case '.zip':
return new OC_Archive_ZIP($path); 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);
} }
} }

View File

@ -139,4 +139,8 @@ class OC_Filestorage_Archive extends OC_Filestorage_Common{
} }
self::$enableAutomount=true; self::$enableAutomount=true;
} }
public function rename($path1,$path2){
return $this->archive->rename($path1,$path2);
}
} }

View File

@ -15,7 +15,6 @@ class OC_Archive_TAR extends OC_Archive{
* @var Archive_Tar tar * @var Archive_Tar tar
*/ */
private $tar=null; private $tar=null;
private $headers=array();
private $path; private $path;
function __construct($source){ function __construct($source){
@ -53,10 +52,17 @@ class OC_Archive_TAR extends OC_Archive{
* @return bool * @return bool
*/ */
function addFolder($path){ function addFolder($path){
if(substr($path,-1)!=='/'){ $tmpBase=get_temp_dir().'/';
if(substr($path,-1,1)!='/'){
$path.='/'; $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 * add a file to the archive
@ -76,10 +82,6 @@ class OC_Archive_TAR extends OC_Archive{
}else{ }else{
$result=$this->tar->addString($path,$source); $result=$this->tar->addString($path,$source);
} }
// $this->reopen();
// var_dump($this->getFiles());
// exit();
return $result; return $result;
} }
@ -98,16 +100,13 @@ class OC_Archive_TAR extends OC_Archive{
unlink($this->path); unlink($this->path);
$types=array(null,'gz','bz'); $types=array(null,'gz','bz');
$this->tar=new Archive_Tar($this->path,$types[self::getTarType($this->path)]); $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){ private function getHeader($file){
if(isset($this->headers[$file])){
return $this->headers[$file];
}
$headers=$this->tar->listContent(); $headers=$this->tar->listContent();
foreach($headers as $header){ foreach($headers as $header){
if($file==$header['filename']){ if($file==$header['filename'] or $file.'/'==$header['filename']){
return $header; return $header;
} }
} }
@ -179,6 +178,9 @@ class OC_Archive_TAR extends OC_Archive{
*/ */
function extractFile($path,$dest){ function extractFile($path,$dest){
$tmp=OC_Helper::tmpFolder(); $tmp=OC_Helper::tmpFolder();
if(!$this->fileExists($path)){
return false;
}
$success=$this->tar->extractList(array($path),$tmp); $success=$this->tar->extractList(array($path),$tmp);
if($success){ if($success){
rename($tmp.$path,$dest); rename($tmp.$path,$dest);
@ -210,6 +212,9 @@ class OC_Archive_TAR extends OC_Archive{
* @return bool * @return bool
*/ */
function remove($path){ function remove($path){
if(!$this->fileExists($path)){
return false;
}
//no proper way to delete, extract entire archive, delete file and remake archive //no proper way to delete, extract entire archive, delete file and remake archive
$tmp=OC_Helper::tmpFolder(); $tmp=OC_Helper::tmpFolder();
$this->tar->extract($tmp); $this->tar->extract($tmp);
@ -218,6 +223,7 @@ class OC_Archive_TAR extends OC_Archive{
unlink($this->path); unlink($this->path);
$this->reopen(); $this->reopen();
$this->tar->createModify(array($tmp),'',$tmp); $this->tar->createModify(array($tmp),'',$tmp);
return true;
} }
/** /**
* get a file handler * get a file handler

View File

@ -11,7 +11,6 @@ class OC_Archive_ZIP extends OC_Archive{
* @var ZipArchive zip * @var ZipArchive zip
*/ */
private $zip=null; private $zip=null;
private $contents=array();
private $success=false; private $success=false;
private $path; private $path;
@ -56,7 +55,9 @@ class OC_Archive_ZIP extends OC_Archive{
* @return bool * @return bool
*/ */
function rename($source,$dest){ 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 * get the uncompressed size of a file in the archive
@ -99,15 +100,11 @@ class OC_Archive_ZIP extends OC_Archive{
* @return array * @return array
*/ */
function getFiles(){ function getFiles(){
if(count($this->contents)){
return $this->contents;
}
$fileCount=$this->zip->numFiles; $fileCount=$this->zip->numFiles;
$files=array(); $files=array();
for($i=0;$i<$fileCount;$i++){ for($i=0;$i<$fileCount;$i++){
$files[]=$this->zip->getNameIndex($i); $files[]=$this->zip->getNameIndex($i);
} }
$this->contents=$files;
return $files; return $files;
} }
/** /**
@ -143,7 +140,7 @@ class OC_Archive_ZIP extends OC_Archive{
* @return bool * @return bool
*/ */
function fileExists($path){ 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 * remove a file or folder from the archive
@ -151,7 +148,11 @@ class OC_Archive_ZIP extends OC_Archive{
* @return bool * @return bool
*/ */
function remove($path){ 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 * get a file handler
@ -188,4 +189,12 @@ class OC_Archive_ZIP extends OC_Archive{
unlink($tmpFile); unlink($tmpFile);
} }
} }
private function stripPath($path){
if(substr($path,0,1)=='/'){
return substr($path,1);
}else{
return $path;
}
}
} }

View File

@ -68,6 +68,7 @@ abstract class Test_Archive extends UnitTestCase {
$this->instance->addFile('lorem.txt',$textFile); $this->instance->addFile('lorem.txt',$textFile);
$this->assertEqual(1,count($this->instance->getFiles())); $this->assertEqual(1,count($this->instance->getFiles()));
$this->assertTrue($this->instance->fileExists('lorem.txt')); $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->assertEqual(file_get_contents($textFile),$this->instance->getFile('lorem.txt'));
$this->instance->addFile('lorem.txt','foobar'); $this->instance->addFile('lorem.txt','foobar');
@ -94,6 +95,17 @@ abstract class Test_Archive extends UnitTestCase {
$this->assertTrue($this->instance->fileExists('lorem.txt')); $this->assertTrue($this->instance->fileExists('lorem.txt'));
$this->assertEqual(file_get_contents($dir.'/lorem.txt'),$this->instance->getFile('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(){ public function testExtract(){
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data'; $dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
$this->instance=$this->getExisting(); $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')); $this->assertEqual(file_get_contents($dir.'/lorem.txt'),file_get_contents($tmpDir.'lorem.txt'));
OC_Helper::rmdirr($tmpDir); 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'));
}
} }