Skip to content

Commit e47f8c2

Browse files
authored
Merge pull request #32 from steffunky/fix-file-size-is-a-multiple-of-buffer
Fix block padding when the file buffer length is a multiple of 512 and smaller than Archive_Tar buffer length
2 parents 716aacb + a1ff994 commit e47f8c2

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

Archive/Tar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ public function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $v_
12731273
while (($v_buffer = fread($v_file, $this->buffer_length)) != '') {
12741274
$buffer_length = strlen("$v_buffer");
12751275
if ($buffer_length != $this->buffer_length) {
1276-
$pack_size = ((int)($buffer_length / 512) + 1) * 512;
1276+
$pack_size = ((int)($buffer_length / 512) + ($buffer_length % 512 !== 0 ? 1 : 0)) * 512;
12771277
$pack_format = sprintf('a%d', $pack_size);
12781278
} else {
12791279
$pack_format = sprintf('a%d', $this->buffer_length);

tests/512nbytesfile.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
test file size that happens to be 512 * n bytes
3+
--SKIPIF--
4+
--FILE--
5+
<?php
6+
$dirname = dirname(__FILE__);
7+
require_once $dirname . '/setup.php.inc';
8+
9+
$tar = new Archive_Tar($dirname . '/512nbytesfile.tar.gz', null, 2048);
10+
$tar->add($dirname .'/testblock3');
11+
$tar->listContent();
12+
$phpunit->assertNoErrors('after tar archive listing');
13+
14+
$returnval = shell_exec('tar -Ptf ' . $dirname . '/512nbytesfile.tar.gz | sort');
15+
$phpunit->assertNoErrors('after shell tar listing');
16+
17+
$expectedvalue =
18+
<<< EOD
19+
$dirname/testblock3
20+
$dirname/testblock3/1024bytes.txt
21+
$dirname/testblock3/randombytes.txt
22+
EOD;
23+
$phpunit->assertEquals($expectedvalue, $returnval, 'wrong output for shell tar verification');
24+
25+
echo 'test done'
26+
?>
27+
--CLEAN--
28+
<?php
29+
$dirname = dirname(__FILE__);
30+
@unlink($dirname.'/512nbytesfile.tar.gz');
31+
?>
32+
--EXPECT--
33+
test done

tests/testblock3/1024bytes.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
194 210 54 166 74 179 115 216 109 21 220 29 144 242 90 206
2+
240 73 172 14 153 163 43 16 233 20 186 91 95 150 60 61
3+
180 187 233 126 130 154 116 206 213 231 202 148 148 12 125 24
4+
6 139 32 235 117 240 56 96 23 1 182 167 116 64 92 73
5+
250 177 157 119 121 148 112 50 89 24 37 214 81 88 201 75
6+
162 108 60 114 137 227 170 186 231 213 177 33 44 143 243 98
7+
14 28 226 77 152 241 243 62 103 237 187 235 227 73 9 119
8+
162 64 127 74 73 90 112 200 39 6 173 251 12 191 166 18
9+
235 100 74 184 126 153 23 118 27 25 128 222 234 246 165 159
10+
170 127 236 199 91 67 96 185 108 159 172 2 245 202 161 177
11+
147 188 0 151 251 230 251 100 226 67 212 38 72 32 44 117
12+
10 39 163 159 0 197 160 225 25 177 16 0 240 16 137 105
13+
187 132 129 98 24 210 169 33 246 77 217 145 242 118 181 47
14+
160 61 62 129 71 52 253 85 2 215 174 241 55 32 236 27
15+
160 120 145 188 132 37 161 178 45 227 209 57 142 19 88 175
16+
230 238 104 128 26 196 126 118 214 122 201 123

tests/testblock3/randombytes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
250 97 98 194 5 149 229 27 58 62 133 73 95 230 219 128
2+
224 98 210 250 162 58 240 80 202 249 173 203 125 186

0 commit comments

Comments
 (0)