1
0
Fork 0

Clean up in case of download/extraction failure, fixes #356

pull/501/head
Jordi Boggiano 2012-03-26 01:49:09 +02:00
parent 87c1629ebc
commit 605e1cb925
2 changed files with 41 additions and 36 deletions

View File

@ -14,7 +14,6 @@ namespace Composer\Downloader;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
/**
* Base downloader for archives
@ -34,6 +33,7 @@ abstract class ArchiveDownloader extends FileDownloader
$fileName = $this->getFileName($package, $path);
$this->io->write(' Unpacking archive');
try {
$this->extract($fileName, $path);
$this->io->write(' Cleaning up');
@ -57,6 +57,11 @@ abstract class ArchiveDownloader extends FileDownloader
}
rmdir($contentDir);
}
} catch (\Exception $e) {
// clean up
$this->fs->removeDirectory($path);
throw $e;
}
$this->io->write('');
}

View File

@ -28,16 +28,18 @@ class FileDownloader implements DownloaderInterface
{
protected $io;
protected $rfs;
protected $filesystem;
/**
* Constructor.
*
* @param IOInterface $io The IO instance
*/
public function __construct(IOInterface $io, RemoteFilesystem $rfs = null)
public function __construct(IOInterface $io, RemoteFilesystem $rfs = null, Filesystem $filesystem = null)
{
$this->io = $io;
$this->rfs = $rfs ?: new RemoteFilesystem($io);
$this->filesystem = $filesystem ?: new Filesystem();
}
/**
@ -58,14 +60,7 @@ class FileDownloader implements DownloaderInterface
throw new \InvalidArgumentException('The given package is missing url information');
}
if (!is_dir($path)) {
if (file_exists($path)) {
throw new \UnexpectedValueException($path.' exists and is not a directory');
}
if (!mkdir($path, 0777, true)) {
throw new \UnexpectedValueException($path.' does not exist and could not be created');
}
}
$this->filesystem->ensureDirectoryExists($path);
$fileName = $this->getFileName($package, $path);
@ -73,6 +68,7 @@ class FileDownloader implements DownloaderInterface
$url = $this->processUrl($url);
try {
$this->rfs->copy($package->getSourceUrl(), $url, $fileName);
if (!file_exists($fileName)) {
@ -84,6 +80,11 @@ class FileDownloader implements DownloaderInterface
if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')');
}
} catch (\Exception $e) {
// clean up
$this->fs->removeDirectory($path);
throw $e;
}
}
/**
@ -100,8 +101,7 @@ class FileDownloader implements DownloaderInterface
*/
public function remove(PackageInterface $package, $path)
{
$fs = new Filesystem();
$fs->removeDirectory($path);
$this->fs->removeDirectory($path);
}
/**