From 139213180bebbd2da410172dcf3ac1d39c4fbabf Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Tue, 23 Jan 2024 14:10:03 +0100 Subject: [PATCH] FileStorage: Fix bug retrieving the first record The file pointer is positioned at the line of the previously read record. When reading the next record, the current one is skipped. After opening the index file, the position is zero and reading the next record then skips the first record. The fix adds a flag that handles this corner-case. --- Clockwork/Storage/FileStorage.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Clockwork/Storage/FileStorage.php b/Clockwork/Storage/FileStorage.php index 9a06ba98..ae7e6beb 100644 --- a/Clockwork/Storage/FileStorage.php +++ b/Clockwork/Storage/FileStorage.php @@ -24,6 +24,10 @@ class FileStorage extends Storage // Index file handle protected $indexHandle; + // Flag whether the first record is to be retrieved next + /** @var bool */ + protected $indexInitial; + // Return new storage, takes path where to store files as argument public function __construct($path, $pathPermissions = 0700, $expiration = null, $compress = false) { @@ -176,6 +180,7 @@ protected function openIndex($position = 'start', $lock = false, $force = false) } $this->indexHandle = fopen("{$this->path}/index", 'r'); + $this->indexInitial = true; if ($lock) flock($this->indexHandle, LOCK_EX); if ($position == 'end') fseek($this->indexHandle, 0, SEEK_END); @@ -236,8 +241,14 @@ protected function readNextIndex() { if (feof($this->indexHandle)) return; - // File pointer is always at the start of the line, call extra fgets to skip current line - fgets($this->indexHandle); + // File pointer is always at the start of the last retrieved line. + // Call an extra `fgets()` to skip current line, unless it is the + // first line actually being read. + if (!$this->indexInitial) { + fgets($this->indexHandle); + } else { + $this->indexInitial = false; + } $line = fgets($this->indexHandle); // Check if we read an empty line or reached the end of file