-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdFileCacheIOEntireFile.cc
208 lines (167 loc) · 5.33 KB
/
XrdFileCacheIOEntireFile.cc
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
//----------------------------------------------------------------------------------
// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
// Author: Alja Mrak-Tadel, Matevz Tadel, Brian Bockelman
//----------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//----------------------------------------------------------------------------------
#include <stdio.h>
#include <fcntl.h>
#include "XrdSys/XrdSysError.hh"
#include "XrdSfs/XrdSfsInterface.hh"
#include "XrdSys/XrdSysPthread.hh"
#include "XrdFileCacheIOEntireFile.hh"
#include "XrdFileCacheStats.hh"
#include "XrdFileCacheTrace.hh"
#include "XrdOuc/XrdOucEnv.hh"
using namespace XrdFileCache;
//______________________________________________________________________________
IOEntireFile::IOEntireFile(XrdOucCacheIO2 *io, XrdOucCacheStats &stats, Cache & cache)
: IO(io, stats, cache),
m_file(0),
m_localStat(0)
{
XrdCl::URL url(m_io->Path());
std::string fname = Cache::GetInstance().RefConfiguration().m_cache_dir + url.GetPath();
if ((m_file = Cache::GetInstance().GetFileWithLocalPath(fname, this)))
{
m_file->WakeUp();
}
else {
struct stat st;
int res = Fstat(st);
// this should not happen, but make a printout to see it
if (res)
TRACEIO(Error, "IOEntireFile::IOEntireFile, could not get valid stat");
m_file = new File(io, fname, 0, st.st_size);
}
Cache::GetInstance().AddActive(this, m_file);
}
IOEntireFile::~IOEntireFile()
{
delete m_localStat;
}
int IOEntireFile::Fstat(struct stat &sbuff)
{
XrdCl::URL url(m_io->Path());
std::string name = url.GetPath();
name += ".cinfo";
int res = 0;
if(!m_localStat) {
res = initCachedStat(name.c_str());
if (res) return res;
}
memcpy(&sbuff, m_localStat, sizeof(struct stat));
return 0;
}
long long IOEntireFile::FSize()
{
return m_file->GetFileSize();
}
void IOEntireFile::RelinquishFile(File* f)
{
TRACEIO(Info, "IOEntireFile::RelinquishFile");
assert(m_file == f);
m_file = 0;
}
int IOEntireFile::initCachedStat(const char* path)
{
// called indirectly from this constructor first time
int res = -1;
struct stat tmpStat;
if (m_cache.GetOss()->Stat(path, &tmpStat) == XrdOssOK) {
XrdOssDF* infoFile = m_cache.GetOss()->newFile(Cache::GetInstance().RefConfiguration().m_username.c_str());
XrdOucEnv myEnv;
if (infoFile->Open(path, O_RDONLY, 0600, myEnv) > 0) {
Info info(m_cache.GetTrace());
printf("reading info file ..\n");
if (info.Read(infoFile) > 0) {
tmpStat.st_size = info.GetFileSize();
TRACEIO(Info, "IOEntireFile::initCachedStat successfuly read size from info file = " << tmpStat.st_size);
res = 0;
}
else {
// file exist but can't read it
TRACEIO(Error, "IOEntireFile::initCachedStat failed to read file size from info file");
}
}
infoFile->Close();
delete infoFile;
}
if (res) {
res = m_io->Fstat(tmpStat);
TRACEIO(Debug, "IOEntireFile::initCachedStat get stat from client res= " << res << "size = " << tmpStat.st_size);
}
if (res == 0)
{
m_localStat = new struct stat;
memcpy(m_localStat, &tmpStat, sizeof(struct stat));
}
return res;
}
bool IOEntireFile::ioActive()
{
if ( ! m_file)
return false;
else
return m_file->ioActive();
}
XrdOucCacheIO *IOEntireFile::Detach()
{
XrdOucCacheIO * io = m_io;
// This will delete us!
m_cache.Detach(this);
return io;
}
void IOEntireFile::Read (XrdOucCacheIOCB &iocb, char *buff, long long offs, int rlen)
{
iocb.Done(IOEntireFile::Read(buff, offs, rlen));
}
int IOEntireFile::Read (char *buff, long long off, int size)
{
TRACEIO(Dump, "IOEntireFile::Read() "<< this << " off: " << off << " size: " << size );
// protect from reads over the file size
if (off >= FSize())
return 0;
if (off < 0)
{
errno = EINVAL;
return -1;
}
if (off + size > FSize())
size = FSize() - off;
ssize_t bytes_read = 0;
ssize_t retval = 0;
retval = m_file->Read(buff, off, size);
if (retval >= 0)
{
bytes_read += retval;
buff += retval;
size -= retval;
if (size > 0)
TRACEIO(Warning, "IOEntireFile::Read() bytes missed " << size );
}
else
{
TRACEIO(Warning, "IOEntireFile::Read() pass to origin bytes ret " << retval );
}
return (retval < 0) ? retval : bytes_read;
}
/*
* Perform a readv from the cache
*/
int IOEntireFile::ReadV (const XrdOucIOVec *readV, int n)
{
TRACE(Dump, "IO::ReadV(), get " << n << " requests, " << m_io->Path());
return m_file->ReadV(readV, n);
}