Browse Source

Clear cached files when they fail to extract or validate, refs #941

Jordi Boggiano 12 years ago
parent
commit
07f7487c60

+ 10 - 0
src/Composer/Cache.php

@@ -104,6 +104,16 @@ class Cache
         return false;
     }
 
+    public function remove($file)
+    {
+        $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
+        if ($this->enabled && file_exists($this->root . $file)) {
+            return unlink($this->root . $file);
+        }
+
+        return false;
+    }
+
     public function gc($ttl)
     {
         $expire = new \DateTime();

+ 7 - 1
src/Composer/Downloader/ArchiveDownloader.php

@@ -35,7 +35,13 @@ abstract class ArchiveDownloader extends FileDownloader
             $this->io->write('    Unpacking archive');
         }
         try {
-            $this->extract($fileName, $path);
+            try {
+                $this->extract($fileName, $path);
+            } catch (\Exception $e) {
+                // remove cache if the file was corrupted
+                parent::clearCache($package, $path);
+                throw $e;
+            }
 
             if ($this->io->isVerbose()) {
                 $this->io->write('    Cleaning up');

+ 9 - 0
src/Composer/Downloader/FileDownloader.php

@@ -121,10 +121,19 @@ class FileDownloader implements DownloaderInterface
         } catch (\Exception $e) {
             // clean up
             $this->filesystem->removeDirectory($path);
+            $this->clearCache($package, $path);
             throw $e;
         }
     }
 
+    protected function clearCache(PackageInterface $package, $path)
+    {
+        if ($this->cache) {
+            $fileName = $this->getFileName($package, $path);
+            $this->cache->remove($this->getCacheKey($package));
+        }
+    }
+
     /**
      * {@inheritDoc}
      */