Skip to content

Commit

Permalink
Fixing file deletion issue in windows
Browse files Browse the repository at this point in the history
where unlink() cannot delete files that have open
file handles.
Fixes #376
  • Loading branch information
markstory committed Aug 29, 2011
1 parent 5b43d6b commit ff0119b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cake/libs/file.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ function close() {
*/ */
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);
} }
Expand Down
20 changes: 19 additions & 1 deletion cake/tests/cases/libs/file.test.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ function testAppend() {
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);
Expand All @@ -411,6 +411,24 @@ function testDelete() {
$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
* *
Expand Down

0 comments on commit ff0119b

Please sign in to comment.