#include "TError.h"
#include "TObjString.h"
#include "TUrl.h"
#include "TNetFile.h"
#include "TNetFileStager.h"
TNetFileStager::TNetFileStager(const char *url) : TFileStager("net")
{
fSystem = 0;
if (url && strlen(url) > 0) {
GetPrefix(url, fPrefix);
fSystem = new TNetSystem(fPrefix);
}
}
TNetFileStager::~TNetFileStager()
{
SafeDelete(fSystem);
fPrefix = "";
}
Bool_t TNetFileStager::IsStaged(const char *path)
{
if (!IsValid()) {
GetPrefix(path, fPrefix);
fSystem = new TNetSystem(path);
}
if (IsValid()) {
TString p(path);
if (!p.BeginsWith(fPrefix)) p.Insert(0, fPrefix);
return (fSystem->AccessPathName(p, kReadPermission) ? kFALSE : kTRUE);
}
Warning("IsStaged","TNetSystem not initialized");
return kFALSE;
}
void TNetFileStager::GetPrefix(const char *url, TString &pfx)
{
if (gDebug > 1)
::Info("TNetFileStager::GetPrefix", "enter: %s", url);
TUrl u(url);
pfx = TString::Format("%s://", u.GetProtocol());
if (strlen(u.GetUser()) > 0)
pfx += TString::Format("%s@", u.GetUser());
pfx += u.GetHost();
if (u.GetPort() != TUrl("root://host").GetPort())
pfx += TString::Format(":%d", u.GetPort());
pfx += "/";
if (gDebug > 1)
::Info("TNetFileStager::GetPrefix", "found prefix: %s", pfx.Data());
}
void TNetFileStager::Print(Option_t *) const
{
Printf("+++ stager: %s %s", GetName(), fPrefix.Data());
}
Int_t TNetFileStager::Locate(const char *path, TString &eurl)
{
if (!IsValid()) {
GetPrefix(path, fPrefix);
fSystem = new TNetSystem(path);
}
if (IsValid()) {
TString p(path);
if (!p.BeginsWith(fPrefix)) p.Insert(0, fPrefix);
if (!fSystem->AccessPathName(p, kReadPermission)) {
eurl = p;
return 0;
}
}
return -1;
}
Bool_t TNetFileStager::Matches(const char *s)
{
if (IsValid()) {
TString pfx;
GetPrefix(s, pfx);
return ((fPrefix == pfx) ? kTRUE : kFALSE);
}
return kFALSE;
}