From c70e06b42823950d7adeabf100cba6a004f9199f Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 13 Apr 2023 13:29:14 +0100 Subject: Add getter for GVDB table contents The xdg-desktop-portal project is using GVDB as the basis for the permissions store. Each permission table is stores as a block of bytes inside another GVDB table, in order to isolate the permissions of each application. Currently, xdg-desktop-portal uses GVDB as a copy-paste library; the only missing API to avoid that, and switch GVDB to a proper subproject, is a way to extract the contents of a GVDB table into a GBytes buffer. --- gvdb/gvdb-builder.c | 34 +++++++++++++++++++++++++--------- gvdb/gvdb-builder.h | 4 ++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/gvdb/gvdb-builder.c b/gvdb/gvdb-builder.c index 5dae03e..336f6bb 100644 --- a/gvdb/gvdb-builder.c +++ b/gvdb/gvdb-builder.c @@ -509,28 +509,44 @@ file_builder_serialise (FileBuilder *fb, return result; } +GBytes * +gvdb_table_get_contents (GHashTable *table, + gboolean byteswap) +{ + struct gvdb_pointer root; + FileBuilder *fb; + GString *str; + GBytes *res; + gsize str_len; + + fb = file_builder_new (byteswap); + file_builder_add_hash (fb, table, &root); + str = file_builder_serialise (fb, root); + + str_len = str->len; + res = g_bytes_new_take (g_string_free (str, FALSE), str_len); + + return res; +} + gboolean gvdb_table_write_contents (GHashTable *table, const gchar *filename, gboolean byteswap, GError **error) { - struct gvdb_pointer root; + GBytes *content; gboolean status; - FileBuilder *fb; - GString *str; g_return_val_if_fail (table != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - fb = file_builder_new (byteswap); - file_builder_add_hash (fb, table, &root); - str = file_builder_serialise (fb, root); - file_builder_free (fb); + content = gvdb_table_get_contents (table, byteswap); + + status = g_file_set_contents (filename, g_bytes_get_data (content, NULL), g_bytes_get_size (content), error); - status = g_file_set_contents (filename, str->str, str->len, error); - g_string_free (str, TRUE); + g_bytes_unref (content); return status; } diff --git a/gvdb/gvdb-builder.h b/gvdb/gvdb-builder.h index 30757d0..e64a7b9 100644 --- a/gvdb/gvdb-builder.h +++ b/gvdb/gvdb-builder.h @@ -63,4 +63,8 @@ gboolean gvdb_table_write_contents_finish (GHashTa GAsyncResult *result, GError **error); +G_GNUC_INTERNAL +GBytes * gvdb_table_get_contents (GHashTable *table, + gboolean byteswap); + #endif /* __gvdb_builder_h__ */ -- cgit v1.2.1