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 0064317 commit f91c755
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion cake/libs/file.php
Expand Up @@ -278,6 +278,10 @@ function close() {
*/
function delete() {
clearstatcache();
if (is_resource($this->handle)) {
fclose($this->handle);
$this->handle = null;
}
if ($this->exists()) {
return unlink($this->path);
}
Expand Down Expand Up @@ -495,4 +499,4 @@ function &Folder() {
return $this->Folder;
}
}
?>
?>
23 changes: 21 additions & 2 deletions cake/tests/cases/libs/file.test.php
Expand Up @@ -380,7 +380,7 @@ function testAppend() {
function testDelete() {
if (!$tmpFile = $this->_getTmpFile()) {
return false;
};
}

if (!file_exists($tmpFile)) {
touch($tmpFile);
Expand All @@ -395,6 +395,25 @@ function testDelete() {
$result = $TmpFile->delete();
$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());
}

/**
* getTmpFile method
*
Expand Down Expand Up @@ -425,4 +444,4 @@ function _getTmpFile($paintSkip = true) {
return false;
}
}
?>
?>

0 comments on commit f91c755

Please sign in to comment.