Definition at line 16 of file Tar.php.
◆ _composeHeader()
_composeHeader |
( |
|
$long = false | ) |
|
|
protected |
Compose header for current file in TAR format. If length of file's name greater 100 characters, method breaks header into two pieces. First contains header and data with long name. Second contain only header.
- Parameters
-
- Returns
- string @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)
Definition at line 319 of file Tar.php.
323 $infoFile = stat($file);
324 $nameFile = str_replace(
$path,
'', $file);
325 $nameFile = str_replace(
'\\',
'/', $nameFile);
328 if (!$long && strlen($nameFile) > 100) {
330 $longHeader .= str_pad($nameFile, floor((strlen($nameFile) + 512 - 1) / 512) * 512,
"\0");
333 $header[
'100-name'] = $long ?
'././@LongLink' : substr($nameFile, 0, 100);
334 $header[
'8-mode'] = $long ?
' ' : str_pad(
335 substr(sprintf(
"%07o", $infoFile[
'mode']), -4),
340 $header[
'8-uid'] = $long || $infoFile[
'uid'] == 0 ?
"\0\0\0\0\0\0\0" : sprintf(
"%07o", $infoFile[
'uid']);
341 $header[
'8-gid'] = $long || $infoFile[
'gid'] == 0 ?
"\0\0\0\0\0\0\0" : sprintf(
"%07o", $infoFile[
'gid']);
342 $header[
'12-size'] = $long ? sprintf(
349 $header[
'12-mtime'] = $long ?
'00000000000' : sprintf(
"%011o", $infoFile[
'mtime']);
350 $header[
'8-check'] = sprintf(
'% 8s',
'');
351 $header[
'1-type'] = $long ?
'L' : (is_link($file) ? 2 : (
is_dir($file) ? 5 : 0));
352 $header[
'100-symlink'] = is_link($file) ? readlink($file) :
'';
353 $header[
'6-magic'] =
'ustar ';
354 $header[
'2-version'] =
' ';
355 $a =
function_exists(
'posix_getpwuid') ? posix_getpwuid(fileowner($file)) : [
'name' =>
''];
356 $header[
'32-uname'] = $a[
'name'];
357 $a =
function_exists(
'posix_getgrgid') ? posix_getgrgid(filegroup($file)) : [
'name' =>
''];
358 $header[
'32-gname'] = $a[
'name'];
359 $header[
'8-devmajor'] =
'';
360 $header[
'8-devminor'] =
'';
361 $header[
'155-prefix'] =
'';
362 $header[
'12-closer'] =
'';
365 foreach ($header as $key =>
$element) {
366 $length = explode(
'-', $key);
371 for (
$i = 0;
$i < 512;
$i++) {
372 $checksum += ord(substr($packedHeader,
$i, 1));
374 $packedHeader = substr_replace($packedHeader, sprintf(
"%07o", $checksum) .
"\0", 148, 8);
376 return $longHeader . $packedHeader;
_composeHeader($long=false)
pack($source, $destination, $skipRoot=false)
◆ _createTar()
_createTar |
( |
|
$skipRoot = false , |
|
|
|
$finalize = false |
|
) |
| |
|
protected |
Recursively walk through file tree and create tarball
- Parameters
-
bool | $skipRoot | |
bool | $finalize | |
- Returns
- void
- Exceptions
-
Definition at line 245 of file Tar.php.
254 $dirFiles = scandir($file, SCANDIR_SORT_NONE);
256 if (
false === $dirFiles) {
257 throw new \Magento\Framework\Exception\LocalizedException(
258 new \
Magento\Framework\Phrase(
'Can\'t scan dir: %1', [$file])
262 array_shift($dirFiles);
264 array_shift($dirFiles);
267 foreach ($dirFiles as
$item) {
273 $this->
_getWriter()->write(str_repeat(
"\0", self::TAR_BLOCK_SIZE * 12));
_packAndWriteCurrentFile()
◆ _destroyReader()
Destroy tarball reader
- Returns
- $this
Definition at line 139 of file Tar.php.
141 if ($this->_reader instanceof File) {
142 $this->_reader->close();
143 $this->_reader =
null;
◆ _destroyWriter()
Destroy tarball writer
- Returns
- $this
Definition at line 97 of file Tar.php.
99 if ($this->_writer instanceof File) {
100 $this->_writer->close();
101 $this->_writer =
null;
◆ _extractAndWriteFile()
_extractAndWriteFile |
( |
|
$fileHeader, |
|
|
|
$destination |
|
) |
| |
|
protected |
Extract next file from tarball by its $header information and save it to $destination
- Parameters
-
array | $fileHeader | |
string | $destination | |
- Returns
- void
Definition at line 499 of file Tar.php.
501 $fileWriter =
new File($destination);
502 $fileWriter->open(
'w', $fileHeader[
'mode']);
506 $filesize = $fileHeader[
'size'];
509 while ($filesize > $bytesExtracted && !$archiveReader->eof()) {
510 $block = $archiveReader->read(self::TAR_BLOCK_SIZE);
511 $nonExtractedBytesCount = $filesize - $bytesExtracted;
513 $data = substr(
$block, 0, $nonExtractedBytesCount);
514 $fileWriter->write(
$data);
516 $bytesExtracted += strlen(
$block);
◆ _extractFileHeader()
Read and decode file header information from tarball
- Returns
- array|bool
Definition at line 441 of file Tar.php.
445 $headerBlock = $archiveReader->read(self::TAR_BLOCK_SIZE);
447 if (strlen($headerBlock) < self::TAR_BLOCK_SIZE) {
451 $header =
unpack(self::_getFormatParseHeader(), $headerBlock);
453 $header[
'mode'] = octdec($header[
'mode']);
454 $header[
'uid'] = octdec($header[
'uid']);
455 $header[
'gid'] = octdec($header[
'gid']);
456 $header[
'size'] = octdec($header[
'size']);
457 $header[
'mtime'] = octdec($header[
'mtime']);
458 $header[
'checksum'] = octdec($header[
'checksum']);
460 if ($header[
'type'] ==
"5") {
465 $headerBlock = substr_replace($headerBlock,
' ', 148, 8);
467 for (
$i = 0;
$i < 512;
$i++) {
468 $checksum += ord(substr($headerBlock,
$i, 1));
471 $checksumOk = $header[
'checksum'] == $checksum;
472 if (isset($header[
'name']) && $checksumOk) {
473 $header[
'name'] = trim($header[
'name']);
474 if (!($header[
'name'] ==
'././@LongLink' && $header[
'type'] ==
'L')) {
478 $realNameBlockSize = floor(
479 ($header[
'size'] + self::TAR_BLOCK_SIZE - 1) / self::TAR_BLOCK_SIZE
481 $realNameBlock = $archiveReader->read($realNameBlockSize);
482 $realName = substr($realNameBlock, 0, $header[
'size']);
485 $headerMain[
'name'] = trim($realName);
unpack($source, $destination)
◆ _getCurrentFile()
Retrieve file which is packing.
- Returns
- string
Definition at line 205 of file Tar.php.
◆ _getCurrentPath()
Retrieve path to file which is packing.
- Returns
- string
Definition at line 232 of file Tar.php.
◆ _getFormatParseHeader()
static _getFormatParseHeader |
( |
| ) |
|
|
staticprotected |
Returns string that is used for tar's header parsing
- Returns
- string
Definition at line 86 of file Tar.php.
88 return 'a100name/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100symlink/a6magic/a2version/' .
89 'a32uname/a32gname/a8devmajor/a8devminor/a155prefix/a12closer';
◆ _getReader()
Get tarball reader
- Returns
- File
Definition at line 154 of file Tar.php.
156 if (!$this->_reader) {
◆ _getWriter()
Get tarball writer
- Returns
- File
Definition at line 112 of file Tar.php.
114 if (!$this->_writer) {
◆ _initReader()
Initialize tarball reader
- Returns
- $this
Definition at line 126 of file Tar.php.
129 $this->_reader->open(
'r');
◆ _initWriter()
Initialize tarball writer
- Returns
- $this
Definition at line 73 of file Tar.php.
75 $this->_writer =
new File($this->_destinationFilePath);
76 $this->_writer->open(
'w');
◆ _packAndWriteCurrentFile()
_packAndWriteCurrentFile |
( |
| ) |
|
|
protected |
Write current file to tarball
- Returns
- void
Definition at line 282 of file Tar.php.
291 if (
is_file($currentFile) && !is_link($currentFile)) {
292 $fileReader =
new File($currentFile);
293 $fileReader->open(
'r');
295 while (!$fileReader->eof()) {
296 $archiveWriter->write($fileReader->read());
299 $fileReader->close();
304 $appendZerosCount = (self::TAR_BLOCK_SIZE - $fileSize %
self::TAR_BLOCK_SIZE) % self::TAR_BLOCK_SIZE;
305 $archiveWriter->write(str_repeat(
"\0", $appendZerosCount));
_composeHeader($long=false)
◆ _setCurrentFile()
Set file which is packing.
- Parameters
-
- Returns
- $this
Definition at line 181 of file Tar.php.
183 $file = str_replace(
'\\',
'/', $file);
184 $this->_currentFile = $file . (!is_link($file) &&
is_dir($file) && substr($file, -1) !=
'/' ?
'/' :
'');
◆ _setCurrentPath()
Set path to file which is packing.
- Parameters
-
- Returns
- $this
Definition at line 216 of file Tar.php.
220 $this->_currentPath =
$path . (substr(
$path, -1) !=
'/' ?
'/' :
'');
222 $this->_currentPath = dirname(
$path) .
'/';
◆ _setDestinationFilePath()
_setDestinationFilePath |
( |
|
$destinationFilePath | ) |
|
|
protected |
Set path to file where tarball should be placed
- Parameters
-
string | $destinationFilePath | |
- Returns
- $this
Definition at line 194 of file Tar.php.
196 $this->_destinationFilePath = $destinationFilePath;
◆ _setSkipRoot()
_setSkipRoot |
( |
|
$skipRoot | ) |
|
|
protected |
Set option that define ability skip first catalog level.
- Parameters
-
- Returns
- $this
Definition at line 169 of file Tar.php.
171 $this->_skipRoot = $skipRoot;
◆ _unpackCurrentTar()
_unpackCurrentTar |
( |
|
$destination | ) |
|
|
protected |
Read TAR string from file, and unpacked it. Create files and directories information about described in the string.
- Parameters
-
string | $destination | path to file is unpacked |
- Returns
- string[] list of files
- Exceptions
-
Definition at line 389 of file Tar.php.
394 while (!$archiveReader->eof()) {
401 $currentFile = $destination . $header[
'name'];
402 $dirname = dirname($currentFile);
404 if (in_array($header[
'type'], [
"0", chr(0),
''])) {
405 if (!file_exists($dirname)) {
406 $mkdirResult = @
mkdir($dirname, 0777,
true);
408 if (
false === $mkdirResult) {
409 throw new \Magento\Framework\Exception\LocalizedException(
410 new \
Magento\Framework\Phrase(
'Failed to create directory %1', [$dirname])
416 $list[] = $currentFile;
417 }
elseif ($header[
'type'] ==
'5') {
418 if (!file_exists($dirname)) {
419 $mkdirResult = @
mkdir($currentFile, $header[
'mode'],
true);
421 if (
false === $mkdirResult) {
422 throw new \Magento\Framework\Exception\LocalizedException(
423 new \
Magento\Framework\Phrase(
'Failed to create directory %1', [$currentFile])
427 $list[] = $currentFile .
'/';
428 }
elseif ($header[
'type'] ==
'2') {
430 @symlink($header[
'symlink'], $currentFile);
elseif(isset( $params[ 'redirect_parent']))
_extractAndWriteFile($fileHeader, $destination)
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
◆ extract()
extract |
( |
|
$file, |
|
|
|
$source, |
|
|
|
$destination |
|
) |
| |
Extract one file from TAR (Tape Archiver).
- Parameters
-
string | $file | |
string | $source | |
string | $destination | |
- Returns
- string
Definition at line 568 of file Tar.php.
576 while (!$archiveReader->eof()) {
578 if ($header[
'name'] == $file) {
579 $extractedFile = $destination . basename($header[
'name']);
584 if ($header[
'type'] != 5) {
586 ($header[
'size'] + self::TAR_BLOCK_SIZE - 1) / self::TAR_BLOCK_SIZE
588 $archiveReader->read($skipBytes);
593 return $extractedFile;
_extractAndWriteFile($fileHeader, $destination)
◆ pack()
pack |
( |
|
$source, |
|
|
|
$destination, |
|
|
|
$skipRoot = false |
|
) |
| |
Pack file to TAR (Tape Archiver).
- Parameters
-
string | $source | |
string | $destination | |
bool | $skipRoot | |
- Returns
- string @SuppressWarnings(PHPMD.UnusedLocalVariable)
Definition at line 529 of file Tar.php.
_createTar($skipRoot=false, $finalize=false)
◆ unpack()
unpack |
( |
|
$source, |
|
|
|
$destination |
|
) |
| |
Unpack file from TAR (Tape Archiver).
- Parameters
-
string | $source | |
string | $destination | |
- Returns
- string
Implements ArchiveInterface.
Definition at line 549 of file Tar.php.
_unpackCurrentTar($destination)
◆ $_currentFile
◆ $_currentPath
◆ $_destinationFilePath
◆ $_reader
◆ $_skipRoot
◆ $_writer
◆ TAR_BLOCK_SIZE
const TAR_BLOCK_SIZE = 512 |
Tar block size
@const int
Definition at line 23 of file Tar.php.
The documentation for this class was generated from the following file:
- vendor/magento/framework/Archive/Tar.php