Filesystem.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. if (!is_dir($source)) {
  216. copy($source, $target);
  217. $this->unlink($source);
  218. return;
  219. }
  220. $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
  221. $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
  222. $this->ensureDirectoryExists($target);
  223. foreach ($ri as $file) {
  224. $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
  225. if ($file->isDir()) {
  226. $this->ensureDirectoryExists($targetPath);
  227. } else {
  228. copy($file->getPathname(), $targetPath);
  229. }
  230. }
  231. $this->removeDirectoryPhp($source);
  232. }
  233. public function rename($source, $target)
  234. {
  235. if (true === @rename($source, $target)) {
  236. return;
  237. }
  238. if (!function_exists('proc_open')) {
  239. return $this->copyThenRemove($source, $target);
  240. }
  241. if (Platform::isWindows()) {
  242. // Try to copy & delete - this is a workaround for random "Access denied" errors.
  243. $command = sprintf('xcopy %s %s /E /I /Q /Y', ProcessExecutor::escape($source), ProcessExecutor::escape($target));
  244. $result = $this->processExecutor->execute($command, $output);
  245. // clear stat cache because external processes aren't tracked by the php stat cache
  246. clearstatcache();
  247. if (0 === $result) {
  248. $this->remove($source);
  249. return;
  250. }
  251. } else {
  252. // We do not use PHP's "rename" function here since it does not support
  253. // the case where $source, and $target are located on different partitions.
  254. $command = sprintf('mv %s %s', ProcessExecutor::escape($source), ProcessExecutor::escape($target));
  255. $result = $this->processExecutor->execute($command, $output);
  256. // clear stat cache because external processes aren't tracked by the php stat cache
  257. clearstatcache();
  258. if (0 === $result) {
  259. return;
  260. }
  261. }
  262. return $this->copyThenRemove($source, $target);
  263. }
  264. /**
  265. * Returns the shortest path from $from to $to
  266. *
  267. * @param string $from
  268. * @param string $to
  269. * @param bool $directories if true, the source/target are considered to be directories
  270. * @throws \InvalidArgumentException
  271. * @return string
  272. */
  273. public function findShortestPath($from, $to, $directories = false)
  274. {
  275. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  276. throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to));
  277. }
  278. $from = lcfirst($this->normalizePath($from));
  279. $to = lcfirst($this->normalizePath($to));
  280. if ($directories) {
  281. $from = rtrim($from, '/') . '/dummy_file';
  282. }
  283. if (dirname($from) === dirname($to)) {
  284. return './'.basename($to);
  285. }
  286. $commonPath = $to;
  287. while (strpos($from.'/', $commonPath.'/') !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath)) {
  288. $commonPath = strtr(dirname($commonPath), '\\', '/');
  289. }
  290. if (0 !== strpos($from, $commonPath) || '/' === $commonPath) {
  291. return $to;
  292. }
  293. $commonPath = rtrim($commonPath, '/') . '/';
  294. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
  295. $commonPathCode = str_repeat('../', $sourcePathDepth);
  296. return ($commonPathCode . substr($to, strlen($commonPath))) ?: './';
  297. }
  298. /**
  299. * Returns PHP code that, when executed in $from, will return the path to $to
  300. *
  301. * @param string $from
  302. * @param string $to
  303. * @param bool $directories if true, the source/target are considered to be directories
  304. * @param bool $staticCode
  305. * @throws \InvalidArgumentException
  306. * @return string
  307. */
  308. public function findShortestPathCode($from, $to, $directories = false, $staticCode = false)
  309. {
  310. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  311. throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to));
  312. }
  313. $from = lcfirst($this->normalizePath($from));
  314. $to = lcfirst($this->normalizePath($to));
  315. if ($from === $to) {
  316. return $directories ? '__DIR__' : '__FILE__';
  317. }
  318. $commonPath = $to;
  319. while (strpos($from.'/', $commonPath.'/') !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
  320. $commonPath = strtr(dirname($commonPath), '\\', '/');
  321. }
  322. if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
  323. return var_export($to, true);
  324. }
  325. $commonPath = rtrim($commonPath, '/') . '/';
  326. if (strpos($to, $from.'/') === 0) {
  327. return '__DIR__ . '.var_export(substr($to, strlen($from)), true);
  328. }
  329. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories;
  330. if ($staticCode) {
  331. $commonPathCode = "__DIR__ . '".str_repeat('/..', $sourcePathDepth)."'";
  332. } else {
  333. $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
  334. }
  335. $relTarget = substr($to, strlen($commonPath));
  336. return $commonPathCode . (strlen($relTarget) ? '.' . var_export('/' . $relTarget, true) : '');
  337. }
  338. /**
  339. * Checks if the given path is absolute
  340. *
  341. * @param string $path
  342. * @return bool
  343. */
  344. public function isAbsolutePath($path)
  345. {
  346. return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
  347. }
  348. /**
  349. * Returns size of a file or directory specified by path. If a directory is
  350. * given, it's size will be computed recursively.
  351. *
  352. * @param string $path Path to the file or directory
  353. * @throws \RuntimeException
  354. * @return int
  355. */
  356. public function size($path)
  357. {
  358. if (!file_exists($path)) {
  359. throw new \RuntimeException("$path does not exist.");
  360. }
  361. if (is_dir($path)) {
  362. return $this->directorySize($path);
  363. }
  364. return filesize($path);
  365. }
  366. /**
  367. * Normalize a path. This replaces backslashes with slashes, removes ending
  368. * slash and collapses redundant separators and up-level references.
  369. *
  370. * @param string $path Path to the file or directory
  371. * @return string
  372. */
  373. public function normalizePath($path)
  374. {
  375. $parts = array();
  376. $path = strtr($path, '\\', '/');
  377. $prefix = '';
  378. $absolute = false;
  379. // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
  380. if (preg_match('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) {
  381. $prefix = $match[1];
  382. $path = substr($path, strlen($prefix));
  383. }
  384. if (substr($path, 0, 1) === '/') {
  385. $absolute = true;
  386. $path = substr($path, 1);
  387. }
  388. $up = false;
  389. foreach (explode('/', $path) as $chunk) {
  390. if ('..' === $chunk && ($absolute || $up)) {
  391. array_pop($parts);
  392. $up = !(empty($parts) || '..' === end($parts));
  393. } elseif ('.' !== $chunk && '' !== $chunk) {
  394. $parts[] = $chunk;
  395. $up = '..' !== $chunk;
  396. }
  397. }
  398. return $prefix.($absolute ? '/' : '').implode('/', $parts);
  399. }
  400. /**
  401. * Return if the given path is local
  402. *
  403. * @param string $path
  404. * @return bool
  405. */
  406. public static function isLocalPath($path)
  407. {
  408. return (bool) preg_match('{^(file://(?!//)|/(?!/)|/?[a-z]:[\\\\/]|\.\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i', $path);
  409. }
  410. public static function getPlatformPath($path)
  411. {
  412. if (Platform::isWindows()) {
  413. $path = preg_replace('{^(?:file:///([a-z]):?/)}i', 'file://$1:/', $path);
  414. }
  415. return preg_replace('{^file://}i', '', $path);
  416. }
  417. protected function directorySize($directory)
  418. {
  419. $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
  420. $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
  421. $size = 0;
  422. foreach ($ri as $file) {
  423. if ($file->isFile()) {
  424. $size += $file->getSize();
  425. }
  426. }
  427. return $size;
  428. }
  429. protected function getProcess()
  430. {
  431. return new ProcessExecutor;
  432. }
  433. /**
  434. * delete symbolic link implementation (commonly known as "unlink()")
  435. *
  436. * symbolic links on windows which link to directories need rmdir instead of unlink
  437. *
  438. * @param string $path
  439. *
  440. * @return bool
  441. */
  442. private function unlinkImplementation($path)
  443. {
  444. if (Platform::isWindows() && is_dir($path) && is_link($path)) {
  445. return rmdir($path);
  446. }
  447. return unlink($path);
  448. }
  449. /**
  450. * Creates a relative symlink from $link to $target
  451. *
  452. * @param string $target The path of the binary file to be symlinked
  453. * @param string $link The path where the symlink should be created
  454. * @return bool
  455. */
  456. public function relativeSymlink($target, $link)
  457. {
  458. $cwd = getcwd();
  459. $relativePath = $this->findShortestPath($link, $target);
  460. chdir(dirname($link));
  461. $result = @symlink($relativePath, $link);
  462. chdir($cwd);
  463. return (bool) $result;
  464. }
  465. /**
  466. * return true if that directory is a symlink.
  467. *
  468. * @param string $directory
  469. *
  470. * @return bool
  471. */
  472. public function isSymlinkedDirectory($directory)
  473. {
  474. if (!is_dir($directory)) {
  475. return false;
  476. }
  477. $resolved = $this->resolveSymlinkedDirectorySymlink($directory);
  478. return is_link($resolved);
  479. }
  480. /**
  481. * @param string $directory
  482. *
  483. * @return bool
  484. */
  485. private function unlinkSymlinkedDirectory($directory)
  486. {
  487. $resolved = $this->resolveSymlinkedDirectorySymlink($directory);
  488. return $this->unlink($resolved);
  489. }
  490. /**
  491. * resolve pathname to symbolic link of a directory
  492. *
  493. * @param string $pathname directory path to resolve
  494. *
  495. * @return string resolved path to symbolic link or original pathname (unresolved)
  496. */
  497. private function resolveSymlinkedDirectorySymlink($pathname)
  498. {
  499. if (!is_dir($pathname)) {
  500. return $pathname;
  501. }
  502. $resolved = rtrim($pathname, '/');
  503. if (!strlen($resolved)) {
  504. return $pathname;
  505. }
  506. return $resolved;
  507. }
  508. /**
  509. * Creates an NTFS junction.
  510. *
  511. * @param string $target
  512. * @param string $junction
  513. */
  514. public function junction($target, $junction)
  515. {
  516. if (!Platform::isWindows()) {
  517. throw new \LogicException(sprintf('Function %s is not available on non-Windows platform', __CLASS__));
  518. }
  519. if (!is_dir($target)) {
  520. throw new IOException(sprintf('Cannot junction to "%s" as it is not a directory.', $target), 0, null, $target);
  521. }
  522. $cmd = sprintf('mklink /J %s %s',
  523. ProcessExecutor::escape(str_replace('/', DIRECTORY_SEPARATOR, $junction)),
  524. ProcessExecutor::escape(realpath($target)));
  525. if ($this->getProcess()->execute($cmd, $output) !== 0) {
  526. throw new IOException(sprintf('Failed to create junction to "%s" at "%s".', $target, $junction), 0, null, $target);
  527. }
  528. clearstatcache(true, $junction);
  529. }
  530. /**
  531. * Returns whether the target directory is a Windows NTFS Junction.
  532. *
  533. * @param string $junction Path to check.
  534. * @return bool
  535. */
  536. public function isJunction($junction)
  537. {
  538. if (!Platform::isWindows()) {
  539. return false;
  540. }
  541. if (!is_dir($junction) || is_link($junction)) {
  542. return false;
  543. }
  544. /**
  545. * According to MSDN at https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx we can detect a junction now
  546. * using the 'mode' value from stat: "The _S_IFDIR bit is set if path specifies a directory; the _S_IFREG bit
  547. * is set if path specifies an ordinary file or a device." We have just tested for a directory above, so if
  548. * we have a directory that isn't one according to lstat(...) we must have a junction.
  549. *
  550. * #define _S_IFDIR 0x4000
  551. * #define _S_IFREG 0x8000
  552. *
  553. * Stat cache should be cleared before to avoid accidentally reading wrong information from previous installs.
  554. */
  555. clearstatcache(true, $junction);
  556. $stat = lstat($junction);
  557. return !($stat['mode'] & 0xC000);
  558. }
  559. /**
  560. * Removes a Windows NTFS junction.
  561. *
  562. * @param string $junction
  563. * @return bool
  564. */
  565. public function removeJunction($junction)
  566. {
  567. if (!Platform::isWindows()) {
  568. return false;
  569. }
  570. $junction = rtrim(str_replace('/', DIRECTORY_SEPARATOR, $junction), DIRECTORY_SEPARATOR);
  571. if (!$this->isJunction($junction)) {
  572. throw new IOException(sprintf('%s is not a junction and thus cannot be removed as one', $junction));
  573. }
  574. $cmd = sprintf('rmdir /S /Q %s', ProcessExecutor::escape($junction));
  575. clearstatcache(true, $junction);
  576. return ($this->getProcess()->execute($cmd, $output) === 0);
  577. }
  578. }