Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 "TObjString.h"
22 #include "TUrl.h"
23 #include "TNetFile.h"
24 #include "TNetFileStager.h"
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Constructor. Init a TNetSystem instance to the remote rootd.
28 
29 TNetFileStager::TNetFileStager(const char *url) : TFileStager("net")
30 {
31  fSystem = 0;
32  if (url && strlen(url) > 0) {
33  GetPrefix(url, fPrefix);
34 
35  fSystem = new TNetSystem(fPrefix);
36  }
37 }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Destructor
41 
42 TNetFileStager::~TNetFileStager()
43 {
44  SafeDelete(fSystem);
45  fPrefix = "";
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Check if the file defined by 'path' is ready to be used.
50 
51 Bool_t TNetFileStager::IsStaged(const char *path)
52 {
53  if (!IsValid()) {
54  GetPrefix(path, fPrefix);
55  fSystem = new TNetSystem(path);
56  }
57 
58  if (IsValid()) {
59  TString p(path);
60  if (!p.BeginsWith(fPrefix)) p.Insert(0, fPrefix);
61  return (fSystem->AccessPathName(p, kReadPermission) ? kFALSE : kTRUE);
62  }
63 
64  // Failure
65  Warning("IsStaged","TNetSystem not initialized");
66  return kFALSE;
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Isolate prefix in url
71 
72 void TNetFileStager::GetPrefix(const char *url, TString &pfx)
73 {
74  if (gDebug > 1)
75  ::Info("TNetFileStager::GetPrefix", "enter: %s", url);
76 
77  TUrl u(url);
78  pfx = TString::Format("%s://", u.GetProtocol());
79  if (strlen(u.GetUser()) > 0)
80  pfx += TString::Format("%s@", u.GetUser());
81  pfx += u.GetHost();
82  if (u.GetPort() != TUrl("root://host").GetPort())
83  pfx += TString::Format(":%d", u.GetPort());
84  pfx += "/";
85 
86  if (gDebug > 1)
87  ::Info("TNetFileStager::GetPrefix", "found prefix: %s", pfx.Data());
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Print basic info about this stager
92 
93 void TNetFileStager::Print(Option_t *) const
94 {
95  Printf("+++ stager: %s %s", GetName(), fPrefix.Data());
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Get actual end-point url for a path
100 /// Returns 0 in case of success and 1 if any error occured
101 
102 Int_t TNetFileStager::Locate(const char *path, TString &eurl)
103 {
104  if (!IsValid()) {
105  GetPrefix(path, fPrefix);
106  fSystem = new TNetSystem(path);
107  }
108 
109  if (IsValid()) {
110  TString p(path);
111  if (!p.BeginsWith(fPrefix)) p.Insert(0, fPrefix);
112  if (!fSystem->AccessPathName(p, kReadPermission)) {
113  eurl = p;
114  return 0;
115  }
116  }
117 
118  // Unable to initialize TNetSystem or file does not exist
119  return -1;
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Returns kTRUE if stager 's' is compatible with current stager.
124 /// Avoids multiple instantiations of the potentially the same TNetSystem.
125 
126 Bool_t TNetFileStager::Matches(const char *s)
127 {
128  if (IsValid()) {
129  TString pfx;
130  GetPrefix(s, pfx);
131  return ((fPrefix == pfx) ? kTRUE : kFALSE);
132  }
133 
134  // Not valid
135  return kFALSE;
136 }