Tar.php
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Read a tar archive
*
* PHP versions 4 and 5
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
*
* @category File Formats
* @package File_Archive
* @author Vincent Lascaux <vincentlascaux@php.net>
* @copyright 1997-2005 The PHP Group
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @version CVS: $Id: Tar.php,v 1.29 2005/07/11 11:53:53 vincentlascaux Exp $
* @link http://pear.php.net/package/File_Archive
*/
require_once "File/Archive/Reader/Archive.php";
/**
* Read a tar archive
*/
class File_Archive_Reader_Tar extends File_Archive_Reader_Archive
{
/**
* @var String Name of the file being read
* @access private
*/
var $currentFilename = null;
/**
* @var Array Stats of the file being read
* In TAR reader, indexes 2, 4, 5, 7, 9 are set
* @access private
*/
var $currentStat = null;
/**
* @var int Number of bytes that still have to be read before the end of
* file
* @access private
*/
var $leftLength = 0;
/**
* @var int Size of the footer
* A TAR file is made of chunks of 512 bytes. If 512 does not
* divide the file size a footer is added
* @access private
*/
var $footerLength = 0;
/**
* @var int nb bytes to seek back in order to reach the end of the archive
* or null if the end of the archive has not been reached
*/
var $seekToEnd = null;
/**
* @see File_Archive_Reader::skip()
*/
function skip($length = -1)
{
if ($length == -1) {
$length = $this->leftLength;
} else {
$length = min($this->leftLength, $length);
}
$skipped = $this->source->skip($length);
if (!PEAR::isError($skipped)) {
$this->leftLength -= $skipped;
}
return $skipped;
}
/**
* @see File_Archive_Reader::rewind()
*/
function rewind($length = -1)
{
if ($length == -1) {
$length = $this->currentStat[7] - $this->leftLength;
} else {
$length = min($length, $this->currentStat[7] - $this->leftLength);
}
$rewinded = $this->source->rewind($length);
if (!PEAR::isError($rewinded)) {
$this->leftLength += $rewinded;
}
return $rewinded;
}
/**
* @see File_Archive_Reader::tell()
*/
function tell()
{
return $this->currentStat[7] - $this->leftLength;
}
/**
* @see File_Archive_Reader::close()
*/
function close()
{
$this->leftLength = 0;
$this->currentFilename = null;
$this->currentStat = null;
$this->seekToEnd = null;
return parent::close();
}
/**
* @see File_Archive_Reader::getFilename()
*/
function getFilename() { return $this->currentFilename; }
/**
* @see File_Archive_Reader::getStat()
*/
function getStat() { return $this->currentStat; }
/**
* @see File_Archive_Reader::next()
*/
function next()
{
$error = parent::next();
if ($error !== true) {
return $error;
}
if ($this->seekToEnd !== null) {
return false;
}
do
{
$error = $this->source->skip($this->leftLength + $this->footerLength);
if (PEAR::isError($error)) {
return $error;
}
$rawHeader = $this->source->getData(512);
if (PEAR::isError($rawHeader)) {
return $rawHeader;
}
if (strlen($rawHeader)<512 || $rawHeader == pack("a512", "")) {
$this->seekToEnd = strlen($rawHeader);
$this->currentFilename = null;
return false;
}
$header = unpack(
"a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/".
"a8checksum/a1type/a100linkname/a6magic/a2version/".
"a32uname/a32gname/a8devmajor/a8devminor/a155prefix",
$rawHeader);
$this->currentStat = array(
2 => octdec($header['mode']),
4 => octdec($header['uid']),
5 => octdec($header['gid']),
7 => octdec($header['size']),
9 => octdec($header['mtime'])
);
$this->currentStat['mode'] = $this->currentStat[2];
$this->currentStat['uid'] = $this->currentStat[4];
$this->currentStat['gid'] = $this->currentStat[5];
$this->currentStat['size'] = $this->currentStat[7];
$this->currentStat['mtime'] = $this->currentStat[9];
if ($header['magic'] == 'ustar') {
$this->currentFilename = $this->getStandardURL(
$header['prefix'] . $header['filename']
);
} else {
$this->currentFilename = $this->getStandardURL(
$header['filename']
);
}
$this->leftLength = $this->currentStat[7];
if ($this->leftLength % 512 == 0) {
$this->footerLength = 0;
} else {
$this->footerLength = 512 - $this->leftLength%512;
}
$checksum = 8*ord(" ");
for ($i = 0; $i < 148; $i++) {
$checksum += ord($rawHeader{$i});
}
for ($i = 156; $i < 512; $i++) {
$checksum += ord($rawHeader{$i});
}
if (octdec($header['checksum']) != $checksum) {
die('Checksum error on entry '.$this->currentFilename);
}
} while ($header['type'] != 0);
return true;
}
/**
* @see File_Archive_Reader::getData()
*/
function getData($length = -1)
{
if ($length == -1) {
$actualLength = $this->leftLength;
} else {
$actualLength = min($this->leftLength, $length);
}
if ($this->leftLength == 0) {
return null;
} else {
$data = $this->source->getData($actualLength);
if (strlen($data) != $actualLength) {
return PEAR::raiseError('Unexpected end of tar archive');
}
$this->leftLength -= $actualLength;
return $data;
}
}
/**
* @see File_Archive_Reader::makeWriterRemoveFiles()
*/
function makeWriterRemoveFiles($pred)
{
require_once "File/Archive/Writer/Tar.php";
$blocks = array();
$seek = null;
$gap = 0;
if ($this->currentFilename !== null && $pred->isTrue($this)) {
$seek = 512 + $this->currentStat[7] + $this->footerLength;
$blocks[] = $seek; //Remove this file
}
while (($error = $this->next()) === true) {
$size = 512 + $this->currentStat[7] + $this->footerLength;
if ($pred->isTrue($this)) {
if ($seek === null) {
$seek = $size;
$blocks[] = $size;
} else if ($gap > 0) {
$blocks[] = $gap; //Don't remove the files between the gap
$blocks[] = $size;
$seek += $size;
} else {
$blocks[count($blocks)-1] += $size; //Also remove this file
$seek += $size;
}
$gap = 0;
} else {
if ($seek !== null) {
$seek += $size;
$gap += $size;
}
}
}
if ($seek === null) {
$seek = $this->seekToEnd;
} else {
$seek += $this->seekToEnd;
if ($gap == 0) {
array_pop($blocks);
} else {
$blocks[] = $gap;
}
}
$writer = new File_Archive_Writer_Tar(null,
$this->source->makeWriterRemoveBlocks($blocks, -$seek)
);
$this->close();
return $writer;
}
/**
* @see File_Archive_Reader::makeWriterRemoveBlocks()
*/
function makeWriterRemoveBlocks($blocks, $seek = 0)
{
if ($this->seekToEnd !== null || $this->currentStat === null) {
return PEAR::raiseError('No file selected');
}
$blockPos = $this->currentStat[7] - $this->leftLength + $seek;
$this->rewind();
$keep = false;
$data = $this->getData($blockPos);
foreach ($blocks as $length) {
if ($keep) {
$data .= $this->getData($length);
} else {
$this->skip($length);
}
$keep = !$keep;
}
if ($keep) {
$data .= $this->getData();
}
$filename = $this->currentFilename;
$stat = $this->currentStat;
$writer = $this->makeWriterRemove();
if (PEAR::isError($writer)) {
return $writer;
}
unset($stat[7]);
$stat[9] = $stat['mtime'] = time();
$writer->newFile($filename, $stat);
$writer->writeData($data);
return $writer;
}
/**
* @see File_Archive_Reader::makeAppendWriter
*/
function makeAppendWriter()
{
require_once "File/Archive/Writer/Tar.php";
while (($error = $this->next()) === true) { }
if (PEAR::isError($error)) {
$this->close();
return $error;
}
$innerWriter = $this->source->makeWriterRemoveBlocks(array(), -$this->seekToEnd);
if (PEAR::isError($innerWriter)) {
return $innerWriter;
}
$this->close();
return new File_Archive_Writer_Tar(null, $innerWriter);
}
}
?>