From 3035f78d3e61762526d998a39e09f514e1fcb000 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 1 Dec 2014 16:07:39 -0800 Subject: [PATCH 1/4] Tests to establish errors in #1427 --- features/core.feature | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/features/core.feature b/features/core.feature index 8fbe329dba..dfd8d408a2 100644 --- a/features/core.feature +++ b/features/core.feature @@ -510,3 +510,59 @@ Feature: Manage WordPress installation """ Warning: The 'en_GB' language is active. """ + + Scenario: Ensure file cache isn't corrupted by core update, part one + Given a WP install + And an empty cache + + When I run `wp core update --version=4.0 --force` + Then STDOUT should contain: + """ + Success: WordPress updated successfully + """ + + When I run `wp core version` + Then STDOUT should be: + """ + 4.0 + """ + + When I run `wp core download --version=4.0 --force` + Then STDOUT should contain: + """ + Success: WordPress downloaded + """ + + When I run `wp core version` + Then STDOUT should be: + """ + 4.0 + """ + + Scenario: Ensure file cache isn't corrupted by core update, part two + Given a WP install + And an empty cache + + When I run `wp core download --version=4.0 --force` + Then STDOUT should contain: + """ + Success: WordPress downloaded + """ + + When I run `wp core version` + Then STDOUT should be: + """ + 4.0 + """ + + When I run `wp core update --version=4.0 --force` + Then STDOUT should contain: + """ + Success: WordPress updated successfully + """ + + When I run `wp core version` + Then STDOUT should be: + """ + 4.0 + """ From 07fc75513c29c19583e0ec5659e0e3db2ba4f4a1 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 1 Dec 2014 16:18:22 -0800 Subject: [PATCH 2/4] If there's a bad cache, fall back to downloading fresh --- features/core.feature | 4 ++++ php/commands/core.php | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/features/core.feature b/features/core.feature index dfd8d408a2..896ad52bf7 100644 --- a/features/core.feature +++ b/features/core.feature @@ -532,6 +532,10 @@ Feature: Manage WordPress installation """ Success: WordPress downloaded """ + And STDERR should contain: + """ + Warning: Extraction failed, downloading a new copy... + """ When I run `wp core version` Then STDOUT should be: diff --git a/php/commands/core.php b/php/commands/core.php index a096dd690b..73200db387 100644 --- a/php/commands/core.php +++ b/php/commands/core.php @@ -150,10 +150,18 @@ public function download( $args, $assoc_args ) { $cache_key = "core/$locale-$version.tar.gz"; $cache_file = $cache->has($cache_key); + $bad_cache = false; if ( $cache_file ) { WP_CLI::log( "Using cached file '$cache_file'..." ); - self::_extract( $cache_file, ABSPATH ); - } else { + try{ + self::_extract( $cache_file, ABSPATH ); + } catch ( Exception $e ) { + WP_CLI::warning( "Extraction failed, downloading a new copy..." ); + $bad_cache = true; + } + } + + if ( ! $cache_file || $bad_cache ) { // We need to use a temporary file because piping from cURL to tar is flaky // on MinGW (and probably in other environments too). $temp = sys_get_temp_dir() . '/' . uniqid('wp_') . '.tar.gz'; From f16b0e6e2bf3f02e271380b7c23c0b75f5e7d156 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 1 Dec 2014 16:25:35 -0800 Subject: [PATCH 3/4] Persist package file extension in `WP_CLI\CoreUpgrader` `wp core update` uses ZIP files, while `wp core download` uses gzipped TAR files. Renaming ZIP files to `.tar.gz` causes nasty bugs. Let's keep them cached separately. --- features/core.feature | 15 ++------------- php/WP_CLI/CoreUpgrader.php | 8 +++++--- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/features/core.feature b/features/core.feature index 896ad52bf7..8ba517e5a3 100644 --- a/features/core.feature +++ b/features/core.feature @@ -511,21 +511,10 @@ Feature: Manage WordPress installation Warning: The 'en_GB' language is active. """ - Scenario: Ensure file cache isn't corrupted by core update, part one + Scenario: Ensure file cache isn't corrupted by a ZIP masquerading as a gzipped TAR, part one Given a WP install And an empty cache - - When I run `wp core update --version=4.0 --force` - Then STDOUT should contain: - """ - Success: WordPress updated successfully - """ - - When I run `wp core version` - Then STDOUT should be: - """ - 4.0 - """ + And I run `mkdir -p {SUITE_CACHE_DIR}/core; wget -O {SUITE_CACHE_DIR}/core/en_US-4.0.tar.gz https://wordpress.org/wordpress-4.0.zip` When I run `wp core download --version=4.0 --force` Then STDOUT should contain: diff --git a/php/WP_CLI/CoreUpgrader.php b/php/WP_CLI/CoreUpgrader.php index d3d3692e44..e156649f54 100644 --- a/php/WP_CLI/CoreUpgrader.php +++ b/php/WP_CLI/CoreUpgrader.php @@ -32,11 +32,13 @@ function download_package( $package ) { if ( empty( $package ) ) return new WP_Error( 'no_package', $this->strings['no_package'] ); - $temp = sys_get_temp_dir() . '/' . uniqid('wp_') . '.tar.gz'; + $ext = pathinfo( $package, PATHINFO_EXTENSION ); + + $temp = sys_get_temp_dir() . '/' . uniqid('wp_') . '.' . $ext; $cache = WP_CLI::get_cache(); $update = $GLOBALS['wp_cli_update_obj']; - $cache_key = "core/{$update->locale}-{$update->version}.tar.gz"; + $cache_key = "core/{$update->locale}-{$update->version}.{$ext}"; $cache_file = $cache->has( $cache_key ); if ( $cache_file ) { @@ -46,7 +48,7 @@ function download_package( $package ) { } else { // We need to use a temporary file because piping from cURL to tar is flaky // on MinGW (and probably in other environments too). - $temp = sys_get_temp_dir() . '/' . uniqid('wp_') . '.tar.gz'; + $temp = sys_get_temp_dir() . '/' . uniqid('wp_') . '.' . $ext; $headers = array('Accept' => 'application/json'); $options = array( From d348f0821d936e875c2274e612b4e6070fddf93b Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 2 Dec 2014 07:22:30 -0800 Subject: [PATCH 4/4] Update cache was changed to ZIP --- features/core.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/core.feature b/features/core.feature index 8ba517e5a3..c68af4ba5d 100644 --- a/features/core.feature +++ b/features/core.feature @@ -376,7 +376,7 @@ Feature: Manage WordPress installation When I run `wp core update --version=3.8.1 --force` Then STDOUT should contain: """ - Using cached file '{SUITE_CACHE_DIR}/core/en_US-3.8.1.tar.gz'... + Using cached file '{SUITE_CACHE_DIR}/core/en_US-3.8.1.zip'... """ And STDOUT should not contain: """