gio-querymodules: Call setlocale in main function
[glib.git] / gio / gresource.c
blob2a10664fed1b9dcec7e5e4a4579793569731ce04
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, see <http://www.gnu.org/licenses/>.
18 * Authors: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
23 #include <string.h>
25 #include "gresource.h"
26 #include <gvdb/gvdb-reader.h>
27 #include <gi18n-lib.h>
28 #include <gio/gmemoryinputstream.h>
29 #include <gio/gzlibdecompressor.h>
30 #include <gio/gconverterinputstream.h>
32 struct _GResource
34 int ref_count;
36 GvdbTable *table;
39 static void register_lazy_static_resources (void);
41 G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref)
43 /**
44 * SECTION:gresource
45 * @short_description: Resource framework
46 * @include: gio/gio.h
48 * Applications and libraries often contain binary or textual data that is
49 * really part of the application, rather than user data. For instance
50 * #GtkBuilder .ui files, splashscreen images, GMenu markup XML, CSS files,
51 * icons, etc. These are often shipped as files in `$datadir/appname`, or
52 * manually included as literal strings in the code.
54 * The #GResource API and the [glib-compile-resources][glib-compile-resources] program
55 * provide a convenient and efficient alternative to this which has some nice properties. You
56 * maintain the files as normal files, so its easy to edit them, but during the build the files
57 * are combined into a binary bundle that is linked into the executable. This means that loading
58 * the resource files are efficient (as they are already in memory, shared with other instances) and
59 * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
60 * also makes it easier to create relocatable applications.
62 * Resource files can also be marked as compressed. Such files will be included in the resource bundle
63 * in a compressed form, but will be automatically uncompressed when the resource is used. This
64 * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
66 * Resource files can also be marked to be preprocessed, by setting the value of the
67 * `preprocess` attribute to a comma-separated list of preprocessing options.
68 * The only options currently supported are:
70 * `xml-stripblanks` which will use the xmllint command
71 * to strip ignorable whitespace from the XML file. For this to work,
72 * the `XMLLINT` environment variable must be set to the full path to
73 * the xmllint executable, or xmllint must be in the `PATH`; otherwise
74 * the preprocessing step is skipped.
76 * `to-pixdata` which will use the 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 `GDK_PIXBUF_PIXDATA` 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 [glib-compile-resources][glib-compile-resources] 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 * An example resource description:
88 * |[
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 * ]|
99 * This will create a resource bundle with the following files:
100 * |[
101 * /org/gtk/Example/data/splashscreen.png
102 * /org/gtk/Example/dialog.ui
103 * /org/gtk/Example/menumarkup.xml
104 * ]|
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 [glib-compile-resources][glib-compile-resources] 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.
112 * This will generate `get_resource()`, `register_resource()` and
113 * `unregister_resource()` functions, prefixed by the `--c-name` argument passed
114 * to [glib-compile-resources][glib-compile-resources]. `get_resource()` returns
115 * the generated #GResource object. The register and unregister functions
116 * register the resource so its files can be accessed using
117 * g_resources_lookup_data().
119 * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
120 * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
121 * to the data. You can also use URIs like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
122 * the resource data.
124 * There are two forms of the generated source, the default version uses the compiler support for constructor
125 * and destructor functions (where available) to automatically create and register the #GResource on startup
126 * or library load time. If you pass --manual-register two functions to register/unregister the resource is instead
127 * created. This requires an explicit initialization call in your application/library, but it works on all platforms,
128 * even on the minor ones where this is not available. (Constructor support is available for at least Win32, Mac OS and Linux.)
130 * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
131 * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
132 * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
133 * is for your own resources, and resource data is often used once, during parsing, and then released.
135 * Since: 2.32
139 * GStaticResource:
141 * #GStaticResource is an opaque data structure and can only be accessed
142 * using the following functions.
146 * g_resource_error_quark:
148 * Gets the #GResource Error Quark.
150 * Returns: a #GQuark
152 * Since: 2.32
154 G_DEFINE_QUARK (g-resource-error-quark, g_resource_error)
157 * g_resource_ref:
158 * @resource: A #GResource
160 * Atomically increments the reference count of @resource by one. This
161 * function is MT-safe and may be called from any thread.
163 * Returns: The passed in #GResource
165 * Since: 2.32
167 GResource *
168 g_resource_ref (GResource *resource)
170 g_atomic_int_inc (&resource->ref_count);
171 return resource;
175 * g_resource_unref:
176 * @resource: A #GResource
178 * Atomically decrements the reference count of @resource by one. If the
179 * reference count drops to 0, all memory allocated by the resource is
180 * released. This function is MT-safe and may be called from any
181 * thread.
183 * Since: 2.32
185 void
186 g_resource_unref (GResource *resource)
188 if (g_atomic_int_dec_and_test (&resource->ref_count))
190 gvdb_table_unref (resource->table);
191 g_free (resource);
195 /*< internal >
196 * g_resource_new_from_table:
197 * @table: (transfer full): a GvdbTable
199 * Returns: (transfer full): a new #GResource for @table
201 static GResource *
202 g_resource_new_from_table (GvdbTable *table)
204 GResource *resource;
206 resource = g_new (GResource, 1);
207 resource->ref_count = 1;
208 resource->table = table;
210 return resource;
214 * g_resource_new_from_data:
215 * @data: A #GBytes
216 * @error: return location for a #GError, or %NULL
218 * Creates a GResource from a reference to the binary resource bundle.
219 * This will keep a reference to @data while the resource lives, so
220 * the data should not be modified or freed.
222 * If you want to use this resource in the global resource namespace you need
223 * to register it with g_resources_register().
225 * Returns: (transfer full): a new #GResource, or %NULL on error
227 * Since: 2.32
229 GResource *
230 g_resource_new_from_data (GBytes *data,
231 GError **error)
233 GvdbTable *table;
235 table = gvdb_table_new_from_data (g_bytes_get_data (data, NULL),
236 g_bytes_get_size (data),
237 TRUE,
238 g_bytes_ref (data),
239 (GvdbRefFunc)g_bytes_ref,
240 (GDestroyNotify)g_bytes_unref,
241 error);
243 if (table == NULL)
244 return NULL;
246 return g_resource_new_from_table (table);
250 * g_resource_load:
251 * @filename: (type filename): the path of a filename to load, in the GLib filename encoding
252 * @error: return location for a #GError, or %NULL
254 * Loads a binary resource bundle and creates a #GResource representation of it, allowing
255 * you to query it for data.
257 * If you want to use this resource in the global resource namespace you need
258 * to register it with g_resources_register().
260 * Returns: (transfer full): a new #GResource, or %NULL on error
262 * Since: 2.32
264 GResource *
265 g_resource_load (const gchar *filename,
266 GError **error)
268 GvdbTable *table;
270 table = gvdb_table_new (filename, FALSE, error);
271 if (table == NULL)
272 return NULL;
274 return g_resource_new_from_table (table);
277 static
278 gboolean do_lookup (GResource *resource,
279 const gchar *path,
280 GResourceLookupFlags lookup_flags,
281 gsize *size,
282 guint32 *flags,
283 const void **data,
284 gsize *data_size,
285 GError **error)
287 char *free_path = NULL;
288 gsize path_len;
289 gboolean res = FALSE;
290 GVariant *value;
292 path_len = strlen (path);
293 if (path[path_len-1] == '/')
295 path = free_path = g_strdup (path);
296 free_path[path_len-1] = 0;
299 value = gvdb_table_get_raw_value (resource->table, path);
301 if (value == NULL)
303 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
304 _("The resource at '%s' does not exist"),
305 path);
307 else
309 guint32 _size, _flags;
310 GVariant *array;
312 g_variant_get (value, "(uu@ay)",
313 &_size,
314 &_flags,
315 &array);
317 _size = GUINT32_FROM_LE (_size);
318 _flags = GUINT32_FROM_LE (_flags);
320 if (size)
321 *size = _size;
322 if (flags)
323 *flags = _flags;
324 if (data)
325 *data = g_variant_get_data (array);
326 if (data_size)
328 /* Don't report trailing newline that non-compressed files has */
329 if (_flags & G_RESOURCE_FLAGS_COMPRESSED)
330 *data_size = g_variant_get_size (array);
331 else
332 *data_size = g_variant_get_size (array) - 1;
334 g_variant_unref (array);
335 g_variant_unref (value);
337 res = TRUE;
340 g_free (free_path);
341 return res;
345 * g_resource_open_stream:
346 * @resource: A #GResource
347 * @path: A pathname inside the resource
348 * @lookup_flags: A #GResourceLookupFlags
349 * @error: return location for a #GError, or %NULL
351 * Looks for a file at the specified @path in the resource and
352 * returns a #GInputStream that lets you read the data.
354 * @lookup_flags controls the behaviour of the lookup.
356 * Returns: (transfer full): #GInputStream or %NULL on error.
357 * Free the returned object with g_object_unref()
359 * Since: 2.32
361 GInputStream *
362 g_resource_open_stream (GResource *resource,
363 const gchar *path,
364 GResourceLookupFlags lookup_flags,
365 GError **error)
367 const void *data;
368 gsize data_size;
369 guint32 flags;
370 GInputStream *stream, *stream2;
372 if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error))
373 return NULL;
375 stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
376 g_object_set_data_full (G_OBJECT (stream), "g-resource",
377 g_resource_ref (resource),
378 (GDestroyNotify)g_resource_unref);
380 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
382 GZlibDecompressor *decompressor =
383 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
385 stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor));
386 g_object_unref (decompressor);
387 g_object_unref (stream);
388 stream = stream2;
391 return stream;
395 * g_resource_lookup_data:
396 * @resource: A #GResource
397 * @path: A pathname inside the resource
398 * @lookup_flags: A #GResourceLookupFlags
399 * @error: return location for a #GError, or %NULL
401 * Looks for a file at the specified @path in the resource and
402 * returns a #GBytes that lets you directly access the data in
403 * memory.
405 * The data is always followed by a zero byte, so you
406 * can safely use the data as a C string. However, that byte
407 * is not included in the size of the GBytes.
409 * For uncompressed resource files this is a pointer directly into
410 * the resource bundle, which is typically in some readonly data section
411 * in the program binary. For compressed files we allocate memory on
412 * the heap and automatically uncompress the data.
414 * @lookup_flags controls the behaviour of the lookup.
416 * Returns: (transfer full): #GBytes or %NULL on error.
417 * Free the returned object with g_bytes_unref()
419 * Since: 2.32
421 GBytes *
422 g_resource_lookup_data (GResource *resource,
423 const gchar *path,
424 GResourceLookupFlags lookup_flags,
425 GError **error)
427 const void *data;
428 guint32 flags;
429 gsize data_size;
430 gsize size;
432 if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
433 return NULL;
435 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
437 char *uncompressed, *d;
438 const char *s;
439 GConverterResult res;
440 gsize d_size, s_size;
441 gsize bytes_read, bytes_written;
444 GZlibDecompressor *decompressor =
445 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
447 uncompressed = g_malloc (size + 1);
449 s = data;
450 s_size = data_size;
451 d = uncompressed;
452 d_size = size;
456 res = g_converter_convert (G_CONVERTER (decompressor),
457 s, s_size,
458 d, d_size,
459 G_CONVERTER_INPUT_AT_END,
460 &bytes_read,
461 &bytes_written,
462 NULL);
463 if (res == G_CONVERTER_ERROR)
465 g_free (uncompressed);
466 g_object_unref (decompressor);
468 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL,
469 _("The resource at '%s' failed to decompress"),
470 path);
471 return NULL;
474 s += bytes_read;
475 s_size -= bytes_read;
476 d += bytes_written;
477 d_size -= bytes_written;
479 while (res != G_CONVERTER_FINISHED);
481 uncompressed[size] = 0; /* Zero terminate */
483 g_object_unref (decompressor);
485 return g_bytes_new_take (uncompressed, size);
487 else
488 return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
492 * g_resource_get_info:
493 * @resource: A #GResource
494 * @path: A pathname inside the resource
495 * @lookup_flags: A #GResourceLookupFlags
496 * @size: (out) (allow-none): a location to place the length of the contents of the file,
497 * or %NULL if the length is not needed
498 * @flags: (out) (allow-none): a location to place the flags about the file,
499 * or %NULL if the length is not needed
500 * @error: return location for a #GError, or %NULL
502 * Looks for a file at the specified @path in the resource and
503 * if found returns information about it.
505 * @lookup_flags controls the behaviour of the lookup.
507 * Returns: %TRUE if the file was found. %FALSE if there were errors
509 * Since: 2.32
511 gboolean
512 g_resource_get_info (GResource *resource,
513 const gchar *path,
514 GResourceLookupFlags lookup_flags,
515 gsize *size,
516 guint32 *flags,
517 GError **error)
519 return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error);
523 * g_resource_enumerate_children:
524 * @resource: A #GResource
525 * @path: A pathname inside the resource
526 * @lookup_flags: A #GResourceLookupFlags
527 * @error: return location for a #GError, or %NULL
529 * Returns all the names of children at the specified @path in the resource.
530 * The return result is a %NULL terminated list of strings which should
531 * be released with g_strfreev().
533 * If @path is invalid or does not exist in the #GResource,
534 * %G_RESOURCE_ERROR_NOT_FOUND will be returned.
536 * @lookup_flags controls the behaviour of the lookup.
538 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
540 * Since: 2.32
542 gchar **
543 g_resource_enumerate_children (GResource *resource,
544 const gchar *path,
545 GResourceLookupFlags lookup_flags,
546 GError **error)
548 gchar **children;
549 gsize path_len;
550 char *path_with_slash;
552 if (*path == 0)
554 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
555 _("The resource at '%s' does not exist"),
556 path);
557 return NULL;
560 path_len = strlen (path);
561 if (path[path_len-1] != '/')
562 path_with_slash = g_strconcat (path, "/", NULL);
563 else
564 path_with_slash = g_strdup (path);
566 children = gvdb_table_list (resource->table, path_with_slash);
567 g_free (path_with_slash);
569 if (children == NULL)
571 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
572 _("The resource at '%s' does not exist"),
573 path);
574 return NULL;
577 return children;
580 static GRWLock resources_lock;
581 static GList *registered_resources;
583 /* This is updated atomically, so we can append to it and check for NULL outside the
584 lock, but all other accesses are done under the write lock */
585 static GStaticResource *lazy_register_resources;
587 static void
588 g_resources_register_unlocked (GResource *resource)
590 registered_resources = g_list_prepend (registered_resources, g_resource_ref (resource));
593 static void
594 g_resources_unregister_unlocked (GResource *resource)
596 if (g_list_find (registered_resources, resource) == NULL)
598 g_warning ("Tried to remove not registered resource");
600 else
602 registered_resources = g_list_remove (registered_resources, resource);
603 g_resource_unref (resource);
608 * g_resources_register:
609 * @resource: A #GResource
611 * Registers the resource with the process-global set of resources.
612 * Once a resource is registered the files in it can be accessed
613 * with the global resource lookup functions like g_resources_lookup_data().
615 * Since: 2.32
617 void
618 g_resources_register (GResource *resource)
620 g_rw_lock_writer_lock (&resources_lock);
621 g_resources_register_unlocked (resource);
622 g_rw_lock_writer_unlock (&resources_lock);
626 * g_resources_unregister:
627 * @resource: A #GResource
629 * Unregisters the resource from the process-global set of resources.
631 * Since: 2.32
633 void
634 g_resources_unregister (GResource *resource)
636 g_rw_lock_writer_lock (&resources_lock);
637 g_resources_unregister_unlocked (resource);
638 g_rw_lock_writer_unlock (&resources_lock);
642 * g_resources_open_stream:
643 * @path: A pathname inside the resource
644 * @lookup_flags: A #GResourceLookupFlags
645 * @error: return location for a #GError, or %NULL
647 * Looks for a file at the specified @path in the set of
648 * globally registered resources and returns a #GInputStream
649 * that lets you read the data.
651 * @lookup_flags controls the behaviour of the lookup.
653 * Returns: (transfer full): #GInputStream or %NULL on error.
654 * Free the returned object with g_object_unref()
656 * Since: 2.32
658 GInputStream *
659 g_resources_open_stream (const gchar *path,
660 GResourceLookupFlags lookup_flags,
661 GError **error)
663 GInputStream *res = NULL;
664 GList *l;
665 GInputStream *stream;
667 register_lazy_static_resources ();
669 g_rw_lock_reader_lock (&resources_lock);
671 for (l = registered_resources; l != NULL; l = l->next)
673 GResource *r = l->data;
674 GError *my_error = NULL;
676 stream = g_resource_open_stream (r, path, lookup_flags, &my_error);
677 if (stream == NULL &&
678 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
680 g_clear_error (&my_error);
682 else
684 if (stream == NULL)
685 g_propagate_error (error, my_error);
686 res = stream;
687 break;
691 if (l == NULL)
692 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
693 _("The resource at '%s' does not exist"),
694 path);
696 g_rw_lock_reader_unlock (&resources_lock);
698 return res;
702 * g_resources_lookup_data:
703 * @path: A pathname inside the resource
704 * @lookup_flags: A #GResourceLookupFlags
705 * @error: return location for a #GError, or %NULL
707 * Looks for a file at the specified @path in the set of
708 * globally registered resources and returns a #GBytes that
709 * lets you directly access the data in memory.
711 * The data is always followed by a zero byte, so you
712 * can safely use the data as a C string. However, that byte
713 * is not included in the size of the GBytes.
715 * For uncompressed resource files this is a pointer directly into
716 * the resource bundle, which is typically in some readonly data section
717 * in the program binary. For compressed files we allocate memory on
718 * the heap and automatically uncompress the data.
720 * @lookup_flags controls the behaviour of the lookup.
722 * Returns: (transfer full): #GBytes or %NULL on error.
723 * Free the returned object with g_bytes_unref()
725 * Since: 2.32
727 GBytes *
728 g_resources_lookup_data (const gchar *path,
729 GResourceLookupFlags lookup_flags,
730 GError **error)
732 GBytes *res = NULL;
733 GList *l;
734 GBytes *data;
736 register_lazy_static_resources ();
738 g_rw_lock_reader_lock (&resources_lock);
740 for (l = registered_resources; l != NULL; l = l->next)
742 GResource *r = l->data;
743 GError *my_error = NULL;
745 data = g_resource_lookup_data (r, path, lookup_flags, &my_error);
746 if (data == NULL &&
747 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
749 g_clear_error (&my_error);
751 else
753 if (data == NULL)
754 g_propagate_error (error, my_error);
755 res = data;
756 break;
760 if (l == NULL)
761 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
762 _("The resource at '%s' does not exist"),
763 path);
765 g_rw_lock_reader_unlock (&resources_lock);
767 return res;
771 * g_resources_enumerate_children:
772 * @path: A pathname inside the resource
773 * @lookup_flags: A #GResourceLookupFlags
774 * @error: return location for a #GError, or %NULL
776 * Returns all the names of children at the specified @path in the set of
777 * globally registered resources.
778 * The return result is a %NULL terminated list of strings which should
779 * be released with g_strfreev().
781 * @lookup_flags controls the behaviour of the lookup.
783 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
785 * Since: 2.32
787 gchar **
788 g_resources_enumerate_children (const gchar *path,
789 GResourceLookupFlags lookup_flags,
790 GError **error)
792 GHashTable *hash = NULL;
793 GList *l;
794 char **children;
795 int i;
797 register_lazy_static_resources ();
799 g_rw_lock_reader_lock (&resources_lock);
801 for (l = registered_resources; l != NULL; l = l->next)
803 GResource *r = l->data;
805 children = g_resource_enumerate_children (r, path, 0, NULL);
807 if (children != NULL)
809 if (hash == NULL)
810 hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
812 for (i = 0; children[i] != NULL; i++)
813 g_hash_table_insert (hash, children[i], children[i]);
814 g_free (children);
818 g_rw_lock_reader_unlock (&resources_lock);
820 if (hash == NULL)
822 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
823 _("The resource at '%s' does not exist"),
824 path);
825 return NULL;
827 else
829 GHashTableIter iter;
830 const char *key;
831 guint n_children;
832 n_children = g_hash_table_size (hash);
833 children = g_new (char *, n_children + 1);
834 i = 0;
836 g_hash_table_iter_init (&iter, hash);
837 while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
838 children[i++] = g_strdup (key);
839 children[i++] = NULL;
841 g_hash_table_destroy (hash);
843 return children;
848 * g_resources_get_info:
849 * @path: A pathname inside the resource
850 * @lookup_flags: A #GResourceLookupFlags
851 * @size: (out) (allow-none): a location to place the length of the contents of the file,
852 * or %NULL if the length is not needed
853 * @flags: (out) (allow-none): a location to place the flags about the file,
854 * or %NULL if the length is not needed
855 * @error: return location for a #GError, or %NULL
857 * Looks for a file at the specified @path in the set of
858 * globally registered resources and if found returns information about it.
860 * @lookup_flags controls the behaviour of the lookup.
862 * Returns: %TRUE if the file was found. %FALSE if there were errors
864 * Since: 2.32
866 gboolean
867 g_resources_get_info (const gchar *path,
868 GResourceLookupFlags lookup_flags,
869 gsize *size,
870 guint32 *flags,
871 GError **error)
873 gboolean res = FALSE;
874 GList *l;
875 gboolean r_res;
877 register_lazy_static_resources ();
879 g_rw_lock_reader_lock (&resources_lock);
881 for (l = registered_resources; l != NULL; l = l->next)
883 GResource *r = l->data;
884 GError *my_error = NULL;
886 r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error);
887 if (!r_res &&
888 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
890 g_clear_error (&my_error);
892 else
894 if (!r_res)
895 g_propagate_error (error, my_error);
896 res = r_res;
897 break;
901 if (l == NULL)
902 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
903 _("The resource at '%s' does not exist"),
904 path);
906 g_rw_lock_reader_unlock (&resources_lock);
908 return res;
911 /* This code is to handle registration of resources very early, from a constructor.
912 * At that point we'd like to do minimal work, to avoid ordering issues. For instance,
913 * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable
914 * before the first call to g_malloc.
916 * So, what we do at construction time is that we just register a static structure on
917 * a list of resources that need to be initialized, and then later, when doing any lookups
918 * in the global list of registered resources, or when getting a reference to the
919 * lazily initialized resource we lazily create and register all the GResources on
920 * the lazy list.
922 * To avoid having to use locks in the constructor, and having to grab the writer lock
923 * when checking the lazy registering list we update lazy_register_resources in
924 * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all
925 * operations except:
926 * * check if there are any resources to lazily initialize
927 * * Add a static resource to the lazy init list
928 * Do use the full writer lock for protection.
931 static void
932 register_lazy_static_resources_unlocked (void)
934 GStaticResource *list;
937 list = lazy_register_resources;
938 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, list, NULL));
940 while (list != NULL)
942 GBytes *bytes = g_bytes_new_static (list->data, list->data_len);
943 GResource *resource = g_resource_new_from_data (bytes, NULL);
944 if (resource)
946 g_resources_register_unlocked (resource);
947 g_atomic_pointer_set (&list->resource, resource);
949 g_bytes_unref (bytes);
951 list = list->next;
955 static void
956 register_lazy_static_resources (void)
958 if (g_atomic_pointer_get (&lazy_register_resources) == NULL)
959 return;
961 g_rw_lock_writer_lock (&resources_lock);
962 register_lazy_static_resources_unlocked ();
963 g_rw_lock_writer_unlock (&resources_lock);
967 * g_static_resource_init:
968 * @static_resource: pointer to a static #GStaticResource
970 * Initializes a GResource from static data using a
971 * GStaticResource.
973 * This is normally used by code generated by
974 * [glib-compile-resources][glib-compile-resources]
975 * and is not typically used by other code.
977 * Since: 2.32
979 void
980 g_static_resource_init (GStaticResource *static_resource)
982 gpointer next;
986 next = lazy_register_resources;
987 static_resource->next = next;
989 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource));
993 * g_static_resource_fini:
994 * @static_resource: pointer to a static #GStaticResource
996 * Finalized a GResource initialized by g_static_resource_init().
998 * This is normally used by code generated by
999 * [glib-compile-resources][glib-compile-resources]
1000 * and is not typically used by other code.
1002 * Since: 2.32
1004 void
1005 g_static_resource_fini (GStaticResource *static_resource)
1007 GResource *resource;
1009 g_rw_lock_writer_lock (&resources_lock);
1011 register_lazy_static_resources_unlocked ();
1013 resource = g_atomic_pointer_get (&static_resource->resource);
1014 if (resource)
1016 g_atomic_pointer_set (&static_resource->resource, NULL);
1017 g_resources_unregister_unlocked (resource);
1018 g_resource_unref (resource);
1021 g_rw_lock_writer_unlock (&resources_lock);
1025 * g_static_resource_get_resource:
1026 * @static_resource: pointer to a static #GStaticResource
1028 * Gets the GResource that was registered by a call to g_static_resource_init().
1030 * This is normally used by code generated by
1031 * [glib-compile-resources][glib-compile-resources]
1032 * and is not typically used by other code.
1034 * Returns: (transfer none): a #GResource
1036 * Since: 2.32
1038 GResource *
1039 g_static_resource_get_resource (GStaticResource *static_resource)
1041 register_lazy_static_resources ();
1043 return g_atomic_pointer_get (&static_resource->resource);