-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdEcReader.cc
666 lines (629 loc) · 29.4 KB
/
XrdEcReader.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//------------------------------------------------------------------------------
// 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.
//------------------------------------------------------------------------------
#include "XrdEc/XrdEcReader.hh"
#include "XrdEc/XrdEcUtilities.hh"
#include "XrdEc/XrdEcConfig.hh"
#include "XrdEc/XrdEcObjCfg.hh"
#include "XrdZip/XrdZipLFH.hh"
#include "XrdZip/XrdZipCDFH.hh"
#include "XrdZip/XrdZipUtils.hh"
#include "XrdOuc/XrdOucCRC32C.hh"
#include "XrdCl/XrdClParallelOperation.hh"
#include "XrdCl/XrdClZipOperations.hh"
#include "XrdCl/XrdClFileOperations.hh"
#include <algorithm>
#include <iterator>
#include <numeric>
#include <tuple>
namespace XrdEc
{
//---------------------------------------------------------------------------
// OpenOnly operation (@see ZipOperation) - a private ZIP operation
//---------------------------------------------------------------------------
template<bool HasHndl>
class OpenOnlyImpl: public XrdCl::ZipOperation<OpenOnlyImpl, HasHndl,
XrdCl::Resp<void>, XrdCl::Arg<std::string>>
{
public:
//-----------------------------------------------------------------------
// Inherit constructors from FileOperation (@see FileOperation)
//-----------------------------------------------------------------------
using XrdCl::ZipOperation<OpenOnlyImpl, HasHndl, XrdCl::Resp<void>,
XrdCl::Arg<std::string>>::ZipOperation;
//-----------------------------------------------------------------------
// Argument indexes in the args tuple
//-----------------------------------------------------------------------
enum { UrlArg };
//-----------------------------------------------------------------------
// @return : name of the operation (@see Operation)
//-----------------------------------------------------------------------
std::string ToString()
{
return "OpenOnly";
}
protected:
//-----------------------------------------------------------------------
// RunImpl operation (@see Operation)
//
// @param params : container with parameters forwarded from
// previous operation
// @return : status of the operation
//-----------------------------------------------------------------------
XrdCl::XRootDStatus RunImpl( XrdCl::PipelineHandler *handler,
uint16_t pipelineTimeout )
{
std::string url = std::get<UrlArg>( this->args ).Get();
uint16_t timeout = pipelineTimeout < this->timeout ?
pipelineTimeout : this->timeout;
return this->zip->OpenOnly( url, handler, timeout );
}
};
//---------------------------------------------------------------------------
// Factory for creating OpenArchiveImpl objects
//---------------------------------------------------------------------------
inline OpenOnlyImpl<false> OpenOnly( XrdCl::Ctx<XrdCl::ZipArchive> zip,
XrdCl::Arg<std::string> fn,
uint16_t timeout = 0 )
{
return OpenOnlyImpl<false>( std::move( zip ), std::move( fn ) ).Timeout( timeout );
}
//-------------------------------------------------------------------------
// A single data block
//-------------------------------------------------------------------------
struct block_t
{
typedef std::tuple<uint64_t, uint32_t, char*, callback_t> args_t;
typedef std::vector<args_t> pending_t;
//-----------------------------------------------------------------------
// Stripe state: empty / loading / valid
//-----------------------------------------------------------------------
enum state_t { Empty = 0, Loading, Valid, Missing, Recovering };
//-----------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------
block_t( size_t blkid, Reader &reader, ObjCfg &objcfg ) : reader( reader ),
objcfg( objcfg ),
stripes( objcfg.nbchunks ),
state( objcfg.nbchunks, Empty ),
pending( objcfg.nbchunks ),
blkid( blkid ),
recovering( 0 )
{
}
//-----------------------------------------------------------------------
// Read data from stripe
//
// @param strpid : stripe ID
// @param offset : relative offset within the stripe
// @param size : number of bytes to be read from the stripe
// @param usrbuff : user buffer for the data
// @param usrcb : user callback to be notified when the read operation
// has been resolved
//-----------------------------------------------------------------------
void read( size_t strpid,
uint64_t offset,
uint32_t size,
char *usrbuff,
callback_t usrcb )
{
std::unique_lock<std::mutex> lck( mtx );
//---------------------------------------------------------------------
// The cache is empty, we need to load the data
//---------------------------------------------------------------------
if( state[strpid] == Empty )
{
reader.Read( blkid, strpid, stripes[strpid], read_callback( strpid ) );
state[strpid] = Loading;
}
//---------------------------------------------------------------------
// The stripe is either corrupted or unreachable
//---------------------------------------------------------------------
if( state[strpid] == Missing )
{
if( !error_correction() )
{
//-----------------------------------------------------------------
// Recovery was not possible, notify the user of the error
//-----------------------------------------------------------------
usrcb( XrdCl::XRootDStatus( XrdCl::stError, XrdCl::errDataError ), 0 );
return;
}
//-------------------------------------------------------------------
// we fall through to the following if-statements that will handle
// Recovering / Valid state
//-------------------------------------------------------------------
}
//---------------------------------------------------------------------
// The cache is loading or recovering, we don't have the data yet
//---------------------------------------------------------------------
if( state[strpid] == Loading || state[strpid] == Recovering )
{
pending[strpid].emplace_back( offset, size, usrbuff, usrcb );
return;
}
//---------------------------------------------------------------------
// We do have the data so we can serve the user right away
//---------------------------------------------------------------------
if( state[strpid] == Valid )
{
if( offset + size > stripes[strpid].size() )
size = stripes[strpid].size() - offset;
memcpy( usrbuff, stripes[strpid].data() + offset, size );
usrcb( XrdCl::XRootDStatus(), size );
return;
}
//---------------------------------------------------------------------
// In principle we should never end up here, nevertheless if this
// happens it is clearly an error ...
//---------------------------------------------------------------------
usrcb( XrdCl::XRootDStatus( XrdCl::stError, XrdCl::errInvalidOp ), 0 );
}
//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
bool error_correction()
{
//---------------------------------------------------------------------
// Do the accounting for our stripes
//---------------------------------------------------------------------
size_t missingcnt = 0, validcnt = 0, loadingcnt = 0, recoveringcnt = 0;
std::for_each( state.begin(), state.end(), [&]( state_t &s )
{
switch( s )
{
case Missing: ++missingcnt; break;
case Valid: ++validcnt; break;
case Loading: ++loadingcnt; break;
case Recovering: ++recoveringcnt; break;
default: ;
}
} );
//---------------------------------------------------------------------
// If there are no missing stripes all is good ...
//---------------------------------------------------------------------
if( missingcnt + recoveringcnt == 0 ) return true;
//---------------------------------------------------------------------
// Check if we can do the recovery at all (if too many stripes are
// missing it wont be possible)
//---------------------------------------------------------------------
if( missingcnt + recoveringcnt > objcfg.nbparity ) return false;
//---------------------------------------------------------------------
// Check if we can do the recovery right away
//---------------------------------------------------------------------
if( validcnt >= objcfg.nbdata )
{
Config &cfg = Config::Instance();
stripes_t strps( get_stripes() );
try
{
cfg.GetRedundancy( objcfg ).compute( strps );
}
catch( const IOError &ex )
{
return false;
}
//-------------------------------------------------------------------
// Now when we recovered the data we need to mark every stripe as
// valid and execute the pending reads
//-------------------------------------------------------------------
for( size_t strpid = 0; strpid < objcfg.nbchunks; ++strpid )
{
if( state[strpid] != Recovering ) continue;
state[strpid] = Valid;
carryout( pending[strpid], stripes[strpid] );
}
return true;
}
//---------------------------------------------------------------------
// Try loading the data and only then attempt recovery
//---------------------------------------------------------------------
size_t i = 0;
while( loadingcnt + validcnt < objcfg.nbdata && i < objcfg.nbchunks )
{
size_t strpid = i++;
if( state[strpid] != Empty ) continue;
reader.Read( blkid, strpid, stripes[strpid], read_callback( strpid ) );
state[strpid] = Loading;
++loadingcnt;
}
//-------------------------------------------------------------------
// Now that we triggered the recovery procedure mark every missing
// stripe as recovering.
//-------------------------------------------------------------------
std::for_each( state.begin(), state.end(),
[]( state_t &s ){ if( s == Missing ) s = Recovering; } );
return true;
}
//-----------------------------------------------------------------------
// Get a callback for read operation
//-----------------------------------------------------------------------
inline
callback_t read_callback( size_t strpid )
{
return [=]( const XrdCl::XRootDStatus &st, uint32_t )
{
std::unique_lock<std::mutex> lck( mtx );
state[strpid] = st.IsOK() ? Valid : Missing;
//------------------------------------------------------------
// Check if we need to do any error correction (either for
// the current stripe, or any other stripe)
//------------------------------------------------------------
bool recoverable = error_correction();
//------------------------------------------------------------
// Carry out the pending read requests if we got the data or
// if there was an error and we cannot recover
//------------------------------------------------------------
if( st.IsOK() || !recoverable )
carryout( pending[strpid], stripes[strpid], st );
};
}
//-----------------------------------------------------------------------
// Get stripes_t data structure used for error recovery
//-----------------------------------------------------------------------
inline stripes_t get_stripes()
{
stripes_t ret;
ret.reserve( objcfg.nbchunks );
for( size_t i = 0; i < objcfg.nbchunks; ++i )
{
if( state[i] == Valid )
ret.emplace_back( stripes[i].data(), true );
else
{
stripes[i].resize( objcfg.chunksize, 0 );
ret.emplace_back( stripes[i].data(), false );
}
}
return ret;
}
//-----------------------------------------------------------------------
// Execute the pending read requests
//-----------------------------------------------------------------------
inline static
void carryout( pending_t &pending,
const buffer_t &stripe,
const XrdCl::XRootDStatus &st = XrdCl::XRootDStatus() )
{
//---------------------------------------------------------------------
// Iterate over all pending read operations for given stripe
//---------------------------------------------------------------------
auto itr = pending.begin();
for( ; itr != pending.end() ; ++itr )
{
auto &args = *itr;
callback_t &callback = std::get<3>( args );
uint32_t nbrd = 0; // number of bytes read
//-------------------------------------------------------------------
// If the read was successful, copy the data to user buffer
//-------------------------------------------------------------------
if( st.IsOK() )
{
uint64_t offset = std::get<0>( args );
uint32_t size = std::get<1>( args );
char *usrbuff = std::get<2>( args );
if( offset + size > stripe.size() )
size = stripe.size() - offset;
memcpy( usrbuff, stripe.data() + offset, size );
nbrd = size;
}
//-------------------------------------------------------------------
// Call the user callback
//-------------------------------------------------------------------
callback( st, nbrd );
}
//---------------------------------------------------------------------
// Now we can clear the pending reads
//---------------------------------------------------------------------
pending.clear();
}
Reader &reader;
ObjCfg &objcfg;
std::vector<buffer_t> stripes; //< data buffer for every stripe
std::vector<state_t> state; //< state of every data buffer (empty/loading/valid)
std::vector<pending_t> pending; //< pending reads per stripe
size_t blkid; //< block ID
bool recovering; //< true if we are in the process of recovering data, false otherwise
std::mutex mtx;
};
//---------------------------------------------------------------------------
// Destructor (we need it in the source file because block_t is defined in
// here)
//---------------------------------------------------------------------------
Reader::~Reader()
{
}
//---------------------------------------------------------------------------
// Open the erasure coded / striped object
//---------------------------------------------------------------------------
void Reader::Open( XrdCl::ResponseHandler *handler )
{
const size_t size = objcfg.plgr.size();
std::vector<XrdCl::Pipeline> opens; opens.reserve( size );
for( size_t i = 0; i < size; ++i )
{
// generate the URL
std::string url = objcfg.GetDataUrl( i );
// create the file object
dataarchs.emplace( url, std::make_shared<XrdCl::ZipArchive>() );
// open the archive
opens.emplace_back( OpenOnly( *dataarchs[url], url ) );
}
// in parallel open the data files and read the metadata
XrdCl::Pipeline p = XrdCl::Parallel( ReadMetadata( 0 ), XrdCl::Parallel( opens ) ) >>
[=]( XrdCl::XRootDStatus &st )
{ // set the central directories in ZIP archives
auto itr = dataarchs.begin();
for( ; itr != dataarchs.end() ; ++itr )
{
const std::string &url = itr->first;
auto &zipptr = itr->second;
if( zipptr->openstage == XrdCl::ZipArchive::NotParsed )
zipptr->SetCD( metadata[url] );
auto itr = zipptr->cdmap.begin();
for( ; itr != zipptr->cdmap.end() ; ++itr )
urlmap.emplace( itr->first, url );
}
metadata.clear();
// call user handler
if( handler )
handler->HandleResponse( new XrdCl::XRootDStatus( st ), nullptr );
};
XrdCl::Async( std::move( p ) );
}
//-----------------------------------------------------------------------
// Read data from the data object
//-----------------------------------------------------------------------
void Reader::Read( uint64_t offset,
uint32_t length,
void *buffer,
XrdCl::ResponseHandler *handler )
{
char *usrbuff = reinterpret_cast<char*>( buffer );
typedef std::tuple<uint64_t, uint32_t, void*, uint32_t, XrdCl::ResponseHandler*> rdctx_t;
auto rdctx = std::make_shared<rdctx_t>( offset, 0, buffer, length, handler );
auto rdmtx = std::make_shared<std::mutex>();
while( length > 0 )
{
size_t blkid = offset / objcfg.datasize; //< ID of the block from which we will be reading
size_t strpid = ( offset % objcfg.datasize ) / objcfg.chunksize; //< ID of the stripe from which we will be reading
uint64_t rdoff = offset - blkid * objcfg.datasize - strpid * objcfg.chunksize; //< relative read offset within the stripe
uint32_t rdsize = objcfg.chunksize - rdoff; //< read size within the stripe
if( rdsize > length ) rdsize = length;
//-------------------------------------------------------------------
// Make sure we operate on a valid block
//-------------------------------------------------------------------
if( !block || block->blkid != blkid )
block = std::make_shared<block_t>( blkid, *this, objcfg );
//-------------------------------------------------------------------
// Prepare the callback for reading from single stripe
//-------------------------------------------------------------------
auto blk = block;
auto callback = [blk, rdctx, rdsize, rdmtx]( const XrdCl::XRootDStatus &st, uint32_t nbrd )
{
std::unique_lock<std::mutex> lck( *rdmtx );
//-----------------------------------------------------------------
// Handle failure ...
//-----------------------------------------------------------------
if( !st.IsOK() )
{
ScheduleHandler( std::get<4>( *rdctx ), XrdCl::XRootDStatus( st ) );
return;
}
//-----------------------------------------------------------------
// Handle success ...
//-----------------------------------------------------------------
std::get<1>( *rdctx ) += nbrd; // number of bytes read
std::get<3>( *rdctx ) -= rdsize; // number of bytes requested
if( std::get<3>( *rdctx ) == 0 )
ScheduleHandler( std::get<0>( *rdctx ), std::get<1>( *rdctx ),
std::get<2>( *rdctx ), std::get<4>( *rdctx ) );
};
//-------------------------------------------------------------------
// Read data from a stripe
//-------------------------------------------------------------------
block->read( strpid, rdoff, rdsize, usrbuff, callback );
//-------------------------------------------------------------------
// Update absolute offset, read length, and user buffer
//-------------------------------------------------------------------
offset += rdsize;
length -= rdsize;
usrbuff += rdsize;
}
}
//-----------------------------------------------------------------------
// Close the data object
//-----------------------------------------------------------------------
void Reader::Close( XrdCl::ResponseHandler *handler )
{
//---------------------------------------------------------------------
// prepare the pipelines ...
//---------------------------------------------------------------------
std::vector<XrdCl::Pipeline> closes;
closes.reserve( dataarchs.size() );
auto itr = dataarchs.begin();
for( ; itr != dataarchs.end() ; ++itr )
{
XrdCl::ZipArchive &zip = *itr->second;
if( zip.IsOpen() )
closes.emplace_back( XrdCl::CloseArchive( zip ) );
}
// if there is nothing to close just schedule the handler
if( closes.empty() ) ScheduleHandler( handler );
// otherwise close the archives
else XrdCl::Async( XrdCl::Parallel( closes ) >> handler );
}
//-------------------------------------------------------------------------
// on-definition is not allowed here beforeiven stripes from given block
//-------------------------------------------------------------------------
void Reader::Read( size_t blknb, size_t strpnb, buffer_t &buffer, callback_t cb )
{
// generate the file name (blknb/strpnb)
std::string fn = objcfg.GetFileName( blknb, strpnb );
// if the block/stripe does not exist it means we are reading passed the end of the file
auto itr = urlmap.find( fn );
if( itr == urlmap.end() ) return cb( XrdCl::XRootDStatus(), 0 );
// get the URL of the ZIP archive with the respective data
const std::string &url = itr->second;
// get the ZipArchive object
auto &zipptr = dataarchs[url];
// check the size of the data to be read
XrdCl::StatInfo *info = nullptr;
auto st = zipptr->Stat( fn, info );
if( !st.IsOK() ) return cb( st, 0 );
uint32_t rdsize = info->GetSize();
delete info;
// create a buffer for the data
buffer.resize( rdsize );
// issue the read request
XrdCl::Async( XrdCl::ReadFrom( *zipptr, fn, 0, buffer.size(), buffer.data() ) >>
[zipptr, fn, cb]( XrdCl::XRootDStatus &st, XrdCl::ChunkInfo &ch )
{
//---------------------------------------------------
// If read failed there's nothing to do, just pass the
// status to user callback
//---------------------------------------------------
if( !st.IsOK() )
{
cb( st, 0 );
return;
}
//---------------------------------------------------
// Get the checksum for the read data
//---------------------------------------------------
uint32_t orgcksum = 0;
auto s = zipptr->GetCRC32( fn, orgcksum );
//---------------------------------------------------
// If we cannot extract the checksum assume the data
// are corrupted
//---------------------------------------------------
if( !st.IsOK() )
{
cb( st, 0 );
return;
}
//---------------------------------------------------
// Verify data integrity
//---------------------------------------------------
uint32_t cksum = crc32c( 0, ch.buffer, ch.length );
if( orgcksum != cksum )
{
cb( XrdCl::XRootDStatus( XrdCl::stError, XrdCl::errDataError ), 0 );
return;
}
//---------------------------------------------------
// All is good, we can call now the user callback
//---------------------------------------------------
cb( XrdCl::XRootDStatus(), ch.length );
} );
}
//-----------------------------------------------------------------------
// Read metadata for the object
//-----------------------------------------------------------------------
XrdCl::Pipeline Reader::ReadMetadata( size_t index )
{
const size_t size = objcfg.plgr.size();
// create the File object
auto file = std::make_shared<XrdCl::File>();
// prepare the URL for Open operation
std::string url = objcfg.GetMetadataUrl( index );
// arguments for the Read operation
XrdCl::Fwd<uint32_t> rdsize;
XrdCl::Fwd<void*> rdbuff;
return XrdCl::Open( *file, url, XrdCl::OpenFlags::Read ) >>
[=]( XrdCl::XRootDStatus &st, XrdCl::StatInfo &info )
{
if( !st.IsOK() )
{
if( index + 1 < size )
XrdCl::Pipeline::Replace( ReadMetadata( index + 1 ) );
return;
}
// prepare the args for the subsequent operation
rdsize = info.GetSize();
rdbuff = new char[info.GetSize()];
}
| XrdCl::Read( *file, 0, rdsize, rdbuff ) >>
[=]( XrdCl::XRootDStatus &st, XrdCl::ChunkInfo &ch )
{
if( !st.IsOK() )
{
if( index + 1 < size )
XrdCl::Pipeline::Replace( ReadMetadata( index + 1 ) );
return;
}
// now parse the metadata
if( !ParseMetadata( ch ) )
{
if( index + 1 < size )
XrdCl::Pipeline::Replace( ReadMetadata( index + 1 ) );
return;
}
}
| XrdCl::Final(
[=]( const XrdCl::XRootDStatus& )
{
// deallocate the buffer if necessary
if( rdbuff.Valid() )
{
char* buffer = reinterpret_cast<char*>( *rdbuff );
delete[] buffer;
}
// close the file if necessary (we don't really care about the result)
if( file->IsOpen() )
XrdCl::Async( XrdCl::Close( *file ) >> [file]( XrdCl::XRootDStatus& ){ } );
} );
}
//-----------------------------------------------------------------------
// Parse metadata from chunk info object
//-----------------------------------------------------------------------
bool Reader::ParseMetadata( XrdCl::ChunkInfo &ch )
{
const size_t mincnt = objcfg.nbdata + objcfg.nbparity;
const size_t maxcnt = objcfg.plgr.size();
char *buffer = reinterpret_cast<char*>( ch.buffer );
size_t length = ch.length;
for( size_t i = 0; i < maxcnt; ++i )
{
uint32_t signature = XrdZip::to<uint32_t>( buffer );
if( signature != XrdZip::LFH::lfhSign )
{
if( i + 1 < mincnt ) return false;
break;
}
XrdZip::LFH lfh( buffer );
// check if we are not reading passed the end of the buffer
if( lfh.lfhSize + lfh.uncompressedSize > length ) return false;
buffer += lfh.lfhSize;
length -= lfh.lfhSize;
// verify the checksum
uint32_t crc32val = crc32c( 0, buffer, lfh.uncompressedSize );
if( crc32val != lfh.ZCRC32 ) return false;
// keep the metadata
metadata.emplace( lfh.filename, buffer_t( buffer, buffer + lfh.uncompressedSize ) );
buffer += lfh.uncompressedSize;
length -= lfh.uncompressedSize;
}
return true;
}
} /* namespace XrdEc */