From nobody Mon Feb 9 12:11:15 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 149856763786828.387499675549293; Tue, 27 Jun 2017 05:47:17 -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 466F680481; Tue, 27 Jun 2017 12:47:13 +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 EE08C7F367; Tue, 27 Jun 2017 12:47:12 +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 435D41853E33; Tue, 27 Jun 2017 12:47:12 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5RCl2TU023776 for ; Tue, 27 Jun 2017 08:47:02 -0400 Received: by smtp.corp.redhat.com (Postfix) id 441478A765; Tue, 27 Jun 2017 12:47:02 +0000 (UTC) Received: from andariel.redhat.com (ovpn-204-24.brq.redhat.com [10.40.204.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 69ADF8A785; Tue, 27 Jun 2017 12:47:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 466F680481 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 466F680481 From: Peter Krempa To: libvir-list@redhat.com Date: Tue, 27 Jun 2017 14:46:44 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 3/9] util: Move JSON object deflattening code to json utility file 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.28]); Tue, 27 Jun 2017 12:47:14 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The code will become more universal so it makes more sense for it to live with the rest of the JSON functions. Reviewed-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virjson.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ src/util/virjson.h | 2 ++ src/util/virstoragefile.c | 65 +------------------------------------------= ---- 4 files changed, 65 insertions(+), 64 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d487b1f43..9f64b8d93 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1931,6 +1931,7 @@ virJSONValueObjectAppendNumberUlong; virJSONValueObjectAppendString; virJSONValueObjectCreate; virJSONValueObjectCreateVArgs; +virJSONValueObjectDeflatten; virJSONValueObjectForeachKeyValue; virJSONValueObjectGet; virJSONValueObjectGetArray; diff --git a/src/util/virjson.c b/src/util/virjson.c index efd6c3a0e..8ab542432 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1965,3 +1965,64 @@ virJSONStringReformat(const char *jsonstr, virJSONValueFree(json); return ret; } + + +static int +virJSONValueObjectDeflattenWorker(const char *key, + virJSONValuePtr value, + void *opaque) +{ + virJSONValuePtr retobj =3D opaque; + virJSONValuePtr newval =3D NULL; + const char *newkey; + + if (!(newkey =3D STRSKIP(key, "file."))) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("JSON object is neither nested nor flattened")); + return -1; + } + + if (!(newval =3D virJSONValueCopy(value))) + return -1; + + if (virJSONValueObjectAppend(retobj, newkey, newval) < 0) { + virJSONValueFree(newval); + return -1; + } + + return 0; +} + + +/** + * virJSONValueObjectDeflatten: + * + * In some cases it's possible to nest JSON objects by prefixing object me= mbers + * with the parent object name followed by the dot and then the attribute = name + * rather than directly using a nested value object (e.g qemu's JSON + * pseudo-protocol in backing file definition). + * + * This function will attempt to reverse the process and provide a nested = json + * hierarchy so that the parsers can be kept simple and we still can use t= he + * weird syntax some users might use. + * + * Currently this function will flatten out just the 'file.' prefix into a= new + * tree. Any other syntax will be rejected. + */ +virJSONValuePtr +virJSONValueObjectDeflatten(virJSONValuePtr json) +{ + virJSONValuePtr ret; + + if (!(ret =3D virJSONValueNewObject())) + return NULL; + + if (virJSONValueObjectForeachKeyValue(json, + virJSONValueObjectDeflattenWorke= r, + ret) < 0) { + virJSONValueFree(ret); + return NULL; + } + + return ret; +} diff --git a/src/util/virjson.h b/src/util/virjson.h index c9d9752de..e89a776ab 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -186,4 +186,6 @@ virJSONValuePtr virJSONValueCopy(const virJSONValue *in= ); char *virJSONStringReformat(const char *jsonstr, bool pretty); +virJSONValuePtr virJSONValueObjectDeflatten(virJSONValuePtr json); + #endif /* __VIR_JSON_H_ */ diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index f0ed5c6bd..52c5301ff 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -3244,69 +3244,6 @@ static const struct virStorageSourceJSONDriverParser= jsonParsers[] =3D { }; -static int -virStorageSourceParseBackingJSONDeflattenWorker(const char *key, - virJSONValuePtr value, - void *opaque) -{ - virJSONValuePtr retobj =3D opaque; - virJSONValuePtr newval =3D NULL; - const char *newkey; - - if (!(newkey =3D STRSKIP(key, "file."))) { - virReportError(VIR_ERR_INVALID_ARG, "%s", - _("JSON backing file syntax is neither nested nor " - "flattened")); - return -1; - } - - if (!(newval =3D virJSONValueCopy(value))) - return -1; - - if (virJSONValueObjectAppend(retobj, newkey, newval) < 0) { - virJSONValueFree(newval); - return -1; - } - - return 0; -} - - -/** - * virStorageSourceParseBackingJSONDeflatten: - * - * The json: pseudo-protocol syntax in qemu allows multiple approaches to - * describe nesting of the values. This is due to the lax handling of the = string - * in qemu and the fact that internally qemu is flattening the values usin= g '.'. - * - * This allows to specify nested json strings either using nested json obj= ects - * or prefixing object members with the parent object name followed by the= dot. - * - * This function will attempt to reverse the process and provide a nested = json - * hierarchy so that the parsers can be kept simple and we still can use t= he - * weird syntax some users might use. - * - * Currently this function will flatten out just the 'file.' prefix into a= new - * tree. Any other syntax will be rejected. - */ -static virJSONValuePtr -virStorageSourceParseBackingJSONDeflatten(virJSONValuePtr json) -{ - virJSONValuePtr ret; - - if (!(ret =3D virJSONValueNewObject())) - return NULL; - - if (virJSONValueObjectForeachKeyValue(json, - virStorageSourceParseBackingJSON= DeflattenWorker, - ret) < 0) { - virJSONValueFree(ret); - return NULL; - } - - return ret; -} - static int virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src, @@ -3320,7 +3257,7 @@ virStorageSourceParseBackingJSONInternal(virStorageSo= urcePtr src, int ret =3D -1; if (!(file =3D virJSONValueObjectGetObject(json, "file"))) { - if (!(fixedroot =3D virStorageSourceParseBackingJSONDeflatten(json= ))) + if (!(fixedroot =3D virJSONValueObjectDeflatten(json))) goto cleanup; file =3D fixedroot; --=20 2.12.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list