PathDownloader.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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);
  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. $fileSystem->symlink($shortestPath, $path);
  114. }
  115. } catch (IOException $e) {
  116. if (in_array(self::STRATEGY_MIRROR, $allowedStrategies)) {
  117. $this->io->writeError('');
  118. $this->io->writeError(' <error>Symlink failed, fallback to use mirroring!</error>');
  119. $currentStrategy = self::STRATEGY_MIRROR;
  120. $isFallback = true;
  121. } else {
  122. throw new \RuntimeException(sprintf('Symlink from "%s" to "%s" failed!', $realUrl, $path));
  123. }
  124. }
  125. }
  126. // Fallback if symlink failed or if symlink is not allowed for the package
  127. if (self::STRATEGY_MIRROR == $currentStrategy) {
  128. $fs = new ComposerFilesystem();
  129. $realUrl = $fs->normalizePath($realUrl);
  130. $this->io->writeError(sprintf('%sMirroring from %s', $isFallback ? ' ' : '', $url), false);
  131. $iterator = new ArchivableFilesFinder($realUrl, array());
  132. $fileSystem->mirror($realUrl, $path, $iterator);
  133. }
  134. $this->io->writeError('');
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public function remove(PackageInterface $package, $path, $output = true)
  140. {
  141. $realUrl = realpath($package->getDistUrl());
  142. if (realpath($path) === $realUrl) {
  143. if ($output) {
  144. $this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>), source is still present in $path");
  145. }
  146. return;
  147. }
  148. /**
  149. * For junctions don't blindly rely on Filesystem::removeDirectory as it may be overzealous. If a process
  150. * inadvertently locks the file the removal will fail, but it would fall back to recursive delete which
  151. * is disastrous within a junction. So in that case we have no other real choice but to fail hard.
  152. */
  153. if (Platform::isWindows() && $this->filesystem->isJunction($path)) {
  154. if ($output) {
  155. $this->io->writeError(" - Removing junction for <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)");
  156. }
  157. if (!$this->filesystem->removeJunction($path)) {
  158. $this->io->writeError(" <warning>Could not remove junction at " . $path . " - is another process locking it?</warning>");
  159. throw new \RuntimeException('Could not reliably remove junction for package ' . $package->getName());
  160. }
  161. } else {
  162. parent::remove($package, $path, $output);
  163. }
  164. }
  165. /**
  166. * {@inheritDoc}
  167. */
  168. public function getVcsReference(PackageInterface $package, $path)
  169. {
  170. $parser = new VersionParser;
  171. $guesser = new VersionGuesser($this->config, new ProcessExecutor($this->io), $parser);
  172. $dumper = new ArrayDumper;
  173. $packageConfig = $dumper->dump($package);
  174. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  175. return $packageVersion['commit'];
  176. }
  177. }
  178. /**
  179. * Returns true if junctions can be created and safely used on Windows
  180. *
  181. * A PHP bug makes junction detection fragile, leading to possible data loss
  182. * when removing a package. See https://bugs.php.net/bug.php?id=77552
  183. *
  184. * For safety we require a minimum version of Windows 7, so we can call the
  185. * system rmdir which will preserve target content if given a junction.
  186. *
  187. * The PHP bug was fixed in 7.2.16 and 7.3.3 (requires at least Windows 7).
  188. *
  189. * @return bool
  190. */
  191. private function safeJunctions()
  192. {
  193. // We need to call mklink, and rmdir on Windows 7 (version 6.1)
  194. return function_exists('proc_open') &&
  195. (PHP_WINDOWS_VERSION_MAJOR > 6 ||
  196. (PHP_WINDOWS_VERSION_MAJOR === 6 && PHP_WINDOWS_VERSION_MINOR >= 1));
  197. }
  198. }