-
Notifications
You must be signed in to change notification settings - Fork 149
/
PyXRootDFile.cc
764 lines (623 loc) · 27 KB
/
PyXRootDFile.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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//------------------------------------------------------------------------------
// Copyright (c) 2012-2014 by European Organization for Nuclear Research (CERN)
// Author: Justin Salmon <jsalmon@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 "PyXRootD.hh"
#include "PyXRootDFile.hh"
#include "AsyncResponseHandler.hh"
#include "ChunkIterator.hh"
#include "Utils.hh"
#include "XrdCl/XrdClFile.hh"
#include "XrdCl/XrdClFileSystem.hh"
namespace PyXRootD
{
//----------------------------------------------------------------------------
//! Open the file pointed to by the given URL
//----------------------------------------------------------------------------
PyObject* File::Open( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "url", "flags", "mode",
"timeout", "callback", NULL };
const char *url;
XrdCl::OpenFlags::Flags flags = XrdCl::OpenFlags::None;
XrdCl::Access::Mode mode = XrdCl::Access::None;
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL;
XrdCl::XRootDStatus status;
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "s|HHHO:open",
(char**) kwlist, &url, &flags, &mode, &timeout, &callback ) )
return NULL;
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::AnyObject>( callback );
if ( !handler ) return NULL;
async( status = self->file->Open( url, flags, mode, handler, timeout ) );
}
else {
async( status = self->file->Open( url, flags, mode, timeout ) );
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, Py_BuildValue( "" ) );
Py_DECREF( pystatus );
return o;
}
//----------------------------------------------------------------------------
//! Close the file
//----------------------------------------------------------------------------
PyObject* File::Close( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "timeout", "callback", NULL };
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL;
XrdCl::XRootDStatus status;
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|HO:close", (char**) kwlist,
&timeout, &callback ) ) return NULL;
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::AnyObject>( callback );
if ( !handler ) return NULL;
async( status = self->file->Close( handler, timeout ) );
}
else {
async( status = self->file->Close( timeout ) )
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, Py_BuildValue( "" ) );
Py_DECREF( pystatus );
return o;
}
//----------------------------------------------------------------------------
//! Obtain status information for this file
//----------------------------------------------------------------------------
PyObject* File::Stat( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "force", "timeout", "callback", NULL };
bool force = false;
uint16_t timeout = 0;
PyObject *callback = NULL, *pyresponse = NULL, *pystatus = NULL;
XrdCl::XRootDStatus status;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|iHO:stat", (char**) kwlist,
&force, &timeout, &callback ) ) return NULL;
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::StatInfo>( callback );
async( status = self->file->Stat( force, handler, timeout ) );
}
else {
XrdCl::StatInfo *response = 0;
async( status = self->file->Stat( force, response, timeout ) );
pyresponse = ConvertType<XrdCl::StatInfo>( response );
delete response;
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, pyresponse );
Py_DECREF( pystatus );
Py_XDECREF( pyresponse );
return o;
}
//----------------------------------------------------------------------------
//! Read a data chunk at a given offset
//----------------------------------------------------------------------------
PyObject* File::Read( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "offset", "size", "timeout", "callback",
NULL };
uint64_t offset = 0;
uint32_t size = 0;
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL, *pyresponse = NULL;
PyObject *py_offset = NULL, *py_size = NULL, *py_timeout = NULL;
char *buffer = 0;
XrdCl::XRootDStatus status;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|OOOO:read",
(char**) kwlist, &py_offset, &py_size, &py_timeout, &callback ) ) return NULL;
unsigned long long tmp_offset = 0;
unsigned int tmp_size = 0;
unsigned short int tmp_timeout = 0;
if ( py_offset && PyObjToUllong( py_offset, &tmp_offset, "offset" ) )
return NULL;
if ( py_size && PyObjToUint(py_size, &tmp_size, "size" ) )
return NULL;
if ( py_timeout && PyObjToUshrt(py_timeout, &tmp_timeout, "timeout" ) )
return NULL;
offset = (uint64_t)tmp_offset;
size = (uint32_t)tmp_size;
timeout = (uint16_t)tmp_timeout;
if (!size) {
XrdCl::StatInfo *info = 0;
async( XrdCl::XRootDStatus status = self->file->Stat(true, info, timeout) );
size = info->GetSize();
if (info) delete info;
}
buffer = new char[size];
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::ChunkInfo>( callback );
if ( !handler ) {
delete[] buffer;
return NULL;
}
async( status = self->file->Read( offset, size, buffer, handler, timeout ) );
}
else {
uint32_t bytesRead = 0;
async( status = self->file->Read( offset, size, buffer, bytesRead, timeout ) );
pyresponse = PyBytes_FromStringAndSize( buffer, bytesRead );
delete[] buffer;
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, pyresponse );
Py_DECREF( pystatus );
Py_XDECREF( pyresponse );
return o;
}
//----------------------------------------------------------------------------
// Read a data chunk at a given offset, until the first newline encountered
// or size data read.
//----------------------------------------------------------------------------
PyObject* File::ReadLine( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "offset", "size", "chunksize", NULL };
uint64_t offset = 0;
uint32_t size = 0;
uint32_t chunksize = 0;
PyObject *pyline = NULL;
PyObject *py_offset = NULL, *py_size = NULL, *py_chunksize = NULL;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|OOO:readline",
(char**) kwlist, &py_offset, &py_size, &py_chunksize ) ) return NULL;
unsigned long long tmp_offset = 0;
unsigned int tmp_size = 0, tmp_chunksize = 0;
if ( py_offset && PyObjToUllong(py_offset, &tmp_offset, "offset" ) )
return NULL;
if ( py_size && PyObjToUint(py_size, &tmp_size, "size" ) )
return NULL;
if ( py_chunksize && PyObjToUint(py_chunksize, &tmp_chunksize, "chunksize" ) )
return NULL;
offset = (uint64_t)tmp_offset;
size = (uint32_t)tmp_size;
chunksize = (uint32_t)tmp_chunksize;
uint64_t off_init = offset;
if (offset == 0)
offset = self->currentOffset;
else
self->currentOffset = offset;
// Default chunk size is 2MB or equal to size if size less then 2MB
if ( !chunksize ) chunksize = 1024 * 1024 * 2;
if ( !size ) size = UINT_MAX;
if ( size < chunksize ) chunksize = size;
uint64_t off_end = offset + size;
XrdCl::Buffer* chunk = new XrdCl::Buffer();
XrdCl::Buffer* line = new XrdCl::Buffer();
while ( offset < off_end )
{
chunk = self->ReadChunk( self, offset, chunksize );
offset += chunk->GetSize();
// Reached end of file
if ( !chunk->GetSize() )
break;
// Check if we read a new line
bool found_newline = false;
for( uint32_t i = 0; i < chunk->GetSize(); ++i )
{
chunk->SetCursor( i );
// Stop if found newline or read required amount of data
if( ( *chunk->GetBufferAtCursor() == '\n') ||
( line->GetSize() + i >= size))
{
found_newline = true;
line->Append( chunk->GetBuffer(), i + 1 );
break;
}
}
if ( !found_newline )
line->Append( chunk->GetBuffer(), chunk->GetSize() );
else
break;
}
if ( line->GetSize() != 0 )
{
// Update file offset if default readline call
if ( off_init == 0 )
self->currentOffset += line->GetSize();
pyline = PyBytes_FromStringAndSize( line->GetBuffer(), line->GetSize() );
}
else
pyline = PyBytes_FromString( "" );
delete line;
delete chunk;
return pyline;
}
//----------------------------------------------------------------------------
//! Read data chunks from a given offset, separated by newlines, until EOF
//! encountered. Return list of lines read. A max read size can be specified,
//! but it should be noted that using this method is probably a bad idea.
//----------------------------------------------------------------------------
PyObject* File::ReadLines( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "offset", "size", "chunksize", NULL };
uint64_t offset = 0;
uint32_t size = 0;
uint32_t chunksize = 0;
PyObject *py_offset = NULL, *py_size = NULL, *py_chunksize = NULL;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|kII:readlines",
(char**) kwlist, &offset, &size, &chunksize ) ) return NULL;
unsigned long long tmp_offset = 0;
unsigned int tmp_size = 0, tmp_chunksize = 0;
if ( py_offset && PyObjToUllong( py_offset, &tmp_offset, "offset" ) )
return NULL;
if ( py_size && PyObjToUint( py_size, &tmp_size, "size" ) )
return NULL;
if ( py_chunksize && PyObjToUint( py_chunksize, &tmp_chunksize, "chunksize" ) )
return NULL;
offset = (uint64_t)tmp_offset;
size = (uint32_t)tmp_size;
chunksize = (uint16_t)tmp_chunksize;
PyObject *lines = PyList_New( 0 );
PyObject *line = NULL;
for (;;)
{
line = self->ReadLine( self, args, kwds );
if ( !line || PyBytes_Size( line ) == 0 )
break;
PyList_Append( lines, line );
}
return lines;
}
//----------------------------------------------------------------------------
//! Read a chunk of the given size from the given offset as a string
//----------------------------------------------------------------------------
XrdCl::Buffer* File::ReadChunk( File *self, uint64_t offset, uint32_t size )
{
XrdCl::XRootDStatus status;
XrdCl::Buffer *buffer;
XrdCl::Buffer *temp;
uint32_t bytesRead = 0;
temp = new XrdCl::Buffer( size );
status = self->file->Read( offset, size, temp->GetBuffer(), bytesRead );
buffer = new XrdCl::Buffer( bytesRead );
buffer->Append( temp->GetBuffer(), bytesRead );
delete temp;
return buffer;
}
//----------------------------------------------------------------------------
//! Read data chunks from a given offset of the given size, until EOF
//! encountered. Return chunk iterator.
//----------------------------------------------------------------------------
PyObject* File::ReadChunks( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "offset", "chunksize", NULL };
uint64_t offset = 0;
uint32_t chunksize = 0;
ChunkIterator *iterator;
PyObject *py_offset = NULL, *py_chunksize = NULL;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|OO:readchunks",
(char**) kwlist, &py_offset, &py_chunksize ) ) return NULL;
unsigned long long tmp_offset = 0;
unsigned int tmp_chunksize = 1024 * 1024 *2; // 2 MB
if ( py_offset && PyObjToUllong( py_offset, &tmp_offset, "offset" ) )
return NULL;
if ( py_chunksize && PyObjToUint( py_chunksize, &tmp_chunksize, "chunksize" ) )
return NULL;
offset = (uint64_t)tmp_offset;
chunksize = (uint32_t)tmp_chunksize;
ChunkIteratorType.tp_new = PyType_GenericNew;
if ( PyType_Ready( &ChunkIteratorType ) < 0 ) return NULL;
args = Py_BuildValue( "OOO", self, Py_BuildValue("k", offset),
Py_BuildValue("I", chunksize) );
iterator = (ChunkIterator*)
PyObject_CallObject( (PyObject *) &ChunkIteratorType, args );
Py_DECREF( args );
if ( !iterator ) return NULL;
return (PyObject *) iterator;
}
//----------------------------------------------------------------------------
//! Write a data chunk at a given offset
//----------------------------------------------------------------------------
PyObject* File::Write( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "buffer", "offset", "size", "timeout",
"callback", NULL };
const char *buffer;
int buffsize;
uint64_t offset = 0;
uint32_t size = 0;
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL;
PyObject *py_offset = NULL, *py_size = NULL, *py_timeout = NULL;
XrdCl::XRootDStatus status;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "s#|OOOO:write",
(char**) kwlist, &buffer, &buffsize, &py_offset, &py_size,
&py_timeout, &callback ) ) return NULL;
unsigned long long tmp_offset = 0;
unsigned int tmp_size = 0;
unsigned short int tmp_timeout = 0;
if (py_offset && PyObjToUllong(py_offset, &tmp_offset, "offset"))
return NULL;
if (py_size && PyObjToUint(py_size, &tmp_size, "size"))
return NULL;
if (py_timeout && PyObjToUshrt(py_timeout, &tmp_timeout, "timeout"))
return NULL;
offset = (uint64_t)tmp_offset;
size = (uint32_t)tmp_size;
timeout = (uint16_t)tmp_timeout;
if (!size) {
size = buffsize;
}
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::AnyObject>( callback );
if ( !handler ) return NULL;
async( status = self->file->Write( offset, size, buffer, handler, timeout ) );
}
else {
async( status = self->file->Write( offset, size, buffer, timeout ) );
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, Py_BuildValue( "" ) );
Py_DECREF( pystatus );
return o;
}
//----------------------------------------------------------------------------
//! Commit all pending disk writes
//----------------------------------------------------------------------------
PyObject* File::Sync( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "timeout", "callback", NULL };
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL;
XrdCl::XRootDStatus status;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|HO:sync", (char**) kwlist,
&timeout, &callback ) ) return NULL;
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::AnyObject>( callback );
if ( !handler ) return NULL;
async( status = self->file->Sync( handler, timeout ) );
}
else {
async( status = self->file->Sync( timeout ) );
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, Py_BuildValue( "" ) );
Py_DECREF( pystatus );
return o;
}
//----------------------------------------------------------------------------
//! Truncate the file to a particular size
//----------------------------------------------------------------------------
PyObject* File::Truncate( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "size", "timeout", "callback", NULL };
uint64_t size;
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL;
PyObject *py_size = NULL, *py_timeout = NULL;
XrdCl::XRootDStatus status;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "O|OO:truncate",
(char**) kwlist, &py_size, &py_timeout, &callback ) ) return NULL;
unsigned long long tmp_size = 0;
unsigned short int tmp_timeout = 0;
if ( py_size && PyObjToUllong( py_size, &tmp_size, "size" ) )
return NULL;
if ( py_timeout && PyObjToUshrt( py_timeout, &tmp_timeout, "timeout" ) )
return NULL;
size = (uint64_t)tmp_size;
timeout = (uint16_t)tmp_timeout;
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::AnyObject>( callback );
if ( !handler ) return NULL;
async( status = self->file->Truncate( size, handler, timeout ) );
}
else {
async( status = self->file->Truncate( size, timeout ) );
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, Py_BuildValue( "" ) );
Py_DECREF( pystatus );
return o;
}
//----------------------------------------------------------------------------
//! Read scattered data chunks in one operation
//----------------------------------------------------------------------------
PyObject* File::VectorRead( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "chunks", "timeout", "callback", NULL };
uint16_t timeout = 0;
uint64_t offset = 0;
uint32_t length = 0;
PyObject *pychunks = NULL, *callback = NULL;
PyObject *pyresponse = NULL, *pystatus = NULL, *py_timeout = NULL;
XrdCl::XRootDStatus status;
XrdCl::ChunkList chunks;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "O|OO:vector_read",
(char**) kwlist, &pychunks, &py_timeout, &callback ) ) return NULL;
unsigned short int tmp_timeout = 0;
if ( py_timeout && PyObjToUshrt( py_timeout, &tmp_timeout, "timeout" ) )
return NULL;
timeout = (uint16_t)tmp_timeout;
if ( !PyList_Check( pychunks ) ) {
PyErr_SetString( PyExc_TypeError, "chunks parameter must be a list" );
return NULL;
}
for ( int i = 0; i < PyList_Size( pychunks ); ++i ) {
PyObject *chunk = PyList_GetItem( pychunks, i );
if ( !PyTuple_Check( chunk ) || ( PyTuple_Size( chunk ) != 2 ) ) {
PyErr_SetString( PyExc_TypeError, "vector_read() expects list of tuples"
" of length 2" );
return NULL;
}
// Check that offset and length values are valid
unsigned long long tmp_offset = 0;
unsigned int tmp_length = 0;
if ( PyObjToUllong( PyTuple_GetItem( chunk, 0 ), &tmp_offset, "offset" ) )
return NULL;
if ( PyObjToUint( PyTuple_GetItem( chunk, 1 ), &tmp_length, "length" ) )
return NULL;
offset = (uint64_t)tmp_offset;
length = (uint32_t)tmp_length;
char *buffer = new char[length];
chunks.push_back( XrdCl::ChunkInfo( offset, length, buffer ) );
}
if ( callback && callback != Py_None ) {
XrdCl::ResponseHandler *handler
= GetHandler<XrdCl::VectorReadInfo>( callback );
if ( !handler ) return NULL;
async( status = self->file->VectorRead( chunks, 0, handler, timeout ) );
}
else {
XrdCl::VectorReadInfo *info = 0;
async( status = self->file->VectorRead( chunks, 0, info, timeout ) );
pyresponse = ConvertType<XrdCl::VectorReadInfo>( info );
delete info;
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, pyresponse );
Py_DECREF( pystatus );
Py_XDECREF( pyresponse );
return o;
}
//----------------------------------------------------------------------------
// Perform a custom operation on an open file
//----------------------------------------------------------------------------
PyObject* File::Fcntl( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "arg", "timeout", "callback", NULL };
const char *buffer = 0;
int buffSize = 0;
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL, *pyresponse = NULL;
XrdCl::XRootDStatus status;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "s#|HO:fcntl",
(char**) kwlist, &buffer, &buffSize, &timeout, &callback ) )
return NULL;
XrdCl::Buffer arg; arg.Append( buffer, buffSize );
if ( callback && callback != Py_None )
{
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::Buffer>( callback );
if( !handler )
return NULL;
async( status = self->file->Fcntl( arg, handler, timeout ) );
}
else {
XrdCl::Buffer *response = 0;
async( status = self->file->Fcntl( arg, response, timeout ) );
pyresponse = ConvertType<XrdCl::Buffer>( response );
delete response;
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, pyresponse );
Py_DECREF( pystatus );
Py_XDECREF( pyresponse );
return o;
}
//----------------------------------------------------------------------------
// Perform a custom operation on an open file
//----------------------------------------------------------------------------
PyObject* File::Visa( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "timeout", "callback", NULL };
uint16_t timeout = 0;
PyObject *callback = NULL, *pystatus = NULL, *pyresponse = NULL;
XrdCl::XRootDStatus status;
if ( !self->file->IsOpen() ) return FileClosedError();
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "|HO:visa",
(char**) kwlist, &timeout, &callback ) )
return NULL;
if ( callback && callback != Py_None )
{
XrdCl::ResponseHandler *handler = GetHandler<XrdCl::Buffer>( callback );
if( !handler )
return NULL;
async( status = self->file->Visa( handler, timeout ) );
}
else {
XrdCl::Buffer *response = 0;
async( status = self->file->Visa( response, timeout ) );
pyresponse = ConvertType<XrdCl::Buffer>( response );
delete response;
}
pystatus = ConvertType<XrdCl::XRootDStatus>( &status );
PyObject *o = ( callback && callback != Py_None ) ?
Py_BuildValue( "O", pystatus ) :
Py_BuildValue( "OO", pystatus, pyresponse );
Py_DECREF( pystatus );
Py_XDECREF( pyresponse );
return o;
}
//----------------------------------------------------------------------------
//! Check if the file is open
//----------------------------------------------------------------------------
PyObject* File::IsOpen( File *self, PyObject *args, PyObject *kwds )
{
if ( !PyArg_ParseTuple( args, ":is_open" ) ) return NULL; // Allow no args
return PyBool_FromLong(self->file->IsOpen());
}
//----------------------------------------------------------------------------
//! Get property
//----------------------------------------------------------------------------
PyObject* File::GetProperty( File *self, PyObject *args, PyObject *kwds )
{
static const char *kwlist[] = { "name", NULL };
char *name = 0;
std::string value;
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "s:get_property",
(char**) kwlist, &name ) ) return NULL;
bool status = self->file->GetProperty( name, value );
return status ? Py_BuildValue( "s", value.c_str() ) : Py_None;
}
//----------------------------------------------------------------------------
//! Set property
//----------------------------------------------------------------------------
PyObject* File::SetProperty( File *self, PyObject *args, PyObject *kwds )
{
(void) FileType; // Suppress unused variable warning
static const char *kwlist[] = { "name", "value", NULL };
char *name = 0;
char *value = 0;
if ( !PyArg_ParseTupleAndKeywords( args, kwds, "ss:set_property",
(char**) kwlist, &name, &value ) ) return NULL;
bool status = self->file->SetProperty( name, value );
return status ? Py_True : Py_False;
}
}