From f5403bdafaa8d5713841d44edf5b62799265a602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Tue, 16 Oct 2018 12:19:45 +0200 Subject: [PATCH] Fixed dumping the PHAR contents and restoring the file limit (#306) - The file descriptor limit was being restored although it had not been changed - The dumped content of the PHAR for debugging purposes was the content directly extracted from the PHAR. However, if the PHAR was being compressed, it was not uncompressing the dumped content causing the dumped files to appear as binaries. The PHAR content is now dumped before the PHAR is being compressed --- src/Box.php | 2 -- src/Console/Command/Compile.php | 40 ++++++++++++++------------- tests/Console/Command/CompileTest.php | 33 ++++++++++++++++++++-- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/Box.php b/src/Box.php index 27e849c8f..a2dc63230 100644 --- a/src/Box.php +++ b/src/Box.php @@ -52,8 +52,6 @@ */ final class Box implements Countable { - public const DEBUG_DIR = '.box_dump'; - /** * @var string The path to the PHAR file */ diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index dcd14bffc..250a0c96b 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -239,12 +239,12 @@ private function createPhar( $this->checkComposerFiles($box, $config, $logger); - $this->configureCompressionAlgorithm($config, $box, $input->getOption(self::DEV_OPTION), $io, $logger); - if ($debug) { $box->getPhar()->extractTo(self::DEBUG_DIR, null, true); } + $this->configureCompressionAlgorithm($config, $box, $input->getOption(self::DEV_OPTION), $io, $logger); + $this->signPhar($config, $box, $config->getTmpOutputPath(), $input, $output, $logger); if ($config->getTmpOutputPath() !== $config->getOutputPath()) { @@ -649,25 +649,27 @@ private static function bumpOpenFileDescriptorLimit(Box $box, SymfonyStyle $io): $softLimit = posix_getrlimit()['soft openfiles']; $hardLimit = posix_getrlimit()['hard openfiles']; - if ($softLimit < $filesCount) { - $io->writeln( - sprintf( - '[debug] Increased the maximum number of open file descriptors from ("%s", "%s") to ("%s", "%s")' - .'', - $softLimit, - $hardLimit, - $filesCount, - 'unlimited' - ), - OutputInterface::VERBOSITY_DEBUG - ); + if ($softLimit >= $filesCount) { + return function (): void {}; + } - posix_setrlimit( - POSIX_RLIMIT_NOFILE, + $io->writeln( + sprintf( + '[debug] Increased the maximum number of open file descriptors from ("%s", "%s") to ("%s", "%s")' + .'', + $softLimit, + $hardLimit, $filesCount, - 'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit - ); - } + 'unlimited' + ), + OutputInterface::VERBOSITY_DEBUG + ); + + posix_setrlimit( + POSIX_RLIMIT_NOFILE, + $filesCount, + 'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit + ); return function () use ($io, $softLimit, $hardLimit): void { if (function_exists('posix_setrlimit') && isset($softLimit, $hardLimit)) { diff --git a/tests/Console/Command/CompileTest.php b/tests/Console/Command/CompileTest.php index 440f50bcd..a5807f87c 100644 --- a/tests/Console/Command/CompileTest.php +++ b/tests/Console/Command/CompileTest.php @@ -32,6 +32,7 @@ use function extension_loaded; use function file_get_contents; use function file_put_contents; +use function Humbug\get_contents; use function iterator_to_array; use function KevinGH\Box\FileSystem\dump_file; use function KevinGH\Box\FileSystem\file_contents; @@ -974,6 +975,9 @@ public function test_it_can_build_a_PHAR_file_in_very_verbose_mode(): void $this->assertSame($expected, $actual); } + /** + * @requires extension zlib + */ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void { mirror(self::FIXTURES_DIR.'/dir000', $this->tmp); @@ -1005,6 +1009,7 @@ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void 'metadata' => ['rand' => $rand = random_int(0, mt_getrandmax())], 'output' => 'test.phar', 'shebang' => $shebang, + 'compression' => 'GZ', ] ) ); @@ -1077,7 +1082,8 @@ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void ) ? Dumping the Composer autoloader ? Removing the Composer dump artefacts -? No compression +? Compressing with the algorithm "GZ" + > Warning: the extension "zlib" will now be required to execute the PHAR ? Signing using a private key Private key passphrase: ? Setting file permissions to 0754 @@ -1360,7 +1366,7 @@ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void ] } ] - -compressionAlgorithm: null + -compressionAlgorithm: 4096 -mainScriptPath: "/path/to/run.php" -mainScriptContents: """ assertSame($expectedDumpedConfig, $actualDumpedConfig); + + // Checks one of the dumped file from the PHAR to ensure the encoding of the extracted file is correct + $this->assertSame( + get_contents('.box_dump/run.php'), + <<<'PHP' +