#include "TError.h"
#include "TFileInfo.h"
#include "TSeqCollection.h"
#include "TFile.h"
#include "TFileStager.h"
#include "TObjString.h"
#include "TPluginManager.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TUrl.h"
#include "TCollection.h"
#include "TFileCollection.h"
#include "THashList.h"
TList* TFileStager::GetStaged(TCollection *pathlist)
{
if (!pathlist) {
Error("GetStaged", "list of pathnames was not specified!");
return 0;
}
TList* stagedlist = new TList();
TIter nxt(pathlist);
TObject* o = 0;
Bool_t local = (!strcmp(GetName(), "local")) ? kTRUE : kFALSE;
while ((o = nxt())) {
TString pn = TFileStager::GetPathName(o);
if (pn == "") {
Warning("GetStaged", "object is of unexpected type %s - ignoring", o->ClassName());
} else if (local || IsStaged(pn))
stagedlist->Add(new TObjString(pn));
}
stagedlist->SetOwner(kTRUE);
Info("GetStaged", "%d files staged", stagedlist->GetSize());
return stagedlist;
}
Bool_t TFileStager::Stage(TCollection *pathlist, Option_t *opt)
{
TIter nxt(pathlist);
TObject *o = 0;
Bool_t success = kFALSE;
while ((o = nxt())) {
TString pn = TFileStager::GetPathName(o);
if (pn == "") {
Warning("Stage", "found object of unexpected type %s - ignoring",
o->ClassName());
continue;
}
success &= Stage(pn, opt);
}
return success;
}
TFileStager *TFileStager::Open(const char *stager)
{
TPluginHandler *h;
TFileStager *s = 0;
if (!stager) {
::Error("TFileStager::Open", "stager name missing: do nothing");
return 0;
}
if (!gSystem->IsPathLocal(stager) &&
(h = gROOT->GetPluginManager()->FindHandler("TFileStager", stager))) {
if (h->LoadPlugin() == -1)
return 0;
s = (TFileStager *) h->ExecPlugin(1, stager);
} else
s = new TFileStager("local");
return s;
}
Bool_t TFileStager::IsStaged(const char *f)
{
TUrl u(f);
u.SetOptions("filetype=raw");
TFile *ff = TFile::Open(u.GetUrl());
Bool_t rc = kTRUE;
if (!ff || ff->IsZombie()) {
rc = kFALSE;
}
if (ff) {
ff->Close();
delete ff;
}
return rc;
}
Int_t TFileStager::Locate(const char *u, TString &f)
{
if (!IsStaged(u))
return -1;
f = u;
return 0;
}
Int_t TFileStager::LocateCollection(TFileCollection *fc, Bool_t)
{
TFileInfo *fi;
TString endp;
TIter it(fc->GetList());
Int_t count = 0;
while ((fi = dynamic_cast<TFileInfo *>(it.Next()))) {
const char *ourl = fi->GetCurrentUrl()->GetUrl();
if (!ourl) continue;
if (Locate(ourl, endp) == 0) {
fi->AddUrl(endp.Data(), kTRUE);
fi->SetBit(TFileInfo::kStaged);
fi->ResetUrl();
}
else {
fi->ResetBit(TFileInfo::kStaged);
}
count++;
}
return count;
}
TString TFileStager::GetPathName(TObject *o)
{
TString pathname;
TString cn(o->ClassName());
if (cn == "TUrl") {
pathname = ((TUrl*)o)->GetUrl();
} else if (cn == "TObjString") {
pathname = ((TObjString*)o)->GetName();
} else if (cn == "TFileInfo") {
TFileInfo *fi = (TFileInfo *)o;
pathname = (fi->GetCurrentUrl()) ? fi->GetCurrentUrl()->GetUrl() : "";
if (fi->GetCurrentUrl()) {
if (strlen(fi->GetCurrentUrl()->GetAnchor()) > 0) {
TUrl url(*(fi->GetCurrentUrl()));
url.SetAnchor("");
pathname = url.GetUrl();
}
} else {
pathname = fi->GetCurrentUrl()->GetUrl();
}
}
return pathname;
}