-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathXrdChecksum.cc
More file actions
248 lines (194 loc) · 6.8 KB
/
XrdChecksum.cc
File metadata and controls
248 lines (194 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <sstream>
#include <algorithm>
#include <fstream>
#include "XrdVersion.hh"
#include "XrdChecksum.hh"
#include "MultiuserFileSystem.hh"
#include "XrdOss/XrdOss.hh"
#include "XrdSfs/XrdSfsInterface.hh"
#include "XrdSec/XrdSecEntity.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSys/XrdSysXAttr.hh"
#include "XrdCks/XrdCks.hh"
extern XrdSysXAttr *XrdSysXAttrActive;
extern MultiuserFileSystem* g_multisuer_oss;
#define ATTR_PREFIX "XrdCks.Human."
ChecksumManager::ChecksumManager(XrdSysError *erP, int iosz,
XrdVersionInfo &vInfo, bool autoload):
XrdCksManager::XrdCksManager(erP, iosz, vInfo, autoload),
m_log(*erP)
{
}
int
ChecksumManager::Set(const char *lfn, const ChecksumState &state)
{
int retval = 0;
if (state.Get(ChecksumManager::CKSUM).size())
retval = this->Set(lfn, "CKSUM", state.Get(ChecksumManager::CKSUM).c_str());
if (state.Get(ChecksumManager::ADLER32).size())
retval = this->Set(lfn, "ADLER32", state.Get(ChecksumManager::ADLER32).c_str());
if (state.Get(ChecksumManager::CRC32).size())
retval = this->Set(lfn, "CRC32", state.Get(ChecksumManager::CRC32).c_str());
if (state.Get(ChecksumManager::MD5).size())
retval = this->Set(lfn, "MD5", state.Get(ChecksumManager::MD5).c_str());
if (state.Get(ChecksumManager::CVMFS).size())
retval = this->Set(lfn, "CVMFS", state.Get(ChecksumManager::CVMFS).c_str());
return retval;
}
int ChecksumManager::Calc( const char *lfn, XrdCksData &Cks, int doSet)
{
std::string pfn = this->LFN2PFN(lfn);
// Figure out what checksum they want
int digests = 0;
int return_digest = 0;
if (doSet)
{
// doSet indicates that the new checksum value must replace any existing xattrs.
// Calculate the enabled checksums (not necessarily all known to the plugin).
digests = g_multisuer_oss.m_digests;
}
if (!strncasecmp(Cks.Name, "md5", Cks.NameSize))
{
return_digest = ChecksumManager::MD5;
}
else if (!strncasecmp(Cks.Name, "cksum", Cks.NameSize))
{
return_digest = ChecksumManager::CKSUM;
}
else if (!strncasecmp(Cks.Name, "crc32", Cks.NameSize))
{
return_digest = ChecksumManager::CRC32;
}
else if (!strncasecmp(Cks.Name, "adler32", Cks.NameSize))
{
return_digest = ChecksumManager::ADLER32;
}
else
{
return -ENOTSUP;
}
digests |= return_digest;
ChecksumState state(digests);
// Open the file to read
std::ifstream is (pfn, std::ios::binary | std::ios::in);
if (is.fail()) {
std::stringstream ss;
ss << "Failed to open file: " << pfn << " error: " << strerror(errno);
m_log.Emsg("Calc", ss.str().c_str());
return -errno;
}
const static int buffer_size = 256*1024;
std::vector<char> read_buffer;
read_buffer.resize(buffer_size);
// Read through the file, checksumming as we go
while(!is.eof()) {
is.read(&read_buffer[0], buffer_size);
int bytes_read = is.gcount();
state.Update((unsigned char*)(&read_buffer[0]), bytes_read);
}
is.close();
state.Finalize();
this->Set(lfn, state);
std::string checksum_value;
switch (return_digest)
{
case ChecksumManager::CKSUM:
checksum_value = state.Get(ChecksumManager::CKSUM);
break;
case ChecksumManager::ADLER32:;
checksum_value = state.Get(ChecksumManager::ADLER32);
break;
case ChecksumManager::CRC32:
checksum_value = state.Get(ChecksumManager::CRC32);
break;
case ChecksumManager::MD5:
checksum_value = state.Get(ChecksumManager::MD5);
break;
default:
return -ENOTSUP;
};
if (!checksum_value.size()) return -EIO;
Cks.Set(checksum_value.c_str(), checksum_value.size());
return 0;
}
int ChecksumManager::Calc( const char *Xfn, XrdCksData &Cks, XrdCksPCB *pcbP, int doSet)
{(void)pcbP; return Calc(Xfn, Cks, doSet);}
int ChecksumManager::Del( const char *lfn, XrdCksData &Cks)
{
std::string pfn = this->LFN2PFN(lfn);
std::string checksum_name(Cks.Name);
std::transform(checksum_name.begin(), checksum_name.end(),
checksum_name.begin(), ::toupper);
// Prepend XRDCKS-
checksum_name = ATTR_PREFIX + checksum_name;
XrdSysXAttrActive->Del(checksum_name.c_str(), pfn.c_str());
return XrdCksManager::Del(lfn, Cks);
}
char* ChecksumManager::List(const char *lfn, char *Buff, int Blen, char Sep)
{
return XrdCksManager::List(lfn, Buff, Blen, Sep);
}
int ChecksumManager::Set( const char *lfn, XrdCksData &Cks, int myTime)
{
// Extract the checksum value from the XrdCksData
char buf[512];
Cks.Get(buf, 512);
return this->Set(lfn, Cks.Name, buf);
}
int ChecksumManager::Set(const char *lfn, const char *cksname, const char *chksvalue) {
// Uppercase the name
std::string checksum_name(cksname);
std::transform(checksum_name.begin(), checksum_name.end(),
checksum_name.begin(), ::toupper);
// Prepend XRDCKS-
checksum_name = ATTR_PREFIX + checksum_name;
std::string pfn = this->LFN2PFN(lfn);
// Set the checksum
XrdSysXAttrActive->Set(checksum_name.c_str(),
chksvalue,
strlen(chksvalue),
pfn.c_str());
checksum_name = cksname;
std::transform(checksum_name.begin(), checksum_name.end(),
checksum_name.begin(), ::tolower);
XrdCksData cks;
strcpy(cks.Name, checksum_name.c_str());
cks.Set(chksvalue, strlen(chksvalue));
return XrdCksManager::Set(pfn.c_str(), cks);
}
int ChecksumManager::Ver( const char *lfn, XrdCksData &Cks)
{
return XrdCksManager::Ver(lfn, Cks);
}
int ChecksumManager::Ver( const char *Xfn, XrdCksData &Cks, XrdCksPCB *pcbP)
{(void)pcbP; return Ver(Xfn, Cks);}
int
ChecksumManager::Init(const char * config_fn, const char *default_checksum)
{
if (default_checksum)
{
m_default_digest = default_checksum;
}
return XrdCksManager::Init(config_fn, default_checksum);
}
int
ChecksumManager::Get(const char *lfn, XrdCksData &cks)
{
std::string pfn = this->LFN2PFN(lfn);
return XrdCksManager::Get(pfn.c_str(), cks);
}
std::string ChecksumManager::LFN2PFN(const char* lfn) {
std::string pfn;
char pfnbuff[MAXPATHLEN];
int rc = 0;
const char* pfn_cstr = g_multisuer_oss->Lfn2Pfn(lfn, pfnbuff, MAXPATHLEN, rc);
if (pfn_cstr == 0)
{
std::stringstream ss;
ss << "Failed to translate lfn to pfn for path: " << lfn << " errno: " << rc;
m_log.Emsg("Get", ss.str().c_str());
return pfn;
}
pfn = pfn_cstr;
return pfn;
}