mirror of https://gitee.com/openkylin/libvirt.git
util: vircgroup: introduce virCgroup(Get|Set)ValueRaw
If we need to get a path of specific file and we need to check its existence before we use it then we can reuse that path to get/set values instead of calling the existing get/set value functions which would be building the path again. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
2f0de10e29
commit
3f741f9ace
|
@ -455,28 +455,22 @@ virCgroupGetBlockDevString(const char *path)
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
virCgroupSetValueStr(virCgroupPtr group,
|
virCgroupSetValueRaw(const char *path,
|
||||||
int controller,
|
|
||||||
const char *key,
|
|
||||||
const char *value)
|
const char *value)
|
||||||
{
|
{
|
||||||
VIR_AUTOFREE(char *) keypath = NULL;
|
char *tmp;
|
||||||
char *tmp = NULL;
|
|
||||||
|
|
||||||
if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
|
VIR_DEBUG("Set value '%s' to '%s'", path, value);
|
||||||
return -1;
|
if (virFileWriteStr(path, value, 0) < 0) {
|
||||||
|
|
||||||
VIR_DEBUG("Set value '%s' to '%s'", keypath, value);
|
|
||||||
if (virFileWriteStr(keypath, value, 0) < 0) {
|
|
||||||
if (errno == EINVAL &&
|
if (errno == EINVAL &&
|
||||||
(tmp = strrchr(keypath, '/'))) {
|
(tmp = strrchr(path, '/'))) {
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
_("Invalid value '%s' for '%s'"),
|
_("Invalid value '%s' for '%s'"),
|
||||||
value, tmp + 1);
|
value, tmp + 1);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
_("Unable to write to '%s'"), keypath);
|
_("Unable to write to '%s'"), path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -484,6 +478,45 @@ virCgroupSetValueStr(virCgroupPtr group,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
virCgroupGetValueRaw(const char *path,
|
||||||
|
char **value)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
*value = NULL;
|
||||||
|
|
||||||
|
VIR_DEBUG("Get value %s", path);
|
||||||
|
|
||||||
|
if ((rc = virFileReadAll(path, 1024*1024, value)) < 0) {
|
||||||
|
virReportSystemError(errno,
|
||||||
|
_("Unable to read from '%s'"), path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Terminated with '\n' has sometimes harmful effects to the caller */
|
||||||
|
if (rc > 0 && (*value)[rc - 1] == '\n')
|
||||||
|
(*value)[rc - 1] = '\0';
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
virCgroupSetValueStr(virCgroupPtr group,
|
||||||
|
int controller,
|
||||||
|
const char *key,
|
||||||
|
const char *value)
|
||||||
|
{
|
||||||
|
VIR_AUTOFREE(char *) keypath = NULL;
|
||||||
|
|
||||||
|
if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return virCgroupSetValueRaw(keypath, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
virCgroupGetValueStr(virCgroupPtr group,
|
virCgroupGetValueStr(virCgroupPtr group,
|
||||||
int controller,
|
int controller,
|
||||||
|
@ -491,26 +524,11 @@ virCgroupGetValueStr(virCgroupPtr group,
|
||||||
char **value)
|
char **value)
|
||||||
{
|
{
|
||||||
VIR_AUTOFREE(char *) keypath = NULL;
|
VIR_AUTOFREE(char *) keypath = NULL;
|
||||||
int rc;
|
|
||||||
|
|
||||||
*value = NULL;
|
|
||||||
|
|
||||||
if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
|
if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
VIR_DEBUG("Get value %s", keypath);
|
return virCgroupGetValueRaw(keypath, value);
|
||||||
|
|
||||||
if ((rc = virFileReadAll(keypath, 1024*1024, value)) < 0) {
|
|
||||||
virReportSystemError(errno,
|
|
||||||
_("Unable to read from '%s'"), keypath);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Terminated with '\n' has sometimes harmful effects to the caller */
|
|
||||||
if (rc > 0 && (*value)[rc - 1] == '\n')
|
|
||||||
(*value)[rc - 1] = '\0';
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,12 @@ struct _virCgroup {
|
||||||
virCgroupV2Controller unified;
|
virCgroupV2Controller unified;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int virCgroupSetValueRaw(const char *path,
|
||||||
|
const char *value);
|
||||||
|
|
||||||
|
int virCgroupGetValueRaw(const char *path,
|
||||||
|
char **value);
|
||||||
|
|
||||||
int virCgroupSetValueStr(virCgroupPtr group,
|
int virCgroupSetValueStr(virCgroupPtr group,
|
||||||
int controller,
|
int controller,
|
||||||
const char *key,
|
const char *key,
|
||||||
|
|
Loading…
Reference in New Issue