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"
31#include "XrdNet/XrdNetUtils.hh"
32
33#include <sys/time.h>
34
35#ifdef __linux__
36#include <sys/fsuid.h>
37#endif
38
39namespace 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,
99 IPv4Mapped6 = 4
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 std::string &server,
138 const std::string &path );
139
140 //------------------------------------------------------------------------
142 //------------------------------------------------------------------------
143 static XRootDStatus GetLocalCheckSum( std::string &checkSum,
144 const std::string &checkSumType,
145 const std::string &path );
146
147 //------------------------------------------------------------------------
149 //------------------------------------------------------------------------
150 static std::string BytesToString( uint64_t bytes );
151
152 //------------------------------------------------------------------------
154 //------------------------------------------------------------------------
155 static XRootDStatus CheckTPC( const std::string &server,
156 uint16_t timeout = 0 );
157
158 //------------------------------------------------------------------------
163 //------------------------------------------------------------------------
164 static XRootDStatus CheckTPCLite( const std::string &server,
165 uint16_t timeout = 0 );
166
167 //------------------------------------------------------------------------
169 //------------------------------------------------------------------------
170 static std::string FQDNToCC( const std::string &fqdn );
171
172 //------------------------------------------------------------------------
174 //------------------------------------------------------------------------
175 static Status GetDirectoryEntries( std::vector<std::string> &entries,
176 const std::string &path );
177
178 //------------------------------------------------------------------------
180 //------------------------------------------------------------------------
181 static Status ProcessConfig( std::map<std::string, std::string> &config,
182 const std::string &file );
183
184 //------------------------------------------------------------------------
186 //------------------------------------------------------------------------
187 static void Trim( std::string &str );
188
189 //------------------------------------------------------------------------
191 //------------------------------------------------------------------------
192 static void LogPropertyList( Log *log,
193 uint64_t topic,
194 const char *format,
195 const PropertyList &list );
196
197 //------------------------------------------------------------------------
199 //------------------------------------------------------------------------
200 static std::string Char2Hex( uint8_t *array, uint16_t size );
201
202 //------------------------------------------------------------------------
204 //------------------------------------------------------------------------
205 static std::string NormalizeChecksum( const std::string &name,
206 const std::string &checksum );
207
208 //------------------------------------------------------------------------
210 //------------------------------------------------------------------------
211 static std::vector<std::string> GetSupportedCheckSums( const XrdCl::URL &url );
212
213 //------------------------------------------------------------------------
221 //------------------------------------------------------------------------
222 static std::string InferChecksumType( const XrdCl::URL &source,
223 const XrdCl::URL &destination,
224 bool zip = false );
225 };
226
227 //----------------------------------------------------------------------------
229 //----------------------------------------------------------------------------
231 {
232 public:
233 //------------------------------------------------------------------------
235 //------------------------------------------------------------------------
236 ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
237
238 //------------------------------------------------------------------------
240 //------------------------------------------------------------------------
242
243 //------------------------------------------------------------------------
245 //------------------------------------------------------------------------
247 {
248 int desc = pDescriptor;
249 pDescriptor = -1;
250 return desc;
251 }
252
253 //------------------------------------------------------------------------
255 //------------------------------------------------------------------------
257 {
258 return pDescriptor;
259 }
260
261 private:
263 };
264
265#ifdef __linux__
266 //----------------------------------------------------------------------------
268 //----------------------------------------------------------------------------
269 class ScopedFsUidSetter
270 {
271 public:
272 //------------------------------------------------------------------------
274 //------------------------------------------------------------------------
275 ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
276 : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
277 {
278 pOk = true;
279 pPrevFsUid = -1;
280 pPrevFsGid = -1;
281
282 //----------------------------------------------------------------------
284 //----------------------------------------------------------------------
285 if(pFsUid >= 0) {
286 pPrevFsUid = setfsuid(pFsUid);
287
288 if(setfsuid(pFsUid) != pFsUid) {
289 pOk = false;
290 return;
291 }
292 }
293
294 //----------------------------------------------------------------------
296 //----------------------------------------------------------------------
297 if(pFsGid >= 0) {
298 pPrevFsGid = setfsgid(pFsGid);
299
300 if(setfsgid(pFsGid) != pFsGid) {
301 pOk = false;
302 return;
303 }
304 }
305 }
306
307 //------------------------------------------------------------------------
309 //------------------------------------------------------------------------
310 ~ScopedFsUidSetter() {
311 Log *log = DefaultEnv::GetLog();
312
313 if(pPrevFsUid >= 0) {
314 int retcode = setfsuid(pPrevFsUid);
315 log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
316 }
317
318 if(pPrevFsGid >= 0) {
319 int retcode = setfsgid(pPrevFsGid);
320 log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
321 }
322 }
323
324 bool IsOk() const {
325 return pOk;
326 }
327
328 private:
329 int pFsUid;
330 int pFsGid;
331
332 const std::string &pStreamName;
333
334 int pPrevFsUid;
335 int pPrevFsGid;
336
337 bool pOk;
338 };
339#endif
340
341}
342
343#endif // __XRD_CL_UTILS_HH__
#define close(a)
Definition: XrdPosix.hh:43
static Log * GetLog()
Get default log.
Handle diagnostics.
Definition: XrdClLog.hh:102
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:231
int pDescriptor
Definition: XrdClUtils.hh:262
~ScopedDescriptor()
Destructor.
Definition: XrdClUtils.hh:241
ScopedDescriptor(int descriptor)
Constructor.
Definition: XrdClUtils.hh:236
int GetDescriptor()
Get the descriptor.
Definition: XrdClUtils.hh:256
int Release()
Release the descriptor being held.
Definition: XrdClUtils.hh:246
URL representation.
Definition: XrdClURL.hh:31
Random utilities.
Definition: XrdClUtils.hh:45
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 XRootDStatus GetRemoteCheckSum(std::string &checkSum, const std::string &checkSumType, const std::string &server, const std::string &path)
Get a checksum from a remote xrootd server.
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 void LogPropertyList(Log *log, uint64_t topic, const char *format, const PropertyList &list)
Log property list.
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 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 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:94
@ IPAll
Definition: XrdClUtils.hh:96
@ IPv6
Definition: XrdClUtils.hh:97
@ IPv4
Definition: XrdClUtils.hh:98
@ IPAuto
Definition: XrdClUtils.hh:95
@ IPv4Mapped6
Definition: XrdClUtils.hh:99
Request status.
Definition: XrdClXRootDResponses.hh:213
Definition: XrdClAnyObject.hh:26
const uint64_t XRootDTransportMsg
Definition: XrdClConstants.hh:34
Procedure execution status.
Definition: XrdClStatus.hh:110