resources: Init refcount to 1
[glib.git] / gio / gresource.c
blob338a2d264af8dbdd69dadb4601aba7a0bd4a6e9b
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>
23 #include "config.h"
25 #include <string.h>
27 #include "gresource.h"
28 #include <gvdb/gvdb-reader.h>
29 #include <gi18n.h>
30 #include <gio/gmemoryinputstream.h>
31 #include <gio/gzlibdecompressor.h>
32 #include <gio/gconverterinputstream.h>
34 struct _GResource
36 int ref_count;
38 GvdbTable *table;
41 static void register_lazy_static_resources ();
43 G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref)
45 /**
46 * SECTION:gresource
47 * @short_description: Resource framework
48 * @include: gio/gio.h
50 * Applications and libraries often contain binary or textual data that is really part of the
51 * application, rather than user data. For instance #GtkBuilder .ui files, splashscreen images,
52 * GMenu markup xml, CSS files, icons, etc. These are often shipped as files in <filename>$datadir/appname</filename>, or
53 * manually included as literal strings in the code.
55 * The #GResource API and the <link linkend="glib-compile-resources">glib-compile-resources</link> program
56 * provide a convenient and efficient alternative to this which has some nice properties. You
57 * maintain the files as normal files, so its easy to edit them, but during the build the files
58 * are combined into a binary bundle that is linked into the executable. This means that loading
59 * the resource files are efficient (as they are already in memory, shared with other instances) and
60 * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
61 * also makes it easier to create relocatable applications.
63 * Resource files can also be marked as compresses. Such files will be included in the resource bundle
64 * in a compressed form, but will be automatically uncompressed when the resource is used. This
65 * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
67 * Resource files can also be marked to be preprocessed, by setting the value of the
68 * <literal>preprocess</literal> attribute to a comma-separated list of preprocessing options.
69 * The only options currently supported are:
71 * <literal>xml-stripblanks</literal> which will use <command>xmllint</command> to strip
72 * ignorable whitespace from the xml file. For this to work, the <envar>XMLLINT</envar>
73 * environment variable must be set to the full path to the xmllint executable, or xmllint
74 * must be in the PATH; otherwise the preprocessing step is skipped.
76 * <literal>to-pixdata</literal> which will use <command>gdk-pixbuf-pixdata</command> to convert
77 * images to the GdkPixdata format, which allows you to create pixbufs directly using the data inside
78 * the resource file, rather than an (uncompressed) copy if it. For this, the gdk-pixbuf-pixdata
79 * program must be in the PATH, or the <envar>GDK_PIXBUF_PIXDATA</envar> environment variable must be
80 * set to the full path to the gdk-pixbuf-pixdata executable; otherwise the resource compiler will
81 * abort.
83 * Resource bundles are created by the <link linkend="glib-compile-resources">glib-compile-resources</link> program
84 * which takes an xml file that describes the bundle, and a set of files that the xml references. These
85 * are combined into a binary resource bundle.
87 * <example id="resource-example"><title>Example resource description</title>
88 * <programlisting><![CDATA[
89 * <?xml version="1.0" encoding="UTF-8"?>
90 * <gresources>
91 * <gresource prefix="/org/gtk/Example">
92 * <file>data/splashscreen.png</file>
93 * <file compressed="true">dialog.ui</file>
94 * <file preprocess="xml-stripblanks">menumarkup.xml</file>
95 * </gresource>
96 * </gresources>
97 * ]]></programlisting></example>
99 * This will create a resource bundle with the following files:
100 * <programlisting><![CDATA[
101 * /org/gtk/Example/data/splashscreen.png
102 * /org/gtk/Example/dialog.ui
103 * /org/gtk/Example/menumarkup.xml
104 * ]]></programlisting>
106 * Note that all resources in the process share the same namespace, so use java-style
107 * path prefixes (like in the above example) to avoid conflicts.
109 * You can then use <link linkend="glib-compile-resources">glib-compile-resources</link> to compile the xml to a
110 * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
111 * --generate-header arguments to create a source file and header to link directly into your application.
113 * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
114 * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
115 * to the data. You can also use uris like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
116 * the resource data.
118 * There are two forms of the generated source, the default version uses the compiler support for constructor
119 * and destructor functions (where availible) to automatically create and register the #GResource on startup
120 * or library load time. If you pass --manual-register two functions to register/unregister the resource is instead
121 * created. This requires an explicit initialization call in your application/library, but it works on all platforms,
122 * even on the minor ones where this is not availible. (Constructor support is availible for at least Win32, MacOS and Linux.)
124 * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
125 * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
126 * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
127 * is for your own resources, and resource data is often used once, during parsing, and then released.
129 * Since: 2.32
133 * g_resource_error_quark:
135 * Gets the #GResource Error Quark.
137 * Return value: a #GQuark.
139 * Since: 2.32
141 GQuark
142 g_resource_error_quark (void)
144 return g_quark_from_static_string ("g-resource-error-quark");
148 * g_resource_ref:
149 * @resource: A #GResource.
151 * Atomically increments the reference count of @array by one. This
152 * function is MT-safe and may be called from any thread.
154 * Returns: The passed in #GResource.
156 * Since: 2.32
158 GResource *
159 g_resource_ref (GResource *resource)
161 g_atomic_int_inc (&resource->ref_count);
162 return resource;
166 * g_resource_unref:
167 * @resource: A #GResource.
169 * Atomically decrements the reference count of @resource by one. If the
170 * reference count drops to 0, all memory allocated by the array is
171 * released. This function is MT-safe and may be called from any
172 * thread.
174 * Since: 2.32
176 void
177 g_resource_unref (GResource *resource)
179 if (g_atomic_int_dec_and_test (&resource->ref_count))
181 gvdb_table_unref (resource->table);
182 g_free (resource);
187 * g_resource_new_from_data:
188 * @data: A #GBytes.
189 * @error: return location for a #GError, or %NULL.
191 * Creates a GResource from a reference to the binary resource bundle.
192 * This will keep a reference to @data while the resource lives, so
193 * the data should not be modified or freed.
195 * If you want to use this resource in the global resource namespace you need
196 * to register it with g_resources_register().
198 * Return value: (transfer full): a new #GResource, or %NULL on error.
200 * Since: 2.32
202 GResource *
203 g_resource_new_from_data (GBytes *data,
204 GError **error)
206 GResource *resource;
207 GvdbTable *table;
209 table = gvdb_table_new_from_data (g_bytes_get_data (data, NULL),
210 g_bytes_get_size (data),
211 TRUE,
212 g_bytes_ref (data),
213 (GvdbRefFunc)g_bytes_ref,
214 (GDestroyNotify)g_bytes_unref,
215 error);
217 if (table == NULL)
218 return NULL;
220 resource = g_new0 (GResource, 1);
221 resource->ref_count = 1;
222 resource->table = table;
224 return resource;
228 * g_resource_load:
229 * @filename: (type filename): the path of a filename to load, in the GLib filename encoding.
230 * @error: return location for a #GError, or %NULL.
232 * Loads a binary resource bundle and creates a #GResource representation of it, allowing
233 * you to query it for data.
235 * If you want to use this resource in the global resource namespace you need
236 * to register it with g_resources_register().
238 * Return value: (transfer full): a new #GResource, or %NULL on error.
240 * Since: 2.32
242 GResource *
243 g_resource_load (const gchar *filename,
244 GError **error)
246 GResource *resource;
247 GvdbTable *table;
249 table = gvdb_table_new (filename, FALSE, error);
250 if (table == NULL)
251 return NULL;
253 resource = g_new0 (GResource, 1);
254 resource->ref_count = 1;
255 resource->table = table;
257 return resource;
260 static gboolean do_lookup (GResource *resource,
261 const char *path,
262 GResourceLookupFlags lookup_flags,
263 gsize *size,
264 guint32 *flags,
265 const void **data,
266 gsize *data_size,
267 GError **error)
269 char *free_path = NULL;
270 gsize path_len;
271 gboolean res = FALSE;
272 GVariant *value;
274 path_len = strlen (path);
275 if (path[path_len-1] == '/')
277 path = free_path = g_strdup (path);
278 free_path[path_len-1] = 0;
281 value = gvdb_table_get_value (resource->table, path);
283 if (value == NULL)
285 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
286 _("The resource at '%s' does not exist"),
287 path);
289 else
291 guint32 _size, _flags;
292 GVariant *array;
294 g_variant_get (value, "(uu@ay)",
295 &_size,
296 &_flags,
297 &array);
299 if (size)
300 *size = _size;
301 if (flags)
302 *flags = _flags;
303 if (data)
304 *data = g_variant_get_data (array);
305 if (data_size)
307 /* Don't report trailing newline that non-compressed files has */
308 if (_flags & G_RESOURCE_FLAGS_COMPRESSED)
309 *data_size = g_variant_get_size (array);
310 else
311 *data_size = g_variant_get_size (array) - 1;
313 g_variant_unref (array);
314 g_variant_unref (value);
316 res = TRUE;
319 g_free (free_path);
320 return res;
324 * g_resource_open_stream:
325 * @resource: A #GResource.
326 * @path: A pathname inside the resource.
327 * @lookup_flags: A #GResourceLookupFlags.
328 * @error: return location for a #GError, or %NULL.
330 * Looks for a file at the specified @path in the resource and
331 * returns a #GInputStream that lets you read the data.
333 * @lookup_flags controls the behaviour of the lookup.
335 * Returns: (transfer full): #GInputStream or %NULL on error.
336 * Free the returned object with g_object_unref().
338 * Since: 2.32
340 GInputStream *
341 g_resource_open_stream (GResource *resource,
342 const char *path,
343 GResourceLookupFlags lookup_flags,
344 GError **error)
346 const void *data;
347 gsize data_size;
348 guint32 flags;
349 GInputStream *stream, *stream2;
351 if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error))
352 return NULL;
354 stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
355 g_object_set_data_full (G_OBJECT (stream), "g-resource",
356 g_resource_ref (resource),
357 (GDestroyNotify)g_resource_unref);
359 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
361 GZlibDecompressor *decompressor =
362 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
364 stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor));
365 g_object_unref (decompressor);
366 g_object_unref (stream);
367 stream = stream2;
370 return stream;
374 * g_resource_lookup_data:
375 * @resource: A #GResource.
376 * @path: A pathname inside the resource.
377 * @lookup_flags: A #GResourceLookupFlags.
378 * @error: return location for a #GError, or %NULL.
380 * Looks for a file at the specified @path in the resource and
381 * returns a #GBytes that lets you directly access the data in
382 * memory.
384 * The data is always followed by a zero byte, so you
385 * can safely use the data as a C string. However, that byte
386 * is not included in the size of the GBytes.
388 * For uncompressed resource files this is a pointer directly into
389 * the resource bundle, which is typically in some readonly data section
390 * in the program binary. For compressed files we allocate memory on
391 * the heap and automatically uncompress the data.
393 * @lookup_flags controls the behaviour of the lookup.
395 * Returns: (transfer full): #GBytes or %NULL on error.
396 * Free the returned object with g_bytes_unref().
398 * Since: 2.32
400 GBytes *
401 g_resource_lookup_data (GResource *resource,
402 const char *path,
403 GResourceLookupFlags lookup_flags,
404 GError **error)
406 const void *data;
407 guint32 flags;
408 gsize data_size;
409 gsize size;
411 if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
412 return NULL;
414 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
416 char *uncompressed, *d;
417 const char *s;
418 GConverterResult res;
419 gsize d_size, s_size;
420 gsize bytes_read, bytes_written;
423 GZlibDecompressor *decompressor =
424 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
426 uncompressed = g_malloc (size + 1);
428 s = data;
429 s_size = data_size;
430 d = uncompressed;
431 d_size = size;
435 res = g_converter_convert (G_CONVERTER (decompressor),
436 s, s_size,
437 d, d_size,
438 G_CONVERTER_INPUT_AT_END,
439 &bytes_read,
440 &bytes_written,
441 NULL);
442 if (res == G_CONVERTER_ERROR)
444 g_free (uncompressed);
445 g_object_unref (decompressor);
447 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL,
448 _("The resource at '%s' failed to decompress"),
449 path);
450 return NULL;
453 s += bytes_read;
454 s_size -= bytes_read;
455 d += bytes_written;
456 d_size -= bytes_written;
458 while (res != G_CONVERTER_FINISHED);
460 uncompressed[size] = 0; /* Zero terminate */
462 g_object_unref (decompressor);
464 return g_bytes_new_take (uncompressed, size);
466 else
467 return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
471 * g_resource_get_info:
472 * @resource: A #GResource.
473 * @path: A pathname inside the resource.
474 * @lookup_flags: A #GResourceLookupFlags.
475 * @size: (out) (allow-none): a location to place the length of the contents of the file,
476 * or %NULL if the length is not needed
477 * @flags: (out) (allow-none): a location to place the flags about the file,
478 * or %NULL if the length is not needed
479 * @error: return location for a #GError, or %NULL.
481 * Looks for a file at the specified @path in the resource and
482 * if found returns information about it.
484 * @lookup_flags controls the behaviour of the lookup.
486 * Returns: %TRUE if the file was found. %FALSE if there were errors.
488 * Since: 2.32
490 gboolean
491 g_resource_get_info (GResource *resource,
492 const char *path,
493 GResourceLookupFlags lookup_flags,
494 gsize *size,
495 guint32 *flags,
496 GError **error)
498 return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error);
502 * g_resource_enumerate_children:
503 * @resource: A #GResource.
504 * @path: A pathname inside the resource.
505 * @lookup_flags: A #GResourceLookupFlags.
506 * @error: return location for a #GError, or %NULL.
508 * Returns all the names of children at the specified @path in the resource.
509 * The return result is a %NULL terminated list of strings which should
510 * be released with g_strfreev().
512 * @lookup_flags controls the behaviour of the lookup.
514 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
516 * Since: 2.32
518 char **
519 g_resource_enumerate_children (GResource *resource,
520 const char *path,
521 GResourceLookupFlags lookup_flags,
522 GError **error)
524 gchar **children;
525 gsize path_len;
526 char *path_with_slash;
528 if (*path == 0)
530 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
531 _("The resource at '%s' does not exist"),
532 path);
533 return NULL;
536 path_len = strlen (path);
537 if (path[path_len-1] != '/')
538 path_with_slash = g_strconcat (path, "/", NULL);
539 else
540 path_with_slash = g_strdup (path);
542 children = gvdb_table_list (resource->table, path_with_slash);
543 g_free (path_with_slash);
545 if (children == NULL)
547 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
548 _("The resource at '%s' does not exist"),
549 path);
550 return NULL;
553 return children;
556 static GRWLock resources_lock;
557 static GList *registered_resources;
559 /* This is updated atomically, so we can append to it and check for NULL outside the
560 lock, but all other accesses are done under the write lock */
561 static GStaticResource *lazy_register_resources;
563 static void
564 g_resources_register_unlocked (GResource *resource)
566 registered_resources = g_list_prepend (registered_resources,
567 g_resource_ref (resource));
570 static void
571 g_resources_unregister_unlocked (GResource *resource)
573 if (g_list_find (registered_resources, resource) == NULL)
575 g_warning ("Tried to remove not registred resource");
577 else
579 registered_resources = g_list_remove (registered_resources,
580 resource);
581 g_resource_unref (resource);
586 * g_resources_register:
587 * @resource: A #GResource.
589 * Registers the resource with the process-global set of resources.
590 * Once a resource is registered the files in it can be accessed
591 * with the global resource lookup functions like g_resources_lookup_data().
593 * Since: 2.32
595 void
596 g_resources_register (GResource *resource)
598 g_rw_lock_writer_lock (&resources_lock);
599 g_resources_register_unlocked (resource);
600 g_rw_lock_writer_unlock (&resources_lock);
604 * g_resources_unregister:
605 * @resource: A #GResource.
607 * Unregisters the resource from the process-global set of resources.
609 * Since: 2.32
611 void
612 g_resources_unregister (GResource *resource)
614 g_rw_lock_writer_lock (&resources_lock);
615 g_resources_unregister_unlocked (resource);
616 g_rw_lock_writer_unlock (&resources_lock);
620 * g_resources_open_stream:
621 * @path: A pathname inside the resource.
622 * @lookup_flags: A #GResourceLookupFlags.
623 * @error: return location for a #GError, or %NULL.
625 * Looks for a file at the specified @path in the set of
626 * globally registred resources and returns a #GInputStream
627 * that lets you read the data.
629 * @lookup_flags controls the behaviour of the lookup.
631 * Returns: (transfer full): #GInputStream or %NULL on error.
632 * Free the returned object with g_object_unref().
634 * Since: 2.32
636 GInputStream *
637 g_resources_open_stream (const char *path,
638 GResourceLookupFlags lookup_flags,
639 GError **error)
641 GInputStream *res = NULL;
642 GList *l;
643 GInputStream *stream;
645 register_lazy_static_resources ();
647 g_rw_lock_reader_lock (&resources_lock);
649 for (l = registered_resources; l != NULL; l = l->next)
651 GResource *r = l->data;
652 GError *my_error = NULL;
654 stream = g_resource_open_stream (r, path, lookup_flags, &my_error);
655 if (stream == NULL &&
656 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
658 g_clear_error (&my_error);
660 else
662 if (stream == NULL)
663 g_propagate_error (error, my_error);
664 res = stream;
665 break;
669 if (l == NULL)
670 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
671 _("The resource at '%s' does not exist"),
672 path);
674 g_rw_lock_reader_unlock (&resources_lock);
676 return res;
680 * g_resources_lookup_data:
681 * @path: A pathname inside the resource.
682 * @lookup_flags: A #GResourceLookupFlags.
683 * @error: return location for a #GError, or %NULL.
685 * Looks for a file at the specified @path in the set of
686 * globally registred resources and returns a #GBytes that
687 * lets you directly access the data in memory.
689 * The data is always followed by a zero byte, so you
690 * can safely use the data as a C string. However, that byte
691 * is not included in the size of the GBytes.
693 * For uncompressed resource files this is a pointer directly into
694 * the resource bundle, which is typically in some readonly data section
695 * in the program binary. For compressed files we allocate memory on
696 * the heap and automatically uncompress the data.
698 * @lookup_flags controls the behaviour of the lookup.
700 * Returns: (transfer full): #GBytes or %NULL on error.
701 * Free the returned object with g_bytes_unref().
703 * Since: 2.32
705 GBytes *
706 g_resources_lookup_data (const char *path,
707 GResourceLookupFlags lookup_flags,
708 GError **error)
710 GBytes *res = NULL;
711 GList *l;
712 GBytes *data;
714 register_lazy_static_resources ();
716 g_rw_lock_reader_lock (&resources_lock);
718 for (l = registered_resources; l != NULL; l = l->next)
720 GResource *r = l->data;
721 GError *my_error = NULL;
723 data = g_resource_lookup_data (r, path, lookup_flags, &my_error);
724 if (data == NULL &&
725 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
727 g_clear_error (&my_error);
729 else
731 if (data == NULL)
732 g_propagate_error (error, my_error);
733 res = data;
734 break;
738 if (l == NULL)
739 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
740 _("The resource at '%s' does not exist"),
741 path);
743 g_rw_lock_reader_unlock (&resources_lock);
745 return res;
749 * g_resources_enumerate_children:
750 * @path: A pathname inside the resource.
751 * @lookup_flags: A #GResourceLookupFlags.
752 * @error: return location for a #GError, or %NULL.
754 * Returns all the names of children at the specified @path in the set of
755 * globally registred resources.
756 * The return result is a %NULL terminated list of strings which should
757 * be released with g_strfreev().
759 * @lookup_flags controls the behaviour of the lookup.
761 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
763 * Since: 2.32
765 char **
766 g_resources_enumerate_children (const char *path,
767 GResourceLookupFlags lookup_flags,
768 GError **error)
770 GHashTable *hash = NULL;
771 GList *l;
772 char **children;
773 int i;
775 register_lazy_static_resources ();
777 g_rw_lock_reader_lock (&resources_lock);
779 for (l = registered_resources; l != NULL; l = l->next)
781 GResource *r = l->data;
783 children = g_resource_enumerate_children (r, path, 0, NULL);
785 if (children != NULL)
787 if (hash == NULL)
788 hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
790 for (i = 0; children[i] != NULL; i++)
791 g_hash_table_insert (hash, children[i], children[i]);
792 g_free (children);
796 g_rw_lock_reader_unlock (&resources_lock);
798 if (hash == NULL)
800 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
801 _("The resource at '%s' does not exist"),
802 path);
803 return NULL;
805 else
807 GHashTableIter iter;
808 const char *key;
809 guint n_children;
810 n_children = g_hash_table_size (hash);
811 children = g_new (char *, n_children + 1);
812 i = 0;
814 g_hash_table_iter_init (&iter, hash);
815 while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
816 children[i++] = g_strdup (key);
817 children[i++] = NULL;
819 g_hash_table_destroy (hash);
821 return children;
826 * g_resources_get_info:
827 * @path: A pathname inside the resource.
828 * @lookup_flags: A #GResourceLookupFlags.
829 * @size: (out) (allow-none): a location to place the length of the contents of the file,
830 * or %NULL if the length is not needed
831 * @flags: (out) (allow-none): a location to place the flags about the file,
832 * or %NULL if the length is not needed
833 * @error: return location for a #GError, or %NULL.
835 * Looks for a file at the specified @path in the set of
836 * globally registred resources and if found returns information about it.
838 * @lookup_flags controls the behaviour of the lookup.
840 * Returns: %TRUE if the file was found. %FALSE if there were errors.
842 * Since: 2.32
844 gboolean
845 g_resources_get_info (const char *path,
846 GResourceLookupFlags lookup_flags,
847 gsize *size,
848 guint32 *flags,
849 GError **error)
851 gboolean res = FALSE;
852 GList *l;
853 gboolean r_res;
855 register_lazy_static_resources ();
857 g_rw_lock_reader_lock (&resources_lock);
859 for (l = registered_resources; l != NULL; l = l->next)
861 GResource *r = l->data;
862 GError *my_error = NULL;
864 r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error);
865 if (!r_res &&
866 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
868 g_clear_error (&my_error);
870 else
872 if (!r_res)
873 g_propagate_error (error, my_error);
874 res = r_res;
875 break;
879 if (l == NULL)
880 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
881 _("The resource at '%s' does not exist"),
882 path);
884 g_rw_lock_reader_unlock (&resources_lock);
886 return res;
889 /* This code is to handle registration of resources very early, from a constructor.
890 * At that point we'd like to do minimal work, to avoid ordering issues. For instance,
891 * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable
892 * before the first call to g_malloc.
894 * So, what we do at construction time is that we just register a static structure on
895 * a list of resources that need to be initialized, and then later, when doing any lookups
896 * in the global list of registered resources, or when getting a reference to the
897 * lazily initialized resource we lazily create and register all the GResources on
898 * the lazy list.
900 * To avoid having to use locks in the constructor, and having to grab the writer lock
901 * when checking the lazy registering list we update lazy_register_resources in
902 * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all
903 * operations except:
904 * * check if there are any resources to lazily initialize
905 * * Add a static resource to the lazy init list
906 * Do use the full writer lock for protection.
909 static void
910 register_lazy_static_resources_unlocked ()
912 GStaticResource *list;
915 list = lazy_register_resources;
916 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, list, NULL));
918 while (list != NULL)
920 GBytes *bytes = g_bytes_new_static (list->data, list->data_len);
921 GResource *resource = g_resource_new_from_data (bytes, NULL);
922 if (resource)
924 g_resources_register_unlocked (resource);
925 g_atomic_pointer_set (&list->resource, resource);
927 g_bytes_unref (bytes);
929 list = list->next;
933 static void
934 register_lazy_static_resources ()
936 if (g_atomic_pointer_get (&lazy_register_resources) == NULL)
937 return;
939 g_rw_lock_writer_lock (&resources_lock);
940 register_lazy_static_resources_unlocked ();
941 g_rw_lock_writer_unlock (&resources_lock);
945 * g_static_resource_init:
946 * @static_resource: pointer to a static #GStaticResource.
948 * Initializes a GResource from static data using a
949 * GStaticResource.
951 * This is normally used by code generated by
952 * <link linkend="glib-compile-resources">glib-compile-resources</link> and is
953 * not typically used by other code.
955 * Since: 2.32
957 void
958 g_static_resource_init (GStaticResource *static_resource)
960 gpointer next;
964 next = lazy_register_resources;
965 static_resource->next = next;
967 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource));
971 * g_static_resource_fini:
972 * @static_resource: pointer to a static #GStaticResource.
974 * Finalized a GResource initialized by g_static_resource_init ().
976 * This is normally used by code generated by
977 * <link linkend="glib-compile-resources">glib-compile-resources</link> and is
978 * not typically used by other code.
980 * Since: 2.32
982 void
983 g_static_resource_fini (GStaticResource *static_resource)
985 GResource *resource;
987 g_rw_lock_writer_lock (&resources_lock);
989 register_lazy_static_resources_unlocked ();
991 resource = g_atomic_pointer_get (&static_resource->resource);
992 if (resource)
994 g_atomic_pointer_set (&static_resource->resource, NULL);
995 g_resources_unregister_unlocked (resource);
996 g_resource_unref (resource);
999 g_rw_lock_writer_unlock (&resources_lock);
1003 * g_static_resource_get_resource:
1004 * @static_resource: pointer to a static #GStaticResource.
1006 * Gets the GResource that was registred by a call to g_static_resource_init ().
1008 * This is normally used by code generated by
1009 * <link linkend="glib-compile-resources">glib-compile-resources</link> and is
1010 * not typically used by other code.
1012 * Return value: (transfer none): a #GResource.
1014 * Since: 2.32
1016 GResource *
1017 g_static_resource_get_resource (GStaticResource *static_resource)
1019 register_lazy_static_resources ();
1021 return g_atomic_pointer_get (&static_resource->resource);