Skip to content

Commit 3dfc585

Browse files
authored
Merge pull request #211 from hreinecke/config-errors
Return json parsing errors
2 parents e97b587 + bee678d commit 3dfc585

4 files changed

Lines changed: 40 additions & 12 deletions

File tree

src/nvme/json.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <stdio.h>
1010
#include <errno.h>
1111
#include <string.h>
12+
#include <unistd.h>
13+
#include <fcntl.h>
1214

1315
#include <json.h>
1416

@@ -130,7 +132,8 @@ static void json_parse_subsys(nvme_host_t h, struct json_object *subsys_obj)
130132
struct json_object *port_obj;
131133

132134
port_obj = json_object_array_get_idx(port_array, p);
133-
json_parse_port(s, port_obj);
135+
if (port_obj)
136+
json_parse_port(s, port_obj);
134137
}
135138
}
136139

@@ -157,26 +160,38 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj)
157160
return;
158161
for (s = 0; s < json_object_array_length(subsys_array); s++) {
159162
subsys_obj = json_object_array_get_idx(subsys_array, s);
160-
json_parse_subsys(h, subsys_obj);
163+
if (subsys_obj)
164+
json_parse_subsys(h, subsys_obj);
161165
}
162166
}
163167

164-
void json_read_config(nvme_root_t r, const char *config_file)
168+
int json_read_config(nvme_root_t r, const char *config_file)
165169
{
166170
struct json_object *json_root, *host_obj;
167-
int h;
171+
int fd, h;
168172

169-
json_root = json_object_from_file(config_file);
173+
fd = open(config_file, O_RDONLY);
174+
if (fd < 0) {
175+
nvme_msg(r, LOG_DEBUG, "Error opening %s, %s\n",
176+
config_file, strerror(errno));
177+
return fd;
178+
}
179+
json_root = json_object_from_fd(fd);
170180
if (!json_root) {
171181
nvme_msg(r, LOG_DEBUG, "Failed to read %s, %s\n",
172182
config_file, json_util_get_last_err());
173-
return;
183+
errno = EPROTO;
184+
close(fd);
185+
return -1;
174186
}
175187
for (h = 0; h < json_object_array_length(json_root); h++) {
176188
host_obj = json_object_array_get_idx(json_root, h);
177-
json_parse_host(r, host_obj);
189+
if (host_obj)
190+
json_parse_host(r, host_obj);
178191
}
179192
json_object_put(json_root);
193+
close(fd);
194+
return 0;
180195
}
181196

182197
#define JSON_STRING_OPTION(c, p, o) \

src/nvme/private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ struct nvme_root {
129129

130130
int nvme_set_attr(const char *dir, const char *attr, const char *value);
131131

132-
void json_read_config(nvme_root_t r, const char *config_file);
132+
int json_read_config(nvme_root_t r, const char *config_file);
133133

134134
int json_update_config(nvme_root_t r, const char *config_file);
135135

src/nvme/tree.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,25 @@ nvme_root_t nvme_create_root(FILE *fp, int log_level)
137137
return r;
138138
}
139139

140-
void nvme_read_config(nvme_root_t r, const char *config_file)
140+
int nvme_read_config(nvme_root_t r, const char *config_file)
141141
{
142+
int err = -1;
142143
#ifdef CONFIG_JSONC
143144
if (r && config_file) {
144-
json_read_config(r, config_file);
145-
r->config_file = strdup(config_file);
145+
err = json_read_config(r, config_file);
146+
if (!err)
147+
r->config_file = strdup(config_file);
148+
/*
149+
* The json configuration file is optional,
150+
* so ignore errors when opening the file.
151+
*/
152+
if (err < 0 && errno != EPROTO)
153+
err = 0;
146154
}
155+
#else
156+
errno = ENOTSUP;
147157
#endif
158+
return err;
148159
}
149160

150161
nvme_root_t nvme_scan(const char *config_file)

src/nvme/tree.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,10 @@ nvme_root_t nvme_scan(const char *config_file);
10811081
*
10821082
* Read in the contents of @config_file and merge them with
10831083
* the elements in @r.
1084+
*
1085+
* Returns: 0 on success, -1 on failure with errno set.
10841086
*/
1085-
void nvme_read_config(nvme_root_t r, const char *config_file);
1087+
int nvme_read_config(nvme_root_t r, const char *config_file);
10861088

10871089
/**
10881090
* nvme_refresh_topology() - refresh nvme_root_t object contents

0 commit comments

Comments
 (0)