XRootD
Loading...
Searching...
No Matches
XrdCksManOss.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d C k s M a n O s s . c c */
4/* */
5/* (c) 2014 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* All Rights Reserved */
7/* Produced by Andrew Hanushevsky for Stanford University under contract */
8/* DE-AC02-76-SFO0515 with the Department of Energy */
9/* */
10/* This file is part of the XRootD software suite. */
11/* */
12/* XRootD is free software: you can redistribute it and/or modify it under */
13/* the terms of the GNU Lesser General Public License as published by the */
14/* Free Software Foundation, either version 3 of the License, or (at your */
15/* option) any later version. */
16/* */
17/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20/* License for more details. */
21/* */
22/* You should have received a copy of the GNU Lesser General Public License */
23/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25/* */
26/* The copyright holder's institutional names and contributor's names may not */
27/* be used to endorse or promote products derived from this software without */
28/* specific prior written permission of the institution or contributor. */
29/******************************************************************************/
30
31#include <cerrno>
32#include <fcntl.h>
33#include <cstring>
34#include <ctime>
35#include <cstdio>
36#include <unistd.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <sys/param.h>
40
41#include "XrdCks/XrdCksCalc.hh"
43#include "XrdOss/XrdOss.hh"
44#include "XrdOuc/XrdOucEnv.hh"
45#include "XrdSys/XrdSysError.hh"
47
48/******************************************************************************/
49/* L o c a l S t a t i c s */
50/******************************************************************************/
51
52namespace
53{
54XrdOss *ossP = 0;
55int rdSz = 67108864;
56}
57
58/******************************************************************************/
59/* L o c a l C l a s s e s */
60/******************************************************************************/
61
62class LfnPfn
63 {public:
64 const char *Lfn;
65 char Pfn[MAXPATHLEN+8];
66
67 LfnPfn(const char *lfn, int &rc) : Lfn(lfn)
68 {rc = ossP->Lfn2Pfn(lfn, Pfn, MAXPATHLEN);
69 if (rc > 0) rc = -rc;
70 }
72 };
73
74namespace
75{
76const char *Pfn2Lfn(const char *Lfn)
77 {LfnPfn *Xfn = (LfnPfn *)(Lfn-sizeof(const char *));
78 return Xfn->Lfn;
79 }
80}
81
82/******************************************************************************/
83/* C o n s t r u c t o r */
84/******************************************************************************/
85
87 XrdVersionInfo &vInfo, bool autoload)
88 : XrdCksManager(erP, rdSz, vInfo, autoload)
89 {if (rdSz <= 65536) rdSz = 67108864;
90 else rdSz = ((rdSz/65536) + (rdSz%65536 != 0)) * 65536;
91 eDest = erP;
92 ossP = ossX;
93 }
94
95/******************************************************************************/
96/* C a l c */
97/******************************************************************************/
98
99int XrdCksManOss::Calc(const char *Lfn, XrdCksData &Cks, int doSet)
100{
101 int rc;
102 LfnPfn Xfn(Lfn, rc);
103
104// If lfn conversion failed, bail out
105//
106 if (rc) return rc;
107
108// Return the result
109//
110 return XrdCksManager::Calc(Xfn.Pfn, Cks, doSet);
111}
112
113/******************************************************************************/
114
115int XrdCksManOss::Calc(const char *Pfn, time_t &MTime, XrdCksCalc *csP)
116{
117 class inFile
118 {public:
119 XrdOssDF *fP;
120 inFile() {fP = ossP->newFile("ckscalc");}
121 ~inFile() {if (fP) delete fP;}
122 } In;
123 XrdOucEnv openEnv;
124 const char *Lfn = Pfn2Lfn(Pfn);
125 struct stat Stat;
126 char *buffP;
127 off_t Offset=0, fileSize;
128 size_t ioSize, calcSize;
129 int rc;
130
131// Open the input file
132//
133 if ((rc = In.fP->Open(Lfn,O_RDONLY,0,openEnv))) return (rc > 0 ? -rc : rc);
134
135// Get the file characteristics
136//
137 if ((rc = In.fP->Fstat(&Stat))) return (rc > 0 ? -rc : rc);
138 if (!(Stat.st_mode & S_IFREG)) return -EPERM;
139 calcSize = fileSize = Stat.st_size;
140 MTime = Stat.st_mtime;
141
142// Compute read size and allocate a buffer
143//
144 ioSize = (fileSize < (off_t)rdSz ? fileSize : rdSz); rc = 0;
145 buffP = (char *)malloc(ioSize);
146 if (!buffP) return -ENOMEM;
147
148// We now compute checksum 64MB at a time using mmap I/O
149//
150 while(calcSize)
151 {if ((rc= In.fP->Read(buffP, Offset, ioSize)) < 0) break;
152 csP->Update(buffP, ioSize);
153 calcSize -= ioSize; Offset += ioSize;
154 if (calcSize < (size_t)ioSize) ioSize = calcSize;
155 }
156 free(buffP);
157
158// Issue error message if we have an error
159//
160 if (rc < 0) eDest->Emsg("Cks", rc, "read", Pfn);
161
162// Return
163//
164 return (rc < 0 ? rc : 0);
165}
166
167/******************************************************************************/
168/* D e l */
169/******************************************************************************/
170
171int XrdCksManOss::Del(const char *Lfn, XrdCksData &Cks)
172{
173 int rc;
174 LfnPfn Xfn(Lfn, rc);
175
176// If lfn conversion failed, bail out
177//
178 if (rc) return rc;
179
180// Delete the attribute and return the result
181//
182 return XrdCksManager::Del(Xfn.Pfn, Cks);
183}
184
185/******************************************************************************/
186/* G e t */
187/******************************************************************************/
188
189int XrdCksManOss::Get(const char *Lfn, XrdCksData &Cks)
190{
191 int rc;
192 LfnPfn Xfn(Lfn, rc);
193
194// If lfn conversion failed, bail out
195//
196 if (rc) return rc;
197
198// Return result
199//
200 return XrdCksManager::Get(Xfn.Pfn, Cks);
201}
202
203/******************************************************************************/
204/* L i s t */
205/******************************************************************************/
206
207char *XrdCksManOss::List(const char *Lfn, char *Buff, int Blen, char Sep)
208{
209 int rc;
210 LfnPfn Xfn(Lfn, rc);
211
212// If lfn conversion failed, bail out
213//
214 if (rc) return 0;
215
216// Simply invoke the base class list
217//
218 return XrdCksManager::List(Xfn.Pfn, Buff, Blen,Sep);
219}
220
221/******************************************************************************/
222/* M o d T i m e */
223/******************************************************************************/
224
225int XrdCksManOss::ModTime(const char *Pfn, time_t &MTime)
226{
227 const char *Lfn = Pfn2Lfn(Pfn);
228 struct stat Stat;
229 int rc;
230
231 if (!(rc = ossP->Stat(Lfn, &Stat))) MTime = Stat.st_mtime;
232
233 return (rc > 0 ? -rc : 0);
234}
235
236/******************************************************************************/
237/* S e t */
238/******************************************************************************/
239
240int XrdCksManOss::Set(const char *Lfn, XrdCksData &Cks, int myTime)
241{
242 int rc;
243 LfnPfn Xfn(Lfn, rc);
244
245// If lfn conversion failed, bail out
246//
247 if (rc) return rc;
248
249// Now set the checksum information in the extended attribute object
250//
251 return XrdCksManager::Set(Xfn.Pfn, Cks, myTime);
252}
253
254/******************************************************************************/
255/* V e r */
256/******************************************************************************/
257
258int XrdCksManOss::Ver(const char *Lfn, XrdCksData &Cks)
259{
260 int rc;
261 LfnPfn Xfn(Lfn, rc);
262
263// If lfn conversion failed, bail out
264//
265 if (rc) return rc;
266
267// Return result invoking the base class
268//
269 return XrdCksManager::Ver(Lfn, Cks);
270}
struct stat Stat
Definition XrdCks.cc:49
#define stat(a, b)
Definition XrdPosix.hh:96
const char * Lfn
LfnPfn(const char *lfn, int &rc)
char Pfn[MAXPATHLEN+8]
virtual void Update(const char *Buff, int BLen)=0
virtual int Ver(const char *Lfn, XrdCksData &Cks)
virtual int Del(const char *Lfn, XrdCksData &Cks)
virtual char * List(const char *Lfn, char *Buff, int Blen, char Sep=' ')
virtual int Get(const char *Lfn, XrdCksData &Cks)
XrdCksManOss(XrdOss *ossX, XrdSysError *erP, int iosz, XrdVersionInfo &vInfo, bool autoload=false)
virtual int Set(const char *Lfn, XrdCksData &Cks, int myTime=0)
virtual int ModTime(const char *Pfn, time_t &MTime)
virtual int Calc(const char *Lfn, XrdCksData &Cks, int doSet=1)
virtual int Get(const char *Pfn, XrdCksData &Cks)
virtual int Del(const char *Pfn, XrdCksData &Cks)
XrdCksManager(XrdSysError *erP, int iosz, XrdVersionInfo &vInfo, bool autoload=false)
virtual int Calc(const char *Pfn, XrdCksData &Cks, int doSet=1)
virtual char * List(const char *Pfn, char *Buff, int Blen, char Sep=' ')
virtual int Ver(const char *Pfn, XrdCksData &Cks)
virtual int Set(const char *Pfn, XrdCksData &Cks, int myTime=0)
XrdSysError * eDest
Definition XrdCks.hh:289
virtual XrdOssDF * newFile(const char *tident)=0
virtual int Lfn2Pfn(const char *Path, char *buff, int blen)
Definition XrdOss.hh:873
virtual int Stat(const char *path, struct stat *buff, int opts=0, XrdOucEnv *envP=0)=0