ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "TSeqCollection.h"
30 #include "TFile.h"
31 #include "TFileStager.h"
32 #include "TObjString.h"
33 #include "TPluginManager.h"
34 #include "TROOT.h"
35 #include "TSystem.h"
36 #include "TUrl.h"
37 #include "TCollection.h"
38 #include "TFileCollection.h"
39 #include "THashList.h"
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Retrieves the staging (online) status for a list of path names. Path names
43 /// must be of type TUrl, TFileInfo or TObjString. The returned list is the list
44 /// of staged files as TObjString (we use TObjString, because you can do a FindObject
45 /// on that list using the file name, which is not possible with TUrl objects.
46 
48 {
49  if (!pathlist) {
50  Error("GetStaged", "list of pathnames was not specified!");
51  return 0;
52  }
53 
54  TList* stagedlist = new TList();
55  TIter nxt(pathlist);
56  TObject* o = 0;
57  Bool_t local = (!strcmp(GetName(), "local")) ? kTRUE : kFALSE;
58  while ((o = nxt())) {
60  if (pn == "") {
61  Warning("GetStaged", "object is of unexpected type %s - ignoring", o->ClassName());
62  } else if (local || IsStaged(pn))
63  stagedlist->Add(new TObjString(pn));
64  }
65 
66  // List of online files
67  stagedlist->SetOwner(kTRUE);
68  Info("GetStaged", "%d files staged", stagedlist->GetSize());
69  return stagedlist;
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Issue a stage request for a list of files.
74 /// Return the '&' of all single Prepare commands.
75 
77 {
78  TIter nxt(pathlist);
79  TObject *o = 0;
80  Bool_t success = kFALSE;
81 
82  while ((o = nxt())) {
84  if (pn == "") {
85  Warning("Stage", "found object of unexpected type %s - ignoring",
86  o->ClassName());
87  continue;
88  }
89 
90  // Issue to prepare
91  success &= Stage(pn, opt);
92  }
93 
94  // return global flag
95  return success;
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Open a stager, after having loaded the relevant plug-in.
100 /// The format of 'stager' depends on the plug-in.
101 
102 TFileStager *TFileStager::Open(const char *stager)
103 {
104  TPluginHandler *h;
105  TFileStager *s = 0;
106 
107  if (!stager) {
108  ::Error("TFileStager::Open", "stager name missing: do nothing");
109  return 0;
110  }
111 
112  if (!gSystem->IsPathLocal(stager) &&
113  (h = gROOT->GetPluginManager()->FindHandler("TFileStager", stager))) {
114  if (h->LoadPlugin() == -1)
115  return 0;
116  s = (TFileStager *) h->ExecPlugin(1, stager);
117  } else
118  s = new TFileStager("local");
119 
120  return s;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Just check if the file exists locally
125 
127 {
128  // The safest is to open in raw mode
129  TUrl u(f);
130  u.SetOptions("filetype=raw");
131  TFile *ff = TFile::Open(u.GetUrl());
132  Bool_t rc = kTRUE;
133  if (!ff || ff->IsZombie()) {
134  rc = kFALSE;
135  }
136  if (ff) {
137  ff->Close();
138  delete ff;
139  }
140  // Done
141  return rc;
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Just check if the file exists locally
146 
148 {
149  if (!IsStaged(u))
150  return -1;
151  f = u;
152  return 0;
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Massive location of files. Returns < 0 on error, or number of files
157 /// processed. Results are returned on the TFileCollection itself
158 
160 {
161  TFileInfo *fi;
162  TString endp;
163  TIter it(fc->GetList());
164  Int_t count = 0;
165 
166  while ((fi = dynamic_cast<TFileInfo *>(it.Next()))) {
167  const char *ourl = fi->GetCurrentUrl()->GetUrl();
168  if (!ourl) continue;
169 
170  if (Locate(ourl, endp) == 0) {
171  fi->AddUrl(endp.Data(), kTRUE);
173  fi->ResetUrl();
174  }
175  else {
177  }
178 
179  count++;
180  }
181 
182  return count;
183 }
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Return the path name contained in object 'o' allowing for
187 /// TUrl, TObjString or TFileInfo
188 
190 {
191  TString pathname;
192  TString cn(o->ClassName());
193  if (cn == "TUrl") {
194  pathname = ((TUrl*)o)->GetUrl();
195  } else if (cn == "TObjString") {
196  pathname = ((TObjString*)o)->GetName();
197  } else if (cn == "TFileInfo") {
198  TFileInfo *fi = (TFileInfo *)o;
199  pathname = (fi->GetCurrentUrl()) ? fi->GetCurrentUrl()->GetUrl() : "";
200  if (fi->GetCurrentUrl()) {
201  if (strlen(fi->GetCurrentUrl()->GetAnchor()) > 0) {
202  TUrl url(*(fi->GetCurrentUrl()));
203  url.SetAnchor("");
204  pathname = url.GetUrl();
205  }
206  } else {
207  pathname = fi->GetCurrentUrl()->GetUrl();
208  }
209  }
210 
211  // Done
212  return pathname;
213 }
virtual Bool_t IsStaged(const char *)
Just check if the file exists locally.
static TString GetPathName(TObject *o)
Return the path name contained in object 'o' allowing for TUrl, TObjString or TFileInfo.
virtual Bool_t IsPathLocal(const char *path)
Returns TRUE if the url in 'path' points to the local file system.
Definition: TSystem.cxx:1222
Collectable string class.
Definition: TObjString.h:32
const char Option_t
Definition: RtypesCore.h:62
This class represents a WWW compatible URL.
Definition: TUrl.h:41
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
TH1 * h
Definition: legend2.C:5
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:892
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
#define gROOT
Definition: TROOT.h:344
virtual TList * GetStaged(TCollection *pathlist)
Retrieves the staging (online) status for a list of path names.
Definition: TFileStager.cxx:47
Int_t LoadPlugin()
Load the plugin library for this handler.
Bool_t IsZombie() const
Definition: TObject.h:141
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Bool_t AddUrl(const char *url, Bool_t infront=kFALSE)
Add a new URL.
Definition: TFileInfo.cxx:293
Long_t ExecPlugin(int nargs, const T &...params)
TFile * f
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3851
const char * Data() const
Definition: TString.h:349
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:839
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
A doubly linked list.
Definition: TList.h:47
TFileStager(const char *stager)
Definition: TFileStager.h:43
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
THashList * GetList()
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
Collection abstract base class.
Definition: TCollection.h:48
virtual Int_t LocateCollection(TFileCollection *fc, Bool_t addDummyUrl=kFALSE)
Massive location of files.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
const char * GetAnchor() const
Definition: TUrl.h:79
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:385
virtual Bool_t Stage(const char *, Option_t *=0)
Definition: TFileStager.h:51
void SetAnchor(const char *anchor)
Definition: TUrl.h:95
virtual Int_t GetSize() const
Definition: TCollection.h:95
static TFileStager * Open(const char *stager)
Open a stager, after having loaded the relevant plug-in.
void ResetUrl()
Definition: TFileInfo.h:80
Mother of all ROOT objects.
Definition: TObject.h:58
TUrl * GetCurrentUrl() const
Return the current url.
Definition: TFileInfo.cxx:246
virtual void Add(TObject *obj)
Definition: TList.h:81
Class that contains a list of TFileInfo's and accumulated meta data information about its entries...
void SetOptions(const char *opt)
Definition: TUrl.h:96
Class describing a generic file including meta information.
Definition: TFileInfo.h:50
void ResetBit(UInt_t f)
Definition: TObject.h:172
virtual Int_t Locate(const char *u, TString &f)
Just check if the file exists locally.
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904