forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
readv2.c
227 lines (205 loc) · 5.92 KB
/
readv2.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
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
/**
* @file
*
* @brief Source for quickdump plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
static int readVersion2 (FILE * file, KeySet * returned, Key * parentKey)
{
// setup buffers
struct stringbuffer valueBuffer;
setupBuffer (&valueBuffer, 4);
struct stringbuffer metaNameBuffer;
setupBuffer (&metaNameBuffer, 4);
// setup name buffer with parent key
struct stringbuffer nameBuffer;
size_t parentSize = keyGetNameSize (parentKey); // includes null terminator
setupBuffer (&nameBuffer, parentSize + 4);
keyGetName (parentKey, nameBuffer.string, parentSize);
nameBuffer.string[parentSize - 1] = '/'; // replaces null terminator
nameBuffer.string[parentSize] = '\0'; // set new null terminator
nameBuffer.offset = parentSize; // set offset to null terminator
char c;
while ((c = fgetc (file)) != EOF)
{
ungetc (c, file);
if (!readStringIntoBuffer (file, &nameBuffer, parentKey))
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
char type = fgetc (file);
if (type == EOF)
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
ELEKTRA_SET_RESOURCE_ERROR (parentKey, "Missing key type");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
Key * k;
switch (type)
{
case 'b':
{
// binary key value
kdb_unsigned_long_long_t valueSize;
if (!readUInt64 (file, &valueSize, parentKey))
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (valueSize == 0)
{
k = keyNew (nameBuffer.string, KEY_BINARY, KEY_SIZE, valueSize, KEY_END);
}
else
{
void * value = elektraMalloc (valueSize);
if (fread (value, sizeof (char), valueSize, file) < valueSize)
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
fclose (file);
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Error while reading file: Reason: %s", strerror (errno));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
k = keyNew (nameBuffer.string, KEY_BINARY, KEY_SIZE, (size_t) valueSize, KEY_VALUE, value, KEY_END);
elektraFree (value);
}
break;
}
case 's':
{
// string key value
if (!readStringIntoBuffer (file, &valueBuffer, parentKey))
{
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
k = keyNew (nameBuffer.string, KEY_VALUE, valueBuffer.string, KEY_END);
break;
}
default:
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Unknown key type %c", type);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
while ((c = fgetc (file)) != 0)
{
if (c == EOF)
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
ELEKTRA_SET_RESOURCE_ERROR (parentKey, "Missing key end");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
switch (c)
{
case 'm':
{
// meta key
if (!readStringIntoBuffer (file, &metaNameBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!readStringIntoBuffer (file, &valueBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
const char * metaValue = valueBuffer.string;
keySetMeta (k, metaNameBuffer.string, metaValue);
break;
}
case 'c':
{
// copy meta
if (!readStringIntoBuffer (file, &nameBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (!readStringIntoBuffer (file, &metaNameBuffer, parentKey))
{
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
const Key * sourceKey = ksLookupByName (returned, nameBuffer.string, 0);
if (sourceKey == NULL)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not copy meta data from key '%s': Key not found",
nameBuffer.string);
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (keyCopyMeta (k, sourceKey, metaNameBuffer.string) != 1)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not copy meta data from key '%s': Error during copy",
&nameBuffer.string[nameBuffer.offset]);
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
break;
}
default:
keyDel (k);
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Unknown meta type %c", type);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
ksAppendKey (returned, k);
}
elektraFree (nameBuffer.string);
elektraFree (metaNameBuffer.string);
elektraFree (valueBuffer.string);
fclose (file);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}