mirror of
https://github.com/openvswitch/ovs
synced 2025-10-27 15:18:06 +00:00
test-json: Avoid use of /dev/stdin to fix builds in limited chroots.
The chroots in which we often build Open vSwitch don't have /proc and thus cannot support /dev/stdin, because on Linux that is a symlink to /proc/self/fd/0. So avoid using /dev/stdin in the testsuite.
This commit is contained in:
26
lib/json.c
26
lib/json.c
@@ -1002,18 +1002,34 @@ json_from_string(const char *string)
|
||||
struct json *
|
||||
json_from_file(const char *file_name)
|
||||
{
|
||||
struct json_parser *p;
|
||||
struct json *json;
|
||||
FILE *stream;
|
||||
|
||||
/* Open file. */
|
||||
stream = fopen(file_name, "r");
|
||||
if (!stream) {
|
||||
return json_string_create_nocopy(
|
||||
xasprintf("error opening \"%s\": %s", file_name, strerror(errno)));
|
||||
}
|
||||
json = json_from_stream(stream);
|
||||
fclose(stream);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
/* Parses the contents of 'stream' as a JSON object or array, and returns a
|
||||
* newly allocated 'struct json'. The caller must free the returned structure
|
||||
* with json_destroy() when it is no longer needed.
|
||||
*
|
||||
* The file must be encoded in UTF-8.
|
||||
*
|
||||
* See json_from_string() for return value semantics.
|
||||
*/
|
||||
struct json *
|
||||
json_from_stream(FILE *stream)
|
||||
{
|
||||
struct json_parser *p;
|
||||
struct json *json;
|
||||
|
||||
/* Read and parse file. */
|
||||
p = json_parser_create(JSPF_TRAILER);
|
||||
for (;;) {
|
||||
char buffer[BUFSIZ];
|
||||
@@ -1026,13 +1042,11 @@ json_from_file(const char *file_name)
|
||||
}
|
||||
json = json_parser_finish(p);
|
||||
|
||||
/* Close file and check for I/O errors. */
|
||||
if (ferror(stream)) {
|
||||
json_destroy(json);
|
||||
json = json_string_create_nocopy(
|
||||
xasprintf("error reading \"%s\": %s", file_name, strerror(errno)));
|
||||
xasprintf("error reading JSON stream: %s", strerror(errno)));
|
||||
}
|
||||
fclose(stream);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -111,6 +111,7 @@ void json_parser_abort(struct json_parser *);
|
||||
|
||||
struct json *json_from_string(const char *string);
|
||||
struct json *json_from_file(const char *file_name);
|
||||
struct json *json_from_stream(FILE *stream);
|
||||
|
||||
/* Serializing JSON. */
|
||||
|
||||
|
||||
@@ -67,23 +67,17 @@ refill(FILE *file, void *buffer, size_t buffer_size, size_t *n, size_t *used)
|
||||
}
|
||||
|
||||
static bool
|
||||
parse_multiple(const char *input_file)
|
||||
parse_multiple(FILE *stream)
|
||||
{
|
||||
struct json_parser *parser;
|
||||
char buffer[BUFSIZ];
|
||||
size_t n, used;
|
||||
FILE *file;
|
||||
bool ok;
|
||||
|
||||
file = fopen(input_file, "r");
|
||||
if (!file) {
|
||||
ovs_fatal(errno, "Cannot open \"%s\"", input_file);
|
||||
}
|
||||
|
||||
parser = NULL;
|
||||
n = used = 0;
|
||||
ok = true;
|
||||
while (used < n || refill(file, buffer, sizeof buffer, &n, &used)) {
|
||||
while (used < n || refill(stream, buffer, sizeof buffer, &n, &used)) {
|
||||
if (!parser && isspace((unsigned char) buffer[used])) {
|
||||
/* Skip white space. */
|
||||
used++;
|
||||
@@ -113,6 +107,7 @@ int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
const char *input_file;
|
||||
FILE *stream;
|
||||
bool ok;
|
||||
|
||||
set_program_name(argv[0]);
|
||||
@@ -146,14 +141,15 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
input_file = argv[optind];
|
||||
if (!strcmp(input_file, "-")) {
|
||||
input_file = "/dev/stdin";
|
||||
stream = !strcmp(input_file, "-") ? stdin : fopen(input_file, "r");
|
||||
if (!stream) {
|
||||
ovs_fatal(errno, "Cannot open \"%s\"", input_file);
|
||||
}
|
||||
|
||||
if (multiple) {
|
||||
ok = parse_multiple(input_file);
|
||||
ok = parse_multiple(stream);
|
||||
} else {
|
||||
ok = print_and_free_json(json_from_file(input_file));
|
||||
ok = print_and_free_json(json_from_stream(stream));
|
||||
}
|
||||
|
||||
return !ok;
|
||||
|
||||
Reference in New Issue
Block a user