From nobody Mon Feb 9 22:18:38 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 1495549704627390.51471060101244; Tue, 23 May 2017 07:28:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D831E8123A; Tue, 23 May 2017 14:28:21 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AC1E64DA3B; Tue, 23 May 2017 14:28:21 +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 5BAA1180BAF9; Tue, 23 May 2017 14:28:21 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v4NEQN14028685 for ; Tue, 23 May 2017 10:26:23 -0400 Received: by smtp.corp.redhat.com (Postfix) id 34C317814F; Tue, 23 May 2017 14:26:23 +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 B024B7C749 for ; Tue, 23 May 2017 14:26:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D831E8123A Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.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 D831E8123A From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 23 May 2017 16:26:11 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-python][PATCH v2 4/4] examples: Introduce sparsestream.py 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.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 23 May 2017 14:28:22 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Sparse streams are not that straight forward to use for the very first time. Especially the sparseRecvAll() and sparseSendAll() methods which expects callbacks. What we can do to make it easier for developers is to have an example where they can take an inspiration from. Signed-off-by: Michal Privoznik --- examples/sparsestream.py | 117 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 117 insertions(+) create mode 100755 examples/sparsestream.py diff --git a/examples/sparsestream.py b/examples/sparsestream.py new file mode 100755 index 0000000..e960c40 --- /dev/null +++ b/examples/sparsestream.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# Example of sparse streams usage +# +# Authors: +# Michal Privoznik + +import libvirt, sys, os + +def bytesWriteHandler(stream, buf, opaque): + fd =3D opaque + return os.write(fd, buf) + +def bytesReadHandler(stream, nbytes, opaque): + fd =3D opaque + return os.read(fd, nbytes) + +def recvSkipHandler(stream, length, opaque): + fd =3D opaque + cur =3D os.lseek(fd, length, os.SEEK_CUR) + return os.ftruncate(fd, cur) + +def sendSkipHandler(stream, length, opaque): + fd =3D opaque + return os.lseek(fd, length, os.SEEK_CUR) + +def holeHandler(stream, opaque): + fd =3D opaque + cur =3D os.lseek(fd, 0, os.SEEK_CUR) + + try: + data =3D os.lseek(fd, cur, os.SEEK_DATA) + except OSError as e: + if e.errno !=3D 6: + raise e + else: + data =3D -1; + # There are three options: + # 1) data =3D=3D cur; @cur is in data + # 2) data > cur; @cur is in a hole, next data at @data + # 3) data < 0; either @cur is in trailing hole, or @cur is beyond EOF. + if data < 0: + # case 3 + inData =3D False + eof =3D os.lseek(fd, 0, os.SEEK_END) + if (eof < cur): + raise RuntimeError("Current position in file after EOF: %d" % = cur) + sectionLen =3D eof - cur + else: + if (data > cur): + # case 2 + inData =3D False + sectionLen =3D data - cur + else: + # case 1 + inData =3D True + + # We don't know where does the next hole start. Let's find out. + # Here we get the same options as above + hole =3D os.lseek(fd, data, os.SEEK_HOLE) + if hole < 0: + # case 3. But wait a second. There is always a trailing ho= le. + # Do the best what we can here + raise RuntimeError("No trailing hole") + + if (hole =3D=3D data): + # case 1. Again, this is suspicious. The reason we are her= e is + # because we are in data. But at the same time we are in a + # hole. WAT? + raise RuntimeError("Impossible happened") + else: + # case 2 + sectionLen =3D hole - data + os.lseek(fd, cur, os.SEEK_SET) + return [inData, sectionLen] + +def download(vol, st, filename): + offset =3D 0 + length =3D 0 + + fd =3D os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode= =3D0o0660) + vol.download(st, offset, length, libvirt.VIR_STORAGE_VOL_DOWNLOAD_SPAR= SE_STREAM) + st.sparseRecvAll(bytesWriteHandler, recvSkipHandler, fd) + + os.close(fd) + +def upload(vol, st, filename): + offset =3D 0 + length =3D 0 + + fd =3D os.open(filename, os.O_RDONLY) + vol.upload(st, offset, length, libvirt.VIR_STORAGE_VOL_UPLOAD_SPARSE_S= TREAM) + st.sparseSendAll(bytesReadHandler, holeHandler, sendSkipHandler, fd) + + os.close(fd) + +# main +if len(sys.argv) !=3D 5: + print("Usage: ", sys.argv[0], " URI --upload/--download VOLUME FILE") + print("Either uploads local FILE to libvirt VOLUME, or downloads libvi= rt ") + print("VOLUME into local FILE while preserving FILE/VOLUME sparseness") + sys.exit(1) + +conn =3D libvirt.open(sys.argv[1]) +vol =3D conn.storageVolLookupByKey(sys.argv[3]) + +st =3D conn.newStream() + +if sys.argv[2] =3D=3D "--download": + download(vol, st, sys.argv[4]) +elif sys.argv[2] =3D=3D "--upload": + upload(vol, st, sys.argv[4]) +else: + print("Unknown operation: %s " % sys.argv[1]) + sys.exit(1) + +st.finish() +conn.close() --=20 2.13.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list