KaliVeda
Toolkit for HIC analysis
KVDataRepository.h
1 /*
2 $Id: KVDataRepository.h,v 1.12 2007/12/11 12:45:47 franklan Exp $
3 $Revision: 1.12 $
4 $Date: 2007/12/11 12:45:47 $
5 */
6 
9 
10 #ifndef __KVDATAREPOSITORY_H
11 #define __KVDATAREPOSITORY_H
12 
13 #include <functional>
14 #include <optional>
15 #include "KVBase.h"
16 #include "TString.h"
17 #include "TSystem.h"
18 #include "TEnv.h"
19 #include "KVList.h"
20 #include "KVUniqueNameList.h"
21 #include "KVDataRepositoryImpl.h"
22 
23 class TFile;
24 
126 class KVDataRepository: public KVBase {
127 
141  Bool_t fCanWrite = kTRUE; //kTRUE if new files can be created and written directly in the repository; kFALSE if they have to be written locally then copied to repository
142 
143  void SetFullPath(TString& path, const TString& protocol);
144 
147  PathConcatenator(const TString& _root) : root_dir{_root} {}
149  {
150  return root_dir;
151  }
152  template<typename... Strings>
153  TString get_full_path(const TString& path, const Strings& ... others)
154  {
155  if (!path.IsNull()) {
156  TString tmp;
158  root_dir = tmp;
159  }
160  return get_full_path(others...);
161  }
162  template<typename ExistCheck, typename MakeDir>
163  void ensure_full_path_exists(ExistCheck, MakeDir) const {}
164  template<typename ExistCheck, typename MakeDir, typename... Strings>
165  void ensure_full_path_exists(ExistCheck ec, MakeDir md, const TString& path, const Strings& ... others)
166  {
167  if (!path.IsNull()) {
168  TString tmp;
170  root_dir = tmp;
172  if (!ec(root_dir))
173  md(root_dir);
174  }
175  ensure_full_path_exists(ec, md, others...);
176  }
177  };
178  template<typename... Paths>
179  TString get_path_with_access_root(const Paths& ... paths) const
180  {
182  auto tmp = pc.get_full_path(paths...);
184  gSystem->ExpandPathName(tmp);
185  return tmp;
186  }
187  template<typename... Paths>
188  TString get_path_with_read_root(const Paths& ... paths) const
189  {
191  auto tmp = pc.get_full_path(paths...);
193  gSystem->ExpandPathName(tmp);
194  return tmp;
195  }
196  template<typename... Paths>
197  TString get_path_with_arbitrary_root(const TString& arb_root, const Paths& ... paths) const
198  {
199  PathConcatenator pc{arb_root};
200  auto tmp = pc.get_full_path(paths...);
202  gSystem->ExpandPathName(tmp);
203  return tmp;
204  }
205 
206 protected:
207  std::unique_ptr<KVDataRepositoryImpl> _impl;
208  void set_impl_backend(const TString& impl_type);
209  template<typename... Paths>
210  void CreateAllNeededSubdirectories(const TString& path1, const Paths& ... paths) const
211  {
212  auto check_path = [&](TString & p) {
215  return _impl->check_path_exists(p);
216  };
217  auto mkdir = [&](TString & p) {
220  _impl->make_directory(p);
221  };
222 
224  pc.ensure_full_path_exists(check_path, mkdir, path1, paths...);
225  }
227 
228 public:
230  KVDataRepository(const TString& name, const TString& root_dir, const TString& access_backend = "local");
231 
232  void ReadWithXrootd(const TString& server, const TString& rootdir);
233  const Char_t* GetType() const override
234  {
238  return _impl->GetType();
239  }
240  virtual Bool_t Init();
241  template<typename... Paths>
242  Bool_t CheckSubdirExists(const TString& path1, const Paths& ... paths) const
243  {
249  return _impl->check_path_exists(get_path_with_access_root(path1, paths...));
250  }
251  template<typename... Paths>
252  std::optional<FileStat_t> GetFileInfo(const TString& filename, const Paths& ... paths) const
253  {
256  FileStat_t fs;
257  if (_impl->GetPathInfo(get_path_with_access_root(paths..., filename), fs))
258  return fs;
259  return std::nullopt;
260  }
261  template<typename... Paths>
262  Bool_t CheckFileStatus(const TString& filename, const Paths& ... paths) const
263  {
265  return _impl->check_path_exists(get_path_with_access_root(paths..., filename));
266  }
267 
268  template<typename... Paths>
269  TString GetRepositoryPath(const Paths& ... paths) const
270  {
277 
278  auto tp = get_path_with_arbitrary_root("/tmp", paths...);
279  tp.Remove(0, 5);
280  return tp;
281  }
282  template<typename... Paths>
283  TString GetFullPathToOpenFile(const TString& filename, const Paths& ... paths) const
284  {
290 
291  if (_impl->look_for_files_downloaded_to_temp_dir()) {
293  }
294  return get_path_with_read_root(paths..., filename);
295  }
296 
297  template<typename... Paths>
298  TString GetFullPathToTransferFile(const TString& filename, const Paths& ... paths) const
299  {
303 
305  }
310  Bool_t CanWrite() const
311  {
312  return fCanWrite;
313  }
314  void SetCanWrite(Bool_t yes = kTRUE)
315  {
316  fCanWrite = yes;
317  }
318 
319  template<typename... Paths>
320  KVUniqueNameList GetDirectoryListing(const Paths& ... paths) const
321  {
329 
330  return _impl->get_directory_listing(get_path_with_access_root(paths...));
331  }
332  template<typename... Paths>
333  void CopyFileFromRepository(const TString& filename, const TString& destination, const Paths& ... paths) const
334  {
338  if (CheckFileStatus(filename, paths...)) {
339  auto path = get_path_with_access_root(paths..., filename);
340  _impl->copy_file_from(path, destination);
341  }
342  }
343  template<typename... Paths>
344  int CopyFileToRepository(const TString& filename, const TString& source, const Paths& ... paths) const
345  {
352 
354  auto path = get_path_with_access_root(paths..., filename);
355  auto status = _impl->copy_file_to(source, path);
356  if (status == 0) {
358  _impl->Chmod(path, 0664);
359  }
360  else {
361  Error("CopyFileToRepository", "Problem copying file %s to repository (status=%d)",
362  source.Data(), status);
363  }
364  return status;
365  }
366  template<typename... Paths>
367  TFile* CreateNewFile(const TString& filename, const Paths& ... paths) const
368  {
376 
377  std::cout << "CreateNewFile : " << filename << std::endl;
378  if (!CanWrite()) {
381  return new TFile(filename, "recreate");
382  }
384  return new TFile(GetFullPathToOpenFile(filename, paths...), "recreate");
385  }
386  template<typename... Paths>
387  void CommitFile(TFile* file, const Paths& ... paths)
388  {
390  TString CommitFileName = file->GetName();
391  delete file;
392 
393  if (CanWrite()) { //all we have to do for repositories in which files can be written directly
394  return;
395  }
396 
399 
400  std::cout << std::endl << "Copying file " << CommitFileName << " to repository" << std::endl;
401 
402  auto s = get_path_with_arbitrary_root(gSystem->pwd(), CommitFileName);
403 
404  CopyFileToRepository(CommitFileName, s, paths...);
406  gSystem->Unlink(CommitFileName);
407  }
408  template<typename... Paths>
409  void MakeSubdirectory(const TString& path1, const Paths& ... paths) const
410  {
416 
417  _impl->make_directory(get_path_with_access_root(path1, paths...));
418  }
419  template<typename... Paths>
420  void DeleteFile(const TString& filename, Bool_t confirm, const Paths& ... paths) const
421  {
425 
426  auto tmp = get_path_with_access_root(paths...);
427  auto path = get_path_with_arbitrary_root(tmp, filename);
428  std::cout << "Deleting file from repository: " << filename << std::endl;
429  if (confirm) {
430  std::cout <<
431  "Are you sure you want to delete this file permanently ? (y/n)"
432  << std::endl;
433  TString answer;
434  std::cin >> answer;
435  answer.ToUpper();
436  if (!answer.BeginsWith("Y")) {
437  std::cout << "File not deleted" << std::endl;
438  return;
439  }
440  }
441  _impl->delete_file(path);
442  }
443  template<typename... Paths>
444  TObject* OpenFile(const TString& filename, const std::function<TObject*(const TString&)>& open_file, const Paths& ... paths)
445  {
452 
453  auto path = GetFullPathToOpenFile(filename, paths...);
454  if (_impl->look_for_files_downloaded_to_temp_dir()) {
456  if (gSystem->AccessPathName(path)) {
458  CopyFileFromRepository(filename, path, paths...);
459  }
460  }
461  return open_file(path);
462  }
463  template<typename... Paths>
464  TFile* OpenROOTFile(const TString& filename, const Paths& ... paths)
465  {
467 
468  auto open_file = [](const TString & f) {
469  return TFile::Open(f);
470  };
471  return dynamic_cast<TFile*>(OpenFile(filename, open_file, paths...));
472  }
473 
474  void Print(Option_t* opt = "") const override;
477  {
478  return fTransfertype;
479  }
482  {
483  return fTransferExec;
484  }
487  {
488  return fTransferserver;
489  }
492  {
493  return fTransferuser;
494  }
497  {
498  return fLocalrootdir;
499  }
500 
501  virtual void cd();
502 
503  ClassDefOverride(KVDataRepository, 0) //Base class handling files in data repository
504 };
505 
507 R__EXTERN KVDataRepository* gDataRepository;
508 
509 #ifdef __CCIN2P3_RFIO
510 #include "TRFIOFile.h"
511 class KVRFIOSystem : public TRFIOSystem {
512 public:
513  KVRFIOSystem(): TRFIOSystem() { };
514  virtual ~KVRFIOSystem() { };
515 
516  Int_t Unlink(const char* path);
517  int Chmod(const char* file, UInt_t mode);
518 
519  ClassDef(KVRFIOSystem, 0) // TRFIOSystem with fully-functioning Unlink and Chmod methods
520 };
521 #endif
522 #endif
int Int_t
unsigned int UInt_t
#define R__EXTERN
#define f(i)
bool Bool_t
char Char_t
constexpr Bool_t kTRUE
const char Option_t
#define ClassDef(name, id)
#define ClassDefOverride(name, id)
winID h TVirtualViewer3D TVirtualGLPainter p
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize fs
void AssignAndDelete(TString &target, char *tobedeleted)
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:140
void Error(const char *method, const char *msgfmt,...) const override
colourised errors (red) !
Definition: KVBase.cpp:1686
Base class for managing repositories of data.
TString GetFullPathToTransferFile(const TString &filename, const Paths &... paths) const
void DeleteFile(const TString &filename, Bool_t confirm, const Paths &... paths) const
virtual void cd()
const Char_t * GetType() const override
Bool_t CheckSubdirExists(const TString &path1, const Paths &... paths) const
TString GetRepositoryPath(const Paths &... paths) const
void CopyFileFromRepository(const TString &filename, const TString &destination, const Paths &... paths) const
KVUniqueNameList GetDirectoryListing(const Paths &... paths) const
TString GetFileTransferUser() const
returns user name used for remote file transfer
void MakeSubdirectory(const TString &path1, const Paths &... paths) const
std::unique_ptr< KVDataRepositoryImpl > _impl
TString GetFileTransferServer() const
returns server url used for remote file transfer
std::optional< FileStat_t > GetFileInfo(const TString &filename, const Paths &... paths) const
virtual Bool_t Init()
Bool_t CanWrite() const
TString get_path_with_arbitrary_root(const TString &arb_root, const Paths &... paths) const
TString GetFileTransferType() const
returns protocol used for remote file transfer
void SetFullPath(TString &path, const TString &protocol)
TFile * OpenROOTFile(const TString &filename, const Paths &... paths)
Bool_t CheckFileStatus(const TString &filename, const Paths &... paths) const
TString GetFullPathToOpenFile(const TString &filename, const Paths &... paths) const
void SetCanWrite(Bool_t yes=kTRUE)
void ReadWithXrootd(const TString &server, const TString &rootdir)
TString GetFileTransferExec() const
returns full path to executable used for remote file transfer
KVDataRepository()
Default constructor.
TObject * OpenFile(const TString &filename, const std::function< TObject *(const TString &)> &open_file, const Paths &... paths)
void CreateAllNeededSubdirectories(const TString &path1, const Paths &... paths) const
void set_impl_backend(const TString &impl_type)
void CreateAllNeededSubdirectories() const
TString get_path_with_read_root(const Paths &... paths) const
void Print(Option_t *opt="") const override
Print info on repository.
TString get_path_with_access_root(const Paths &... paths) const
void CommitFile(TFile *file, const Paths &... paths)
TString GetRootDirectory() const
returns root directory of data repository (fLocalrootdir)
TFile * CreateNewFile(const TString &filename, const Paths &... paths) const
int CopyFileToRepository(const TString &filename, const TString &source, const Paths &... paths) const
Optimised list in which named objects can only be placed once.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
const char * Data() const
void ToUpper()
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Bool_t IsNull() const
const char * pwd()
virtual char * ConcatFileName(const char *dir, const char *name)
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
virtual char * ExpandPathName(const char *path)
virtual int Unlink(const char *name)
virtual const char * TempDirectory() const
TString get_full_path(const TString &path, const Strings &... others)
void ensure_full_path_exists(ExistCheck ec, MakeDir md, const TString &path, const Strings &... others)
void ensure_full_path_exists(ExistCheck, MakeDir) const
PathConcatenator(const TString &_root)