mirror of https://gitee.com/openkylin/libvirt.git
util: simplify virPCIProbeStubDriver()
This function had a loop that was only executed twice; it was artificially constructed with a label, a goto, and a boolean to tell that it had already been executed once. Aside from that, the body of the loop contained only two lines that needed to be repeated (the second time through, everything beyond those two lines would be skipped). One side effect of this strange loop was that a g_autofree string was manually freed and re-initialized; I've been told that manually freeing a g_auto_free object is highly discouraged. This patch refactors the function to simply repeat the 2 lines that might possibly be executed twice, thus eliminating the ugly use of goto to construct a loop, and also takes advantage of the fact that virPCIDriverDir() was previously returning *exactly* the same string both times it was called to eliminate the manual VIR_FREE of drvpath. Signed-off-by: Laine Stump <laine@redhat.com> Reviewed-by: Erik Skultety <eskultet@redhat.com> Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
parent
b3066b55bf
commit
bc7c4f5415
|
@ -988,7 +988,7 @@ virPCIProbeStubDriver(virPCIStubDriver driver)
|
|||
{
|
||||
const char *drvname = NULL;
|
||||
g_autofree char *drvpath = NULL;
|
||||
bool probed = false;
|
||||
g_autofree char *errbuf = NULL;
|
||||
|
||||
if (driver == VIR_PCI_STUB_DRIVER_NONE ||
|
||||
!(drvname = virPCIStubDriverTypeToString(driver))) {
|
||||
|
@ -998,25 +998,21 @@ virPCIProbeStubDriver(virPCIStubDriver driver)
|
|||
return -1;
|
||||
}
|
||||
|
||||
recheck:
|
||||
drvpath = virPCIDriverDir(drvname);
|
||||
|
||||
/* driver previously loaded, return */
|
||||
if (virFileExists(drvpath))
|
||||
/* driver already loaded, return */
|
||||
return 0;
|
||||
|
||||
if (!probed) {
|
||||
g_autofree char *errbuf = NULL;
|
||||
probed = true;
|
||||
if ((errbuf = virKModLoad(drvname))) {
|
||||
VIR_WARN("failed to load driver %s: %s", drvname, errbuf);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
VIR_FREE(drvpath);
|
||||
goto recheck;
|
||||
if ((errbuf = virKModLoad(drvname))) {
|
||||
VIR_WARN("failed to load driver %s: %s", drvname, errbuf);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* driver loaded after probing */
|
||||
if (virFileExists(drvpath))
|
||||
return 0;
|
||||
|
||||
cleanup:
|
||||
/* If we know failure was because of admin config, let's report that;
|
||||
* otherwise, report a more generic failure message
|
||||
|
|
Loading…
Reference in New Issue