KaliVeda
Toolkit for HIC analysis
Loading...
Searching...
No Matches
KVLockfile.cpp
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
15using namespace std;
16
18
19
20
21
22
24
25KVLockfile::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
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,
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
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Interface to (Linux) system lockfile command.
Definition KVLockfile.h:70
Bool_t FindExecutable(TString &exec, const Char_t *path="$(PATH)")
copied from KVBase to avoid circular dependency
Bool_t Release()
int locktimeout
time after which lock automatically opens
Definition KVLockfile.h:76
int retries
number of times to retry
Definition KVLockfile.h:75
KVString fFile
name of file
Definition KVLockfile.h:71
bool locked
kTRUE when Lock() has been called successfully
Definition KVLockfile.h:79
virtual ~KVLockfile()
int sleeptime
time to wait before retrying lock
Definition KVLockfile.h:74
void writecmd()
Writes lockfile command with current values of parameters.
int suspend
suspend time after timeout
Definition KVLockfile.h:77
KVString fLockfile
full path to lockfile executable (if defined)
Definition KVLockfile.h:72
Bool_t Lock(const Char_t *filename="")
KVString cmd
command to execute
Definition KVLockfile.h:78
bool have_exec
kTRUE if lockfile found on system
Definition KVLockfile.h:73
void init()
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
void Form(const char *fmt,...)
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 *fmt,...)
void init()
ClassImp(TPyArg)