Loading...
Searching...
No Matches
XrdClUtils.hh
Go to the documentation of this file.
//------------------------------------------------------------------------------
// Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#ifndef __XRD_CL_UTILS_HH__
#define __XRD_CL_UTILS_HH__
#include <string>
#include <vector>
#include "XrdCl/XrdClStatus.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdCl/XrdClPropertyList.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClPostMaster.hh"
#include "XrdCl/XrdClXRootDTransport.hh"
#include "XrdNet/XrdNetUtils.hh"
#include "XrdOuc/XrdOucTUtils.hh"
#include <sys/time.h>
#ifdef __linux__
#include <sys/fsuid.h>
#endif
namespace XrdCl
{
class Message;
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
class Utils
{
public:
//------------------------------------------------------------------------
//------------------------------------------------------------------------
template<class Container>
static void splitString( Container &result,
const std::string &input,
const std::string &delimiter )
{
/*
* This was done in order to not duplicate code as this method
* is also used in XrdHttp
* TODO: Maybe this method could be collapsed
* to avoid this middle-man call here
*/
XrdOucTUtils::splitString(result,input,delimiter);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static int GetIntParameter( const URL &url,
const std::string &name,
int defaultVal );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::string GetStringParameter( const URL &url,
const std::string &name,
const std::string &defaultVal );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
enum AddressType
{
IPAuto = 0,
IPAll = 1,
IPv6 = 2,
IPv4 = 3,
IPv4Mapped6 = 4
};
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static AddressType String2AddressType( const std::string &addressType );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static Status GetHostAddresses( std::vector<XrdNetAddr> &addresses,
const URL &url,
AddressType type );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void LogHostAddresses( Log *log,
uint64_t type,
const std::string &hostId,
std::vector<XrdNetAddr> &addresses );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::string TimeToString( time_t timestamp );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static uint64_t GetElapsedMicroSecs( timeval start, timeval end );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static XRootDStatus GetRemoteCheckSum( std::string &checkSum,
const std::string &checkSumType,
const URL &url );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static XRootDStatus GetLocalCheckSum( std::string &checkSum,
const std::string &checkSumType,
const std::string &path );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::string BytesToString( uint64_t bytes );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static XRootDStatus CheckTPC( const std::string &server,
uint16_t timeout = 0 );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static XRootDStatus CheckTPCLite( const std::string &server,
uint16_t timeout = 0 );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::string FQDNToCC( const std::string &fqdn );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static Status GetDirectoryEntries( std::vector<std::string> &entries,
const std::string &path );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static Status ProcessConfig( std::map<std::string, std::string> &config,
const std::string &file );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static Status ProcessConfigDir( std::map<std::string, std::string> &config,
const std::string &dir );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void Trim( std::string &str );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void LogPropertyList( Log *log,
uint64_t topic,
const char *format,
const PropertyList &list );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::string Char2Hex( uint8_t *array, uint16_t size );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::string NormalizeChecksum( const std::string &name,
const std::string &checksum );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::vector<std::string> GetSupportedCheckSums( const XrdCl::URL &url );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static std::string InferChecksumType( const XrdCl::URL &source,
const XrdCl::URL &destination,
bool zip = false );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static bool CheckEC( const Message *req, const URL &url );
//------------------------------------------------------------------------
//------------------------------------------------------------------------
inline static XrdCl::XRootDStatus GetProtocolVersion( const XrdCl::URL url, int &protver )
{
XrdCl::AnyObject qryResult;
XrdCl::XRootDStatus st = XrdCl::DefaultEnv::GetPostMaster()->
QueryTransport( url, XrdCl::XRootDQuery::ProtocolVersion, qryResult );
if( !st.IsOK() ) return st;
int *tmp = 0;
qryResult.Get( tmp );
if( !tmp )
return st;
protver = *tmp;
delete tmp;
return XrdCl::XRootDStatus();
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
inline static bool HasXAttr( const XrdCl::URL &url )
{
if( url.IsLocalFile() ) return true;
int protver = 0;
auto st = GetProtocolVersion( url, protver );
if( !st.IsOK() ) return false;
return protver >= kXR_PROTXATTVERSION;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
inline static bool HasPgRW( const XrdCl::URL &url )
{
if( url.IsLocalFile() ) return false;
int protver = 0;
auto st = GetProtocolVersion( url, protver );
if( !st.IsOK() ) return false;
return protver >= kXR_PROTPGRWVERSION;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void SplitChunks( std::vector<ChunkList> &listsvec,
const ChunkList &chunks,
const uint32_t maxcs,
const size_t maxc );
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
class ScopedDescriptor
{
public:
//------------------------------------------------------------------------
//------------------------------------------------------------------------
ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
~ScopedDescriptor() { if( pDescriptor >= 0 ) close( pDescriptor ); }
//------------------------------------------------------------------------
//------------------------------------------------------------------------
int Release()
{
int desc = pDescriptor;
pDescriptor = -1;
return desc;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
int GetDescriptor()
{
return pDescriptor;
}
private:
int pDescriptor;
};
#ifdef __linux__
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
class ScopedFsUidSetter
{
public:
//------------------------------------------------------------------------
//------------------------------------------------------------------------
ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
: pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
{
pOk = true;
pPrevFsUid = -1;
pPrevFsGid = -1;
//----------------------------------------------------------------------
//----------------------------------------------------------------------
if(pFsUid >= 0) {
pPrevFsUid = setfsuid(pFsUid);
if(setfsuid(pFsUid) != pFsUid) {
pOk = false;
return;
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
if(pFsGid >= 0) {
pPrevFsGid = setfsgid(pFsGid);
if(setfsgid(pFsGid) != pFsGid) {
pOk = false;
return;
}
}
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
~ScopedFsUidSetter() {
Log *log = DefaultEnv::GetLog();
if(pPrevFsUid >= 0) {
int retcode = setfsuid(pPrevFsUid);
log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
}
if(pPrevFsGid >= 0) {
int retcode = setfsgid(pPrevFsGid);
log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
}
}
bool IsOk() const {
return pOk;
}
private:
int pFsUid;
int pFsGid;
const std::string &pStreamName;
int pPrevFsUid;
int pPrevFsGid;
bool pOk;
};
#endif
}
#endif // __XRD_CL_UTILS_HH__
kXR_PROTXATTVERSION
#define kXR_PROTXATTVERSION
Definition XProtocol.hh:71
kXR_PROTPGRWVERSION
#define kXR_PROTPGRWVERSION
Definition XProtocol.hh:73
XrdClConstants.hh
XrdClDefaultEnv.hh
XrdClLog.hh
XrdClPostMaster.hh
XrdClPropertyList.hh
XrdClStatus.hh
XrdClURL.hh
XrdClXRootDResponses.hh
XrdClXRootDTransport.hh
XrdNetUtils.hh
XrdOucTUtils.hh
close
#define close(a)
Definition XrdPosix.hh:48
XrdCl::AnyObject
Definition XrdClAnyObject.hh:33
XrdCl::AnyObject::Get
void Get(Type &object)
Retrieve the object being held.
Definition XrdClAnyObject.hh:78
XrdCl::DefaultEnv::GetPostMaster
static PostMaster * GetPostMaster()
Get default post master.
Definition XrdClDefaultEnv.cc:453
XrdCl::Log
Handle diagnostics.
Definition XrdClLog.hh:101
XrdCl::Message
The message representation used throughout the system.
Definition XrdClMessage.hh:32
XrdCl::PropertyList
A key-value pair map storing both keys and values as strings.
Definition XrdClPropertyList.hh:41
XrdCl::ScopedDescriptor::~ScopedDescriptor
~ScopedDescriptor()
Destructor.
Definition XrdClUtils.hh:305
XrdCl::ScopedDescriptor::ScopedDescriptor
ScopedDescriptor(int descriptor)
Constructor.
Definition XrdClUtils.hh:300
XrdCl::ScopedDescriptor::GetDescriptor
int GetDescriptor()
Get the descriptor.
Definition XrdClUtils.hh:320
XrdCl::ScopedDescriptor::Release
int Release()
Release the descriptor being held.
Definition XrdClUtils.hh:310
XrdCl::URL
URL representation.
Definition XrdClURL.hh:31
XrdCl::URL::IsLocalFile
bool IsLocalFile() const
Definition XrdClURL.cc:474
XrdCl::Utils
Random utilities.
Definition XrdClUtils.hh:50
XrdCl::Utils::TimeToString
static std::string TimeToString(time_t timestamp)
Convert timestamp to a string.
Definition XrdClUtils.cc:256
XrdCl::Utils::CheckTPCLite
static XRootDStatus CheckTPCLite(const std::string &server, uint16_t timeout=0)
Definition XrdClUtils.cc:426
XrdCl::Utils::LogHostAddresses
static void LogHostAddresses(Log *log, uint64_t type, const std::string &hostId, std::vector< XrdNetAddr > &addresses)
Log all the addresses on the list.
Definition XrdClUtils.cc:234
XrdCl::Utils::NormalizeChecksum
static std::string NormalizeChecksum(const std::string &name, const std::string &checksum)
Normalize checksum.
Definition XrdClUtils.cc:648
XrdCl::Utils::ProcessConfig
static Status ProcessConfig(std::map< std::string, std::string > &config, const std::string &file)
Process a config file and return key-value pairs.
Definition XrdClUtils.cc:534
XrdCl::Utils::ProcessConfigDir
static Status ProcessConfigDir(std::map< std::string, std::string > &config, const std::string &dir)
Process a config directory and return key-value pairs.
Definition XrdClUtils.cc:566
XrdCl::Utils::FQDNToCC
static std::string FQDNToCC(const std::string &fqdn)
Convert the fully qualified host name to country code.
Definition XrdClUtils.cc:490
XrdCl::Utils::InferChecksumType
static std::string InferChecksumType(const XrdCl::URL &source, const XrdCl::URL &destination, bool zip=false)
Automatically infer the right checksum type.
Definition XrdClUtils.cc:771
XrdCl::Utils::LogPropertyList
static void LogPropertyList(Log *log, uint64_t topic, const char *format, const PropertyList &list)
Log property list.
Definition XrdClUtils.cc:617
XrdCl::Utils::Char2Hex
static std::string Char2Hex(uint8_t *array, uint16_t size)
Print a char array as hex.
Definition XrdClUtils.cc:635
XrdCl::Utils::splitString
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition XrdClUtils.hh:56
XrdCl::Utils::GetHostAddresses
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
Definition XrdClUtils.cc:140
XrdCl::Utils::GetElapsedMicroSecs
static uint64_t GetElapsedMicroSecs(timeval start, timeval end)
Get the elapsed microseconds between two timevals.
Definition XrdClUtils.cc:269
XrdCl::Utils::GetSupportedCheckSums
static std::vector< std::string > GetSupportedCheckSums(const XrdCl::URL &url)
Get supported checksum types for given URL.
Definition XrdClUtils.cc:665
XrdCl::Utils::String2AddressType
static AddressType String2AddressType(const std::string &addressType)
Interpret a string as address type, default to IPAll.
Definition XrdClUtils.cc:123
XrdCl::Utils::GetIntParameter
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
XrdCl::Utils::GetDirectoryEntries
static Status GetDirectoryEntries(std::vector< std::string > &entries, const std::string &path)
Get directory entries.
Definition XrdClUtils.cc:506
XrdCl::Utils::GetLocalCheckSum
static XRootDStatus GetLocalCheckSum(std::string &checkSum, const std::string &checkSumType, const std::string &path)
Get a checksum from local file.
Definition XrdClUtils.cc:330
XrdCl::Utils::BytesToString
static std::string BytesToString(uint64_t bytes)
Convert bytes to a human readable string.
Definition XrdClUtils.cc:367
XrdCl::Utils::Trim
static void Trim(std::string &str)
Trim a string.
Definition XrdClUtils.cc:606
XrdCl::Utils::CheckEC
static bool CheckEC(const Message *req, const URL &url)
Check if this client can support given EC redirect.
Definition XrdClUtils.cc:703
XrdCl::Utils::HasXAttr
static bool HasXAttr(const XrdCl::URL &url)
Definition XrdClUtils.hh:255
XrdCl::Utils::GetRemoteCheckSum
static XRootDStatus GetRemoteCheckSum(std::string &checkSum, const std::string &checkSumType, const URL &url)
Get a checksum from a remote xrootd server.
Definition XrdClUtils.cc:279
XrdCl::Utils::GetStringParameter
static std::string GetStringParameter(const URL &url, const std::string &name, const std::string &defaultVal)
Get a parameter either from the environment or URL.
Definition XrdClUtils.cc:104
XrdCl::Utils::CheckTPC
static XRootDStatus CheckTPC(const std::string &server, uint16_t timeout=0)
Check if peer supports tpc.
Definition XrdClUtils.cc:382
XrdCl::Utils::AddressType
AddressType
Address type.
Definition XrdClUtils.hh:87
XrdCl::Utils::IPAll
@ IPAll
Definition XrdClUtils.hh:89
XrdCl::Utils::IPv6
@ IPv6
Definition XrdClUtils.hh:90
XrdCl::Utils::IPv4
@ IPv4
Definition XrdClUtils.hh:91
XrdCl::Utils::IPAuto
@ IPAuto
Definition XrdClUtils.hh:88
XrdCl::Utils::IPv4Mapped6
@ IPv4Mapped6
Definition XrdClUtils.hh:92
XrdCl::Utils::GetProtocolVersion
static XrdCl::XRootDStatus GetProtocolVersion(const XrdCl::URL url, int &protver)
Definition XrdClUtils.hh:235
XrdCl::Utils::SplitChunks
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.
Definition XrdClUtils.cc:875
XrdCl::Utils::HasPgRW
static bool HasPgRW(const XrdCl::URL &url)
Definition XrdClUtils.hh:269
XrdCl::XRootDStatus
Request status.
Definition XrdClXRootDResponses.hh:219
XrdOucTUtils::splitString
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition XrdOucTUtils.hh:51
XrdCl
Definition XrdClAction.hh:34
XrdCl::ChunkList
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition XrdClXRootDResponses.hh:1056
XrdGlobal::Log
XrdSysError Log
Definition XrdConfig.cc:113
XrdCl::Status
Procedure execution status.
Definition XrdClStatus.hh:115
XrdCl::Status::IsOK
bool IsOK() const
We're fine.
Definition XrdClStatus.hh:124
XrdCl::XRootDQuery::ProtocolVersion
static const uint16_t ProtocolVersion
returns the protocol version
Definition XrdClPostMasterInterfaces.hh:291