1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <pool.h>
#include "pool_internal.h"
/*
* libpool Value Manipulation Routines
*
* pool_value.c implements the value (pool_value_t) functionality for
* libpool. The datatypes supported are: uint64_t, int64_t, double,
* uchar_t (boolean), const char * (string). Values are used to
* represent data stored to and retrieved from the datastore in a
* simple discriminated union.
*
* Values are dynamically allocated using pool_value_alloc() and
* destroyed using pool_value_free().
*
* Values may be allocated statically for internal use in
* libpool. Statically allocated pool_value_t variables must be
* initialised with the POOL_VALUE_INITIALIZER macro, otherwise the
* results are unpredictable.
*
* A pool_value_t variable can be used to store values in any of the
* supported datatypes.
*
* A pool_value_t's name and string value are limited in size to
* PV_NAME_MAX_LEN and PV_VALUE_MAX_LEN respectively. Attempting to
* store values which are greater than this in length will fail with a
* POE_BADPARAM error.
*/
/*
* Get the uint64_t data held by the value. If the data type isn't
* uint64_t return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
*/
int
pool_value_get_uint64(const pool_value_t *pv, uint64_t *result)
{
if (pv->pv_class != POC_UINT) {
pool_seterror(POE_BAD_PROP_TYPE);
return (PO_FAIL);
}
*result = pv->pv_u.u;
return (PO_SUCCESS);
}
/*
* Get the int64_t data held by the value. If the data type isn't
* int64_t return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
*/
int
pool_value_get_int64(const pool_value_t *pv, int64_t *result)
{
if (pv->pv_class != POC_INT) {
pool_seterror(POE_BAD_PROP_TYPE);
return (PO_FAIL);
}
*result = pv->pv_u.i;
return (PO_SUCCESS);
}
/*
* Get the double data held by the value. If the data type isn't
* double return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
*/
int
pool_value_get_double(const pool_value_t *pv, double *result)
{
if (pv->pv_class != POC_DOUBLE) {
pool_seterror(POE_BAD_PROP_TYPE);
return (PO_FAIL);
}
*result = pv->pv_u.d;
return (PO_SUCCESS);
}
/*
* Get the boolean data held by the value. If the data type isn't
* boolean return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
*/
int
pool_value_get_bool(const pool_value_t *pv, uchar_t *result)
{
if (pv->pv_class != POC_BOOL) {
pool_seterror(POE_BAD_PROP_TYPE);
return (PO_FAIL);
}
*result = pv->pv_u.b;
return (PO_SUCCESS);
}
/*
* Get the string data held by the value. If the data type isn't
* string return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
*/
int
pool_value_get_string(const pool_value_t *pv, const char **result)
{
if (pv->pv_class != POC_STRING) {
pool_seterror(POE_BAD_PROP_TYPE);
return (PO_FAIL);
}
*result = pv->pv_u.s;
return (PO_SUCCESS);
}
/*
* Get the type of the data held by the value. If the value has never
* been used to store data, then the type is POC_INVAL.
*/
pool_value_class_t
pool_value_get_type(const pool_value_t *pv)
{
return (pv->pv_class);
}
/*
* Set the value's data to the supplied uint64_t data. Update the type
* of the value data to POC_UINT.
*/
void
pool_value_set_uint64(pool_value_t *pv, uint64_t val)
{
if (pv->pv_class == POC_STRING)
atom_free(pv->pv_u.s);
pv->pv_class = POC_UINT;
pv->pv_u.u = val;
}
/*
* Set the value's data to the supplied int64_t data. Update the type
* of the value data to POC_INT.
*/
void
pool_value_set_int64(pool_value_t *pv, int64_t val)
{
if (pv->pv_class == POC_STRING)
atom_free(pv->pv_u.s);
pv->pv_class = POC_INT;
pv->pv_u.i = val;
}
/*
* Set the value's data to the supplied double data. Update the type
* of the value data to POC_DOUBLE.
*/
void
pool_value_set_double(pool_value_t *pv, double val)
{
if (pv->pv_class == POC_STRING)
atom_free(pv->pv_u.s);
pv->pv_class = POC_DOUBLE;
pv->pv_u.d = val;
}
/*
* Set the value's data to the supplied uchar_t data. Update the type
* of the value data to POC_BOOL.
*/
void
pool_value_set_bool(pool_value_t *pv, uchar_t val)
{
if (pv->pv_class == POC_STRING)
atom_free(pv->pv_u.s);
pv->pv_class = POC_BOOL;
pv->pv_u.b = !!val; /* Lock value at 0 or 1 */
}
/*
* Try to make an internal copy of the val, returning PO_SUCCESS or
* PO_FAIL if the copy works or fails.
*/
int
pool_value_set_string(pool_value_t *pv, const char *val)
{
if (pv->pv_class == POC_STRING)
atom_free(pv->pv_u.s);
pv->pv_class = POC_STRING;
if (val == NULL || strlen(val) >= PV_VALUE_MAX_LEN) {
pool_seterror(POE_BADPARAM);
return (PO_FAIL);
} else {
if ((pv->pv_u.s = atom_string(val)) == NULL)
return (PO_FAIL);
}
return (PO_SUCCESS);
}
/*
* Allocate a pool_value_t structure and initialise it to 0. Set the
* type to POC_INVAL and return a pointer to the new pool_value_t. If
* memory allocation fails, set POE_SYSTEM and return NULL.
*/
pool_value_t *
pool_value_alloc(void)
{
pool_value_t *val;
if ((val = malloc(sizeof (pool_value_t))) == NULL) {
pool_seterror(POE_SYSTEM);
return (NULL);
}
(void) memset(val, 0, sizeof (pool_value_t));
val->pv_class = POC_INVAL;
return (val);
}
/*
* Free any atoms associated with the value and then free the value
* itself.
*/
void
pool_value_free(pool_value_t *pv)
{
if (pv->pv_name)
atom_free(pv->pv_name);
if (pv->pv_class == POC_STRING)
atom_free(pv->pv_u.s);
free(pv);
}
/*
* Return a pointer to the name of the value. This may be NULL if the
* name has never been set.
*/
const char *
pool_value_get_name(const pool_value_t *pv)
{
return (pv->pv_name);
}
/*
* Set the name of the value to the supplied name.
*/
int
pool_value_set_name(pool_value_t *pv, const char *name)
{
if (name == NULL || strlen(name) >= PV_NAME_MAX_LEN) {
pool_seterror(POE_BADPARAM);
return (PO_FAIL);
} else {
if (pv->pv_name)
atom_free(pv->pv_name);
if ((pv->pv_name = atom_string(name)) == NULL)
return (PO_FAIL);
}
return (PO_SUCCESS);
}
/*
* Use the supplied nvpair_t to set the name, type and value of the
* supplied pool_value_t.
*
* Return: PO_SUCCESS/PO_FAIL
*/
int
pool_value_from_nvpair(pool_value_t *pv, nvpair_t *pn)
{
uchar_t bval;
uint64_t uval;
int64_t ival;
double dval;
uint_t nelem;
uchar_t *dval_b;
char *sval;
if (pool_value_set_name(pv, nvpair_name(pn)) != PO_SUCCESS)
return (PO_FAIL);
switch (nvpair_type(pn)) {
case DATA_TYPE_BYTE:
if (nvpair_value_byte(pn, &bval) != 0) {
pool_seterror(POE_SYSTEM);
return (PO_FAIL);
}
pool_value_set_bool(pv, bval);
break;
case DATA_TYPE_BYTE_ARRAY:
if (nvpair_value_byte_array(pn, &dval_b, &nelem) != 0) {
pool_seterror(POE_SYSTEM);
return (PO_FAIL);
}
(void) memcpy(&dval, dval_b, sizeof (double));
pool_value_set_double(pv, dval);
break;
case DATA_TYPE_INT64:
if (nvpair_value_int64(pn, &ival) != 0) {
pool_seterror(POE_SYSTEM);
return (PO_FAIL);
}
pool_value_set_int64(pv, ival);
break;
case DATA_TYPE_UINT64:
if (nvpair_value_uint64(pn, &uval) != 0) {
pool_seterror(POE_SYSTEM);
return (PO_FAIL);
}
pool_value_set_uint64(pv, uval);
break;
case DATA_TYPE_STRING:
if (nvpair_value_string(pn, &sval) != 0) {
pool_seterror(POE_SYSTEM);
return (PO_FAIL);
}
if (pool_value_set_string(pv, sval) != PO_SUCCESS)
return (PO_FAIL);
break;
default:
pool_seterror(POE_SYSTEM);
return (PO_FAIL);
}
return (PO_SUCCESS);
}
/*
* Check to see if the values held by two supplied values are
* equal. First compare the pointers to see if we are comparing to
* ourselves, if we are return PO_TRUE. If not, get the types and
* ensure they match, if they don't return PO_FALSE. Then do a type
* specific comparison returning PO_TRUE or PO_FALSE accordingly.
*/
int
pool_value_equal(pool_value_t *pv1, pool_value_t *pv2)
{
uint64_t uval1, uval2;
int64_t ival1, ival2;
double dval1, dval2;
uchar_t bval1, bval2;
const char *sval1, *sval2;
pool_value_class_t type;
if (pv1 == pv2) /* optimisation */
return (PO_TRUE);
type = pool_value_get_type(pv1);
if (type != pool_value_get_type(pv2))
return (PO_FALSE);
switch (type) {
case POC_UINT:
(void) pool_value_get_uint64(pv1, &uval1);
(void) pool_value_get_uint64(pv2, &uval2);
if (uval1 == uval2)
return (PO_TRUE);
break;
case POC_INT:
(void) pool_value_get_int64(pv1, &ival1);
(void) pool_value_get_int64(pv2, &ival2);
if (ival1 == ival2)
return (PO_TRUE);
break;
case POC_DOUBLE:
(void) pool_value_get_double(pv1, &dval1);
(void) pool_value_get_double(pv2, &dval2);
if (dval1 == dval2)
return (PO_TRUE);
break;
case POC_BOOL:
(void) pool_value_get_bool(pv1, &bval1);
(void) pool_value_get_bool(pv2, &bval2);
if (bval1 == bval2)
return (PO_TRUE);
break;
case POC_STRING:
(void) pool_value_get_string(pv1, &sval1);
(void) pool_value_get_string(pv2, &sval2);
if (strcmp(sval1, sval2) == 0)
return (PO_TRUE);
break;
}
return (PO_FALSE);
}
#ifdef DEBUG
/*
* Trace pool_value_t details using dprintf
*/
void
pool_value_dprintf(const pool_value_t *pv)
{
const char *class_name[] = {
"POC_UINT",
"POC_INT",
"POC_DOUBLE",
"POC_BOOL",
"POC_STRING"
};
dprintf("name: %s\n", pv->pv_name ? pv->pv_name : "NULL");
if (pv->pv_class >= POC_UINT && pv->pv_class <= POC_STRING)
dprintf("type: %s\n", class_name[pv->pv_class]);
else
dprintf("type: POC_INVAL\n");
switch (pv->pv_class) {
case POC_UINT:
dprintf("value: %llu\n", pv->pv_u.u);
break;
case POC_INT:
dprintf("value: %lld\n", pv->pv_u.i);
break;
case POC_DOUBLE:
dprintf("value: %f\n", pv->pv_u.d);
break;
case POC_BOOL:
dprintf("value: %s\n", pv->pv_u.b ? "true" : "false");
break;
case POC_STRING:
dprintf("value: %s\n", pv->pv_u.s);
break;
default:
dprintf("value: invalid\n");
break;
}
}
#endif /* DEBUG */
|