diff --git a/gio/gresource.c b/gio/gresource.c index b7222b8eb..79a49d33d 100644 --- a/gio/gresource.c +++ b/gio/gresource.c @@ -800,7 +800,9 @@ g_resource_lookup_data (GResource *resource, if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error)) return NULL; - if (flags & G_RESOURCE_FLAGS_COMPRESSED) + if (size == 0) + return g_bytes_new_with_free_func ("", 0, (GDestroyNotify) g_resource_unref, g_resource_ref (resource)); + else if (flags & G_RESOURCE_FLAGS_COMPRESSED) { char *uncompressed, *d; const char *s; diff --git a/gio/tests/empty.txt b/gio/tests/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/gio/tests/resources.c b/gio/tests/resources.c index 0749b7d01..325775fd6 100644 --- a/gio/tests/resources.c +++ b/gio/tests/resources.c @@ -68,6 +68,15 @@ test_resource (GResource *resource) g_assert_cmpint (size, ==, 6); g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED); + found = g_resource_get_info (resource, + "/empty.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, + &size, &flags, &error); + g_assert_true (found); + g_assert_no_error (error); + g_assert_cmpint (size, ==, 0); + g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED); + found = g_resource_get_info (resource, "/a_prefix/test2.txt", G_RESOURCE_LOOKUP_FLAGS_NONE, @@ -105,6 +114,14 @@ test_resource (GResource *resource) g_assert_no_error (error); g_bytes_unref (data); + data = g_resource_lookup_data (resource, + "/empty.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, + &error); + g_assert_cmpuint (g_bytes_get_size (data), ==, 0); + g_assert_no_error (error); + g_bytes_unref (data); + for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++) { in = g_resource_open_stream (resource, @@ -136,6 +153,24 @@ test_resource (GResource *resource) g_assert_no_error (error); g_clear_object (&in); + in = g_resource_open_stream (resource, + "/empty.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, + &error); + g_assert_no_error (error); + g_assert_nonnull (in); + + success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1, + &size, + NULL, &error); + g_assert_no_error (error); + g_assert_true (success); + g_assert_cmpint (size, ==, 0); + + g_input_stream_close (in, NULL, &error); + g_assert_no_error (error); + g_clear_object (&in); + data = g_resource_lookup_data (resource, "/a_prefix/test2.txt", G_RESOURCE_LOOKUP_FLAGS_NONE, @@ -398,6 +433,14 @@ test_resource_registered (void) g_assert_cmpint (size, ==, 6); g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED)); + found = g_resources_get_info ("/empty.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, + &size, &flags, &error); + g_assert_no_error (error); + g_assert_true (found); + g_assert_cmpint (size, ==, 0); + g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED)); + found = g_resources_get_info ("/a_prefix/test2.txt", G_RESOURCE_LOOKUP_FLAGS_NONE, &size, &flags, &error); @@ -440,6 +483,30 @@ test_resource_registered (void) g_assert_no_error (error); g_clear_object (&in); + data = g_resources_lookup_data ("/empty.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, + &error); + g_assert_no_error (error); + g_assert_cmpuint (g_bytes_get_size (data), ==, 0); + g_bytes_unref (data); + + in = g_resources_open_stream ("/empty.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, + &error); + g_assert_no_error (error); + g_assert_nonnull (in); + + success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1, + &size, + NULL, &error); + g_assert_no_error (error); + g_assert_true (success); + g_assert_cmpint (size, ==, 0); + + g_input_stream_close (in, NULL, &error); + g_assert_no_error (error); + g_clear_object (&in); + data = g_resources_lookup_data ("/a_prefix/test2.txt", G_RESOURCE_LOOKUP_FLAGS_NONE, &error); diff --git a/gio/tests/test.gresource.xml b/gio/tests/test.gresource.xml index dd08aa006..62a31f496 100644 --- a/gio/tests/test.gresource.xml +++ b/gio/tests/test.gresource.xml @@ -4,6 +4,7 @@ test-generated.txt test1.txt test.gresource.xml + empty.txt test2.txt