summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/file_locking.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/util/file_locking.rb')
-rw-r--r--lib/puppet/util/file_locking.rb15
1 files changed, 6 insertions, 9 deletions
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