Fixing file deletion issue in windows

where unlink() cannot delete files that have open
file handles.
Fixes #376
This commit is contained in:
Mark Story mark@mark-story.com 2011-08-28 22:19:53 -04:00
parent 5b43d6b9a7
commit ff0119ba6f
2 changed files with 23 additions and 1 deletions

View file

@ -290,6 +290,10 @@ class File extends Object {
*/ */
function delete() { function delete() {
clearstatcache(); clearstatcache();
if (is_resource($this->handle)) {
fclose($this->handle);
$this->handle = null;
}
if ($this->exists()) { if ($this->exists()) {
return unlink($this->path); return unlink($this->path);
} }

View file

@ -395,7 +395,7 @@ class FileTest extends CakeTestCase {
function testDelete() { function testDelete() {
if (!$tmpFile = $this->_getTmpFile()) { if (!$tmpFile = $this->_getTmpFile()) {
return false; return false;
}; }
if (!file_exists($tmpFile)) { if (!file_exists($tmpFile)) {
touch($tmpFile); touch($tmpFile);
@ -411,6 +411,24 @@ class FileTest extends CakeTestCase {
$this->assertFalse($result); $this->assertFalse($result);
} }
/**
* Windows has issues unlinking files if there are
* active filehandles open.
*
* @return void
*/
function testDeleteAfterRead() {
if (!$tmpFile = $this->_getTmpFile()) {
return false;
}
if (!file_exists($tmpFile)) {
touch($tmpFile);
}
$file =& new File($tmpFile);
$file->read();
$this->assertTrue($file->delete());
}
/** /**
* testCopy method * testCopy method
* *