XRootD
Loading...
Searching...
No Matches
XrdClCheckSumManager.cc
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// Copyright (c) 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
20#include "XrdCl/XrdClLog.hh"
21#include "XrdCl/XrdClUtils.hh"
24#include "XrdCks/XrdCksCalc.hh"
26#include "XrdCks/XrdCksCalc.hh"
31#include "XrdSys/XrdSysE2T.hh"
33#include "XrdVersion.hh"
34
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <fcntl.h>
38#include <memory>
39
41
42namespace XrdCl
43{
44 //----------------------------------------------------------------------------
45 // Constructor
46 //----------------------------------------------------------------------------
48 {
49 pLoader = new XrdCksLoader( XrdVERSIONINFOVAR( XrdCl ) );
50 pCalculators["md5"] = new XrdCksCalcmd5();
51 pCalculators["crc32"] = new XrdCksCalccrc32;
52 pCalculators["crc32c"] = new XrdCksCalccrc32C;
53 pCalculators["adler32"] = new XrdCksCalcadler32;
54 }
55
56 //----------------------------------------------------------------------------
57 // Destructor
58 //----------------------------------------------------------------------------
60 {
61 CalcMap::iterator it;
62 for( it = pCalculators.begin(); it != pCalculators.end(); ++it )
63 delete it->second;
64 delete pLoader;
65 }
66
67 //----------------------------------------------------------------------------
68 // Get a calculator
69 //----------------------------------------------------------------------------
70 XrdCksCalc *CheckSumManager::GetCalculator( const std::string &algName )
71 {
72 Log *log = DefaultEnv::GetLog();
73 XrdSysMutexHelper scopedLock( pMutex );
74 CalcMap::iterator it = pCalculators.find( algName );
75 if( it == pCalculators.end() )
76 {
77 char *errBuff = new char[1024];
78 log->Dump( UtilityMsg, "Attempting to load a calculator for: %s",
79 algName.c_str() );
80 XrdCksCalc *c = pLoader->Load( algName.c_str(), "", errBuff, 1024 );
81 if( !c )
82 {
83 log->Error( UtilityMsg, "Unable to load %s calculator: %s",
84 algName.c_str(), errBuff );
85 delete [] errBuff;
86 return 0;
87
88 }
89 delete [] errBuff;
90
91 pCalculators[algName] = c;
92 return c->New();
93 }
94 return it->second->New();;
95 }
96
97 //----------------------------------------------------------------------------
98 // Stop the manager
99 //----------------------------------------------------------------------------
101 const std::string &algName,
102 const std::string &filePath )
103 {
104 //--------------------------------------------------------------------------
105 // Get a calculator
106 //--------------------------------------------------------------------------
107 Log *log = DefaultEnv::GetLog();
108 XrdCksCalc *calc = GetCalculator( algName );
109
110 if( !calc )
111 {
112 log->Error( UtilityMsg, "Unable to get a calculator for %s",
113 algName.c_str() );
114 return false;
115 }
116 std::unique_ptr<XrdCksCalc> calcPtr( calc );
117
118 //--------------------------------------------------------------------------
119 // Open the file
120 //--------------------------------------------------------------------------
121 log->Debug( UtilityMsg, "Opening %s for reading (checksum calc)",
122 filePath.c_str() );
123
124 int fd = open( filePath.c_str(), O_RDONLY );
125 if( fd == -1 )
126 {
127 log->Error( UtilityMsg, "Unable to open %s: %s", filePath.c_str(),
128 XrdSysE2T( errno ) );
129 return false;
130 }
131
132 //--------------------------------------------------------------------------
133 // Calculate the checksum
134 //--------------------------------------------------------------------------
135 const uint32_t buffSize = 2*1024*1024;
136 char *buffer = new char[buffSize];
137 int64_t bytesRead = 0;
138
139 while( (bytesRead = read( fd, buffer, buffSize )) )
140 {
141 if( bytesRead == -1 )
142 {
143 log->Error( UtilityMsg, "Unable read from %s: %s", filePath.c_str(),
144 XrdSysE2T( errno ) );
145 close( fd );
146 delete [] buffer;
147 return false;
148 }
149 calc->Update( buffer, bytesRead );
150 }
151
152 int size;
153 calc->Type( size );
154 result.Set( (void*)calc->Final(), size );
155
156 //--------------------------------------------------------------------------
157 // Clean up
158 //--------------------------------------------------------------------------
159 delete [] buffer;
160 close( fd );
161 return true;
162 }
163}
XrdVERSIONINFOREF(XrdCl)
#define close(a)
Definition XrdPosix.hh:43
#define open
Definition XrdPosix.hh:71
#define read(a, b, c)
Definition XrdPosix.hh:77
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104
virtual void Update(const char *Buff, int BLen)=0
virtual XrdCksCalc * New()=0
virtual const char * Type(int &csSize)=0
virtual char * Final()=0
int Set(const char *csName)
Definition XrdCksData.hh:81
XrdCksCalc * GetCalculator(const std::string &algName)
bool Calculate(XrdCksData &result, const std::string &algName, const std::string &filePath)
Calculate a checksum of for a given file.
static Log * GetLog()
Get default log.
Handle diagnostics.
Definition XrdClLog.hh:101
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition XrdClLog.cc:231
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
Definition XrdClLog.cc:299
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
Definition XrdClLog.cc:282
const uint64_t UtilityMsg