Filesystem.php 22 KB

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