From b6a353107552e33aec01f9885362d91af0dad21c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 6 Mar 2015 13:32:51 +0100 Subject: [PATCH] better handling of sudo output --- bin/pharext | Bin 34353 -> 36120 bytes src/pharext/Installer.php | 155 +++++++++++++++++++++++++++----------- 2 files changed, 112 insertions(+), 43 deletions(-) diff --git a/bin/pharext b/bin/pharext index 07bdaa54e897ef422c3cefe2a12a344b24ceb743..30306c5d052ca0d58d83ceaa3ade2553823a96cd 100755 GIT binary patch delta 2524 zcmZuyTWDNG7|zAk?pA7Q)0!rmIys!QXVcAI8rv9ism6wCwW-!Z!E_DBv$MNn&aE@E zxi#ye^hFS9GcQ43^reV`SQf#{Lt7sN5v-u#Qy;_^QD1x!iirL*b53)?kYvvM^Z)<+ z-}hhU`de*l@3eh#rMqqAnpo`~l*_H|V{*CMo$2UJwzRBVyL(Qq#iR5$akQteG_T=R5PrXB_fsT(K(gt$YFeBAZq~geeW^Yz6aK*Jj*G&lnzN%&uUgDBchEY*Sid_b}S7%XX&V z@VWYQSA&C@)UZr_oI$2Ogz)^D>uL}iq3Poo-MV(oCl^V{ibG3K%xly~ETqKeWeecv z`dPN085zWum_p6*BVITQp;~FUcS``$QC*zLUeBDXVkG0#@YaznV8p6H%`KrI#MI^( z5nqILG{C(OjFXAnz(6vA29R{H$;<)7s7wP-s<%4}C2_m@TJvIVs(s&6F1Gzzs4iSL z5?7}mJ~%sD!i>|J1F2HEHF=e=!3js3DCqU1tcT{EBgflCX<~6dh@)~qq2Le?knM0XPb6&|_`!=D`-}uWkcgIo8bx6>%o3mU zLDi~UB-aUj9#in+A=_U>Bv_E#Ep*VsXg0hZv;>v%;W>~j??3}dn85MjK zI%vlAt`598olKBtP=5GmK7*hvz%=f79iVD&OXc_l6=*_bc}PbMvB?&W5d#_(8~BvE zQINn3NFibHA?piHIP~k$1=}0 zAinePH%x*Ugd}K!NWrA*YI+Ib@?UUO=3{ zYl#MOaRd>n!-vrrD&*6Rs2L^G5F3F%=mfYagN!(V>jjyJpQ#JJdZLu3zFT#v{uiYX zWr0xXN)ZkBN6f1 sui8~(H78eoNTjY@_-Oi)`{jWfzbE(hBu}>Ve!gb6wY0RHoIE}GAAHIUZvX%Q delta 810 zcma)3O-vI(7%c^j3o!wsgtP^wn+@zhDHJ13#9}0rp9IAej7G$=w$pa){<*vLC#5L_ zxDkSUF)@;G^X5U>s7Ew`v5Y2%nbMauKkt2 zo3Z^0LxGO^&Evlf6jyG(%fr=x$RtYvSNS{;VQeoDrrhyFHu*7C8ud%yxa~0!xFq5-e{CnL($Vjy}ndV)zkF<<+H5E5bB9Wd)%HlXdP~KtnyYh z%r6}A6Vud9w{JzVY!OfMDzPjnOZXWFFMj3%d*vSoH5>1L&aLiVj>Nuv*nXM#=Jj|y JBZKD$e*p|@3XuQ+ diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index 626cfa6..57bbf06 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -11,6 +11,18 @@ class Installer implements Command { use CliCommand; + /** + * The temporary directory we should operate in + * @var string + */ + private $tmp; + + /** + * The directory we came from + * @var string + */ + private $cwd; + /** * Create the command */ @@ -35,6 +47,13 @@ class Installer implements Command ]); } + /** + * Cleanup temp directory + */ + public function __destruct() { + $this->cleanup(); + } + /** * @inheritdoc * @see \pharext\Command::run() @@ -84,33 +103,38 @@ class Installer implements Command } /** - * @inheritdoc - * @see \pharext\Command::getArgs() - */ - public function getArgs() { - return $this->args; - } - - /** - * @inheritdoc - * @see \pharext\Command::info() + * Prepares, configures, builds and installs the extension */ - public function info($fmt) { - if (!$this->args->quiet) { - vprintf($fmt, array_slice(func_get_args(), 1)); + private function installPackage() { + $this->extract(); + + if (!chdir($this->tmp)) { + $this->error(null); + exit(4); } + + $this->exec("phpize", $this->php("ize")); + $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ". + implode(" ", (array) $this->args->configure)); + $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3"); + $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true); + + $this->cleanup(); + + $this->info("\nDon't forget to activiate the extension in your php.ini!\n"); } - + /** - * @inheritdoc - * @see \pharext\Command::error() + * Perform any cleanups */ - public function error($fmt) { - if (!$this->args->quiet) { - vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1)); + private function cleanup() { + if (is_dir($this->tmp)) { + chdir($this->cwd); + $this->info("Cleaning up %s ...\n", $this->tmp); + $this->rm($this->tmp); } } - + /** * Extract the phar to a temporary directory */ @@ -119,13 +143,73 @@ class Installer implements Command $this->error("Did your run the ext.phar?\n"); exit(3); } - $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar"); - is_dir($temp) or mkdir($temp, 0750, true); - $phar = new Phar($file); - $phar->extractTo($temp, null, true); - chdir($temp); + + $temp = $this->tempname(basename($file)); + if (!is_dir($temp)) { + if (!mkdir($temp, 0750, true)) { + $this->error(null); + exit(3); + } + } + $this->tmp = $temp; + $this->cwd = getcwd(); + + try { + $phar = new Phar($file); + $phar->extractTo($temp, null, true); + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + exit(3); + } + } + + /** + * rm -r + * @param string $dir + */ + private function rm($dir) { + foreach (scandir($dir) as $entry) { + if ($entry === "." || $entry === "..") { + continue; + } elseif (is_dir("$dir/$entry")) { + $this->rm("$dir/$entry"); + } elseif (!unlink("$dir/$entry")) { + $this->error(null); + } + } + if (!rmdir($dir)) { + $this->error(null); + } } + /** + * Execute a program with escalated privileges handling interactive password prompt + * @param string $command + * @param string $output + * @return int + */ + private function sudo($command, &$output) { + if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) { + return -1; + } + $stdout = $pipes[1]; + $passwd = 0; + while (!feof($stdout)) { + $R = [$stdout]; $W = []; $E = []; + if (!stream_select($R, $W, $E, null)) { + continue; + } + $data = fread($stdout, 0x1000); + /* only check a few times */ + if ($passwd++ < 10) { + if (stristr($data, "password")) { + printf("\n%s", $data); + } + } + $output .= $data; + } + return proc_close($proc); + } /** * Execute a system command * @param string $name pretty name @@ -135,20 +219,17 @@ class Installer implements Command private function exec($name, $command, $sudo = false) { $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " "); if ($sudo && isset($this->args->sudo)) { - if (($proc = proc_open(sprintf($this->args->sudo, $command)." 2>&1", [STDIN,STDOUT,STDERR], $pipes))) { - $retval = proc_close($proc); - } else { - $retval = -1; - } + $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output); } elseif ($this->args->verbose) { passthru($command ." 2>&1", $retval); } else { exec($command ." 2>&1", $output, $retval); + $output = implode("\n", $output); } if ($retval) { $this->error("Command %s failed with (%s)\n", $command, $retval); if (isset($output) && !$this->args->quiet) { - printf("%s\n", implode("\n", $output)); + printf("%s\n", $output); } exit(2); } @@ -167,16 +248,4 @@ class Installer implements Command } return $cmd; } - - /** - * Prepares, configures, builds and installs the extension - */ - private function installPackage() { - $this->extract(); - $this->exec("phpize", $this->php("ize")); - $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ". - implode(" ", (array) $this->args->configure)); - $this->exec("make", "make -sj3"); - $this->exec("install", "make -s install", true); - } } -- 2.30.2