Permalink
Browse files

Fixing file deletion issue in windows

where unlink() cannot delete files that have open
file handles.
Fixes #376
  • Loading branch information...
1 parent 0064317 commit f91c755cd7b44beb2c5a52c40d1d0c3ebc7da93f @markstory markstory committed Aug 29, 2011
Showing with 26 additions and 3 deletions.
  1. +5 −1 cake/libs/file.php
  2. +21 −2 cake/tests/cases/libs/file.test.php
View
6 cake/libs/file.php
@@ -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);
}
@@ -495,4 +499,4 @@ function &Folder() {
return $this->Folder;
}
}
-?>
+?>
View
23 cake/tests/cases/libs/file.test.php
@@ -380,7 +380,7 @@ function testAppend() {
function testDelete() {
if (!$tmpFile = $this->_getTmpFile()) {
return false;
- };
+ }
if (!file_exists($tmpFile)) {
touch($tmpFile);
@@ -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
*
@@ -425,4 +444,4 @@ function _getTmpFile($paintSkip = true) {
return false;
}
}
-?>
+?>

0 comments on commit f91c755

Please sign in to comment.