No Description

LockHandler.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Filesystem;
  11. use Symfony\Component\Filesystem\Exception\IOException;
  12. /**
  13. * LockHandler class provides a simple abstraction to lock anything by means of
  14. * a file lock.
  15. *
  16. * A locked file is created based on the lock name when calling lock(). Other
  17. * lock handlers will not be able to lock the same name until it is released
  18. * (explicitly by calling release() or implicitly when the instance holding the
  19. * lock is destroyed).
  20. *
  21. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  22. * @author Romain Neutron <imprec@gmail.com>
  23. * @author Nicolas Grekas <p@tchwork.com>
  24. */
  25. class LockHandler
  26. {
  27. private $file;
  28. private $handle;
  29. /**
  30. * @param string $name The lock name
  31. * @param string|null $lockPath The directory to store the lock. Default values will use temporary directory
  32. * @throws IOException If the lock directory could not be created or is not writable
  33. */
  34. public function __construct($name, $lockPath = null)
  35. {
  36. $lockPath = $lockPath ?: sys_get_temp_dir();
  37. if (!is_dir($lockPath)) {
  38. $fs = new Filesystem();
  39. $fs->mkdir($lockPath);
  40. }
  41. if (!is_writable($lockPath)) {
  42. throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath);
  43. }
  44. $this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));
  45. }
  46. /**
  47. * Lock the resource
  48. *
  49. * @param bool $blocking wait until the lock is released
  50. * @return bool Returns true if the lock was acquired, false otherwise
  51. * @throws IOException If the lock file could not be created or opened
  52. */
  53. public function lock($blocking = false)
  54. {
  55. if ($this->handle) {
  56. return true;
  57. }
  58. // Silence both userland and native PHP error handlers
  59. $errorLevel = error_reporting(0);
  60. set_error_handler('var_dump', 0);
  61. if (!$this->handle = fopen($this->file, 'r')) {
  62. if ($this->handle = fopen($this->file, 'x')) {
  63. chmod($this->file, 0444);
  64. } elseif (!$this->handle = fopen($this->file, 'r')) {
  65. usleep(100); // Give some time for chmod() to complete
  66. $this->handle = fopen($this->file, 'r');
  67. }
  68. }
  69. restore_error_handler();
  70. error_reporting($errorLevel);
  71. if (!$this->handle) {
  72. $error = error_get_last();
  73. throw new IOException($error['message'], 0, null, $this->file);
  74. }
  75. // On Windows, even if PHP doc says the contrary, LOCK_NB works, see
  76. // https://bugs.php.net/54129
  77. if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) {
  78. fclose($this->handle);
  79. $this->handle = null;
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * Release the resource
  86. */
  87. public function release()
  88. {
  89. if ($this->handle) {
  90. flock($this->handle, LOCK_UN | LOCK_NB);
  91. fclose($this->handle);
  92. $this->handle = null;
  93. }
  94. }
  95. }