Rather than using a content-checksum to name the files, we can just use the
combination of a PID and serial number to give a very high chance of being
unique in a single process.

(This will typically fail only if we have multiple concurrent threads and
 lose a race on the lock-free counter, or have wrapped pids and had spooled
 reports from a previous instance with the same pid.)

We ensure uniqueness using O_CREAT|O_EXCL, so we have the assurance from the
OS that the race will never result in overwritten files, just an exception and
retry.

Reviewed-By: Matt Robinson <[email protected]>
---
 app/controllers/reports_controller.rb |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/app/controllers/reports_controller.rb 
b/app/controllers/reports_controller.rb
index 5952c73..01d0d9a 100644
--- a/app/controllers/reports_controller.rb
+++ b/app/controllers/reports_controller.rb
@@ -25,12 +25,13 @@ class ReportsController < InheritedResources::Base
     end
   end
 
+  filter_parameter_logging :report
+
   def upload
     begin
+      @@n ||= 0
       yaml = params[:report][:report]
-      md5  = Digest::MD5.hexdigest(yaml)
-      file = Rails.root + 'spool' + "#{md5}.yaml"
-      n    = 0
+      file = Rails.root + 'spool' + "report-#{$$}-#{@@n += 1}.yaml"
 
       begin
         fd = File.new(file, File::CREAT|File::EXCL|File::RDWR, 0600)
@@ -38,9 +39,9 @@ class ReportsController < InheritedResources::Base
         fd.close
 
         Report.delay.create_from_yaml_file(file.to_s, :delete => true)
-        render :text => "Report queued for import as #{md5}"
+        render :text => "Report queued for import as #{file.basename}"
       rescue Errno::EEXIST
-        file = Rails.root + 'spool' + "#{md5}-#{n += 1}.yaml"
+        file = Rails.root + 'spool' + "report-#{$$}-#{@@n += 1}.yaml"
         retry
       end
     rescue => e
-- 
1.7.5.4

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to