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,
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 time_t timeout = 0 );
149
150 //------------------------------------------------------------------------
155 //------------------------------------------------------------------------
156 static XRootDStatus CheckTPCLite( const std::string &server,
157 time_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 //------------------------------------------------------------------------
266 //------------------------------------------------------------------------
267 inline static bool HasKSameFS( const XrdCl::URL &url )
268 {
269 if( url.IsLocalFile() ) return true;
270 int protver = 0;
271 auto st = GetProtocolVersion( url, protver );
272 if( !st.IsOK() ) return false;
273 return protver >= kXR_PROTCLONEVERSION;
274 }
275
276 //------------------------------------------------------------------------
280 //------------------------------------------------------------------------
281 inline static bool HasPgRW( const XrdCl::URL &url )
282 {
283 if( url.IsLocalFile() ) return false;
284 int protver = 0;
285 auto st = GetProtocolVersion( url, protver );
286 if( !st.IsOK() ) return false;
287 return protver >= kXR_PROTPGRWVERSION;
288 }
289
290 //------------------------------------------------------------------------
296 //------------------------------------------------------------------------
297 static void SplitChunks( std::vector<ChunkList> &listsvec,
298 const ChunkList &chunks,
299 const uint32_t maxcs,
300 const size_t maxc );
301 };
302
303 //----------------------------------------------------------------------------
305 //----------------------------------------------------------------------------
307 {
308 public:
309 //------------------------------------------------------------------------
311 //------------------------------------------------------------------------
312 ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
313
314 //------------------------------------------------------------------------
316 //------------------------------------------------------------------------
317 ~ScopedDescriptor() { if( pDescriptor >= 0 ) close( pDescriptor ); }
318
319 //------------------------------------------------------------------------
321 //------------------------------------------------------------------------
323 {
324 int desc = pDescriptor;
325 pDescriptor = -1;
326 return desc;
327 }
328
329 //------------------------------------------------------------------------
331 //------------------------------------------------------------------------
333 {
334 return pDescriptor;
335 }
336
337 private:
338 int pDescriptor;
339 };
340
341#ifdef __linux__
342 //----------------------------------------------------------------------------
344 //----------------------------------------------------------------------------
345 class ScopedFsUidSetter
346 {
347 public:
348 //------------------------------------------------------------------------
350 //------------------------------------------------------------------------
351 ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
352 : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
353 {
354 pOk = true;
355 pPrevFsUid = -1;
356 pPrevFsGid = -1;
357
358 //----------------------------------------------------------------------
360 //----------------------------------------------------------------------
361 if(pFsUid >= 0) {
362 pPrevFsUid = setfsuid(pFsUid);
363
364 if(setfsuid(pFsUid) != pFsUid) {
365 pOk = false;
366 return;
367 }
368 }
369
370 //----------------------------------------------------------------------
372 //----------------------------------------------------------------------
373 if(pFsGid >= 0) {
374 pPrevFsGid = setfsgid(pFsGid);
375
376 if(setfsgid(pFsGid) != pFsGid) {
377 pOk = false;
378 return;
379 }
380 }
381 }
382
383 //------------------------------------------------------------------------
385 //------------------------------------------------------------------------
386 ~ScopedFsUidSetter() {
387 Log *log = DefaultEnv::GetLog();
388
389 if(pPrevFsUid >= 0) {
390 int retcode = setfsuid(pPrevFsUid);
391 log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
392 }
393
394 if(pPrevFsGid >= 0) {
395 int retcode = setfsgid(pPrevFsGid);
396 log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
397 }
398 }
399
400 bool IsOk() const {
401 return pOk;
402 }
403
404 private:
405 int pFsUid;
406 int pFsGid;
407
408 const std::string &pStreamName;
409
410 int pPrevFsUid;
411 int pPrevFsGid;
412
413 bool pOk;
414 };
415#endif
416
417}
418
419#endif // __XRD_CL_UTILS_HH__
#define kXR_PROTXATTVERSION
Definition XProtocol.hh:71
#define kXR_PROTCLONEVERSION
Definition XProtocol.hh:74
#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.
~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 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 bool HasKSameFS(const XrdCl::URL &url)
Check if given server supports kXR_clone and kXR_samefs.
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 CheckTPCLite(const std::string &server, time_t timeout=0)
static XRootDStatus GetRemoteCheckSum(std::string &checkSum, const std::string &checkSumType, const URL &url)
Get a checksum from a remote xrootd server.
static XRootDStatus CheckTPC(const std::string &server, time_t timeout=0)
Check if peer supports tpc.
static std::string GetStringParameter(const URL &url, const std::string &name, const std::string &defaultVal)
Get a parameter either from the environment or URL.
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