From 29b6dfbce4bb2902fc0000e0bbb7edb464964c26 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 23 Jan 2013 08:22:27 +0100 Subject: [PATCH] Added file locking for file creation. --- lib/session.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/session.rb b/lib/session.rb index 77adc0b6b..1498c76e1 100644 --- a/lib/session.rb +++ b/lib/session.rb @@ -71,7 +71,7 @@ module Session def self.send( client_id, data ) path = @path + '/' + client_id.to_s + '/' - filename = 'send-' + Time.new().to_i.to_s + '-' + rand(999999).to_s + filename = 'send-' + Time.new().to_i.to_s + '-' + rand(99999999).to_s if File::exists?( path + filename ) filename = filename + '-1' if File::exists?( path + filename ) @@ -79,15 +79,19 @@ module Session if File::exists?( path + filename ) filename = filename + '-1' if File::exists?( path + filename ) - filename = filename + '-' + rand(999999).to_s + filename = filename + '-' + rand(99999999).to_s end end end end return false if !File.directory? path File.open( path + 'a-' + filename, 'w' ) { |file| + file.flock( File::LOCK_EX ) file.puts data.to_json + file.flock( File::LOCK_UN ) + file.close } + FileUtils.mv( path + 'a-' + filename, path + filename) return true end