From nobody Sat Feb 7 09:46: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=1609862129; cv=none; d=zohomail.com; s=zohoarc; b=SFmacAw6dHEcUN1rYGikV+GwCPG04/wwRVugIJmOTPw2yOhv6jgbkZik3AskI1GNW2wxZiAY9smu36mmpt1SezE40EE+KISKa5vV70/HJRaST361gHxa3G+6bn68M+jPX9Z/gO/5wFpp1wSBqJeaE5mGw0FfKZ2s2/nSftXmD2A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1609862129; h=Content-Type:Content-Transfer-Encoding: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=pLcZQWIe9paDDId4CM617P7krrELrnn8OyWKxr41fLU=; b=iLX2yp2M1AZ7AIFmOVJHIyzzM6TN0O7L1jNXpC+2FixXa/W4OyXPYl5Rm5ojeetxh4PHKxtE3x4QmLkdeamt8ZhfTc4ASPTN2Qg7JQWoO2Dxv8XPInOuiOzJAdgkCW/vLDjtKMIbKj8hhRMynw2wPwl5l5NshFZzb2GFnYujjB0= 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 1609862129065334.18542292250186; Tue, 5 Jan 2021 07:55:29 -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-78-haOctMUfNHaiJCu48__5HA-1; Tue, 05 Jan 2021 10:55:23 -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 B5EA680362B; Tue, 5 Jan 2021 15:55:17 +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 92F5760C15; Tue, 5 Jan 2021 15:55:17 +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 5B4C44E590; Tue, 5 Jan 2021 15:55:17 +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 105FsrCH020953 for ; Tue, 5 Jan 2021 10:54:53 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1BA845D6CF; Tue, 5 Jan 2021 15:54:53 +0000 (UTC) Received: from carol.localdomain (unknown [10.40.193.241]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9060D5D6D3 for ; Tue, 5 Jan 2021 15:54:49 +0000 (UTC) Received: from carol.redhat.com (carol.k8r.cz [127.0.0.1]) by carol.localdomain (Postfix) with ESMTP id DA922C201C4 for ; Tue, 5 Jan 2021 16:54:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1609862126; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to: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=pLcZQWIe9paDDId4CM617P7krrELrnn8OyWKxr41fLU=; b=O3Too5T05YMFwxBZhI596adC/I2G4iaRuCwDBBJsBl6W+a3beagySjFif5qLKLYxXFOmz1 czIKLhcxx4Q7unmGEpzZN0KDllO3tDERPuepOxokt5S1LYIxHBNTl8ZJNuCstNImoeuZ21 yZoMDuKrOwVB7ZtkIzmx/Y0tC/qR4GA= X-MC-Unique: haOctMUfNHaiJCu48__5HA-1 From: Martin Kletzander To: libvir-list@redhat.com Subject: [libvirt PATCH v3 3/8] vmx: Make virVMXParseFileName return an integer Date: Tue, 5 Jan 2021 16:54:42 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com 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" And return the actual extracted value in a parameter. This way we can later return success even without any extracted value. Signed-off-by: Martin Kletzander --- src/esx/esx_driver.c | 31 ++++++++++++++++++------------- src/vmware/vmware_conf.c | 20 ++++++++++++++------ src/vmware/vmware_conf.h | 9 ++++++++- src/vmware/vmware_driver.c | 6 +++--- src/vmx/vmx.c | 25 ++++++++++++------------- src/vmx/vmx.h | 2 +- tests/vmx2xmltest.c | 21 ++++++++++++--------- 7 files changed, 68 insertions(+), 46 deletions(-) diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 51c26e29c65e..86d5396147a3 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -125,10 +125,11 @@ esxFreePrivate(esxPrivate **priv) * exception and need special handling. Parse the datastore name and use it * to lookup the datastore by name to verify that it exists. */ -static char * -esxParseVMXFileName(const char *fileName, void *opaque) +static int +esxParseVMXFileName(const char *fileName, + void *opaque, + char **out) { - char *result =3D NULL; esxVMX_Data *data =3D opaque; esxVI_String *propertyNameList =3D NULL; esxVI_ObjectContent *datastoreList =3D NULL; @@ -140,18 +141,22 @@ esxParseVMXFileName(const char *fileName, void *opaqu= e) char *strippedFileName =3D NULL; char *copyOfFileName =3D NULL; char *directoryAndFileName; + int ret =3D -1; + + *out =3D NULL; =20 if (!strchr(fileName, '/') && !strchr(fileName, '\\')) { /* Plain file name, use same directory as for the .vmx file */ - return g_strdup_printf("%s/%s", data->datastorePathWithoutFileName, + *out =3D g_strdup_printf("%s/%s", data->datastorePathWithoutFileNa= me, fileName); + return 0; } =20 if (esxVI_String_AppendValueToList(&propertyNameList, "summary.name") < 0 || esxVI_LookupDatastoreList(data->ctx, propertyNameList, &datastoreList) < 0) { - return NULL; + return -1; } =20 /* Search for datastore by mount path */ @@ -189,13 +194,13 @@ esxParseVMXFileName(const char *fileName, void *opaqu= e) ++tmp; } =20 - result =3D g_strdup_printf("[%s] %s", datastoreName, strippedFileN= ame); + *out =3D g_strdup_printf("[%s] %s", datastoreName, strippedFileNam= e); =20 break; } =20 /* Fallback to direct datastore name match */ - if (!result && STRPREFIX(fileName, "/vmfs/volumes/")) { + if (!*out && STRPREFIX(fileName, "/vmfs/volumes/")) { copyOfFileName =3D g_strdup(fileName); =20 /* Expected format: '/vmfs/volumes//' */ @@ -223,22 +228,22 @@ esxParseVMXFileName(const char *fileName, void *opaqu= e) goto cleanup; } =20 - result =3D g_strdup_printf("[%s] %s", datastoreName, - directoryAndFileName); + *out =3D g_strdup_printf("[%s] %s", datastoreName, directoryAndFil= eName); } =20 /* If it's an absolute path outside of a datastore just use it as is */ - if (!result && *fileName =3D=3D '/') { + if (!*out && *fileName =3D=3D '/') { /* FIXME: need to deal with Windows paths here too */ - result =3D g_strdup(fileName); + *out =3D g_strdup(fileName); } =20 - if (!result) { + if (!*out) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not handle file name '%s'"), fileName); goto cleanup; } =20 + ret =3D 0; cleanup: esxVI_String_Free(&propertyNameList); esxVI_ObjectContent_Free(&datastoreList); @@ -246,7 +251,7 @@ esxParseVMXFileName(const char *fileName, void *opaque) VIR_FREE(strippedFileName); VIR_FREE(copyOfFileName); =20 - return result; + return ret; } =20 =20 diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c index e44673247ff1..4f7dc3001d2b 100644 --- a/src/vmware/vmware_conf.c +++ b/src/vmware/vmware_conf.c @@ -139,7 +139,7 @@ vmwareLoadDomains(struct vmware_driver *driver) char *saveptr =3D NULL; virCommandPtr cmd; =20 - ctx.parseFileName =3D vmwareCopyVMXFileName; + ctx.parseFileName =3D vmwareParseVMXFileName; ctx.formatFileName =3D NULL; ctx.autodetectSCSIControllerModel =3D NULL; ctx.datacenterPath =3D NULL; @@ -507,11 +507,19 @@ vmwareExtractPid(const char * vmxPath) return pid_value; } =20 -char * -vmwareCopyVMXFileName(const char *datastorePath, void *opaque G_GNUC_UNUSE= D) +int +vmwareParseVMXFileName(const char *datastorePath, + void *opaque G_GNUC_UNUSED, + char **out) { - char *path; + *out =3D g_strdup(datastorePath); + + return *out ? 0 : -1; +} =20 - path =3D g_strdup(datastorePath); - return path; +char * +vmwareFormatVMXFileName(const char *datastorePath, + void *opaque G_GNUC_UNUSED) +{ + return g_strdup(datastorePath); } diff --git a/src/vmware/vmware_conf.h b/src/vmware/vmware_conf.h index 5e0ef3744f76..6f86983f511f 100644 --- a/src/vmware/vmware_conf.h +++ b/src/vmware/vmware_conf.h @@ -83,4 +83,11 @@ int vmwareMakePath(char *srcDir, char *srcName, char *sr= cExt, =20 int vmwareExtractPid(const char * vmxPath); =20 -char *vmwareCopyVMXFileName(const char *datastorePath, void *opaque); +int +vmwareParseVMXFileName(const char *datastorePath, + void *opaque, + char **out); + +char * +vmwareFormatVMXFileName(const char *datastorePath, + void *opaque); diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c index 087664a34185..481c59ef3cce 100644 --- a/src/vmware/vmware_driver.c +++ b/src/vmware/vmware_driver.c @@ -409,7 +409,7 @@ vmwareDomainDefineXMLFlags(virConnectPtr conn, const ch= ar *xml, unsigned int fla parse_flags |=3D VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA; =20 ctx.parseFileName =3D NULL; - ctx.formatFileName =3D vmwareCopyVMXFileName; + ctx.formatFileName =3D vmwareFormatVMXFileName; ctx.autodetectSCSIControllerModel =3D NULL; ctx.datacenterPath =3D NULL; =20 @@ -662,7 +662,7 @@ vmwareDomainCreateXML(virConnectPtr conn, const char *x= ml, parse_flags |=3D VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA; =20 ctx.parseFileName =3D NULL; - ctx.formatFileName =3D vmwareCopyVMXFileName; + ctx.formatFileName =3D vmwareFormatVMXFileName; ctx.autodetectSCSIControllerModel =3D NULL; ctx.datacenterPath =3D NULL; =20 @@ -950,7 +950,7 @@ vmwareConnectDomainXMLFromNative(virConnectPtr conn, co= nst char *nativeFormat, return NULL; } =20 - ctx.parseFileName =3D vmwareCopyVMXFileName; + ctx.parseFileName =3D vmwareParseVMXFileName; ctx.formatFileName =3D NULL; ctx.autodetectSCSIControllerModel =3D NULL; ctx.datacenterPath =3D NULL; diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c index b86dbe9ca267..97591842f789 100644 --- a/src/vmx/vmx.c +++ b/src/vmx/vmx.c @@ -2388,7 +2388,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptio= nPtr xmlopt, virConfPtr con */ goto ignore; } else if (virStringHasCaseSuffix(fileName, ".vmdk")) { - char *tmp; + char *tmp =3D NULL; =20 if (deviceType !=3D NULL) { if (busType =3D=3D VIR_DOMAIN_DISK_BUS_SCSI && @@ -2411,7 +2411,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptio= nPtr xmlopt, virConfPtr con } =20 virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE); - if (!(tmp =3D ctx->parseFileName(fileName, ctx->opaque))) + if (ctx->parseFileName(fileName, ctx->opaque, &tmp) < 0) goto cleanup; virDomainDiskSetSource(*def, tmp); VIR_FREE(tmp); @@ -2438,7 +2438,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptio= nPtr xmlopt, virConfPtr con */ goto ignore; } else if (fileName && virStringHasCaseSuffix(fileName, ".iso")) { - char *tmp; + char *tmp =3D NULL; =20 if (deviceType && STRCASENEQ(deviceType, "cdrom-image")) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -2448,7 +2448,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptio= nPtr xmlopt, virConfPtr con } =20 virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE); - if (!(tmp =3D ctx->parseFileName(fileName, ctx->opaque))) + if (ctx->parseFileName(fileName, ctx->opaque, &tmp) < 0) goto cleanup; virDomainDiskSetSource(*def, tmp); VIR_FREE(tmp); @@ -2514,7 +2514,8 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptio= nPtr xmlopt, virConfPtr con char *tmp =3D NULL; =20 virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE); - if (fileName && !(tmp =3D ctx->parseFileName(fileName, ctx->op= aque))) + if (fileName && + ctx->parseFileName(fileName, ctx->opaque, &tmp) < 0) goto cleanup; virDomainDiskSetSource(*def, tmp); VIR_FREE(tmp); @@ -2974,10 +2975,9 @@ virVMXParseSerial(virVMXContext *ctx, virConfPtr con= f, int port, } else if (STRCASEEQ(fileType, "file")) { (*def)->target.port =3D port; (*def)->source->type =3D VIR_DOMAIN_CHR_TYPE_FILE; - (*def)->source->data.file.path =3D ctx->parseFileName(fileName, - ctx->opaque); - - if ((*def)->source->data.file.path =3D=3D NULL) + if (ctx->parseFileName(fileName, + ctx->opaque, + &(*def)->source->data.file.path) < 0) goto cleanup; } else if (STRCASEEQ(fileType, "pipe")) { /* @@ -3140,10 +3140,9 @@ virVMXParseParallel(virVMXContext *ctx, virConfPtr c= onf, int port, } else if (STRCASEEQ(fileType, "file")) { (*def)->target.port =3D port; (*def)->source->type =3D VIR_DOMAIN_CHR_TYPE_FILE; - (*def)->source->data.file.path =3D ctx->parseFileName(fileName, - ctx->opaque); - - if ((*def)->source->data.file.path =3D=3D NULL) + if (ctx->parseFileName(fileName, + ctx->opaque, + &(*def)->source->data.file.path) < 0) goto cleanup; } else { virReportError(VIR_ERR_INTERNAL_ERROR, diff --git a/src/vmx/vmx.h b/src/vmx/vmx.h index df5d39157b99..e5420c970a4b 100644 --- a/src/vmx/vmx.h +++ b/src/vmx/vmx.h @@ -36,7 +36,7 @@ virDomainXMLOptionPtr virVMXDomainXMLConfInit(virCapsPtr = caps); * Context */ =20 -typedef char * (*virVMXParseFileName)(const char *fileName, void *opaque); +typedef int (*virVMXParseFileName)(const char *fileName, void *opaque, cha= r **src); typedef char * (*virVMXFormatFileName)(const char *src, void *opaque); typedef int (*virVMXAutodetectSCSIControllerModel)(virDomainDiskDefPtr def, int *model, void *opaqu= e); diff --git a/tests/vmx2xmltest.c b/tests/vmx2xmltest.c index 8cc227bbedfc..bb7c498d1b41 100644 --- a/tests/vmx2xmltest.c +++ b/tests/vmx2xmltest.c @@ -127,15 +127,18 @@ testCompareHelper(const void *data) return ret; } =20 -static char * -testParseVMXFileName(const char *fileName, void *opaque G_GNUC_UNUSED) +static int +testParseVMXFileName(const char *fileName, + void *opaque G_GNUC_UNUSED, + char **src) { g_autofree char *copyOfFileName =3D NULL; char *tmp =3D NULL; char *saveptr =3D NULL; char *datastoreName =3D NULL; char *directoryAndFileName =3D NULL; - char *src =3D NULL; + + *src =3D NULL; =20 if (STRPREFIX(fileName, "/vmfs/volumes/")) { /* Found absolute path referencing a file inside a datastore */ @@ -145,22 +148,22 @@ testParseVMXFileName(const char *fileName, void *opaq= ue G_GNUC_UNUSED) if ((tmp =3D STRSKIP(copyOfFileName, "/vmfs/volumes/")) =3D=3D NUL= L || (datastoreName =3D strtok_r(tmp, "/", &saveptr)) =3D=3D NULL || (directoryAndFileName =3D strtok_r(NULL, "", &saveptr)) =3D=3D= NULL) { - return NULL; + return -1; } =20 - src =3D g_strdup_printf("[%s] %s", datastoreName, directoryAndFile= Name); + *src =3D g_strdup_printf("[%s] %s", datastoreName, directoryAndFil= eName); } else if (STRPREFIX(fileName, "/")) { /* Found absolute path referencing a file outside a datastore */ - src =3D g_strdup(fileName); + *src =3D g_strdup(fileName); } else if (strchr(fileName, '/') !=3D NULL) { /* Found relative path, this is not supported */ - return NULL; + return -1; } else { /* Found single file name referencing a file inside a datastore */ - src =3D g_strdup_printf("[datastore] directory/%s", fileName); + *src =3D g_strdup_printf("[datastore] directory/%s", fileName); } =20 - return src; + return 0; } =20 static int --=20 2.30.0