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