From nobody Sun Feb 8 19:24:20 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1494944573121140.9773491063579; Tue, 16 May 2017 07:22:53 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 28CEF7F7DE; Tue, 16 May 2017 14:21:59 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0C0388F638; Tue, 16 May 2017 14:21:44 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 841614A48D; Tue, 16 May 2017 14:21:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v4GE4QnE027768 for ; Tue, 16 May 2017 10:04:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id C2481860E9; Tue, 16 May 2017 14:04:26 +0000 (UTC) Received: from moe.brq.redhat.com (dhcp129-131.brq.redhat.com [10.34.129.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B2AA860E4 for ; Tue, 16 May 2017 14:04:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 28CEF7F7DE Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 28CEF7F7DE From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 16 May 2017 16:03:49 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 09/31] Introduce virStreamSparseSendAll X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 16 May 2017 14:22:51 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This is just a wrapper over new function that have been just introduced: virStreamSendHole() . It's very similar to virStreamSendAll() except it handles sparse streams well. Signed-off-by: Michal Privoznik Reviewed-by: John Ferlan --- include/libvirt/libvirt-stream.h | 65 +++++++++++++++- src/libvirt-stream.c | 159 +++++++++++++++++++++++++++++++++++= ++++ src/libvirt_public.syms | 1 + 3 files changed, 222 insertions(+), 3 deletions(-) diff --git a/include/libvirt/libvirt-stream.h b/include/libvirt/libvirt-str= eam.h index a5e69a1c1..d18d43140 100644 --- a/include/libvirt/libvirt-stream.h +++ b/include/libvirt/libvirt-stream.h @@ -71,9 +71,9 @@ int virStreamRecvHole(virStreamPtr, * @nbytes: size of the data array * @opaque: optional application provided data * - * The virStreamSourceFunc callback is used together - * with the virStreamSendAll function for libvirt to - * obtain the data that is to be sent. + * The virStreamSourceFunc callback is used together with + * the virStreamSendAll and virStreamSparseSendAll functions + * for libvirt to obtain the data that is to be sent. * * The callback will be invoked multiple times, * fetching data in small chunks. The application @@ -96,6 +96,65 @@ int virStreamSendAll(virStreamPtr st, virStreamSourceFunc handler, void *opaque); =20 +/** + * virStreamSourceHoleFunc: + * @st: the stream object + * @inData: are we in data section + * @length: how long is the section we are currently in + * @opaque: optional application provided data + * + * The virStreamSourceHoleFunc callback is used together with the + * virStreamSparseSendAll function for libvirt to obtain the + * length of section stream is currently in. + * + * Moreover, upon successful return, @length should be updated + * with how many bytes are left until the current section ends + * (either data section or hole section). Also the stream is + * currently in data section, @inData should be set to a non-zero + * value and vice versa. + * + * NB: there's an implicit hole at the end of each file. If + * that's the case, @inData and @length should be both set to 0. + * + * This function should not adjust the current position within + * the file. + * + * Returns 0 on success, + * -1 upon error + */ +typedef int (*virStreamSourceHoleFunc)(virStreamPtr st, + int *inData, + long long *length, + void *opaque); + +/** + * virStreamSourceSkipFunc: + * @st: the stream object + * @length: stream hole size + * @opaque: optional application provided data + * + * This callback is used together with the virStreamSparseSendAll + * to skip holes in the underlying file as reported by + * virStreamSourceHoleFunc. + * + * The callback may be invoked multiple times as holes are found + * during processing a stream. The application should skip + * processing the hole in the stream source and then return. + * A return value of -1 at any time will abort the send operation. + * + * Returns 0 on success, + * -1 upon error. + */ +typedef int (*virStreamSourceSkipFunc)(virStreamPtr st, + long long length, + void *opaque); + +int virStreamSparseSendAll(virStreamPtr st, + virStreamSourceFunc handler, + virStreamSourceHoleFunc holeHandler, + virStreamSourceSkipFunc skipHandler, + void *opaque); + /** * virStreamSinkFunc: * diff --git a/src/libvirt-stream.c b/src/libvirt-stream.c index 6bf4c4f29..4cbe5eee1 100644 --- a/src/libvirt-stream.c +++ b/src/libvirt-stream.c @@ -574,6 +574,165 @@ virStreamSendAll(virStreamPtr stream, } =20 =20 +/** + * virStreamSparseSendAll: + * @stream: pointer to the stream object + * @handler: source callback for reading data from application + * @holeHandler: source callback for determining holes + * @skipHandler: skip holes as reported by @holeHandler + * @opaque: application defined data + * + * Send the entire data stream, reading the data from the + * requested data source. This is simply a convenient alternative + * to virStreamSend, for apps that do blocking-I/O. + * + * An example using this with a hypothetical file upload + * API looks like + * + * int mysource(virStreamPtr st, char *buf, int nbytes, void *opaque) { + * int *fd =3D opaque; + * + * return read(*fd, buf, nbytes); + * } + * + * int myskip(virStreamPtr st, long long offset, void *opaque) { + * int *fd =3D opaque; + * + * return lseek(*fd, offset, SEEK_CUR) =3D=3D (off_t) -1 ? -1 : 0; + * } + * + * int myindata(virStreamPtr st, int *inData, + * long long *offset, void *opaque) { + * int *fd =3D opaque; + * + * if (@fd in hole) { + * *inData =3D 0; + * *offset =3D holeSize; + * } else { + * *inData =3D 1; + * *offset =3D dataSize; + * } + * + * return 0; + * } + * + * virStreamPtr st =3D virStreamNew(conn, 0); + * int fd =3D open("demo.iso", O_RDONLY); + * + * virConnectUploadFile(conn, st); + * if (virStreamSparseSendAll(st, + * mysource, + * myindata, + * myskip, + * &fd) < 0) { + * ...report an error ... + * goto done; + * } + * if (virStreamFinish(st) < 0) + * ...report an error... + * virStreamFree(st); + * close(fd); + * + * Note that @opaque data are shared between @handler, @holeHandler and @s= kipHandler. + * + * Returns 0 if all the data was successfully sent. The caller + * should invoke virStreamFinish(st) to flush the stream upon + * success and then virStreamFree. + * + * Returns -1 upon any error, with virStreamAbort() already + * having been called, so the caller need only call + * virStreamFree(). + */ +int virStreamSparseSendAll(virStreamPtr stream, + virStreamSourceFunc handler, + virStreamSourceHoleFunc holeHandler, + virStreamSourceSkipFunc skipHandler, + void *opaque) +{ + char *bytes =3D NULL; + size_t want =3D VIR_NET_MESSAGE_LEGACY_PAYLOAD_MAX; + int ret =3D -1; + unsigned long long dataLen =3D 0; + + VIR_DEBUG("stream=3D%p handler=3D%p holeHandler=3D%p opaque=3D%p", + stream, handler, holeHandler, opaque); + + virResetLastError(); + + virCheckStreamReturn(stream, -1); + virCheckNonNullArgGoto(handler, cleanup); + virCheckNonNullArgGoto(holeHandler, cleanup); + virCheckNonNullArgGoto(skipHandler, cleanup); + + if (stream->flags & VIR_STREAM_NONBLOCK) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("data sources cannot be used for non-blocking str= eams")); + goto cleanup; + } + + if (VIR_ALLOC_N(bytes, want) < 0) + goto cleanup; + + for (;;) { + int inData, got, offset =3D 0; + long long sectionLen; + const unsigned int skipFlags =3D 0; + + if (!dataLen) { + if (holeHandler(stream, &inData, §ionLen, opaque) < 0) { + virStreamAbort(stream); + goto cleanup; + } + + if (!inData && sectionLen) { + if (virStreamSendHole(stream, sectionLen, skipFlags) < 0) { + virStreamAbort(stream); + goto cleanup; + } + + if (skipHandler(stream, sectionLen, opaque) < 0) { + virReportSystemError(errno, "%s", + _("unable to skip hole")); + virStreamAbort(stream); + goto cleanup; + } + continue; + } else { + dataLen =3D sectionLen; + } + } + + if (want > dataLen) + want =3D dataLen; + + got =3D (handler)(stream, bytes, want, opaque); + if (got < 0) { + virStreamAbort(stream); + goto cleanup; + } + if (got =3D=3D 0) + break; + while (offset < got) { + int done; + done =3D virStreamSend(stream, bytes + offset, got - offset); + if (done < 0) + goto cleanup; + offset +=3D done; + dataLen -=3D done; + } + } + ret =3D 0; + + cleanup: + VIR_FREE(bytes); + + if (ret !=3D 0) + virDispatchError(stream->conn); + + return ret; +} + + /** * virStreamRecvAll: * @stream: pointer to the stream object diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 37fc4e224..fac77fbea 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -765,6 +765,7 @@ LIBVIRT_3.4.0 { virStreamRecvHole; virStreamSendHole; virStreamSparseRecvAll; + virStreamSparseSendAll; } LIBVIRT_3.1.0; =20 # .... define new API here using predicted next version number .... --=20 2.13.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list