util: Refactor virResctrlAllocSetID to set allocation ID

Refactor virResctrlAllocSetID generating an error if an attempt
is made to overwrite the existing value.

Signed-off-by: Wang Huaqiang <huaqiang.wang@intel.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
Wang Huaqiang 2018-11-12 21:31:40 +08:00 committed by John Ferlan
parent 2f22364688
commit 4e54c4b289

View File

@ -1361,17 +1361,32 @@ virResctrlAllocForeachMemory(virResctrlAllocPtr alloc,
}
static int
virResctrlSetID(char **resctrlid,
const char *id)
{
if (!id) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("New resctrl 'id' cannot be NULL"));
return -1;
}
if (*resctrlid) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Attempt to overwrite resctrlid='%s' with id='%s'"),
*resctrlid, id);
return -1;
}
return VIR_STRDUP(*resctrlid, id);
}
int
virResctrlAllocSetID(virResctrlAllocPtr alloc,
const char *id)
{
if (!id) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Resctrl allocation 'id' cannot be NULL"));
return -1;
}
return VIR_STRDUP(alloc->id, id);
return virResctrlSetID(&alloc->id, id);
}