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

Last change on this file since 660 was 459, checked in by rybkin, 16 years ago

See C.L. 360

  • 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
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"
[459]12#include "cmt_log.h"
[2]13
14//----------------------------------------------------------
15CmtLock::status CmtLock::lock ()
16{
[429]17//   status s = check ();
[2]18
[429]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)
[2]47    {
[429]48      lock_command = lock_command_macro->resolve_macro_value ();
[2]49    }
[429]50  if (lock_command == "")
51    {
52      CmtError::set (CmtError::cannot_write_lock, "lock>");
53      return (still_unlocked);
54    }
[2]55
[429]56  cmt_string lock_file;
57  Symbol* lock_file_macro = Symbol::find ("lock_file");
58  if (lock_file_macro != 0)
[2]59    {
[429]60      lock_file = lock_file_macro->resolve_macro_value ();
61    }
62  if (lock_file == "")
63    {
[2]64      CmtError::set (CmtError::cannot_write_lock, "lock>");
65      return (still_unlocked);
66    }
67
[429]68  cmt_string command = lock_command + " " + lock_file;
69
70  if (CmtSystem::execute (command) != 0)
[2]71    {
[429]72      CmtError::set (CmtError::cannot_run_lock_command, "lock>");
73      return (still_unlocked);
[2]74    }
75
[459]76  CmtMessage::info ("Package now locked");
77  //  cerr << "Package now locked" << endl;
[2]78
79  return (locked_by_user);
80}
81
82//----------------------------------------------------------
83CmtLock::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:
[459]95      CmtMessage::info ("Package not locked");
96      //      cerr << "Package not locked" << endl;
[2]97      return (s);
98    }
99
[429]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)
[2]124    {
[429]125      unlock_command = unlock_command_macro->resolve_macro_value ();
[2]126    }
[429]127  if (unlock_command == "")
128    {
129      CmtError::set (CmtError::cannot_remove_lock, "unlock>");
130      return (still_locked);
131    }
[2]132
[429]133  cmt_string lock_file;
134  Symbol* lock_file_macro = Symbol::find ("lock_file");
135  if (lock_file_macro != 0)
[2]136    {
[429]137      lock_file = lock_file_macro->resolve_macro_value ();
138    }
139  if (lock_file == "")
140    {
[2]141      CmtError::set (CmtError::cannot_remove_lock, "unlock>");
142      return (still_locked);
143    }
144
[429]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
[459]153  CmtMessage::info ("Package now unlocked");
154  //  cerr << "Package now unlocked" << endl;
[2]155
156  return (not_locked);
157}
158
159//----------------------------------------------------------
160CmtLock::status CmtLock::check ()
161{
[429]162//   cmt_string me = CmtSystem::user ();
163//   cmt_string text;
[2]164
[429]165//   if (text.read ("lock.cmt"))
166//     {
167//       CmtSystem::cmt_string_vector words;
[2]168
[429]169//       CmtSystem::split (text, " ", words);
[2]170
[429]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 ();
[2]188    }
[429]189
190  if (CmtSystem::test_file (lock_file) || CmtSystem::test_directory (lock_file))
191    {
192      return (locked_by_user);
193    }
[2]194       
195  return (not_locked);
196}
Note: See TracBrowser for help on using the repository browser.