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"
33#include "XrdNet/XrdNetUtils.hh"
34
35#include <sys/time.h>
36
37#ifdef __linux__
38#include <sys/fsuid.h>
39#endif
40
41namespace XrdCl
42{
43 class Message;
44
45 //----------------------------------------------------------------------------
47 //----------------------------------------------------------------------------
48 class Utils
49 {
50 public:
51 //------------------------------------------------------------------------
53 //------------------------------------------------------------------------
54 template<class Container>
55 static void splitString( Container &result,
56 const std::string &input,
57 const std::string &delimiter )
58 {
59 size_t start = 0;
60 size_t end = 0;
61 size_t length = 0;
62
63 do
64 {
65 end = input.find( delimiter, start );
66
67 if( end != std::string::npos )
68 length = end - start;
69 else
70 length = input.length() - start;
71
72 if( length )
73 result.push_back( input.substr( start, length ) );
74
75 start = end + delimiter.size();
76 }
77 while( end != std::string::npos );
78 }
79
80 //------------------------------------------------------------------------
82 //------------------------------------------------------------------------
83 static int GetIntParameter( const URL &url,
84 const std::string &name,
85 int defaultVal );
86
87 //------------------------------------------------------------------------
89 //------------------------------------------------------------------------
90 static std::string GetStringParameter( const URL &url,
91 const std::string &name,
92 const std::string &defaultVal );
93
94 //------------------------------------------------------------------------
96 //------------------------------------------------------------------------
98 {
99 IPAuto = 0,
100 IPAll = 1,
101 IPv6 = 2,
102 IPv4 = 3,
103 IPv4Mapped6 = 4
104 };
105
106 //------------------------------------------------------------------------
108 //------------------------------------------------------------------------
109 static AddressType String2AddressType( const std::string &addressType );
110
111 //------------------------------------------------------------------------
113 //------------------------------------------------------------------------
114 static Status GetHostAddresses( std::vector<XrdNetAddr> &addresses,
115 const URL &url,
116 AddressType type );
117
118 //------------------------------------------------------------------------
120 //------------------------------------------------------------------------
121 static void LogHostAddresses( Log *log,
122 uint64_t type,
123 const std::string &hostId,
124 std::vector<XrdNetAddr> &addresses );
125
126 //------------------------------------------------------------------------
128 //------------------------------------------------------------------------
129 static std::string TimeToString( time_t timestamp );
130
131 //------------------------------------------------------------------------
133 //------------------------------------------------------------------------
134 static uint64_t GetElapsedMicroSecs( timeval start, timeval end );
135
136 //------------------------------------------------------------------------
138 //------------------------------------------------------------------------
139 static XRootDStatus GetRemoteCheckSum( std::string &checkSum,
140 const std::string &checkSumType,
141 const URL &url );
142
143 //------------------------------------------------------------------------
145 //------------------------------------------------------------------------
146 static XRootDStatus GetLocalCheckSum( std::string &checkSum,
147 const std::string &checkSumType,
148 const std::string &path );
149
150 //------------------------------------------------------------------------
152 //------------------------------------------------------------------------
153 static std::string BytesToString( uint64_t bytes );
154
155 //------------------------------------------------------------------------
157 //------------------------------------------------------------------------
158 static XRootDStatus CheckTPC( const std::string &server,
159 uint16_t timeout = 0 );
160
161 //------------------------------------------------------------------------
166 //------------------------------------------------------------------------
167 static XRootDStatus CheckTPCLite( const std::string &server,
168 uint16_t timeout = 0 );
169
170 //------------------------------------------------------------------------
172 //------------------------------------------------------------------------
173 static std::string FQDNToCC( const std::string &fqdn );
174
175 //------------------------------------------------------------------------
177 //------------------------------------------------------------------------
178 static Status GetDirectoryEntries( std::vector<std::string> &entries,
179 const std::string &path );
180
181 //------------------------------------------------------------------------
183 //------------------------------------------------------------------------
184 static Status ProcessConfig( std::map<std::string, std::string> &config,
185 const std::string &file );
186
187 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
190 static Status ProcessConfigDir( std::map<std::string, std::string> &config,
191 const std::string &dir );
192
193 //------------------------------------------------------------------------
195 //------------------------------------------------------------------------
196 static void Trim( std::string &str );
197
198 //------------------------------------------------------------------------
200 //------------------------------------------------------------------------
201 static void LogPropertyList( Log *log,
202 uint64_t topic,
203 const char *format,
204 const PropertyList &list );
205
206 //------------------------------------------------------------------------
208 //------------------------------------------------------------------------
209 static std::string Char2Hex( uint8_t *array, uint16_t size );
210
211 //------------------------------------------------------------------------
213 //------------------------------------------------------------------------
214 static std::string NormalizeChecksum( const std::string &name,
215 const std::string &checksum );
216
217 //------------------------------------------------------------------------
219 //------------------------------------------------------------------------
220 static std::vector<std::string> GetSupportedCheckSums( const XrdCl::URL &url );
221
222 //------------------------------------------------------------------------
230 //------------------------------------------------------------------------
231 static std::string InferChecksumType( const XrdCl::URL &source,
232 const XrdCl::URL &destination,
233 bool zip = false );
234
235 //------------------------------------------------------------------------
237 //------------------------------------------------------------------------
238 static bool CheckEC( const Message *req, const URL &url );
239
240 //------------------------------------------------------------------------
245 //------------------------------------------------------------------------
246 inline static XrdCl::XRootDStatus GetProtocolVersion( const XrdCl::URL url, int &protver )
247 {
248 XrdCl::AnyObject qryResult;
250 QueryTransport( url, XrdCl::XRootDQuery::ProtocolVersion, qryResult );
251 if( !st.IsOK() ) return st;
252 int *tmp = 0;
253 qryResult.Get( tmp );
254 protver = *tmp;
255 delete tmp;
256 return XrdCl::XRootDStatus();
257 }
258
259 //------------------------------------------------------------------------
263 //------------------------------------------------------------------------
264 inline static bool HasXAttr( const XrdCl::URL &url )
265 {
266 if( url.IsLocalFile() ) return true;
267 int protver = 0;
268 auto st = GetProtocolVersion( url, protver );
269 if( !st.IsOK() ) return false;
270 return protver >= kXR_PROTXATTVERSION;
271 }
272
273 //------------------------------------------------------------------------
277 //------------------------------------------------------------------------
278 inline static bool HasPgRW( const XrdCl::URL &url )
279 {
280 if( url.IsLocalFile() ) return false;
281 int protver = 0;
282 auto st = GetProtocolVersion( url, protver );
283 if( !st.IsOK() ) return false;
284 return protver >= kXR_PROTPGRWVERSION;
285 }
286 };
287
288 //----------------------------------------------------------------------------
290 //----------------------------------------------------------------------------
292 {
293 public:
294 //------------------------------------------------------------------------
296 //------------------------------------------------------------------------
297 ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
298
299 //------------------------------------------------------------------------
301 //------------------------------------------------------------------------
303
304 //------------------------------------------------------------------------
306 //------------------------------------------------------------------------
308 {
309 int desc = pDescriptor;
310 pDescriptor = -1;
311 return desc;
312 }
313
314 //------------------------------------------------------------------------
316 //------------------------------------------------------------------------
318 {
319 return pDescriptor;
320 }
321
322 private:
324 };
325
326#ifdef __linux__
327 //----------------------------------------------------------------------------
329 //----------------------------------------------------------------------------
330 class ScopedFsUidSetter
331 {
332 public:
333 //------------------------------------------------------------------------
335 //------------------------------------------------------------------------
336 ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
337 : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
338 {
339 pOk = true;
340 pPrevFsUid = -1;
341 pPrevFsGid = -1;
342
343 //----------------------------------------------------------------------
345 //----------------------------------------------------------------------
346 if(pFsUid >= 0) {
347 pPrevFsUid = setfsuid(pFsUid);
348
349 if(setfsuid(pFsUid) != pFsUid) {
350 pOk = false;
351 return;
352 }
353 }
354
355 //----------------------------------------------------------------------
357 //----------------------------------------------------------------------
358 if(pFsGid >= 0) {
359 pPrevFsGid = setfsgid(pFsGid);
360
361 if(setfsgid(pFsGid) != pFsGid) {
362 pOk = false;
363 return;
364 }
365 }
366 }
367
368 //------------------------------------------------------------------------
370 //------------------------------------------------------------------------
371 ~ScopedFsUidSetter() {
372 Log *log = DefaultEnv::GetLog();
373
374 if(pPrevFsUid >= 0) {
375 int retcode = setfsuid(pPrevFsUid);
376 log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
377 }
378
379 if(pPrevFsGid >= 0) {
380 int retcode = setfsgid(pPrevFsGid);
381 log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
382 }
383 }
384
385 bool IsOk() const {
386 return pOk;
387 }
388
389 private:
390 int pFsUid;
391 int pFsGid;
392
393 const std::string &pStreamName;
394
395 int pPrevFsUid;
396 int pPrevFsGid;
397
398 bool pOk;
399 };
400#endif
401
402}
403
404#endif // __XRD_CL_UTILS_HH__
#define kXR_PROTXATTVERSION
Definition: XProtocol.hh:71
#define kXR_PROTPGRWVERSION
Definition: XProtocol.hh:73
#define close(a)
Definition: XrdPosix.hh:43
Definition: XrdClAnyObject.hh:33
void Get(Type &object)
Retrieve the object being held.
Definition: XrdClAnyObject.hh:78
static PostMaster * GetPostMaster()
Get default post master.
static Log * GetLog()
Get default log.
Handle diagnostics.
Definition: XrdClLog.hh:101
The message representation used throughout the system.
Definition: XrdClMessage.hh:30
A key-value pair map storing both keys and values as strings.
Definition: XrdClPropertyList.hh:41
Smart descriptor - closes the descriptor on destruction.
Definition: XrdClUtils.hh:292
int pDescriptor
Definition: XrdClUtils.hh:323
~ScopedDescriptor()
Destructor.
Definition: XrdClUtils.hh:302
ScopedDescriptor(int descriptor)
Constructor.
Definition: XrdClUtils.hh:297
int GetDescriptor()
Get the descriptor.
Definition: XrdClUtils.hh:317
int Release()
Release the descriptor being held.
Definition: XrdClUtils.hh:307
URL representation.
Definition: XrdClURL.hh:31
bool IsLocalFile() const
Random utilities.
Definition: XrdClUtils.hh:49
static XRootDStatus CheckTPCLite(const std::string &server, uint16_t timeout=0)
static void Trim(std::string &str)
Trim a string.
static std::string Char2Hex(uint8_t *array, uint16_t size)
Print a char array as hex.
static uint64_t GetElapsedMicroSecs(timeval start, timeval end)
Get the elapsed microseconds between two timevals.
static Status GetDirectoryEntries(std::vector< std::string > &entries, const std::string &path)
Get directory entries.
static std::string FQDNToCC(const std::string &fqdn)
Convert the fully qualified host name to country code.
static std::string NormalizeChecksum(const std::string &name, const std::string &checksum)
Normalize checksum.
static std::vector< std::string > GetSupportedCheckSums(const XrdCl::URL &url)
Get supported checksum types for given URL.
static int GetIntParameter(const URL &url, const std::string &name, int defaultVal)
Get a parameter either from the environment or URL.
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
static void LogHostAddresses(Log *log, uint64_t type, const std::string &hostId, std::vector< XrdNetAddr > &addresses)
Log all the addresses on the list.
static bool CheckEC(const Message *req, const URL &url)
Check if this client can support given EC redirect.
static XRootDStatus GetRemoteCheckSum(std::string &checkSum, const std::string &checkSumType, const URL &url)
Get a checksum from a remote xrootd server.
static void LogPropertyList(Log *log, uint64_t topic, const char *format, const PropertyList &list)
Log property list.
static Status ProcessConfigDir(std::map< std::string, std::string > &config, const std::string &dir)
Process a config directory and return key-value pairs.
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdClUtils.hh:55
static XRootDStatus CheckTPC(const std::string &server, uint16_t timeout=0)
Check if peer supports tpc.
static AddressType String2AddressType(const std::string &addressType)
Interpret a string as address type, default to IPAll.
static XRootDStatus GetLocalCheckSum(std::string &checkSum, const std::string &checkSumType, const std::string &path)
Get a checksum from local file.
static bool HasXAttr(const XrdCl::URL &url)
Definition: XrdClUtils.hh:264
static Status ProcessConfig(std::map< std::string, std::string > &config, const std::string &file)
Process a config file and return key-value pairs.
static std::string BytesToString(uint64_t bytes)
Convert bytes to a human readable string.
static std::string InferChecksumType(const XrdCl::URL &source, const XrdCl::URL &destination, bool zip=false)
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 std::string TimeToString(time_t timestamp)
Convert timestamp to a string.
AddressType
Address type.
Definition: XrdClUtils.hh:98
@ IPAll
Definition: XrdClUtils.hh:100
@ IPv6
Definition: XrdClUtils.hh:101
@ IPv4
Definition: XrdClUtils.hh:102
@ IPAuto
Definition: XrdClUtils.hh:99
@ IPv4Mapped6
Definition: XrdClUtils.hh:103
static XrdCl::XRootDStatus GetProtocolVersion(const XrdCl::URL url, int &protver)
Definition: XrdClUtils.hh:246
static bool HasPgRW(const XrdCl::URL &url)
Definition: XrdClUtils.hh:278
Request status.
Definition: XrdClXRootDResponses.hh:219
Definition: XrdClAction.hh:34
const uint64_t XRootDTransportMsg
Definition: XrdClConstants.hh:37
Procedure execution status.
Definition: XrdClStatus.hh:114
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:123
static const uint16_t ProtocolVersion
returns the protocol version
Definition: XrdClPostMasterInterfaces.hh:291