Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- Changes | 1 + Virt.xs | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/Sys/Virt/Stream.pm | 19 ++++++++ t/030-api-coverage.t | 2 + 4 files changed, 146 insertions(+) diff --git a/Changes b/Changes index 4c1e071..989b795 100644 --- a/Changes +++ b/Changes @@ -8,6 +8,7 @@ Revision history for perl module Sys::Virt register RECV_STOP_AT_HOLE constant - Introduce Stream::recvHole() and Stream::sendHole() - Introduce Stream::sparse_recv_all() + - Introduce Stream::sparse_send_all() 3.3.0 2017-05-08 diff --git a/Virt.xs b/Virt.xs index 002bd6a..fc282b8 100644 --- a/Virt.xs +++ b/Virt.xs @@ -1960,6 +1960,100 @@ _stream_send_all_source(virStreamPtr st, } +static int +_stream_sparse_send_all_holeHandler(virStreamPtr st, + int *inData, + long long *length, + void *opaque) +{ + AV *av = opaque; + SV **self; + SV **holeHandler; + SV *inDataSV; + SV *lengthSV; + int count; + int ret; + dSP; + + self = av_fetch(av, 0, 0); + holeHandler = av_fetch(av, 2, 0); + + SvREFCNT_inc(*self); + + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(*self); + PUTBACK; + + count = call_sv((SV*)*holeHandler, G_ARRAY); + + SPAGAIN; + + if (count == 2) { + /* @holeHandler returns (inData, length), but on a stack. + * Therefore the order is reversed. */ + lengthSV = POPs; + inDataSV = POPs; + *inData = virt_SvIVll(inDataSV); + *length = virt_SvIVll(lengthSV); + ret = 0; + } else { + ret = -1; + } + + PUTBACK; + FREETMPS; + LEAVE; + + return ret; +} + + +static int +_stream_sparse_send_all_skipHandler(virStreamPtr st, + long long length, + void *opaque) +{ + AV *av = opaque; + SV **self; + SV **skipHandler; + int rv; + int ret; + dSP; + + self = av_fetch(av, 0, 0); + skipHandler = av_fetch(av, 3, 0); + + SvREFCNT_inc(*self); + + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(*self); + XPUSHs(sv_2mortal(virt_newSVll(length))); + PUTBACK; + + rv = call_sv((SV*)*skipHandler, G_SCALAR); + + SPAGAIN; + + if (rv == 1) { + ret = POPi; + } else { + ret = -1; + } + + PUTBACK; + FREETMPS; + LEAVE; + + return ret; +} + + static int _stream_recv_all_sink(virStreamPtr st, const char *data, @@ -8041,6 +8135,36 @@ sparse_recv_all(stref, handler, holeHandler) SvREFCNT_dec(opaque); +void +sparse_send_all(stref, handler, holeHandler, skipHandler) + SV *stref; + SV *handler; + SV *holeHandler; + SV *skipHandler; + PREINIT: + AV *opaque; + virStreamPtr st; + CODE: + st = (virStreamPtr)SvIV((SV*)SvRV(stref)); + + opaque = newAV(); + SvREFCNT_inc(stref); + SvREFCNT_inc(handler); + SvREFCNT_inc(holeHandler); + SvREFCNT_inc(skipHandler); + av_push(opaque, stref); + av_push(opaque, handler); + av_push(opaque, holeHandler); + av_push(opaque, skipHandler); + + if (virStreamSparseSendAll(st, + _stream_send_all_source, + _stream_sparse_send_all_holeHandler, + _stream_sparse_send_all_skipHandler, + opaque) < 0) + _croak_error(); + + SvREFCNT_dec(opaque); void add_callback(stref, events, cb) diff --git a/lib/Sys/Virt/Stream.pm b/lib/Sys/Virt/Stream.pm index c32b957..e3b25a5 100644 --- a/lib/Sys/Virt/Stream.pm +++ b/lib/Sys/Virt/Stream.pm @@ -144,6 +144,25 @@ bytes). The C<$holeHandler> is expected to return a non-negative number on success (usually 0) and a negative number (usually -1) otherwise. +=item $st->sparse_send_all($handler, $holeHandler, $skipHandler) + +Send all data produced by C<$handler> to the stream. The +C<$handler> parameter must be a function which expects three +arguments, the C<$st> stream object, a scalar which must be +filled with data and a maximum data byte count desired. The +function should return the number of bytes filled, 0 on end of +file, or -1 upon error. The second argument C<$holeHandler> is a +function expecting just one argument C<$st> and returning an +array of two elements (C<$inData>, C<$sectionLen>) where +C<$inData> has zero or non-zero value if underlying file is in a +hole or data section respectively. The C<$sectionLen> then is the +number of remaining bytes in the current section in the +underlying file. Finally, the third C<$skipHandler> is a function +expecting two arguments C<$st> and C<$length> which moves cursor +in the underlying file for C<$length> bytes. The C<$skipHandler> +is expected to return a non-negative number on success (usually +0) and a negative number (usually -1) otherwise. + =item $st->add_callback($events, $coderef) Register a callback to be invoked whenever the stream has diff --git a/t/030-api-coverage.t b/t/030-api-coverage.t index 6a281ba..95bbd11 100644 --- a/t/030-api-coverage.t +++ b/t/030-api-coverage.t @@ -116,6 +116,8 @@ virStreamEventCallback virStreamSinkFunc virStreamSinkHoleFunc virStreamSourceFunc +virStreamSourceHoleFunc +virStreamSourceSkipFunc virConnectCloseFunc -- 2.13.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list