source: CMT/v1r25-branch/source/cmt_lock.cxx @ 664

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

merge -r 646:663 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    }
100
101//   Symbol* unlock_command = Symbol::find ("unlock_command");
102//   if (unlock_command != 0)
103//     {
104//       cmt_string command = unlock_command->build_macro_value ();
105
106//       if (command != "")
107//      {
108//        if (CmtSystem::execute (command) != 0)
109//          {
110//            CmtError::set (CmtError::cannot_run_unlock_command, "unlock>");
111//            return (still_locked);
112//          }
113//      }
114//     }
115
116//   if (!CmtSystem::remove_file ("lock.cmt"))
117//     {
118//       CmtError::set (CmtError::cannot_remove_lock, "unlock>");
119//       return (still_locked);
120//     }
121
122  cmt_string unlock_command;
123  Symbol* unlock_command_macro = Symbol::find ("unlock_command");
124  if (unlock_command_macro != 0)
125    {
126      unlock_command = unlock_command_macro->resolve_macro_value ();
127    }
128  if (unlock_command == "")
129    {
130      CmtError::set (CmtError::cannot_remove_lock, "unlock>");
131      return (still_locked);
132    }
133
134  cmt_string lock_file;
135  Symbol* lock_file_macro = Symbol::find ("lock_file");
136  if (lock_file_macro != 0)
137    {
138      lock_file = lock_file_macro->resolve_macro_value ();
139    }
140  if (lock_file == "")
141    {
142      CmtError::set (CmtError::cannot_remove_lock, "unlock>");
143      return (still_locked);
144    }
145
146  cmt_string command = unlock_command + " " + lock_file;
147
148  if (CmtSystem::execute (command) != 0)
149    {
150      CmtError::set (CmtError::cannot_run_unlock_command, "unlock>");
151      return (still_locked);
152    }
153
154  CmtMessage::info ("Package now unlocked");
155  //  cerr << "Package now unlocked" << endl;
156
157  return (not_locked);
158}
159
160//----------------------------------------------------------
161CmtLock::status CmtLock::check ()
162{
163//   cmt_string me = CmtSystem::user ();
164//   cmt_string text;
165
166//   if (text.read ("lock.cmt"))
167//     {
168//       CmtSystem::cmt_string_vector words;
169
170//       CmtSystem::split (text, " ", words);
171
172//       if (words.size () >= 3)
173//      {
174//        if (words[2] == me)
175//          {
176//            return (locked_by_user);
177//          }
178//        else
179//          {
180//            return (locked_by_another_user);
181//          }
182//      }
183//     }
184  cmt_string lock_file;
185  Symbol* lock_file_macro = Symbol::find ("lock_file");
186  if (lock_file_macro != 0)
187    {
188      lock_file = lock_file_macro->resolve_macro_value ();
189    }
190
191  if (CmtSystem::test_file (lock_file) || CmtSystem::test_directory (lock_file))
192    {
193      return (locked_by_user);
194    }
195       
196  return (not_locked);
197}
Note: See TracBrowser for help on using the repository browser.