Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bug #12770 [Filesystem] fix lock file permissions (nicolas-grekas)
This PR was merged into the 2.6 branch.

Discussion
----------

[Filesystem] fix lock file permissions

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12589, #12634
| License       | MIT
| Doc PR        | -

This PR replaces #12634. It tunes permissions on lock files so that locking is allowed for any OS user.

Commits
-------

ceecb50 [Filesystem] fix lock file permissions
  • Loading branch information
fabpot committed Dec 2, 2014
2 parents 1c8bfea + ceecb50 commit 09d1a15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/Symfony/Component/Filesystem/LockHandler.php
Expand Up @@ -49,9 +49,7 @@ public function __construct($name, $lockPath = null)
throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath);
}

$name = sprintf('%s-%s', preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));

$this->file = sprintf('%s/%s', $lockPath, $name);
$this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));
}

/**
Expand All @@ -67,14 +65,24 @@ public function lock($blocking = false)
return true;
}

// Set an error handler to not trigger the registered error handler if
// the file can not be opened.
// Silence both userland and native PHP error handlers
$errorLevel = error_reporting(0);
set_error_handler('var_dump', 0);
$this->handle = @fopen($this->file, 'c');

if (!$this->handle = fopen($this->file, 'r')) {
if ($this->handle = fopen($this->file, 'x')) {
chmod($this->file, 0444);
} elseif (!$this->handle = fopen($this->file, 'r')) {
usleep(100); // Give some time for chmod() to complete
$this->handle = fopen($this->file, 'r');
}
}
restore_error_handler();
error_reporting($errorLevel);

if (!$this->handle) {
throw new IOException(sprintf('Unable to fopen "%s".', $this->file), 0, null, $this->file);
$error = error_get_last();
throw new IOException($error['message'], 0, null, $this->file);
}

// On Windows, even if PHP doc says the contrary, LOCK_NB works, see
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Filesystem/Tests/LockHandlerTest.php
Expand Up @@ -28,7 +28,7 @@ public function testConstructSanitizeName()
{
$lock = new LockHandler('<?php echo "% hello word ! %" ?>');

$file = sprintf('%s/-php-echo-hello-word--4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f', sys_get_temp_dir());
$file = sprintf('%s/sf.-php-echo-hello-word-.4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f.lock', sys_get_temp_dir());
// ensure the file does not exist before the lock
@unlink($file);

Expand Down

0 comments on commit 09d1a15

Please sign in to comment.