-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClZipArchive.hh
276 lines (237 loc) · 9.39 KB
/
XrdClZipArchive.hh
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
//------------------------------------------------------------------------------
// Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
// Author: Michal Simon <michal.simon@cern.ch>
//------------------------------------------------------------------------------
// This file is part of the XRootD software suite.
//
// 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/>.
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//------------------------------------------------------------------------------
#ifndef SRC_XRDZIP_XRDZIPARCHIVE_HH_
#define SRC_XRDZIP_XRDZIPARCHIVE_HH_
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdCl/XrdClOperations.hh"
#include "XrdCl/XrdClFile.hh"
#include "XrdCl/XrdClResponseJob.hh"
#include "XrdCl/XrdClJobManager.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClPostMaster.hh"
#include "XrdZip/XrdZipEOCD.hh"
#include "XrdZip/XrdZipCDFH.hh"
#include "XrdZip/XrdZipZIP64EOCD.hh"
#include "XrdZip/XrdZipLFH.hh"
#include "XrdCl/XrdClZipCache.hh"
#include <memory>
#include <unordered_map>
//-----------------------------------------------------------------------------
// Forward declaration needed for friendship
//-----------------------------------------------------------------------------
namespace XrdEc{ class StrmWriter; class Reader; };
namespace XrdCl
{
using namespace XrdZip;
class ZipArchive
{
friend class XrdEc::StrmWriter;
friend class XrdEc::Reader;
public:
ZipArchive();
virtual ~ZipArchive();
XRootDStatus OpenArchive( const std::string &url,
OpenFlags::Flags flags,
ResponseHandler *handler,
uint16_t timeout = 0 );
XRootDStatus OpenFile( const std::string &fn,
OpenFlags::Flags flags = OpenFlags::None,
uint64_t size = 0,
uint32_t crc32 = 0 );
inline
XRootDStatus Read( uint64_t offset,
uint32_t size,
void *buffer,
ResponseHandler *handler,
uint16_t timeout = 0 )
{
if( openfn.empty() ) return XRootDStatus( stError, errInvalidOp );
return ReadFrom( openfn, offset, size, buffer, handler, timeout );
}
XRootDStatus ReadFrom( const std::string &fn,
uint64_t offset,
uint32_t size,
void *buffer,
ResponseHandler *handler,
uint16_t timeout = 0 );
XRootDStatus Write( uint32_t size,
const void *buffer,
ResponseHandler *handler,
uint16_t timeout = 0 );
inline XRootDStatus Stat( const std::string &fn, StatInfo *&info )
{ // make sure archive has been opened and CD has been parsed
if( openstage != Done )
return XRootDStatus( stError, errInvalidOp );
// make sure the file is part of the archive
auto cditr = cdmap.find( fn );
if( cditr == cdmap.end() )
return XRootDStatus( stError, errNotFound );
// create the result
info = make_stat( fn );
return XRootDStatus();
}
inline XRootDStatus Stat( StatInfo *&info )
{
if( openfn.empty() )
return XRootDStatus( stError, errInvalidOp );
return Stat( openfn, info );
}
inline XRootDStatus GetCRC32( const std::string &fn, uint32_t &cksum )
{ // make sure archive has been opened and CD has been parsed
if( openstage != Done )
return XRootDStatus( stError, errInvalidOp );
// make sure the file is part of the archive
auto cditr = cdmap.find( fn );
if( cditr == cdmap.end() )
return XRootDStatus( stError, errNotFound );
cksum = cdvec[cditr->second]->ZCRC32;
return XRootDStatus();
}
XRootDStatus CloseArchive( ResponseHandler *handler,
uint16_t timeout = 0 );
inline XRootDStatus CloseFile()
{
if( openstage != Done || openfn.empty() )
return XRootDStatus( stError, errInvalidOp,
errInvalidOp, "Archive not opened." );
openfn.clear();
lfh.reset();
return XRootDStatus();
}
XRootDStatus List( DirectoryList *&list );
private:
XRootDStatus OpenOnly( const std::string &url,
ResponseHandler *handler,
uint16_t timeout = 0 );
buffer_t GetCD();
void SetCD( const buffer_t &buffer );
template<typename Response>
inline static AnyObject* PkgRsp( Response *rsp )
{
if( !rsp ) return nullptr;
AnyObject *pkg = new AnyObject();
pkg->Set( rsp );
return pkg;
}
template<typename Response>
inline static void Free( XRootDStatus *st, Response *rsp )
{
delete st;
delete rsp;
}
inline void Schedule( ResponseHandler *handler, XRootDStatus *st )
{
if( !handler ) return delete st;
JobManager *jobMgr = DefaultEnv::GetPostMaster()->GetJobManager();
if( jobMgr->IsWorker() )
// this is a worker thread so we can simply call the handler
handler->HandleResponse( st, nullptr );
else
{
ResponseJob *job = new ResponseJob( handler, st, nullptr, nullptr );
DefaultEnv::GetPostMaster()->GetJobManager()->QueueJob( job );
}
}
template<typename Response>
inline static void Schedule( ResponseHandler *handler, XRootDStatus *st, Response *rsp )
{
if( !handler ) return Free( st, rsp );
JobManager *jobMgr = DefaultEnv::GetPostMaster()->GetJobManager();
if( jobMgr->IsWorker() )
// this is a worker thread so we can simply call the handler
handler->HandleResponse( st, PkgRsp( rsp ) );
else
{
ResponseJob *job = new ResponseJob( handler, st, PkgRsp( rsp ), 0 );
DefaultEnv::GetPostMaster()->GetJobManager()->QueueJob( job );
}
}
inline static StatInfo* make_stat( const StatInfo &starch, uint64_t size )
{
StatInfo *info = new StatInfo( starch );
uint32_t flags = info->GetFlags();
info->SetFlags( flags & ( ~StatInfo::IsWritable ) ); // make sure it is not listed as writable
info->SetSize( size );
return info;
}
inline StatInfo* make_stat( const std::string &fn )
{
StatInfo *infoptr = 0;
XRootDStatus st = archive.Stat( false, infoptr );
std::unique_ptr<StatInfo> stinfo( infoptr );
auto itr = cdmap.find( fn );
if( itr == cdmap.end() ) return nullptr;
size_t index = itr->second;
return make_stat( *stinfo, cdvec[index]->uncompressedSize );
}
inline static XRootDStatus* make_status( const XRootDStatus &status )
{
return new XRootDStatus( status );
}
inline static XRootDStatus* make_status()
{
return new XRootDStatus();
}
inline void Clear()
{
buffer.reset();
eocd.reset();
cdvec.clear();
cdmap.clear();
zip64eocd.reset();
openstage = None;
}
enum OpenStages
{
None = 0,
HaveEocdBlk,
HaveZip64EocdlBlk,
HaveZip64EocdBlk,
HaveCdRecords,
Done,
Error,
NotParsed
};
typedef std::unordered_map<std::string, ZipCache> zipcache_t;
File archive;
uint64_t archsize;
bool cdexists;
bool updated;
std::unique_ptr<char[]> buffer;
std::unique_ptr<EOCD> eocd;
cdvec_t cdvec;
cdmap_t cdmap;
uint64_t cdoff;
uint32_t orgcdsz; //> original CD size
uint32_t orgcdcnt; //> original number CDFH records
buffer_t orgcdbuf; //> buffer with the original CDFH records
std::unique_ptr<ZIP64_EOCD> zip64eocd;
OpenStages openstage;
std::string openfn;
zipcache_t zipcache;
OpenFlags::Flags flags;
std::unique_ptr<LFH> lfh;
};
} /* namespace XrdZip */
#endif /* SRC_XRDZIP_XRDZIPARCHIVE_HH_ */