From 2d6374822b481e885aa5f74ef9e6f993d5c792c5 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 15 Aug 2013 09:17:37 -0400 Subject: Initial basic static delta code drop This has a very basic level of functionality (deltas can be generated, and applied offline). There is only some stubbed out pull code to fetch them via HTTP. But, better to commit this now and improve it from a known starting point, rather than have it languish in a branch. --- src/libotutil/ot-variant-utils.c | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'src/libotutil/ot-variant-utils.c') diff --git a/src/libotutil/ot-variant-utils.c b/src/libotutil/ot-variant-utils.c index 291f7466..0f8e4bee 100644 --- a/src/libotutil/ot-variant-utils.c +++ b/src/libotutil/ot-variant-utils.c @@ -23,8 +23,10 @@ #include "config.h" #include +#include #include +#include #include "otutil.h" @@ -149,6 +151,55 @@ ot_util_variant_map (GFile *src, return ret; } +typedef struct { + gpointer addr; + gsize len; +} VariantMapData; + +static void +variant_map_data_destroy (gpointer data) +{ + VariantMapData *mdata = data; + (void) munmap (mdata->addr, mdata->len); +} + +gboolean +ot_util_variant_map_fd (GFileDescriptorBased *stream, + goffset start, + const GVariantType *type, + gboolean trusted, + GVariant **out_variant, + GError **error) +{ + gboolean ret = FALSE; + gpointer map; + struct stat stbuf; + VariantMapData *mdata = NULL; + gsize len; + + if (!gs_stream_fstat (stream, &stbuf, NULL, error)) + goto out; + + len = stbuf.st_size - start; + map = mmap (NULL, len, PROT_READ, MAP_PRIVATE, + g_file_descriptor_based_get_fd (stream), start); + if (!map) + { + ot_util_set_error_from_errno (error, errno); + goto out; + } + + mdata = g_new (VariantMapData, 1); + mdata->addr = map; + mdata->len = len; + + ret = TRUE; + *out_variant = g_variant_new_from_data (type, map, len, trusted, + variant_map_data_destroy, mdata); + out: + return ret; +} + /** * Read all input from @src, allocating a new #GVariant from it into * output variable @out_variant. @src will be closed as a result. @@ -219,3 +270,17 @@ ot_util_variant_builder_from_variant (GVariant *variant, return builder; } +GVariant * +ot_variant_new_from_bytes (const GVariantType *type, + GBytes *bytes, + gboolean trusted) +{ +#if GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_36 + return g_variant_new_from_bytes (type, bytes, trusted); +#else + gsize size; + gconstpointer data = g_bytes_get_data (bytes, &size); + return g_variant_new_from_data (type, data, size, trusted, + (GDestroyNotify)g_bytes_unref, bytes); +#endif +} -- cgit v1.2.1