From 4820ddf2dd87e9188270df84f64172dddad61a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draic=20Brady?= Date: Wed, 17 May 2017 20:03:26 +0100 Subject: [PATCH 1/5] Move exception handling for downloads - closes #40 Ensure backup phar (made pre-download) is deleted if download fails --- src/Updater.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Updater.php b/src/Updater.php index 0e03277..405200f 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -137,9 +137,19 @@ public function update() || (!is_bool($this->newVersionAvailable) && !$this->hasUpdate())) { return false; } + $this->backupPhar(); - $this->downloadPhar(); + + try { + $this->downloadPhar(); + } catch (\Exception $e) { + restore_error_handler(); + $this->cleanupAfterError(); + throw $e; + } + $this->replacePhar(); + return true; } @@ -375,13 +385,7 @@ protected function downloadPhar() } } - try { - $this->validatePhar($this->getTempPharFile()); - } catch (\Exception $e) { - restore_error_handler(); - $this->cleanupAfterError(); - throw $e; - } + $this->validatePhar($this->getTempPharFile()); } protected function replacePhar() @@ -498,7 +502,7 @@ protected function validatePhar($phar) protected function cleanupAfterError() { - //@unlink($this->getBackupPharFile()); + @unlink($this->getBackupPharFile()); @unlink($this->getTempPharFile()); @unlink($this->getTempPubKeyFile()); } From e8f293a172b724ba15407ea85454ad1bbd412929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draic=20Brady?= Date: Thu, 18 May 2017 11:24:47 +0100 Subject: [PATCH 2/5] Remove unnecessary cleanupAfterError() call --- src/Updater.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Updater.php b/src/Updater.php index 405200f..bf7bb9a 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -375,7 +375,6 @@ protected function downloadPhar() $algo = 'SHA-256'; } if ($tmpVersion !== $this->getNewVersion()) { - $this->cleanupAfterError(); throw new HttpRequestException(sprintf( 'Download file appears to be corrupted or outdated. The file ' . 'received does not have the expected %s hash: %s.', From 792ff810524f14a14425e4227574c7b62821b508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draic=20Brady?= Date: Thu, 18 May 2017 11:26:33 +0100 Subject: [PATCH 3/5] Remove unnecessary restore_error_handler() Should be called within Strategy classes as part of download() methods (if used). --- src/Updater.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Updater.php b/src/Updater.php index bf7bb9a..1cce9aa 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -143,7 +143,6 @@ public function update() try { $this->downloadPhar(); } catch (\Exception $e) { - restore_error_handler(); $this->cleanupAfterError(); throw $e; } From 0f353f16de684224915d8c490c5b3903029e22de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draic=20Brady?= Date: Thu, 18 May 2017 11:27:53 +0100 Subject: [PATCH 4/5] Trailing space --- src/Updater.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Updater.php b/src/Updater.php index 1cce9aa..7f61228 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -137,7 +137,7 @@ public function update() || (!is_bool($this->newVersionAvailable) && !$this->hasUpdate())) { return false; } - + $this->backupPhar(); try { From 6bdbeefa1396a34e80b976a08451618423345d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draic=20Brady?= Date: Fri, 19 May 2017 10:36:19 +0100 Subject: [PATCH 5/5] Relocate restore_error_handler calls --- src/Updater.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Updater.php b/src/Updater.php index 7f61228..6dfb8d3 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -485,13 +485,13 @@ protected function validatePhar($phar) /** Switch invalid key errors to RuntimeExceptions */ set_error_handler(array($this, 'throwRuntimeException')); $phar = new \Phar($phar); + restore_error_handler(); $signature = $phar->getSignature(); if ($this->hasPubKey() && strtolower($signature['hash_type']) !== 'openssl') { throw new NoSignatureException( 'The downloaded phar file has no OpenSSL signature.' ); } - restore_error_handler(); if ($this->hasPubKey()) { @unlink($phar . '.pubkey'); } @@ -500,6 +500,7 @@ protected function validatePhar($phar) protected function cleanupAfterError() { + restore_error_handler(); @unlink($this->getBackupPharFile()); @unlink($this->getTempPharFile()); @unlink($this->getTempPubKeyFile());