xrootd
XrdClUtils.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_UTILS_HH__
20 #define __XRD_CL_UTILS_HH__
21 
22 #include <string>
23 #include <vector>
24 #include "XrdCl/XrdClStatus.hh"
25 #include "XrdCl/XrdClLog.hh"
26 #include "XrdCl/XrdClURL.hh"
29 #include "XrdCl/XrdClDefaultEnv.hh"
30 #include "XrdCl/XrdClConstants.hh"
31 #include "XrdNet/XrdNetUtils.hh"
32 
33 #include <sys/time.h>
34 
35 #ifdef __linux__
36 #include <sys/fsuid.h>
37 #endif
38 
39 namespace XrdCl
40 {
41  class Message;
42 
43  //----------------------------------------------------------------------------
45  //----------------------------------------------------------------------------
46  class Utils
47  {
48  public:
49  //------------------------------------------------------------------------
51  //------------------------------------------------------------------------
52  template<class Container>
53  static void splitString( Container &result,
54  const std::string &input,
55  const std::string &delimiter )
56  {
57  size_t start = 0;
58  size_t end = 0;
59  size_t length = 0;
60 
61  do
62  {
63  end = input.find( delimiter, start );
64 
65  if( end != std::string::npos )
66  length = end - start;
67  else
68  length = input.length() - start;
69 
70  if( length )
71  result.push_back( input.substr( start, length ) );
72 
73  start = end + delimiter.size();
74  }
75  while( end != std::string::npos );
76  }
77 
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  static int GetIntParameter( const URL &url,
82  const std::string &name,
83  int defaultVal );
84 
85  //------------------------------------------------------------------------
87  //------------------------------------------------------------------------
88  static std::string GetStringParameter( const URL &url,
89  const std::string &name,
90  const std::string &defaultVal );
91 
92  //------------------------------------------------------------------------
94  //------------------------------------------------------------------------
96  {
97  IPAuto = 0,
98  IPAll = 1,
99  IPv6 = 2,
100  IPv4 = 3,
102  };
103 
104  //------------------------------------------------------------------------
106  //------------------------------------------------------------------------
107  static AddressType String2AddressType( const std::string &addressType );
108 
109  //------------------------------------------------------------------------
111  //------------------------------------------------------------------------
112  static Status GetHostAddresses( std::vector<XrdNetAddr> &addresses,
113  const URL &url,
114  AddressType type );
115 
116  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  static void LogHostAddresses( Log *log,
120  uint64_t type,
121  const std::string &hostId,
122  std::vector<XrdNetAddr> &addresses );
123 
124  //------------------------------------------------------------------------
126  //------------------------------------------------------------------------
127  static std::string TimeToString( time_t timestamp );
128 
129  //------------------------------------------------------------------------
131  //------------------------------------------------------------------------
132  static uint64_t GetElapsedMicroSecs( timeval start, timeval end );
133 
134  //------------------------------------------------------------------------
136  //------------------------------------------------------------------------
137  static XRootDStatus GetRemoteCheckSum( std::string &checkSum,
138  const std::string &checkSumType,
139  const URL &url );
140 
141  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  static XRootDStatus GetLocalCheckSum( std::string &checkSum,
145  const std::string &checkSumType,
146  const std::string &path );
147 
148  //------------------------------------------------------------------------
150  //------------------------------------------------------------------------
151  static std::string BytesToString( uint64_t bytes );
152 
153  //------------------------------------------------------------------------
155  //------------------------------------------------------------------------
156  static XRootDStatus CheckTPC( const std::string &server,
157  uint16_t timeout = 0 );
158 
159  //------------------------------------------------------------------------
164  //------------------------------------------------------------------------
165  static XRootDStatus CheckTPCLite( const std::string &server,
166  uint16_t timeout = 0 );
167 
168  //------------------------------------------------------------------------
170  //------------------------------------------------------------------------
171  static std::string FQDNToCC( const std::string &fqdn );
172 
173  //------------------------------------------------------------------------
175  //------------------------------------------------------------------------
176  static Status GetDirectoryEntries( std::vector<std::string> &entries,
177  const std::string &path );
178 
179  //------------------------------------------------------------------------
181  //------------------------------------------------------------------------
182  static Status ProcessConfig( std::map<std::string, std::string> &config,
183  const std::string &file );
184 
185  //------------------------------------------------------------------------
187  //------------------------------------------------------------------------
188  static Status ProcessConfigDir( std::map<std::string, std::string> &config,
189  const std::string &dir );
190 
191  //------------------------------------------------------------------------
193  //------------------------------------------------------------------------
194  static void Trim( std::string &str );
195 
196  //------------------------------------------------------------------------
198  //------------------------------------------------------------------------
199  static void LogPropertyList( Log *log,
200  uint64_t topic,
201  const char *format,
202  const PropertyList &list );
203 
204  //------------------------------------------------------------------------
206  //------------------------------------------------------------------------
207  static std::string Char2Hex( uint8_t *array, uint16_t size );
208 
209  //------------------------------------------------------------------------
211  //------------------------------------------------------------------------
212  static std::string NormalizeChecksum( const std::string &name,
213  const std::string &checksum );
214 
215  //------------------------------------------------------------------------
217  //------------------------------------------------------------------------
218  static std::vector<std::string> GetSupportedCheckSums( const XrdCl::URL &url );
219 
220  //------------------------------------------------------------------------
228  //------------------------------------------------------------------------
229  static std::string InferChecksumType( const XrdCl::URL &source,
230  const XrdCl::URL &destination,
231  bool zip = false );
232 
233  //------------------------------------------------------------------------
235  //------------------------------------------------------------------------
236  static bool CheckEC( const Message *req, const URL &url );
237  };
238 
239  //----------------------------------------------------------------------------
241  //----------------------------------------------------------------------------
243  {
244  public:
245  //------------------------------------------------------------------------
247  //------------------------------------------------------------------------
248  ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
249 
250  //------------------------------------------------------------------------
252  //------------------------------------------------------------------------
254 
255  //------------------------------------------------------------------------
257  //------------------------------------------------------------------------
258  int Release()
259  {
260  int desc = pDescriptor;
261  pDescriptor = -1;
262  return desc;
263  }
264 
265  //------------------------------------------------------------------------
267  //------------------------------------------------------------------------
269  {
270  return pDescriptor;
271  }
272 
273  private:
275  };
276 
277 #ifdef __linux__
278  //----------------------------------------------------------------------------
280  //----------------------------------------------------------------------------
281  class ScopedFsUidSetter
282  {
283  public:
284  //------------------------------------------------------------------------
286  //------------------------------------------------------------------------
287  ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
288  : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
289  {
290  pOk = true;
291  pPrevFsUid = -1;
292  pPrevFsGid = -1;
293 
294  //----------------------------------------------------------------------
296  //----------------------------------------------------------------------
297  if(pFsUid >= 0) {
298  pPrevFsUid = setfsuid(pFsUid);
299 
300  if(setfsuid(pFsUid) != pFsUid) {
301  pOk = false;
302  return;
303  }
304  }
305 
306  //----------------------------------------------------------------------
308  //----------------------------------------------------------------------
309  if(pFsGid >= 0) {
310  pPrevFsGid = setfsgid(pFsGid);
311 
312  if(setfsgid(pFsGid) != pFsGid) {
313  pOk = false;
314  return;
315  }
316  }
317  }
318 
319  //------------------------------------------------------------------------
321  //------------------------------------------------------------------------
322  ~ScopedFsUidSetter() {
323  Log *log = DefaultEnv::GetLog();
324 
325  if(pPrevFsUid >= 0) {
326  int retcode = setfsuid(pPrevFsUid);
327  log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
328  }
329 
330  if(pPrevFsGid >= 0) {
331  int retcode = setfsgid(pPrevFsGid);
332  log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
333  }
334  }
335 
336  bool IsOk() const {
337  return pOk;
338  }
339 
340  private:
341  int pFsUid;
342  int pFsGid;
343 
344  const std::string &pStreamName;
345 
346  int pPrevFsUid;
347  int pPrevFsGid;
348 
349  bool pOk;
350  };
351 #endif
352 
353 }
354 
355 #endif // __XRD_CL_UTILS_HH__
Definition: XrdClUtils.hh:98
static std::string TimeToString(time_t timestamp)
Convert timestamp to a string.
static Status ProcessConfigDir(std::map< std::string, std::string > &config, const std::string &dir)
Process a config directory and return key-value pairs.
The message representation used throughout the system.
Definition: XrdClMessage.hh:29
static std::string FQDNToCC(const std::string &fqdn)
Convert the fully qualified host name to country code.
static void LogHostAddresses(Log *log, uint64_t type, const std::string &hostId, std::vector< XrdNetAddr > &addresses)
Log all the addresses on the list.
static std::string GetStringParameter(const URL &url, const std::string &name, const std::string &defaultVal)
Get a parameter either from the environment or URL.
static Status GetDirectoryEntries(std::vector< std::string > &entries, const std::string &path)
Get directory entries.
static std::vector< std::string > GetSupportedCheckSums(const XrdCl::URL &url)
Get supported checksum types for given URL.
Definition: XrdClUtils.hh:97
static std::string Char2Hex(uint8_t *array, uint16_t size)
Print a char array as hex.
~ScopedDescriptor()
Destructor.
Definition: XrdClUtils.hh:253
Procedure execution status.
Definition: XrdClStatus.hh:112
static bool CheckEC(const Message *req, const URL &url)
Check if this client can support given EC redirect.
int GetDescriptor()
Get the descriptor.
Definition: XrdClUtils.hh:268
AddressType
Address type.
Definition: XrdClUtils.hh:95
static int GetIntParameter(const URL &url, const std::string &name, int defaultVal)
Get a parameter either from the environment or URL.
Random utilities.
Definition: XrdClUtils.hh:46
static Log * GetLog()
Get default log.
static XRootDStatus GetRemoteCheckSum(std::string &checkSum, const std::string &checkSumType, const URL &url)
Get a checksum from a remote xrootd server.
static std::string BytesToString(uint64_t bytes)
Convert bytes to a human readable string.
int Release()
Release the descriptor being held.
Definition: XrdClUtils.hh:258
Request status.
Definition: XrdClXRootDResponses.hh:218
Definition: XrdClAnyObject.hh:25
static XRootDStatus CheckTPCLite(const std::string &server, uint16_t timeout=0)
static Status ProcessConfig(std::map< std::string, std::string > &config, const std::string &file)
Process a config file and return key-value pairs.
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdClUtils.hh:53
static XRootDStatus CheckTPC(const std::string &server, uint16_t timeout=0)
Check if peer supports tpc.
static std::string NormalizeChecksum(const std::string &name, const std::string &checksum)
Normalize checksum.
URL representation.
Definition: XrdClURL.hh:30
#define close(a)
Definition: XrdPosix.hh:43
static XRootDStatus GetLocalCheckSum(std::string &checkSum, const std::string &checkSumType, const std::string &path)
Get a checksum from local file.
const uint64_t XRootDTransportMsg
Definition: XrdClConstants.hh:36
Definition: XrdClUtils.hh:99
int pDescriptor
Definition: XrdClUtils.hh:274
Definition: XrdClUtils.hh:100
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
ScopedDescriptor(int descriptor)
Constructor.
Definition: XrdClUtils.hh:248
static AddressType String2AddressType(const std::string &addressType)
Interpret a string as address type, default to IPAll.
static std::string InferChecksumType(const XrdCl::URL &source, const XrdCl::URL &destination, bool zip=false)
A key-value pair map storing both keys and values as strings.
Definition: XrdClPropertyList.hh:40
Definition: XrdClUtils.hh:101
Smart descriptor - closes the descriptor on destruction.
Definition: XrdClUtils.hh:242
static void LogPropertyList(Log *log, uint64_t topic, const char *format, const PropertyList &list)
Log property list.
static void Trim(std::string &str)
Trim a string.
Handle diagnostics.
Definition: XrdClLog.hh:102
static uint64_t GetElapsedMicroSecs(timeval start, timeval end)
Get the elapsed microseconds between two timevals.