Description:
fixed bug in dir_init teardown (wrong log filename)
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r109:f75d1acc9cdd - - 1 file changed: 6 inserted, 2 deleted

@@ -1,109 +1,113
1 1 require 'ftools'
2 2
3 3 # DirInit::Manager handles directory initialization and clean-up when
4 4 # there are many concurrent processes that wants to modify the
5 5 # directory in the same way.
6 6 #
7 7 # An example usage is when each process wants to copy some temporary
8 8 # files to the directory and delete these files after finishing its
9 9 # job. Problems may occur when the first process delete the files
10 10 # while the second process is still using the files.
11 11 #
12 12 # This library maintain a reference counter on the processes using the
13 13 # directory. It locks the dir to manage critical section when
14 14 # updating the reference counter.
15 15
16 16 module DirInit
17 17
18 18 class Manager
19 19
20 20 def initialize(dir_name, usage_filename='.usage_counter')
21 21 @dir_name = dir_name
22 22 @usage_filename = usage_filename
23 23 end
24 24
25 + def lock_filename
26 + return @dir_name + '/lockfile'
27 + end
28 +
25 29 # Check if someone has initialized the dir. If not, call block.
26 30
27 31 def setup # :yields: block
28 - dir = File.new(@dir_name + '/lockfile',"w+")
32 + dir = File.new(lock_filename,"w+")
29 33 dir.flock(File::LOCK_EX)
30 34 begin
31 35 counter_filename = get_counter_filename
32 36 if File.exist? counter_filename
33 37 # someone is here
34 38 f = File.new(counter_filename,"r+")
35 39 counter = f.read.to_i
36 40 f.seek(0)
37 41 f.write("#{counter+1}\n")
38 42 f.close
39 43 else
40 44 # i'm the first, create the counter file
41 45 counter = 0
42 46 f = File.new(counter_filename,"w")
43 47 f.write("1\n")
44 48 f.close
45 49 end
46 50
47 51 # if no one is here
48 52 if counter == 0
49 53 if block_given?
50 54 yield
51 55 end
52 56 end
53 57
54 58 rescue
55 59 raise
56 60
57 61 ensure
58 62 # make sure it unlock the directory
59 63 dir.flock(File::LOCK_UN)
60 64 dir.close
61 65 end
62 66 end
63 67
64 68 # Check if I am the last one using the dir. If true, call block.
65 69
66 70 def teardown
67 - dir = File.new(@dir_name)
71 + dir = File.new(lock_filename)
68 72 dir.flock(File::LOCK_EX)
69 73 begin
70 74 counter_filename = get_counter_filename
71 75 if File.exist? counter_filename
72 76 # someone is here
73 77 f = File.new(counter_filename,"r+")
74 78 counter = f.read.to_i
75 79 f.seek(0)
76 80 f.write("#{counter-1}\n")
77 81 f.close
78 82
79 83 if counter == 1
80 84 # i'm the last one
81 85
82 86 File.delete(counter_filename)
83 87 if block_given?
84 88 yield
85 89 end
86 90 end
87 91 else
88 92 # This is BAD
89 93 raise "Error: reference count missing"
90 94 end
91 95
92 96 rescue
93 97 raise
94 98
95 99 ensure
96 100 # make sure it unlock the directory
97 101 dir.flock(File::LOCK_UN)
98 102 dir.close
99 103 end
100 104 end
101 105
102 106 protected
103 107
104 108 def get_counter_filename
105 109 return File.join(@dir_name,@usage_filename)
106 110 end
107 111
108 112 end
109 113 end
You need to be logged in to leave comments. Login now