summaryrefslogtreecommitdiffstats
path: root/apps/files_archive
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-03-28 23:46:44 +0200
committerRobin Appelman <icewind@owncloud.com>2012-03-28 23:47:44 +0200
commit1b6fe4f65efb1d1f696f35b70454ad30cae6310b (patch)
tree03abff26ef05581372af60408cb7b3e763ef47aa /apps/files_archive
parentc26e00346210492ed9dd60c7a910b5187310d47b (diff)
downloadnextcloud-server-1b6fe4f65efb1d1f696f35b70454ad30cae6310b.tar.gz
nextcloud-server-1b6fe4f65efb1d1f696f35b70454ad30cae6310b.zip
stricter tests for archive backends and make sure we make the tests
Diffstat (limited to 'apps/files_archive')
-rw-r--r--apps/files_archive/lib/archive.php9
-rw-r--r--apps/files_archive/lib/storage.php4
-rw-r--r--apps/files_archive/lib/tar.php30
-rw-r--r--apps/files_archive/lib/zip.php25
-rw-r--r--apps/files_archive/tests/archive.php25
5 files changed, 73 insertions, 20 deletions
diff --git a/apps/files_archive/lib/archive.php b/apps/files_archive/lib/archive.php
index 3be3388a3b0..113f92e9604 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 72a96ca5a5d..598b85f0dbe 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 40b314a2abf..a5d54004788 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 16f2273f443..5a5bc766875 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 2619d91b3b8..9e99466a521 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'));
+ }
}