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

main
Jordi Boggiano 12 years ago
parent 87c1629ebc
commit 605e1cb925

@ -14,7 +14,6 @@ namespace Composer\Downloader;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
/** /**
* Base downloader for archives * Base downloader for archives
@ -34,28 +33,34 @@ abstract class ArchiveDownloader extends FileDownloader
$fileName = $this->getFileName($package, $path); $fileName = $this->getFileName($package, $path);
$this->io->write(' Unpacking archive'); $this->io->write(' Unpacking archive');
$this->extract($fileName, $path); try {
$this->extract($fileName, $path);
$this->io->write(' Cleaning up'); $this->io->write(' Cleaning up');
unlink($fileName); unlink($fileName);
// If we have only a one dir inside it suppose to be a package itself // If we have only a one dir inside it suppose to be a package itself
$contentDir = glob($path . '/*'); $contentDir = glob($path . '/*');
if (1 === count($contentDir)) { if (1 === count($contentDir)) {
$contentDir = $contentDir[0]; $contentDir = $contentDir[0];
// Rename the content directory to avoid error when moving up // Rename the content directory to avoid error when moving up
// a child folder with the same name // a child folder with the same name
$temporaryName = md5(time().rand()); $temporaryName = md5(time().rand());
rename($contentDir, $temporaryName); rename($contentDir, $temporaryName);
$contentDir = $temporaryName; $contentDir = $temporaryName;
foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) { foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
if (trim(basename($file), '.')) { if (trim(basename($file), '.')) {
rename($file, $path . '/' . basename($file)); rename($file, $path . '/' . basename($file));
}
} }
rmdir($contentDir);
} }
rmdir($contentDir); } catch (\Exception $e) {
// clean up
$this->fs->removeDirectory($path);
throw $e;
} }
$this->io->write(''); $this->io->write('');

@ -28,16 +28,18 @@ class FileDownloader implements DownloaderInterface
{ {
protected $io; protected $io;
protected $rfs; protected $rfs;
protected $filesystem;
/** /**
* Constructor. * Constructor.
* *
* @param IOInterface $io The IO instance * @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->io = $io;
$this->rfs = $rfs ?: new RemoteFilesystem($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'); throw new \InvalidArgumentException('The given package is missing url information');
} }
if (!is_dir($path)) { $this->filesystem->ensureDirectoryExists($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');
}
}
$fileName = $this->getFileName($package, $path); $fileName = $this->getFileName($package, $path);
@ -73,16 +68,22 @@ class FileDownloader implements DownloaderInterface
$url = $this->processUrl($url); $url = $this->processUrl($url);
$this->rfs->copy($package->getSourceUrl(), $url, $fileName); try {
$this->rfs->copy($package->getSourceUrl(), $url, $fileName);
if (!file_exists($fileName)) { if (!file_exists($fileName)) {
throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the' throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
.' directory is writable and you have internet connectivity'); .' directory is writable and you have internet connectivity');
} }
$checksum = $package->getDistSha1Checksum(); $checksum = $package->getDistSha1Checksum();
if ($checksum && hash_file('sha1', $fileName) !== $checksum) { if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')'); 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) public function remove(PackageInterface $package, $path)
{ {
$fs = new Filesystem(); $this->fs->removeDirectory($path);
$fs->removeDirectory($path);
} }
/** /**

Loading…
Cancel
Save