XRootD
XrdClFileSystemUtils.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2014 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
26 #include "XrdCl/XrdClFileSystem.hh"
27 #include "XrdCl/XrdClFileSystem.hh"
28 #include "XrdCl/XrdClURL.hh"
29 
30 #include <vector>
31 #include <utility>
32 #include <memory>
33 
34 namespace XrdCl
35 {
36  //----------------------------------------------------------------------------
37  // The data holder implementation
38  //----------------------------------------------------------------------------
40  {
41  SpaceInfoImpl( uint64_t total, uint64_t free, uint64_t used,
42  uint64_t largestChunk ):
43  pTotal( total ),
44  pFree( free ),
45  pUsed( used ),
46  pLargestChunk( largestChunk )
47  {
48  }
49 
50  uint64_t pTotal;
51  uint64_t pFree;
52  uint64_t pUsed;
53  uint64_t pLargestChunk;
54  };
55 
56  //----------------------------------------------------------------------------
57  // Constructor
58  //----------------------------------------------------------------------------
59  FileSystemUtils::SpaceInfo::SpaceInfo( uint64_t total, uint64_t free, uint64_t used,
60  uint64_t largestChunk ):
61  pImpl( new SpaceInfoImpl( total, free, used, largestChunk ) )
62  {
63  }
64 
65  //---------------------------------------------------------------------------
66  // Destructor (needs to be here due to the unique_ptr guarding PIMPL)
67  //---------------------------------------------------------------------------
69  {
70  }
71 
72  //----------------------------------------------------------------------------
73  // Amount of total space in MB
74  //----------------------------------------------------------------------------
75  uint64_t FileSystemUtils::SpaceInfo::GetTotal() const { return pImpl->pTotal; }
76 
77  //----------------------------------------------------------------------------
78  // Amount of free space in MB
79  //----------------------------------------------------------------------------
80  uint64_t FileSystemUtils::SpaceInfo::GetFree() const { return pImpl->pFree; }
81 
82  //----------------------------------------------------------------------------
83  // Amount of used space in MB
84  //----------------------------------------------------------------------------
85  uint64_t FileSystemUtils::SpaceInfo::GetUsed() const { return pImpl->pUsed; }
86 
87  //----------------------------------------------------------------------------
88  // Largest single chunk of free space
89  //----------------------------------------------------------------------------
91  {
92  return pImpl->pLargestChunk;
93  }
94 
95  //----------------------------------------------------------------------------
96  // Recursively get space information for given path
97  //----------------------------------------------------------------------------
99  FileSystem *fs,
100  const std::string &path )
101  {
102  //--------------------------------------------------------------------------
103  // Locate all the disk servers containing the space
104  //--------------------------------------------------------------------------
105  LocationInfo *locationInfo = 0;
106  XRootDStatus st = fs->DeepLocate( path, OpenFlags::Compress, locationInfo );
107  if( !st.IsOK() )
108  return st;
109 
110  std::unique_ptr<LocationInfo> locationInfoPtr( locationInfo );
111 
112  bool partial = st.code == suPartial ? true : false;
113 
114  std::vector<std::pair<std::string, uint64_t> > resp;
115  resp.push_back( std::make_pair( std::string("oss.space"), (uint64_t)0 ) );
116  resp.push_back( std::make_pair( std::string("oss.free"), (uint64_t)0 ) );
117  resp.push_back( std::make_pair( std::string("oss.used"), (uint64_t)0 ) );
118  resp.push_back( std::make_pair( std::string("oss.maxf"), (uint64_t)0 ) );
119 
120  //--------------------------------------------------------------------------
121  // Loop over the file servers and get the space info from each of them
122  //--------------------------------------------------------------------------
124  Buffer pathArg; pathArg.FromString( path );
125  for( it = locationInfo->Begin(); it != locationInfo->End(); ++it )
126  {
127  //------------------------------------------------------------------------
128  // Query the server
129  //------------------------------------------------------------------------
130  Buffer *spaceInfo = 0;
131  FileSystem fs1( it->GetAddress() );
132  st = fs1.Query( QueryCode::Space, pathArg, spaceInfo );
133  if( !st.IsOK() )
134  return st;
135 
136  std::unique_ptr<Buffer> spaceInfoPtr( spaceInfo );
137 
138  //------------------------------------------------------------------------
139  // Parse the cgi
140  //------------------------------------------------------------------------
141  std::string fakeUrl = "root://fake/fake?" + spaceInfo->ToString();
142  URL url( fakeUrl );
143 
144  if( !url.IsValid() )
146 
147  URL::ParamsMap params = url.GetParams();
148 
149  //------------------------------------------------------------------------
150  // Convert and add up the params
151  //------------------------------------------------------------------------
153  for( size_t i = 0; i < resp.size(); ++i )
154  {
155  URL::ParamsMap::iterator paramIt = params.find( resp[i].first );
156  if( paramIt == params.end() ) return st;
157  char *res;
158  uint64_t num = ::strtoll( paramIt->second.c_str(), &res, 0 );
159  if( *res != 0 ) return st;
160  if( resp[i].first == "oss.maxf" )
161  { if( num > resp[i].second ) resp[i].second = num; }
162  else
163  resp[i].second += num;
164  }
165  }
166 
167  result = new SpaceInfo( resp[0].second, resp[1].second, resp[2].second,
168  resp[3].second );
169 
170  st = XRootDStatus(); if( partial ) st.code = suPartial;
171  return st;
172  }
173 }
Binary blob representation.
Definition: XrdClBuffer.hh:34
void FromString(const std::string str)
Fill the buffer from a string.
Definition: XrdClBuffer.hh:205
std::string ToString() const
Convert the buffer to a string.
Definition: XrdClBuffer.hh:215
Container for space information.
uint64_t GetUsed() const
Amount of used space in MB.
uint64_t GetLargestFreeChunk() const
Largest single chunk of free space.
uint64_t GetTotal() const
Amount of total space in MB.
uint64_t GetFree() const
Amount of free space in MB.
SpaceInfo(uint64_t total, uint64_t free, uint64_t used, uint64_t largestChunk)
static XRootDStatus GetSpaceInfo(SpaceInfo *&result, FileSystem *fs, const std::string &path)
Recursively get space information for given path.
Send file/filesystem queries to an XRootD cluster.
XRootDStatus Query(QueryCode::Code queryCode, const Buffer &arg, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus DeepLocate(const std::string &path, OpenFlags::Flags flags, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
Path location info.
Iterator Begin()
Get the location begin iterator.
LocationList::iterator Iterator
Iterator over locations.
Iterator End()
Get the location end iterator.
URL representation.
Definition: XrdClURL.hh:31
std::map< std::string, std::string > ParamsMap
Definition: XrdClURL.hh:33
const ParamsMap & GetParams() const
Get the URL params.
Definition: XrdClURL.hh:244
bool IsValid() const
Is the url valid.
Definition: XrdClURL.cc:445
const uint16_t suPartial
Definition: XrdClStatus.hh:41
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
const uint16_t errInvalidResponse
Definition: XrdClStatus.hh:99
SpaceInfoImpl(uint64_t total, uint64_t free, uint64_t used, uint64_t largestChunk)
@ Space
Query logical space stats.
uint16_t code
Error type, or additional hints on what to do.
Definition: XrdClStatus.hh:147
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124