Filesystem.php 22 KB

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