From 44fd75ef383b01298a4acb108943a67920b5e890 Mon Sep 17 00:00:00 2001 From: Guillaume LECERF Date: Wed, 12 Feb 2014 14:48:56 +0100 Subject: [PATCH] Fix Cache::gc() when COMPOSER_CACHE_DIR=/dev/null If we set COMPOSER_CACHE_DIR=/dev/null, and the garbage collector is triggered, we end up with the following error : The "/dev/null/" directory does not exist. This is because the Cache::gc() function does not check for Cache::enabled and instanciates a Finder unconditionnaly. Fix this by adding a check on Cache::enabled. --- src/Composer/Cache.php | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Composer/Cache.php b/src/Composer/Cache.php index 6bdf43d5d..6fe01702b 100644 --- a/src/Composer/Cache.php +++ b/src/Composer/Cache.php @@ -144,28 +144,33 @@ class Cache public function gc($ttl, $maxSize) { - $expire = new \DateTime(); - $expire->modify('-'.$ttl.' seconds'); - - $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s')); - foreach ($finder as $file) { - unlink($file->getRealPath()); - } + if ($this->enabled) + { + $expire = new \DateTime(); + $expire->modify('-'.$ttl.' seconds'); + + $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s')); + foreach ($finder as $file) { + unlink($file->getRealPath()); + } - $totalSize = $this->filesystem->size($this->root); - if ($totalSize > $maxSize) { - $iterator = $this->getFinder()->sortByAccessedTime()->getIterator(); - while ($totalSize > $maxSize && $iterator->valid()) { - $filepath = $iterator->current()->getRealPath(); - $totalSize -= $this->filesystem->size($filepath); - unlink($filepath); - $iterator->next(); + $totalSize = $this->filesystem->size($this->root); + if ($totalSize > $maxSize) { + $iterator = $this->getFinder()->sortByAccessedTime()->getIterator(); + while ($totalSize > $maxSize && $iterator->valid()) { + $filepath = $iterator->current()->getRealPath(); + $totalSize -= $this->filesystem->size($filepath); + unlink($filepath); + $iterator->next(); + } } - } - self::$cacheCollected = true; + self::$cacheCollected = true; - return true; + return true; + } + + return false; } public function sha1($file)