Fixing locking for file-based cache, fixes #5541

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7744 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-10-15 12:42:03 +00:00
parent 1b9357f5e8
commit 6a06194292
2 changed files with 35 additions and 21 deletions

View file

@ -159,29 +159,32 @@ class File extends Object {
* @access public
*/
function read($bytes = false, $mode = 'rb', $force = false) {
$success = false;
if ($this->lock !== null) {
if (flock($this->handle, LOCK_SH) === false) {
return false;
}
if ($bytes === false && $this->lock === null) {
return file_get_contents($this->pwd());
}
if ($bytes === false) {
$success = file_get_contents($this->pwd());
} elseif ($this->open($mode, $force) === true) {
if (is_int($bytes)) {
$success = fread($this->handle, $bytes);
} else {
$data = '';
while (!feof($this->handle)) {
$data .= fgets($this->handle, 4096);
}
$success = trim($data);
}
if (!$this->open($mode, $force)) {
return false;
}
if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
return false;
}
if (is_int($bytes)) {
return fread($this->handle, $bytes);
}
$data = '';
while (!feof($this->handle)) {
$data .= fgets($this->handle, 4096);
}
$data = trim($data);
if ($this->lock !== null) {
flock($this->handle, LOCK_UN);
}
return $success;
if ($bytes === false) {
$this->close();
}
return $data;
}
/**
* Sets or gets the offset for the currently opened file.

View file

@ -61,7 +61,10 @@ class FileTest extends CakeTestCase {
$this->assertEqual($result, $expecting);
$result = $this->File->info();
$expecting = array('dirname'=> dirname(__FILE__), 'basename'=> basename(__FILE__), 'extension'=> 'php', 'filename'=>'file.test');
$expecting = array(
'dirname' => dirname(__FILE__), 'basename' => basename(__FILE__),
'extension' => 'php', 'filename' =>'file.test'
);
$this->assertEqual($result, $expecting);
$result = $this->File->ext();
@ -112,6 +115,12 @@ class FileTest extends CakeTestCase {
$this->assertEqual($result, $expecting);
$this->assertTrue(!is_resource($this->File->handle));
$this->File->lock = true;
$result = $this->File->read();
$expecting = file_get_contents(__FILE__);
$this->assertEqual($result, $expecting);
$this->File->lock = null;
$data = $expecting;
$expecting = substr($data, 0, 3);
$result = $this->File->read(3);
@ -230,7 +239,8 @@ class FileTest extends CakeTestCase {
$expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
$this->assertIdentical(File::prepare($string), $expected);
$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\nfor\r\n\r\n\r\n\r\n\r\nhere";
$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
$this->assertIdentical(File::prepare($string, true), $expected);
}
/**
@ -407,7 +417,8 @@ class FileTest extends CakeTestCase {
$assertLine = $assertLine->traceMethod();
$shortPath = substr($tmpFile, strlen(ROOT));
$message = sprintf(__('[FileTest] Skipping %s because "%s" not writeable!', true), $caller, $shortPath).$assertLine;
$message = '[FileTest] Skipping %s because "%s" not writeable!';
$message = sprintf(__($message, true), $caller, $shortPath).$assertLine;
$this->_reporter->paintSkip($message);
}
return false;