KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVLockfile.cpp
Go to the documentation of this file.
1 /*
2 $Id: KVLockfile.cpp,v 1.2 2008/03/12 11:21:58 franklan Exp $
3 $Revision: 1.2 $
4 $Date: 2008/03/12 11:21:58 $
5 */
6 
7 //Created by KVClassFactory on Wed Feb 6 12:39:42 2008
8 //Author: franklan
9 
10 #include "KVLockfile.h"
11 #include "TSystem.h"
12 #include "Riostream.h"
13 #include "KVError.h"
14 
15 using namespace std;
16 
18 
19 
20 
21 
22 
25 KVLockfile::KVLockfile(const Char_t* filename): fFile(filename), fLockfile("lockfile")
26 {
27  // Default constructor
28  init();
29 }
30 
31 
33 
34 
38 
40 {
41  // Destructor
42  // If file is still locked, we call Release
43  if (locked) Release();
44 }
45 
46 
48 
49 
54 
56 {
57  //Initialisation
58  //Look for 'lockfile' executable and store path if found
59  //Set value of have_exec accordingly
60  have_exec = FindExecutable(fLockfile);
61  if (!have_exec) {
62  Warning(KV__ERROR(init), "Unix 'lockfile' command not found on system. You should install it.");
63  }
64  sleeptime = 8; //time to wait before retrying lock
65  retries = -1; //number of times to retry
66  locktimeout = 0; //time after which lock automatically opens
67  suspend = 16; //suspend time after timeout
68  locked = kFALSE;
69 }
70 
71 
74 
76 {
77  //copied from KVBase to avoid circular dependency
78 
79  TString spath(path), backup(exec), backup2(exec), expandexec(exec);
80  gSystem->ExpandPathName(expandexec);
81  if (gSystem->IsAbsoluteFileName(expandexec.Data())) {
82  //executable given as absolute path
83  //we check if it exists
84  if (!gSystem->AccessPathName(expandexec)) {
85  exec = expandexec;
86  return kTRUE;
87  }
88  else {
89  //try with ".exe" in case of Windows system
90  if (!expandexec.EndsWith(".exe")) {
91  expandexec += ".exe";
92  if (!gSystem->AccessPathName(expandexec)) {
93  exec = expandexec;
94  return kTRUE;
95  }
96  }
97  }
98  exec = backup;
99  return kFALSE;
100  }
101  gSystem->ExpandPathName(spath);
102  if (gSystem->FindFile(spath.Data(), exec))
103  return kTRUE;
104  if (!backup.EndsWith(".exe")) {
105  backup += ".exe";
106  if (gSystem->FindFile(spath.Data(), backup)) {
107  exec = backup;
108  return kTRUE;
109  }
110  }
111  exec = backup2;
112  return kFALSE;
113 }
114 
115 
117 
118 
121 
123 {
124  //Writes lockfile command with current values of parameters
125  if (locktimeout)
126  cmd.Form("%s -%d -r%d -l%d -s%d %s.lock",
127  fLockfile.Data(),
128  sleeptime,
129  retries,
130  locktimeout,
131  suspend,
132  fFile.Data()
133  );
134  else
135  cmd.Form("%s -%d -r%d -s%d %s.lock",
136  fLockfile.Data(),
137  sleeptime,
138  retries,
139  suspend,
140  fFile.Data()
141  );
142 }
143 
144 
146 
147 
151 
153 {
154  //Executes lockfile command with current values of parameters
155  //(call KVLockfile::writecmd() before)
156  return gSystem->Exec(cmd.Data());
157 }
158 
159 
161 
162 
164 
166 {
167  if (!have_exec) {
168  locked = kTRUE;
169  return kTRUE;
170  }
171  if (locked) {
172  cout << "<Error in KVLockfile::Lock: file " << fFile.Data() << " is already locked. Release it first>" << endl;
173  return kFALSE;
174  }
175  if (strcmp(filename, "")) fFile = filename;
176  writecmd();
177  if (!testlock()) {
178  //cout << "<Info in KVLockfile::Lock : Locked " << fFile.Data() << ">" << endl;
179  locked = kTRUE;
180  return kTRUE;
181  }
182  else {
183  cout << "<Error in KVLockfile::Lock: can't get a lock for file " << fFile.Data() << ">" << endl;
184  locked = kFALSE;
185  return kFALSE;
186  }
187 }
188 
189 
191 
192 
194 
196 {
197  if (!have_exec) {
198  locked = kFALSE;
199  return kTRUE;
200  }
201  if (!locked) {
202  cout << "<Error in KVLockfile::Release: file is not locked. Lock it first>" << endl;
203  return kFALSE;
204  }
205  //cout << "<Info in KVLockfile::Release : Released " << fFile.Data() << ">" << endl;
206  locked = kFALSE;
207  return (gSystem->Unlink(Form("%s.lock", fFile.Data())) != -1);
208 }
209 
210 
Defines macros for standard error messages.
#define KV__ERROR(method)
Definition: KVError.h:30
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Interface to (Linux) system lockfile command.
Definition: KVLockfile.h:69
Bool_t FindExecutable(TString &exec, const Char_t *path="$(PATH)")
copied from KVBase to avoid circular dependency
Definition: KVLockfile.cpp:75
Bool_t Release()
Definition: KVLockfile.cpp:195
int testlock()
Definition: KVLockfile.cpp:152
virtual ~KVLockfile()
Definition: KVLockfile.cpp:39
void writecmd()
Writes lockfile command with current values of parameters.
Definition: KVLockfile.cpp:122
Bool_t Lock(const Char_t *filename="")
Definition: KVLockfile.cpp:165
void init()
Definition: KVLockfile.cpp:55
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
virtual const char * FindFile(const char *search, TString &file, EAccessMode mode=kFileExists)
virtual Int_t Exec(const char *shellcmd)
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
virtual Bool_t IsAbsoluteFileName(const char *dir)
virtual char * ExpandPathName(const char *path)
virtual int Unlink(const char *name)
void Warning(const char *location, const char *va_(fmt),...)