Back to index

php5  5.3.10
Public Member Functions | Protected Attributes
tarmaker Class Reference
Inheritance diagram for tarmaker:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 __construct ($path, $compress= 'zlib')
 addFile ($path, $fileOrStream, $stat=null)
 save a file inside this package
 init ()
 Initialize the package creator.
 mkdir ($dir)
 Create an internal directory, creating parent directories as needed.
 close ()
 Finish saving the package.

Protected Attributes

 $archive
 $tmp
 $path
 $compress

Detailed Description

Definition at line 3 of file tarmaker.php.inc.


Constructor & Destructor Documentation

tarmaker::__construct ( path,
compress = 'zlib' 
)

Definition at line 19 of file tarmaker.php.inc.

    {
        $this->compress = $compress;
        if ($compress === 'bz2' && !function_exists('bzopen')) {
            throw new PEAR2_Pyrus_Developer_Creator_Exception(
                'bzip2 extension not available');
        }
        if ($compress === 'zlib' && !function_exists('gzopen')) {
            throw new PEAR2_Pyrus_Developer_Creator_Exception(
                'zlib extension not available');
        }
        $this->path = $path;
    }

Member Function Documentation

tarmaker::addFile ( path,
fileOrStream,
stat = null 
)

save a file inside this package

This code is modified from Vincent Lascaux's File_Archive package, which is licensed under the LGPL license.

Parameters:
stringrelative path within the package
string|resourcefile contents or open file handle

Definition at line 41 of file tarmaker.php.inc.

    {
        clearstatcache();
        if ($stat === null) {
            if (is_resource($fileOrStream)) {
                $stat = fstat($fileOrStream);
            } else {
                $stat = array(
                    'mode' => 0x8000 + 0644,
                    'uid' => 0,
                    'gid' => 0,
                    'size' => strlen($fileOrStream),
                    'mtime' => time(),
                );
            }
        }

        $link = null;
        if ($stat['mode'] & 0x4000) {
            $type = 5;        // Directory
        } else if ($stat['mode'] & 0x8000) {
            $type = 0;        // Regular
        } else if ($stat['mode'] & 0xA000) {
            $type = 1;        // Link
            $link = @readlink($current);
        } else {
            $type = 9;        // Unknown
        }

        $filePrefix = '';
        if (strlen($path) > 255) {
            throw new Exception(
                "$path is too long, must be 255 characters or less"
            );
        } else if (strlen($path) > 100) {
            $filePrefix = substr($path, 0, strlen($path)-100);
            $path = substr($path, -100);
        }

        $block = pack('a100a8a8a8a12A12',
                $path,
                decoct($stat['mode']),
                sprintf('%6s ',decoct($stat['uid'])),
                sprintf('%6s ',decoct($stat['gid'])),
                sprintf('%11s ',decoct($stat['size'])),
                sprintf('%11s ',decoct($stat['mtime']))
            );

        $blockend = pack('a1a100a6a2a32a32a8a8a155a12',
            $type,
            $link,
            'ustar',
            '00',
            'Pyrus',
            'Pyrus',
            '',
            '',
            $filePrefix,
            '');

        $checkheader = array_merge(str_split($block), str_split($blockend));
        if (!function_exists('_pear2tarchecksum')) {
            function _pear2tarchecksum($a, $b) {return $a + ord($b);}
        }
        $checksum = 256; // 8 * ord(' ');
        $checksum += array_reduce($checkheader, '_pear2tarchecksum');

        $checksum = pack('a8', sprintf('%6s ', decoct($checksum)));

        fwrite($this->tmp, (binary)$block . $checksum . $blockend, 512);
        if (is_resource($fileOrStream)) {
            stream_copy_to_stream($fileOrStream, $this->tmp);
            if ($stat['size'] % 512) {
                fwrite($this->tmp, (binary)str_repeat("\0", 512 - $stat['size'] % 512));
            }
        } else {
            fwrite($this->tmp, (binary)$fileOrStream);
            if (strlen($fileOrStream) % 512) {
                fwrite($this->tmp, (binary)str_repeat("\0", 512 - strlen($fileOrStream) % 512));
            }
        }
    }

Here is the caller graph for this function:

Finish saving the package.

Reimplemented in corrupter, and corrupter.

Definition at line 164 of file tarmaker.php.inc.

    {
        fwrite($this->tmp, pack('a1024', ''));
        fclose($this->tmp);
    }

Initialize the package creator.

Definition at line 127 of file tarmaker.php.inc.

    {
        switch ($this->compress) {
            case 'zlib' :
                $this->tmp = gzopen($this->path, 'wb');
                break;
            case 'bz2' :
                $this->tmp = bzopen($this->path, 'w');
                break;
            case 'none' :
                $this->tmp = fopen($this->path, 'wb');
                break;
            default :
                throw new Exception(
                    'unknown compression type ' . $this->compress);
        }
    }
tarmaker::mkdir ( dir)

Create an internal directory, creating parent directories as needed.

Parameters:
string$dir

Definition at line 150 of file tarmaker.php.inc.

    {
        $this->addFile($dir, "", array(
                    'mode' => 0x4000 + 0644,
                    'uid' => 0,
                    'gid' => 0,
                    'size' => 0,
                    'mtime' => time(),
                ));
    }

Here is the call graph for this function:


Member Data Documentation

tarmaker::$archive [protected]

Definition at line 10 of file tarmaker.php.inc.

tarmaker::$compress [protected]

Definition at line 18 of file tarmaker.php.inc.

tarmaker::$path [protected]

Definition at line 17 of file tarmaker.php.inc.

tarmaker::$tmp [protected]

Definition at line 16 of file tarmaker.php.inc.


The documentation for this class was generated from the following file: