summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-07 18:52:22 +0000
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-07 18:52:22 +0000
commitf9e5b3bace9add673250078dd61ffc1e7d64efee (patch)
tree35c78a4fb1a2acf4b2c1f7574bd86df36cb5296c /lib
parent41c8c78f11a41c89da33adbcc4cd2e877d905a22 (diff)
merge revision(s) 13935:
* lib/cgi/session.rb (CGI::Session::FileStore::restore): use lockfile for exclusive locks. a patch from <tommy AT tmtm.org>. [ruby-dev:32296] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_5@16937 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/cgi/session.rb16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/cgi/session.rb b/lib/cgi/session.rb
index 82eb7534d8..5568b28dce 100644
--- a/lib/cgi/session.rb
+++ b/lib/cgi/session.rb
@@ -391,8 +391,9 @@ class CGI
unless @hash
@hash = {}
begin
+ lockf = File.open(@path+".lock", "r")
+ lockf.flock File::LOCK_SH
f = File.open(@path, 'r')
- f.flock File::LOCK_SH
for line in f
line.chomp!
k, v = line.split('=',2)
@@ -400,6 +401,7 @@ class CGI
end
ensure
f.close unless f.nil?
+ lockf.close if lockf
end
end
@hash
@@ -409,13 +411,17 @@ class CGI
def update
return unless @hash
begin
- f = File.open(@path, File::CREAT|File::TRUNC|File::RDWR, 0600)
- f.flock File::LOCK_EX
+ lockf = File.open(@path+".lock", File::CREAT|File::RDWR, 0600)
+ lockf.flock File::LOCK_EX
+ f = File.open(@path+".new", File::CREAT|File::TRUNC|File::WRONLY, 0600)
for k,v in @hash
f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(v))
end
+ f.close
+ File.rename @path+".new", @path
ensure
- f.close unless f.nil?
+ f.close if f and !f.closed?
+ lockf.close if lockf
end
end
@@ -426,6 +432,8 @@ class CGI
# Close and delete the session's FileStore file.
def delete
+ File::unlink @path+".lock" rescue nil
+ File::unlink @path+".new" rescue nil
File::unlink @path
rescue Errno::ENOENT
end