/** * {@inheritDoc} */ public function download(PackageInterface $package, $path) { parent::download($package, $path); $fileName = $this->getFileName($package, $path); $this->io->write(' Unpacking archive'); $this->extract($fileName, $path); $this->io->write(' Cleaning up'); unlink($fileName); // If we have only a one dir inside it suppose to be a package itself $contentDir = glob($path . '/*'); if (1 === count($contentDir)) { $contentDir = $contentDir[0]; // Rename the content directory to avoid error when moving up // a child folder with the same name $temporaryName = md5(time() . rand()); rename($contentDir, $temporaryName); $contentDir = $temporaryName; foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) { if (trim(basename($file), '.')) { rename($file, $path . '/' . basename($file)); } } rmdir($contentDir); } $this->io->write(''); }
/** * {@inheritDoc} */ public function download(PackageInterface $package, $path) { $temporaryDir = $this->config->get('vendor-dir') . '/composer/' . substr(md5(uniqid('', true)), 0, 8); $retries = 3; while ($retries--) { $fileName = parent::download($package, $path); if ($this->io->isVerbose()) { $this->io->writeError(' Extracting archive'); } try { $this->filesystem->ensureDirectoryExists($temporaryDir); try { $this->extract($fileName, $temporaryDir); } catch (\Exception $e) { // remove cache if the file was corrupted parent::clearCache($package, $path); throw $e; } $this->filesystem->unlink($fileName); $contentDir = $this->getFolderContent($temporaryDir); // only one dir in the archive, extract its contents out of it if (1 === count($contentDir) && is_dir(reset($contentDir))) { $contentDir = $this->getFolderContent((string) reset($contentDir)); } // move files back out of the temp dir foreach ($contentDir as $file) { $file = (string) $file; $this->filesystem->rename($file, $path . '/' . basename($file)); } $this->filesystem->removeDirectory($temporaryDir); if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir') . '/composer/')) { $this->filesystem->removeDirectory($this->config->get('vendor-dir') . '/composer/'); } if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir'))) { $this->filesystem->removeDirectory($this->config->get('vendor-dir')); } } catch (\Exception $e) { // clean up $this->filesystem->removeDirectory($path); $this->filesystem->removeDirectory($temporaryDir); // retry downloading if we have an invalid zip file if ($retries && $e instanceof \UnexpectedValueException && class_exists('ZipArchive') && $e->getCode() === \ZipArchive::ER_NOZIP) { if ($this->io->isDebug()) { $this->io->writeError(' Invalid zip file (' . $e->getMessage() . '), retrying...'); } else { $this->io->writeError(' Invalid zip file, retrying...'); } usleep(500000); continue; } throw $e; } break; } $this->io->writeError(''); }
/** * {@inheritDoc} */ public function download(PackageInterface $package, $path) { parent::download($package, $path); $fileName = $this->getFileName($package, $path); if ($this->io->isVerbose()) { $this->io->write(' Installing PEAR package'); } try { $pearExtractor = new PearPackageExtractor($fileName); $pearExtractor->extractTo($path); if ($this->io->isVerbose()) { $this->io->write(' Cleaning up'); } unlink($fileName); } catch (\Exception $e) { // clean up $this->filesystem->removeDirectory($path); throw $e; } $this->io->write(''); }
public function download(PackageInterface $package, $path) { $temporaryDir = $this->config->get('vendor-dir') . '/composer/' . substr(md5(uniqid('', true)), 0, 8); $retries = 3; while ($retries--) { $fileName = parent::download($package, $path); if ($this->io->isVerbose()) { $this->io->write(' Extracting archive'); } try { $this->filesystem->ensureDirectoryExists($temporaryDir); try { $this->extract($fileName, $temporaryDir); } catch (\Exception $e) { parent::clearCache($package, $path); throw $e; } unlink($fileName); $contentDir = $this->listFiles($temporaryDir); if (1 === count($contentDir) && !is_file($contentDir[0])) { $contentDir = $this->listFiles($contentDir[0]); } foreach ($contentDir as $file) { $this->filesystem->rename($file, $path . '/' . basename($file)); } $this->filesystem->removeDirectory($temporaryDir); } catch (\Exception $e) { $this->filesystem->removeDirectory($path); $this->filesystem->removeDirectory($temporaryDir); if ($retries && $e instanceof \UnexpectedValueException && class_exists('ZipArchive') && $e->getCode() === \ZipArchive::ER_NOZIP) { $this->io->write(' Invalid zip file, retrying...'); usleep(500000); continue; } throw $e; } break; } $this->io->write(''); }
/** * {@inheritDoc} */ public function download(PackageInterface $package, $path) { parent::download($package, $path); $fileName = $this->getFileName($package, $path); if ($this->io->isVerbose()) { $this->io->write(' Unpacking archive'); } try { $this->extract($fileName, $path); if ($this->io->isVerbose()) { $this->io->write(' Cleaning up'); } unlink($fileName); // If we have only a one dir inside it suppose to be a package itself $contentDir = glob($path . '/*'); if (1 === count($contentDir)) { $contentDir = $contentDir[0]; if (is_file($contentDir)) { $this->filesystem->rename($contentDir, $path . '/' . basename($contentDir)); } else { // Rename the content directory to avoid error when moving up // a child folder with the same name $temporaryDir = sys_get_temp_dir() . '/' . md5(time() . rand()); $this->filesystem->rename($contentDir, $temporaryDir); $contentDir = $temporaryDir; foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) { if (trim(basename($file), '.')) { $this->filesystem->rename($file, $path . '/' . basename($file)); } } $this->filesystem->removeDirectory($contentDir); } } } catch (\Exception $e) { // clean up $this->filesystem->removeDirectory($path); throw $e; } $this->io->write(''); }
/** * {@inheritDoc} */ public function download(PackageInterface $package, $path) { parent::download($package, $path); $fileName = $this->getFileName($package, $path); if ($this->io->isVerbose()) { $this->io->write(' Extracting archive'); } $temporaryDir = sys_get_temp_dir() . '/cmp' . substr(md5(time() . mt_rand()), 0, 5); try { $this->filesystem->ensureDirectoryExists($temporaryDir); try { $this->extract($fileName, $temporaryDir); } catch (\Exception $e) { // remove cache if the file was corrupted parent::clearCache($package, $path); throw $e; } unlink($fileName); // get file list $contentDir = $this->listFiles($temporaryDir); // only one dir in the archive, extract its contents out of it if (1 === count($contentDir) && !is_file($contentDir[0])) { $contentDir = $this->listFiles($contentDir[0]); } // move files back out of the temp dir foreach ($contentDir as $file) { $this->filesystem->rename($file, $path . '/' . basename($file)); } $this->filesystem->removeDirectory($temporaryDir); } catch (\Exception $e) { // clean up $this->filesystem->removeDirectory($path); $this->filesystem->removeDirectory($temporaryDir); throw $e; } $this->io->write(''); }
public function testDownloadFileWithInvalidChecksum() { $packageMock = $this->getMock('Composer\\Package\\PackageInterface'); $packageMock->expects($this->any())->method('getDistUrl')->will($this->returnValue('http://example.com/script.js')); $packageMock->expects($this->any())->method('getDistSha1Checksum')->will($this->returnValue('invalid')); do { $path = sys_get_temp_dir() . '/' . md5(time() . rand()); } while (file_exists($path)); $downloader = new FileDownloader($this->getMock('Composer\\IO\\IOInterface')); try { $downloader->download($packageMock, $path); $this->fail(); } catch (\Exception $e) { if (is_dir($path)) { $fs = new Filesystem(); $fs->removeDirectory($path); } else { if (is_file($path)) { unset($path); } } $this->assertInstanceOf('UnexpectedValueException', $e); $this->assertContains('checksum verification', $e->getMessage()); } }