Logo ROOT  
Reference Guide
TFileStager.cxx
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: A. Peters, G. Ganis 7/2/2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2002, 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 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TFileStager //
15 // //
16 // Abstract base class defining an interface to a stager. //
17 // //
18 // To open a connection to a stager use the static method //
19 // Open("<stager>"), where <stager> contains a keyword allowing to load //
20 // the relevant plug-in, e.g. //
21 // TFileStager::Open("root://lxb6064.cern.ch") //
22 // will load TXNetFileStager and initialize it for the redirector at //
23 // lxb6046.cern.ch . //
24 // //
25 //////////////////////////////////////////////////////////////////////////
26 
27 #include "TError.h"
28 #include "TFileInfo.h"
29 #include "TFile.h"
30 #include "TFileStager.h"
31 #include "TObjString.h"
32 #include "TPluginManager.h"
33 #include "TROOT.h"
34 #include "TSystem.h"
35 #include "TUrl.h"
36 #include "TCollection.h"
37 #include "TFileCollection.h"
38 #include "THashList.h"
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Retrieves the staging (online) status for a list of path names. Path names
42 /// must be of type TUrl, TFileInfo or TObjString. The returned list is the list
43 /// of staged files as TObjString (we use TObjString, because you can do a FindObject
44 /// on that list using the file name, which is not possible with TUrl objects.
45 
47 {
48  if (!pathlist) {
49  Error("GetStaged", "list of pathnames was not specified!");
50  return 0;
51  }
52 
53  TList* stagedlist = new TList();
54  TIter nxt(pathlist);
55  TObject* o = 0;
56  Bool_t local = (!strcmp(GetName(), "local")) ? kTRUE : kFALSE;
57  while ((o = nxt())) {
59  if (pn == "") {
60  Warning("GetStaged", "object is of unexpected type %s - ignoring", o->ClassName());
61  } else if (local || IsStaged(pn))
62  stagedlist->Add(new TObjString(pn));
63  }
64 
65  // List of online files
66  stagedlist->SetOwner(kTRUE);
67  Info("GetStaged", "%d files staged", stagedlist->GetSize());
68  return stagedlist;
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Issue a stage request for a list of files.
73 /// Return the '&' of all single Prepare commands.
74 
76 {
77  TIter nxt(pathlist);
78  TObject *o = 0;
79  Bool_t success = kFALSE;
80 
81  while ((o = nxt())) {
83  if (pn == "") {
84  Warning("Stage", "found object of unexpected type %s - ignoring",
85  o->ClassName());
86  continue;
87  }
88 
89  // Issue to prepare
90  success &= Stage(pn, opt);
91  }
92 
93  // return global flag
94  return success;
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Open a stager, after having loaded the relevant plug-in.
99 /// The format of 'stager' depends on the plug-in.
100 
101 TFileStager *TFileStager::Open(const char *stager)
102 {
103  TPluginHandler *h;
104  TFileStager *s = 0;
105 
106  if (!stager) {
107  ::Error("TFileStager::Open", "stager name missing: do nothing");
108  return 0;
109  }
110 
111  if (!gSystem->IsPathLocal(stager) &&
112  (h = gROOT->GetPluginManager()->FindHandler("TFileStager", stager))) {
113  if (h->LoadPlugin() == -1)
114  return 0;
115  s = (TFileStager *) h->ExecPlugin(1, stager);
116  } else
117  s = new TFileStager("local");
118 
119  return s;
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Just check if the file exists locally
124 
126 {
127  // The safest is to open in raw mode
128  TUrl u(f);
129  u.SetOptions("filetype=raw");
130  TFile *ff = TFile::Open(u.GetUrl());
131  Bool_t rc = kTRUE;
132  if (!ff || ff->IsZombie()) {
133  rc = kFALSE;
134  }
135  if (ff) {
136  ff->Close();
137  delete ff;
138  }
139  // Done
140  return rc;
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Just check if the file exists locally
145 
147 {
148  if (!IsStaged(u))
149  return -1;
150  f = u;
151  return 0;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Massive location of files. Returns < 0 on error, or number of files
156 /// processed. Results are returned on the TFileCollection itself
157 
159 {
160  TFileInfo *fi;
161  TString endp;
162  TIter it(fc->GetList());
163  Int_t count = 0;
164 
165  while ((fi = dynamic_cast<TFileInfo *>(it.Next()))) {
166  const char *ourl = fi->GetCurrentUrl()->GetUrl();
167  if (!ourl) continue;
168 
169  if (Locate(ourl, endp) == 0) {
170  fi->AddUrl(endp.Data(), kTRUE);
172  fi->ResetUrl();
173  }
174  else {
176  }
177 
178  count++;
179  }
180 
181  return count;
182 }
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 /// Return the path name contained in object 'o' allowing for
186 /// TUrl, TObjString or TFileInfo
187 
189 {
190  TString pathname;
191  TString cn(o->ClassName());
192  if (cn == "TUrl") {
193  pathname = ((TUrl*)o)->GetUrl();
194  } else if (cn == "TObjString") {
195  pathname = ((TObjString*)o)->GetName();
196  } else if (cn == "TFileInfo") {
197  TFileInfo *fi = (TFileInfo *)o;
198  pathname = (fi->GetCurrentUrl()) ? fi->GetCurrentUrl()->GetUrl() : "";
199  if (fi->GetCurrentUrl()) {
200  if (strlen(fi->GetCurrentUrl()->GetAnchor()) > 0) {
201  TUrl url(*(fi->GetCurrentUrl()));
202  url.SetAnchor("");
203  pathname = url.GetUrl();
204  }
205  } else {
206  pathname = fi->GetCurrentUrl()->GetUrl();
207  }
208  }
209 
210  // Done
211  return pathname;
212 }
TFileInfo
Class describing a generic file including meta information.
Definition: TFileInfo.h:39
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TFileInfo::GetCurrentUrl
TUrl * GetCurrentUrl() const
Return the current url.
Definition: TFileInfo.cxx:249
f
#define f(i)
Definition: RSha256.hxx:104
Option_t
const char Option_t
Definition: RtypesCore.h:66
fc
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:3728
TString::Data
const char * Data() const
Definition: TString.h:369
TObjString.h
TCollection.h
TObject::Info
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:864
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TCollection::SetOwner
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Definition: TCollection.cxx:746
TFileStager::GetPathName
static TString GetPathName(TObject *o)
Return the path name contained in object 'o' allowing for TUrl, TObjString or TFileInfo.
Definition: TFileStager.cxx:188
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
TFile::Open
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3995
TUrl::SetAnchor
void SetAnchor(const char *anchor)
Definition: TUrl.h:86
TFileInfo::ResetUrl
void ResetUrl()
Definition: TFileInfo.h:69
TUrl::SetOptions
void SetOptions(const char *opt)
Definition: TUrl.h:87
TString
Basic string class.
Definition: TString.h:136
TSystem::IsPathLocal
virtual Bool_t IsPathLocal(const char *path)
Returns TRUE if the url in 'path' points to the local file system.
Definition: TSystem.cxx:1303
TFileStager::GetStaged
virtual TList * GetStaged(TCollection *pathlist)
Retrieves the staging (online) status for a list of path names.
Definition: TFileStager.cxx:46
TFileStager::TFileStager
TFileStager(const char *stager)
Definition: TFileStager.h:39
TFile.h
bool
TROOT.h
TObjString
Collectable string class.
Definition: TObjString.h:28
TFileInfo::AddUrl
Bool_t AddUrl(const char *url, Bool_t infront=kFALSE)
Add a new URL.
Definition: TFileInfo.cxx:296
TPluginHandler
Definition: TPluginManager.h:101
TFileStager::LocateCollection
virtual Int_t LocateCollection(TFileCollection *fc, Bool_t addDummyUrl=kFALSE)
Massive location of files.
Definition: TFileStager.cxx:158
TObject::ResetBit
void ResetBit(UInt_t f)
Definition: TObject.h:171
TSystem.h
TFileInfo.h
h
#define h(i)
Definition: RSha256.hxx:106
TFileStager
Definition: TFileStager.h:36
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TFileStager::Stage
virtual Bool_t Stage(const char *, Option_t *=0)
Definition: TFileStager.h:47
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TFileCollection.h
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TUrl::GetUrl
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:387
TUrl
This class represents a WWW compatible URL.
Definition: TUrl.h:33
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
THashList.h
TObject::IsZombie
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TIter::Next
TObject * Next()
Definition: TCollection.h:249
TFileInfo::kStaged
@ kStaged
Definition: TFileInfo.h:58
TPluginManager.h
TCollection::GetSize
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
TUrl::GetAnchor
const char * GetAnchor() const
Definition: TUrl.h:70
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TFile::Close
void Close(Option_t *option="") override
Close a file.
Definition: TFile.cxx:876
TFileStager::Open
static TFileStager * Open(const char *stager)
Open a stager, after having loaded the relevant plug-in.
Definition: TFileStager.cxx:101
TIter
Definition: TCollection.h:233
TFileStager.h
TFileStager::IsStaged
virtual Bool_t IsStaged(const char *)
Just check if the file exists locally.
Definition: TFileStager.cxx:125
TFileStager::Locate
virtual Int_t Locate(const char *u, TString &f)
Just check if the file exists locally.
Definition: TFileStager.cxx:146
TCollection
Collection abstract base class.
Definition: TCollection.h:63
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TObject::ClassName
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
TUrl.h
TList
A doubly linked list.
Definition: TList.h:44
gROOT
#define gROOT
Definition: TROOT.h:406
int
TFileCollection
Class that contains a list of TFileInfo's and accumulated meta data information about its entries.
Definition: TFileCollection.h:39
TError.h