KaliVeda
Toolkit for HIC analysis
KVDataRepository.cpp
1 /*
2 $Id: KVDataRepository.cpp,v 1.21 2009/01/16 14:55:20 franklan Exp $
3 $Revision: 1.21 $
4 $Date: 2009/01/16 14:55:20 $
5 */
6 
7 //Created by KVClassFactory on Wed Apr 26 16:17:37 2006
8 //Author: franklan
9 
10 #include "KVDataRepository.h"
11 #include "KVDataRepositoryImpl.h"
12 #include "KVBase.h"
13 #include "KVList.h"
14 #include "TError.h"
15 #include "Riostream.h"
16 #include "TObjString.h"
17 #include "TFile.h"
18 #include "TOrdCollection.h"
19 #include "TRegexp.h"
20 #include "TPluginManager.h"
21 #include "TROOT.h"
22 #include "KVConfig.h"
23 #include <TClass.h>
24 #include <TMethodCall.h>
25 
26 using namespace std;
27 
29 
30 KVDataRepository* gDataRepository = nullptr;
31 
32 //___________________________________________________________________________
33 
34 
38 
40 {
41  //Make this the 'active' or 'default' data repository,
42  //i.e. gDataRepository points to this
43  gDataRepository = this;
44 }
45 
46 
47 
48 
58 
60 {
61  //Initialises data repository based on information in .kvrootrc file.
62  //Each line beginning "name.DataRepository" is used for this data repository,
63  //where "name" is the name of this repository.
64  //
65  //Then we create and initialise the data set manager for this repository.
66  //
67  //returns kTRUE if all goes well.
68  //returns kFALSE if not (i.e. data set manager cannot be initialised, no available datasets)
69 
70  fLocalrootdir =
71  gEnv->GetValue(Form("%s.DataRepository.RootDir", GetName()), "");
72  if (fLocalrootdir == "") {
73  Error("Init", "Top-level directory for repository %s is not defined. Set %s.DataRepository.RootDir variable in .kvrootrc",
74  GetName(), GetName());
75  return kFALSE;
76  }
77  fAccessprotocol =
78  gEnv->GetValue(Form("%s.DataRepository.AccessProtocol", GetName()),
79  "local");
80  fReadprotocol =
81  gEnv->GetValue(Form("%s.DataRepository.ReadProtocol", GetName()),
82  "local");
83  _impl->SetReadProtocol(fReadprotocol);
84  //can we create and write directly new files in the repository ?
85  //for local repositories, default is yes, unless 'DataRepository.CanWrite' = 'NO'
86  fCanWrite = (Bool_t)gEnv->GetValue(Form("%s.DataRepository.CanWrite", GetName()), kTRUE);
87  //xrootd protocol
88  fXrootdserver =
89  gEnv->GetValue(Form("%s.DataRepository.XRDServer", GetName()), "");
90  fXrootdrootdir =
91  gEnv->GetValue(Form("%s.DataRepository.XRDRootDir", GetName()), "");
92 
93  //rfio protocol
94  fRfioserver =
95  gEnv->GetValue(Form("%s.DataRepository.RFIOServer", GetName()), "");
96  fRfiorootdir =
97  gEnv->GetValue(Form("%s.DataRepository.RFIORootDir", GetName()),
98  "");
99  //file transfer protocol
100  fTransfertype =
101  gEnv->
102  GetValue(Form("%s.DataRepository.FileTransfer.type", GetName()),
103  "sftp");
104  if (fTransfertype.Contains("bbftp")) {
105  fTransferExec = fTransfertype;
106  fTransfertype = "bbftp";
107  if (!KVBase::FindExecutable(fTransferExec)) {
108  Error("Init", "Executable for bbftp client not found. Check %s.DataRepository.FileTransfer.type",
109  GetName());
110  }
111  }
112  else if (fTransfertype.Contains("sftp")) {
113  fTransferExec = fTransfertype;
114  fTransfertype = "sftp";
115  if (!KVBase::FindExecutable(fTransferExec)) {
116  fTransferExec = "";
117  }
118  }
119  else if (fTransfertype.Contains("root")) {
120  fTransferExec = "root";
121  fTransfertype = "xrd";
122  }
123  fTransferserver =
124  gEnv->
125  GetValue(Form("%s.DataRepository.FileTransfer.server", GetName()),
126  "");
127  fTransferuser = gEnv->GetValue(Form("%s.DataRepository.FileTransfer.user", GetName()), ""); //if not given, we use current username
128  if (fTransferuser == "") {
129  UserGroup_t* user = gSystem->GetUserInfo();
130  fTransferuser = user->fUser;
131  }
132  //set full paths to be used for checking existence of files and for opening files
133  SetFullPath(fAccessroot, fAccessprotocol);
134  SetFullPath(fReadroot, fReadprotocol);
135 
136  return kTRUE;
137 }
138 
139 
140 
143 
145 {
146  //Print info on repository
147  Info("Print", "configuration of repository is as follows - ");
148  cout << "\tname = " << GetName() << endl;
149  cout << "\tRootDir = " << fLocalrootdir.Data() << endl;
150  cout << "\tAccessProtocol = " << fAccessprotocol.Data() << endl;
151  cout << "\tAccessRootDir = " << fAccessroot.Data() << endl;
152  cout << "\tReadProtocol = " << fReadprotocol.Data() << endl;
153  cout << "\tReadRootDir = " << fReadroot.Data() << endl;
154  cout << "\tCanWrite = " << fCanWrite << endl;
155  cout << "\tXRDServer = " << fXrootdserver.Data() << endl;
156  cout << "\tXRDRootDir = " << fXrootdrootdir.Data() << endl;
157  cout << "\tRFIOServer = " << fRfioserver.Data() << endl;
158  cout << "\tRFIORootDir = " << fRfiorootdir.Data() << endl;
159  cout << "\tTransferType = " << fTransfertype.Data() << endl;
160  cout << "\tTransferServer = " << fTransferserver.Data() << endl;
161  cout << "\tTransferUser = " << fTransferuser.Data() << endl;
162 }
163 
164 
165 
166 
169 
171 {
172  //Default constructor
173  SetType("local");
174 }
175 
176 
177 
184 
185 KVDataRepository::KVDataRepository(const TString& name, const TString& root_dir, const TString& access_backend)
186  : KVBase(name), fLocalrootdir{root_dir}
187 {
188  // Create a new data repository with characteristics given by argument values
189  //
190  // \param[in] name name of repository
191  // \param[in] root_dir full path to root directory of data repository
192  // \param[in] access_backend protocol to access repository. Can be "local" (default), or "irods"
193 
194  fLocalrootdir = root_dir;
195  set_impl_backend(access_backend);
196  //set full paths to be used for checking existence of files and for opening files
199 }
200 
201 
202 
208 
209 void KVDataRepository::ReadWithXrootd(const TString& server, const TString& rootdir)
210 {
211  // Call this method if you can read files in the repository using an xrootd server.
212  //
213  // \param[in] server complete URL of xrootd server (without any network transport specification i.e. 'root://', 'https://', etc.)
214  // \param[in] rootdir the full path of the root directory used to access the repository through the xrootd server (replaces root directory given to constructor)
215 
216  fXrootdserver = server;
218  fReadprotocol = "root";
220  _impl->SetReadProtocol("root");
221 }
222 
223 
224 
230 
232 {
233  //Will replace contents of 'path' with the full base path needed for the given protocol:
234  // + protocol = "local" : `path = fLocalrootdir`
235  // + protocol = "root" : `path = "root://" + fXrootdserver + "/" + fXrootdrootdir`
236  // + protocol = "rfio" : `path = "rfio:" + fRfioserver + ":" + fRfiorootdir`
237 
238  if (tmp == "local")
239  path = fLocalrootdir;
240  else if (tmp == "root")
241  path.Form("root://%s/%s", fXrootdserver.Data(),
242  fXrootdrootdir.Data());
243  else if (tmp == "rfio")
244  path.Form("rfio:%s:%s", fRfioserver.Data(), fRfiorootdir.Data());
245  else
246  Warning("SetFullPath",
247  "Unknown protocol in call to SetFullPath : %s", tmp.Data());
248 }
249 
250 
251 
259 
261 {
262  // Initialises the back-end used to access/interrogate the repository, accessed
263  // through member pointer std::unique_ptr<KVDataRepositoryImpl> _impl
264  //
265  // \param[in] impl_type type of back-end. can be "local" (repository on local disk) or "irods" (access through iRODs client)
266  //
267  // if impl_type!="local", we set CanWrite() = false
268 
269  auto ph = KVBase::LoadPlugin("KVDataRepositoryImpl", impl_type);
270  //execute constructor/macro for plugin
271  _impl.reset(((KVDataRepositoryImpl*) ph->ExecPlugin(0)));
272  if (impl_type != "local") SetCanWrite(false);
273 }
274 
275 
276 #ifdef __CCIN2P3_RFIO
277 
278 ClassImp(KVRFIOSystem)
279 
280 //This class corrects a bug in TRFIOSystem::Unlink
282 //and adds a fully-functioning Chmod method and
283 //CopyFile which can handle either or both of source
284 //and target file being accessed via rfiod (the other is
285 //assumed to be on the local filesystem).
286 
287 
288 
290 extern "C" {
291  FILE* rfio_fopen(const char* path, const char* mode);
292  int rfio_fclose(FILE* s);
293  int rfio_fread(void* buffer, size_t size, size_t count, FILE* stream);
294  int rfio_fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
295  int rfio_feof(FILE* stream);
296  int rfio_stat(const char* path, struct stat* statbuf);
297  int rfio_rmdir(const char* path);
298  int rfio_unlink(const char* path);
299  int rfio_chmod(const char* path, mode_t mode);
300 };
301 
302 
303 
304 
308 
309 Int_t KVRFIOSystem::Unlink(const char* path)
310 {
311  // Unlink, i.e. remove, a file or directory. Returns 0 when succesfull,
312  // -1 in case of failure.
313 
314  TUrl url(path);
315  struct stat finfo;
316  if (rfio_stat(url.GetFile(), &finfo) < 0)
317  return -1;
318 
319  if (R_ISDIR(finfo.st_mode))
320  return rfio_rmdir(url.GetFile());
321  else
322  return rfio_unlink(url.GetFile());
323 }
324 
325 
326 
327 
330 
331 int KVRFIOSystem::Chmod(const char* file, UInt_t mode)
332 {
333  // Set the file permission bits. Returns -1 in case or error, 0 otherwise.
334 
335  TUrl url(file);
336  return rfio_chmod(url.GetFile(), mode);
337 }
338 
339 
340 #endif
341 
int Int_t
unsigned int UInt_t
bool Bool_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
R__EXTERN TEnv * gEnv
const char rootdir[]
char name[80]
char * Form(const char *fmt,...)
Bool_t R_ISDIR(Int_t mode)
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:140
static Bool_t FindExecutable(TString &exec, const Char_t *path="$(PATH)")
Definition: KVBase.cpp:995
static TPluginHandler * LoadPlugin(const Char_t *base, const Char_t *uri="0")
Definition: KVBase.cpp:796
void Warning(const char *method, const char *msgfmt,...) const override
colourised warnings (orange) !
Definition: KVBase.cpp:1671
Base class for managing repositories of data.
virtual void cd()
std::unique_ptr< KVDataRepositoryImpl > _impl
virtual Bool_t Init()
void SetFullPath(TString &path, const TString &protocol)
void SetCanWrite(Bool_t yes=kTRUE)
void ReadWithXrootd(const TString &server, const TString &rootdir)
KVDataRepository()
Default constructor.
void set_impl_backend(const TString &impl_type)
void Print(Option_t *opt="") const override
Print info on repository.
virtual const char * GetValue(const char *name, const char *dflt) const
const char * Data() const
void Form(const char *fmt,...)
virtual UserGroup_t * GetUserInfo(const char *user=nullptr)
void Error(const char *location, const char *fmt,...)
void Info(const char *location, const char *fmt,...)
TString fUser
ClassImp(TPyArg)