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 protver = *tmp;
244 delete tmp;
245 return XrdCl::XRootDStatus();
246 }
247
248 //------------------------------------------------------------------------
252 //------------------------------------------------------------------------
253 inline static bool HasXAttr( const XrdCl::URL &url )
254 {
255 if( url.IsLocalFile() ) return true;
256 int protver = 0;
257 auto st = GetProtocolVersion( url, protver );
258 if( !st.IsOK() ) return false;
259 return protver >= kXR_PROTXATTVERSION;
260 }
261
262 //------------------------------------------------------------------------
266 //------------------------------------------------------------------------
267 inline static bool HasPgRW( const XrdCl::URL &url )
268 {
269 if( url.IsLocalFile() ) return false;
270 int protver = 0;
271 auto st = GetProtocolVersion( url, protver );
272 if( !st.IsOK() ) return false;
273 return protver >= kXR_PROTPGRWVERSION;
274 }
275
276 //------------------------------------------------------------------------
282 //------------------------------------------------------------------------
283 static void SplitChunks( std::vector<ChunkList> &listsvec,
284 const ChunkList &chunks,
285 const uint32_t maxcs,
286 const size_t maxc );
287 };
288
289 //----------------------------------------------------------------------------
291 //----------------------------------------------------------------------------
293 {
294 public:
295 //------------------------------------------------------------------------
297 //------------------------------------------------------------------------
298 ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
299
300 //------------------------------------------------------------------------
302 //------------------------------------------------------------------------
304
305 //------------------------------------------------------------------------
307 //------------------------------------------------------------------------
309 {
310 int desc = pDescriptor;
311 pDescriptor = -1;
312 return desc;
313 }
314
315 //------------------------------------------------------------------------
317 //------------------------------------------------------------------------
319 {
320 return pDescriptor;
321 }
322
323 private:
325 };
326
327#ifdef __linux__
328 //----------------------------------------------------------------------------
330 //----------------------------------------------------------------------------
331 class ScopedFsUidSetter
332 {
333 public:
334 //------------------------------------------------------------------------
336 //------------------------------------------------------------------------
337 ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
338 : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
339 {
340 pOk = true;
341 pPrevFsUid = -1;
342 pPrevFsGid = -1;
343
344 //----------------------------------------------------------------------
346 //----------------------------------------------------------------------
347 if(pFsUid >= 0) {
348 pPrevFsUid = setfsuid(pFsUid);
349
350 if(setfsuid(pFsUid) != pFsUid) {
351 pOk = false;
352 return;
353 }
354 }
355
356 //----------------------------------------------------------------------
358 //----------------------------------------------------------------------
359 if(pFsGid >= 0) {
360 pPrevFsGid = setfsgid(pFsGid);
361
362 if(setfsgid(pFsGid) != pFsGid) {
363 pOk = false;
364 return;
365 }
366 }
367 }
368
369 //------------------------------------------------------------------------
371 //------------------------------------------------------------------------
372 ~ScopedFsUidSetter() {
373 Log *log = DefaultEnv::GetLog();
374
375 if(pPrevFsUid >= 0) {
376 int retcode = setfsuid(pPrevFsUid);
377 log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
378 }
379
380 if(pPrevFsGid >= 0) {
381 int retcode = setfsgid(pPrevFsGid);
382 log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
383 }
384 }
385
386 bool IsOk() const {
387 return pOk;
388 }
389
390 private:
391 int pFsUid;
392 int pFsGid;
393
394 const std::string &pStreamName;
395
396 int pPrevFsUid;
397 int pPrevFsGid;
398
399 bool pOk;
400 };
401#endif
402
403}
404
405#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.
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:293
int pDescriptor
Definition XrdClUtils.hh:324
~ScopedDescriptor()
Destructor.
Definition XrdClUtils.hh:303
ScopedDescriptor(int descriptor)
Constructor.
Definition XrdClUtils.hh:298
int GetDescriptor()
Get the descriptor.
Definition XrdClUtils.hh:318
int Release()
Release the descriptor being held.
Definition XrdClUtils.hh:308
URL representation.
Definition XrdClURL.hh:31
bool IsLocalFile() const
Random utilities.
Definition XrdClUtils.hh:50
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 void SplitChunks(std::vector< ChunkList > &listsvec, const ChunkList &chunks, const uint32_t maxcs, const size_t maxc)
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:56
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:253
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:87
@ IPAll
Definition XrdClUtils.hh:89
@ IPv6
Definition XrdClUtils.hh:90
@ IPv4
Definition XrdClUtils.hh:91
@ IPAuto
Definition XrdClUtils.hh:88
@ IPv4Mapped6
Definition XrdClUtils.hh:92
static XrdCl::XRootDStatus GetProtocolVersion(const XrdCl::URL url, int &protver)
Definition XrdClUtils.hh:235
static bool HasPgRW(const XrdCl::URL &url)
Definition XrdClUtils.hh:267
Request status.
Definition XrdClXRootDResponses.hh:219
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition XrdOucTUtils.hh:49
Definition XrdClAction.hh:34
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition XrdClXRootDResponses.hh:1055
Procedure execution status.
Definition XrdClStatus.hh:115
bool IsOK() const
We're fine.
Definition XrdClStatus.hh:124
static const uint16_t ProtocolVersion
returns the protocol version
Definition XrdClPostMasterInterfaces.hh:291