[libvirt] [PATCH 41/75] esx: Use g_strdup_printf() instead of virAsprintf()

Michal Privoznik mprivozn at redhat.com
Tue Oct 22 13:57:45 UTC 2019


Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/esx/esx_driver.c               | 40 +++++++++++++-------------
 src/esx/esx_storage_backend_vmfs.c | 46 +++++++++++++++---------------
 src/esx/esx_stream.c               |  8 +++---
 src/esx/esx_vi.c                   | 23 +++++++--------
 4 files changed, 58 insertions(+), 59 deletions(-)

diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index 117ac674d4..8117c454e4 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -143,8 +143,8 @@ esxParseVMXFileName(const char *fileName, void *opaque)
 
     if (!strchr(fileName, '/') && !strchr(fileName, '\\')) {
         /* Plain file name, use same directory as for the .vmx file */
-        virAsprintf(&result, "%s/%s", data->datastorePathWithoutFileName,
-                    fileName);
+        result = g_strdup_printf("%s/%s", data->datastorePathWithoutFileName,
+                                 fileName);
     } else {
         if (esxVI_String_AppendValueToList(&propertyNameList,
                                            "summary.name") < 0 ||
@@ -188,7 +188,7 @@ esxParseVMXFileName(const char *fileName, void *opaque)
                 ++tmp;
             }
 
-            virAsprintf(&result, "[%s] %s", datastoreName, strippedFileName);
+            result = g_strdup_printf("[%s] %s", datastoreName, strippedFileName);
 
             break;
         }
@@ -222,8 +222,8 @@ esxParseVMXFileName(const char *fileName, void *opaque)
                 goto cleanup;
             }
 
-            virAsprintf(&result, "[%s] %s", datastoreName,
-                        directoryAndFileName);
+            result = g_strdup_printf("[%s] %s", datastoreName,
+                                     directoryAndFileName);
         }
 
         /* If it's an absolute path outside of a datastore just use it as is */
@@ -635,8 +635,8 @@ esxConnectToHost(esxPrivate *priv,
                                         conn->uri->server)))
         goto cleanup;
 
