Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 
47 TList* TFileStager::GetStaged(TCollection *pathlist)
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())) {
59  TString pn = TFileStager::GetPathName(o);
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 
76 Bool_t TFileStager::Stage(TCollection *pathlist, Option_t *opt)
77 {
78  TIter nxt(pathlist);
79  TObject *o = 0;
80  Bool_t success = kFALSE;
81 
82  while ((o = nxt())) {
83  TString pn = TFileStager::GetPathName(o);
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 
126 Bool_t TFileStager::IsStaged(const char *f)
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 
147 Int_t TFileStager::Locate(const char *u, TString &f)
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 
159 Int_t TFileStager::LocateCollection(TFileCollection *fc, Bool_t)
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);
172  fi->SetBit(TFileInfo::kStaged);
173  fi->ResetUrl();
174  }
175  else {
176  fi->ResetBit(TFileInfo::kStaged);
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 
189 TString TFileStager::GetPathName(TObject *o)
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 }