source: CMT/v1r25p20140131/source/cmt_lock.cxx

Last change on this file was 669, checked in by rybkin, 10 years ago

svn merge -r 666:668 HEAD

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