1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2011 Red Hat, Inc
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Alexander Larsson <alexl@redhat.com>
27 #include "gresource.h"
28 #include <gvdb/gvdb-reader.h>
30 #include <gio/gmemoryinputstream.h>
31 #include <gio/gzlibdecompressor.h>
32 #include <gio/gconverterinputstream.h>
41 G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref)
45 * @short_description: Resource framework
48 * Applications and libraries often contain binary or textual data that is really part of the
49 * application, rather than user data. For instance #GtkBuilder .ui files, splashscreen images,
50 * GMenu markup xml, CSS files, icons, etc. These are often shipped as files in <filename>$datadir/appname</filename>, or
51 * manually included as literal strings in the code.
53 * The #GResource API and the <link linkend="glib-compile-schemas">glib-compile-resources</link> program
54 * provide a convenient and efficient alternative to this which has some nice properties. You
55 * maintain the files as normal files, so its easy to edit them, but during the build the files
56 * are combined into a binary bundle that is linked into the executable. This means that loading
57 * the resource files are efficient (as they are already in memory, shared with other instances) and
58 * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
59 * also makes it easier to create relocatable applications.
61 * Resource files can also be marked as compresses. Such files will be included in the resource bundle
62 * in a compressed form, but will be automatically uncompressed when the resource is used. This
63 * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
65 * Resource bundles are created by the <link linkend="glib-compile-schemas">glib-compile-resources</link> program
66 * which takes an xml file that describes the bundle, and a set of files that the xml references. These
67 * are combined into a binary resource bundle.
69 * <example id="resource-example"><title>Example resource description</title>
70 * <programlisting><![CDATA[
71 * <?xml version="1.0" encoding="UTF-8"?>
73 * <gresource prefix="/org/gtk/Example">
74 * <file>data/splashscreen.png</file>
75 * <file compressed="true">dialog.ui</file>
76 * <file>menumarkup.xml</file>
79 * ]]></programlisting></example>
81 * This will create a resource bundle with the following files:
82 * <programlisting><![CDATA[
83 * /org/gtk/Example/data/splashscreen.png
84 * /org/gtk/Example/dialog.ui
85 * /org/gtk/Example/menumarkup.xml
86 * ]]></programlisting>
88 * Note that all resources in the process share the same namespace, so use java-style
89 * path prefixes (like in the above example) to avoid conflicts.
91 * You can then use <link linkend="glib-compile-schemas">glib-compile-resources</link> to compile the xml to a
92 * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
93 * --generate-header arguments to create a source file and header to link directly into your application.
95 * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
96 * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
97 * to the data. You can also use uris like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
100 * There are two forms of the generated source, the default version uses the compiler support for constructor
101 * and destructor functions (where availible) to automatically create and register the #GResource on startup
102 * or library load time. If you pass --manual-register two functions to register/unregister the resource is instead
103 * created. This requires an explicit initialization call in your application/library, but it works on all platforms,
104 * even on the minor ones where this is not availible. (Constructor support is availible for at least Win32, MacOS and Linux.)
106 * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
107 * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
108 * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
109 * is for your own resources, and resource data is often used once, during parsing, and then released.
115 * g_resource_error_quark:
117 * Gets the #GResource Error Quark.
119 * Return value: a #GQuark.
124 g_resource_error_quark (void)
126 return g_quark_from_static_string ("g-resource-error-quark");
131 * @resource: A #GResource.
133 * Atomically increments the reference count of @array by one. This
134 * function is MT-safe and may be called from any thread.
136 * Returns: The passed in #GResource.
141 g_resource_ref (GResource *resource)
143 g_atomic_int_inc (&resource->ref_count);
149 * @resource: A #GResource.
151 * Atomically decrements the reference count of @resource by one. If the
152 * reference count drops to 0, all memory allocated by the array is
153 * released. This function is MT-safe and may be called from any
159 g_resource_unref (GResource *resource)
161 if (g_atomic_int_dec_and_test (&resource->ref_count))
163 gvdb_table_unref (resource->table);
169 * g_resource_new_from_data:
171 * @error: return location for a #GError, or %NULL.
173 * Creates a GResource from a reference to the binary resource bundle.
174 * This will keep a reference to @data while the resource lives, so
175 * the data should not be modified or freed.
177 * If you want to use this resource in the global resource namespace you need
178 * to register it with g_resources_register().
180 * Return value: (transfer full): a new #GResource, or %NULL on error.
185 g_resource_new_from_data (GBytes *data,
191 table = gvdb_table_new_from_data (g_bytes_get_data (data, NULL),
192 g_bytes_get_size (data),
195 (GvdbRefFunc)g_bytes_ref,
196 (GDestroyNotify)g_bytes_unref,
202 resource = g_new0 (GResource, 1);
203 resource->ref_count = 1;
204 resource->table = table;
211 * @filename: (type filename): the path of a filename to load, in the GLib filename encoding.
212 * @error: return location for a #GError, or %NULL.
214 * Loads a binary resource bundle and creates a #GResource representation of it, allowing
215 * you to query it for data.
217 * If you want to use this resource in the global resource namespace you need
218 * to register it with g_resources_register().
220 * Return value: (transfer full): a new #GResource, or %NULL on error.
225 g_resource_load (const gchar *filename,
231 table = gvdb_table_new (filename, FALSE, error);
235 resource = g_new0 (GResource, 1);
236 resource->table = table;
241 static gboolean do_lookup (GResource *resource,
243 GResourceLookupFlags lookup_flags,
250 char *free_path = NULL;
252 gboolean res = FALSE;
255 path_len = strlen (path);
256 if (path[path_len-1] == '/')
258 path = free_path = g_strdup (path);
259 free_path[path_len-1] = 0;
262 value = gvdb_table_get_value (resource->table, path);
266 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
267 _("The resource at '%s' does not exist"),
272 guint32 _size, _flags;
275 g_variant_get (value, "(uu@ay)",
287 *data = g_variant_get_data (array);
290 /* Don't report trailing newline that non-compressed files has */
291 if (_flags & G_RESOURCE_FLAGS_COMPRESSED)
292 *data_size = g_variant_get_size (array);
294 *data_size = g_variant_get_size (array) - 1;
305 * g_resource_open_stream:
306 * @resource: A #GResource.
307 * @path: A pathname inside the resource.
308 * @lookup_flags: A #GResourceLookupFlags.
309 * @error: return location for a #GError, or %NULL.
311 * Looks for a file at the specified @path in the resource and
312 * returns a #GInputStream that lets you read the data.
314 * @lookup_flags controls the behaviour of the lookup.
316 * Returns: (transfer full): #GInputStream or %NULL on error.
317 * Free the returned object with g_object_unref().
322 g_resource_open_stream (GResource *resource,
324 GResourceLookupFlags lookup_flags,
330 GInputStream *stream, *stream2;
332 if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error))
335 stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
336 g_object_set_data_full (G_OBJECT (stream), "g-resource",
337 g_resource_ref (resource),
338 (GDestroyNotify)g_resource_unref);
340 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
342 GZlibDecompressor *decompressor =
343 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
345 stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor));
346 g_object_unref (decompressor);
347 g_object_unref (stream);
355 * g_resource_lookup_data:
356 * @resource: A #GResource.
357 * @path: A pathname inside the resource.
358 * @lookup_flags: A #GResourceLookupFlags.
359 * @error: return location for a #GError, or %NULL.
361 * Looks for a file at the specified @path in the resource and
362 * returns a #GBytes that lets you directly access the data in
365 * The data is always followed by a zero byte, so you
366 * can safely use the data as a C string. However, that byte
367 * is not included in the size of the GBytes.
369 * For uncompressed resource files this is a pointer directly into
370 * the resource bundle, which is typically in some readonly data section
371 * in the program binary. For compressed files we allocate memory on
372 * the heap and automatically uncompress the data.
374 * @lookup_flags controls the behaviour of the lookup.
376 * Returns: (transfer full): #GBytes or %NULL on error.
377 * Free the returned object with g_bytes_unref().
382 g_resource_lookup_data (GResource *resource,
384 GResourceLookupFlags lookup_flags,
392 if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
395 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
397 char *uncompressed, *d;
399 GConverterResult res;
400 gsize d_size, s_size;
401 gsize bytes_read, bytes_written;
404 GZlibDecompressor *decompressor =
405 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
407 uncompressed = g_malloc (size + 1);
416 res = g_converter_convert (G_CONVERTER (decompressor),
419 G_CONVERTER_INPUT_AT_END,
423 if (res == G_CONVERTER_ERROR)
425 g_free (uncompressed);
426 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL,
427 _("The resource at '%s' failed to decompress"),
433 s_size -= bytes_read;
435 d_size -= bytes_written;
437 while (res != G_CONVERTER_FINISHED);
439 uncompressed[size] = 0; /* Zero terminate */
441 return g_bytes_new_take (uncompressed, size);
444 return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
448 * g_resource_get_info:
449 * @resource: A #GResource.
450 * @path: A pathname inside the resource.
451 * @lookup_flags: A #GResourceLookupFlags.
452 * @size: (out) (allow-none): a location to place the length of the contents of the file,
453 * or %NULL if the length is not needed
454 * @flags: (out) (allow-none): a location to place the flags about the file,
455 * or %NULL if the length is not needed
456 * @error: return location for a #GError, or %NULL.
458 * Looks for a file at the specified @path in the resource and
459 * if found returns information about it.
461 * @lookup_flags controls the behaviour of the lookup.
463 * Returns: %TRUE if the file was found. %FALSE if there were errors.
468 g_resource_get_info (GResource *resource,
470 GResourceLookupFlags lookup_flags,
475 return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error);
479 * g_resource_enumerate_children:
480 * @resource: A #GResource.
481 * @path: A pathname inside the resource.
482 * @lookup_flags: A #GResourceLookupFlags.
483 * @error: return location for a #GError, or %NULL.
485 * Returns all the names of children at the specified @path in the resource.
486 * The return result is a %NULL terminated list of strings which should
487 * be released with g_strfreev().
489 * @lookup_flags controls the behaviour of the lookup.
491 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
496 g_resource_enumerate_children (GResource *resource,
498 GResourceLookupFlags lookup_flags,
503 char *path_with_slash;
507 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
508 _("The resource at '%s' does not exist"),
513 path_len = strlen (path);
514 if (path[path_len-1] != '/')
515 path_with_slash = g_strconcat (path, "/", NULL);
517 path_with_slash = g_strdup (path);
519 children = gvdb_table_list (resource->table, path_with_slash);
520 g_free (path_with_slash);
522 if (children == NULL)
524 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
525 _("The resource at '%s' does not exist"),
533 static GRWLock resources_lock;
534 static GList *registered_resources;
537 * g_resources_register:
538 * @resource: A #GResource.
540 * Registers the resource with the process-global set of resources.
541 * Once a resource is registered the files in it can be accessed
542 * with the global resource lookup functions like g_resources_lookup_data().
547 g_resources_register (GResource *resource)
549 g_rw_lock_writer_lock (&resources_lock);
551 registered_resources = g_list_prepend (registered_resources,
552 g_resource_ref (resource));
554 g_rw_lock_writer_unlock (&resources_lock);
558 * g_resources_unregister:
559 * @resource: A #GResource.
561 * Unregisters the resource from the process-global set of resources.
566 g_resources_unregister (GResource *resource)
568 g_rw_lock_writer_lock (&resources_lock);
570 if (g_list_find (registered_resources, resource) == NULL)
572 g_warning ("Tried to remove not registred resource");
576 registered_resources = g_list_remove (registered_resources,
578 g_resource_unref (resource);
581 g_rw_lock_writer_unlock (&resources_lock);
585 * g_resources_open_stream:
586 * @path: A pathname inside the resource.
587 * @lookup_flags: A #GResourceLookupFlags.
588 * @error: return location for a #GError, or %NULL.
590 * Looks for a file at the specified @path in the set of
591 * globally registred resources and returns a #GInputStream
592 * that lets you read the data.
594 * @lookup_flags controls the behaviour of the lookup.
596 * Returns: (transfer full): #GInputStream or %NULL on error.
597 * Free the returned object with g_object_unref().
602 g_resources_open_stream (const char *path,
603 GResourceLookupFlags lookup_flags,
606 GInputStream *res = NULL;
608 GInputStream *stream;
610 g_rw_lock_reader_lock (&resources_lock);
612 for (l = registered_resources; l != NULL; l = l->next)
614 GResource *r = l->data;
615 GError *my_error = NULL;
617 stream = g_resource_open_stream (r, path, lookup_flags, &my_error);
618 if (stream == NULL &&
619 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
621 g_clear_error (&my_error);
626 g_propagate_error (error, my_error);
633 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
634 _("The resource at '%s' does not exist"),
637 g_rw_lock_reader_unlock (&resources_lock);
643 * g_resources_lookup_data:
644 * @path: A pathname inside the resource.
645 * @lookup_flags: A #GResourceLookupFlags.
646 * @error: return location for a #GError, or %NULL.
648 * Looks for a file at the specified @path in the set of
649 * globally registred resources and returns a #GBytes that
650 * lets you directly access the data in memory.
652 * The data is always followed by a zero byte, so you
653 * can safely use the data as a C string. However, that byte
654 * is not included in the size of the GBytes.
656 * For uncompressed resource files this is a pointer directly into
657 * the resource bundle, which is typically in some readonly data section
658 * in the program binary. For compressed files we allocate memory on
659 * the heap and automatically uncompress the data.
661 * @lookup_flags controls the behaviour of the lookup.
663 * Returns: (transfer full): #GBytes or %NULL on error.
664 * Free the returned object with g_bytes_unref().
669 g_resources_lookup_data (const char *path,
670 GResourceLookupFlags lookup_flags,
677 g_rw_lock_reader_lock (&resources_lock);
679 for (l = registered_resources; l != NULL; l = l->next)
681 GResource *r = l->data;
682 GError *my_error = NULL;
684 data = g_resource_lookup_data (r, path, lookup_flags, &my_error);
686 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
688 g_clear_error (&my_error);
693 g_propagate_error (error, my_error);
700 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
701 _("The resource at '%s' does not exist"),
704 g_rw_lock_reader_unlock (&resources_lock);
710 * g_resources_enumerate_children:
711 * @path: A pathname inside the resource.
712 * @lookup_flags: A #GResourceLookupFlags.
713 * @error: return location for a #GError, or %NULL.
715 * Returns all the names of children at the specified @path in the set of
716 * globally registred resources.
717 * The return result is a %NULL terminated list of strings which should
718 * be released with g_strfreev().
720 * @lookup_flags controls the behaviour of the lookup.
722 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
727 g_resources_enumerate_children (const char *path,
728 GResourceLookupFlags lookup_flags,
731 GHashTable *hash = NULL;
736 g_rw_lock_reader_lock (&resources_lock);
738 for (l = registered_resources; l != NULL; l = l->next)
740 GResource *r = l->data;
742 children = g_resource_enumerate_children (r, path, 0, NULL);
744 if (children != NULL)
747 hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
749 for (i = 0; children[i] != NULL; i++)
750 g_hash_table_insert (hash, children[i], children[i]);
755 g_rw_lock_reader_unlock (&resources_lock);
759 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
760 _("The resource at '%s' does not exist"),
769 n_children = g_hash_table_size (hash);
770 children = g_new (char *, n_children + 1);
773 g_hash_table_iter_init (&iter, hash);
774 while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
775 children[i++] = g_strdup (key);
776 children[i++] = NULL;
778 g_hash_table_destroy (hash);
785 * g_resources_get_info:
786 * @path: A pathname inside the resource.
787 * @lookup_flags: A #GResourceLookupFlags.
788 * @size: (out) (allow-none): a location to place the length of the contents of the file,
789 * or %NULL if the length is not needed
790 * @flags: (out) (allow-none): a location to place the flags about the file,
791 * or %NULL if the length is not needed
792 * @error: return location for a #GError, or %NULL.
794 * Looks for a file at the specified @path in the set of
795 * globally registred resources and if found returns information about it.
797 * @lookup_flags controls the behaviour of the lookup.
799 * Returns: %TRUE if the file was found. %FALSE if there were errors.
804 g_resources_get_info (const char *path,
805 GResourceLookupFlags lookup_flags,
810 gboolean res = FALSE;
814 g_rw_lock_reader_lock (&resources_lock);
816 for (l = registered_resources; l != NULL; l = l->next)
818 GResource *r = l->data;
819 GError *my_error = NULL;
821 r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error);
823 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
825 g_clear_error (&my_error);
830 g_propagate_error (error, my_error);
837 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
838 _("The resource at '%s' does not exist"),
841 g_rw_lock_reader_unlock (&resources_lock);