ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TNetXNGSystem.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 // TNetXNGSystem //
13 // //
14 // Authors: Justin Salmon, Lukasz Janyst //
15 // CERN, 2013 //
16 // //
17 // Enables access to XRootD filesystem interface using the new client. //
18 // //
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include "TNetXNGSystem.h"
22 #include "TFileStager.h"
23 #include "Rtypes.h"
24 #include "TList.h"
25 #include "TUrl.h"
26 #include "TVirtualMutex.h"
27 #include <XrdCl/XrdClFileSystem.hh>
28 #include <XrdCl/XrdClXRootDResponses.hh>
29 #include <XrdSys/XrdSysDNS.hh>
30 
32 
35 
36 namespace
37 {
38  struct DirectoryInfo {
39  XrdCl::URL *fUrl; // Path of this directory
40  XrdCl::DirectoryList *fDirList; // Directory listing
41  XrdCl::DirectoryList::Iterator *fDirListIter; // Iterator for this listing
42 
43  public:
44  DirectoryInfo(const char *dir) : fUrl(new XrdCl::URL(dir)), fDirList(0), fDirListIter(0) {}
45 
46  ~DirectoryInfo() {
47  delete fUrl;
48  delete fDirList;
49  }
50  };
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Constructor: Create system class without connecting to server
55 ///
56 /// param owner: (unused)
57 
59  TSystem("-root", "Net file Helper System"), fUrl(0), fFileSystem(0)
60 {
61  // Name must start with '-' to bypass the TSystem singleton check, then
62  // be changed to "root"
63  SetName("root");
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// Constructor: Create system class and connect to server
68 ///
69 /// param url: URL of the entry-point server to be contacted
70 /// param owner: (unused)
71 
72 TNetXNGSystem::TNetXNGSystem(const char *url, Bool_t /*owner*/) :
73  TSystem("-root", "Net file Helper System")
74 {
75  using namespace XrdCl;
76 
77  // Name must start with '-' to bypass the TSystem singleton check
78  SetName("root");
79  fUrl = new URL(std::string(url));
80  fFileSystem = new FileSystem(fUrl->GetURL());
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Destructor
85 
87 {
88  delete fFileSystem;
89  delete fUrl;
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Open a directory
94 ///
95 /// param dir: the name of the directory to open
96 /// returns: a non-zero pointer (with no special purpose) in case of
97 /// success, 0 in case of error
98 
100 {
101  using namespace XrdCl;
102 
103  DirectoryInfo *dirInfo = new DirectoryInfo(dir);
104  fDirPtrs.insert( (void*)dirInfo );
105  return (void *) dirInfo;
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Create a directory
110 ///
111 /// param dir: the directory name
112 /// returns: 0 on success, -1 otherwise
113 
115 {
116  using namespace XrdCl;
117  URL url(dir);
118  XRootDStatus st = fFileSystem->MkDir(url.GetPath(), MkDirFlags::MakePath,
119  Access::None);
120  if (!st.IsOK()) {
121  Error("MakeDirectory", "%s", st.GetErrorMessage().c_str());
122  return -1;
123  }
124 
125  return 0;
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// Free a directory
130 ///
131 /// param dirp: the pointer to the directory to be freed
132 
134 {
135  fDirPtrs.erase( dirp );
136  delete (DirectoryInfo *) dirp;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Get a directory entry.
141 ///
142 /// param dirp: the directory pointer
143 /// returns: 0 in case there are no more entries
144 
145 const char* TNetXNGSystem::GetDirEntry(void *dirp)
146 {
147  using namespace XrdCl;
148  DirectoryInfo *dirInfo = (DirectoryInfo *) dirp;
149 
150  if (!dirInfo->fDirList) {
151  XRootDStatus st = fFileSystem->DirList(dirInfo->fUrl->GetPath(),
152  DirListFlags::Locate,
153  dirInfo->fDirList);
154  if (!st.IsOK()) {
155  Error("GetDirEntry", "%s", st.GetErrorMessage().c_str());
156  return 0;
157  }
158  dirInfo->fDirListIter = new DirectoryList::Iterator(dirInfo->fDirList->Begin());
159  }
160 
161  if (*(dirInfo->fDirListIter) != dirInfo->fDirList->End()) {
162  const char *filename = (**(dirInfo->fDirListIter))->GetName().c_str();
163  (*(dirInfo->fDirListIter))++;
164  return filename;
165 
166  } else {
167  return 0;
168  }
169 }
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Get info about a file (stat)
173 ///
174 /// param path: the path of the file to stat (in)
175 /// param buf: structure that will hold the stat info (out)
176 /// returns: 0 if success, 1 if the file could not be stat'ed
177 
179 {
180  using namespace XrdCl;
181  StatInfo *info = 0;
182  URL target(path);
183  XRootDStatus st = fFileSystem->Stat(target.GetPath(), info);
184 
185  if (!st.IsOK()) {
186 
187  if (gDebug > 1) {
188  Info("GetPathInfo", "Stat error: %s", st.GetErrorMessage().c_str());
189  }
190  delete info;
191  return 1;
192 
193  } else {
194 
195  // Flag offline files
196  if (info->TestFlags(StatInfo::Offline)) {
197  buf.fMode = kS_IFOFF;
198  } else {
199  std::stringstream sstr(info->GetId());
200  Long64_t id;
201  sstr >> id;
202 
203  buf.fDev = (id >> 32);
204  buf.fIno = (id & 0x00000000FFFFFFFF);
205  buf.fUid = -1; // not available
206  buf.fGid = -1; // not available
207  buf.fIsLink = 0; // not available
208  buf.fSize = info->GetSize();
209  buf.fMtime = info->GetModTime();
210 
211  if (info->TestFlags(StatInfo::XBitSet))
212  buf.fMode = (kS_IFREG | kS_IXUSR | kS_IXGRP | kS_IXOTH);
213  if (info->GetFlags() == 0) buf.fMode = kS_IFREG;
214  if (info->TestFlags(StatInfo::IsDir)) buf.fMode = kS_IFDIR;
215  if (info->TestFlags(StatInfo::Other)) buf.fMode = kS_IFSOCK;
216  if (info->TestFlags(StatInfo::IsReadable)) buf.fMode |= kS_IRUSR;
217  if (info->TestFlags(StatInfo::IsWritable)) buf.fMode |= kS_IWUSR;
218  }
219  }
220 
221  delete info;
222  return 0;
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Check consistency of this helper with the one required by 'path' or
227 /// 'dirptr'
228 ///
229 /// param path: the path to check
230 /// param dirptr: the directory pointer to check
231 
232 Bool_t TNetXNGSystem::ConsistentWith(const char *path, void *dirptr)
233 {
234  using namespace XrdCl;
235 
236  if( path )
237  {
238  URL url(path);
239 
240  if( gDebug > 1 )
241  Info("ConsistentWith", "Protocol: '%s' (%s), Username: '%s' (%s), "
242  "Password: '%s' (%s), Hostname: '%s' (%s), Port: %d (%d)",
243  fUrl->GetProtocol().c_str(), url.GetProtocol().c_str(),
244  fUrl->GetUserName().c_str(), url.GetUserName().c_str(),
245  fUrl->GetPassword().c_str(), url.GetPassword().c_str(),
246  fUrl->GetHostName().c_str(), url.GetHostName().c_str(),
247  fUrl->GetPort(), url.GetPort());
248 
249  // Require match of protocol, user, password, host and port
250  if( fUrl->GetProtocol() == url.GetProtocol() &&
251  fUrl->GetUserName() == url.GetUserName() &&
252  fUrl->GetPassword() == url.GetPassword() &&
253  fUrl->GetHostName() == url.GetHostName() &&
254  fUrl->GetPort() == url.GetPort())
255  return kTRUE;
256  }
257 
258  if( dirptr )
259  return fDirPtrs.find( dirptr ) != fDirPtrs.end();
260 
261  return kFALSE;
262 }
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// Unlink a file on the remote server
266 ///
267 /// param path: the path of the file to unlink
268 /// returns: 0 on success, -1 otherwise
269 
270 int TNetXNGSystem::Unlink(const char *path)
271 {
272  using namespace XrdCl;
273  StatInfo *info;
274  URL url(path);
275 
276  // Stat the path to find out if it's a file or a directory
277  XRootDStatus st = fFileSystem->Stat(url.GetPath(), info);
278  if (!st.IsOK()) {
279  Error("Unlink", "%s", st.GetErrorMessage().c_str());
280  delete info;
281  return -1;
282  }
283 
284  if (info->TestFlags(StatInfo::IsDir))
285  st = fFileSystem->RmDir(url.GetPath());
286  else
287  st = fFileSystem->Rm(url.GetPath());
288  delete info;
289 
290  if (!st.IsOK()) {
291  Error("Unlink", "%s", st.GetErrorMessage().c_str());
292  return -1;
293  }
294 
295  return 0;
296 }
297 
298 ////////////////////////////////////////////////////////////////////////////////
299 /// Is this path a local path?
300 ///
301 /// param path: the URL of the path to check
302 /// returns: kTRUE if the path is local, kFALSE otherwise
303 
305 {
306  return TSystem::IsPathLocal(path);
307 }
308 
309 ////////////////////////////////////////////////////////////////////////////////
310 /// Get the endpoint URL of a file.
311 ///
312 /// param path: the entry-point URL of the file (in)
313 /// param endurl: the endpoint URL of the file (out)
314 /// returns: 0 in case of success and 1 if the file could not be
315 /// stat'ed.
316 
317 Int_t TNetXNGSystem::Locate(const char *path, TString &endurl)
318 {
319  using namespace XrdCl;
320  LocationInfo *info = 0;
321  URL pathUrl(path);
322 
323  // Locate the file
324  XRootDStatus st = fFileSystem->Locate(pathUrl.GetPath(), OpenFlags::None,
325  info);
326  if (!st.IsOK()) {
327  Error("Locate", "%s", st.GetErrorMessage().c_str());
328  delete info;
329  return 1;
330  }
331 
332  // Use the first endpoint address returned by the client
333  URL locUrl(info->Begin()->GetAddress());
334  TString loc = locUrl.GetHostName();
335  delete info;
336  info = 0;
337 
339 
340  // The location returned by the client library is the numeric host address
341  // without path. Try to lookup a hostname and replace the path portion of
342  // the url before returning the result.
343  TNamed *hn = 0;
344  if (fgAddrFQDN.GetSize() <= 0 ||
345  !(hn = dynamic_cast<TNamed *>(fgAddrFQDN.FindObject(loc)))) {
346  char *addr[1] = {0}, *name[1] = {0};
347  int naddr = XrdSysDNS::getAddrName(loc.Data(), 1, addr, name);
348  if (naddr == 1) {
349  hn = new TNamed(loc.Data(), name[0]);
350  } else {
351  hn = new TNamed(loc, loc);
352  }
353  fgAddrFQDN.Add(hn);
354  free(addr[0]);
355  free(name[0]);
356  if (gDebug > 0)
357  Info("Locate","caching host name: %s", hn->GetTitle());
358  }
359 
360  TUrl res(path);
361  res.SetHost(hn->GetTitle());
362  res.SetPort(locUrl.GetPort());
363  endurl = res.GetUrl();
364 
365  return 0;
366 }
367 
368 ////////////////////////////////////////////////////////////////////////////////
369 /// Issue a stage request for a single file
370 ///
371 /// param path: the path of the file to stage
372 /// param opt: defines 'option' and 'priority' for 'Prepare': the format is
373 /// opt = "option=o priority=p"
374 /// returns: 0 for success, -1 for error
375 
376 Int_t TNetXNGSystem::Stage(const char* path, UChar_t priority)
377 {
378  TList *files = new TList();
379  files->Add((TObject *) new TUrl(path));
380  return Stage((TCollection *) files, priority);
381 }
382 
383 ////////////////////////////////////////////////////////////////////////////////
384 /// Issue stage requests for multiple files
385 ///
386 /// param pathlist: list of paths of files to stage
387 /// param opt: defines 'option' and 'priority' for 'Prepare': the
388 /// format is opt = "option=o priority=p"
389 /// returns: 0 for success, -1 for error
390 
392 {
393  using namespace XrdCl;
394  std::vector<std::string> fileList;
395  TIter it(files);
396  TObject *object = 0;
397 
398  while ((object = (TObject *) it.Next())) {
399 
400  TString path = TFileStager::GetPathName(object);
401  if (path == "") {
402  Warning("Stage", "object is of unexpected type %s - ignoring",
403  object->ClassName());
404  continue;
405  }
406 
407  fileList.push_back(std::string(URL(path.Data()).GetPath()));
408  }
409 
410  Buffer *response;
411  XRootDStatus st = fFileSystem->Prepare(fileList, PrepareFlags::Stage,
412  (uint8_t) priority, response);
413  if (!st.IsOK()) {
414  Error("Stage", "%s", st.GetErrorMessage().c_str());
415  return -1;
416  }
417 
418  return 0;
419 }
420 
std::set< void * > fDirPtrs
Definition: TNetXNGSystem.h:39
virtual Int_t MakeDirectory(const char *dir)
Create a directory.
Definition: TMutex.h:34
void SetPort(Int_t port)
Definition: TUrl.h:97
static TString GetPathName(TObject *o)
Return the path name contained in object 'o' allowing for TUrl, TObjString or TFileInfo.
virtual Bool_t IsPathLocal(const char *path)
Returns TRUE if the url in 'path' points to the local file system.
Definition: TSystem.cxx:1222
long long Long64_t
Definition: RtypesCore.h:69
virtual Bool_t IsPathLocal(const char *path)
Is this path a local path?
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:213
This class represents a WWW compatible URL.
Definition: TUrl.h:41
Int_t fUid
Definition: TSystem.h:139
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:892
virtual Int_t Stage(const char *path, UChar_t priority)
Issue a stage request for a single file.
static const char * filename()
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Long_t fMtime
Definition: TSystem.h:142
TNetXNGSystem(Bool_t owner=kTRUE)
Constructor: Create system class without connecting to server.
virtual Bool_t ConsistentWith(const char *path, void *dirptr)
Check consistency of this helper with the one required by 'path' or 'dirptr'.
Long64_t fSize
Definition: TSystem.h:141
Int_t fMode
Definition: TSystem.h:138
const char * Data() const
Definition: TString.h:349
XrdCl::URL * fUrl
Definition: TNetXNGSystem.h:44
THashList implements a hybrid collection class consisting of a hash table and a list to store TObject...
Definition: THashList.h:36
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
ClassImp(TNetXNGSystem)
if(pyself &&pyself!=Py_None)
static TMutex fgAddrMutex
Definition: TNetXNGSystem.h:41
XFontStruct * id
Definition: TGX11.cxx:108
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
Int_t fGid
Definition: TSystem.h:140
virtual int Unlink(const char *path)
Unlink a file on the remote server.
A doubly linked list.
Definition: TList.h:47
#define None
Definition: TGWin32.h:68
Bool_t fIsLink
Definition: TSystem.h:143
TObject * Next()
Definition: TCollection.h:158
Collection abstract base class.
Definition: TCollection.h:48
virtual const char * GetDirEntry(void *dirp)
Get a directory entry.
virtual void FreeDirectory(void *dirp)
Free a directory.
virtual ~TNetXNGSystem()
Destructor.
tuple free
Definition: fildir.py:30
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:385
void SetHost(const char *host)
Definition: TUrl.h:93
virtual Int_t GetSize() const
Definition: TCollection.h:95
virtual Int_t GetPathInfo(const char *path, FileStat_t &buf)
Get info about a file (stat)
void dir(char *path=0)
Definition: rootalias.C:30
TNamed()
Definition: TNamed.h:40
#define R__LOCKGUARD(mutex)
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
virtual void Add(TObject *obj)
Definition: TList.h:81
static THashList fgAddrFQDN
Definition: TNetXNGSystem.h:40
Long_t fIno
Definition: TSystem.h:137
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128
virtual void * OpenDirectory(const char *dir)
Open a directory.
unsigned char UChar_t
Definition: RtypesCore.h:34
Long_t fDev
Definition: TSystem.h:136
Abstract base class defining a generic interface to the underlying Operating System.
Definition: TSystem.h:258
const Bool_t kTRUE
Definition: Rtypes.h:91
XrdCl::FileSystem * fFileSystem
Definition: TNetXNGSystem.h:45
virtual Int_t Locate(const char *path, TString &endurl)
Get the endpoint URL of a file.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904