1 | //----------------------------------------------------------- |
---|
2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
3 | // arnault@lal.in2p3.fr |
---|
4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
5 | //----------------------------------------------------------- |
---|
6 | |
---|
7 | #include "cmt_std.h" |
---|
8 | #include "cmt_lock.h" |
---|
9 | #include "cmt_system.h" |
---|
10 | #include "cmt_error.h" |
---|
11 | #include "cmt_symbol.h" |
---|
12 | #include "cmt_log.h" |
---|
13 | |
---|
14 | //---------------------------------------------------------- |
---|
15 | CmtLock::status CmtLock::lock () |
---|
16 | { |
---|
17 | // status s = check (); |
---|
18 | |
---|
19 | // switch (s) |
---|
20 | // { |
---|
21 | // case locked_by_user: |
---|
22 | // cerr << "Package already locked by you" << endl; |
---|
23 | // return (s); |
---|
24 | // case locked_by_another_user: |
---|
25 | // CmtError::set (CmtError::cannot_lock, "lock> Package already locked by another user"); |
---|
26 | // return (s); |
---|
27 | // case not_locked: |
---|
28 | // break; |
---|
29 | // } |
---|
30 | |
---|
31 | // cmt_string text = "locked by "; |
---|
32 | // text += CmtSystem::user (); |
---|
33 | // text += " date "; |
---|
34 | // text += CmtSystem::now (); |
---|
35 | |
---|
36 | // if (!text.write ("lock.cmt")) |
---|
37 | // { |
---|
38 | // CmtError::set (CmtError::cannot_write_lock, "lock>"); |
---|
39 | // return (still_unlocked); |
---|
40 | // } |
---|
41 | |
---|
42 | // cmt_string command = lock_command->build_macro_value (); |
---|
43 | |
---|
44 | cmt_string lock_command; |
---|
45 | Symbol* lock_command_macro = Symbol::find ("lock_command"); |
---|
46 | if (lock_command_macro != 0) |
---|
47 | { |
---|
48 | lock_command = lock_command_macro->resolve_macro_value (); |
---|
49 | } |
---|
50 | if (lock_command == "") |
---|
51 | { |
---|
52 | CmtError::set (CmtError::cannot_write_lock, "lock>"); |
---|
53 | return (still_unlocked); |
---|
54 | } |
---|
55 | |
---|
56 | cmt_string lock_file; |
---|
57 | Symbol* lock_file_macro = Symbol::find ("lock_file"); |
---|
58 | if (lock_file_macro != 0) |
---|
59 | { |
---|
60 | lock_file = lock_file_macro->resolve_macro_value (); |
---|
61 | } |
---|
62 | if (lock_file == "") |
---|
63 | { |
---|
64 | CmtError::set (CmtError::cannot_write_lock, "lock>"); |
---|
65 | return (still_unlocked); |
---|
66 | } |
---|
67 | |
---|
68 | cmt_string command = lock_command + " " + lock_file; |
---|
69 | |
---|
70 | if (CmtSystem::execute (command) != 0) |
---|
71 | { |
---|
72 | CmtError::set (CmtError::cannot_run_lock_command, "lock>"); |
---|
73 | return (still_unlocked); |
---|
74 | } |
---|
75 | |
---|
76 | CmtMessage::info ("Package now locked"); |
---|
77 | // cerr << "Package now locked" << endl; |
---|
78 | |
---|
79 | return (locked_by_user); |
---|
80 | } |
---|
81 | |
---|
82 | //---------------------------------------------------------- |
---|
83 | CmtLock::status CmtLock::unlock () |
---|
84 | { |
---|
85 | status s = check (); |
---|
86 | |
---|
87 | switch (s) |
---|
88 | { |
---|
89 | case locked_by_user: |
---|
90 | break; |
---|
91 | case locked_by_another_user: |
---|
92 | CmtError::set (CmtError::cannot_unlock, "unlock> Package locked by another user"); |
---|
93 | return (s); |
---|
94 | case not_locked: |
---|
95 | CmtMessage::info ("Package not locked"); |
---|
96 | // cerr << "Package not locked" << endl; |
---|
97 | return (s); |
---|
98 | } |
---|
99 | |
---|
100 | // Symbol* unlock_command = Symbol::find ("unlock_command"); |
---|
101 | // if (unlock_command != 0) |
---|
102 | // { |
---|
103 | // cmt_string command = unlock_command->build_macro_value (); |
---|
104 | |
---|
105 | // if (command != "") |
---|
106 | // { |
---|
107 | // if (CmtSystem::execute (command) != 0) |
---|
108 | // { |
---|
109 | // CmtError::set (CmtError::cannot_run_unlock_command, "unlock>"); |
---|
110 | // return (still_locked); |
---|
111 | // } |
---|
112 | // } |
---|
113 | // } |
---|
114 | |
---|
115 | // if (!CmtSystem::remove_file ("lock.cmt")) |
---|
116 | // { |
---|
117 | // CmtError::set (CmtError::cannot_remove_lock, "unlock>"); |
---|
118 | // return (still_locked); |
---|
119 | // } |
---|
120 | |
---|
121 | cmt_string unlock_command; |
---|
122 | Symbol* unlock_command_macro = Symbol::find ("unlock_command"); |
---|
123 | if (unlock_command_macro != 0) |
---|
124 | { |
---|
125 | unlock_command = unlock_command_macro->resolve_macro_value (); |
---|
126 | } |
---|
127 | if (unlock_command == "") |
---|
128 | { |
---|
129 | CmtError::set (CmtError::cannot_remove_lock, "unlock>"); |
---|
130 | return (still_locked); |
---|
131 | } |
---|
132 | |
---|
133 | cmt_string lock_file; |
---|
134 | Symbol* lock_file_macro = Symbol::find ("lock_file"); |
---|
135 | if (lock_file_macro != 0) |
---|
136 | { |
---|
137 | lock_file = lock_file_macro->resolve_macro_value (); |
---|
138 | } |
---|
139 | if (lock_file == "") |
---|
140 | { |
---|
141 | CmtError::set (CmtError::cannot_remove_lock, "unlock>"); |
---|
142 | return (still_locked); |
---|
143 | } |
---|
144 | |
---|
145 | cmt_string command = unlock_command + " " + lock_file; |
---|
146 | |
---|
147 | if (CmtSystem::execute (command) != 0) |
---|
148 | { |
---|
149 | CmtError::set (CmtError::cannot_run_unlock_command, "unlock>"); |
---|
150 | return (still_locked); |
---|
151 | } |
---|
152 | |
---|
153 | CmtMessage::info ("Package now unlocked"); |
---|
154 | // cerr << "Package now unlocked" << endl; |
---|
155 | |
---|
156 | return (not_locked); |
---|
157 | } |
---|
158 | |
---|
159 | //---------------------------------------------------------- |
---|
160 | CmtLock::status CmtLock::check () |
---|
161 | { |
---|
162 | // cmt_string me = CmtSystem::user (); |
---|
163 | // cmt_string text; |
---|
164 | |
---|
165 | // if (text.read ("lock.cmt")) |
---|
166 | // { |
---|
167 | // CmtSystem::cmt_string_vector words; |
---|
168 | |
---|
169 | // CmtSystem::split (text, " ", words); |
---|
170 | |
---|
171 | // if (words.size () >= 3) |
---|
172 | // { |
---|
173 | // if (words[2] == me) |
---|
174 | // { |
---|
175 | // return (locked_by_user); |
---|
176 | // } |
---|
177 | // else |
---|
178 | // { |
---|
179 | // return (locked_by_another_user); |
---|
180 | // } |
---|
181 | // } |
---|
182 | // } |
---|
183 | cmt_string lock_file; |
---|
184 | Symbol* lock_file_macro = Symbol::find ("lock_file"); |
---|
185 | if (lock_file_macro != 0) |
---|
186 | { |
---|
187 | lock_file = lock_file_macro->resolve_macro_value (); |
---|
188 | } |
---|
189 | |
---|
190 | if (CmtSystem::test_file (lock_file) || CmtSystem::test_directory (lock_file)) |
---|
191 | { |
---|
192 | return (locked_by_user); |
---|
193 | } |
---|
194 | |
---|
195 | return (not_locked); |
---|
196 | } |
---|