-    virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport,
-                conn->uri->server, conn->uri->port);
+    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,
@@ -723,8 +723,8 @@ esxConnectToVCenter(esxPrivate *priv,
     if (!(password = virAuthGetPassword(conn, auth, "esx", username, hostname)))
         goto cleanup;
 
-    virAsprintf(&url, "%s://%s:%d/sdk", priv->parsedUri->transport, hostname,
-                conn->uri->port);
+    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,
@@ -1158,7 +1158,7 @@ esxConnectGetHostname(virConnectPtr conn)
     if (!domainName || strlen(domainName) < 1) {
         complete = g_strdup(hostName);
     } else {
-        virAsprintf(&complete, "%s.%s", hostName, domainName);
+        complete = g_strdup_printf("%s.%s", hostName, domainName);
     }
 
  cleanup:
@@ -2638,10 +2638,10 @@ esxDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
     data.ctx = priv->primary;
 
     if (!directoryName) {
-        virAsprintf(&data.datastorePathWithoutFileName, "[%s]", datastoreName);
+        data.datastorePathWithoutFileName = g_strdup_printf("[%s]", datastoreName);
     } else {
-        virAsprintf(&data.datastorePathWithoutFileName, "[%s] %s",
-                    datastoreName, directoryName);
+        data.datastorePathWithoutFileName = g_strdup_printf("[%s] %s",
+                                                            datastoreName, directoryName);
     }
 
     ctx.opaque = &data;
@@ -3106,11 +3106,11 @@ esxDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
 
     /* Register the domain */
     if (directoryName) {
-        virAsprintf(&datastoreRelatedPath, "[%s] %s/%s.vmx", datastoreName,
-                    directoryName, escapedName);
+        datastoreRelatedPath = g_strdup_printf("[%s] %s/%s.vmx", datastoreName,
+                                               directoryName, escapedName);
     } else {
-        virAsprintf(&datastoreRelatedPath, "[%s] %s.vmx", datastoreName,
-                    escapedName);
+        datastoreRelatedPath = g_strdup_printf("[%s] %s.vmx", datastoreName,
+                                               escapedName);
     }
 
     if (esxVI_RegisterVM_Task(priv->primary, priv->primary->datacenter->vmFolder,
@@ -3722,9 +3722,9 @@ esxDomainMigratePrepare(virConnectPtr dconn,
     virCheckFlags(ESX_MIGRATION_FLAGS, -1);
 
     if (!uri_in) {
-        virAsprintf(uri_out, "vpxmigr://%s/%s/%s", priv->vCenter->ipAddress,
-                    priv->vCenter->computeResource->resourcePool->value,
-                    priv->vCenter->hostSystem->_reference->value);
+        *uri_out = g_strdup_printf("vpxmigr://%s/%s/%s", priv->vCenter->ipAddress,
+                                   priv->vCenter->computeResource->resourcePool->value,
+                                   priv->vCenter->hostSystem->_reference->value);
     }
 
     return 0;
diff --git a/src/esx/esx_storage_backend_vmfs.c b/src/esx/esx_storage_backend_vmfs.c
index 24deee7215..5f25f80072 100644
--- a/src/esx/esx_storage_backend_vmfs.c
+++ b/src/esx/esx_storage_backend_vmfs.c
@@ -606,8 +606,8 @@ esxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names,
             if (length < 1) {
                 names[count] = g_strdup(fileInfo->path);
             } else {
-                virAsprintf(&names[count], "%s/%s",
-                            directoryAndFileName, fileInfo->path);
+                names[count] = g_strdup_printf("%s/%s",
+                                               directoryAndFileName, fileInfo->path);
             }
 
             ++count;
@@ -641,7 +641,7 @@ esxStorageVolLookupByName(virStoragePoolPtr pool,
     char *datastorePath = NULL;
     char *key = NULL;
 
-    virAsprintf(&datastorePath, "[%s] %s", pool->name, name);
+    datastorePath = g_strdup_printf("[%s] %s", pool->name, name);
 
     if (esxVI_LookupStorageVolumeKeyByDatastorePath(priv->primary,
                                                     datastorePath, &key) < 0) {
@@ -773,11 +773,11 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
                 if (length < 1) {
                     volumeName = g_strdup(fileInfo->path);
                 } else {
-                    virAsprintf(&volumeName, "%s/%s",
-                                directoryAndFileName, fileInfo->path);
+                    volumeName = g_strdup_printf("%s/%s",
+                                                 directoryAndFileName, fileInfo->path);
                 }
 
-                virAsprintf(&datastorePath, "[%s] %s", datastoreName, volumeName);
+                datastorePath = g_strdup_printf("[%s] %s", datastoreName, volumeName);
 
                 if (!esxVI_VmDiskFileInfo_DynamicCast(fileInfo)) {
                     /* Only a VirtualDisk has a UUID */
@@ -884,7 +884,7 @@ esxStorageVolCreateXML(virStoragePoolPtr pool,
         goto cleanup;
     }
 
-    virAsprintf(&unescapedDatastorePath, "[%s] %s", pool->name, def->name);
+    unescapedDatastorePath = g_strdup_printf("[%s] %s", pool->name, def->name);
 
     if (def->target.format == VIR_STORAGE_FILE_VMDK) {
         /* Parse and escape datastore path */
@@ -905,11 +905,11 @@ esxStorageVolCreateXML(virStoragePoolPtr pool,
         if (!fileName)
             goto cleanup;
 
-        virAsprintf(&datastorePathWithoutFileName, "[%s] %s", pool->name,
-                    directoryName);
+        datastorePathWithoutFileName = g_strdup_printf("[%s] %s", pool->name,
+                                                       directoryName);
 
-        virAsprintf(&datastorePath, "[%s] %s/%s", pool->name, directoryName,
-                    fileName);
+        datastorePath = g_strdup_printf("[%s] %s/%s", pool->name, directoryName,
+                                        fileName);
 
         /* Create directory, if it doesn't exist yet */
         if (esxVI_LookupFileInfoByDatastorePath
@@ -1066,8 +1066,8 @@ esxStorageVolCreateXMLFrom(virStoragePoolPtr pool,
         goto cleanup;
     }
 
-    virAsprintf(&sourceDatastorePath, "[%s] %s", sourceVolume->pool,
-                sourceVolume->name);
+    sourceDatastorePath = g_strdup_printf("[%s] %s", sourceVolume->pool,
+                                          sourceVolume->name);
 
     /* Parse config */
     def = virStorageVolDefParseString(&poolDef, xmldesc, 0);
@@ -1098,7 +1098,7 @@ esxStorageVolCreateXMLFrom(virStoragePoolPtr pool,
         goto cleanup;
     }
 
-    virAsprintf(&unescapedDatastorePath, "[%s] %s", pool->name, def->name);
+    unescapedDatastorePath = g_strdup_printf("[%s] %s", pool->name, def->name);
 
     if (def->target.format == VIR_STORAGE_FILE_VMDK) {
         /* Parse and escape datastore path */
@@ -1119,11 +1119,11 @@ esxStorageVolCreateXMLFrom(virStoragePoolPtr pool,
         if (!fileName)
             goto cleanup;
 
-        virAsprintf(&datastorePathWithoutFileName, "[%s] %s", pool->name,
-                    directoryName);
+        datastorePathWithoutFileName = g_strdup_printf("[%s] %s", pool->name,
+                                                       directoryName);
 
-        virAsprintf(&datastorePath, "[%s] %s/%s", pool->name, directoryName,
-                    fileName);
+        datastorePath = g_strdup_printf("[%s] %s/%s", pool->name, directoryName,
+                                        fileName);
 
         /* Create directory, if it doesn't exist yet */
         if (esxVI_LookupFileInfoByDatastorePath
@@ -1218,7 +1218,7 @@ esxStorageVolDelete(virStorageVolPtr volume, unsigned int flags)
 
     virCheckFlags(0, -1);
 
-    virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name);
+    datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
 
     if (esxVI_DeleteVirtualDisk_Task(priv->primary, datastorePath,
                                      priv->primary->datacenter->_reference,
@@ -1260,7 +1260,7 @@ esxStorageVolWipe(virStorageVolPtr volume, unsigned int flags)
 
     virCheckFlags(0, -1);
 
-    virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name);
+    datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
 
     if (esxVI_ZeroFillVirtualDisk_Task(priv->primary, datastorePath,
                                        priv->primary->datacenter->_reference,
@@ -1302,7 +1302,7 @@ esxStorageVolGetInfo(virStorageVolPtr volume,
 
     memset(info, 0, sizeof(*info));
 
-    virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name);
+    datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
 
     if (esxVI_LookupFileInfoByDatastorePath(priv->primary, datastorePath,
                                             false, &fileInfo,
@@ -1359,7 +1359,7 @@ esxStorageVolGetXMLDesc(virStorageVolPtr volume,
     }
 
     /* Lookup file info */
-    virAsprintf(&datastorePath, "[%s] %s", volume->pool, volume->name);
+    datastorePath = g_strdup_printf("[%s] %s", volume->pool, volume->name);
 
     if (esxVI_LookupFileInfoByDatastorePath(priv->primary, datastorePath,
                                             false, &fileInfo,
@@ -1420,7 +1420,7 @@ esxStorageVolGetPath(virStorageVolPtr volume)
 {
     char *path;
 
-    virAsprintf(&path, "[%s] %s", volume->pool, volume->name);
+    path = g_strdup_printf("[%s] %s", volume->pool, volume->name);
     return path;
 }
 
diff --git a/src/esx/esx_stream.c b/src/esx/esx_stream.c
index 1a779aa095..fe3c42ae02 100644
--- a/src/esx/esx_stream.c
+++ b/src/esx/esx_stream.c
@@ -415,9 +415,9 @@ esxStreamOpen(virStreamPtr stream, esxPrivate *priv, const char *url,
     streamPriv->mode = mode;
 
     if (length > 0) {
-        virAsprintf(&range, "%llu-%llu", offset, offset + length - 1);
+        range = g_strdup_printf("%llu-%llu", offset, offset + length - 1);
     } else if (offset > 0) {
-        virAsprintf(&range, "%llu-", offset);
+        range = g_strdup_printf("%llu-", offset);
     }
 
     if (esxVI_CURL_Alloc(&streamPriv->curl) < 0 ||
@@ -446,8 +446,8 @@ esxStreamOpen(virStreamPtr stream, esxPrivate *priv, const char *url,
     curl_easy_setopt(streamPriv->curl->handle, CURLOPT_PASSWORD,
                      priv->primary->password);
 #else
-    virAsprintf(&userpwd, "%s:%s", priv->primary->username,
-                priv->primary->password);
+    userpwd = g_strdup_printf("%s:%s", priv->primary->username,
+                              priv->primary->password);
 
     curl_easy_setopt(streamPriv->curl->handle, CURLOPT_USERPWD, userpwd);
 #endif
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
index 61082c5113..3ff25e2a93 100644
--- a/src/esx/esx_vi.c
+++ b/src/esx/esx_vi.c
@@ -387,9 +387,9 @@ esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content,
             return -1;
         }
 
-        virAsprintf(&range, "%llu-%llu", offset, offset + *length - 1);
+        range = g_strdup_printf("%llu-%llu", offset, offset + *length - 1);
     } else if (offset > 0) {
-        virAsprintf(&range, "%llu-", offset);
+        range = g_strdup_printf("%llu-", offset);
     }
 
     virMutexLock(&curl->lock);
@@ -1487,9 +1487,8 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
 
             goto cleanup;
         } else {
-            virAsprintf(&xpathExpression,
-                        "/soapenv:Envelope/soapenv:Body/vim:%sResponse",
-                        methodName);
+            xpathExpression = g_strdup_printf("/soapenv:Envelope/soapenv:Body/vim:%sResponse",
+                                              methodName);
 
             responseNode = virXPathNode(xpathExpression, xpathContext);
 
@@ -3585,12 +3584,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.
          */
-        virAsprintf(&datastorePathWithoutFileName, "[%s]", datastoreName);
+        datastorePathWithoutFileName = g_strdup_printf("[%s]", datastoreName);
 
         fileName = g_strdup(directoryAndFileName);
     } else {
-        virAsprintf(&datastorePathWithoutFileName, "[%s] %s", datastoreName,
-                    directoryName);
+        datastorePathWithoutFileName = g_strdup_printf("[%s] %s", datastoreName,
+                                                       directoryName);
 
         length = strlen(directoryName);
 
@@ -3816,7 +3815,7 @@ esxVI_LookupDatastoreContentByDatastoreName
     floppyImageFileQuery = NULL;
 
     /* Search datastore for files */
-    virAsprintf(&datastorePath, "[%s]", datastoreName);
+    datastorePath = g_strdup_printf("[%s]", datastoreName);
 
     if (esxVI_SearchDatastoreSubFolders_Task(ctx, hostDatastoreBrowser,
                                              datastorePath, searchSpec,
@@ -4534,9 +4533,9 @@ esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
         } else if (!taskInfo->error->localizedMessage) {
             *errorMessage = g_strdup(taskInfo->error->fault->_actualType);
         } else {
-            virAsprintf(errorMessage, "%s - %s",
-                        taskInfo->error->fault->_actualType,
-                        taskInfo->error->localizedMessage);
+            *errorMessage = g_strdup_printf("%s - %s",
+                                            taskInfo->error->fault->_actualType,
+                                            taskInfo->error->localizedMessage);
         }
     }
 
-- 
2.21.0




More information about the libvir-list mailing list