KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVDMS.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Thu Oct 18 10:38:52 2012
2 //Author: John Frankland
3 
4 #include "KVDMS.h"
5 #include "TSystem.h"
6 #include "TList.h"
7 #include "TObjArray.h"
8 #include "TObjString.h"
9 #include "TROOT.h"
10 #include "Riostream.h"
11 
12 #include "KVUniqueNameList.h"
13 
14 using namespace std;
15 
17 
18 
19 
22 KVDMS::KVDMS(const Char_t* name, const Char_t* title)
23  : KVBase(name, title)
24 {
25  // Default ctor with name and title
26 }
27 
28 
29 
31 
33 {
34 }
35 
36 
37 
47 
48 Bool_t KVDMS::buildCommand(const Char_t* scmd, const Char_t* args, Option_t* opts)
49 {
50  // scmd = one of the DMS commands (init, ls, get, etc.)
51  // args = (optional) list of arguments for command (filename, etc.)
52  // opts = (optional) list of options for command
53  // If the command give in 'scmd' is found in the path specified by the
54  // user's PATH environment variables, this method returns kTRUE and
55  // private member variables fexec and fcmd will hold the full path to
56  // the executable and the full command line to pass to gSystem->Exec.
57  // If executable is not found, returns kFALSE.
58 
59  fexec = scmd;
60  if (!FindExecutable(fexec)) {
61  fexec = "";
62  fcmd = "";
63  return kFALSE;
64  }
65  fcmd = fexec + " ";
66  fcmd += opts;
67  fcmd += " ";
68  fcmd += args;
69  return kTRUE;
70 }
71 
72 
73 
78 
80 {
81  // Execute last command initialised with buildCommand() and return
82  // the value given by the operating system.
83  // Returns -1 if no command has been defined.
84 
85  if (fcmd == "") return -1;
86  return gSystem->Exec(fcmd.Data());
87 }
88 
89 
90 
94 
96 {
97  // Execute last command initialised with buildCommand() and return
98  // the value given by the operating system in a string.
99 
100  if (fcmd == "") return TString("");
101  fout = gSystem->GetFromPipe(fcmd.Data());
102  return fout;
103 }
104 
105 
106 
110 
112 {
113  // 'line' is a line in a directory listing
114  // This method should return kTRUE if the line corresponds to a container
115 
116  return (line.BeginsWith("C-"));
117 }
118 
119 
120 
126 
128 {
129  // Create and fill TList with info (name, size, modification date)
130  // on all files & containers in current directory
131  // (default) or in given directory.
132  // TList is filled with DMSFile_t objects which belong to the list, list must be deleted after use.
133 
134  longlist(directory);
135  if (fout == "") {
136  Error("GetListing", "Unknown directory %s", directory);
137  return 0;
138  }
139 
140  TObjArray* toks = fout.Tokenize("\n");
142  list->SetOwner(kTRUE);
143  list->SetName(((TObjString*)(*toks)[0])->String().Remove(TString::kBoth, ' ').Data());
144  for (int i = 1; i < toks->GetEntries(); i++) {
145  TString tmp = ((TObjString*)(*toks)[i])->String().Remove(TString::kBoth, ' ');
146  DMSFile_t* f = new DMSFile_t;
147  if (IsContainer(tmp)) { // container
148  f->SetName(gSystem->BaseName(tmp.Data()));
149  f->SetIsContainer();
150  }
151  else {
152  ExtractFileInfos(tmp, f);
153  }
154  list->Add(f);
155  }
156  delete toks;
157  return list;
158 }
159 
160 
161 
165 
167 {
168  // Returns kTRUE if 'path' exists (file or directory, absolute or relative pathname).
169  // If so, KVDMSFile_t object will be filled with information on file/container
170 
171  TString filename = gSystem->BaseName(path);
172  TString dirname = gSystem->DirName(path);
173  if (DirectoryContains(filename.Data(), dirname.Data())) {
174  longlist(path);
175  fout.Remove(TString::kBoth, ' ');
176  fs.SetName(filename.Data());
177  if (IsContainer(fout)) { // container
178  fs.SetIsContainer();
179  }
180  else {
181  ExtractFileInfos(fout, &fs);
182  }
183  return kTRUE;
184  }
185  return kFALSE;
186 }
187 
188 
189 
194 
195 TList* KVDMS::GetListing(const Char_t* directory)
196 {
197  // Create and fill TList with just the names of files & containers in current directory
198  // (default) or in given directory.
199  // TList is filled with DMSFile_t objects which belong to the list, list must be deleted after use.
200 
201  list(directory);
202  if (fout == "") {
203  Error("GetListing", "Unknown directory %s", directory);
204  return 0;
205  }
206 
207  TObjArray* toks = fout.Tokenize("\n");
208  TList* list = new TList;
209  list->SetOwner(kTRUE);
210  list->SetName(((TObjString*)(*toks)[0])->String().Remove(TString::kBoth, ' ').Data());
211  for (int i = 1; i < toks->GetEntries(); i++) {
212  TString tmp = ((TObjString*)(*toks)[i])->String().Remove(TString::kBoth, ' ');
213  DMSFile_t* f = new DMSFile_t;
214  if (IsContainer(tmp)) { // container
215  f->SetName(gSystem->BaseName(tmp.Data()));
216  f->SetIsContainer();
217  }
218  else {
219  f->SetName(tmp.Data());
220  }
221  list->Add(f);
222  }
223  delete toks;
224  return list;
225 }
226 
227 
228 
232 
233 Bool_t KVDMS::DirectoryContains(const Char_t* name, const Char_t* directory)
234 {
235  // Returns true if the current directory (default) or the given directory
236  // contains a file or a container with given name.
237 
238  TList* list = GetListing(directory);
239  if (!list)
240  return kFALSE;
241  Bool_t ok = list->FindObject(name);
242  delete list;
243  return ok;
244 }
245 
246 
248 
249 // DMSFile_t
251 //
252 // Describes Data Management System file/container attributes
254 
255 
256 
259 void DMSFile_t::ls(Option_t* /*opt*/) const
260 {
261  // List file/container attributes
262 
264  if (IsContainer())
265  cout << GetName() << "/" << endl;
266  else
267  cout << GetName() << "\t" << GetSize() << "\t" << GetModTime().AsString() << endl;
268 }
269 
270 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define f(i)
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
const char Option_t
R__EXTERN TSystem * gSystem
void SetIsContainer(Bool_t yes=kTRUE)
Definition: KVDMS.h:46
Base class for KaliVeda framework.
Definition: KVBase.h:141
Abstract base class for interfaces to Data Management Systems (SRB, IRODS, etc.)
Definition: KVDMS.h:59
virtual ~KVDMS()
Definition: KVDMS.cpp:32
virtual Bool_t GetPathInfo(const Char_t *path, DMSFile_t &fs)
Definition: KVDMS.cpp:166
TString pipeCommand()
Definition: KVDMS.cpp:95
virtual Bool_t DirectoryContains(const Char_t *name, const Char_t *directory="")
Definition: KVDMS.cpp:233
virtual TList * GetListing(const Char_t *directory="")
Definition: KVDMS.cpp:195
Int_t execCommand()
Definition: KVDMS.cpp:79
Bool_t buildCommand(const Char_t *scmd, const Char_t *args="", Option_t *opts="")
Definition: KVDMS.cpp:48
virtual Bool_t IsContainer(TString &) const
Definition: KVDMS.cpp:111
virtual KVUniqueNameList * GetFullListing(const Char_t *directory="")
Definition: KVDMS.cpp:127
virtual void SetOwner(Bool_t enable=kTRUE)
Optimised list in which named objects can only be placed once.
virtual void Add(TObject *obj)
void SetName(const char *name)
virtual void SetOwner(Bool_t enable=kTRUE)
virtual void Add(TObject *obj)
virtual TObject * FindObject(const char *name) const
virtual void SetName(const char *name)
Int_t GetEntries() const
static void IndentLevel()
const char * Data() const
virtual const char * DirName(const char *pathname)
virtual Int_t Exec(const char *shellcmd)
virtual const char * BaseName(const char *pathname)
virtual TString GetFromPipe(const char *command)
TLine * line
void Error(const char *location, const char *va_(fmt),...)
const char * String