提交 61ca98b0 编写于 作者: E Eric Blake

util: add new file for virTypedParameter utils

Preparation for another patch that refactors common patterns
into the new file for fewer lines of code overall.

* src/util/util.h (virTypedParameterArrayClear): Move...
* src/util/virtypedparam.h: ...to new file.
(virTypedParameterArrayValidate, virTypedParameterAssign): New
prototypes.
* src/util/util.c (virTypedParameterArrayClear): Likewise.
* src/util/virtypedparam.c: New file.
* po/POTFILES.in: Mark file for translation.
* src/Makefile.am (UTIL_SOURCES): Build it.
* src/libvirt_private.syms (util.h): Split...
(virtypedparam.h): to new section.
(virkeycode.h): Sort.
* daemon/remote.c: Adjust callers.
* tools/virsh.c: Likewise.
上级 9c377576
master openEuler-20.03-LTS openEuler-20.09 v0.10.2-maint v0.9.11-maint v0.9.12-maint v1.0.0-maint v1.0.1-maint v1.0.2-maint v1.0.3-maint v1.0.4-maint v1.0.5-maint v1.0.6-maint v1.1.0-maint v1.1.1-maint v1.1.2-maint v1.1.3-maint v1.1.4-maint v1.2.0-maint v1.2.1-maint v1.2.10-maint v1.2.11-maint v1.2.12-maint v1.2.13-maint v1.2.14-maint v1.2.15-maint v1.2.16-maint v1.2.17-maint v1.2.18-maint v1.2.19-maint v1.2.2-maint v1.2.20-maint v1.2.21-maint v1.2.3-maint v1.2.4-maint v1.2.5-maint v1.2.6-maint v1.2.7-maint v1.2.8-maint v1.2.9-maint v1.3.0-maint v1.3.1-maint v1.3.2-maint v1.3.3-maint v1.3.4-maint v1.3.5-maint v2.0-maint v2.1-maint v2.2-maint v3.0-maint v3.2-maint v3.7-maint v4.1-maint v4.10-maint v4.2-maint v4.3-maint v4.4-maint v4.5-maint v4.6-maint v4.7-maint v4.8-maint v4.9-maint v5.0-maint v5.1-maint v5.1.0-maint v5.2-maint v5.3-maint v6.5.0-rc2 v6.5.0-rc1 v6.4.0 v6.4.0-rc1 v6.3.0 v6.3.0-rc1 v6.2.0 v6.2.0-rc1 v6.1.0 v6.1.0-rc2 v6.1.0-rc1 v6.0.0 v6.0.0-rc2 v6.0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.1.3.9 v1.1.3.8 v1.1.3.7 v1.1.3.6 v1.1.3.5 v1.1.3.4 v1.1.3.3 v1.1.3.2 v1.1.3.1 v1.1.3 v1.1.3-rc2 v1.1.3-rc1 v1.1.2 v1.1.2-rc2 v1.1.2-rc1 v1.1.1 v1.1.1-rc2 v1.1.1-rc1 v1.1.0 v1.1.0-rc2 v1.1.0-rc1 v1.0.6 v1.0.6-rc2 v1.0.6-rc1 v1.0.5.9 v1.0.5.8 v1.0.5.7 v1.0.5.6 v1.0.5.5 v1.0.5.4 v1.0.5.3 v1.0.5.2 v1.0.5.1 v1.0.5 v1.0.5-rc1 v1.0.4 v1.0.4-rc2 v1.0.4-rc1 v1.0.3 v1.0.3-rc2 v1.0.3-rc1 v1.0.2 v1.0.2-rc2 v1.0.2-rc1 v1.0.1 v1.0.1-rc2 v1.0.1-rc1 v1.0.0 v1.0.0-rc3 v1.0.0-rc2 v1.0.0-rc1 v0.10.2.8 v0.10.2.7 v0.10.2.6 v0.10.2.5 v0.10.2.4 v0.10.2.3 v0.10.2.2 v0.10.2.1 v0.10.2 v0.10.2-rc2 v0.10.2-rc1 v0.10.1 v0.10.0 v0.10.0-rc2 v0.10.0-rc1 v0.10.0-rc0 v0.9.13 v0.9.13-rc2 v0.9.13-rc1 v0.9.12.3 v0.9.12.2 v0.9.12.1 v0.9.12 v0.9.12-rc2 v0.9.12-rc1 v0.9.11.10 v0.9.11.9 v0.9.11.8 v0.9.11.7 v0.9.11.6 v0.9.11.5 v0.9.11.4 v0.9.11.3 v0.9.11.2 v0.9.11.1 v0.9.11 v0.9.11-rc2 v0.9.11-rc1 v0.9.10 v0.9.10-rc2 v0.9.10-rc1 CVE-2017-1000256 CVE-2017-2635 CVE-2016-5008 CVE-2015-5313 CVE-2015-5247-3 CVE-2015-5247-2 CVE-2015-5247-1 CVE-2015-0236-2 CVE-2015-0236-1 CVE-2014-8136 CVE-2014-8135 CVE-2014-8131-2 CVE-2014-8131-1 CVE-2014-7823 CVE-2014-3657 CVE-2014-3633 CVE-2014-1447-2 CVE-2014-1447-1 CVE-2014-0179 CVE-2014-0028 CVE-2013-7336 CVE-2013-6458-4 CVE-2013-6458-3 CVE-2013-6458-2 CVE-2013-6458-1 CVE-2013-6457 CVE-2013-6436 CVE-2013-5651 CVE-2013-4401 CVE-2013-4400-3 CVE-2013-4400-2 CVE-2013-4400-1 CVE-2013-4399 CVE-2013-4311 CVE-2013-4297 CVE-2013-4296 CVE-2013-4292 CVE-2013-4291 CVE-2013-4239 CVE-2013-4154 CVE-2013-4153 CVE-2013-2230 CVE-2013-2218 CVE-2013-1962 CVE-2013-0170 CVE-2012-4423 CVE-2012-3445 CVE-2012-3411
无相关合并请求
......@@ -44,6 +44,7 @@
#include "virnetserverservice.h"
#include "virnetserver.h"
#include "virfile.h"
#include "virtypedparam.h"
#include "remote_protocol.h"
#include "qemu_protocol.h"
......
......@@ -135,6 +135,7 @@ src/util/virpidfile.c
src/util/virsocketaddr.c
src/util/virterror.c
src/util/virtime.c
src/util/virtypedparam.c
src/util/xml.c
src/vbox/vbox_MSCOMGlue.c
src/vbox/vbox_XPCOMCGlue.c
......
......@@ -87,6 +87,7 @@ UTIL_SOURCES = \
util/virfile.c util/virfile.h \
util/virnodesuspend.c util/virnodesuspend.h \
util/virpidfile.c util/virpidfile.h \
util/virtypedparam.c util/virtypedparam.h \
util/xml.c util/xml.h \
util/virterror.c util/virterror_internal.h \
util/virkeycode.c util/virkeycode.h \
......
......@@ -1146,7 +1146,6 @@ virStrToLong_ull;
virStrcpy;
virStrncpy;
virTrimSpaces;
virTypedParameterArrayClear;
virVasprintf;
......@@ -1177,6 +1176,13 @@ virFileFdopen;
virFileRewrite;
# virkeycode.h
virKeycodeSetTypeFromString;
virKeycodeSetTypeToString;
virKeycodeValueFromString;
virKeycodeValueTranslate;
# virnetclient.h
virNetClientHasPassFD;
......@@ -1391,12 +1397,6 @@ virSetError;
virSetErrorLogPriorityFunc;
virStrerror;
# virkeycode.h
virKeycodeSetTypeToString;
virKeycodeSetTypeFromString;
virKeycodeValueFromString;
virKeycodeValueTranslate;
# virtime.h
virTimeFieldsNow;
......@@ -1411,6 +1411,12 @@ virTimeStringThen;
virTimeStringThenRaw;
# virtypedparam.h
virTypedParameterArrayClear;
virTypedParameterArrayValidate;
virTypedParameterAssign;
# xml.h
virXMLChildElementCount;
virXMLParseHelper;
......
/*
* utils.c: common, generic utility functions
*
* Copyright (C) 2006-2011 Red Hat, Inc.
* Copyright (C) 2006-2012 Red Hat, Inc.
* Copyright (C) 2006 Daniel P. Berrange
* Copyright (C) 2006, 2007 Binary Karma
* Copyright (C) 2006 Shuveb Hussain
......@@ -2630,17 +2630,3 @@ or other application using the libvirt API.\n\
return 0;
}
void
virTypedParameterArrayClear(virTypedParameterPtr params, int nparams)
{
int i;
if (!params)
return;
for (i = 0; i < nparams; i++) {
if (params[i].type == VIR_TYPED_PARAM_STRING)
VIR_FREE(params[i].value.s);
}
}
/*
* utils.h: common, generic utility functions
*
* Copyright (C) 2010-2011 Red Hat, Inc.
* Copyright (C) 2010-2012 Red Hat, Inc.
* Copyright (C) 2006, 2007 Binary Karma
* Copyright (C) 2006 Shuveb Hussain
*
......@@ -258,6 +258,4 @@ int virEmitXMLWarning(int fd,
const char *name,
const char *cmd) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
void virTypedParameterArrayClear(virTypedParameterPtr params, int nparams);
#endif /* __VIR_UTIL_H__ */
/*
* virtypedparam.c: utility functions for dealing with virTypedParameters
*
* Copyright (C) 2011-2012 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <config.h>
#include "virtypedparam.h"
#include <stdarg.h>
#include "memory.h"
#include "util.h"
#include "virterror_internal.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#define virUtilError(code, ...) \
virReportErrorHelper(VIR_FROM_NONE, code, __FILE__, \
__FUNCTION__, __LINE__, __VA_ARGS__)
VIR_ENUM_DECL(virTypedParameter)
VIR_ENUM_IMPL(virTypedParameter, VIR_TYPED_PARAM_STRING + 1,
"unknown",
"int",
"uint",
"llong",
"ullong",
"double",
"boolean",
"string")
void
virTypedParameterArrayClear(virTypedParameterPtr params, int nparams)
{
int i;
if (!params)
return;
for (i = 0; i < nparams; i++) {
if (params[i].type == VIR_TYPED_PARAM_STRING)
VIR_FREE(params[i].value.s);
}
}
/* Validate that PARAMS contains only recognized parameter names with
* correct types, and with no duplicates. Pass in as many name/type
* pairs as appropriate, and pass NULL to end the list of accepted
* parameters. Return 0 on success, -1 on failure with error message
* already issued. */
int
virTypedParameterArrayValidate(virTypedParameterPtr params, int nparams, ...)
{
va_list ap;
int ret = -1;
int i, j;
const char *name;
int type;
va_start(ap, nparams);
/* Yes, this is quadratic, but since we reject duplicates and
* unknowns, it is constrained by the number of var-args passed
* in, which is expected to be small enough to not be
* noticeable. */
for (i = 0; i < nparams; i++) {
va_end(ap);
va_start(ap, nparams);
name = va_arg(ap, const char *);
while (name) {
type = va_arg(ap, int);
if (STREQ(params[i].field, name)) {
if (params[i].type != type) {
const char *badtype;
badtype = virTypedParameterTypeToString(params[i].type);
if (!badtype)
badtype = virTypedParameterTypeToString(0);
virUtilError(VIR_ERR_INVALID_ARG,
_("invalid type '%s' for parameter '%s', "
"expected '%s'"),
badtype, params[i].field,
virTypedParameterTypeToString(type));
}
break;
}
name = va_arg(ap, const char *);
}
if (!name) {
virUtilError(VIR_ERR_INVALID_ARG,
_("parameter '%s' not supported"),
params[i].field);
goto cleanup;
}
for (j = 0; j < i; j++) {
if (STREQ(params[i].field, params[j].field)) {
virUtilError(VIR_ERR_INVALID_ARG,
_("parameter '%s' occurs multiple times"),
params[i].field);
goto cleanup;
}
}
}
ret = 0;
cleanup:
va_end(ap);
return ret;
}
/* Assign name, type, and the appropriately typed arg to param; in the
* case of a string, the caller is assumed to have malloc'd a string,
* or can pass NULL to have this function malloc an empty string.
* Return 0 on success, -1 after an error message on failure. */
int
virTypedParameterAssign(virTypedParameterPtr param, const char *name,
int type, ...)
{
va_list ap;
int ret = -1;
va_start(ap, type);
if (virStrcpyStatic(param->field, name) == NULL) {
virUtilError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
name);
goto cleanup;
}
param->type = type;
switch (type)
{
case VIR_TYPED_PARAM_INT:
param->value.i = va_arg(ap, int);
break;
case VIR_TYPED_PARAM_UINT:
param->value.ui = va_arg(ap, unsigned int);
break;
case VIR_TYPED_PARAM_LLONG:
param->value.l = va_arg(ap, long long int);
break;
case VIR_TYPED_PARAM_ULLONG:
param->value.ul = va_arg(ap, unsigned long long int);
break;
case VIR_TYPED_PARAM_DOUBLE:
param->value.d = va_arg(ap, double);
break;
case VIR_TYPED_PARAM_BOOLEAN:
param->value.b = !!va_arg(ap, int);
break;
case VIR_TYPED_PARAM_STRING:
param->value.s = va_arg(ap, char *);
if (!param->value.s)
param->value.s = strdup("");
if (!param->value.s) {
virReportOOMError();
goto cleanup;
}
break;
default:
virUtilError(VIR_ERR_INTERNAL_ERROR,
_("unexpected type %d for field %s"), type, name);
goto cleanup;
}
ret = 0;
cleanup:
va_end(ap);
return ret;
}
/*
* virtypedparam.h: managing typed parameters
*
* Copyright (C) 2011-2012 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __VIR_TYPED_PARAM_H_
# define __VIR_TYPED_PARAM_H_
# include "internal.h"
void virTypedParameterArrayClear(virTypedParameterPtr params, int nparams);
int virTypedParameterArrayValidate(virTypedParameterPtr params, int nparams,
/* const char *name, int type ... */ ...)
ATTRIBUTE_SENTINEL;
int virTypedParameterAssign(virTypedParameterPtr param, const char *name,
int type, /* TYPE arg */ ...);
#endif /* __VIR_TYPED_PARAM_H */
......@@ -62,6 +62,7 @@
#include "virnetdevbandwidth.h"
#include "util/bitmap.h"
#include "conf/domain_conf.h"
#include "virtypedparam.h"
static char *progname;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
反馈
建议
客服 返回
顶部