From 1afa597aa26380440077ef0a59670ef7a011ca45 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 20 Dec 2025 16:08:46 +0900 Subject: [PATCH 1/2] Fix in-place editing to use atomic file operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use temporary files for atomic backup creation and fix related tests. Prevents data corruption during in-place editing operations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- mrblib/rf/runner.rb | 21 +++++++++------------ spec/global_options/in_place_option_spec.rb | 6 +++++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/mrblib/rf/runner.rb b/mrblib/rf/runner.rb index 62ec7d0..fc6cbfd 100644 --- a/mrblib/rf/runner.rb +++ b/mrblib/rf/runner.rb @@ -51,7 +51,6 @@ def run if in_place write_file = write_open(filename, in_place) $output = write_file - tempfile = write_file if in_place.empty? end records = filter.new(input) @@ -60,11 +59,11 @@ def run binary_match = apply_expressions(records) warn_binary_match(filename) if binary_match - next unless tempfile + next if $output == $stdout - tempfile.close + $output.close input.close - File.rename(tempfile.path, filename) + File.rename($output.path, filename) end end @@ -83,15 +82,13 @@ def read_open(file) end def write_open(file, in_place) - if in_place.empty? - dir = File.dirname(file) - Tempfile.new('.rf', dir) - else - raise NotFound, file unless File.exist?(file) - raise NotRegularFile, file unless File.file?(file) + raise NotFound, file unless File.exist?(file) + raise NotRegularFile, file unless File.file?(file) - File.open("#{file}#{in_place}", 'w') - end + File.rename(file, "#{file}#{in_place}") if in_place + + dir = File.dirname(file) + Tempfile.new('.rf', dir) end def split(val) diff --git a/spec/global_options/in_place_option_spec.rb b/spec/global_options/in_place_option_spec.rb index c18dffd..8c85bb1 100644 --- a/spec/global_options/in_place_option_spec.rb +++ b/spec/global_options/in_place_option_spec.rb @@ -19,9 +19,13 @@ end it_behaves_like 'a successful exec' do - let(:file_content) { read_file("foo#{suffix}") } + let(:file_content) { read_file('foo') } it { expect(file_content).to eq "bar\n" } + + it 'creates backup file when suffix is provided' do + expect(read_file("foo#{suffix}")).to eq 'foo' unless suffix.empty? + end end end From 44971b30c153326bf138c48eb2b22954c7cab87d Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 20 Dec 2025 18:14:37 +0900 Subject: [PATCH 2/2] Replace File.rename to File.write --- mrblib/rf/runner.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mrblib/rf/runner.rb b/mrblib/rf/runner.rb index fc6cbfd..8943f1b 100644 --- a/mrblib/rf/runner.rb +++ b/mrblib/rf/runner.rb @@ -85,7 +85,11 @@ def write_open(file, in_place) raise NotFound, file unless File.exist?(file) raise NotRegularFile, file unless File.file?(file) - File.rename(file, "#{file}#{in_place}") if in_place + if in_place + File.open("#{file}#{in_place}", 'w') do |f| # rubocop: disable Style/FileWrite + f.write(File.read(file)) + end + end dir = File.dirname(file) Tempfile.new('.rf', dir)