فهرست منبع

Merge remote-tracking branch 'hakre/patch-1'

Jordi Boggiano 10 سال پیش
والد
کامیت
923191a489
2فایلهای تغییر یافته به همراه47 افزوده شده و 2 حذف شده
  1. 20 2
      src/Composer/Util/Filesystem.php
  2. 27 0
      tests/Composer/Test/Util/FilesystemTest.php

+ 20 - 2
src/Composer/Util/Filesystem.php

@@ -181,9 +181,9 @@ class Filesystem
      */
     public function unlink($path)
     {
-        if (!@unlink($path)) {
+        if (!@$this->unlinkImplementation($path)) {
             // retry after a bit on windows since it tends to be touchy with mass removals
-            if (!defined('PHP_WINDOWS_VERSION_BUILD') || (usleep(350000) && !@unlink($path))) {
+            if (!defined('PHP_WINDOWS_VERSION_BUILD') || (usleep(350000) && !@$this->unlinkImplementation($path))) {
                 $error = error_get_last();
                 $message = 'Could not delete '.$path.': ' . @$error['message'];
                 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
@@ -489,4 +489,22 @@ class Filesystem
     {
         return new ProcessExecutor;
     }
+
+    /**
+     * delete symbolic link implementation (commonly known as "unlink()")
+     *
+     * symbolic links on windows which link to directories need rmdir instead of unlink
+     *
+     * @param string $path
+     *
+     * @return bool
+     */
+    private function unlinkImplementation($path)
+    {
+        if (defined('PHP_WINDOWS_VERSION_BUILD') && is_dir($path) && is_link($path)) {
+            return rmdir($path);
+        }
+
+        return unlink($path);
+    }
 }

+ 27 - 0
tests/Composer/Test/Util/FilesystemTest.php

@@ -176,4 +176,31 @@ class FilesystemTest extends TestCase
             array('phar://c:../Foo', 'phar://c:../Foo'),
         );
     }
+
+    /**
+     * @link https://github.com/composer/composer/issues/3157
+     */
+    public function testUnlinkSymlinkedDirectory()
+    {
+        $tmp       = sys_get_temp_dir();
+        $basepath  = $tmp . "/composer_testdir";
+        $symlinked = $basepath . "/linked";
+        @mkdir($basepath . "/real", 0777, true);
+        touch($basepath . "/real/FILE");
+
+        $result = @symlink($basepath . "/real", $symlinked);
+
+        if (!$result) {
+            $this->markTestSkipped('Symbolic links for directories not supported on this platform');
+        }
+
+        if (!is_dir($symlinked)) {
+            $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
+        }
+
+        $fs     = new Filesystem();
+        $result = $fs->unlink($symlinked);
+        $this->assertTrue($result);
+        $this->assertFalse(file_exists($symlinked));
+    }
 }