xrootd
Loading...
Searching...
No Matches
XrdClCheckSumHelper.hh
Go to the documentation of this file.
1/*
2 * CheckSumHelper.hh
3 *
4 * Created on: Sep 20, 2021
5 * Author: simonm
6 */
7
8#ifndef SRC_XRDCL_XRDCLCHECKSUMHELPER_HH_
9#define SRC_XRDCL_XRDCLCHECKSUMHELPER_HH_
10
13#include "XrdCks/XrdCksCalc.hh"
15#include "XrdCl/XrdClLog.hh"
16#include "XrdCl/XrdClUtils.hh"
17
18#include <string>
19
20namespace XrdCl
21{
22 //----------------------------------------------------------------------------
24 //----------------------------------------------------------------------------
26 {
27 public:
28 //------------------------------------------------------------------------
30 //------------------------------------------------------------------------
31 CheckSumHelper( const std::string &name,
32 const std::string &ckSumType ):
33 pName( name ),
34 pCkSumType( ckSumType ),
35 pCksCalcObj( 0 )
36 {};
37
38 //------------------------------------------------------------------------
40 //------------------------------------------------------------------------
42 {
43 delete pCksCalcObj;
44 }
45
46 //------------------------------------------------------------------------
48 //------------------------------------------------------------------------
50 {
51 if( pCkSumType.empty() )
52 return XRootDStatus();
53
54 Log *log = DefaultEnv::GetLog();
56
57 if( !cksMan )
58 {
59 log->Error( UtilityMsg, "Unable to get the checksum manager" );
61 }
62
64 if( !pCksCalcObj )
65 {
66 log->Error( UtilityMsg, "Unable to get a calculator for %s",
67 pCkSumType.c_str() );
69 }
70
71 return XRootDStatus();
72 }
73
74 //------------------------------------------------------------------------
75 // Update the checksum
76 //------------------------------------------------------------------------
77 void Update( const void *buffer, uint32_t size )
78 {
79 if( pCksCalcObj )
80 pCksCalcObj->Update( (const char *)buffer, size );
81 }
82
83 //------------------------------------------------------------------------
84 // Get checksum
85 //------------------------------------------------------------------------
86 XRootDStatus GetCheckSum( std::string &checkSum,
87 std::string &checkSumType )
88 {
89 using namespace XrdCl;
90 Log *log = DefaultEnv::GetLog();
91
92 int calcSize = 0;
93 auto st = GetCheckSumImpl( checkSumType, calcSize );
94 if( !st.IsOK() ) return st;
95
96 //----------------------------------------------------------------------
97 // Response
98 //----------------------------------------------------------------------
99 XrdCksData ckSum;
100 ckSum.Set( checkSumType.c_str() );
101 ckSum.Set( (void*)pCksCalcObj->Final(), calcSize );
102 char *cksBuffer = new char[265];
103 ckSum.Get( cksBuffer, 256 );
104 checkSum = checkSumType + ":";
105 checkSum += Utils::NormalizeChecksum( checkSumType, cksBuffer );
106 delete [] cksBuffer;
107
108 log->Dump( UtilityMsg, "Checksum for %s is: %s", pName.c_str(),
109 checkSum.c_str() );
110 return XRootDStatus();
111 }
112
113 template<typename T>
114 XRootDStatus GetRawCheckSum( const std::string &checkSumType, T &value )
115 {
116 int calcSize = 0;
117 auto st = GetCheckSumImpl( checkSumType, calcSize );
118 if( !st.IsOK() ) return st;
119 if( sizeof( T ) != calcSize )
121 "checksum size mismatch" );
122 value = *reinterpret_cast<T*>( pCksCalcObj->Final() );
123 return XRootDStatus();
124 }
125
126 const std::string& GetType()
127 {
128 return pCkSumType;
129 }
130
131 private:
132
133 //------------------------------------------------------------------------
134 // Get checksum
135 //------------------------------------------------------------------------
136 inline
137 XRootDStatus GetCheckSumImpl( const std::string &checkSumType,
138 int &calcSize )
139 {
140 using namespace XrdCl;
141 Log *log = DefaultEnv::GetLog();
142
143 //----------------------------------------------------------------------
144 // Sanity check
145 //----------------------------------------------------------------------
146 if( !pCksCalcObj )
147 {
148 log->Error( UtilityMsg, "Calculator for %s was not initialized",
149 pCkSumType.c_str() );
151 }
152
153 std::string calcType = pCksCalcObj->Type( calcSize );
154
155 if( calcType != checkSumType )
156 {
157 log->Error( UtilityMsg, "Calculated checksum: %s, requested "
158 "checksum: %s", pCkSumType.c_str(),
159 checkSumType.c_str() );
161 }
162
163 return XRootDStatus();
164 }
165
166 std::string pName;
167 std::string pCkSumType;
169 };
170}
171
172
173#endif /* SRC_XRDCL_XRDCLCHECKSUMHELPER_HH_ */
Definition XrdCksCalc.hh:40
virtual void Update(const char *Buff, int BLen)=0
virtual const char * Type(int &csSize)=0
virtual char * Final()=0
Definition XrdCksData.hh:38
int Set(const char *csName)
Definition XrdCksData.hh:81
int Get(char *Buff, int Blen)
Definition XrdCksData.hh:69
Check sum helper for stdio.
Definition XrdClCheckSumHelper.hh:26
XRootDStatus Initialize()
Initialize.
Definition XrdClCheckSumHelper.hh:49
const std::string & GetType()
Definition XrdClCheckSumHelper.hh:126
XRootDStatus GetCheckSum(std::string &checkSum, std::string &checkSumType)
Definition XrdClCheckSumHelper.hh:86
XRootDStatus GetCheckSumImpl(const std::string &checkSumType, int &calcSize)
Definition XrdClCheckSumHelper.hh:137
std::string pCkSumType
Definition XrdClCheckSumHelper.hh:167
void Update(const void *buffer, uint32_t size)
Definition XrdClCheckSumHelper.hh:77
std::string pName
Definition XrdClCheckSumHelper.hh:166
virtual ~CheckSumHelper()
Destructor.
Definition XrdClCheckSumHelper.hh:41
CheckSumHelper(const std::string &name, const std::string &ckSumType)
Constructor.
Definition XrdClCheckSumHelper.hh:31
XRootDStatus GetRawCheckSum(const std::string &checkSumType, T &value)
Definition XrdClCheckSumHelper.hh:114
XrdCksCalc * pCksCalcObj
Definition XrdClCheckSumHelper.hh:168
Manage the checksum calc objects.
Definition XrdClCheckSumManager.hh:42
XrdCksCalc * GetCalculator(const std::string &algName)
static CheckSumManager * GetCheckSumManager()
Get checksum manager.
static Log * GetLog()
Get default log.
Handle diagnostics.
Definition XrdClLog.hh:101
void Error(uint64_t topic, const char *format,...)
Report an error.
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
static std::string NormalizeChecksum(const std::string &name, const std::string &checksum)
Normalize checksum.
Request status.
Definition XrdClXRootDResponses.hh:219
Definition XrdClAction.hh:34
const uint16_t stError
An error occurred that could potentially be retried.
Definition XrdClStatus.hh:32
const uint16_t errInternal
Internal error.
Definition XrdClStatus.hh:56
const uint64_t UtilityMsg
Definition XrdClConstants.hh:33
const uint16_t errInvalidArgs
Definition XrdClStatus.hh:58
const uint16_t errCheckSumError
Definition XrdClStatus.hh:101