Filesystem.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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\Util;
  12. use RecursiveDirectoryIterator;
  13. use RecursiveIteratorIterator;
  14. use Symfony\Component\Filesystem\Exception\IOException;
  15. use Symfony\Component\Finder\Finder;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class Filesystem
  21. {
  22. private $processExecutor;
  23. public function __construct(ProcessExecutor $executor = null)
  24. {
  25. $this->processExecutor = $executor ?: new ProcessExecutor();
  26. }
  27. public function remove($file)
  28. {
  29. if (is_dir($file)) {
  30. return $this->removeDirectory($file);
  31. }
  32. if (file_exists($file)) {
  33. return $this->unlink($file);
  34. }
  35. return false;
  36. }
  37. /**
  38. * Checks if a directory is empty
  39. *
  40. * @param string $dir
  41. * @return bool
  42. */
  43. public function isDirEmpty($dir)
  44. {
  45. $finder = Finder::create()
  46. ->ignoreVCS(false)
  47. ->ignoreDotFiles(false)
  48. ->depth(0)
  49. ->in($dir);
  50. return count($finder) === 0;
  51. }
  52. public function emptyDirectory($dir, $ensureDirectoryExists = true)
  53. {
  54. if (file_exists($dir) && is_link($dir)) {
  55. $this->unlink($dir);
  56. }
  57. if ($ensureDirectoryExists) {
  58. $this->ensureDirectoryExists($dir);
  59. }
  60. if (is_dir($dir)) {
  61. $finder = Finder::create()
  62. ->ignoreVCS(false)
  63. ->ignoreDotFiles(false)
  64. ->depth(0)
  65. ->in($dir);
  66. foreach ($finder as $path) {
  67. $this->remove((string) $path);
  68. }
  69. }
  70. }
  71. /**
  72. * Recursively remove a directory
  73. *
  74. * Uses the process component if proc_open is enabled on the PHP
  75. * installation.
  76. *
  77. * @param string $directory
  78. * @throws \RuntimeException
  79. * @return bool
  80. */
  81. public function removeDirectory($directory)
  82. {
  83. if ($this->isSymlinkedDirectory($directory)) {
  84. return $this->unlinkSymlinkedDirectory($directory);
  85. }
  86. if ($this->isJunction($directory)) {
  87. return $this->removeJunction($directory);
  88. }
  89. if (!file_exists($directory) || !is_dir($directory)) {
  90. return true;
  91. }
  92. if (preg_match('{^(?:[a-z]:)?[/\\\\]+$}i', $directory)) {
  93. throw new \RuntimeException('Aborting an attempted deletion of '.$directory.', this was probably not intended, if it is a real use case please report it.');
  94. }
  95. if (!function_exists('proc_open')) {
  96. return $this->removeDirectoryPhp($directory);
  97. }
  98. if (Platform::isWindows()) {
  99. $cmd = sprintf('rmdir /S /Q %s', ProcessExecutor::escape(realpath($directory)));
  100. } else {
  101. $cmd = sprintf('rm -rf %s', ProcessExecutor::escape($directory));
  102. }
  103. $result = $this->getProcess()->execute($cmd, $output) === 0;
  104. // clear stat cache because external processes aren't tracked by the php stat cache
  105. clearstatcache();
  106. if ($result && !file_exists($directory)) {
  107. return true;
  108. }
  109. return $this->removeDirectoryPhp($directory);
  110. }
  111. /**
  112. * Recursively delete directory using PHP iterators.
  113. *
  114. * Uses a CHILD_FIRST RecursiveIteratorIterator to sort files
  115. * before directories, creating a single non-recursive loop
  116. * to delete files/directories in the correct order.
  117. *
  118. * @param string $directory
  119. * @return bool
  120. */
  121. public function removeDirectoryPhp($directory)
  122. {
  123. try {
  124. $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
  125. } catch (\UnexpectedValueException $e) {
  126. // re-try once after clearing the stat cache if it failed as it
  127. // sometimes fails without apparent reason, see https://github.com/composer/composer/issues/4009
  128. clearstatcache();
  129. usleep(100000);
  130. if (!is_dir($directory)) {
  131. return true;
  132. }
  133. $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
  134. }
  135. $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
  136. foreach ($ri as $file) {
  137. if ($file->isDir()) {
  138. $this->rmdir($file->getPathname());
  139. } else {
  140. $this->unlink($file->getPathname());
  141. }
  142. }
  143. return $this->rmdir($directory);
  144. }
  145. public function ensureDirectoryExists($directory)
  146. {
  147. if (!is_dir($directory)) {
  148. if (file_exists($directory)) {
  149. throw new \RuntimeException(
  150. $directory.' exists and is not a directory.'
  151. );
  152. }
  153. if (!@mkdir($directory, 0777, true)) {
  154. throw new \RuntimeException(
  155. $directory.' does not exist and could not be created.'
  156. );
  157. }
  158. }
  159. }
  160. /**
  161. * Attempts to unlink a file and in case of failure retries after 350ms on windows
  162. *
  163. * @param string $path
  164. * @throws \RuntimeException
  165. * @return bool
  166. */
  167. public function unlink($path)
  168. {
  169. if (!@$this->unlinkImplementation($path)) {
  170. // retry after a bit on windows since it tends to be touchy with mass removals
  171. if (!Platform::isWindows() || (usleep(350000) && !@$this->unlinkImplementation($path))) {
  172. $error = error_get_last();
  173. $message = 'Could not delete '.$path.': ' . @$error['message'];
  174. if (Platform::isWindows()) {
  175. $message .= "\nThis can be due to an antivirus or the Windows Search Indexer locking the file while they are analyzed";
  176. }
  177. throw new \RuntimeException($message);
  178. }
  179. }
  180. return true;
  181. }
  182. /**
  183. * Attempts to rmdir a file and in case of failure retries after 350ms on windows
  184. *
  185. * @param string $path
  186. * @throws \RuntimeException
  187. * @return bool
  188. */
  189. public function rmdir($path)
  190. {
  191. if (!@rmdir($path)) {
  192. // retry after a bit on windows since it tends to be touchy with mass removals
  193. if (!Platform::isWindows() || (usleep(350000) && !@rmdir($path))) {
  194. $error = error_get_last();
  195. $message = 'Could not delete '.$path.': ' . @$error['message'];
  196. if (Platform::isWindows()) {
  197. $message .= "\nThis can be due to an antivirus or the Windows Search Indexer locking the file while they are analyzed";
  198. }
  199. throw new \RuntimeException($message);
  200. }
  201. }
  202. return true;
  203. }
  204. /**
  205. * Copy then delete is a non-atomic version of {@link rename}.
  206. *
  207. * Some systems can't rename and also don't have proc_open,
  208. * which requires this solution.
  209. *
  210. * @param string $source
  211. * @param string $target
  212. */
  213. public function copyThenRemove($source, $target)
  214. {
  215. $this->copy($source, $target);
  216. if (!is_dir($source)) {
  217. $this->unlink($source);
  218. return;
  219. }
  220. $this->removeDirectoryPhp($source);
  221. }
  222. /**
  223. * Copies a file or directory from $source to $target.
  224. *
  225. * @param $source
  226. * @param $target
  227. * @return bool
  228. */
  229. public function copy($source, $target)
  230. {
  231. if (!is_dir($source)) {
  232. return copy($source, $target);
  233. }
  234. $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
  235. $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
  236. $this->ensureDirectoryExists($target);
  237. $result = true;
  238. foreach ($ri as $file) {
  239. $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
  240. if ($file->isDir()) {
  241. $this->ensureDirectoryExists($targetPath);
  242. } else {
  243. $result = $result && copy($file->getPathname(), $targetPath);
  244. }
  245. }
  246. return $result;
  247. }
  248. public function rename($source, $target)
  249. {
  250. if (true === @rename($source, $target)) {
  251. return;
  252. }
  253. if (!function_exists('proc_open')) {
  254. return $this->copyThenRemove($source, $target);
  255. }
  256. if (Platform::isWindows()) {
  257. // Try to copy & delete - this is a workaround for random "Access denied" errors.
  258. $command = sprintf('xcopy %s %s /E /I /Q /Y', ProcessExecutor::escape($source), ProcessExecutor::escape($target));
  259. $result = $this->processExecutor->execute($command, $output);
  260. // clear stat cache because external processes aren't tracked by the php stat cache
  261. clearstatcache();
  262. if (0 === $result) {
  263. $this->remove($source);
  264. return;
  265. }
  266. } else {
  267. // We do not use PHP's "rename" function here since it does not support
  268. // the case where $source, and $target are located on different partitions.
  269. $command = sprintf('mv %s %s', ProcessExecutor::escape($source), ProcessExecutor::escape($target));
  270. $result = $this->processExecutor->execute($command, $output);
  271. // clear stat cache because external processes aren't tracked by the php stat cache
  272. clearstatcache();
  273. if (0 === $result) {
  274. return;
  275. }
  276. }
  277. return $this->copyThenRemove($source, $target);
  278. }
  279. /**
  280. * Returns the shortest path from $from to $to
  281. *
  282. * @param string $from
  283. * @param string $to
  284. * @param bool $directories if true, the source/target are considered to be directories
  285. * @throws \InvalidArgumentException
  286. * @return string
  287. */
  288. public function findShortestPath($from, $to, $directories = false)
  289. {
  290. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  291. throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to));
  292. }
  293. $from = lcfirst($this->normalizePath($from));
  294. $to = lcfirst($this->normalizePath($to));
  295. if ($directories) {
  296. $from = rtrim($from, '/') . '/dummy_file';
  297. }
  298. if (dirname($from) === dirname($to)) {
  299. return './'.basename($to);
  300. }
  301. $commonPath = $to;
  302. while (strpos($from.'/', $commonPath.'/') !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath)) {
  303. $commonPath = strtr(dirname($commonPath), '\\', '/');
  304. }
  305. if (0 !== strpos($from, $commonPath) || '/' === $commonPath) {
  306. return $to;
  307. }
  308. $commonPath = rtrim($commonPath, '/') . '/';
  309. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
  310. $commonPathCode = str_repeat('../', $sourcePathDepth);
  311. return ($commonPathCode . substr($to, strlen($commonPath))) ?: './';
  312. }
  313. /**
  314. * Returns PHP code that, when executed in $from, will return the path to $to
  315. *
  316. * @param string $from
  317. * @param string $to
  318. * @param bool $directories if true, the source/target are considered to be directories
  319. * @param bool $staticCode
  320. * @throws \InvalidArgumentException
  321. * @return string
  322. */
  323. public function findShortestPathCode($from, $to, $directories = false, $staticCode = false)
  324. {
  325. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  326. throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to));
  327. }
  328. $from = lcfirst($this->normalizePath($from));
  329. $to = lcfirst($this->normalizePath($to));
  330. if ($from === $to) {
  331. return $directories ? '__DIR__' : '__FILE__';
  332. }
  333. $commonPath = $to;
  334. while (strpos($from.'/', $commonPath.'/') !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
  335. $commonPath = strtr(dirname($commonPath), '\\', '/');
  336. }
  337. if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
  338. return var_export($to, true);
  339. }
  340. $commonPath = rtrim($commonPath, '/') . '/';
  341. if (strpos($to, $from.'/') === 0) {
  342. return '__DIR__ . '.var_export(substr($to, strlen($from)), true);
  343. }
  344. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories;
  345. if ($staticCode) {
  346. $commonPathCode = "__DIR__ . '".str_repeat('/..', $sourcePathDepth)."'";
  347. } else {
  348. $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
  349. }
  350. $relTarget = substr($to, strlen($commonPath));
  351. return $commonPathCode . (strlen($relTarget) ? '.' . var_export('/' . $relTarget, true) : '');
  352. }
  353. /**
  354. * Checks if the given path is absolute
  355. *
  356. * @param string $path
  357. * @return bool
  358. */
  359. public function isAbsolutePath($path)
  360. {
  361. return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
  362. }
  363. /**
  364. * Returns size of a file or directory specified by path. If a directory is
  365. * given, it's size will be computed recursively.
  366. *
  367. * @param string $path Path to the file or directory
  368. * @throws \RuntimeException
  369. * @return int
  370. */
  371. public function size($path)
  372. {
  373. if (!file_exists($path)) {
  374. throw new \RuntimeException("$path does not exist.");
  375. }
  376. if (is_dir($path)) {
  377. return $this->directorySize($path);
  378. }
  379. return filesize($path);
  380. }
  381. /**
  382. * Normalize a path. This replaces backslashes with slashes, removes ending
  383. * slash and collapses redundant separators and up-level references.
  384. *
  385. * @param string $path Path to the file or directory
  386. * @return string
  387. */
  388. public function normalizePath($path)
  389. {
  390. $parts = array();
  391. $path = strtr($path, '\\', '/');
  392. $prefix = '';
  393. $absolute = false;
  394. // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
  395. if (preg_match('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) {
  396. $prefix = $match[1];
  397. $path = substr($path, strlen($prefix));
  398. }
  399. if (substr($path, 0, 1) === '/') {
  400. $absolute = true;
  401. $path = substr($path, 1);
  402. }
  403. $up = false;
  404. foreach (explode('/', $path) as $chunk) {
  405. if ('..' === $chunk && ($absolute || $up)) {
  406. array_pop($parts);
  407. $up = !(empty($parts) || '..' === end($parts));
  408. } elseif ('.' !== $chunk && '' !== $chunk) {
  409. $parts[] = $chunk;
  410. $up = '..' !== $chunk;
  411. }
  412. }
  413. return $prefix.($absolute ? '/' : '').implode('/', $parts);
  414. }
  415. /**
  416. * Return if the given path is local
  417. *
  418. * @param string $path
  419. * @return bool
  420. */
  421. public static function isLocalPath($path)
  422. {
  423. return (bool) preg_match('{^(file://(?!//)|/(?!/)|/?[a-z]:[\\\\/]|\.\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i', $path);
  424. }
  425. public static function getPlatformPath($path)
  426. {
  427. if (Platform::isWindows()) {
  428. $path = preg_replace('{^(?:file:///([a-z]):?/)}i', 'file://$1:/', $path);
  429. }
  430. return preg_replace('{^file://}i', '', $path);
  431. }
  432. protected function directorySize($directory)
  433. {
  434. $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
  435. $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
  436. $size = 0;
  437. foreach ($ri as $file) {
  438. if ($file->isFile()) {
  439. $size += $file->getSize();
  440. }
  441. }
  442. return $size;
  443. }
  444. protected function getProcess()
  445. {
  446. return new ProcessExecutor;
  447. }
  448. /**
  449. * delete symbolic link implementation (commonly known as "unlink()")
  450. *
  451. * symbolic links on windows which link to directories need rmdir instead of unlink
  452. *
  453. * @param string $path
  454. *
  455. * @return bool
  456. */
  457. private function unlinkImplementation($path)
  458. {
  459. if (Platform::isWindows() && is_dir($path) && is_link($path)) {
  460. return rmdir($path);
  461. }
  462. return unlink($path);
  463. }
  464. /**
  465. * Creates a relative symlink from $link to $target
  466. *
  467. * @param string $target The path of the binary file to be symlinked
  468. * @param string $link The path where the symlink should be created
  469. * @return bool
  470. */
  471. public function relativeSymlink($target, $link)
  472. {
  473. $cwd = getcwd();
  474. $relativePath = $this->findShortestPath($link, $target);
  475. chdir(dirname($link));
  476. $result = @symlink($relativePath, $link);
  477. chdir($cwd);
  478. return (bool) $result;
  479. }
  480. /**
  481. * return true if that directory is a symlink.
  482. *
  483. * @param string $directory
  484. *
  485. * @return bool
  486. */
  487. public function isSymlinkedDirectory($directory)
  488. {
  489. if (!is_dir($directory)) {
  490. return false;
  491. }
  492. $resolved = $this->resolveSymlinkedDirectorySymlink($directory);
  493. return is_link($resolved);
  494. }
  495. /**
  496. * @param string $directory
  497. *
  498. * @return bool
  499. */
  500. private function unlinkSymlinkedDirectory($directory)
  501. {
  502. $resolved = $this->resolveSymlinkedDirectorySymlink($directory);
  503. return $this->unlink($resolved);
  504. }
  505. /**
  506. * resolve pathname to symbolic link of a directory
  507. *
  508. * @param string $pathname directory path to resolve
  509. *
  510. * @return string resolved path to symbolic link or original pathname (unresolved)
  511. */
  512. private function resolveSymlinkedDirectorySymlink($pathname)
  513. {
  514. if (!is_dir($pathname)) {
  515. return $pathname;
  516. }
  517. $resolved = rtrim($pathname, '/');
  518. if (!strlen($resolved)) {
  519. return $pathname;
  520. }
  521. return $resolved;
  522. }
  523. /**
  524. * Creates an NTFS junction.
  525. *
  526. * @param string $target
  527. * @param string $junction
  528. */
  529. public function junction($target, $junction)
  530. {
  531. if (!Platform::isWindows()) {
  532. throw new \LogicException(sprintf('Function %s is not available on non-Windows platform', __CLASS__));
  533. }
  534. if (!is_dir($target)) {
  535. throw new IOException(sprintf('Cannot junction to "%s" as it is not a directory.', $target), 0, null, $target);
  536. }
  537. $cmd = sprintf('mklink /J %s %s',
  538. ProcessExecutor::escape(str_replace('/', DIRECTORY_SEPARATOR, $junction)),
  539. ProcessExecutor::escape(realpath($target)));
  540. if ($this->getProcess()->execute($cmd, $output) !== 0) {
  541. throw new IOException(sprintf('Failed to create junction to "%s" at "%s".', $target, $junction), 0, null, $target);
  542. }
  543. clearstatcache(true, $junction);
  544. }
  545. /**
  546. * Returns whether the target directory is a Windows NTFS Junction.
  547. *
  548. * @param string $junction Path to check.
  549. * @return bool
  550. */
  551. public function isJunction($junction)
  552. {
  553. if (!Platform::isWindows()) {
  554. return false;
  555. }
  556. if (!is_dir($junction) || is_link($junction)) {
  557. return false;
  558. }
  559. /**
  560. * According to MSDN at https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx we can detect a junction now
  561. * using the 'mode' value from stat: "The _S_IFDIR bit is set if path specifies a directory; the _S_IFREG bit
  562. * is set if path specifies an ordinary file or a device." We have just tested for a directory above, so if
  563. * we have a directory that isn't one according to lstat(...) we must have a junction.
  564. *
  565. * #define _S_IFDIR 0x4000
  566. * #define _S_IFREG 0x8000
  567. *
  568. * Stat cache should be cleared before to avoid accidentally reading wrong information from previous installs.
  569. */
  570. clearstatcache(true, $junction);
  571. clearstatcache(false);
  572. $stat = lstat($junction);
  573. return !($stat['mode'] & 0xC000);
  574. }
  575. /**
  576. * Removes a Windows NTFS junction.
  577. *
  578. * @param string $junction
  579. * @return bool
  580. */
  581. public function removeJunction($junction)
  582. {
  583. if (!Platform::isWindows()) {
  584. return false;
  585. }
  586. $junction = rtrim(str_replace('/', DIRECTORY_SEPARATOR, $junction), DIRECTORY_SEPARATOR);
  587. if (!$this->isJunction($junction)) {
  588. throw new IOException(sprintf('%s is not a junction and thus cannot be removed as one', $junction));
  589. }
  590. $cmd = sprintf('rmdir /S /Q %s', ProcessExecutor::escape($junction));
  591. clearstatcache(true, $junction);
  592. return ($this->getProcess()->execute($cmd, $output) === 0);
  593. }
  594. }