From nobody Fri May 3 04:22:48 2024 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.zohomail.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 15173969516911006.6957550488245; Wed, 31 Jan 2018 03:09:11 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2EE64693D0; Wed, 31 Jan 2018 11:09:04 +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 48FDA608F1; Wed, 31 Jan 2018 11:09:02 +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 5A6FF3FB1F; Wed, 31 Jan 2018 11:09:00 +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 w0VB8DjX009666 for ; Wed, 31 Jan 2018 06:08:13 -0500 Received: by smtp.corp.redhat.com (Postfix) id EF6535EDEE; Wed, 31 Jan 2018 11:08:12 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4AF125EDE0; Wed, 31 Jan 2018 11:08:12 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 31 Jan 2018 12:07:31 +0100 Message-Id: <2f7111c75b7b1f14963e0f08db7c0774c0ab116e.1517396746.git.pkrempa@redhat.com> 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 Cc: Peter Krempa Subject: [libvirt] [PATCH 1/2] util: json: Add helper to return string or number properties as string 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 31 Jan 2018 11:09:04 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The helper is useful when in cases when JSON we have to parse may return one of the two due to historical reasons and the number value itself would be stored as a string. --- src/util/virjson.c | 27 +++++++++++++++++++++++++++ src/util/virjson.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/util/virjson.c b/src/util/virjson.c index 17b11f2b3d..14b68b8c93 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1208,6 +1208,33 @@ virJSONValueObjectGetString(virJSONValuePtr object, } +/** + * virJSONValueObjectGetStringOrNumber: + * @object: JSON value object + * @key: name of the property in @object to get + * + * Gets a property named @key from the JSON object @object. The value may = be + * a number or a string and is returned as a string. In cases when the pro= perty + * is not present or is not a string or number NULL is returned. + */ +const char * +virJSONValueObjectGetStringOrNumber(virJSONValuePtr object, + const char *key) +{ + virJSONValuePtr val =3D virJSONValueObjectGet(object, key); + + if (!val) + return NULL; + + if (val->type =3D=3D VIR_JSON_TYPE_STRING) + return val->data.string; + else if (val->type =3D=3D VIR_JSON_TYPE_NUMBER) + return val->data.number; + + return NULL; +} + + int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, diff --git a/src/util/virjson.h b/src/util/virjson.h index e89a776ab5..b76a3c3472 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -149,6 +149,7 @@ virJSONValuePtr virJSONValueObjectStealArray(virJSONVal= uePtr object, const char *key); const char *virJSONValueObjectGetString(virJSONValuePtr object, const char= *key); +const char *virJSONValueObjectGetStringOrNumber(virJSONValuePtr object, co= nst char *key); int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key= , int *value); int virJSONValueObjectGetNumberUint(virJSONValuePtr object, const char *ke= y, unsigned int *value); int virJSONValueObjectGetNumberLong(virJSONValuePtr object, const char *ke= y, long long *value); --=20 2.15.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 04:22:48 2024 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.zohomail.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 1517396961439721.3340567044105; Wed, 31 Jan 2018 03:09:21 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E341FC05B012; Wed, 31 Jan 2018 11:09:19 +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 9E7015DA60; Wed, 31 Jan 2018 11:09:19 +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 1AFB43FC73; Wed, 31 Jan 2018 11:09:19 +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 w0VB8EkJ009678 for ; Wed, 31 Jan 2018 06:08:14 -0500 Received: by smtp.corp.redhat.com (Postfix) id 5DBBF5D760; Wed, 31 Jan 2018 11:08:14 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 48B635EDEE; Wed, 31 Jan 2018 11:08:13 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 31 Jan 2018 12:07:32 +0100 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 Cc: Peter Krempa Subject: [libvirt] [PATCH 2/2] util: storage: Parse 'lun' for iSCSI protocol from JSON as string or number 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 31 Jan 2018 11:09:20 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" While the QEMU QAPI schema describes 'lun' as a number, the code dealing with JSON strings does not strictly adhere to this schema and thus formats the number back as a string. Use the new helper to retrieve both possibilities. Note that the formatting code is okay and qemu will accept it as an int. Tweak also one of the test stringst to verify that both formats work with libvirt. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1540290 --- src/util/virstoragefile.c | 13 +++++-------- tests/virstoragetest.c | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 5780180a94..5f661c956c 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -2976,10 +2976,9 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSour= cePtr src, const char *transport =3D virJSONValueObjectGetString(json, "transport= "); const char *portal =3D virJSONValueObjectGetString(json, "portal"); const char *target =3D virJSONValueObjectGetString(json, "target"); + const char *lun =3D virJSONValueObjectGetStringOrNumber(json, "lun"); const char *uri; char *port; - unsigned int lun =3D 0; - char *fulltarget =3D NULL; int ret =3D -1; /* legacy URI based syntax passed via 'filename' option */ @@ -2990,6 +2989,9 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSourc= ePtr src, src->type =3D VIR_STORAGE_TYPE_NETWORK; src->protocol =3D VIR_STORAGE_NET_PROTOCOL_ISCSI; + if (!lun) + lun =3D "0"; + if (VIR_ALLOC(src->hosts) < 0) goto cleanup; @@ -3026,17 +3028,12 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSou= rcePtr src, *port =3D '\0'; } - ignore_value(virJSONValueObjectGetNumberUint(json, "lun", &lun)); - - if (virAsprintf(&fulltarget, "%s/%u", target, lun) < 0) + if (virAsprintf(&src->path, "%s/%s", target, lun) < 0) goto cleanup; - VIR_STEAL_PTR(src->path, fulltarget); - ret =3D 0; cleanup: - VIR_FREE(fulltarget); return ret; } diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index 7a0d4a8260..1dc7608cee 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -1572,7 +1572,7 @@ mymain(void) "\"transport\":\"tcp\"," "\"portal\":\"test.org:1234\"," "\"target\":\"iqn.2016-12.com.virtt= est:emulated-iscsi-noauth.target\"," - "\"lun\":6" + "\"lun\":\"6\"" "}" "}", "\n" --=20 2.15.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list