提交 50f7960d 编写于 作者: E Eric Blake

virsh: kill over-engineered asprintf failure recovery

I noticed this while shortening switch statements via VIR_ENUM.
Basically, the only ways virAsprintf can fail are if we pass a
bogus format string (but we're not THAT bad) or if we run out
of memory (but it already warns on our behalf in that case).
Throw away the cruft that tries too hard to diagnose a printf
failure.

* tools/virsh-volume.c (cmdVolList): Simplify.
* tools/virsh-pool.c (cmdPoolList): Likewise.
Signed-off-by: NEric Blake <eblake@redhat.com>
上级 466b12ab
...@@ -961,9 +961,8 @@ static bool ...@@ -961,9 +961,8 @@ static bool
cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{ {
virStoragePoolInfo info; virStoragePoolInfo info;
int ret;
size_t i; size_t i;
bool functionReturn = false; bool ret = false;
size_t stringLength = 0, nameStrLength = 0; size_t stringLength = 0, nameStrLength = 0;
size_t autostartStrLength = 0, persistStrLength = 0; size_t autostartStrLength = 0, persistStrLength = 0;
size_t stateStrLength = 0, capStrLength = 0; size_t stateStrLength = 0, capStrLength = 0;
...@@ -1121,29 +1120,20 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1121,29 +1120,20 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
double val; double val;
const char *unit; const char *unit;
/* Create the capacity output string */
val = vshPrettyCapacity(info.capacity, &unit); val = vshPrettyCapacity(info.capacity, &unit);
ret = virAsprintf(&poolInfoTexts[i].capacity, if (virAsprintf(&poolInfoTexts[i].capacity,
"%.2lf %s", val, unit); "%.2lf %s", val, unit) < 0)
if (ret < 0) goto cleanup;
/* An error occurred creating the string, return */
goto asprintf_failure;
/* Create the allocation output string */
val = vshPrettyCapacity(info.allocation, &unit); val = vshPrettyCapacity(info.allocation, &unit);
ret = virAsprintf(&poolInfoTexts[i].allocation, if (virAsprintf(&poolInfoTexts[i].allocation,
"%.2lf %s", val, unit); "%.2lf %s", val, unit) < 0)
if (ret < 0) goto cleanup;
/* An error occurred creating the string, return */
goto asprintf_failure;
/* Create the available space output string */
val = vshPrettyCapacity(info.available, &unit); val = vshPrettyCapacity(info.available, &unit);
ret = virAsprintf(&poolInfoTexts[i].available, if (virAsprintf(&poolInfoTexts[i].available,
"%.2lf %s", val, unit); "%.2lf %s", val, unit) < 0)
if (ret < 0) goto cleanup;
/* An error occurred creating the string, return */
goto asprintf_failure;
} else { } else {
/* Capacity related information isn't available */ /* Capacity related information isn't available */
poolInfoTexts[i].capacity = vshStrdup(ctl, _("-")); poolInfoTexts[i].capacity = vshStrdup(ctl, _("-"));
...@@ -1213,7 +1203,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1213,7 +1203,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
} }
/* Cleanup and return */ /* Cleanup and return */
functionReturn = true; ret = true;
goto cleanup; goto cleanup;
} }
...@@ -1273,19 +1263,16 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1273,19 +1263,16 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
/* Create the output template. Each column is sized according to /* Create the output template. Each column is sized according to
* the longest string. * the longest string.
*/ */
ret = virAsprintf(&outputStr, if (virAsprintf(&outputStr,
" %%-%lus %%-%lus %%-%lus %%-%lus %%%lus %%%lus %%%lus\n", " %%-%lus %%-%lus %%-%lus %%-%lus %%%lus %%%lus %%%lus\n",
(unsigned long) nameStrLength, (unsigned long) nameStrLength,
(unsigned long) stateStrLength, (unsigned long) stateStrLength,
(unsigned long) autostartStrLength, (unsigned long) autostartStrLength,
(unsigned long) persistStrLength, (unsigned long) persistStrLength,
(unsigned long) capStrLength, (unsigned long) capStrLength,
(unsigned long) allocStrLength, (unsigned long) allocStrLength,
(unsigned long) availStrLength); (unsigned long) availStrLength) < 0)
if (ret < 0) { goto cleanup;
/* An error occurred creating the string, return */
goto asprintf_failure;
}
/* Display the header */ /* Display the header */
vshPrint(ctl, outputStr, _("Name"), _("State"), _("Autostart"), vshPrint(ctl, outputStr, _("Name"), _("State"), _("Autostart"),
...@@ -1310,21 +1297,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1310,21 +1297,7 @@ cmdPoolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
} }
/* Cleanup and return */ /* Cleanup and return */
functionReturn = true; ret = true;
goto cleanup;
asprintf_failure:
/* Display an appropriate error message then cleanup and return */
switch (errno) {
case ENOMEM:
/* Couldn't allocate memory */
vshError(ctl, "%s", _("Out of memory"));
break;
default:
/* Some other error */
vshError(ctl, _("virAsprintf failed (errno %d)"), errno);
}
functionReturn = false;
cleanup: cleanup:
VIR_FREE(outputStr); VIR_FREE(outputStr);
...@@ -1341,7 +1314,7 @@ cleanup: ...@@ -1341,7 +1314,7 @@ cleanup:
VIR_FREE(poolInfoTexts); VIR_FREE(poolInfoTexts);
vshStoragePoolListFree(list); vshStoragePoolListFree(list);
return functionReturn; return ret;
} }
/* /*
......
...@@ -1332,8 +1332,7 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1332,8 +1332,7 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
double val; double val;
bool details = vshCommandOptBool(cmd, "details"); bool details = vshCommandOptBool(cmd, "details");
size_t i; size_t i;
int ret; bool ret = false;
bool functionReturn = false;
int stringLength = 0; int stringLength = 0;
size_t allocStrLength = 0, capStrLength = 0; size_t allocStrLength = 0, capStrLength = 0;
size_t nameStrLength = 0, pathStrLength = 0; size_t nameStrLength = 0, pathStrLength = 0;
...@@ -1382,23 +1381,15 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1382,23 +1381,15 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
volInfoTexts[i].type = vshStrdup(ctl, volInfoTexts[i].type = vshStrdup(ctl,
vshVolumeTypeToString(volumeInfo.type)); vshVolumeTypeToString(volumeInfo.type));
/* Create the capacity output string */
val = vshPrettyCapacity(volumeInfo.capacity, &unit); val = vshPrettyCapacity(volumeInfo.capacity, &unit);
ret = virAsprintf(&volInfoTexts[i].capacity, if (virAsprintf(&volInfoTexts[i].capacity,
"%.2lf %s", val, unit); "%.2lf %s", val, unit) < 0)
if (ret < 0) { goto cleanup;
/* An error occurred creating the string, return */
goto asprintf_failure;
}
/* Create the allocation output string */
val = vshPrettyCapacity(volumeInfo.allocation, &unit); val = vshPrettyCapacity(volumeInfo.allocation, &unit);
ret = virAsprintf(&volInfoTexts[i].allocation, if (virAsprintf(&volInfoTexts[i].allocation,
"%.2lf %s", val, unit); "%.2lf %s", val, unit) < 0)
if (ret < 0) { goto cleanup;
/* An error occurred creating the string, return */
goto asprintf_failure;
}
} }
/* Remember the largest length for each output string. /* Remember the largest length for each output string.
...@@ -1450,7 +1441,7 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1450,7 +1441,7 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
} }
/* Cleanup and return */ /* Cleanup and return */
functionReturn = true; ret = true;
goto cleanup; goto cleanup;
} }
...@@ -1493,18 +1484,14 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1493,18 +1484,14 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
vshDebug(ctl, VSH_ERR_DEBUG, vshDebug(ctl, VSH_ERR_DEBUG,
"Longest allocation string = %zu chars\n", allocStrLength); "Longest allocation string = %zu chars\n", allocStrLength);
/* Create the output template */ if (virAsprintf(&outputStr,
ret = virAsprintf(&outputStr, " %%-%lus %%-%lus %%-%lus %%%lus %%%lus\n",
" %%-%lus %%-%lus %%-%lus %%%lus %%%lus\n", (unsigned long) nameStrLength,
(unsigned long) nameStrLength, (unsigned long) pathStrLength,
(unsigned long) pathStrLength, (unsigned long) typeStrLength,
(unsigned long) typeStrLength, (unsigned long) capStrLength,
(unsigned long) capStrLength, (unsigned long) allocStrLength) < 0)
(unsigned long) allocStrLength); goto cleanup;
if (ret < 0) {
/* An error occurred creating the string, return */
goto asprintf_failure;
}
/* Display the header */ /* Display the header */
vshPrint(ctl, outputStr, _("Name"), _("Path"), _("Type"), vshPrint(ctl, outputStr, _("Name"), _("Path"), _("Type"),
...@@ -1526,22 +1513,7 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) ...@@ -1526,22 +1513,7 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
} }
/* Cleanup and return */ /* Cleanup and return */
functionReturn = true; ret = true;
goto cleanup;
asprintf_failure:
/* Display an appropriate error message then cleanup and return */
switch (errno) {
case ENOMEM:
/* Couldn't allocate memory */
vshError(ctl, "%s", _("Out of memory"));
break;
default:
/* Some other error */
vshError(ctl, _("virAsprintf failed (errno %d)"), errno);
}
functionReturn = false;
cleanup: cleanup:
...@@ -1563,7 +1535,7 @@ cleanup: ...@@ -1563,7 +1535,7 @@ cleanup:
vshStorageVolListFree(list); vshStorageVolListFree(list);
/* Return the desired value */ /* Return the desired value */
return functionReturn; return ret;
} }
/* /*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册