diff --git a/cake/libs/file.php b/cake/libs/file.php index 039135d9c..b9cd3612d 100644 --- a/cake/libs/file.php +++ b/cake/libs/file.php @@ -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. diff --git a/cake/tests/cases/libs/file.test.php b/cake/tests/cases/libs/file.test.php index 6d1347109..fa01c9701 100644 --- a/cake/tests/cases/libs/file.test.php +++ b/cake/tests/cases/libs/file.test.php @@ -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;