-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvl_convert.c
148 lines (129 loc) · 3.39 KB
/
nvl_convert.c
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
/*
* Copyright (c) 2014-2017 Josef 'Jeff' Sipek <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <jeffpc/nvl.h>
/*
* Check if a condition is true. Returns:
*
* 0 if true
* -EBUSY if false
* -EINVAL if args are wrong
*/
static int check_condition_str(struct str *str, enum nvcvtcond cond)
{
switch (cond) {
case NVCVT_COND_ALWAYS:
return 0;
case NVCVT_COND_STR_EMPTY:
return str_len(str) ? -EBUSY : 0;
}
return -EINVAL;
}
static int cvt_string(struct nvlist *nvl, const struct nvpair *pair,
enum val_type tgt, enum nvcvtcond cond)
{
const char *name = nvpair_name(pair);
struct str *str;
uint64_t intval;
int ret = -ENOTSUP;
ASSERT3U(nvpair_type(pair), ==, VT_STR);
str = nvpair_value_str(pair);
if (IS_ERR(str)) {
VERIFY3S(PTR_ERR(str), !=, -ENOENT);
VERIFY3S(PTR_ERR(str), !=, -ERANGE);
return PTR_ERR(str);
}
/* check if the specified condition is true */
ret = check_condition_str(str, cond);
if (ret != 0) {
str_putref(str);
return (ret == -EBUSY) ? 0 : ret;
}
switch (tgt) {
case VT_ARRAY:
case VT_BLOB:
case VT_BOOL:
case VT_SYM:
case VT_CONS:
case VT_CHAR:
ret = -ENOTSUP;
break;
case VT_INT:
ret = str2u64(str_cstr(str), &intval);
if (!ret)
ret = nvl_set_int(nvl, name, intval);
break;
case VT_NULL:
ret = nvl_set_null(nvl, name);
break;
case VT_NVL:
ret = -ENOTSUP;
break;
case VT_STR:
/* nothing to do */
ret = 0;
break;
}
str_putref(str);
return ret;
}
#define CHECK_ERROR(e, cvtall) \
((!(cvtall) && (e)) || \
((cvtall) && ((e) == -ENOTSUP)))
int nvl_convert(struct nvlist *nvl, const struct nvl_convert_info *table,
bool convert_all)
{
/*
* If we weren't given a table, we have nothing to do.
*/
if (!table)
return 0;
for (; table->name; table++) {
const struct nvpair *pair;
int ret = -ENOTSUP;
pair = nvl_lookup(nvl, table->name);
if (IS_ERR(pair)) {
ret = PTR_ERR(pair);
if ((ret == -ENOENT) || !CHECK_ERROR(ret, convert_all))
continue;
return ret;
}
switch (nvpair_type(pair)) {
case VT_ARRAY:
case VT_BLOB:
case VT_BOOL:
case VT_INT:
case VT_NULL:
case VT_NVL:
case VT_SYM:
case VT_CONS:
case VT_CHAR:
return -ENOTSUP;
case VT_STR:
ret = cvt_string(nvl, pair, table->tgt_type,
table->cond);
break;
}
if (CHECK_ERROR(ret, convert_all))
return ret;
}
return 0;
}