~serge-hallyn/ubuntu/raring/libvirt/libvirt-virbr0

« back to all changes in this revision

Viewing changes to src/util/virtypedparam.c

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-13 15:44:12 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20120513154412-fgmn5sxqdzgnzlx3
Tags: 0.9.12-0ubuntu1
* New upstream version:
  * Synchronize with debian packaging:
    - debian/control: Update build depends.
    - debian/libvirt-bin.postrm: Cleanup /var/log/libvirt
      on purge.
    - Bump standards verson (no changes).
    - debian/patches/Don-t-fail-if-we-can-t-setup-avahi.patch: Added
  * Dropped patches:
    - debian/patches/Debianize-libvirt-guests.patch
    - debian/patches/rewrite-lxc-controller-eof-handling-yet-again
    - debian/patches/ubuntu/libnl13.patch
    - debian/patches/ubuntu/fix-lxc-startup-error.patch
    - debian/patches/ubuntu/fix-bridge-fd.patch
    - debian/patches/ubuntu/skip-labelling-network-disks.patch
    - debian/patches/ubuntu/xen-xend-shutdown-detection.patch
    - debian/patches/ubuntu/xen-config-no-vfb-for-hvm.patch
    - debian/patches/debian/Disable-daemon-start-test.patch
    - debian/patches/debian/Disable-gnulib-s-test-nonplocking-pipe.sh.patch
    - debian/patches/ubuntu/9006-default-config-test-case.patch
    - debian/patches/fix-block-migration.patch
    - debian/patches/ubuntu/9022-qemu-unescape-HMP-commands-before-converting-them-to.patch
    - debian/patches/ubuntu/9023-qemu-change-rbd-auth_supported-separation-character-.patch
    - debian/patches/ubuntu/9024-qemu-allow-snapshotting-of-sheepdog-and-rbd-disks.patch
    - debian/patches/9025-qemu-change-rbd-auth_supported-separation-character-.patch
    - debian/patches/ubuntu/arm-gcc-workaround.patch
  * Rediffed:
    - debian/patches/Allow-libvirt-group-to-access-the-socket.patch
    - debian/patches/Disable-failing-virnetsockettest.patch
    - debian/patches/dnsmasq-as-priv-user
    - debian/patches/9002-better_default_uri_virsh.patch
  * debian/control: Add libnl-route-3-dev ass a build depends.
  * debian/patches/libnl3-build-fix.patch: Fix build with libnl3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * virtypedparam.c: utility functions for dealing with virTypedParameters
 
3
 *
 
4
 * Copyright (C) 2011-2012 Red Hat, Inc.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
19
 *
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
#include "virtypedparam.h"
 
24
 
 
25
#include <stdarg.h>
 
26
 
 
27
#include "memory.h"
 
28
#include "util.h"
 
29
#include "virterror_internal.h"
 
30
 
 
31
#define VIR_FROM_THIS VIR_FROM_NONE
 
32
 
 
33
#define virUtilError(code, ...)                                            \
 
34
        virReportErrorHelper(VIR_FROM_NONE, code, __FILE__,                \
 
35
                             __FUNCTION__, __LINE__, __VA_ARGS__)
 
36
 
 
37
VIR_ENUM_DECL(virTypedParameter)
 
38
VIR_ENUM_IMPL(virTypedParameter, VIR_TYPED_PARAM_LAST,
 
39
              "unknown",
 
40
              "int",
 
41
              "uint",
 
42
              "llong",
 
43
              "ullong",
 
44
              "double",
 
45
              "boolean",
 
46
              "string")
 
47
 
 
48
void
 
49
virTypedParameterArrayClear(virTypedParameterPtr params, int nparams)
 
50
{
 
51
    int i;
 
52
 
 
53
    if (!params)
 
54
        return;
 
55
 
 
56
    for (i = 0; i < nparams; i++) {
 
57
        if (params[i].type == VIR_TYPED_PARAM_STRING)
 
58
            VIR_FREE(params[i].value.s);
 
59
    }
 
60
}
 
61
 
 
62
/* Validate that PARAMS contains only recognized parameter names with
 
63
 * correct types, and with no duplicates.  Pass in as many name/type
 
64
 * pairs as appropriate, and pass NULL to end the list of accepted
 
65
 * parameters.  Return 0 on success, -1 on failure with error message
 
66
 * already issued.  */
 
67
int
 
68
virTypedParameterArrayValidate(virTypedParameterPtr params, int nparams, ...)
 
