forked from sbabic/swupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswupdate_dict.c
214 lines (169 loc) · 4.25 KB
/
swupdate_dict.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
/*
* (C) Copyright 2016
* Stefano Babic, DENX Software Engineering, [email protected].
*
* Copyright (C) 2018 Weidmüller Interface GmbH & Co. KG
* Stefan Herbrechtsmeier <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <assert.h>
#include "generated/autoconf.h"
#include "bsdqueue.h"
#include "util.h"
#include "swupdate_dict.h"
static int insert_list_elem(struct dict_list *list, const char *value)
{
struct dict_list_elem *elem = (struct dict_list_elem *)malloc(sizeof(*elem));
if (!elem)
return -ENOMEM;
memset(elem, 0, sizeof(*elem));
elem->value = strdup(value);
LIST_INSERT_HEAD(list, elem, next);
return 0;
}
static void remove_list_elem(struct dict_list_elem *elem)
{
LIST_REMOVE(elem, next);
free(elem->value);
free(elem);
}
static void remove_list(struct dict_list *list)
{
struct dict_list_elem *elem;
struct dict_list_elem *tmp;
LIST_FOREACH_SAFE(elem, list, next, tmp) {
remove_list_elem(elem);
}
}
static struct dict_entry *insert_entry(struct dict *dictionary, const char *key)
{
struct dict_entry *entry = (struct dict_entry *)malloc(sizeof(*entry));
if (!entry)
return NULL;
memset(entry, 0, sizeof(*entry));
entry->key = strdup(key);
LIST_INSERT_HEAD(dictionary, entry, next);
return entry;
}
static struct dict_entry *get_entry(struct dict *dictionary, const char *key)
{
struct dict_entry *entry;
LIST_FOREACH(entry, dictionary, next) {
if (strcmp(key, entry->key) == 0)
return entry;
}
return NULL;
}
static void remove_entry(struct dict_entry *entry)
{
LIST_REMOVE(entry, next);
free(entry->key);
remove_list(&entry->list);
free(entry);
}
char *dict_entry_get_key(struct dict_entry *entry)
{
if (!entry)
return NULL;
return entry->key;
}
char *dict_entry_get_value(struct dict_entry *entry)
{
if (!entry || !LIST_FIRST(&entry->list))
return NULL;
return LIST_FIRST(&entry->list)->value;
}
struct dict_list *dict_get_list(struct dict *dictionary, const char *key)
{
struct dict_entry *entry = get_entry(dictionary, key);
if (!entry)
return NULL;
return &entry->list;
}
char *dict_get_value(struct dict *dictionary, const char *key)
{
struct dict_entry *entry = get_entry(dictionary, key);
if (!entry)
return NULL;
return dict_entry_get_value(entry);
}
int dict_insert_value(struct dict *dictionary, const char *key, const char *value)
{
struct dict_entry *entry = get_entry(dictionary, key);
if (!entry) {
entry = insert_entry(dictionary, key);
if (!entry)
return -ENOMEM;
}
return insert_list_elem(&entry->list, value);
}
int dict_set_value(struct dict *dictionary, const char *key, const char *value)
{
struct dict_entry *entry = get_entry(dictionary, key);
if (entry)
remove_entry(entry);
entry = insert_entry(dictionary, key);
if (!entry)
return -ENOMEM;
return insert_list_elem(&entry->list, value);
}
void dict_remove(struct dict *dictionary, const char *key)
{
struct dict_entry *entry = get_entry(dictionary, key);
if (!entry)
return;
remove_entry(entry);
}
void dict_drop_db(struct dict *dictionary)
{
struct dict_entry *entry;
struct dict_entry *tmp;
LIST_FOREACH_SAFE(entry, dictionary, next, tmp) {
remove_entry(entry);
}
}
int dict_parse_script(struct dict *dictionary, const char *script)
{
FILE *fp = NULL;
int ret = 0;
char *line = NULL, *key = NULL, *value = NULL;
size_t len = 0;
char *saveptr;
/* open script generated during sw-description parsing */
fp = fopen(script, "rb");
if (!fp) {
ERROR("Failed to open script file: %s\n", script);
ret = -1;
goto cleanup;
}
/* load key-value pairs from script into dictionary */
while ((getline(&line, &len, fp)) != -1) {
key = strtok_r(line, " \t\n", &saveptr);
value = strtok_r(NULL, "\t\n", &saveptr);
if (value != NULL && key != NULL) {
ret = dict_set_value(dictionary, key, value);
if (ret) {
ERROR("Adding pair [%s] = %s into dictionary"
"list failed\n", key, value);
goto cleanup;
}
}
if (value == NULL && key != NULL) {
dict_remove(dictionary, key);
}
}
cleanup:
if (fp) fclose(fp);
/* free(null) should not harm anything */
free(line);
return ret;
}