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