Repository navigation
Expand file tree
/
Copy pathXrdOucName2NameXcacheH.cc
More file actions
327 lines (282 loc) · 10.9 KB
/
Copy pathXrdOucName2NameXcacheH.cc
File metadata and controls
327 lines (282 loc) · 10.9 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Author: Wei Yang
* SLAC National Accelerator Laboratory / Stanford University, 2020
*/
using namespace std;
#include <stdio.h>
#include <string>
#include <errno.h>
#include <netdb.h>
#include <openssl/md5.h>
#include "XrdVersion.hh"
XrdVERSIONINFO(XrdOucgetName2Name, "N2N-XcacheH");
#include "XcacheH.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucName2Name.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdSys/XrdSysError.hh"
class XrdOucName2NameXcacheH : public XrdOucName2Name
{
public:
virtual int lfn2pfn(const char* lfn, char* buff, int blen);
virtual int lfn2rfn(const char* lfn, char* buff, int blen);
virtual int pfn2lfn(const char* lfn, char* buff, int blen);
XrdOucName2NameXcacheH(XrdSysError *erp, const char* confg, const char* parms);
virtual ~XrdOucName2NameXcacheH() {};
friend XrdOucName2Name *XrdOucgetName2Name(XrdOucgetName2NameArgs);
private:
string myName;
struct cacheOptions cacheOpts;
XrdSysError *eDest;
bool isCmsd;
};
XrdOucName2NameXcacheH::XrdOucName2NameXcacheH(XrdSysError* erp, const char* confg, const char* parms)
{
std::string myProg;
std::string opts, message, key, value;
std::string::iterator it;
std::size_t i;
int unit, x;
char unitName;
char *hostName;
myName = "XcacheH";
eDest = erp;
isCmsd = false;
if (getenv("XRDPROG"))
{
myProg = getenv("XRDPROG");
if (myProg == "cmsd") isCmsd = true;
}
setenv("XRD_METALINKPROCESSING", "1", 0);
setenv("XRD_LOCALMETALINKFILE", "1", 0);
x = 0;
key = "";
value = "";
// the default
cacheOpts.lifeT = 3600;
cacheOpts.blockSize = 1048576;
cacheOpts.xrdPort = std::stoi(getenv("XRDPORT"));
hostName = (char*)malloc(256);
gethostname(hostName, 256);
struct hostent *myHostEnt = gethostbyname(hostName);
cacheOpts.hostName = myHostEnt->h_name;
free(hostName);
opts = parms;
opts += " ";
for (it=opts.begin(); it!=opts.end(); ++it)
{
if (*it == '=') x = 1;
else if (*it == ' ')
{
if (key == "cacheLife") // unit: s/S (default), m/M, h/H, d/D,
{
unit = 1;
if (value.find_first_not_of("0123456789.") != std::string::npos)
{
unitName = value.substr(value.find_first_not_of("0123456789."), 1).c_str()[0];
if (unitName == 's' || unitName == 'S')
unit = 1;
else if (unitName == 'm' || unitName == 'M')
unit = 60;
else if (unitName == 'h' || unitName == 'H')
unit = 3600;
else if (unitName == 'd' || unitName == 'D')
unit = 86400;
else
{
message = myName + " Init: option cacheLife = "
+ value
+ " is invalid";
eDest->Say(message.c_str());
}
value.replace(value.find_first_not_of("0123456789."),
value.length() - value.find_first_not_of("0123456789."),
"");
}
if (value.find_first_not_of("0123456789.") == std::string::npos)
{
cacheOpts.lifeT = atoi(value.c_str()) * unit;
}
else
{
message = myName + " Init: option cacheLife = "
+ value
+ " is invalid. Using default ("
+ std::to_string(cacheOpts.lifeT)
+ " seconds)";
eDest->Say(message.c_str());
}
}
else if (key == "cacheBlockSize") // unit: b/B (default), k/K, m/M
{
unit = 1;
if (value.find_first_not_of("0123456789.") != std::string::npos)
{
unitName = value.substr(value.find_first_not_of("0123456789."), 1).c_str()[0];
if (unitName == 'b' || unitName == 'B')
unit = 1;
else if (unitName == 'k' || unitName == 'K')
unit = 1024;
else if (unitName == 'm' || unitName == 'M')
unit = 1048576;
else
{
message = myName + " Init: option cacheBlockSize = "
+ value
+ " is invalid";
eDest->Say(message.c_str());
}
value.replace(value.find_first_not_of("0123456789."),
value.length() - value.find_first_not_of("0123456789."),
"");
}
if (value.find_first_not_of("0123456789.") == std::string::npos)
{
cacheOpts.blockSize = atoi(value.c_str()) * unit;
}
else
{
message = myName + " Init: option cacheBlockSize = "
+ value
+ " is invalid. Using default ("
+ std::to_string(cacheOpts.blockSize)
+ " bytes)";
eDest->Say(message.c_str());
}
}
else if (key == "xrdPort")
{
if (value.find_first_not_of("0123456789.") == std::string::npos)
{
cacheOpts.xrdPort = atoi(value.c_str());
}
else
{
message = myName + " Init: option xrdPort = "
+ value
+ " is invalid";
eDest->Say(message.c_str());
}
}
else if (key == "hostName") // my hostName to be used by sparse stagein request
{
cacheOpts.hostName = value;
}
key = "";
value = "";
x = 0;
}
else
{
if (x == 0) key += *it;
if (x == 1) value += *it;
}
}
message = myName + " Init: effective option cacheLife = " + std::to_string(cacheOpts.lifeT);
eDest->Say(message.c_str());
message = myName + " Init: effective option cacheBlockSize = " + std::to_string(cacheOpts.blockSize);
eDest->Say(message.c_str());
message = myName + " Init: effective option hostName:xrdPort = " + cacheOpts.hostName
+ ":"
+ std::to_string(cacheOpts.xrdPort);
eDest->Say(message.c_str());
XcacheHInit(eDest, myName, &cacheOpts);
}
int XrdOucName2NameXcacheH::lfn2pfn(const char* lfn, char* buff, int blen)
{ return -EOPNOTSUPP; }
// when "pss.namelib -lfncachesrc ..." is used, pfn will look like:
// /images/junk1?src=http://u25@wt2.slac.stanford.edu/
// when "pss.namelib -lfncachesrc+ ..." is used, pfn will look like:
// /images/junk1?src=http://u25@wt2.slac.stanford.edu&
// /images/junk1?src=http://u25@wt2.slac.stanford.edu&mycgi=hello&his=none
int XrdOucName2NameXcacheH::pfn2lfn(const char* pfn, char* buff, int blen)
{
std::string myLfn, myPfn, myUrl;
myPfn = myUrl = pfn;
if (isCmsd) // cmsd shouldn't do pfn2lfn()
{
blen = myPfn.find("?src=");
blen = strlen(pfn);
strncpy(buff, pfn, blen);
return 0;
}
std::string myPath, myProt, myHostPort, myCGI;
// it is important to use string::rfind() to search from the end. <-- why?
myPath = myUrl.substr(0, myUrl.find("?src="));
myUrl.replace(0, myPath.length() +5, "");
// sometime the myPath doesn't start with a / (e.g. if the incoming is via the root protocol)
if (myPath.c_str()[0] != '/')
myPath = "/" + myPath;
myProt = myUrl.substr(0, myUrl.find("://") +3);
myUrl.replace(0, myProt.length(), "");
// found a @ before the first '/' and '&'
if (myUrl.find("@") != std::string::npos &&
(myUrl.find("@") < myUrl.find("/") || myUrl.find("@") < myUrl.find("&")))
{
myUrl.replace(0, myUrl.find("@") +1, "");
}
if (myUrl.find("&") != std::string::npos) // test '&'
myHostPort = myUrl.substr(0, myUrl.find("&"));
else // no CGI
myHostPort = myUrl;
myUrl.replace(0, myHostPort.length(), "");
// remove trailing "/"
if (myHostPort.find("/") == (myHostPort.length() -1))
myHostPort.replace(myHostPort.length() -1, 1, "");
myCGI = myUrl; // note this CGI, if not empty, starts with a "&"
std::string stageinToken = "xcachestagein";;
int stageinRequest = 0;
if (myCGI.find(stageinToken + "=") != std::string::npos) // This is a stage in request
{
stageinRequest = 1;
myCGI.replace(myCGI.find(stageinToken + "="), stageinToken.length() +1, "");
}
else if (myCGI.find(stageinToken) != std::string::npos) // This is a stage in request
{
stageinRequest = 1;
myCGI.replace(myCGI.find(stageinToken), stageinToken.length(), "");
}
// trim the trailing ? or &
while ( myCGI.length() != 0 &&
((myCGI.rfind("?") == (myCGI.length() -1)) ||
(myCGI.rfind("&") == (myCGI.length() -1))) )
myCGI.replace(myCGI.length() -1, 1, "");
// trim ?& to ?
while ( myCGI.length() != 0 && myCGI.rfind("?&") != std::string::npos )
myCGI.replace(myCGI.rfind("?&") +1, 1, "");
// trim ?& to &
while ( myCGI.length() != 0 && myCGI.rfind("&&") != std::string::npos )
myCGI.replace(myCGI.rfind("&&") +1, 1, "");
if (myCGI.length() == 0)
myUrl = myProt + myHostPort + myPath;
else
myUrl = myProt + myHostPort + myPath + myCGI.replace(0, 1, "?");
// this scenarios should NOT happen
if (myUrl.find("http://") != 0 && myUrl.find("https://") != 0)
{
blen = 0;
buff[0] = 0;
return EINVAL; // see XrdOucName2Name.hh
}
myLfn = XcacheHCheckFile(myUrl, stageinRequest);
if (myLfn == "EFAULT")
return EFAULT;
else if (myLfn == "ENOENT")
return ENOENT;
else if (myLfn == "EALREADY")
return EALREADY;
blen = myLfn.length();
strncpy(buff, myLfn.c_str(), blen);
buff[blen] = 0;
return 0;
}
int XrdOucName2NameXcacheH::lfn2rfn(const char* lfn, char* buff, int blen)
{ return -EOPNOTSUPP; }
XrdOucName2Name *XrdOucgetName2Name(XrdOucgetName2NameArgs)
{
static XrdOucName2NameXcacheH *inst = NULL;
if (inst) return (XrdOucName2Name *)inst;
inst = new XrdOucName2NameXcacheH(eDest, confg, parms);
if (!inst) return NULL;
return (XrdOucName2Name *)inst;
}