From 6ec9d204804bff078820652386b7a6cbe37a57dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz=20Gonz=C3=A1lez?= Date: Wed, 12 Sep 2018 12:21:26 +0100 Subject: [PATCH 1/3] Only restore backups created after boot --- src/modules/Update.rb | 24 +++++++++++++++++++----- test/update_test.rb | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/modules/Update.rb b/src/modules/Update.rb index c45f6855..e97d5ad7 100644 --- a/src/modules/Update.rb +++ b/src/modules/Update.rb @@ -801,16 +801,25 @@ def clean_backup :force => true, :secure => true) end - # restores backup + # Restores valid backups + # + # It will look for "restore-*.sh" files inside the BACKUP_DIR, executing only those created + # after last boot (bsc#1097297) def restore_backup log.info "Restoring backup" mounted_root = Installation.destdir - script_glob = File.join(mounted_root, BACKUP_DIR,"restore-*.sh") + script_glob = File.join(mounted_root, BACKUP_DIR, "restore-*.sh") # sort the scripts to execute them in the expected order ::Dir.glob(script_glob).sort.each do |path| - cmd = "sh #{path} #{File.join("/", mounted_root)}" - res = SCR.Execute(path(".target.bash_output"), cmd) - log.info "Restoring with script #{cmd} result: #{res}" + backup_time = File.stat(path).ctime.to_i + + if boot_time < backup_time + cmd = "sh #{path} #{File.join("/", mounted_root)}" + res = SCR.Execute(path(".target.bash_output"), cmd) + log.info "Restoring with script #{cmd} result: #{res}" + else + log.info "Discarding #{path} (created before last boot)" + end end end @@ -850,6 +859,11 @@ def restore_backup private + # @return [Integer] boot time found in /proc/stat + def boot_time + @boot_time ||= IO.read("/proc/stat")[/btime.*/].split.last.to_i + end + def create_tarball(tarball_path, root, paths) # tar reports an error if a file does not exist. # So we have to check this before. diff --git a/test/update_test.rb b/test/update_test.rb index cf73821c..aec5a3fe 100755 --- a/test/update_test.rb +++ b/test/update_test.rb @@ -137,12 +137,39 @@ def default_SetDesktopPattern_stubs end describe "#restore_backup" do - it "call all restore scripts in backup directory" do - expect(::Dir).to receive(:glob).and_return(["restore-a.sh", "restore-b.sh"]) - expect(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".target.bash_output"), /sh .*restore-a.sh \/mnt/). - and_return({"exit" => 0}) - expect(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".target.bash_output"), /sh .*restore-b.sh \/mnt/). - and_return({"exit" => 0}) + let(:boot_time) { 1514764800 } + let(:valid_backup_time) { boot_time + 1000 } + let(:invalid_backup_time) { boot_time - 1000 } + let(:first_backup) { "000-restore-c.sh" } + let(:second_backup) { "restore-a.sh" } + let(:third_backup) { "restore-b.sh" } + let(:mock_stat) { double({ ctime: nil }) } + + before do + allow(::Dir).to receive(:glob).and_return([third_backup, first_backup, second_backup]) + + allow(::File).to receive(:stat).with(anything).and_return(mock_stat) + allow(mock_stat).to receive(:ctime) + .and_return(valid_backup_time, valid_backup_time, invalid_backup_time) + + allow(Yast::Update).to receive(:boot_time).and_return(boot_time) + end + + it "found all available backups" do + expect(::Dir).to receive(:glob).and_return(["restore-b.sh", "000-restore-c.sh", "restore-a.sh"]) + + Yast::Update.restore_backup + end + + it "execute only backups created after boot" do + expect(Yast::SCR).to receive(:Execute) + .with(Yast::Path.new(".target.bash_output"), /sh .*000-restore-c.sh \/mnt/) + .and_return({"exit" => 0}) + expect(Yast::SCR).to receive(:Execute) + .with(Yast::Path.new(".target.bash_output"), /sh .*restore-a.sh \/mnt/) + .and_return({"exit" => 0}) + expect(Yast::SCR).to_not receive(:Execute) + .with(Yast::Path.new(".target.bash_output"), /sh .*restore-b.sh \/mnt/) Yast::Update.restore_backup end From 9f1045cb0310c6f4826b6f8b1185d27258f4164e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz=20Gonz=C3=A1lez?= Date: Wed, 12 Sep 2018 12:42:23 +0100 Subject: [PATCH 2/3] Remove trailing spaces --- src/modules/Update.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/modules/Update.rb b/src/modules/Update.rb index e97d5ad7..3099e452 100644 --- a/src/modules/Update.rb +++ b/src/modules/Update.rb @@ -285,7 +285,7 @@ def ProductsCompatible found = Builtins.find(to_install) { |u| u == i } found != nil end - @_products_compatible = equal_product != nil + @_products_compatible = equal_product != nil # no product name found # bugzilla #218720, valid without testing according to comment #10 else @@ -430,7 +430,7 @@ def GetProductName # Remove 'Beta...' from product release if Builtins.regexpmatch(old_name, "Beta") - old_name = Builtins.regexpsub(old_name, "^(.*)[ \t]+Beta.*$", "\\1") + old_name = Builtins.regexpsub(old_name, "^(.*)[ \t]+Beta.*$", "\\1") # Remove 'Alpha...' from product release elsif Builtins.regexpmatch(old_name, "Alpha") old_name = Builtins.regexpsub(old_name, "^(.*)[ \t]+Alpha.*$", "\\1") @@ -467,7 +467,7 @@ def GetProductName Installation.installedVersion, "major", Builtins.tointeger(inst_ver) - ) + ) # openSUSE elsif Builtins.regexpmatch(inst_ver, "^[0123456789]+.[0123456789]+$") Ops.set( @@ -550,7 +550,7 @@ def GetProductName Ops.subtract(num, 1), 0 ) - end + end # default for !Stage::normal else update_to_source = Packages.GetBaseSourceID @@ -624,7 +624,7 @@ def GetProductName Installation.updateVersion, "major", Builtins.tointeger(new_ver) - ) + ) # openSUSE elsif Builtins.regexpmatch(new_ver, "^[0123456789]+.[0123456789]$") Ops.set( From 58f33399315de6b9fc26ce26ebbcbdbd3a1a1e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz=20Gonz=C3=A1lez?= Date: Wed, 12 Sep 2018 12:46:36 +0100 Subject: [PATCH 3/3] Update version and changelog --- package/yast2-update.changes | 6 ++++++ package/yast2-update.spec | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/package/yast2-update.changes b/package/yast2-update.changes index 761f38f4..4feea119 100644 --- a/package/yast2-update.changes +++ b/package/yast2-update.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Wed Sep 12 11:44:09 UTC 2018 - dgonzalez@suse.com + +- Avoid to restore old backups when upgrade fails (bsc#1097297) +- 4.1.3 + ------------------------------------------------------------------- Tue Sep 11 15:34:53 CEST 2018 - aschnell@suse.com diff --git a/package/yast2-update.spec b/package/yast2-update.spec index e63e7153..530e1cfd 100644 --- a/package/yast2-update.spec +++ b/package/yast2-update.spec @@ -17,7 +17,7 @@ Name: yast2-update -Version: 4.1.2 +Version: 4.1.3 Release: 0 BuildRoot: %{_tmppath}/%{name}-%{version}-build