Permalink
Browse files

Fixing file deletion issue in windows

where unlink() cannot delete files that have open
file handles.
Fixes #376
  • Loading branch information...
markstory committed Aug 29, 2011
1 parent 5b43d6b commit ff0119ba6f4fb95249ab00616c7d9d67477fb550
Showing with 23 additions and 1 deletion.
  1. +4 −0 cake/libs/file.php
  2. +19 −1 cake/tests/cases/libs/file.test.php
View
@@ -290,6 +290,10 @@ function close() {
*/
function delete() {
clearstatcache();
+ if (is_resource($this->handle)) {
+ fclose($this->handle);
+ $this->handle = null;
+ }
if ($this->exists()) {
return unlink($this->path);
}
@@ -395,7 +395,7 @@ function testAppend() {
function testDelete() {
if (!$tmpFile = $this->_getTmpFile()) {
return false;
- };
+ }
if (!file_exists($tmpFile)) {
touch($tmpFile);
@@ -411,6 +411,24 @@ function testDelete() {
$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
*

0 comments on commit ff0119b

Please sign in to comment.