From nobody Mon Feb 9 08:50:37 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1605803223; cv=none; d=zohomail.com; s=zohoarc; b=ICIi+d4CEQVYLlm/FmpQboOeeUghzwjVuWKinNLiZaYntPBQ3V1QMDODCKmUcgAxaxDM2aIhYBLMBFXHFQz1DwBHtuSoWF3HwwiDK9ufHFHchF5vl7LD1jbM6bEWCc3yOpgkjS1NbYuX/HnEBv27ex1fzsTK7qForZmSgcr+kzo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605803223; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=j7gz/Bolx0TilHaIn5BEPhi+dnJCdGSADIot8u5CBsU=; b=FixrPlCqIavEoBK/M72AVfOs3OaDyjbq9cmaJQF5rzfxXC1bcmLDsFQgI/lQ3QUsVZ+iohaWCD/c0K1+tFMhYqfWodFlEPavlxorAxqk3vCCGx67Chzcg2p6C9zyECq+9Wjg0RNh91WCOx1GP2B6mR+ykfcUxH09+pHBgXfq/BQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1605803223723254.39862909623332; Thu, 19 Nov 2020 08:27:03 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-412-IYiH7z_9OiC5scoq4Vy4cg-1; Thu, 19 Nov 2020 11:27:00 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 95093801B1B; Thu, 19 Nov 2020 16:26:54 +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 752FD60BE2; Thu, 19 Nov 2020 16:26:54 +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 3F78C5003E; Thu, 19 Nov 2020 16:26:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0AJGQofR030805 for ; Thu, 19 Nov 2020 11:26:50 -0500 Received: by smtp.corp.redhat.com (Postfix) id 8EF685C1A1; Thu, 19 Nov 2020 16:26:50 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4FCA5C1D1; Thu, 19 Nov 2020 16:26:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605803222; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=j7gz/Bolx0TilHaIn5BEPhi+dnJCdGSADIot8u5CBsU=; b=BtRcT1WeHzEFnGVCoVaWL1NHrPWNMhxwgisTYdwh19Yy/VkC/0lUPJKfP2VzMeluE5AobW bMP+oBXn9hO3ceTzdMJbuP45xCWoRPckbf2l+s+uUJvoxanNw5Y06KDfqfasOy0Heo40Yt 5ZrbtAnvFuiFVhmDiom6dacKnlhL9e0= X-MC-Unique: IYiH7z_9OiC5scoq4Vy4cg-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 12/16] virsh: cmdAttachDisk: Use enum for disk source type Date: Thu, 19 Nov 2020 17:26:18 +0100 Message-Id: <1d083a1f8351c0315017470803ebc646734d85c1.1605802531.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Cc: Ryan Gahagan 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" For extendability and clarity add enum virshAttachDiskSourceType and use it to drive the XML formatting. Signed-off-by: Peter Krempa --- tools/virsh-domain.c | 68 +++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index f8d97593b8..5c7cb994f1 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -519,6 +519,22 @@ cmdAttachDiskFormatAddress(vshControl *ctl, } +enum virshAttachDiskSourceType { + VIRSH_ATTACH_DISK_SOURCE_TYPE_NONE, + VIRSH_ATTACH_DISK_SOURCE_TYPE_FILE, + VIRSH_ATTACH_DISK_SOURCE_TYPE_BLOCK, + + VIRSH_ATTACH_DISK_SOURCE_TYPE_LAST +}; + +VIR_ENUM_DECL(virshAttachDiskSource); +VIR_ENUM_IMPL(virshAttachDiskSource, + VIRSH_ATTACH_DISK_SOURCE_TYPE_LAST, + "", + "file", + "block"); + + static bool cmdAttachDisk(vshControl *ctl, const vshCmd *cmd) { @@ -527,7 +543,7 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd) const char *target =3D NULL; const char *driver =3D NULL; const char *subdriver =3D NULL; - const char *type =3D NULL; + const char *device =3D NULL; const char *mode =3D NULL; const char *iothread =3D NULL; const char *cache =3D NULL; @@ -537,10 +553,10 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd) const char *wwn =3D NULL; const char *targetbus =3D NULL; const char *alias =3D NULL; - bool isBlock =3D false; int ret; unsigned int flags =3D VIR_DOMAIN_AFFECT_CURRENT; const char *stype =3D NULL; + int type =3D VIR_STORAGE_TYPE_NONE; g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; g_autofree char *xml =3D NULL; struct stat st; @@ -564,7 +580,7 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd) vshCommandOptStringReq(ctl, cmd, "target", &target) < 0 || vshCommandOptStringReq(ctl, cmd, "driver", &driver) < 0 || vshCommandOptStringReq(ctl, cmd, "subdriver", &subdriver) < 0 || - vshCommandOptStringReq(ctl, cmd, "type", &type) < 0 || + vshCommandOptStringReq(ctl, cmd, "type", &device) < 0 || vshCommandOptStringReq(ctl, cmd, "mode", &mode) < 0 || vshCommandOptStringReq(ctl, cmd, "iothread", &iothread) < 0 || vshCommandOptStringReq(ctl, cmd, "cache", &cache) < 0 || @@ -577,18 +593,22 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd) vshCommandOptStringReq(ctl, cmd, "sourcetype", &stype) < 0) return false; - if (!stype) { + if (stype && + (type =3D virshAttachDiskSourceTypeFromString(stype)) < 0) { + vshError(ctl, _("Unknown source type: '%s'"), stype); + return false; + } + + if (type =3D=3D VIRSH_ATTACH_DISK_SOURCE_TYPE_NONE) { if (STRNEQ_NULLABLE(driver, "file") && STRNEQ_NULLABLE(driver, "tap") && source && stat(source, &st) =3D=3D 0 && - S_ISBLK(st.st_mode)) - isBlock =3D true; - } else if (STREQ(stype, "block")) { - isBlock =3D true; - } else if (STRNEQ(stype, "file")) { - vshError(ctl, _("Unknown source type: '%s'"), stype); - return false; + S_ISBLK(st.st_mode)) { + type =3D VIRSH_ATTACH_DISK_SOURCE_TYPE_BLOCK; + } else { + type =3D VIRSH_ATTACH_DISK_SOURCE_TYPE_FILE; + } } if (mode) { @@ -604,13 +624,10 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd) /* Make XML of disk */ virBufferAddLit(&buf, "\n"); @@ -633,9 +650,20 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd) virBufferAddLit(&buf, "/>\n"); } - if (source) - virBufferAsprintf(&buf, "\n", - !isBlock ? "file" : "dev", source); + switch ((enum virshAttachDiskSourceType) type) { + case VIRSH_ATTACH_DISK_SOURCE_TYPE_FILE: + virBufferEscapeString(&buf, "\n", source); + break; + + case VIRSH_ATTACH_DISK_SOURCE_TYPE_BLOCK: + virBufferEscapeString(&buf, "\n", source); + break; + + case VIRSH_ATTACH_DISK_SOURCE_TYPE_NONE: + case VIRSH_ATTACH_DISK_SOURCE_TYPE_LAST: + break; + } + virBufferAsprintf(&buf, "