XRootD
Loading...
Searching...
No Matches
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"
35
36#include <sys/time.h>
37
38#ifdef __linux__
39#include <sys/fsuid.h>
40#endif
41
42namespace XrdCl
43{
44 class Message;
45
46 //----------------------------------------------------------------------------
48 //----------------------------------------------------------------------------
49 class Utils
50 {
51 public:
52 //------------------------------------------------------------------------
54 //------------------------------------------------------------------------
55 template<class Container>
56 static void splitString( Container &result,
57 const std::string &input,
58 const std::string &delimiter )
59 {
60 /*
61 * This was done in order to not duplicate code as this method
62 * is also used in XrdHttp
63 * TODO: Maybe this method could be collapsed
64 * to avoid this middle-man call here
65 */
66 XrdOucTUtils::splitString(result,input,delimiter);
67 }
68
69 //------------------------------------------------------------------------
71 //------------------------------------------------------------------------
72 static int GetIntParameter( const URL &url,
73 const std::string &name,
74 int defaultVal );
75
76 //------------------------------------------------------------------------
78 //------------------------------------------------------------------------
79 static std::string GetStringParameter( const URL &url,
80 const std::string &name,
81 const std::string &defaultVal );
82
83 //------------------------------------------------------------------------
85 //------------------------------------------------------------------------
87 {
88 IPAuto = 0,
89 IPAll = 1,
90 IPv6 = 2,
91 IPv4 = 3,
92 IPv4Mapped6 = 4
93 };
94
95 //------------------------------------------------------------------------
97 //------------------------------------------------------------------------
98 static AddressType String2AddressType( const std::string &addressType );
99
100 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
103 static Status GetHostAddresses( std::vector<XrdNetAddr> &addresses,
104 const URL &url,
105 AddressType type );
106
107 //------------------------------------------------------------------------
109 //------------------------------------------------------------------------
110 static void LogHostAddresses( Log *log,
111 uint64_t type,
112 const std::string &hostId,
113 std::vector<XrdNetAddr> &addresses );
114
115 //------------------------------------------------------------------------
117 //------------------------------------------------------------------------
118 static std::string TimeToString( time_t timestamp );
119
120 //------------------------------------------------------------------------
122 //------------------------------------------------------------------------
123 static uint64_t GetElapsedMicroSecs( timeval start, timeval end );
124
125 //------------------------------------------------------------------------
127 //------------------------------------------------------------------------
128 static XRootDStatus GetRemoteCheckSum( std::string &checkSum,
129 const std::string &checkSumType,
130 const URL &url );
131
132 //------------------------------------------------------------------------
134 //------------------------------------------------------------------------
135 static XRootDStatus GetLocalCheckSum( std::string &checkSum,
136 const std::string &checkSumType,
137 const std::string &path );
138
139 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
142 static std::string BytesToString( uint64_t bytes );
143
144 //------------------------------------------------------------------------
146 //------------------------------------------------------------------------
147 static XRootDStatus CheckTPC( const std::string &server,
148 uint16_t timeout = 0 );
149
150 //------------------------------------------------------------------------
155 //------------------------------------------------------------------------
156 static XRootDStatus CheckTPCLite( const std::string &server,
157 uint16_t timeout = 0 );
158
159 //------------------------------------------------------------------------
161 //------------------------------------------------------------------------
162 static std::string FQDNToCC( const std::string &fqdn );
163
164 //------------------------------------------------------------------------
166 //------------------------------------------------------------------------
167 static Status GetDirectoryEntries( std::vector<std::string> &entries,
168 const std::string &path );
169
170 //------------------------------------------------------------------------
172 //------------------------------------------------------------------------
173 static Status ProcessConfig( std::map<std::string, std::string> &config,
174 const std::string &file );
175
176 //------------------------------------------------------------------------
178 //------------------------------------------------------------------------
179 static Status ProcessConfigDir( std::map<std::string, std::string> &config,
180 const std::string &dir );
181
182 //------------------------------------------------------------------------
184 //------------------------------------------------------------------------
185 static void Trim( std::string &str );
186
187 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
190 static void LogPropertyList( Log *log,
191 uint64_t topic,
192 const char *format,
193 const PropertyList &list );
194
195 //------------------------------------------------------------------------
197 //------------------------------------------------------------------------
198 static std::string Char2Hex( uint8_t *array, uint16_t size );
199
200 //------------------------------------------------------------------------
202 //------------------------------------------------------------------------
203 static std::string NormalizeChecksum( const std::string &name,
204 const std::string &checksum );
205
206 //------------------------------------------------------------------------
208 //------------------------------------------------------------------------
209 static std::vector<std::string> GetSupportedCheckSums( const XrdCl::URL &url );
210
211 //------------------------------------------------------------------------
219 //------------------------------------------------------------------------
220 static std::string InferChecksumType( const XrdCl::URL &source,
221 const XrdCl::URL &destination,
222 bool zip = false );
223
224 //------------------------------------------------------------------------
226 //------------------------------------------------------------------------
227 static bool CheckEC( const Message *req, const URL &url );
228
229 //------------------------------------------------------------------------
234 //------------------------------------------------------------------------
235 inline static XrdCl::XRootDStatus GetProtocolVersion( const XrdCl::URL url, int &protver )
236 {
237 XrdCl::AnyObject qryResult;
239 QueryTransport( url, XrdCl::XRootDQuery::ProtocolVersion, qryResult );
240 if( !st.IsOK() ) return st;
241 int *tmp = 0;
242 qryResult.Get( tmp );
243 if( !tmp )
244 return st;
245 protver = *tmp;
246 delete tmp;
247 return XrdCl::XRootDStatus();
248 }
249
250 //------------------------------------------------------------------------
254 //------------------------------------------------------------------------
255 inline static bool HasXAttr( const XrdCl::URL &url )
256 {
257 if( url.IsLocalFile() ) return true;
258 int protver = 0;
259 auto st = GetProtocolVersion( url, protver );
260 if( !st.IsOK() ) return false;
261 return protver >= kXR_PROTXATTVERSION;
262 }
263
264 //------------------------------------------------------------------------
268 //------------------------------------------------------------------------
269 inline static bool HasPgRW( const XrdCl::URL &url )
270 {
271 if( url.IsLocalFile() ) return false;
272 int protver = 0;
273 auto st = GetProtocolVersion( url, protver );
274 if( !st.IsOK() ) return false;
275 return protver >= kXR_PROTPGRWVERSION;
276 }
277
278 //------------------------------------------------------------------------
284 //------------------------------------------------------------------------
285 static void SplitChunks( std::vector<ChunkList> &listsvec,
286 const ChunkList &chunks,
287 const uint32_t maxcs,
288 const size_t maxc );
289 };
290
291 //----------------------------------------------------------------------------
293 //----------------------------------------------------------------------------
295 {
296 public:
297 //------------------------------------------------------------------------
299 //------------------------------------------------------------------------
300 ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
301
302 //------------------------------------------------------------------------
304 //------------------------------------------------------------------------
305 ~ScopedDescriptor() { if( pDescriptor >= 0 ) close( pDescriptor ); }
306
307 //------------------------------------------------------------------------
309 //------------------------------------------------------------------------
311 {
312 int desc = pDescriptor;
313 pDescriptor = -1;
314 return desc;
315 }
316
317 //------------------------------------------------------------------------
319 //------------------------------------------------------------------------
321 {
322 return pDescriptor;
323 }
324
325 private:
326 int pDescriptor;
327 };
328
329#ifdef __linux__
330 //----------------------------------------------------------------------------
332 //----------------------------------------------------------------------------
333 class ScopedFsUidSetter
334 {
335 public:
336 //------------------------------------------------------------------------
338 //------------------------------------------------------------------------
339 ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
340 : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
341 {
342 pOk = true;
343 pPrevFsUid = -1;
344 pPrevFsGid = -1;
345
346 //----------------------------------------------------------------------
348 //----------------------------------------------------------------------
349 if(pFsUid >= 0) {
350 pPrevFsUid = setfsuid(pFsUid);
351
352 if(setfsuid(pFsUid) != pFsUid) {
353 pOk = false;
354 return;
355 }
356 }
357
358 //----------------------------------------------------------------------
360 //----------------------------------------------------------------------
361 if(pFsGid >= 0) {
362 pPrevFsGid = setfsgid(pFsGid);
363
364 if(setfsgid(pFsGid) != pFsGid) {
365 pOk = false;
366 return;
367 }
368 }
369 }
370
371 //------------------------------------------------------------------------
373 //------------------------------------------------------------------------
374 ~ScopedFsUidSetter() {
375 Log *log = DefaultEnv::GetLog();
376
377 if(pPrevFsUid >= 0) {
378 int retcode = setfsuid(pPrevFsUid);
379 log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
380 }
381
382 if(pPrevFsGid >= 0) {
383 int retcode = setfsgid(pPrevFsGid);
384 log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
385 }
386 }
387
388 bool IsOk() const {
389 return pOk;
390 }
391
392 private:
393 int pFsUid;
394 int pFsGid;
395
396 const std::string &pStreamName;
397
398 int pPrevFsUid;
399 int pPrevFsGid;
400
401 bool pOk;
402 };
403#endif
404
405}
406
407#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:48
void Get(Type &object)
Retrieve the object being held.
static PostMaster * GetPostMaster()
Get default post master.
Handle diagnostics.
Definition XrdClLog.hh:101
The message representation used throughout the system.
A key-value pair map storing both keys and values as strings.
Smart descriptor - closes the descriptor on destruction.
~ScopedDescriptor()
Destructor.
ScopedDescriptor(int descriptor)
Constructor.
int GetDescriptor()
Get the descriptor.
int Release()
Release the descriptor being held.
URL representation.
Definition XrdClURL.hh:31
bool IsLocalFile() const
Definition XrdClURL.cc:474
Random utilities.
Definition XrdClUtils.hh:50
static std::string TimeToString(time_t timestamp)
Convert timestamp to a string.
static XRootDStatus CheckTPCLite(const std::string &server, uint16_t timeout=0)
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 NormalizeChecksum(const std::string &name, const std::string &checksum)
Normalize checksum.
static Status ProcessConfig(std::map< std::string, std::string > &config, const std::string &file)
Process a config file and return key-value pairs.
static Status ProcessConfigDir(std::map< std::string, std::string > &config, const std::string &dir)
Process a config directory and return key-value pairs.
static std::string FQDNToCC(const std::string &fqdn)
Convert the fully qualified host name to country code.
static std::string InferChecksumType(const XrdCl::URL &source, const XrdCl::URL &destination, bool zip=false)
Automatically infer the right checksum type.
static void LogPropertyList(Log *log, uint64_t topic, const char *format, const PropertyList &list)
Log property list.
static std::string Char2Hex(uint8_t *array, uint16_t size)
Print a char array as hex.
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition XrdClUtils.hh:56
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
static uint64_t GetElapsedMicroSecs(timeval start, timeval end)
Get the elapsed microseconds between two timevals.
static std::vector< std::string > GetSupportedCheckSums(const XrdCl::URL &url)
Get supported checksum types for given URL.
static AddressType String2AddressType(const std::string &addressType)
Interpret a string as address type, default to IPAll.
static int GetIntParameter(const URL &url, const std::string &name, int defaultVal)
Get a parameter either from the environment or URL.
Definition XrdClUtils.cc:81
static Status GetDirectoryEntries(std::vector< std::string > &entries, const std::string &path)
Get directory entries.
static XRootDStatus GetLocalCheckSum(std::string &checkSum, const std::string &checkSumType, const std::string &path)
Get a checksum from local file.
static std::string BytesToString(uint64_t bytes)
Convert bytes to a human readable string.
static void Trim(std::string &str)
Trim a string.
static bool CheckEC(const Message *req, const URL &url)
Check if this client can support given EC redirect.
static bool HasXAttr(const XrdCl::URL &url)
static XRootDStatus GetRemoteCheckSum(std::string &checkSum, const std::string &checkSumType, const URL &url)
Get a checksum from a remote xrootd server.
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 XRootDStatus CheckTPC(const std::string &server, uint16_t timeout=0)
Check if peer supports tpc.
AddressType
Address type.
Definition XrdClUtils.hh:87
static XrdCl::XRootDStatus GetProtocolVersion(const XrdCl::URL url, int &protver)
static void SplitChunks(std::vector< ChunkList > &listsvec, const ChunkList &chunks, const uint32_t maxcs, const size_t maxc)
Split chunks in a ChunkList into one or more ChunkLists.
static bool HasPgRW(const XrdCl::URL &url)
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
std::vector< ChunkInfo > ChunkList
List of chunks.
XrdSysError Log
Definition XrdConfig.cc:113
Procedure execution status.
bool IsOK() const
We're fine.
static const uint16_t ProtocolVersion
returns the protocol version