Skip to content

Commit

Permalink
- Fix the case where content was unchanged in the middle of a filter …
Browse files Browse the repository at this point in the history
…chain.

- Change the dry run behaviour.
  • Loading branch information
knu committed Sep 17, 2008
1 parent 73ad595 commit 7191937
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions inplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ def filter(origfile, infile, outfile)
flunk origfile, "file not found"
end

outfile_is_original = !tmpfile?(outfile)

if File.symlink?(outfile)
$dereference or
flunk origfile, "symlink"
Expand Down Expand Up @@ -272,7 +274,7 @@ def filter(origfile, infile, outfile)

if destructive?
debug "cp(%s, %s)", infile, tmpfile
FileUtils.cp(infile, tmpfile) unless $dry_run
FileUtils.cp(infile, tmpfile)
filtercommand = @formatter.format(origfile, tmpfile)
else
filtercommand = @formatter.format(origfile, infile, tmpfile)
Expand All @@ -285,21 +287,22 @@ def filter(origfile, infile, outfile)

if !$accept_empty && File.zero?(tmpfile)
flunk origfile, "empty output"
end unless $dry_run
end

if !$dry_run && FileUtils.cmp(infile, tmpfile)
info "#{origfile}: unchanged"
else
stat = File.stat(infile)
if outfile_is_original && FileUtils.cmp(origfile, tmpfile)
flunk origfile, "unchanged"
end

uninterruptible {
replace(tmpfile, outfile, stat)
}
stat = File.stat(infile)
newstat = File.stat(tmpfile) if $dry_run

uninterruptible {
replace(tmpfile, outfile, stat)
}

newstat = File.stat(outfile)
newstat = File.stat(outfile) unless $dry_run

info "#{origfile}: edited (%d bytes -> %d bytes)", stat.size, newstat.size unless $dry_run
end
info "#{origfile}: edited (%d bytes -> %d bytes)", stat.size, newstat.size
else
flunk origfile, "command exited with %d", $?.exitstatus
end
Expand Down Expand Up @@ -349,7 +352,7 @@ def error(fmt, *args)

def run(command)
debug "command: %s", command
$dry_run or system(command)
system(command)
end

class OverwriteError < RuntimeError
Expand All @@ -363,7 +366,7 @@ def replace(file1, file2, stat)

if $preserve_inode
debug "copy: %s -> %s", file2.shellescape, bakfile.shellescape
FileUtils.cp(file2, bakfile, :preserve => true) unless $dry_run
FileUtils.cp(file2, bakfile) unless $dry_run
else
debug "move: %s -> %s", file2.shellescape, bakfile.shellescape
FileUtils.mv(file2, bakfile) unless $dry_run
Expand Down

0 comments on commit 7191937

Please sign in to comment.