diff --git a/lib/rmt/mirror/base.rb b/lib/rmt/mirror/base.rb index 2b521cc00..43bb3023c 100644 --- a/lib/rmt/mirror/base.rb +++ b/lib/rmt/mirror/base.rb @@ -157,6 +157,7 @@ def cleanup_stale_metadata def move_files(glob:, destination:) FileUtils.mkpath(destination) unless Dir.exist?(destination) + FileUtils.rm_rf(Dir.glob(File.join(destination, '*'))) FileUtils.mv(Dir.glob(glob), destination, force: true) rescue StandardError => e raise RMT::Mirror::Exception.new(_('Error while moving files %{glob} to %{dest}: %{error}') % { diff --git a/package/obs/rmt-server.changes b/package/obs/rmt-server.changes index c84e3dbf6..71fdda942 100644 --- a/package/obs/rmt-server.changes +++ b/package/obs/rmt-server.changes @@ -13,6 +13,7 @@ Wed Oct 30 09:01:32 UTC 2024 - Natnael Getahun * Allow SLE Micro system to access SLES repositories (bsc#1230419) * Skip system token rotation in read-only APIs * Enable RMT to handle the new dl.suse.com CDN domain (bsc#1234641) + * Clean up RMT metadata after repo update (bsc#1233308) ------------------------------------------------------------------- Wed Aug 21 15:28:43 UTC 2024 - Jesús Bermúdez Velázquez diff --git a/spec/lib/rmt/mirror/base_spec.rb b/spec/lib/rmt/mirror/base_spec.rb index 1add79b21..01453a459 100644 --- a/spec/lib/rmt/mirror/base_spec.rb +++ b/spec/lib/rmt/mirror/base_spec.rb @@ -230,6 +230,40 @@ expect { base.move_files(glob: src, destination: dest) }.to raise_exception(/Error while moving files/) end + + it 'removes existing files in the destination directory before moving' do + allow(Dir).to receive(:exist?).with(dest).and_return(true) + allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return(%w[/destination/path/file1.txt /destination/path/file2.txt]) + allow(Dir).to receive(:glob).with(src).and_return(%w[/source/path/newfile1.txt /source/path/newfile2.txt]) + expect(FileUtils).not_to receive(:mkpath).with(dest) + expect(FileUtils).to receive(:rm_rf).with(%w[/destination/path/file1.txt /destination/path/file2.txt]) + expect(FileUtils).to receive(:mv).with(%w[/source/path/newfile1.txt /source/path/newfile2.txt], dest, force: true) + + base.move_files(glob: src, destination: dest) + end + + it 'removes existing files when destination directory is empty' do + allow(Dir).to receive(:exist?).with(dest).and_return(false) + allow(Dir).to receive(:glob).with(src).and_return(%w[/source/path/newfile1.txt /source/path/newfile2.txt]) + allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return([]) + expect(FileUtils).to receive(:mkpath).with(dest) + expect(FileUtils).to receive(:rm_rf).with([]) + expect(FileUtils).to receive(:mv).with(%w[/source/path/newfile1.txt /source/path/newfile2.txt], dest, force: true) + + base.move_files(glob: src, destination: dest) + end + + it 'handles errors during removal of existing files' do + allow(Dir).to receive(:exist?).with(dest).and_return(true) + existing_files = ['/destination/path/file1.txt'] + + allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return(existing_files) + allow(FileUtils).to receive(:rm_rf).with(existing_files).and_raise(StandardError.new('Remove failed')) + + expect do + base.move_files(glob: src, destination: dest) + end.to raise_exception(/Error while moving files/) + end end describe '#need_to_download?' do