69
{
 
70
    va_list ap;
 
71
    int ret = -1;
 
72
    int i, j;
 
73
    const char *name;
 
74
    int type;
 
75
 
 
76
    va_start(ap, nparams);
 
77
 
 
78
    /* Yes, this is quadratic, but since we reject duplicates and
 
79
     * unknowns, it is constrained by the number of var-args passed
 
80
     * in, which is expected to be small enough to not be
 
81
     * noticeable.  */
 
82
    for (i = 0; i < nparams; i++) {
 
83
        va_end(ap);
 
84
        va_start(ap, nparams);
 
85
 
 
86
        name = va_arg(ap, const char *);
 
87
        while (name) {
 
88
            type = va_arg(ap, int);
 
89
            if (STREQ(params[i].field, name)) {
 
90
                if (params[i].type != type) {
 
91
                    const char *badtype;
 
92
 
 
93
                    badtype = virTypedParameterTypeToString(params[i].type);
 
94
                    if (!badtype)
 
95
                        badtype = virTypedParameterTypeToString(0);
 
96
                    virUtilError(VIR_ERR_INVALID_ARG,
 
97
                                 _("invalid type '%s' for parameter '%s', "
 
98
                                   "expected '%s'"),
 
99
                                 badtype, params[i].field,
 
100
                                 virTypedParameterTypeToString(type));
 
101
                }
 
102
                break;
 
103
            }
 
104
            name = va_arg(ap, const char *);
 
105
        }
 
106
        if (!name) {
 
107
            virUtilError(VIR_ERR_INVALID_ARG,
 
108
                         _("parameter '%s' not supported"),
 
109
                         params[i].field);
 
110
            goto cleanup;
 
111
        }
 
112
        for (j = 0; j < i; j++) {
 
113
            if (STREQ(params[i].field, params[j].field)) {
 
114
                virUtilError(VIR_ERR_INVALID_ARG,
 
115
                             _("parameter '%s' occurs multiple times"),
 
116
                             params[i].field);
 
117
                goto cleanup;
 
118
            }
 
119
        }
 
120
    }
 
121
 
 
122
    ret = 0;
 
123
cleanup:
 
124
    va_end(ap);
 
125
    return ret;
 
126
 
 
127
}
 
128
 
 
129
/* Assign name, type, and the appropriately typed arg to param; in the
 
130
 * case of a string, the caller is assumed to have malloc'd a string,
 
131
 * or can pass NULL to have this function malloc an empty string.
 
132
 * Return 0 on success, -1 after an error message on failure.  */
 
133
int
 
134
virTypedParameterAssign(virTypedParameterPtr param, const char *name,
 
135
                        int type, ...)
 
136
{
 
137
    va_list ap;
 
138
    int ret = -1;
 
139
 
 
140
    va_start(ap, type);
 
141
 
 
142
    if (virStrcpyStatic(param->field, name) == NULL) {
 
143
        virUtilError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
 
144
                     name);
 
145
        goto cleanup;
 
146
    }
 
147
    param->type = type;
 
148
    switch (type)
 
149
    {
 
150
    case VIR_TYPED_PARAM_INT:
 
151
        param->value.i = va_arg(ap, int);
 
152
        break;
 
153
    case VIR_TYPED_PARAM_UINT:
 
154
        param->value.ui = va_arg(ap, unsigned int);
 
155
        break;
 
156
    case VIR_TYPED_PARAM_LLONG:
 
157
        param->value.l = va_arg(ap, long long int);
 
158
        break;
 
159
    case VIR_TYPED_PARAM_ULLONG:
 
160
        param->value.ul = va_arg(ap, unsigned long long int);
 
161
        break;
 
162
    case VIR_TYPED_PARAM_DOUBLE:
 
163
        param->value.d = va_arg(ap, double);
 
164
        break;
 
165
    case VIR_TYPED_PARAM_BOOLEAN:
 
166
        param->value.b = !!va_arg(ap, int);
 
167
        break;
 
168
    case VIR_TYPED_PARAM_STRING:
 
169
        param->value.s = va_arg(ap, char *);
 
170
        if (!param->value.s)
 
171
            param->value.s = strdup("");
 
172
        if (!param->value.s) {
 
173
            virReportOOMError();
 
174
            goto cleanup;
 
175
        }
 
176
        break;
 
177
    default:
 
178
        virUtilError(VIR_ERR_INTERNAL_ERROR,
 
179
                     _("unexpected type %d for field %s"), type, name);
 
180
        goto cleanup;
 
181
    }
 
182
 
 
183
    ret = 0;
 
184
cleanup:
 
185
    va_end(ap);
 
186
    return ret;
 
187
}