From nobody Wed Feb 11 02:14:09 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 1498140192513622.1961710706898; Thu, 22 Jun 2017 07:03:12 -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 184E07F40F; Thu, 22 Jun 2017 14:03:09 +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 E3D7E171E4; Thu, 22 Jun 2017 14:03:08 +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 956B94E9B4; Thu, 22 Jun 2017 14:03:08 +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 v5ME2vg7004535 for ; Thu, 22 Jun 2017 10:02:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id DA36D5D967; Thu, 22 Jun 2017 14:02:57 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-36.phx2.redhat.com [10.3.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99E665D960 for ; Thu, 22 Jun 2017 14:02:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 184E07F40F 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 184E07F40F From: John Ferlan To: libvir-list@redhat.com Date: Thu, 22 Jun 2017 10:02:44 -0400 Message-Id: <20170622140246.31777-15-jferlan@redhat.com> In-Reply-To: <20170622140246.31777-1-jferlan@redhat.com> References: <20170622140246.31777-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 14/16] util: Introduce virObjectLookupHashClone 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]); Thu, 22 Jun 2017 14:03:10 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" A convenience API that will utilize the virHashForEach API for a specified LookupHash in order to create a clone/copy. Primary consumer is the interfa= ce driver which has a desire to save off a copy of it's @Name LookupHash table in order to possible restore it if something goes wrong during processing. The callback function's primary purpose is to copy anything within the local LookupKeys into the target. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virobject.c | 79 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 10 ++++++ 3 files changed, 90 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2c4296a..41638f6 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2287,6 +2287,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectLookupHashAdd; +virObjectLookupHashClone; virObjectLookupHashFind; virObjectLookupHashForEach; virObjectLookupHashGetName; diff --git a/src/util/virobject.c b/src/util/virobject.c index 60bc5b5..9182a2e 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -992,3 +992,82 @@ virObjectLookupHashSearch(void *tableobj, =20 return obj; } + + +struct cloneData { + virObjectLookupHashCloneCallback callback; + virObjectLookupHashPtr dst; + bool error; +}; + +/* + * Take the provided virHashForEach element and call the driver @cb functi= on + * with the input @dstTable and the source element from the @srcTable in o= rder + * to perform the copy - tracking success/failure using the error boolean. + * Once there's a failure, no future copy/clone will occur. + * + * The @cb function can expect the @srcTable object to be locked upon entr= y. + * + * Returns 0 to the virHashForEach on success, -1 on failure. + */ +static int +cloneCallback(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virObjectLookupKeysPtr obj =3D payload; + struct cloneData *data =3D opaque; + + if (data->error) + return 0; + + virObjectLock(obj); + + if (data->callback(data->dst, obj) < 0) + data->error =3D true; + + virObjectUnlock(obj); + + if (data->error) + return -1; + + return 0; +} + +/** + * virObjectLookupHashClone + * @srcTable: source poolable hash table pointer to clone from + * @dstTable: destination poolable hash table pointer to clone to + * @useUUID: Use the objsUUID for clone (or objsName) + * @cb: callback function from driver code to handle the clone + * + * The clone function is designed to traverse each @srcTable hash element + * and call the driver specific @cb function with the element from the + * @srcTable in order to clone into the @dstTable. If @useUUID is true, + * then clone the objsUUID table; otherwise, clone the objsName table. + * + * Return 0 on success, -1 on failure + */ +int +virObjectLookupHashClone(void *srcTable, + void *dstTable, + bool useUUID, + virObjectLookupHashCloneCallback cb) +{ + virObjectLookupHashPtr src =3D virObjectGetLookupHashObj(srcTable); + virObjectLookupHashPtr dst =3D virObjectGetLookupHashObj(dstTable); + struct cloneData data =3D { .callback =3D cb, .dst =3D dst, .error =3D= false }; + + if (!src || !dst) + return -1; + + virObjectLock(src); + virHashForEach(useUUID ? src->objsUUID : src->objsName, + cloneCallback, &data); + virObjectUnlock(src); + + if (data.error) + return -1; + + return 0; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index 7e58e34..350929d 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -224,4 +224,14 @@ virObjectLookupHashSearch(void *tableobj, virHashSearcher callback, void *opaque); =20 +typedef int (*virObjectLookupHashCloneCallback)(void *dstTable, + void *srcElem); +int +virObjectLookupHashClone(void *srcTable, + void *dstTable, + bool useUUID, + virObjectLookupHashCloneCallback cb) + ATTRIBUTE_NONNULL(4); + + #endif /* __VIR_OBJECT_H */ --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list