source: CMT/v1r20p20080222/source/cmt_lock.cxx @ 594

Last change on this file since 594 was 429, checked in by rybkin, 17 years ago

See C.L. 335

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