KaliVeda
Toolkit for HIC analysis
KVDataRepositoryImpl.cpp
1 #include "KVDataRepositoryImpl.h"
2 #include "TROOT.h"
3 #include "TRegexp.h"
4 #include "TNetFile.h"
5 
7 
8 
9 
12 {
13  SetType("local");
14 }
15 
16 
17 
21 
22 int KVDataRepositoryImpl::Chmod(const char* file, UInt_t mode) const
23 {
24  //Used to change file access permissions in the repository
25 
26  //do we need a special helper for this filesystem ?
27  TSystem* helper = FindHelper(file);
28  return (helper ? helper->Chmod(file, mode) : gSystem->Chmod(file, mode));
29 }
30 
31 
32 
33 
36 
38 {
39  // \return kTRUE if given full path exists in repository
40  return !gSystem->AccessPathName(path);
41 }
42 
43 
44 
49 
51 {
52  // \return kTRUE if file with given full path exists in the repository
53  // \param[in] path full path to file, see get_path_with_access_root()
54  // \param[out] fs informations on the file (if it exists)
55 
56  return !gSystem->GetPathInfo(path, fs);
57 }
58 
59 
60 
62 
63 int KVDataRepositoryImpl::copy_file_from(const TString& source, const TString& destination) const
64 {
65  return CopyFile(source, destination);
66 }
67 
68 
69 
71 
72 int KVDataRepositoryImpl::copy_file_to(const TString& source, const TString& destination) const
73 {
74  return CopyFile(source, destination);
75 }
76 
77 
78 
80 
82 {
83  std::cout << "Creating new repository directory: " << path << std::endl;
84  gSystem->mkdir(path, kTRUE); // create all parent directories as needed
85  //change file access permissions to 775
86  Chmod(path, 0775);
87 }
88 
89 
90 
93 
95 {
96  //open directory
97  void* dirp = gSystem->OpenDirectory(path.Data());
98  if (!dirp) {
99  Error("KVDataRepository::GetDirectoryListing", "Cannot open %s",
100  path.Data());
101  return 0;
102  }
103 
104  KVUniqueNameList dirlist(kTRUE);
105  dirlist.SetOwner(kTRUE);
106 
107  TString direntry = gSystem->GetDirEntry(dirp);
108  while (direntry != "") { //loop over all entries in directory
109  if (direntry == "." || direntry == "..") {
110  //skip "." and ".."
111  }
112  else {
113  dirlist.Add(new KVBase(direntry));
114  }
115  //get next entry
116  direntry = gSystem->GetDirEntry(dirp);
117  }
118  //close directory
119  gSystem->FreeDirectory(dirp);
120  return dirlist;
121 }
122 
123 
124 
126 
128 {
129  return gSystem->Unlink(path);
130 }
131 
132 
133 
137 
138 TSystem* KVDataRepositoryImpl::FindHelper(const char* path, void* dirptr) const
139 {
140  // Create helper TSystem to handle file and directory operations that
141  // might be special for remote file access, like via rfiod or rootd.
142 
143  TPluginHandler* h;
144  TSystem* helper = 0;
145  TUrl url(path, kTRUE);
146 
147  // look for existing helpers
148  TIter next(&fHelpers);
149  while ((helper = (TSystem*) next()))
150  if (HelperIsConsistentWith(helper, path, dirptr))
151  return helper;
152 
153  if (!path)
154  return 0;
155 
156  // create new helper
157  TRegexp re("^root.*:"); // also roots, rootk, etc
158  TString pname = path;
159  if (pname.Index(re) != kNPOS) {
160  // rootd daemon ...
161  if ((h = gROOT->GetPluginManager()->FindHandler("TSystem", path)) &&
162  h->LoadPlugin() == 0)
163  helper = (TSystem*) h->ExecPlugin(2, path, kFALSE);
164  else
165 #ifdef __WITHOUT_TNETSYSTEM_CTOR_BOOL_T
166  helper = new TNetSystem(path);
167 #else
168  helper = new TNetSystem(path, kFALSE);
169 #endif
170  }
171  else if (!strcmp(url.GetProtocol(), "http") &&
172  pname.BeginsWith("http")) {
173  // http ...
174  if ((h = gROOT->GetPluginManager()->FindHandler("TSystem", path)) &&
175  h->LoadPlugin() == 0)
176  helper = (TSystem*) h->ExecPlugin(0);
177  else {
178  ; // no default helper yet
179  }
180  }
181  else if ((h = gROOT->GetPluginManager()->FindHandler("TSystem", path))) {
182  if (h->LoadPlugin() == -1)
183  return 0;
184  helper = (TSystem*) h->ExecPlugin(0);
185  }
186 
187  if (helper)
188  fHelpers.Add(helper);
189 
190  return helper;
191 }
192 
193 
194 
199 
200 Bool_t KVDataRepositoryImpl::HelperIsConsistentWith(TSystem* helper, const TString& path, void* dirptr) const
201 {
202  // Copy of (protected) method TSystem::ConsistentWith
203  // Check consistency of this helper with the one required
204  // by 'path' or 'dirptr'
205 
206  Bool_t checkproto = kFALSE;
207  if (path) {
208  if (!helper->GetDirPtr()) {
209  TUrl url(path, kTRUE);
210  if (!strncmp(url.GetProtocol(), helper->GetName(), strlen(helper->GetName())))
211  checkproto = kTRUE;
212  }
213  }
214 
215  Bool_t checkdir = kFALSE;
216  if (helper->GetDirPtr() && helper->GetDirPtr() == dirptr)
217  checkdir = kTRUE;
218 
219  return (checkproto || checkdir);
220 }
221 
222 
223 
224 
235 
236 int KVDataRepositoryImpl::CopyFile(const TString& f, const TString& t, Bool_t overwrite) const
237 {
238  //Method to copy any file from anywhere to anywhere
239  //(if the necessary TFile and TSystem plugins are available).
240  //
241  // Copy a file.
242  //
243  // If overwrite is true and file already exists the file will be overwritten
244  //
245  //\returns 0 when successful, -1 in case
246  // of failure, -2 in case the file already exists and overwrite was false.
247 
248  if (!gSystem->AccessPathName(t) && !overwrite)
249  return -2;
250 
251  //save current directory
252  TDirectory* dir_sav = gDirectory;
253 
254  auto ok = TFile::Cp(f, t);
255 
256  return ok ? 0 : -1;
257 }
258 
259 
unsigned int UInt_t
#define f(i)
bool Bool_t
constexpr Bool_t kFALSE
constexpr Ssiz_t kNPOS
constexpr Bool_t kTRUE
#define gDirectory
Option_t Option_t TPoint TPoint const char mode
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize fs
#define gROOT
R__EXTERN TSystem * gSystem
void Error(const char *method, const char *msgfmt,...) const override
colourised errors (red) !
Definition: KVBase.cpp:1686
KVBase()
Default constructor.
Definition: KVBase.cpp:329
virtual KVUniqueNameList get_directory_listing(const TString &path) const
open directory
virtual Bool_t check_path_exists(const TString &) const
virtual int copy_file_from(const TString &source, const TString &destination) const
virtual int copy_file_to(const TString &source, const TString &destination) const
virtual Bool_t GetPathInfo(const TString &, FileStat_t &) const
virtual int Chmod(const char *file, UInt_t mode) const
virtual void make_directory(const TString &path) const
Bool_t HelperIsConsistentWith(TSystem *helper, const TString &path, void *dirptr=0) const
int CopyFile(const TString &f, const TString &t, Bool_t overwrite=kFALSE) const
TSystem * FindHelper(const char *path, void *dirptr=0) const
List of helper classes for alternative file/directory access.
virtual int delete_file(const TString &path) const
void Add(TObject *obj) override
void SetOwner(Bool_t enable=kTRUE) override
Optimised list in which named objects can only be placed once.
void Add(TObject *obj) override
virtual Bool_t Cp(const char *dst, Bool_t progressbar=kTRUE, UInt_t buffersize=1000000)
const char * GetName() const override
const char * Data() const
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
virtual void * GetDirPtr() const
virtual int Chmod(const char *file, UInt_t mode)
virtual void FreeDirectory(void *dirp)
virtual void * OpenDirectory(const char *name)
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
virtual int GetPathInfo(const char *path, FileStat_t &buf)
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
virtual const char * GetDirEntry(void *dirp)
virtual int Unlink(const char *name)
const char * GetProtocol() const
TH1 * h
ClassImp(TPyArg)