Skip to content

[Filesystem] improve error handling in lock() #22910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Symfony/Component/Filesystem/LockHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ public function lock($blocking = false)
return true;
}

$error = null;

// Silence error reporting
set_error_handler(function () {});
set_error_handler(function ($errno, $msg) use (&$error) {
$error = $msg;
});

if (!$this->handle = fopen($this->file, 'r')) {
if ($this->handle = fopen($this->file, 'x')) {
Expand All @@ -82,8 +86,7 @@ public function lock($blocking = false)
restore_error_handler();

if (!$this->handle) {
$error = error_get_last();
throw new IOException($error['message'], 0, null, $this->file);
throw new IOException($error, 0, null, $this->file);
}

// On Windows, even if PHP doc says the contrary, LOCK_NB works, see
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/LockHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Filesystem\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\LockHandler;

class LockHandlerTest extends TestCase
Expand Down Expand Up @@ -40,6 +42,44 @@ public function testConstructWhenRepositoryIsNotWriteable()
new LockHandler('lock', '/');
}

public function testErrorHandlingInLockIfLockPathBecomesUnwritable()
{
// skip test on Windows; PHP can't easily set file as unreadable on Windows
if ('\\' === DIRECTORY_SEPARATOR) {
$this->markTestSkipped('This test cannot run on Windows.');
}

$lockPath = sys_get_temp_dir().'/'.uniqid();
$e = null;
$wrongMessage = null;

try {
mkdir($lockPath);

$lockHandler = new LockHandler('lock', $lockPath);

chmod($lockPath, 0444);

$lockHandler->lock();
} catch (IOException $e) {
if (false === strpos($e->getMessage(), 'Permission denied')) {
$wrongMessage = $e->getMessage();
} else {
$this->addToAssertionCount(1);
}
} catch (\Exception $e) {
} catch (\Throwable $e) {
}

if (is_dir($lockPath)) {
$fs = new Filesystem();
$fs->remove($lockPath);
}

$this->assertInstanceOf('Symfony\Component\Filesystem\Exception\IOException', $e, sprintf('Expected IOException to be thrown, got %s instead.', get_class($e)));
$this->assertNull($wrongMessage, sprintf('Expected exception message to contain "Permission denied", got "%s" instead.', $wrongMessage));
}

public function testConstructSanitizeName()
{
$lock = new LockHandler('<?php echo "% hello word ! %" ?>');
Expand Down