Skip to content

Commit f63aa92

Browse files
committed
Merge pull request cakephp#1497 from hiromi2424/fix-unlink-windows
Fix unlink() for CacheEngine::clear() failed on windows. Refs cakephp#3930
2 parents 86dcebf + 29314e0 commit f63aa92

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/Cake/Cache/Engine/FileEngine.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ public function clear($check) {
223223
if (!$this->_init) {
224224
return false;
225225
}
226+
$this->_File = null;
227+
226228
$threshold = $now = false;
227229
if ($check) {
228230
$now = time();
@@ -233,11 +235,17 @@ public function clear($check) {
233235

234236
$directory = new RecursiveDirectoryIterator($this->settings['path']);
235237
$contents = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
238+
$cleared = array();
236239
foreach ($contents as $path) {
237240
if ($path->isFile()) {
238241
continue;
239242
}
240-
$this->_clearDirectory($path->getRealPath() . DS, $now, $threshold);
243+
244+
$path = $path->getRealPath() . DS;
245+
if (!in_array($path, $cleared)) {
246+
$this->_clearDirectory($path, $now, $threshold);
247+
$cleared[] = $path;
248+
}
241249
}
242250
return true;
243251
}
@@ -263,7 +271,7 @@ protected function _clearDirectory($path, $now, $threshold) {
263271
continue;
264272
}
265273
$filePath = $path . $entry;
266-
if (is_dir($filePath)) {
274+
if (!file_exists($filePath) || is_dir($filePath)) {
267275
continue;
268276
}
269277
$file = new SplFileObject($path . $entry, 'r');
@@ -281,7 +289,9 @@ protected function _clearDirectory($path, $now, $threshold) {
281289
}
282290
}
283291
if ($file->isFile()) {
284-
unlink($file->getRealPath());
292+
$_path = $file->getRealPath();
293+
$file = null;
294+
unlink($_path);
285295
}
286296
}
287297
}
@@ -388,6 +398,7 @@ public function key($key) {
388398
* @return boolean success
389399
*/
390400
public function clearGroup($group) {
401+
$this->_File = null;
391402
$directoryIterator = new RecursiveDirectoryIterator($this->settings['path']);
392403
$contents = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
393404
foreach ($contents as $object) {
@@ -397,7 +408,6 @@ public function clearGroup($group) {
397408
unlink($object->getPathName());
398409
}
399410
}
400-
$this->_File = null;
401411
return true;
402412
}
403413
}

0 commit comments

Comments
 (0)