From c0cd69d2807bc097bdb102c91da327efdc0a902f Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Thu, 27 Aug 2020 19:03:23 +0200 Subject: [PATCH] DEV: Restoring backups didn't work on macOS This is a follow-up for f51ccea02816c768863b1073977db219f4b66ff3 because specs were failing on macOS. macOS uses bsdtar by default and the --transform option isn't supported by bsdtar. But the -s option is and it works the same. --- lib/backup_restore/backup_file_handler.rb | 27 ++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/backup_restore/backup_file_handler.rb b/lib/backup_restore/backup_file_handler.rb index e0244b4787..86103663e2 100644 --- a/lib/backup_restore/backup_file_handler.rb +++ b/lib/backup_restore/backup_file_handler.rb @@ -64,12 +64,19 @@ module BackupRestore def decompress_archive return if !@is_archive + # the transformation is a workaround for a bug which existed between v2.6.0.beta1 and v2.6.0.beta2 + path_transformation = + case tar_implementation + when :gnu + ['--transform', 's|var/www/discourse/public/uploads/|uploads/|'] + when :bsd + ['-s', '|var/www/discourse/public/uploads/|uploads/|'] + end + log "Unzipping archive, this may take a while..." Discourse::Utils.execute_command( 'tar', '--extract', '--gzip', '--file', @archive_path, '--directory', @tmp_directory, - '--transform', 's|var/www/discourse/public/uploads/|uploads/|', - # the transformation is a workaround for a bug which existed between v2.6.0.beta1 and v2.6.0.beta2 - failure_message: "Failed to decompress archive." + *path_transformation, failure_message: "Failed to decompress archive." ) end @@ -95,5 +102,19 @@ module BackupRestore def available_size SiteSetting.decompressed_backup_max_file_size_mb end + + def tar_implementation + @tar_version ||= begin + tar_version = Discourse::Utils.execute_command('tar', '--version') + + if tar_version.include?("GNU tar") + :gnu + elsif tar_version.include?("bsdtar") + :bsd + else + raise "Unknown tar implementation: #{tar_version}" + end + end + end end end