phptar.in 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!@prefix@/bin/php -Cq
  2. <?php // -*- PHP -*-
  3. // {{{ setup
  4. define('S_IFDIR', 0040000); // Directory
  5. define('S_IFCHR', 0020000); // Character device
  6. define('S_IFBLK', 0060000); // Block device
  7. define('S_IFREG', 0100000); // Regular file
  8. define('S_IFIFO', 0010000); // FIFO
  9. define('S_IFLNK', 0120000); // Symbolic link
  10. define('S_IFSOCK', 0140000); // Socket
  11. require_once "PEAR.php";
  12. require_once "Archive/Tar.php";
  13. require_once "Console/Getopt.php";
  14. // }}}
  15. // {{{ options
  16. $verbose = false;
  17. $op_create = false;
  18. $op_list = false;
  19. $op_extract = false;
  20. $use_gzip = false;
  21. $file = '';
  22. $progname = basename(array_shift($argv));
  23. $options = Console_Getopt::getopt($argv, "h?ctxvzf:");
  24. if (PEAR::isError($options)) {
  25. usage($options);
  26. }
  27. $opts = $options[0];
  28. foreach ($opts as $opt) {
  29. switch ($opt[0]) {
  30. case 'v': {
  31. $verbose = true;
  32. break;
  33. }
  34. case 'c': {
  35. $op_create = true;
  36. break;
  37. }
  38. case 't': {
  39. $op_list = true;
  40. break;
  41. }
  42. case 'x': {
  43. $op_extract = true;
  44. break;
  45. }
  46. case 'z': {
  47. $use_gzip = true;
  48. break;
  49. }
  50. case 'f': {
  51. $file = $opt[1];
  52. break;
  53. }
  54. case 'h':
  55. case '?': {
  56. usage();
  57. break;
  58. }
  59. }
  60. }
  61. if ($op_create + $op_list + $op_extract > 1) {
  62. usage("Only one of -c, -t and -x can be specified at once!");
  63. }
  64. if ($op_create + $op_list + $op_extract == 0) {
  65. usage("Please specify either -c, -t or -x!");
  66. }
  67. if (empty($file)) {
  68. if ($op_create) {
  69. $file = "php://stdout";
  70. } else {
  71. $file = "php://stdin";
  72. }
  73. }
  74. // }}}
  75. $tar = new Archive_Tar($file, $use_gzip);
  76. $tar->setErrorHandling(PEAR_ERROR_DIE, "$progname error: %s\n");
  77. if ($op_create) {
  78. do_create($tar, $options[1]);
  79. $tar->create($options[1]);
  80. } elseif ($op_list) {
  81. do_list($tar, $verbose);
  82. } elseif ($op_extract) {
  83. do_extract($tar);
  84. }
  85. // {{{ getrwx()
  86. function getrwx($bits) {
  87. $str = '';
  88. $str .= ($bits & 4) ? 'r' : '-';
  89. $str .= ($bits & 2) ? 'w' : '-';
  90. $str .= ($bits & 1) ? 'x' : '-';
  91. return $str;
  92. }
  93. // }}}
  94. // {{{ getfiletype()
  95. function getfiletype($bits) {
  96. static $map = array(
  97. '-' => S_IFREG,
  98. 'd' => S_IFDIR,
  99. 'l' => S_IFLNK,
  100. 'c' => S_IFCHR,
  101. 'b' => S_IFBLK,
  102. 'p' => S_IFIFO,
  103. 's' => S_IFSOCK,
  104. );
  105. foreach ($map as $char => $mask) {
  106. if ($bits & $mask) {
  107. return $char;
  108. }
  109. }
  110. }
  111. // }}}
  112. // {{{ getuser()
  113. function getuser($uid) {
  114. static $cache = array();
  115. if (isset($cache[$uid])) {
  116. return $cache[$uid];
  117. }
  118. if (function_exists("posix_getpwuid")) {
  119. if (is_array($user = @posix_getpwuid($uid))) {
  120. $cache[$uid] = $user['name'];
  121. return $user['name'];
  122. }
  123. }
  124. $cache[$uid] = $uid;
  125. return $uid;
  126. }
  127. // }}}
  128. // {{{ getgroup()
  129. function getgroup($gid) {
  130. static $cache = array();
  131. if (isset($cache[$gid])) {
  132. return $cache[$gid];
  133. }
  134. if (function_exists("posix_getgrgid")) {
  135. if (is_array($group = @posix_getgrgid($gid))) {
  136. $cache[$gid] = $group['name'];
  137. return $group['name'];
  138. }
  139. }
  140. $cache[$gid] = $gid;
  141. return $gid;
  142. }
  143. // }}}
  144. // {{{ do_create()
  145. function do_create(&$tar, &$files)
  146. {
  147. $tar->create($files);
  148. }
  149. // }}}
  150. // {{{ do_list()
  151. function do_list(&$tar, $verbose)
  152. {
  153. static $rwx = array(4 => 'r', 2 => 'w', 1 => 'x');
  154. $files = $tar->listContent();
  155. if (is_array($files) && sizeof($files) > 0) {
  156. foreach ($files as $file) {
  157. if ($verbose) {
  158. $fm = (int)$file['mode'];
  159. $mode = sprintf('%s%s%s%s', getfiletype($fm),
  160. getrwx(($fm >> 6) & 7), getrwx(($fm >> 3) & 7),
  161. getrwx($fm & 7));
  162. $owner = getuser($file['uid']) . '/' . getgroup($file['gid']);
  163. printf("%10s %-11s %7d %s %s\n", $mode, $owner, $file['size'],
  164. date('Y-m-d H:i:s', $file['mtime']), $file['filename']);
  165. } else {
  166. printf("%s\n", $file['filename']);
  167. }
  168. }
  169. }
  170. }
  171. // }}}
  172. // {{{ do_extract()
  173. function do_extract(&$tar, $destdir = ".")
  174. {
  175. $tar->extract($destdir);
  176. }
  177. // }}}
  178. // {{{ usage()
  179. function usage($errormsg = '')
  180. {
  181. global $progname;
  182. $fp = fopen("php://stderr", "w");
  183. if ($errormsg) {
  184. if (PEAR::isError($errormsg)) {
  185. fwrite($fp, $errormsg->getMessage() . "\n");
  186. } else {
  187. fwrite($fp, "$errormsg\n");
  188. }
  189. }
  190. fwrite($fp, "$progname [-h|-?] {-c|-t|-x} [-z] [-v] [-f file] [file(s)...]
  191. Options:
  192. -h, -? Show this screen
  193. -c Create archive
  194. -t List archive
  195. -x Extract archive
  196. -z Run input/output through gzip
  197. -f file Use <file> as input or output (default is stdin/stdout)
  198. ");
  199. fclose($fp);
  200. exit;
  201. }
  202. // }}}
  203. ?>