Logo ROOT   6.07/09
Reference Guide
TLockPath.cxx
Go to the documentation of this file.
1 // @(#)root/proof:$Id$
2 // Author: G. Ganis, Oct 2011
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TLockPath
13 \ingroup proofkernel
14 
15 Path locking class allowing shared and exclusive locks
16 
17 */
18 
19 #include "TLockPath.h"
20 #include "TSystem.h"
21 #include <sys/file.h>
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// Locks the directory. Waits if lock is hold by an other process.
25 /// Returns 0 on success, -1 in case of error.
26 
27 TLockPath::TLockPath(const char *path) : fName(path), fLockId(-1)
28 {
29  // Work with full names
31  Warning("TLockPath", "problems expanding path '%s'", fName.Data());
32 }
33 
35 {
36  const char *pname = GetName();
37 
38  if (gSystem->AccessPathName(pname))
39  fLockId = open(pname, O_CREAT|O_RDWR, 0644);
40  else
41  fLockId = open(pname, O_RDWR);
42 
43  if (fLockId == -1) {
44  SysError("Lock", "cannot open lock file %s", pname);
45  return -1;
46  }
47 
48  if (gDebug > 1)
49  Info("Lock", "%d: locking file %s ...", gSystem->GetPid(), pname);
50  // lock the file
51 #if !defined(R__WIN32) && !defined(R__WINGCC)
52  int op = (shared) ? LOCK_SH : LOCK_EX ;
53  if (flock(fLockId, op) == -1) {
54  SysError("Lock", "error locking %s", pname);
55  close(fLockId);
56  fLockId = -1;
57  return -1;
58  }
59 #endif
60 
61  if (gDebug > 1)
62  Info("Lock", "%d: file %s locked", gSystem->GetPid(), pname);
63 
64  return 0;
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Unlock the directory. Returns 0 in case of success,
69 /// -1 in case of error.
70 
72 {
73  if (!IsLocked())
74  return 0;
75 
76  if (gDebug > 1)
77  Info("Unlock", "%d: unlocking file %s ...", gSystem->GetPid(), GetName());
78  // unlock the file
79  lseek(fLockId, 0, SEEK_SET);
80 #if !defined(R__WIN32) && !defined(R__WINGCC)
81  if (flock(fLockId, LOCK_UN) == -1) {
82  SysError("Unlock", "error unlocking %s", GetName());
83  close(fLockId);
84  fLockId = -1;
85  return -1;
86  }
87 #endif
88 
89  if (gDebug > 1)
90  Info("Unlock", "%d: file %s unlocked", gSystem->GetPid(), GetName());
91 
92  close(fLockId);
93  fLockId = -1;
94 
95  return 0;
96 }
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1265
Int_t Unlock()
Unlock the directory.
Definition: TLockPath.cxx:71
virtual int GetPid()
Get process id.
Definition: TSystem.cxx:712
Int_t Lock(Bool_t shared=kFALSE)
Definition: TLockPath.cxx:34
const char * GetName() const
Returns name of object.
Definition: TLockPath.h:39
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:899
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const char * Data() const
Definition: TString.h:349
Bool_t IsLocked() const
Definition: TLockPath.h:45
TString fName
Definition: TLockPath.h:32
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
Int_t fLockId
Definition: TLockPath.h:33
virtual void SysError(const char *method, const char *msgfmt,...) const
Issue system error message.
Definition: TObject.cxx:939
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128
TLockPath(const char *path="")
Locks the directory.
Definition: TLockPath.cxx:27
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1243
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:911