Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TNetXNGFileStager.cxx
Go to the documentation of this file.
1 // @(#)root/netx:$Id$
2 /*************************************************************************
3  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
4  * All rights reserved. *
5  * *
6  * For the licensing terms see $ROOTSYS/LICENSE. *
7  * For the list of contributors see $ROOTSYS/README/CREDITS. *
8  *************************************************************************/
9 
10 ////////////////////////////////////////////////////////////////////////////////
11 // //
12 // TNetXNGFileStager //
13 // //
14 // Authors: Justin Salmon, Lukasz Janyst //
15 // CERN, 2013 //
16 // //
17 // Enables access to XRootD staging capabilities using the new client. //
18 // //
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include "TNetXNGFileStager.h"
22 #include "TNetXNGSystem.h"
23 #include "THashList.h"
24 #include "TFileInfo.h"
25 #include "TFileCollection.h"
26 #include <XrdCl/XrdClFileSystem.hh>
27 
28 ClassImp( TNetXNGFileStager);
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Constructor
32 ///
33 /// param url: the URL of the entry-point server
34 
35 TNetXNGFileStager::TNetXNGFileStager(const char *url) :
36  TFileStager("xrd")
37 {
38  fSystem = new TNetXNGSystem(url);
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Destructor
43 
44 TNetXNGFileStager::~TNetXNGFileStager()
45 {
46  delete fSystem;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Check if a file is staged
51 ///
52 /// param path: the URL of the file
53 
54 Bool_t TNetXNGFileStager::IsStaged(const char *path)
55 {
56  FileStat_t st;
57  if (fSystem->GetPathInfo(path, st) != 0) {
58  if (gDebug > 0)
59  Info("IsStaged", "path %s cannot be stat'ed", path);
60  return kFALSE;
61  }
62 
63  if (R_ISOFF(st.fMode)) {
64  if (gDebug > 0)
65  Info("IsStaged", "path '%s' is offline", path);
66  return kFALSE;
67  }
68 
69  return kTRUE;
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Get actual endpoint URL
74 ///
75 /// param path: the entry-point URL
76 /// param endpath: the actual endpoint URL
77 /// returns: 0 in the case of success and 1 if any error occurred
78 
79 Int_t TNetXNGFileStager::Locate(const char *path, TString &url)
80 {
81  return fSystem->Locate(path, url);
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Bulk locate request for a collection of files
86 ///
87 /// param fc: collection of files to be located
88 /// param addDummyUrl: append a dummy noop URL if the file is not staged or
89 /// redirector == endpoint
90 /// returns: < 0 in case of errors, number of files processed
91 /// otherwise
92 
93 Int_t TNetXNGFileStager::LocateCollection(TFileCollection *fc,
94  Bool_t addDummyUrl)
95 {
96  if (!fc) {
97  Error("LocateCollection", "No input collection given");
98  return -1;
99  }
100 
101  int numFiles = 0;
102  TFileInfo *info;
103  TIter it(fc->GetList());
104  TString startUrl, endUrl;
105 
106  while ((info = dynamic_cast<TFileInfo *>(it.Next())) != NULL) {
107  startUrl = info->GetCurrentUrl()->GetUrl();
108 
109  // File not staged
110  if (fSystem->Locate(startUrl.Data(), endUrl)) {
111  info->ResetBit(TFileInfo::kStaged);
112 
113  if (addDummyUrl)
114  info->AddUrl("noop://none", kTRUE);
115 
116  if (gDebug > 1)
117  Info("LocateCollection", "Not found: %s", startUrl.Data());
118  }
119 
120  // File staged
121  else {
122  info->SetBit(TFileInfo::kStaged);
123 
124  if (startUrl != endUrl) {
125  info->AddUrl(endUrl.Data(), kTRUE);
126  } else if (addDummyUrl) {
127  // Returned URL identical to redirector URL
128  info->AddUrl("noop://redir", kTRUE);
129  }
130 
131  if (gDebug > 1)
132  Info("LocateCollection", "Found: %s --> %s", startUrl.Data(),
133  endUrl.Data());
134  }
135  numFiles++;
136  }
137 
138  return numFiles;
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Returns kTRUE if stager 's' is compatible with current stager. Avoids
143 /// multiple instantiations of the potentially the same TNetXNGFileStager.
144 
145 Bool_t TNetXNGFileStager::Matches(const char *s)
146 {
147  return ((s && (fName == s)) ? kTRUE : kFALSE);
148 }
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 /// Issue a stage request for a single file
152 ///
153 /// param path: the path of the file to stage
154 /// param opt: defines 'option' and 'priority' for 'Prepare': the format is
155 /// opt = "option=o priority=p"
156 
157 Bool_t TNetXNGFileStager::Stage(const char *path, Option_t *opt)
158 {
159  Int_t priority = ParseStagePriority(opt);
160  return fSystem->Stage(path, priority);
161 }
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Issue stage requests for multiple files
165 ///
166 /// param pathlist: list of paths of files to stage
167 /// param opt: defines 'option' and 'priority' for 'Prepare': the
168 /// format is opt = "option=o priority=p"
169 
170 Bool_t TNetXNGFileStager::Stage(TCollection *paths, Option_t *opt)
171 {
172  Int_t priority = ParseStagePriority(opt);
173  return fSystem->Stage(paths, priority);
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Get a staging priority value from an option string
178 
179 UChar_t TNetXNGFileStager::ParseStagePriority(Option_t *opt)
180 {
181  UChar_t priority = 0;
182  Ssiz_t from = 0;
183  TString token;
184 
185  while (TString(opt).Tokenize(token, from, "[ ,|]")) {
186  if (token.Contains("priority=")) {
187  token.ReplaceAll("priority=", "");
188  if (token.IsDigit()) {
189  priority = token.Atoi();
190  }
191  }
192  }
193 
194  return priority;
195 }