PathDownloader.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Downloader;
  12. use Composer\Package\Archiver\ArchivableFilesFinder;
  13. use Composer\Package\Dumper\ArrayDumper;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Package\Version\VersionGuesser;
  16. use Composer\Package\Version\VersionParser;
  17. use Composer\Util\Platform;
  18. use Composer\Util\ProcessExecutor;
  19. use Composer\Util\Filesystem as ComposerFilesystem;
  20. use Symfony\Component\Filesystem\Exception\IOException;
  21. use Symfony\Component\Filesystem\Filesystem;
  22. /**
  23. * Download a package from a local path.
  24. *
  25. * @author Samuel Roze <samuel.roze@gmail.com>
  26. * @author Johann Reinke <johann.reinke@gmail.com>
  27. */
  28. class PathDownloader extends FileDownloader implements VcsCapableDownloaderInterface
  29. {
  30. const STRATEGY_SYMLINK = 10;
  31. const STRATEGY_MIRROR = 20;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function download(PackageInterface $package, $path, $output = true)
  36. {
  37. $url = $package->getDistUrl();
  38. $realUrl = realpath($url);
  39. if (false === $realUrl || !file_exists($realUrl) || !is_dir($realUrl)) {
  40. throw new \RuntimeException(sprintf(
  41. 'Source path "%s" is not found for package %s',
  42. $url,
  43. $package->getName()
  44. ));
  45. }
  46. if (strpos(realpath($path) . DIRECTORY_SEPARATOR, $realUrl . DIRECTORY_SEPARATOR) === 0) {
  47. // IMPORTANT NOTICE: If you wish to change this, don't. You are wasting your time and ours.
  48. //
  49. // Please see https://github.com/composer/composer/pull/5974 and https://github.com/composer/composer/pull/6174
  50. // for previous attempts that were shut down because they did not work well enough or introduced too many risks.
  51. throw new \RuntimeException(sprintf(
  52. 'Package %s cannot install to "%s" inside its source at "%s"',
  53. $package->getName(),
  54. realpath($path),
  55. $realUrl
  56. ));
  57. }
  58. // Get the transport options with default values
  59. $transportOptions = $package->getTransportOptions() + array('symlink' => null);
  60. // When symlink transport option is null, both symlink and mirror are allowed
  61. $currentStrategy = self::STRATEGY_SYMLINK;
  62. $allowedStrategies = array(self::STRATEGY_SYMLINK, self::STRATEGY_MIRROR);
  63. $mirrorPathRepos = getenv('COMPOSER_MIRROR_PATH_REPOS');
  64. if ($mirrorPathRepos) {
  65. $currentStrategy = self::STRATEGY_MIRROR;
  66. }
  67. if (true === $transportOptions['symlink']) {
  68. $currentStrategy = self::STRATEGY_SYMLINK;
  69. $allowedStrategies = array(self::STRATEGY_SYMLINK);
  70. } elseif (false === $transportOptions['symlink']) {
  71. $currentStrategy = self::STRATEGY_MIRROR;
  72. $allowedStrategies = array(self::STRATEGY_MIRROR);
  73. }
  74. // Check we can use junctions safely if we are on Windows
  75. if (Platform::isWindows() && self::STRATEGY_SYMLINK === $currentStrategy && !$this->safeJunctions()) {
  76. $currentStrategy = self::STRATEGY_MIRROR;
  77. $allowedStrategies = array(self::STRATEGY_MIRROR);
  78. }
  79. $fileSystem = new Filesystem();
  80. $this->filesystem->removeDirectory($path);
  81. if ($output) {
  82. $this->io->writeError(sprintf(
  83. ' - Installing <info>%s</info> (<comment>%s</comment>): ',
  84. $package->getName(),
  85. $package->getFullPrettyVersion()
  86. ), false);
  87. }
  88. $isFallback = false;
  89. if (self::STRATEGY_SYMLINK == $currentStrategy) {
  90. try {
  91. if (Platform::isWindows()) {
  92. // Implement symlinks as NTFS junctions on Windows
  93. $this->io->writeError(sprintf('Junctioning from %s', $url), false);
  94. $this->filesystem->junction($realUrl, $path);
  95. } else {
  96. $absolutePath = $path;
  97. if (!$this->filesystem->isAbsolutePath($absolutePath)) {
  98. $absolutePath = getcwd() . DIRECTORY_SEPARATOR . $path;
  99. }
  100. $shortestPath = $this->filesystem->findShortestPath($absolutePath, $realUrl);
  101. $path = rtrim($path, "/");
  102. $this->io->writeError(sprintf('Symlinking from %s', $url), false);
  103. $fileSystem->symlink($shortestPath, $path);
  104. }
  105. } catch (IOException $e) {
  106. if (in_array(self::STRATEGY_MIRROR, $allowedStrategies)) {
  107. $this->io->writeError('');
  108. $this->io->writeError(' <error>Symlink failed, fallback to use mirroring!</error>');
  109. $currentStrategy = self::STRATEGY_MIRROR;
  110. $isFallback = true;
  111. } else {
  112. throw new \RuntimeException(sprintf('Symlink from "%s" to "%s" failed!', $realUrl, $path));
  113. }
  114. }
  115. }
  116. // Fallback if symlink failed or if symlink is not allowed for the package
  117. if (self::STRATEGY_MIRROR == $currentStrategy) {
  118. $fs = new ComposerFilesystem();
  119. $realUrl = $fs->normalizePath($realUrl);
  120. $this->io->writeError(sprintf('%sMirroring from %s', $isFallback ? ' ' : '', $url), false);
  121. $iterator = new ArchivableFilesFinder($realUrl, array());
  122. $fileSystem->mirror($realUrl, $path, $iterator);
  123. }
  124. $this->io->writeError('');
  125. }
  126. /**
  127. * {@inheritDoc}
  128. */
  129. public function remove(PackageInterface $package, $path, $output = true)
  130. {
  131. /**
  132. * For junctions don't blindly rely on Filesystem::removeDirectory as it may be overzealous. If a process
  133. * inadvertently locks the file the removal will fail, but it would fall back to recursive delete which
  134. * is disastrous within a junction. So in that case we have no other real choice but to fail hard.
  135. */
  136. if (Platform::isWindows() && $this->filesystem->isJunction($path)) {
  137. if ($output) {
  138. $this->io->writeError(" - Removing junction for <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)");
  139. }
  140. if (!$this->filesystem->removeJunction($path)) {
  141. $this->io->writeError(" <warning>Could not remove junction at " . $path . " - is another process locking it?</warning>");
  142. throw new \RuntimeException('Could not reliably remove junction for package ' . $package->getName());
  143. }
  144. } else {
  145. parent::remove($package, $path, $output);
  146. }
  147. }
  148. /**
  149. * {@inheritDoc}
  150. */
  151. public function getVcsReference(PackageInterface $package, $path)
  152. {
  153. $parser = new VersionParser;
  154. $guesser = new VersionGuesser($this->config, new ProcessExecutor($this->io), $parser);
  155. $dumper = new ArrayDumper;
  156. $packageConfig = $dumper->dump($package);
  157. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  158. return $packageVersion['commit'];
  159. }
  160. }
  161. /**
  162. * Returns true if junctions can be safely used on Windows
  163. *
  164. * A PHP bug makes junction detection fragile, leading to possible data loss
  165. * when removing a package. See https://bugs.php.net/bug.php?id=77552
  166. *
  167. * For safety we require a minimum version of Windows 7, so we can call the
  168. * system rmdir which can detect junctions and not delete target content.
  169. *
  170. * @return bool
  171. */
  172. private function safeJunctions()
  173. {
  174. // Bug fixed in 7.3.3 and 7.2.16
  175. if (PHP_VERSION_ID >= 70303 || (PHP_VERSION_ID >= 70216 && PHP_VERSION_ID < 70300)) {
  176. return true;
  177. }
  178. // Windows 7 is version 6.1
  179. return function_exists('proc_open') &&
  180. (PHP_WINDOWS_VERSION_MAJOR > 6 ||
  181. (PHP_WINDOWS_VERSION_MAJOR === 6 && PHP_WINDOWS_VERSION_MINOR >= 1));
  182. }
  183. }