7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/barron/tmpfile.rb', line 7
def lock name, options={}
options = {
:block => true,
:tmpdir => '/tmp'
}.merge(options)
value = nil
open("#{options[:tmpdir]}/barron-#{Digest::MD5.hexdigest("#{name}")}", File::RDWR|File::CREAT, 0666) do |f|
begin
if options[:block]
f.flock(File::LOCK_EX)
begin
f.chmod(0666)
rescue Errno::EPERM
end
value = yield f
elsif f.flock(File::LOCK_EX|File::LOCK_NB)
begin
f.chmod(0666)
rescue Errno::EPERM
end
value = yield f
end
ensure
f.flock(File::LOCK_UN)
end
end
value
end
|