Logo ROOT  
Reference Guide
TNetFileStager.cxx
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: G. Ganis Feb 2011
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 // TNetFileStager //
15 // //
16 // TFileStager implementation for a 'rootd' backend. //
17 // //
18 //////////////////////////////////////////////////////////////////////////
19 
20 #include "TError.h"
21 #include "TUrl.h"
22 #include "TNetFile.h"
23 #include "TNetFileStager.h"
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 /// Constructor. Init a TNetSystem instance to the remote rootd.
27 
29 {
30  fSystem = 0;
31  if (url && strlen(url) > 0) {
32  GetPrefix(url, fPrefix);
33 
34  fSystem = new TNetSystem(fPrefix);
35  }
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Destructor
40 
42 {
44  fPrefix = "";
45 }
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Check if the file defined by 'path' is ready to be used.
49 
51 {
52  if (!IsValid()) {
53  GetPrefix(path, fPrefix);
54  fSystem = new TNetSystem(path);
55  }
56 
57  if (IsValid()) {
58  TString p(path);
59  if (!p.BeginsWith(fPrefix)) p.Insert(0, fPrefix);
61  }
62 
63  // Failure
64  Warning("IsStaged","TNetSystem not initialized");
65  return kFALSE;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Isolate prefix in url
70 
71 void TNetFileStager::GetPrefix(const char *url, TString &pfx)
72 {
73  if (gDebug > 1)
74  ::Info("TNetFileStager::GetPrefix", "enter: %s", url);
75 
76  TUrl u(url);
77  pfx = TString::Format("%s://", u.GetProtocol());
78  if (strlen(u.GetUser()) > 0)
79  pfx += TString::Format("%s@", u.GetUser());
80  pfx += u.GetHost();
81  if (u.GetPort() != TUrl("root://host").GetPort())
82  pfx += TString::Format(":%d", u.GetPort());
83  pfx += "/";
84 
85  if (gDebug > 1)
86  ::Info("TNetFileStager::GetPrefix", "found prefix: %s", pfx.Data());
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Print basic info about this stager
91 
93 {
94  Printf("+++ stager: %s %s", GetName(), fPrefix.Data());
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Get actual end-point url for a path
99 /// Returns 0 in case of success and 1 if any error occured
100 
101 Int_t TNetFileStager::Locate(const char *path, TString &eurl)
102 {
103  if (!IsValid()) {
104  GetPrefix(path, fPrefix);
105  fSystem = new TNetSystem(path);
106  }
107 
108  if (IsValid()) {
109  TString p(path);
110  if (!p.BeginsWith(fPrefix)) p.Insert(0, fPrefix);
112  eurl = p;
113  return 0;
114  }
115  }
116 
117  // Unable to initialize TNetSystem or file does not exist
118  return -1;
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Returns kTRUE if stager 's' is compatible with current stager.
123 /// Avoids multiple instantiations of the potentially the same TNetSystem.
124 
126 {
127  if (IsValid()) {
128  TString pfx;
129  GetPrefix(s, pfx);
130  return ((fPrefix == pfx) ? kTRUE : kFALSE);
131  }
132 
133  // Not valid
134  return kFALSE;
135 }
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
Option_t
const char Option_t
Definition: RtypesCore.h:66
TString::Data
const char * Data() const
Definition: TString.h:369
TNetFileStager::IsStaged
Bool_t IsStaged(const char *path)
Check if the file defined by 'path' is ready to be used.
Definition: TNetFileStager.cxx:50
TObject::Info
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:864
TUrl::GetPort
Int_t GetPort() const
Definition: TUrl.h:78
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
SafeDelete
#define SafeDelete(p)
Definition: RConfig.hxx:543
TNetFileStager::fPrefix
TString fPrefix
Definition: TNetFileStager.h:31
TNetFileStager::Print
void Print(Option_t *option="") const
Print basic info about this stager.
Definition: TNetFileStager.cxx:92
TString::Format
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2311
TNetFileStager::TNetFileStager
TNetFileStager(const char *stager="")
Constructor. Init a TNetSystem instance to the remote rootd.
Definition: TNetFileStager.cxx:28
TString
Basic string class.
Definition: TString.h:136
bool
TNetSystem::AccessPathName
Bool_t AccessPathName(const char *path, EAccessMode mode)
Returns FALSE if one can access a file using the specified access mode.
Definition: TNetFile.cxx:1096
TNetFileStager::Matches
Bool_t Matches(const char *s)
Returns kTRUE if stager 's' is compatible with current stager.
Definition: TNetFileStager.cxx:125
TFileStager
Definition: TFileStager.h:36
TNetFileStager::Locate
Int_t Locate(const char *path, TString &endpath)
Get actual end-point url for a path Returns 0 in case of success and 1 if any error occured.
Definition: TNetFileStager.cxx:101
TNetFileStager::GetPrefix
static void GetPrefix(const char *url, TString &pfx)
Isolate prefix in url.
Definition: TNetFileStager.cxx:71
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TNetFileStager::fSystem
TNetSystem * fSystem
Definition: TNetFileStager.h:32
gDebug
Int_t gDebug
Definition: TROOT.cxx:590
TString::Insert
TString & Insert(Ssiz_t pos, const char *s)
Definition: TString.h:649
TString::BeginsWith
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
TNetFile.h
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TUrl
This class represents a WWW compatible URL.
Definition: TUrl.h:33
TNetSystem
Definition: TNetFile.h:85
TNetFileStager::IsValid
Bool_t IsValid() const
Definition: TNetFileStager.h:44
Printf
void Printf(const char *fmt,...)
TUrl::GetProtocol
const char * GetProtocol() const
Definition: TUrl.h:64
TNetFileStager.h
TUrl::GetHost
const char * GetHost() const
Definition: TUrl.h:67
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
kReadPermission
@ kReadPermission
Definition: TSystem.h:47
TUrl::GetUser
const char * GetUser() const
Definition: TUrl.h:65
TNetFileStager::~TNetFileStager
virtual ~TNetFileStager()
Destructor.
Definition: TNetFileStager.cxx:41
TUrl.h
int
TError.h