Merge branch '3.4' into 4.4

* 3.4:
  [Filesystem] Check if failed unlink was caused by permission denied
This commit is contained in:
Fabien Potencier 2020-10-21 06:38:54 +02:00
commit 19637c5de3
2 changed files with 25 additions and 1 deletions

View File

@ -180,7 +180,7 @@ class Filesystem
if (!self::box('rmdir', $file) && file_exists($file)) {
throw new IOException(sprintf('Failed to remove directory "%s": ', $file).self::$lastError);
}
} elseif (!self::box('unlink', $file) && file_exists($file)) {
} elseif (!self::box('unlink', $file) && (false !== strpos(self::$lastError, 'Permission denied') || file_exists($file))) {
throw new IOException(sprintf('Failed to remove file "%s": ', $file).self::$lastError);
}
}

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\Filesystem\Tests;
use Symfony\Component\Filesystem\Exception\IOException;
/**
* Test class for Filesystem.
*/
@ -334,6 +336,28 @@ class FilesystemTest extends FilesystemTestCase
$this->assertFileDoesNotExist($basePath.'dir');
}
public function testRemoveThrowsExceptionOnPermissionDenied()
{
$this->markAsSkippedIfChmodIsMissing();
$basePath = $this->workspace.\DIRECTORY_SEPARATOR.'dir_permissions';
mkdir($basePath);
$file = $basePath.\DIRECTORY_SEPARATOR.'file';
touch($file);
chmod($basePath, 0400);
try {
$this->filesystem->remove($file);
$this->fail('Filesystem::remove() should throw an exception');
} catch (IOException $e) {
$this->assertStringContainsString('Failed to remove file "'.$file.'"', $e->getMessage());
$this->assertStringContainsString('Permission denied', $e->getMessage());
} finally {
// Make sure we can clean up this file
chmod($basePath, 0777);
}
}
public function testRemoveCleansInvalidLinks()
{
$this->markAsSkippedIfSymlinkIsMissing();