source: CMT/HEAD/source/cmt_lock.cxx @ 667

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

See C.L. 524

  • Property svn:eol-style set to native
File size: 4.9 KB
RevLine 
[2]1//-----------------------------------------------------------
2// Copyright Christian Arnault LAL-Orsay CNRS
3// arnault@lal.in2p3.fr
[663]4// Modified by Grigory Rybkin
[2]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"
[459]13#include "cmt_log.h"
[2]14
15//----------------------------------------------------------
16CmtLock::status CmtLock::lock ()
17{
[429]18//   status s = check ();
[2]19
[429]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)
[2]48    {
[429]49      lock_command = lock_command_macro->resolve_macro_value ();
[2]50    }
[429]51  if (lock_command == "")
52    {
53      CmtError::set (CmtError::cannot_write_lock, "lock>");
54      return (still_unlocked);
55    }
[2]56
[429]57  cmt_string lock_file;
58  Symbol* lock_file_macro = Symbol::find ("lock_file");
59  if (lock_file_macro != 0)
[2]60    {
[429]61      lock_file = lock_file_macro->resolve_macro_value ();
62    }
63  if (lock_file == "")
64    {
[2]65      CmtError::set (CmtError::cannot_write_lock, "lock>");
66      return (still_unlocked);
67    }
68
[429]69  cmt_string command = lock_command + " " + lock_file;
70
71  if (CmtSystem::execute (command) != 0)
[2]72    {
[429]73      CmtError::set (CmtError::cannot_run_lock_command, "lock>");
74      return (still_unlocked);
[2]75    }
76
[459]77  CmtMessage::info ("Package now locked");
78  //  cerr << "Package now locked" << endl;
[2]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:
[459]96      CmtMessage::info ("Package not locked");
97      //      cerr << "Package not locked" << endl;
[2]98      return (s);
[667]99    default:
100      break;
[2]101    }
102
[429]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)
[2]127    {
[429]128      unlock_command = unlock_command_macro->resolve_macro_value ();
[2]129    }
[429]130  if (unlock_command == "")
131    {
132      CmtError::set (CmtError::cannot_remove_lock, "unlock>");
133      return (still_locked);
134    }
[2]135
[429]136  cmt_string lock_file;
137  Symbol* lock_file_macro = Symbol::find ("lock_file");
138  if (lock_file_macro != 0)
[2]139    {
[429]140      lock_file = lock_file_macro->resolve_macro_value ();
141    }
142  if (lock_file == "")
143    {
[2]144      CmtError::set (CmtError::cannot_remove_lock, "unlock>");
145      return (still_locked);
146    }
147
[429]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
[459]156  CmtMessage::info ("Package now unlocked");
157  //  cerr << "Package now unlocked" << endl;
[2]158
159  return (not_locked);
160}
161
162//----------------------------------------------------------
163CmtLock::status CmtLock::check ()
164{
[429]165//   cmt_string me = CmtSystem::user ();
166//   cmt_string text;
[2]167
[429]168//   if (text.read ("lock.cmt"))
169//     {
170//       CmtSystem::cmt_string_vector words;
[2]171
[429]172//       CmtSystem::split (text, " ", words);
[2]173
[429]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 ();
[2]191    }
[429]192
193  if (CmtSystem::test_file (lock_file) || CmtSystem::test_directory (lock_file))
194    {
195      return (locked_by_user);
196    }
[2]197       
198  return (not_locked);
199}
Note: See TracBrowser for help on using the repository browser.