summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--_test/tests/inc/tar.test.php44
-rw-r--r--inc/Tar.class.php9
2 files changed, 50 insertions, 3 deletions
diff --git a/_test/tests/inc/tar.test.php b/_test/tests/inc/tar.test.php
index e8805a75d..90bc2e49e 100644
--- a/_test/tests/inc/tar.test.php
+++ b/_test/tests/inc/tar.test.php
@@ -316,4 +316,48 @@ class Tar_TestCase extends DokuWikiTest {
TestUtils::rdelete($out);
}
+
+ /**
+ * A single zero file should be just a header block + the footer
+ */
+ public function test_zerofile(){
+ $dir = dirname(__FILE__).'/tar';
+ $tar = new Tar();
+ $tar->create();
+ $tar->addFile("$dir/zero.txt", 'zero.txt');
+ $file = $tar->getArchive(Tar::COMPRESS_NONE);
+
+ $this->assertEquals(512*3, strlen($file)); // 1 header block + 2 footer blocks
+ }
+
+ public function test_zerodata(){
+ $tar = new Tar();
+ $tar->create();
+ $tar->addData('zero.txt','');
+ $file = $tar->getArchive(Tar::COMPRESS_NONE);
+
+ $this->assertEquals(512*3, strlen($file)); // 1 header block + 2 footer blocks
+ }
+
+ /**
+ * A file of exactly one block should be just a header block + data block + the footer
+ */
+ public function test_blockfile(){
+ $dir = dirname(__FILE__).'/tar';
+ $tar = new Tar();
+ $tar->create();
+ $tar->addFile("$dir/block.txt", 'block.txt');
+ $file = $tar->getArchive(Tar::COMPRESS_NONE);
+
+ $this->assertEquals(512*4, strlen($file)); // 1 header block + data block + 2 footer blocks
+ }
+
+ public function test_blockdata(){
+ $tar = new Tar();
+ $tar->create();
+ $tar->addData('block.txt', str_pad('', 512, 'x'));
+ $file = $tar->getArchive(Tar::COMPRESS_NONE);
+
+ $this->assertEquals(512*4, strlen($file)); // 1 header block + data block + 2 footer blocks
+ }
} \ No newline at end of file
diff --git a/inc/Tar.class.php b/inc/Tar.class.php
index 10e82109b..d1a38ea0e 100644
--- a/inc/Tar.class.php
+++ b/inc/Tar.class.php
@@ -262,7 +262,7 @@ class Tar {
if(!$this->fh) throw new TarIOException('Could not open file for writing: '.$this->file);
}
- $this->writeaccess = false;
+ $this->writeaccess = true;
$this->closed = false;
}
@@ -295,8 +295,11 @@ class Tar {
filemtime($file)
);
- if(filesize($file)) while(!feof($fp)) {
- $packed = pack("a512", fread($fp, 512));
+ while(!feof($fp)) {
+ $data = fread($fp, 512);
+ if($data === false) break;
+ if($data === '') break;
+ $packed = pack("a512", $data);
$this->writebytes($packed);
}
fclose($fp);