diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php
index 82f11af92..5358af43a 100644
--- a/src/Composer/Command/SelfUpdateCommand.php
+++ b/src/Composer/Command/SelfUpdateCommand.php
@@ -14,6 +14,7 @@ namespace Composer\Command;
use Composer\Composer;
use Composer\Util\RemoteFilesystem;
+use Composer\Downloader\FilesystemException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -41,6 +42,18 @@ EOT
protected function execute(InputInterface $input, OutputInterface $output)
{
+ $localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
+ $tempFilename = dirname($localFilename) . '/' . basename($localFilename, '.phar').'-temp.phar';
+
+ // check for permissions in local filesystem before start connection process
+ if (!is_writable($tempDirectory = dirname($tempFilename))) {
+ throw new FilesystemException('Composer update failed: the "'.$tempDirectory.'" directory used to download the temp file could not be written');
+ }
+
+ if (!is_writable($localFilename)) {
+ throw new FilesystemException('Composer update failed: the "'.$localFilename. '" file could not be written');
+ }
+
$protocol = extension_loaded('openssl') ? 'https' : 'http';
$rfs = new RemoteFilesystem($this->getIO());
$latest = trim($rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/version', false));
@@ -48,9 +61,7 @@ EOT
if (Composer::VERSION !== $latest) {
$output->writeln(sprintf("Updating to version %s.", $latest));
- $remoteFilename = $protocol . '://getcomposer.org/composer.phar';
- $localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
- $tempFilename = dirname($localFilename) . '/' . basename($localFilename, '.phar').'-temp.phar';
+ $remoteFilename = $protocol . '://getcomposer.org/composer.phar';
$rfs->copy('getcomposer.org', $remoteFilename, $tempFilename);
diff --git a/src/Composer/Downloader/FilesystemException.php b/src/Composer/Downloader/FilesystemException.php
new file mode 100644
index 000000000..9829b7d4a
--- /dev/null
+++ b/src/Composer/Downloader/FilesystemException.php
@@ -0,0 +1,26 @@
+
+ * Jordi Boggiano
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Downloader;
+
+/**
+ * Exception thrown when issues exist on local filesystem
+ *
+ * @author Javier Spagnoletti
+ */
+class FilesystemException extends \Exception
+{
+ public function __construct($message = null, $code = null, \Exception $previous = null)
+ {
+ parent::__construct("Filesystem exception: \n".$message, $code, $previous);
+ }
+}