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  //----------------------------------------------------------------------------
43  //----------------------------------------------------------------------------
44  class Utils
45  {
46  public:
47  //------------------------------------------------------------------------
49  //------------------------------------------------------------------------
50  template<class Container>
51  static void splitString( Container &result,
52  const std::string &input,
53  const std::string &delimiter )
54  {
55  size_t start = 0;
56  size_t end = 0;
57  size_t length = 0;
58 
59  do
60  {
61  end = input.find( delimiter, start );
62 
63  if( end != std::string::npos )
64  length = end - start;
65  else
66  length = input.length() - start;
67 
68  if( length )
69  result.push_back( input.substr( start, length ) );
70 
71  start = end + delimiter.size();
72  }
73  while( end != std::string::npos );
74  }
75 
76  //------------------------------------------------------------------------
78  //------------------------------------------------------------------------
79  static int GetIntParameter( const URL &url,
80  const std::string &name,
81  int defaultVal );
82 
83  //------------------------------------------------------------------------
85  //------------------------------------------------------------------------
86  static std::string GetStringParameter( const URL &url,
87  const std::string &name,
88  const std::string &defaultVal );
89 
90  //------------------------------------------------------------------------
92  //------------------------------------------------------------------------
94  {
95  IPAuto = 0,
96  IPAll = 1,
97  IPv6 = 2,
98  IPv4 = 3,
100  };
101 
102  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
105  static AddressType String2AddressType( const std::string &addressType );
106 
107  //------------------------------------------------------------------------
109  //------------------------------------------------------------------------
110  static Status GetHostAddresses( std::vector<XrdNetAddr> &addresses,
111  const URL &url,
112  AddressType type );
113 
114  //------------------------------------------------------------------------
116  //------------------------------------------------------------------------
117  static void LogHostAddresses( Log *log,
118  uint64_t type,
119  const std::string &hostId,
120  std::vector<XrdNetAddr> &addresses );
121 
122  //------------------------------------------------------------------------
124  //------------------------------------------------------------------------
125  static std::string TimeToString( time_t timestamp );
126 
127  //------------------------------------------------------------------------
129  //------------------------------------------------------------------------
130  static uint64_t GetElapsedMicroSecs( timeval start, timeval end );
131 
132  //------------------------------------------------------------------------
134  //------------------------------------------------------------------------
135  static XRootDStatus GetRemoteCheckSum( std::string &checkSum,
136  const std::string &checkSumType,
137  const URL &url );
138 
139  //------------------------------------------------------------------------
141  //------------------------------------------------------------------------
142  static XRootDStatus GetLocalCheckSum( std::string &checkSum,
143  const std::string &checkSumType,
144  const std::string &path );
145 
146  //------------------------------------------------------------------------
148  //------------------------------------------------------------------------
149  static std::string BytesToString( uint64_t bytes );
150 
151  //------------------------------------------------------------------------
153  //------------------------------------------------------------------------
154  static XRootDStatus CheckTPC( const std::string &server,
155  uint16_t timeout = 0 );
156 
157  //------------------------------------------------------------------------
162  //------------------------------------------------------------------------
163  static XRootDStatus CheckTPCLite( const std::string &server,
164  uint16_t timeout = 0 );
165 
166  //------------------------------------------------------------------------
168  //------------------------------------------------------------------------
169  static std::string FQDNToCC( const std::string &fqdn );
170 
171  //------------------------------------------------------------------------
173  //------------------------------------------------------------------------
174  static Status GetDirectoryEntries( std::vector<std::string> &entries,
175  const std::string &path );
176 
177  //------------------------------------------------------------------------
179  //------------------------------------------------------------------------
180  static Status ProcessConfig( std::map<std::string, std::string> &config,
181  const std::string &file );
182 
183  //------------------------------------------------------------------------
185  //------------------------------------------------------------------------
186  static void Trim( std::string &str );
187 
188  //------------------------------------------------------------------------
190  //------------------------------------------------------------------------
191  static void LogPropertyList( Log *log,
192  uint64_t topic,
193  const char *format,
194  const PropertyList &list );
195 
196  //------------------------------------------------------------------------
198  //------------------------------------------------------------------------
199  static std::string Char2Hex( uint8_t *array, uint16_t size );
200 
201  //------------------------------------------------------------------------
203  //------------------------------------------------------------------------
204  static std::string NormalizeChecksum( const std::string &name,
205  const std::string &checksum );
206 
207  //------------------------------------------------------------------------
209  //------------------------------------------------------------------------
210  static std::vector<std::string> GetSupportedCheckSums( const XrdCl::URL &url );
211 
212  //------------------------------------------------------------------------
220  //------------------------------------------------------------------------
221  static std::string InferChecksumType( const XrdCl::URL &source,
222  const XrdCl::URL &destination,
223  bool zip = false );
224  };
225 
226  //----------------------------------------------------------------------------
228  //----------------------------------------------------------------------------
230  {
231  public:
232  //------------------------------------------------------------------------
234  //------------------------------------------------------------------------
235  ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
236 
237  //------------------------------------------------------------------------
239  //------------------------------------------------------------------------
241 
242  //------------------------------------------------------------------------
244  //------------------------------------------------------------------------
245  int Release()
246  {
247  int desc = pDescriptor;
248  pDescriptor = -1;
249  return desc;
250  }
251 
252  //------------------------------------------------------------------------
254  //------------------------------------------------------------------------
256  {
257  return pDescriptor;
258  }
259 
260  private:
262  };
263 
264 #ifdef __linux__
265  //----------------------------------------------------------------------------
267  //----------------------------------------------------------------------------
268  class ScopedFsUidSetter
269  {
270  public:
271  //------------------------------------------------------------------------
273  //------------------------------------------------------------------------
274  ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
275  : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
276  {
277  pOk = true;
278  pPrevFsUid = -1;
279  pPrevFsGid = -1;
280 
281  //----------------------------------------------------------------------
283  //----------------------------------------------------------------------
284  if(pFsUid >= 0) {
285  pPrevFsUid = setfsuid(pFsUid);
286 
287  if(setfsuid(pFsUid) != pFsUid) {
288  pOk = false;
289  return;
290  }
291  }
292 
293  //----------------------------------------------------------------------
295  //----------------------------------------------------------------------
296  if(pFsGid >= 0) {
297  pPrevFsGid = setfsgid(pFsGid);
298 
299  if(setfsgid(pFsGid) != pFsGid) {
300  pOk = false;
301  return;
302  }
303  }
304  }
305 
306  //------------------------------------------------------------------------
308  //------------------------------------------------------------------------
309  ~ScopedFsUidSetter() {
310  Log *log = DefaultEnv::GetLog();
311 
312  if(pPrevFsUid >= 0) {
313  int retcode = setfsuid(pPrevFsUid);
314  log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
315  }
316 
317  if(pPrevFsGid >= 0) {
318  int retcode = setfsgid(pPrevFsGid);
319  log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
320  }
321  }
322 
323  bool IsOk() const {
324  return pOk;
325  }
326 
327  private:
328  int pFsUid;
329  int pFsGid;
330 
331  const std::string &pStreamName;
332 
333  int pPrevFsUid;
334  int pPrevFsGid;
335 
336  bool pOk;
337  };
338 #endif
339 
340 }
341 
342 #endif // __XRD_CL_UTILS_HH__
Definition: XrdClUtils.hh:96
static std::string TimeToString(time_t timestamp)
Convert timestamp to a string.
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:95
static std::string Char2Hex(uint8_t *array, uint16_t size)
Print a char array as hex.
~ScopedDescriptor()
Destructor.
Definition: XrdClUtils.hh:240
Procedure execution status.
Definition: XrdClStatus.hh:110
int GetDescriptor()
Get the descriptor.
Definition: XrdClUtils.hh:255
AddressType
Address type.
Definition: XrdClUtils.hh:93
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:44
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:245
Request status.
Definition: XrdClXRootDResponses.hh:214
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:51
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:34
Definition: XrdClUtils.hh:97
int pDescriptor
Definition: XrdClUtils.hh:261
Definition: XrdClUtils.hh:98
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
ScopedDescriptor(int descriptor)
Constructor.
Definition: XrdClUtils.hh:235
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:99
Smart descriptor - closes the descriptor on destruction.
Definition: XrdClUtils.hh:229
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.