Skip to content
Merged
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
29 changes: 29 additions & 0 deletions src/Services/FilesystemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

use function dirname;
use function fclose;
use function fflush;
use function flock;
use function fopen;
use function ftruncate;
use function fwrite;
use function is_resource;
use function microtime;
Expand Down Expand Up @@ -43,6 +46,8 @@ public function createDraft(string $filename) // @pest-ignore-type
// @codeCoverageIgnoreEnd
}

$this->lock($resource);

return $resource;
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
Expand Down Expand Up @@ -70,6 +75,7 @@ public function release($resource, string $path): void // @pest-ignore-type
{
$temp = $this->getMetaPath($resource);

$this->unlock($resource);
$this->close($resource);

if ($this->file->exists($path)) {
Expand Down Expand Up @@ -132,4 +138,27 @@ protected function getMetaPath($file): string

return $meta['uri'] ?? throw new ResourceMetaException;
}

/**
* @param resource $resource
*/
protected function lock($resource): void // @pest-ignore-type
{
if (! flock($resource, LOCK_EX)) {
// @codeCoverageIgnoreStart
throw new RuntimeException('Resource lock error. The resource may be in use by another process.');
// @codeCoverageIgnoreEnd
}

ftruncate($resource, 0);
}

/**
* @param resource $resource
*/
protected function unlock($resource): void // @pest-ignore-type
{
fflush($resource);
flock($resource, LOCK_UN);
}
}