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
|
#!@prefix@/bin/php -Cq
<?php // -*- PHP -*-
// {{{ setup
define('S_IFDIR', 0040000); // Directory
define('S_IFCHR', 0020000); // Character device
define('S_IFBLK', 0060000); // Block device
define('S_IFREG', 0100000); // Regular file
define('S_IFIFO', 0010000); // FIFO
define('S_IFLNK', 0120000); // Symbolic link
define('S_IFSOCK', 0140000); // Socket
require_once "PEAR.php";
require_once "Archive/Tar.php";
require_once "Console/Getopt.php";
// }}}
// {{{ options
$verbose = false;
$op_create = false;
$op_list = false;
$op_extract = false;
$use_gzip = false;
$file = '';
$progname = basename(array_shift($argv));
$options = Console_Getopt::getopt($argv, "h?ctxvzf:");
if (PEAR::isError($options)) {
usage($options);
}
$opts = $options[0];
foreach ($opts as $opt) {
switch ($opt[0]) {
case 'v': {
$verbose = true;
break;
}
case 'c': {
$op_create = true;
break;
}
case 't': {
$op_list = true;
break;
}
case 'x': {
$op_extract = true;
break;
}
case 'z': {
$use_gzip = true;
break;
}
case 'f': {
$file = $opt[1];
break;
}
case 'h':
case '?': {
usage();
break;
}
}
}
if ($op_create + $op_list + $op_extract > 1) {
usage("Only one of -c, -t and -x can be specified at once!");
}
if ($op_create + $op_list + $op_extract == 0) {
usage("Please specify either -c, -t or -x!");
}
if (empty($file)) {
if ($op_create) {
$file = "php://stdout";
} else {
$file = "php://stdin";
}
}
// }}}
$tar = new Archive_Tar($file, $use_gzip);
$tar->setErrorHandling(PEAR_ERROR_DIE, "$progname error: %s\n");
if ($op_create) {
do_create($tar, $options[1]);
$tar->create($options[1]);
} elseif ($op_list) {
do_list($tar, $verbose);
} elseif ($op_extract) {
do_extract($tar);
}
// {{{ getrwx()
function getrwx($bits) {
$str = '';
$str .= ($bits & 4) ? 'r' : '-';
$str .= ($bits & 2) ? 'w' : '-';
$str .= ($bits & 1) ? 'x' : '-';
return $str;
}
// }}}
// {{{ getfiletype()
function getfiletype($bits) {
static $map = array(
'-' => S_IFREG,
'd' => S_IFDIR,
'l' => S_IFLNK,
'c' => S_IFCHR,
'b' => S_IFBLK,
'p' => S_IFIFO,
's' => S_IFSOCK,
);
foreach ($map as $char => $mask) {
if ($bits & $mask) {
return $char;
}
}
}
// }}}
// {{{ getuser()
function getuser($uid) {
static $cache = array();
if (isset($cache[$uid])) {
return $cache[$uid];
}
if (function_exists("posix_getpwuid")) {
if (is_array($user = @posix_getpwuid($uid))) {
$cache[$uid] = $user['name'];
return $user['name'];
}
}
$cache[$uid] = $uid;
return $uid;
}
// }}}
// {{{ getgroup()
function getgroup($gid) {
static $cache = array();
if (isset($cache[$gid])) {
return $cache[$gid];
}
if (function_exists("posix_getgrgid")) {
if (is_array($group = @posix_getgrgid($gid))) {
$cache[$gid] = $group['name'];
return $group['name'];
}
}
$cache[$gid] = $gid;
return $gid;
}
// }}}
// {{{ do_create()
function do_create(&$tar, &$files)
{
$tar->create($files);
}
// }}}
// {{{ do_list()
function do_list(&$tar, $verbose)
{
static $rwx = array(4 => 'r', 2 => 'w', 1 => 'x');
$files = $tar->listContent();
if (is_array($files) && sizeof($files) > 0) {
foreach ($files as $file) {
if ($verbose) {
$fm = (int)$file['mode'];
$mode = sprintf('%s%s%s%s', getfiletype($fm),
getrwx(($fm >> 6) & 7), getrwx(($fm >> 3) & 7),
getrwx($fm & 7));
$owner = getuser($file['uid']) . '/' . getgroup($file['gid']);
printf("%10s %-11s %7d %s %s\n", $mode, $owner, $file['size'],
date('Y-m-d H:i:s', $file['mtime']), $file['filename']);
} else {
printf("%s\n", $file['filename']);
}
}
}
}
// }}}
// {{{ do_extract()
function do_extract(&$tar, $destdir = ".")
{
$tar->extract($destdir);
}
// }}}
// {{{ usage()
function usage($errormsg = '')
{
global $progname;
$fp = fopen("php://stderr", "w");
if ($errormsg) {
if (PEAR::isError($errormsg)) {
fwrite($fp, $errormsg->getMessage() . "\n");
} else {
fwrite($fp, "$errormsg\n");
}
}
fwrite($fp, "$progname [-h|-?] {-c|-t|-x} [-z] [-v] [-f file] [file(s)...]
Options:
-h, -? Show this screen
-c Create archive
-t List archive
-x Extract archive
-z Run input/output through gzip
-f file Use <file> as input or output (default is stdin/stdout)
");
fclose($fp);
exit;
}
// }}}
?>
|