esx: Use g_strdup_printf() instead of virAsprintf()

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2019-10-22 15:26:14 +02:00
parent 2b3061f2a1
commit 8cc297932a
4 changed files with 60 additions and 103 deletions

View File

@ -143,9 +143,8 @@ esxParseVMXFileName(const char *fileName, void *opaque)
if (!strchr(fileName, '/') && !strchr(fileName, '\\')) {
/* Plain file name, use same directory as for the .vmx file */
if (virAsprintf(&result, "%s/%s",
data->datastorePathWithoutFileName, fileName) < 0)
goto cleanup;
result = g_strdup_printf("%s/%s", data->datastorePathWithoutFileName,
fileName);
} else {
if (esxVI_String_AppendValueToList(&propertyNameList,
"summary.name") < 0 ||
@ -189,9 +188,7 @@ esxParseVMXFileName(const char *fileName, void *opaque)
++tmp;
}
if (virAsprintf(&result, "[%s] %s", datastoreName,
strippedFileName) < 0)
goto cleanup;
result = g_strdup_printf("[%s] %s", datastoreName, strippedFileName);
break;
}
@ -225,9 +222,8 @@ esxParseVMXFileName(const char *fileName, void *opaque)
goto cleanup;
}
if (virAsprintf(&result, "[%s] %s", datastoreName,
directoryAndFileName) < 0)
goto cleanup;
result = g_strdup_printf("[%s] %s", datastoreName,
directoryAndFileName);
}
/* If it's an absolute path outside of a datastore just use it as is */
@ -636,9 +632,8 @@ esxConnectToHost(esxPrivate *priv,
conn->uri->server)))
goto cleanup;
if (virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport,
conn->uri->server, conn->uri->port) < 0)
goto cleanup;
url = g_strdup_printf("%s://%s:%d/sdk", priv->parsedUri->transport,
conn->uri->server, conn->uri->port);
if (esxVI_Context_Alloc(&priv->host) < 0 ||
esxVI_Context_Connect(priv->host, url, ipAddress, username, password,
@ -725,9 +720,8 @@ esxConnectToVCenter(esxPrivate *priv,
if (!(password = virAuthGetPassword(conn, auth, "esx", username, hostname)))
goto cleanup;
if (virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport,
hostname, conn->uri->port) < 0)
goto cleanup;
url = g_strdup_printf("%s://%s:%d/sdk", priv->parsedUri->transport, hostname,
conn->uri->port);
if (esxVI_Context_Alloc(&priv->vCenter) < 0 ||
esxVI_Context_Connect(priv->vCenter, url, ipAddress, username,
@ -1161,8 +1155,7 @@ esxConnectGetHostname(virConnectPtr conn)
if (!domainName || strlen(domainName) < 1) {
complete = g_strdup(hostName);
} else {
if (virAsprintf(&complete, "%s.%s", hostName, domainName) < 0)
goto cleanup;
complete = g_strdup_printf("%s.%s", hostName, domainName);
}
cleanup:
@ -2636,13 +2629,10 @@ esxDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
data.ctx = priv->primary;
if (!directoryName) {
if (virAsprintf(&data.datastorePathWithoutFileName, "[%s]",
datastoreName) < 0)
goto cleanup;
data.datastorePathWithoutFileName = g_strdup_printf("[%s]", datastoreName);
} else {
if (virAsprintf(&data.datastorePathWithoutFileName, "[%s] %s",
datastoreName, directoryName) < 0)
goto cleanup;
data.datastorePathWithoutFileName = g_strdup_printf("[%s] %s",
datastoreName, directoryName);
}
ctx.opaque = &data;
@ -3104,13 +3094,11 @@ esxDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
/* Register the domain */
if (directoryName) {
if (virAsprintf(&datastoreRelatedPath, "[%s] %s/%s.vmx", datastoreName,
directoryName, escapedName) < 0)
goto cleanup;
datastoreRelatedPath = g_strdup_printf("[%s] %s/%s.vmx", datastoreName,
directoryName, escapedName);
} else {
if (virAsprintf(&datastoreRelatedPath, "[%s] %s.vmx", datastoreName,
escapedName) < 0)
goto cleanup;
datastoreRelatedPath = g_strdup_printf("[%s] %s.vmx", datastoreName,
escapedName);
}
if (esxVI_RegisterVM_Task(priv->primary, priv->primary->datacenter->vmFolder,
@ -3722,11 +3710,9 @@ esxDomainMigratePrepare(virConnectPtr dconn,
virCheckFlags(ESX_MIGRATION_FLAGS, -1);
if (!uri_in) {
if (virAsprintf(uri_out, "vpxmigr://%s/%s/%s",
priv->vCenter->ipAddress,
priv->vCenter->computeResource->resourcePool->value,
priv->vCenter->hostSystem->_reference->value) < 0)
return -1;
*uri_out = g_strdup_printf("vpxmigr://%s/%s/%s", priv->vCenter->ipAddress,
priv->vCenter->computeResource->resourcePool->value,
priv->vCenter->hostSystem->_reference->value);
}
return 0;

View File

@ -605,9 +605,9 @@ esxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names,
fileInfo = fileInfo->_next) {
if (length < 1) {
names[count] = g_strdup(fileInfo->path);
} else if (virAsprintf(&names[count], "%s/%s", directoryAndFileName,
fileInfo->path) < 0) {
goto cleanup;
} else {
names[count] = g_strdup_printf("%s/%s",
directoryAndFileName, fileInfo->path);
}
++count;
@ -641,8 +641,7 @@ esxStorageVolLookupByName(virStoragePoolPtr pool,
char *datastorePath = NULL;
char *key = NULL;
if (virAsprintf(&datastorePath, "[%s] %s", pool->name, name) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s", pool->name, name);
if (esxVI_LookupStorageVolumeKeyByDatastorePath(priv->primary,
datastorePath, &key) < 0) {
@ -773,15 +772,12 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
if (length < 1) {
volumeName = g_strdup(fileInfo->path);
} else if (virAsprintf(&volumeName, "%s/%s",
directoryAndFileName,
fileInfo->path) < 0) {
goto cleanup;
} else {
volumeName = g_strdup_printf("%s/%s",
directoryAndFileName, fileInfo->path);
}
if (virAsprintf(&datastorePath, "[%s] %s", datastoreName,
volumeName) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s", datastoreName, volumeName);
if (!esxVI_VmDiskFileInfo_DynamicCast(fileInfo)) {
/* Only a VirtualDisk has a UUID */
@ -888,9 +884,7 @@ esxStorageVolCreateXML(virStoragePoolPtr pool,
goto cleanup;
}
if (virAsprintf(&unescapedDatastorePath, "[%s] %s", pool->name,
def->name) < 0)
goto cleanup;
unescapedDatastorePath = g_strdup_printf("[%s] %s", pool->name, def->name);
if (def->target.format == VIR_STORAGE_FILE_VMDK) {
/* Parse and escape datastore path */
@ -911,13 +905,11 @@ esxStorageVolCreateXML(virStoragePoolPtr pool,
if (!fileName)
goto cleanup;
if (virAsprintf(&datastorePathWithoutFileName, "[%s] %s", pool->name,
directoryName) < 0)
goto cleanup;
datastorePathWithoutFileName = g_strdup_printf("[%s] %s", pool->name,
directoryName);
if (virAsprintf(&datastorePath, "[%s] %s/%s", pool->name, directoryName,
fileName) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s/%s", pool->name, directoryName,
fileName);
/* Create directory, if it doesn't exist yet */
if (esxVI_LookupFileInfoByDatastorePath
@ -1074,9 +1066,8 @@ esxStorageVolCreateXMLFrom(virStoragePoolPtr pool,
goto cleanup;
}
if (virAsprintf(&sourceDatastorePath, "[%s] %s", sourceVolume->pool,
sourceVolume->name) < 0)
goto cleanup;
sourceDatastorePath = g_strdup_printf("[%s] %s", sourceVolume->pool,
sourceVolume->name);
/* Parse config */
def = virStorageVolDefParseString(&poolDef, xmldesc, 0);
@ -1107,9 +1098,7 @@ esxStorageVolCreateXMLFrom(virStoragePoolPtr pool,
goto cleanup;
}
if (virAsprintf(&unescapedDatastorePath, "[%s] %s", pool->name,
def->name) < 0)
goto cleanup;
unescapedDatastorePath = g_strdup_printf("[%s] %s", pool->name, def->name);
if (def->target.format == VIR_STORAGE_FILE_VMDK) {
/* Parse and escape datastore path */
@ -1130,13 +1119,11 @@ esxStorageVolCreateXMLFrom(virStoragePoolPtr pool,
if (!fileName)
goto cleanup;
if (virAsprintf(&datastorePathWithoutFileName, "[%s] %s", pool->name,
directoryName) < 0)
goto cleanup;
datastorePathWithoutFileName = g_strdup_printf("[%s] %s", pool->name,
directoryName);
if (virAsprintf(&datastorePath, "[%s] %s/%s", pool->name, directoryName,
fileName) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s/%s", pool->name, directoryName,
fileName);
/* Create directory, if it doesn't exist yet */
if (esxVI_LookupFileInfoByDatastorePath
@ -1231,8 +1218,7 @@ esxStorageVolDelete(virStorageVolPtr volume, unsigned int flags)
virCheckFlags(0, -1);
if (virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
if (esxVI_DeleteVirtualDisk_Task(priv->primary, datastorePath,
priv->primary->datacenter->_reference,
@ -1274,8 +1260,7 @@ esxStorageVolWipe(virStorageVolPtr volume, unsigned int flags)
virCheckFlags(0, -1);
if (virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
if (esxVI_ZeroFillVirtualDisk_Task(priv->primary, datastorePath,
priv->primary->datacenter->_reference,
@ -1317,8 +1302,7 @@ esxStorageVolGetInfo(virStorageVolPtr volume,
memset(info, 0, sizeof(*info));
if (virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
if (esxVI_LookupFileInfoByDatastorePath(priv->primary, datastorePath,
false, &fileInfo,
@ -1375,8 +1359,7 @@ esxStorageVolGetXMLDesc(virStorageVolPtr volume,
}
/* Lookup file info */
if (virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
if (esxVI_LookupFileInfoByDatastorePath(priv->primary, datastorePath,
false, &fileInfo,
@ -1437,7 +1420,7 @@ esxStorageVolGetPath(virStorageVolPtr volume)
{
char *path;
ignore_value(virAsprintf(&path, "[%s] %s", volume->pool, volume->name));
path = g_strdup_printf("[%s] %s", volume->pool, volume->name);
return path;
}

View File

@ -415,11 +415,9 @@ esxStreamOpen(virStreamPtr stream, esxPrivate *priv, const char *url,
streamPriv->mode = mode;
if (length > 0) {
if (virAsprintf(&range, "%llu-%llu", offset, offset + length - 1) < 0)
goto cleanup;
range = g_strdup_printf("%llu-%llu", offset, offset + length - 1);
} else if (offset > 0) {
if (virAsprintf(&range, "%llu-", offset) < 0)
goto cleanup;
range = g_strdup_printf("%llu-", offset);
}
if (esxVI_CURL_Alloc(&streamPriv->curl) < 0 ||
@ -448,9 +446,8 @@ esxStreamOpen(virStreamPtr stream, esxPrivate *priv, const char *url,
curl_easy_setopt(streamPriv->curl->handle, CURLOPT_PASSWORD,
priv->primary->password);
#else
if (virAsprintf(&userpwd, "%s:%s", priv->primary->username,
priv->primary->password) < 0)
goto cleanup;
userpwd = g_strdup_printf("%s:%s", priv->primary->username,
priv->primary->password);
curl_easy_setopt(streamPriv->curl->handle, CURLOPT_USERPWD, userpwd);
#endif

View File

@ -387,11 +387,9 @@ esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content,
return -1;
}
if (virAsprintf(&range, "%llu-%llu", offset, offset + *length - 1) < 0)
goto cleanup;
range = g_strdup_printf("%llu-%llu", offset, offset + *length - 1);
} else if (offset > 0) {
if (virAsprintf(&range, "%llu-", offset) < 0)
goto cleanup;
range = g_strdup_printf("%llu-", offset);
}
virMutexLock(&curl->lock);
@ -1477,10 +1475,8 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
goto cleanup;
} else {
if (virAsprintf(&xpathExpression,
"/soapenv:Envelope/soapenv:Body/vim:%sResponse",
methodName) < 0)
goto cleanup;
xpathExpression = g_strdup_printf("/soapenv:Envelope/soapenv:Body/vim:%sResponse",
methodName);
responseNode = virXPathNode(xpathExpression, xpathContext);
@ -3576,15 +3572,12 @@ esxVI_LookupFileInfoByDatastorePath(esxVI_Context *ctx,
* The <path> part of the datatore path didn't contain a '/', assume
* that the <path> part is actually the file name.
*/
if (virAsprintf(&datastorePathWithoutFileName, "[%s]",
datastoreName) < 0)
goto cleanup;
datastorePathWithoutFileName = g_strdup_printf("[%s]", datastoreName);
fileName = g_strdup(directoryAndFileName);
} else {
if (virAsprintf(&datastorePathWithoutFileName, "[%s] %s",
datastoreName, directoryName) < 0)
goto cleanup;
datastorePathWithoutFileName = g_strdup_printf("[%s] %s", datastoreName,
directoryName);
length = strlen(directoryName);
@ -3810,8 +3803,7 @@ esxVI_LookupDatastoreContentByDatastoreName
floppyImageFileQuery = NULL;
/* Search datastore for files */
if (virAsprintf(&datastorePath, "[%s]", datastoreName) < 0)
goto cleanup;
datastorePath = g_strdup_printf("[%s]", datastoreName);
if (esxVI_SearchDatastoreSubFolders_Task(ctx, hostDatastoreBrowser,
datastorePath, searchSpec,
@ -4526,10 +4518,9 @@ esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
} else if (!taskInfo->error->localizedMessage) {
*errorMessage = g_strdup(taskInfo->error->fault->_actualType);
} else {
if (virAsprintf(errorMessage, "%s - %s",
taskInfo->error->fault->_actualType,
taskInfo->error->localizedMessage) < 0)
goto cleanup;
*errorMessage = g_strdup_printf("%s - %s",
taskInfo->error->fault->_actualType,
taskInfo->error->localizedMessage);
}
}