1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-20 07:59:00 +00:00

vbox: Use g_strdup_printf() instead of virAsprintf()

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Michal Privoznik 2019-10-22 15:26:14 +02:00
parent 837f5619e7
commit 24d213b03c
7 changed files with 55 additions and 82 deletions

View File

@ -525,8 +525,8 @@ vboxComInitialize_v2(const char *pszVirtualBoxIID, IVirtualBox **ppVirtualBox,
CoInitialize(NULL); CoInitialize(NULL);
if (virAsprintf(&mbsVirtualBoxIID, "{%s}", pszVirtualBoxIID) < 0 || mbsVirtualBoxIID = g_strdup_printf("{%s}", pszVirtualBoxIID);
virAsprintf(&mbsSessionIID, "{%s}", pszSessionIID) < 0) mbsSessionIID = g_strdup_printf("{%s}", pszSessionIID)
goto cleanup; goto cleanup;
if (vboxUtf8ToUtf16(mbsVirtualBoxIID, &wcsVirtualBoxIID) < 0 || if (vboxUtf8ToUtf16(mbsVirtualBoxIID, &wcsVirtualBoxIID) < 0 ||

View File

@ -85,8 +85,7 @@ tryLoadOne(const char *dir, bool setAppHome, bool ignoreMissing,
PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions; PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;
if (dir != NULL) { if (dir != NULL) {
if (virAsprintf(&name, "%s/%s", dir, DYNLIB_NAME) < 0) name = g_strdup_printf("%s/%s", dir, DYNLIB_NAME);
return -1;
if (!virFileExists(name)) { if (!virFileExists(name)) {
if (!ignoreMissing) if (!ignoreMissing)

View File

@ -1770,13 +1770,12 @@ vboxAttachUSB(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine)
/* Zero pad for nice alignment when fewer than 9999 /* Zero pad for nice alignment when fewer than 9999
* devices. * devices.
*/ */
if (virAsprintf(&filtername, "filter%04zu", i) >= 0) { filtername = g_strdup_printf("filter%04zu", i);
VBOX_UTF8_TO_UTF16(filtername, &filternameUtf16); VBOX_UTF8_TO_UTF16(filtername, &filternameUtf16);
VIR_FREE(filtername); VIR_FREE(filtername);
gVBoxAPI.UIUSBCommon.CreateDeviceFilter(USBCommon, gVBoxAPI.UIUSBCommon.CreateDeviceFilter(USBCommon,
filternameUtf16, filternameUtf16,
&filter); &filter);
}
VBOX_UTF16_FREE(filternameUtf16); VBOX_UTF16_FREE(filternameUtf16);
if (!filter) if (!filter)
@ -2127,10 +2126,9 @@ vboxStartMachine(virDomainPtr dom, int maxDomID, IMachine *machine, vboxIID *iid
if (guiPresent) { if (guiPresent) {
if (guiDisplay) { if (guiDisplay) {
char *displayutf8; char *displayutf8;
if (virAsprintf(&displayutf8, "DISPLAY=%s", guiDisplay) >= 0) { displayutf8 = g_strdup_printf("DISPLAY=%s", guiDisplay);
VBOX_UTF8_TO_UTF16(displayutf8, &env); VBOX_UTF8_TO_UTF16(displayutf8, &env);
VIR_FREE(displayutf8); VIR_FREE(displayutf8);
}
VIR_FREE(guiDisplay); VIR_FREE(guiDisplay);
} }
@ -2140,10 +2138,9 @@ vboxStartMachine(virDomainPtr dom, int maxDomID, IMachine *machine, vboxIID *iid
if (sdlPresent) { if (sdlPresent) {
if (sdlDisplay) { if (sdlDisplay) {
char *displayutf8; char *displayutf8;
if (virAsprintf(&displayutf8, "DISPLAY=%s", sdlDisplay) >= 0) { displayutf8 = g_strdup_printf("DISPLAY=%s", sdlDisplay);
VBOX_UTF8_TO_UTF16(displayutf8, &env); VBOX_UTF8_TO_UTF16(displayutf8, &env);
VIR_FREE(displayutf8); VIR_FREE(displayutf8);
}
VIR_FREE(sdlDisplay); VIR_FREE(sdlDisplay);
} }
@ -4619,8 +4616,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
} }
VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName); VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName);
if (virAsprintf(&nameTmpUse, "%s.vbox", machineName) < 0) nameTmpUse = g_strdup_printf("%s.vbox", machineName);
goto cleanup;
machineLocationPath = virStringReplace(settingsFilePath_Utf8, nameTmpUse, ""); machineLocationPath = virStringReplace(settingsFilePath_Utf8, nameTmpUse, "");
if (machineLocationPath == NULL) { if (machineLocationPath == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -4636,9 +4632,8 @@ vboxSnapshotRedefine(virDomainPtr dom,
goto cleanup; goto cleanup;
} }
if (snapshotMachineDesc->currentSnapshot != NULL) { if (snapshotMachineDesc->currentSnapshot != NULL) {
if (virAsprintf(&currentSnapshotXmlFilePath, "%s%s.xml", machineLocationPath, currentSnapshotXmlFilePath = g_strdup_printf("%s%s.xml",
snapshotMachineDesc->currentSnapshot) < 0) machineLocationPath, snapshotMachineDesc->currentSnapshot);
goto cleanup;
snapshotFileExists = virFileExists(currentSnapshotXmlFilePath); snapshotFileExists = virFileExists(currentSnapshotXmlFilePath);
} }
@ -5206,8 +5201,8 @@ vboxSnapshotRedefine(virDomainPtr dom,
vboxIIDUnalloc(&parentiid); vboxIIDUnalloc(&parentiid);
VBOX_UTF8_TO_UTF16("VDI", &formatUtf16); VBOX_UTF8_TO_UTF16("VDI", &formatUtf16);
if (virAsprintf(&newLocationUtf8, "%sfakedisk-%d.vdi", machineLocationPath, it) < 0) newLocationUtf8 = g_strdup_printf("%sfakedisk-%d.vdi",
goto cleanup; machineLocationPath, it);
VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation); VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation);
rc = gVBoxAPI.UIVirtualBox.CreateHardDisk(data->vboxObj, rc = gVBoxAPI.UIVirtualBox.CreateHardDisk(data->vboxObj,
formatUtf16, formatUtf16,
@ -5305,8 +5300,8 @@ vboxSnapshotRedefine(virDomainPtr dom,
* next define. This file is saved as "'machineLocation'/snapshot-'uuid'.xml" * next define. This file is saved as "'machineLocation'/snapshot-'uuid'.xml"
*/ */
VIR_FREE(currentSnapshotXmlFilePath); VIR_FREE(currentSnapshotXmlFilePath);
if (virAsprintf(&currentSnapshotXmlFilePath, "%s%s.xml", machineLocationPath, snapshotMachineDesc->currentSnapshot) < 0) currentSnapshotXmlFilePath = g_strdup_printf("%s%s.xml",
goto cleanup; machineLocationPath, snapshotMachineDesc->currentSnapshot);
char *snapshotContent = virDomainSnapshotDefFormat(NULL, def, char *snapshotContent = virDomainSnapshotDefFormat(NULL, def,
data->caps, data->caps,
data->xmlopt, data->xmlopt,
@ -6893,8 +6888,7 @@ vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot)
goto cleanup; goto cleanup;
} }
VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName); VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName);
if (virAsprintf(&nameTmpUse, "%s.vbox", machineName) < 0) nameTmpUse = g_strdup_printf("%s.vbox", machineName);
goto cleanup;
machineLocationPath = virStringReplace(settingsFilepath, nameTmpUse, ""); machineLocationPath = virStringReplace(settingsFilepath, nameTmpUse, "");
if (machineLocationPath == NULL) { if (machineLocationPath == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -6978,9 +6972,8 @@ vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot)
VBOX_UTF16_FREE(locationUtf16); VBOX_UTF16_FREE(locationUtf16);
VBOX_UTF8_TO_UTF16("VDI", &formatUtf16); VBOX_UTF8_TO_UTF16("VDI", &formatUtf16);
if (virAsprintf(&newLocationUtf8, "%sfakedisk-%s-%d.vdi", newLocationUtf8 = g_strdup_printf("%sfakedisk-%s-%d.vdi",
machineLocationPath, def->parent.parent_name, it) < 0) machineLocationPath, def->parent.parent_name, it);
goto cleanup;
VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation); VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation);
rc = gVBoxAPI.UIVirtualBox.CreateHardDisk(data->vboxObj, rc = gVBoxAPI.UIVirtualBox.CreateHardDisk(data->vboxObj,
formatUtf16, formatUtf16,
@ -7383,17 +7376,14 @@ vboxDomainScreenshot(virDomainPtr dom,
return NULL; return NULL;
} }
if ((privileged && virAsprintf(&cacheDir, "%s/cache/libvirt", LOCALSTATEDIR) < 0) || if (privileged) {
(!privileged && !(cacheDir = virGetUserCacheDirectory()))) { cacheDir = g_strdup_printf("%s/cache/libvirt", LOCALSTATEDIR);
} else if (!(cacheDir = virGetUserCacheDirectory())) {
VBOX_RELEASE(machine); VBOX_RELEASE(machine);
return NULL; return NULL;
} }
if (virAsprintf(&tmp, "%s/vbox.screendump.XXXXXX", cacheDir) < 0) { tmp = g_strdup_printf("%s/vbox.screendump.XXXXXX", cacheDir);
VBOX_RELEASE(machine);
VIR_FREE(cacheDir);
return NULL;
}
if ((tmp_fd = mkostemp(tmp, O_CLOEXEC)) == -1) { if ((tmp_fd = mkostemp(tmp, O_CLOEXEC)) == -1) {
virReportSystemError(errno, _("mkostemp(\"%s\") failed"), tmp); virReportSystemError(errno, _("mkostemp(\"%s\") failed"), tmp);

View File

@ -432,8 +432,8 @@ vboxNetworkDefineCreateXML(virConnectPtr conn, const char *xml, bool start)
VBOX_UTF16_TO_UTF8(networkInterfaceNameUtf16, &networkInterfaceNameUtf8); VBOX_UTF16_TO_UTF8(networkInterfaceNameUtf16, &networkInterfaceNameUtf8);
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", networkInterfaceNameUtf8) < 0) networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s",
goto cleanup; networkInterfaceNameUtf8);
VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16); VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
@ -588,8 +588,7 @@ vboxNetworkUndefineDestroy(virNetworkPtr network, bool removeinterface)
* show up in the net-list in virsh * show up in the net-list in virsh
*/ */
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0) networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s", network->name);
goto cleanup;
VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16); VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
@ -693,8 +692,7 @@ static int vboxNetworkCreate(virNetworkPtr network)
* server by giving the machine static IP * server by giving the machine static IP
*/ */
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0) networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s", network->name);
goto cleanup;
VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16); VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
@ -791,8 +789,7 @@ static char *vboxNetworkGetXMLDesc(virNetworkPtr network, unsigned int flags)
def->ips = ipdef; def->ips = ipdef;
def->nips = 1; def->nips = 1;
if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0) networkNameUtf8 = g_strdup_printf("HostInterfaceNetworking-%s", network->name);
goto cleanup;
VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16); VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);

View File

@ -84,8 +84,7 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode,
} }
if (location[0] != '/') { if (location[0] != '/') {
/*The location is a relative path, so we must change it into an absolute one. */ /*The location is a relative path, so we must change it into an absolute one. */
if (virAsprintf(&tmp, "%s%s", machineLocation, location) < 0) tmp = g_strdup_printf("%s%s", machineLocation, location);
goto cleanup;
hardDisk->location = g_strdup(tmp); hardDisk->location = g_strdup(tmp);
} else { } else {
hardDisk->location = g_strdup(location); hardDisk->location = g_strdup(location);
@ -331,8 +330,7 @@ virVBoxSnapshotConfCreateHardDiskNode(virVBoxSnapshotConfHardDiskPtr hardDisk)
size_t i = 0; size_t i = 0;
char *uuid = NULL; char *uuid = NULL;
xmlNodePtr ret = xmlNewNode(NULL, BAD_CAST "HardDisk"); xmlNodePtr ret = xmlNewNode(NULL, BAD_CAST "HardDisk");
if (virAsprintf(&uuid, "{%s}", hardDisk->uuid) < 0) uuid = g_strdup_printf("{%s}", hardDisk->uuid);
goto cleanup;
if (xmlNewProp(ret, BAD_CAST "uuid", BAD_CAST uuid) == NULL) if (xmlNewProp(ret, BAD_CAST "uuid", BAD_CAST uuid) == NULL)
goto cleanup; goto cleanup;
@ -378,8 +376,7 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,
char **secondRegex = NULL; char **secondRegex = NULL;
int secondRegexResult = 0; int secondRegexResult = 0;
if (virAsprintf(&uuid, "{%s}", snapshot->uuid) < 0) uuid = g_strdup_printf("{%s}", snapshot->uuid);
goto cleanup;
if (xmlNewProp(node, BAD_CAST "uuid", BAD_CAST uuid) == NULL) if (xmlNewProp(node, BAD_CAST "uuid", BAD_CAST uuid) == NULL)
goto cleanup; goto cleanup;
@ -400,8 +397,7 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,
goto cleanup; goto cleanup;
if (secondRegexResult < 1) if (secondRegexResult < 1)
goto cleanup; goto cleanup;
if (virAsprintf(&timeStamp, "%sT%sZ", firstRegex[0], secondRegex[0]) < 0) timeStamp = g_strdup_printf("%sT%sZ", firstRegex[0], secondRegex[0]);
goto cleanup;
if (xmlNewProp(node, BAD_CAST "timeStamp", BAD_CAST timeStamp) == NULL) if (xmlNewProp(node, BAD_CAST "timeStamp", BAD_CAST timeStamp) == NULL)
goto cleanup; goto cleanup;
@ -1073,8 +1069,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
} }
if (machine->currentSnapshot != NULL) { if (machine->currentSnapshot != NULL) {
if (virAsprintf(&currentSnapshot, "{%s}", machine->currentSnapshot) < 0) currentSnapshot = g_strdup_printf("{%s}", machine->currentSnapshot);
goto cleanup;
if (!xmlNewProp(machineNode, BAD_CAST "currentSnapshot", BAD_CAST currentSnapshot)) { if (!xmlNewProp(machineNode, BAD_CAST "currentSnapshot", BAD_CAST currentSnapshot)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Error in xmlNewProp")); _("Error in xmlNewProp"));
@ -1111,8 +1106,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
if (secondRegexResult < 1) if (secondRegexResult < 1)
goto cleanup; goto cleanup;
if (virAsprintf(&timeStamp, "%sT%sZ", firstRegex[0], secondRegex[0]) < 0) timeStamp = g_strdup_printf("%sT%sZ", firstRegex[0], secondRegex[0]);
goto cleanup;
if (!xmlNewProp(machineNode, BAD_CAST "lastStateChange", BAD_CAST timeStamp)) { if (!xmlNewProp(machineNode, BAD_CAST "lastStateChange", BAD_CAST timeStamp)) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Error in xmlNewProp")); _("Error in xmlNewProp"));

View File

@ -442,9 +442,8 @@ vboxStorageVolCreateXML(virStoragePoolPtr pool,
} }
/* If target.path isn't given, use default path ~/.VirtualBox/image_name */ /* If target.path isn't given, use default path ~/.VirtualBox/image_name */
if (def->target.path == NULL && if (!def->target.path)
virAsprintf(&def->target.path, "%s/.VirtualBox/%s", virGetUserDirectory(), def->name) < 0) def->target.path = g_strdup_printf("%s/.VirtualBox/%s", virGetUserDirectory(), def->name);
goto cleanup;
VBOX_UTF8_TO_UTF16(def->target.path, &hddNameUtf16); VBOX_UTF8_TO_UTF16(def->target.path, &hddNameUtf16);
if (!hddFormatUtf16 || !hddNameUtf16) if (!hddFormatUtf16 || !hddNameUtf16)

View File

@ -613,29 +613,23 @@ _virtualboxCreateMachine(vboxDriverPtr data, virDomainDefPtr def, IMachine **mac
{ {
vboxIID iid = VBOX_IID_INITIALIZER; vboxIID iid = VBOX_IID_INITIALIZER;
PRUnichar *machineNameUtf16 = NULL; PRUnichar *machineNameUtf16 = NULL;
char *createFlags = NULL;
PRUnichar *createFlagsUtf16 = NULL;
nsresult rc = -1; nsresult rc = -1;
VBOX_UTF8_TO_UTF16(def->name, &machineNameUtf16); VBOX_UTF8_TO_UTF16(def->name, &machineNameUtf16);
vboxIIDFromUUID(&iid, def->uuid); vboxIIDFromUUID(&iid, def->uuid);
{ createFlags = g_strdup_printf("UUID=%s,forceOverwrite=0", uuidstr);
char *createFlags = NULL; VBOX_UTF8_TO_UTF16(createFlags, &createFlagsUtf16);
PRUnichar *createFlagsUtf16 = NULL; rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
NULL,
if (virAsprintf(&createFlags, machineNameUtf16,
"UUID=%s,forceOverwrite=0", uuidstr) < 0) 0,
goto cleanup; nsnull,
VBOX_UTF8_TO_UTF16(createFlags, &createFlagsUtf16); nsnull,
rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj, createFlagsUtf16,
NULL, machine);
machineNameUtf16, VIR_FREE(createFlags);
0,
nsnull,
nsnull,
createFlagsUtf16,
machine);
cleanup:
VIR_FREE(createFlags);
}
VBOX_UTF16_FREE(machineNameUtf16); VBOX_UTF16_FREE(machineNameUtf16);
vboxIIDUnalloc(&iid); vboxIIDUnalloc(&iid);
return rc; return rc;