From cf19bd8dea141a59cdff5a7f1edc56d3620ab0e2 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Mon, 15 Dec 2008 00:22:09 -0600 Subject: Not using a temporary file when locking files for writing. The temporary file was not actually useful, because we could never really get atomic renames, for annoying, complicated reasons. This hopefully finally fixes #1812. Signed-off-by: Luke Kanies --- lib/puppet/util/file_locking.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'lib/puppet/util') diff --git a/lib/puppet/util/file_locking.rb b/lib/puppet/util/file_locking.rb index 80a0b2b0c..101ece308 100644 --- a/lib/puppet/util/file_locking.rb +++ b/lib/puppet/util/file_locking.rb @@ -21,6 +21,11 @@ module Puppet::Util::FileLocking tmpfile = file + ".tmp" unless mode + # It's far more likely that the file will be there than not, so it's + # better to stat once to check for existence and mode. + # If we can't stat, it's most likely because the file's not there, + # but could also be because the directory isn't readable, in which case + # we won't be able to write anyway. begin mode = File.stat(file).mode rescue @@ -31,15 +36,7 @@ module Puppet::Util::FileLocking Puppet::Util.sync(file).synchronize(Sync::EX) do File.open(file, "w", mode) do |rf| rf.lock_exclusive do |lrf| - File.open(tmpfile, "w", mode) do |tf| - yield tf - end - begin - File.rename(tmpfile, file) - rescue => detail - File.unlink(tmpfile) if File.exist?(tmpfile) - raise Puppet::Error, "Could not rename %s to %s: %s; file %s was unchanged" % [file, tmpfile, Thread.current.object_id, detail, file] - end + yield lrf end end end -- cgit