Skip to content
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

FileStorage: Fix bug retrieving the first record #672

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions Clockwork/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down