source: CMT/v1r18p20041201/source/cmt_lock.cxx @ 1

Last change on this file since 1 was 1, checked in by arnault, 19 years ago

Import all tags

File size: 3.0 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      cout << "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  Symbol* lock_command = Symbol::find ("lock_command");
42  if (lock_command != 0)
43    {
44      cmt_string command = lock_command->build_macro_value ();
45
46      if (command != "")
47        {
48          if (CmtSystem::execute (command) != 0)
49            {
50              CmtError::set (CmtError::cannot_run_lock_command, "lock>");
51              return (still_unlocked);
52            }
53        }
54    }
55
56  cout << "Package now locked" << endl;
57
58  return (locked_by_user);
59}
60
61//----------------------------------------------------------
62CmtLock::status CmtLock::unlock ()
63{
64  status s = check ();
65
66  switch (s)
67    {
68    case locked_by_user:
69      break;
70    case locked_by_another_user:
71      CmtError::set (CmtError::cannot_unlock, "unlock> Package locked by another user");
72      return (s);
73    case not_locked:
74      cout << "The package was not locked" << endl;
75      return (s);
76    }
77
78  Symbol* unlock_command = Symbol::find ("unlock_command");
79  if (unlock_command != 0)
80    {
81      cmt_string command = unlock_command->build_macro_value ();
82
83      if (command != "")
84        {
85          if (CmtSystem::execute (command) != 0)
86            {
87              CmtError::set (CmtError::cannot_run_unlock_command, "unlock>");
88              return (still_locked);
89            }
90        }
91    }
92
93  if (!CmtSystem::remove_file ("lock.cmt"))
94    {
95      CmtError::set (CmtError::cannot_remove_lock, "unlock>");
96      return (still_locked);
97    }
98
99  cout << "Package now unlocked" << endl;
100
101  return (not_locked);
102}
103
104//----------------------------------------------------------
105CmtLock::status CmtLock::check ()
106{
107  cmt_string me = CmtSystem::user ();
108  cmt_string text;
109
110  if (text.read ("lock.cmt"))
111    {
112      CmtSystem::cmt_string_vector words;
113
114      CmtSystem::split (text, " ", words);
115
116      if (words.size () >= 3)
117        {
118          if (words[2] == me)
119            {
120              return (locked_by_user);
121            }
122          else
123            {
124              return (locked_by_another_user);
125            }
126        }
127    }
128       
129  return (not_locked);
130}
Note: See TracBrowser for help on using the repository browser.