From effacc1185858308a45056b1c11c135490064541 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 31 May 2014 21:36:09 +0200 Subject: [PATCH] Do not realpath relative local URLs, fixes #2916 --- src/Composer/Downloader/VcsDownloader.php | 6 ++++++ src/Composer/Repository/Vcs/GitDriver.php | 11 +++++++---- src/Composer/Repository/Vcs/HgDriver.php | 6 +++--- src/Composer/Repository/Vcs/SvnDriver.php | 2 +- src/Composer/Repository/Vcs/VcsDriver.php | 19 +++---------------- src/Composer/Util/Filesystem.php | 11 +++++++++++ 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php index bff9201f7..55a12954e 100644 --- a/src/Composer/Downloader/VcsDownloader.php +++ b/src/Composer/Downloader/VcsDownloader.php @@ -60,6 +60,9 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa $urls = $package->getSourceUrls(); while ($url = array_shift($urls)) { try { + if (Filesystem::isLocalPath($url)) { + $url = realpath($url); + } $this->doDownload($package, $path, $url); break; } catch (\Exception $e) { @@ -107,6 +110,9 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa $urls = $target->getSourceUrls(); while ($url = array_shift($urls)) { try { + if (Filesystem::isLocalPath($url)) { + $url = realpath($url); + } $this->doUpdate($initial, $target, $path, $url); break; } catch (\Exception $e) { diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index 422868442..0e6286906 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -37,8 +37,9 @@ class GitDriver extends VcsDriver */ public function initialize() { - if (static::isLocalUrl($this->url)) { - $this->repoDir = str_replace('file://', '', $this->url); + if (Filesystem::isLocalPath($this->url)) { + $this->repoDir = $this->url; + $cacheUrl = realpath($this->url); } else { $this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/'; @@ -72,12 +73,14 @@ class GitDriver extends VcsDriver $gitUtil->runCommand($commandCallable, $this->url, $this->repoDir, true); } + + $cacheUrl = $this->url; } $this->getTags(); $this->getBranches(); - $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url)); + $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl)); } /** @@ -215,7 +218,7 @@ class GitDriver extends VcsDriver } // local filesystem - if (static::isLocalUrl($url)) { + if (Filesystem::isLocalPath($url)) { if (!is_dir($url)) { throw new \RuntimeException('Directory does not exist: '.$url); } diff --git a/src/Composer/Repository/Vcs/HgDriver.php b/src/Composer/Repository/Vcs/HgDriver.php index 060a77e45..7b9d1ff8c 100644 --- a/src/Composer/Repository/Vcs/HgDriver.php +++ b/src/Composer/Repository/Vcs/HgDriver.php @@ -34,8 +34,8 @@ class HgDriver extends VcsDriver */ public function initialize() { - if (static::isLocalUrl($this->url)) { - $this->repoDir = str_replace('file://', '', $this->url); + if (Filesystem::isLocalPath($this->url)) { + $this->repoDir = $this->url; } else { $cacheDir = $this->config->get('cache-vcs-dir'); $this->repoDir = $cacheDir . '/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/'; @@ -197,7 +197,7 @@ class HgDriver extends VcsDriver } // local filesystem - if (static::isLocalUrl($url)) { + if (Filesystem::isLocalPath($url)) { if (!is_dir($url)) { throw new \RuntimeException('Directory does not exist: '.$url); } diff --git a/src/Composer/Repository/Vcs/SvnDriver.php b/src/Composer/Repository/Vcs/SvnDriver.php index 506c176b8..4f8e07f20 100644 --- a/src/Composer/Repository/Vcs/SvnDriver.php +++ b/src/Composer/Repository/Vcs/SvnDriver.php @@ -252,7 +252,7 @@ class SvnDriver extends VcsDriver } // proceed with deep check for local urls since they are fast to process - if (!$deep && !static::isLocalUrl($url)) { + if (!$deep && !Filesystem::isLocalPath($url)) { return false; } diff --git a/src/Composer/Repository/Vcs/VcsDriver.php b/src/Composer/Repository/Vcs/VcsDriver.php index 04956e816..262309e67 100644 --- a/src/Composer/Repository/Vcs/VcsDriver.php +++ b/src/Composer/Repository/Vcs/VcsDriver.php @@ -17,6 +17,7 @@ use Composer\Config; use Composer\IO\IOInterface; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; +use Composer\Util\Filesystem; /** * A driver implementation for driver with authentication interaction. @@ -44,11 +45,8 @@ abstract class VcsDriver implements VcsDriverInterface */ final public function __construct(array $repoConfig, IOInterface $io, Config $config, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null) { - - if (self::isLocalUrl($repoConfig['url'])) { - $repoConfig['url'] = realpath( - preg_replace('/^file:\/\//', '', $repoConfig['url']) - ); + if (Filesystem::isLocalPath($repoConfig['url'])) { + $repoConfig['url'] = preg_replace('{^file://}', '', $repoConfig['url']); } $this->url = $repoConfig['url']; @@ -101,17 +99,6 @@ abstract class VcsDriver implements VcsDriverInterface return $this->remoteFilesystem->getContents($this->originUrl, $url, false); } - /** - * Return if current repository url is local - * - * @param string $url - * @return boolean Repository url is local - */ - protected static function isLocalUrl($url) - { - return (bool) preg_match('{^(file://|/|[a-z]:[\\\\/])}i', $url); - } - /** * {@inheritDoc} */ diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index d88e00393..e8755645a 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -357,6 +357,17 @@ class Filesystem return $prefix.($absolute ? '/' : '').implode('/', $parts); } + /** + * Return if the given path is local + * + * @param string $path + * @return bool + */ + public static function isLocalPath($path) + { + return (bool) preg_match('{^(file://|/|[a-z]:[\\\\/]|\.\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i', $path); + } + protected function directorySize($directory) { $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);