From 32bd68cdee0cbebfb2abdfee1da49a0fd44eb271 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Wed, 12 Apr 2023 22:33:09 +0200 Subject: validate: Use g_file_load_bytes() That code can deal with files >4GB, the current inputstream loading code cannot. --- json-glib/json-glib-validate.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/json-glib/json-glib-validate.c b/json-glib/json-glib-validate.c index b717275..0ceda7d 100644 --- a/json-glib/json-glib-validate.c +++ b/json-glib/json-glib-validate.c @@ -46,28 +46,29 @@ static gboolean validate (JsonParser *parser, GFile *file) { - GInputStream *in; + GBytes *bytes; GError *error; gboolean res = TRUE; - gboolean parse_res; - gboolean close_res; error = NULL; - in = (GInputStream *) g_file_read (file, NULL, &error); - if (in == NULL) + bytes = g_file_load_bytes (file, NULL, NULL, &error); + if (bytes == NULL) { /* Translators: the first %s is the program name, the second one * is the URI of the file, the third is the error message. */ - g_printerr (_("%s: %s: error opening file: %s\n"), + g_printerr (_("%s: %s: error reading file: %s\n"), g_get_prgname (), g_file_get_uri (file), error->message); g_error_free (error); return FALSE; } - parse_res = json_parser_load_from_stream (parser, in, NULL, &error); - if (!parse_res) + res = json_parser_load_from_data (parser, + g_bytes_get_data (bytes, NULL), + g_bytes_get_size (bytes), + &error); + if (!res) { /* Translators: the first %s is the program name, the second one * is the URI of the file, the third is the error message. @@ -75,20 +76,9 @@ validate (JsonParser *parser, g_printerr (_("%s: %s: error parsing file: %s\n"), g_get_prgname (), g_file_get_uri (file), error->message); g_clear_error (&error); - res = FALSE; } - close_res = g_input_stream_close (in, NULL, &error); - if (!close_res) - { - /* Translators: the first %s is the program name, the second one - * is the URI of the file, the third is the error message. - */ - g_printerr (_("%s: %s: error closing: %s\n"), - g_get_prgname (), g_file_get_uri (file), error->message); - g_clear_error (&error); - res = FALSE; - } + g_bytes_unref (bytes); return res; } -- cgit v1.2.1