PathDownloader.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 (realpath($path) === $realUrl) {
  47. if ($output) {
  48. $this->io->writeError(sprintf(
  49. ' - Installing <info>%s</info> (<comment>%s</comment>): Source already present',
  50. $package->getName(),
  51. $package->getFullPrettyVersion()
  52. ));
  53. }
  54. return;
  55. }
  56. if (strpos(realpath($path) . DIRECTORY_SEPARATOR, $realUrl . DIRECTORY_SEPARATOR) === 0) {
  57. // IMPORTANT NOTICE: If you wish to change this, don't. You are wasting your time and ours.
  58. //
  59. // Please see https://github.com/composer/composer/pull/5974 and https://github.com/composer/composer/pull/6174
  60. // for previous attempts that were shut down because they did not work well enough or introduced too many risks.
  61. throw new \RuntimeException(sprintf(
  62. 'Package %s cannot install to "%s" inside its source at "%s"',
  63. $package->getName(),
  64. realpath($path),
  65. $realUrl
  66. ));
  67. }
  68. // Get the transport options with default values
  69. $transportOptions = $package->getTransportOptions() + array('symlink' => null, 'relative' => true);
  70. // When symlink transport option is null, both symlink and mirror are allowed
  71. $currentStrategy = self::STRATEGY_SYMLINK;
  72. $allowedStrategies = array(self::STRATEGY_SYMLINK, self::STRATEGY_MIRROR);
  73. $mirrorPathRepos = getenv('COMPOSER_MIRROR_PATH_REPOS');
  74. if ($mirrorPathRepos) {
  75. $currentStrategy = self::STRATEGY_MIRROR;
  76. }
  77. if (true === $transportOptions['symlink']) {
  78. $currentStrategy = self::STRATEGY_SYMLINK;
  79. $allowedStrategies = array(self::STRATEGY_SYMLINK);
  80. } elseif (false === $transportOptions['symlink']) {
  81. $currentStrategy = self::STRATEGY_MIRROR;
  82. $allowedStrategies = array(self::STRATEGY_MIRROR);
  83. }
  84. // Check we can use junctions safely if we are on Windows
  85. if (Platform::isWindows() && self::STRATEGY_SYMLINK === $currentStrategy && !$this->safeJunctions()) {
  86. $currentStrategy = self::STRATEGY_MIRROR;
  87. $allowedStrategies = array(self::STRATEGY_MIRROR);
  88. }
  89. $fileSystem = new Filesystem();
  90. $this->filesystem->removeDirectory($path);
  91. if ($output) {
  92. $this->io->writeError(sprintf(
  93. ' - Installing <info>%s</info> (<comment>%s</comment>): ',
  94. $package->getName(),
  95. $package->getFullPrettyVersion()
  96. ), false);
  97. }
  98. $isFallback = false;
  99. if (self::STRATEGY_SYMLINK == $currentStrategy) {
  100. try {
  101. if (Platform::isWindows()) {
  102. // Implement symlinks as NTFS junctions on Windows
  103. $this->io->writeError(sprintf('Junctioning from %s', $url), false);
  104. $this->filesystem->junction($realUrl, $path);
  105. } else {
  106. $absolutePath = $path;
  107. if (!$this->filesystem->isAbsolutePath($absolutePath)) {
  108. $absolutePath = getcwd() . DIRECTORY_SEPARATOR . $path;
  109. }
  110. $shortestPath = $this->filesystem->findShortestPath($absolutePath, $realUrl);
  111. $path = rtrim($path, "/");
  112. $this->io->writeError(sprintf('Symlinking from %s', $url), false);
  113. if ($transportOptions['relative']) {
  114. $fileSystem->symlink($shortestPath, $path);
  115. } else {
  116. $fileSystem->symlink($realUrl, $path);
  117. }
  118. }
  119. } catch (IOException $e) {
  120. if (in_array(self::STRATEGY_MIRROR, $allowedStrategies)) {
  121. $this->io->writeError('');
  122. $this->io->writeError(' <error>Symlink failed, fallback to use mirroring!</error>');
  123. $currentStrategy = self::STRATEGY_MIRROR;
  124. $isFallback = true;
  125. } else {
  126. throw new \RuntimeException(sprintf('Symlink from "%s" to "%s" failed!', $realUrl, $path));
  127. }
  128. }
  129. }
  130. // Fallback if symlink failed or if symlink is not allowed for the package
  131. if (self::STRATEGY_MIRROR == $currentStrategy) {
  132. $fs = new ComposerFilesystem();
  133. $realUrl = $fs->normalizePath($realUrl);
  134. $this->io->writeError(sprintf('%sMirroring from %s', $isFallback ? ' ' : '', $url), false);
  135. $iterator = new ArchivableFilesFinder($realUrl, array());
  136. $fileSystem->mirror($realUrl, $path, $iterator);
  137. }
  138. $this->io->writeError('');
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function remove(PackageInterface $package, $path, $output = true)
  144. {
  145. $realUrl = realpath($package->getDistUrl());
  146. if (realpath($path) === $realUrl) {
  147. if ($output) {
  148. $this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>), source is still present in $path");
  149. }
  150. return;
  151. }
  152. /**
  153. * For junctions don't blindly rely on Filesystem::removeDirectory as it may be overzealous. If a process
  154. * inadvertently locks the file the removal will fail, but it would fall back to recursive delete which
  155. * is disastrous within a junction. So in that case we have no other real choice but to fail hard.
  156. */
  157. if (Platform::isWindows() && $this->filesystem->isJunction($path)) {
  158. if ($output) {
  159. $this->io->writeError(" - Removing junction for <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)");
  160. }
  161. if (!$this->filesystem->removeJunction($path)) {
  162. $this->io->writeError(" <warning>Could not remove junction at " . $path . " - is another process locking it?</warning>");
  163. throw new \RuntimeException('Could not reliably remove junction for package ' . $package->getName());
  164. }
  165. } else {
  166. parent::remove($package, $path, $output);
  167. }
  168. }
  169. /**
  170. * {@inheritDoc}
  171. */
  172. public function getVcsReference(PackageInterface $package, $path)
  173. {
  174. $parser = new VersionParser;
  175. $guesser = new VersionGuesser($this->config, new ProcessExecutor($this->io), $parser);
  176. $dumper = new ArrayDumper;
  177. $packageConfig = $dumper->dump($package);
  178. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  179. return $packageVersion['commit'];
  180. }
  181. }
  182. /**
  183. * Returns true if junctions can be created and safely used on Windows
  184. *
  185. * A PHP bug makes junction detection fragile, leading to possible data loss
  186. * when removing a package. See https://bugs.php.net/bug.php?id=77552
  187. *
  188. * For safety we require a minimum version of Windows 7, so we can call the
  189. * system rmdir which will preserve target content if given a junction.
  190. *
  191. * The PHP bug was fixed in 7.2.16 and 7.3.3 (requires at least Windows 7).
  192. *
  193. * @return bool
  194. */
  195. private function safeJunctions()
  196. {
  197. // We need to call mklink, and rmdir on Windows 7 (version 6.1)
  198. return function_exists('proc_open') &&
  199. (PHP_WINDOWS_VERSION_MAJOR > 6 ||
  200. (PHP_WINDOWS_VERSION_MAJOR === 6 && PHP_WINDOWS_VERSION_MINOR >= 1));
  201. }
  202. }