Change LGPL-2.1+ to LGPL-2.1-or-later
[platform/upstream/glib.git] / gio / gresource.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2011 Red Hat, Inc
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  * Authors: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "gresource.h"
28 #include <gvdb/gvdb-reader.h>
29 #include <gi18n-lib.h>
30 #include <gstdio.h>
31 #include <gio/gfile.h>
32 #include <gio/gioerror.h>
33 #include <gio/gmemoryinputstream.h>
34 #include <gio/gzlibdecompressor.h>
35 #include <gio/gconverterinputstream.h>
36
37 #include "glib-private.h"
38
39 struct _GResource
40 {
41   int ref_count;
42
43   GvdbTable *table;
44 };
45
46 static void register_lazy_static_resources (void);
47
48 G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref)
49
50 /**
51  * SECTION:gresource
52  * @short_description: Resource framework
53  * @include: gio/gio.h
54  *
55  * Applications and libraries often contain binary or textual data that is
56  * really part of the application, rather than user data. For instance
57  * #GtkBuilder .ui files, splashscreen images, GMenu markup XML, CSS files,
58  * icons, etc. These are often shipped as files in `$datadir/appname`, or
59  * manually included as literal strings in the code.
60  *
61  * The #GResource API and the [glib-compile-resources][glib-compile-resources] program
62  * provide a convenient and efficient alternative to this which has some nice properties. You
63  * maintain the files as normal files, so its easy to edit them, but during the build the files
64  * are combined into a binary bundle that is linked into the executable. This means that loading
65  * the resource files are efficient (as they are already in memory, shared with other instances) and
66  * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
67  * also makes it easier to create relocatable applications.
68  *
69  * Resource files can also be marked as compressed. Such files will be included in the resource bundle
70  * in a compressed form, but will be automatically uncompressed when the resource is used. This
71  * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
72  *
73  * Resource files can also be marked to be preprocessed, by setting the value of the
74  * `preprocess` attribute to a comma-separated list of preprocessing options.
75  * The only options currently supported are:
76  *
77  * `xml-stripblanks` which will use the xmllint command
78  * to strip ignorable whitespace from the XML file. For this to work,
79  * the `XMLLINT` environment variable must be set to the full path to
80  * the xmllint executable, or xmllint must be in the `PATH`; otherwise
81  * the preprocessing step is skipped.
82  *
83  * `to-pixdata` (deprecated since gdk-pixbuf 2.32) which will use the
84  * `gdk-pixbuf-pixdata` command to convert images to the #GdkPixdata format,
85  * which allows you to create pixbufs directly using the data inside the
86  * resource file, rather than an (uncompressed) copy of it. For this, the
87  * `gdk-pixbuf-pixdata` program must be in the `PATH`, or the
88  * `GDK_PIXBUF_PIXDATA` environment variable must be set to the full path to the
89  * `gdk-pixbuf-pixdata` executable; otherwise the resource compiler will abort.
90  * `to-pixdata` has been deprecated since gdk-pixbuf 2.32, as #GResource
91  * supports embedding modern image formats just as well. Instead of using it,
92  * embed a PNG or SVG file in your #GResource.
93  *
94  * `json-stripblanks` which will use the `json-glib-format` command to strip
95  * ignorable whitespace from the JSON file. For this to work, the
96  * `JSON_GLIB_FORMAT` environment variable must be set to the full path to the
97  * `json-glib-format` executable, or it must be in the `PATH`;
98  * otherwise the preprocessing step is skipped. In addition, at least version
99  * 1.6 of `json-glib-format` is required.
100  *
101  * Resource files will be exported in the GResource namespace using the
102  * combination of the given `prefix` and the filename from the `file` element.
103  * The `alias` attribute can be used to alter the filename to expose them at a
104  * different location in the resource namespace. Typically, this is used to
105  * include files from a different source directory without exposing the source
106  * directory in the resource namespace, as in the example below.
107  *
108  * Resource bundles are created by the [glib-compile-resources][glib-compile-resources] program
109  * which takes an XML file that describes the bundle, and a set of files that the XML references. These
110  * are combined into a binary resource bundle.
111  *
112  * An example resource description:
113  * |[
114  * <?xml version="1.0" encoding="UTF-8"?>
115  * <gresources>
116  *   <gresource prefix="/org/gtk/Example">
117  *     <file>data/splashscreen.png</file>
118  *     <file compressed="true">dialog.ui</file>
119  *     <file preprocess="xml-stripblanks">menumarkup.xml</file>
120  *     <file alias="example.css">data/example.css</file>
121  *   </gresource>
122  * </gresources>
123  * ]|
124  *
125  * This will create a resource bundle with the following files:
126  * |[
127  * /org/gtk/Example/data/splashscreen.png
128  * /org/gtk/Example/dialog.ui
129  * /org/gtk/Example/menumarkup.xml
130  * /org/gtk/Example/example.css
131  * ]|
132  *
133  * Note that all resources in the process share the same namespace, so use Java-style
134  * path prefixes (like in the above example) to avoid conflicts.
135  *
136  * You can then use [glib-compile-resources][glib-compile-resources] to compile the XML to a
137  * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
138  * --generate-header arguments to create a source file and header to link directly into your application.
139  * This will generate `get_resource()`, `register_resource()` and
140  * `unregister_resource()` functions, prefixed by the `--c-name` argument passed
141  * to [glib-compile-resources][glib-compile-resources]. `get_resource()` returns
142  * the generated #GResource object. The register and unregister functions
143  * register the resource so its files can be accessed using
144  * g_resources_lookup_data().
145  *
146  * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
147  * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
148  * to the data. You can also use URIs like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
149  * the resource data.
150  *
151  * Some higher-level APIs, such as #GtkApplication, will automatically load
152  * resources from certain well-known paths in the resource namespace as a
153  * convenience. See the documentation for those APIs for details.
154  *
155  * There are two forms of the generated source, the default version uses the compiler support for constructor
156  * and destructor functions (where available) to automatically create and register the #GResource on startup
157  * or library load time. If you pass `--manual-register`, two functions to register/unregister the resource are created
158  * instead. This requires an explicit initialization call in your application/library, but it works on all platforms,
159  * even on the minor ones where constructors are not supported. (Constructor support is available for at least Win32, Mac OS and Linux.)
160  *
161  * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
162  * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
163  * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
164  * are for your own resources, and resource data is often used once, during parsing, and then released.
165  *
166  * When debugging a program or testing a change to an installed version, it is often useful to be able to
167  * replace resources in the program or library, without recompiling, for debugging or quick hacking and testing
168  * purposes. Since GLib 2.50, it is possible to use the `G_RESOURCE_OVERLAYS` environment variable to selectively overlay
169  * resources with replacements from the filesystem.  It is a %G_SEARCHPATH_SEPARATOR-separated list of substitutions to perform
170  * during resource lookups. It is ignored when running in a setuid process.
171  *
172  * A substitution has the form
173  *
174  * |[
175  *    /org/gtk/libgtk=/home/desrt/gtk-overlay
176  * ]|
177  *
178  * The part before the `=` is the resource subpath for which the overlay applies.  The part after is a
179  * filesystem path which contains files and subdirectories as you would like to be loaded as resources with the
180  * equivalent names.
181  *
182  * In the example above, if an application tried to load a resource with the resource path
183  * `/org/gtk/libgtk/ui/gtkdialog.ui` then GResource would check the filesystem path
184  * `/home/desrt/gtk-overlay/ui/gtkdialog.ui`.  If a file was found there, it would be used instead.  This is an
185  * overlay, not an outright replacement, which means that if a file is not found at that path, the built-in
186  * version will be used instead.  Whiteouts are not currently supported.
187  *
188  * Substitutions must start with a slash, and must not contain a trailing slash before the '='.  The path after
189  * the slash should ideally be absolute, but this is not strictly required.  It is possible to overlay the
190  * location of a single resource with an individual file.
191  *
192  * Since: 2.32
193  */
194
195 /**
196  * GStaticResource:
197  *
198  * #GStaticResource is an opaque data structure and can only be accessed
199  * using the following functions.
200  **/
201 typedef gboolean (* CheckCandidate) (const gchar *candidate, gpointer user_data);
202
203 static gboolean
204 open_overlay_stream (const gchar *candidate,
205                      gpointer     user_data)
206 {
207   GInputStream **res = (GInputStream **) user_data;
208   GError *error = NULL;
209   GFile *file;
210
211   file = g_file_new_for_path (candidate);
212   *res = (GInputStream *) g_file_read (file, NULL, &error);
213
214   if (*res)
215     {
216       g_message ("Opened file '%s' as a resource overlay", candidate);
217     }
218   else
219     {
220       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
221         g_warning ("Can't open overlay file '%s': %s", candidate, error->message);
222       g_error_free (error);
223     }
224
225   g_object_unref (file);
226
227   return *res != NULL;
228 }
229
230 static gboolean
231 get_overlay_bytes (const gchar *candidate,
232                    gpointer     user_data)
233 {
234   GBytes **res = (GBytes **) user_data;
235   GMappedFile *mapped_file;
236   GError *error = NULL;
237
238   mapped_file = g_mapped_file_new (candidate, FALSE, &error);
239
240   if (mapped_file)
241     {
242       g_message ("Mapped file '%s' as a resource overlay", candidate);
243       *res = g_mapped_file_get_bytes (mapped_file);
244       g_mapped_file_unref (mapped_file);
245     }
246   else
247     {
248       if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
249         g_warning ("Can't mmap overlay file '%s': %s", candidate, error->message);
250       g_error_free (error);
251     }
252
253   return *res != NULL;
254 }
255
256 static gboolean
257 enumerate_overlay_dir (const gchar *candidate,
258                        gpointer     user_data)
259 {
260   GHashTable **hash = (GHashTable **) user_data;
261   GError *error = NULL;
262   GDir *dir;
263   const gchar *name;
264
265   dir = g_dir_open (candidate, 0, &error);
266   if (dir)
267     {
268       if (*hash == NULL)
269         /* note: keep in sync with same line below */
270         *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
271
272       g_message ("Enumerating directory '%s' as resource overlay", candidate);
273
274       while ((name = g_dir_read_name (dir)))
275         {
276           gchar *fullname = g_build_filename (candidate, name, NULL);
277
278           /* match gvdb behaviour by suffixing "/" on dirs */
279           if (g_file_test (fullname, G_FILE_TEST_IS_DIR))
280             g_hash_table_add (*hash, g_strconcat (name, "/", NULL));
281           else
282             g_hash_table_add (*hash, g_strdup (name));
283
284           g_free (fullname);
285         }
286
287       g_dir_close (dir);
288     }
289   else
290     {
291       if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
292         g_warning ("Can't enumerate overlay directory '%s': %s", candidate, error->message);
293       g_error_free (error);
294       return FALSE;
295     }
296
297   /* We may want to enumerate results from more than one overlay
298    * directory.
299    */
300   return FALSE;
301 }
302
303 typedef struct {
304   gsize size;
305   guint32 flags;
306 } InfoData;
307
308 static gboolean
309 get_overlay_info (const gchar *candidate,
310                   gpointer     user_data)
311 {
312   InfoData *info = user_data;
313   GStatBuf buf;
314
315   if (g_stat (candidate, &buf) < 0)
316     return FALSE;
317
318   info->size = buf.st_size;
319   info->flags = G_RESOURCE_FLAGS_NONE;
320
321   return TRUE;
322 }
323
324 static gboolean
325 g_resource_find_overlay (const gchar    *path,
326                          CheckCandidate  check,
327                          gpointer        user_data)
328 {
329   /* This is a null-terminated array of replacement strings (with '=' inside) */
330   static const gchar * const *overlay_dirs;
331   gboolean res = FALSE;
332   gint path_len = -1;
333   gint i;
334
335   /* We try to be very fast in case there are no overlays.  Otherwise,
336    * we can take a bit more time...
337    */
338
339   if (g_once_init_enter (&overlay_dirs))
340     {
341       gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
342       const gchar * const *result;
343       const gchar *envvar;
344
345       /* Don’t load overlays if setuid, as they could allow reading privileged
346        * files. */
347       envvar = !is_setuid ? g_getenv ("G_RESOURCE_OVERLAYS") : NULL;
348       if (envvar != NULL)
349         {
350           gchar **parts;
351           gint j;
352
353           parts = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
354
355           /* Sanity check the parts, dropping those that are invalid.
356            * 'i' may grow faster than 'j'.
357            */
358           for (i = j = 0; parts[i]; i++)
359             {
360               gchar *part = parts[i];
361               gchar *eq;
362
363               eq = strchr (part, '=');
364               if (eq == NULL)
365                 {
366                   g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks '='.  Ignoring.", part);
367                   g_free (part);
368                   continue;
369                 }
370
371               if (eq == part)
372                 {
373                   g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path before '='.  Ignoring.", part);
374                   g_free (part);
375                   continue;
376                 }
377
378               if (eq[1] == '\0')
379                 {
380                   g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path after '='.  Ignoring", part);
381                   g_free (part);
382                   continue;
383                 }
384
385               if (part[0] != '/')
386                 {
387                   g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks leading '/'.  Ignoring.", part);
388                   g_free (part);
389                   continue;
390                 }
391
392               if (eq[-1] == '/')
393                 {
394                   g_critical ("G_RESOURCE_OVERLAYS segment '%s' has trailing '/' before '='.  Ignoring", part);
395                   g_free (part);
396                   continue;
397                 }
398
399               if (!g_path_is_absolute (eq + 1))
400                 {
401                   g_critical ("G_RESOURCE_OVERLAYS segment '%s' does not have an absolute path after '='.  Ignoring", part);
402                   g_free (part);
403                   continue;
404                 }
405
406               g_message ("Adding GResources overlay '%s'", part);
407               parts[j++] = part;
408             }
409
410           parts[j] = NULL;
411
412           result = (const gchar **) parts;
413         }
414       else
415         {
416           /* We go out of the way to avoid malloc() in the normal case
417            * where the environment variable is not set.
418            */
419           static const gchar *const empty_strv[0 + 1] = { 0 };
420           result = empty_strv;
421         }
422
423       g_once_init_leave (&overlay_dirs, result);
424     }
425
426   for (i = 0; overlay_dirs[i]; i++)
427     {
428       const gchar *src;
429       gint src_len;
430       const gchar *dst;
431       gint dst_len;
432       gchar *candidate;
433
434       {
435         gchar *eq;
436
437         /* split the overlay into src/dst */
438         src = overlay_dirs[i];
439         eq = strchr (src, '=');
440         g_assert (eq); /* we checked this already */
441         src_len = eq - src;
442         dst = eq + 1;
443         /* hold off on dst_len because we will probably fail the checks below */
444       }
445
446       if (path_len == -1)
447         path_len = strlen (path);
448
449       /* The entire path is too short to match the source */
450       if (path_len < src_len)
451         continue;
452
453       /* It doesn't match the source */
454       if (memcmp (path, src, src_len) != 0)
455         continue;
456
457       /* The prefix matches, but it's not a complete path component */
458       if (path[src_len] && path[src_len] != '/')
459         continue;
460
461       /* OK.  Now we need this. */
462       dst_len = strlen (dst);
463
464       /* The candidate will be composed of:
465        *
466        *    dst + remaining_path + nul
467        */
468       candidate = g_malloc (dst_len + (path_len - src_len) + 1);
469       memcpy (candidate, dst, dst_len);
470       memcpy (candidate + dst_len, path + src_len, path_len - src_len);
471       candidate[dst_len + (path_len - src_len)] = '\0';
472
473       /* No matter what, 'r' is what we need, including the case where
474        * we are trying to enumerate a directory.
475        */
476       res = (* check) (candidate, user_data);
477       g_free (candidate);
478
479       if (res)
480         break;
481     }
482
483   return res;
484 }
485
486 /**
487  * g_resource_error_quark:
488  *
489  * Gets the #GResource Error Quark.
490  *
491  * Returns: a #GQuark
492  *
493  * Since: 2.32
494  */
495 G_DEFINE_QUARK (g-resource-error-quark, g_resource_error)
496
497 /**
498  * g_resource_ref:
499  * @resource: A #GResource
500  *
501  * Atomically increments the reference count of @resource by one. This
502  * function is MT-safe and may be called from any thread.
503  *
504  * Returns: The passed in #GResource
505  *
506  * Since: 2.32
507  **/
508 GResource *
509 g_resource_ref (GResource *resource)
510 {
511   g_atomic_int_inc (&resource->ref_count);
512   return resource;
513 }
514
515 /**
516  * g_resource_unref:
517  * @resource: A #GResource
518  *
519  * Atomically decrements the reference count of @resource by one. If the
520  * reference count drops to 0, all memory allocated by the resource is
521  * released. This function is MT-safe and may be called from any
522  * thread.
523  *
524  * Since: 2.32
525  **/
526 void
527 g_resource_unref (GResource *resource)
528 {
529   if (g_atomic_int_dec_and_test (&resource->ref_count))
530     {
531       gvdb_table_free (resource->table);
532       g_free (resource);
533     }
534 }
535
536 /*< internal >
537  * g_resource_new_from_table:
538  * @table: (transfer full): a GvdbTable
539  *
540  * Returns: (transfer full): a new #GResource for @table
541  */
542 static GResource *
543 g_resource_new_from_table (GvdbTable *table)
544 {
545   GResource *resource;
546
547   resource = g_new (GResource, 1);
548   resource->ref_count = 1;
549   resource->table = table;
550
551   return resource;
552 }
553
554 static void
555 g_resource_error_from_gvdb_table_error (GError **g_resource_error,
556                                         GError  *gvdb_table_error  /* (transfer full) */)
557 {
558   if (g_error_matches (gvdb_table_error, G_FILE_ERROR, G_FILE_ERROR_INVAL))
559     g_set_error_literal (g_resource_error,
560                          G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL,
561                          gvdb_table_error->message);
562   else
563     g_propagate_error (g_resource_error, g_steal_pointer (&gvdb_table_error));
564   g_clear_error (&gvdb_table_error);
565 }
566
567 /**
568  * g_resource_new_from_data:
569  * @data: A #GBytes
570  * @error: return location for a #GError, or %NULL
571  *
572  * Creates a GResource from a reference to the binary resource bundle.
573  * This will keep a reference to @data while the resource lives, so
574  * the data should not be modified or freed.
575  *
576  * If you want to use this resource in the global resource namespace you need
577  * to register it with g_resources_register().
578  *
579  * Note: @data must be backed by memory that is at least pointer aligned.
580  * Otherwise this function will internally create a copy of the memory since
581  * GLib 2.56, or in older versions fail and exit the process.
582  *
583  * If @data is empty or corrupt, %G_RESOURCE_ERROR_INTERNAL will be returned.
584  *
585  * Returns: (transfer full): a new #GResource, or %NULL on error
586  *
587  * Since: 2.32
588  **/
589 GResource *
590 g_resource_new_from_data (GBytes  *data,
591                           GError **error)
592 {
593   GvdbTable *table;
594   gboolean unref_data = FALSE;
595   GError *local_error = NULL;
596
597   if (((guintptr) g_bytes_get_data (data, NULL)) % sizeof (gpointer) != 0)
598     {
599       data = g_bytes_new (g_bytes_get_data (data, NULL),
600                           g_bytes_get_size (data));
601       unref_data = TRUE;
602     }
603
604   table = gvdb_table_new_from_bytes (data, TRUE, &local_error);
605
606   if (unref_data)
607     g_bytes_unref (data);
608
609   if (table == NULL)
610     {
611       g_resource_error_from_gvdb_table_error (error, g_steal_pointer (&local_error));
612       return NULL;
613     }
614
615   return g_resource_new_from_table (table);
616 }
617
618 /**
619  * g_resource_load:
620  * @filename: (type filename): the path of a filename to load, in the GLib filename encoding
621  * @error: return location for a #GError, or %NULL
622  *
623  * Loads a binary resource bundle and creates a #GResource representation of it, allowing
624  * you to query it for data.
625  *
626  * If you want to use this resource in the global resource namespace you need
627  * to register it with g_resources_register().
628  *
629  * If @filename is empty or the data in it is corrupt,
630  * %G_RESOURCE_ERROR_INTERNAL will be returned. If @filename doesn’t exist, or
631  * there is an error in reading it, an error from g_mapped_file_new() will be
632  * returned.
633  *
634  * Returns: (transfer full): a new #GResource, or %NULL on error
635  *
636  * Since: 2.32
637  **/
638 GResource *
639 g_resource_load (const gchar  *filename,
640                  GError      **error)
641 {
642   GvdbTable *table;
643   GError *local_error = NULL;
644
645   table = gvdb_table_new (filename, FALSE, &local_error);
646   if (table == NULL)
647     {
648       g_resource_error_from_gvdb_table_error (error, g_steal_pointer (&local_error));
649       return NULL;
650     }
651
652   return g_resource_new_from_table (table);
653 }
654
655 static gboolean
656 do_lookup (GResource             *resource,
657            const gchar           *path,
658            GResourceLookupFlags   lookup_flags,
659            gsize                 *size,
660            guint32               *flags,
661            const void           **data,
662            gsize                 *data_size,
663            GError               **error)
664 {
665   char *free_path = NULL;
666   gsize path_len;
667   gboolean res = FALSE;
668   GVariant *value;
669
670   /* Drop any trailing slash. */
671   path_len = strlen (path);
672   if (path_len >= 1 && path[path_len-1] == '/')
673     {
674       path = free_path = g_strdup (path);
675       free_path[path_len-1] = 0;
676     }
677
678   value = gvdb_table_get_raw_value (resource->table, path);
679
680   if (value == NULL)
681     {
682       g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
683                    _("The resource at “%s” does not exist"),
684                    path);
685     }
686   else
687     {
688       guint32 _size, _flags;
689       GVariant *array;
690
691       g_variant_get (value, "(uu@ay)",
692                      &_size,
693                      &_flags,
694                      &array);
695
696       _size = GUINT32_FROM_LE (_size);
697       _flags = GUINT32_FROM_LE (_flags);
698
699       if (size)
700         *size = _size;
701       if (flags)
702         *flags = _flags;
703       if (data)
704         *data = g_variant_get_data (array);
705       if (data_size)
706         {
707           /* Don't report trailing newline that non-compressed files has */
708           if (_flags & G_RESOURCE_FLAGS_COMPRESSED)
709             *data_size = g_variant_get_size (array);
710           else
711             *data_size = g_variant_get_size (array) - 1;
712         }
713       g_variant_unref (array);
714       g_variant_unref (value);
715
716       res = TRUE;
717     }
718
719   g_free (free_path);
720   return res;
721 }
722
723 /**
724  * g_resource_open_stream:
725  * @resource: A #GResource
726  * @path: A pathname inside the resource
727  * @lookup_flags: A #GResourceLookupFlags
728  * @error: return location for a #GError, or %NULL
729  *
730  * Looks for a file at the specified @path in the resource and
731  * returns a #GInputStream that lets you read the data.
732  *
733  * @lookup_flags controls the behaviour of the lookup.
734  *
735  * Returns: (transfer full): #GInputStream or %NULL on error.
736  *     Free the returned object with g_object_unref()
737  *
738  * Since: 2.32
739  **/
740 GInputStream *
741 g_resource_open_stream (GResource             *resource,
742                         const gchar           *path,
743                         GResourceLookupFlags   lookup_flags,
744                         GError               **error)
745 {
746   const void *data;
747   gsize data_size;
748   guint32 flags;
749   GInputStream *stream, *stream2;
750
751   if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error))
752     return NULL;
753
754   stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
755   g_object_set_data_full (G_OBJECT (stream), "g-resource",
756                           g_resource_ref (resource),
757                           (GDestroyNotify)g_resource_unref);
758
759   if (flags & G_RESOURCE_FLAGS_COMPRESSED)
760     {
761       GZlibDecompressor *decompressor =
762         g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
763
764       stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor));
765       g_object_unref (decompressor);
766       g_object_unref (stream);
767       stream = stream2;
768     }
769
770   return stream;
771 }
772
773 /**
774  * g_resource_lookup_data:
775  * @resource: A #GResource
776  * @path: A pathname inside the resource
777  * @lookup_flags: A #GResourceLookupFlags
778  * @error: return location for a #GError, or %NULL
779  *
780  * Looks for a file at the specified @path in the resource and
781  * returns a #GBytes that lets you directly access the data in
782  * memory.
783  *
784  * The data is always followed by a zero byte, so you
785  * can safely use the data as a C string. However, that byte
786  * is not included in the size of the GBytes.
787  *
788  * For uncompressed resource files this is a pointer directly into
789  * the resource bundle, which is typically in some readonly data section
790  * in the program binary. For compressed files we allocate memory on
791  * the heap and automatically uncompress the data.
792  *
793  * @lookup_flags controls the behaviour of the lookup.
794  *
795  * Returns: (transfer full): #GBytes or %NULL on error.
796  *     Free the returned object with g_bytes_unref()
797  *
798  * Since: 2.32
799  **/
800 GBytes *
801 g_resource_lookup_data (GResource             *resource,
802                         const gchar           *path,
803                         GResourceLookupFlags   lookup_flags,
804                         GError               **error)
805 {
806   const void *data;
807   guint32 flags;
808   gsize data_size;
809   gsize size;
810
811   if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
812     return NULL;
813
814   if (size == 0)
815     return g_bytes_new_with_free_func ("", 0, (GDestroyNotify) g_resource_unref, g_resource_ref (resource));
816   else if (flags & G_RESOURCE_FLAGS_COMPRESSED)
817     {
818       char *uncompressed, *d;
819       const char *s;
820       GConverterResult res;
821       gsize d_size, s_size;
822       gsize bytes_read, bytes_written;
823
824
825       GZlibDecompressor *decompressor =
826         g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
827
828       uncompressed = g_malloc (size + 1);
829
830       s = data;
831       s_size = data_size;
832       d = uncompressed;
833       d_size = size;
834
835       do
836         {
837           res = g_converter_convert (G_CONVERTER (decompressor),
838                                      s, s_size,
839                                      d, d_size,
840                                      G_CONVERTER_INPUT_AT_END,
841                                      &bytes_read,
842                                      &bytes_written,
843                                      NULL);
844           if (res == G_CONVERTER_ERROR)
845             {
846               g_free (uncompressed);
847               g_object_unref (decompressor);
848
849               g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL,
850                            _("The resource at “%s” failed to decompress"),
851                            path);
852               return NULL;
853
854             }
855           s += bytes_read;
856           s_size -= bytes_read;
857           d += bytes_written;
858           d_size -= bytes_written;
859         }
860       while (res != G_CONVERTER_FINISHED);
861
862       uncompressed[size] = 0; /* Zero terminate */
863
864       g_object_unref (decompressor);
865
866       return g_bytes_new_take (uncompressed, size);
867     }
868   else
869     return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
870 }
871
872 /**
873  * g_resource_get_info:
874  * @resource: A #GResource
875  * @path: A pathname inside the resource
876  * @lookup_flags: A #GResourceLookupFlags
877  * @size:  (out) (optional): a location to place the length of the contents of the file,
878  *    or %NULL if the length is not needed
879  * @flags:  (out) (optional): a location to place the flags about the file,
880  *    or %NULL if the length is not needed
881  * @error: return location for a #GError, or %NULL
882  *
883  * Looks for a file at the specified @path in the resource and
884  * if found returns information about it.
885  *
886  * @lookup_flags controls the behaviour of the lookup.
887  *
888  * Returns: %TRUE if the file was found. %FALSE if there were errors
889  *
890  * Since: 2.32
891  **/
892 gboolean
893 g_resource_get_info (GResource             *resource,
894                      const gchar           *path,
895                      GResourceLookupFlags   lookup_flags,
896                      gsize                 *size,
897                      guint32               *flags,
898                      GError               **error)
899 {
900   return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error);
901 }
902
903 /**
904  * g_resource_enumerate_children:
905  * @resource: A #GResource
906  * @path: A pathname inside the resource
907  * @lookup_flags: A #GResourceLookupFlags
908  * @error: return location for a #GError, or %NULL
909  *
910  * Returns all the names of children at the specified @path in the resource.
911  * The return result is a %NULL terminated list of strings which should
912  * be released with g_strfreev().
913  *
914  * If @path is invalid or does not exist in the #GResource,
915  * %G_RESOURCE_ERROR_NOT_FOUND will be returned.
916  *
917  * @lookup_flags controls the behaviour of the lookup.
918  *
919  * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
920  *
921  * Since: 2.32
922  **/
923 gchar **
924 g_resource_enumerate_children (GResource             *resource,
925                                const gchar           *path,
926                                GResourceLookupFlags   lookup_flags,
927                                GError               **error)
928 {
929   gchar local_str[256];
930   const gchar *path_with_slash;
931   gchar **children;
932   gchar *free_path = NULL;
933   gsize path_len;
934
935   /*
936    * Size of 256 is arbitrarily chosen based on being large enough
937    * for pretty much everything we come across, but not cumbersome
938    * on the stack. It also matches common cacheline sizes.
939    */
940
941   if (*path == 0)
942     {
943       if (error)
944         g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
945                      _("The resource at “%s” does not exist"),
946                      path);
947       return NULL;
948     }
949
950   path_len = strlen (path);
951
952   if G_UNLIKELY (path[path_len-1] != '/')
953     {
954       if (path_len < sizeof (local_str) - 2)
955         {
956           /*
957            * We got a path that does not have a trailing /. It is not the
958            * ideal use of this API as we require trailing / for our lookup
959            * into gvdb. Some degenerate application configurations can hit
960            * this code path quite a bit, so we try to avoid using the
961            * g_strconcat()/g_free().
962            */
963           memcpy (local_str, path, path_len);
964           local_str[path_len] = '/';
965           local_str[path_len+1] = 0;
966           path_with_slash = local_str;
967         }
968       else
969         {
970           path_with_slash = free_path = g_strconcat (path, "/", NULL);
971         }
972     }
973   else
974     {
975       path_with_slash = path;
976     }
977
978   children = gvdb_table_list (resource->table, path_with_slash);
979   g_free (free_path);
980
981   if (children == NULL)
982     {
983       if (error)
984         g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
985                      _("The resource at “%s” does not exist"),
986                      path);
987       return NULL;
988     }
989
990   return children;
991 }
992
993 static GRWLock resources_lock;
994 static GList *registered_resources;
995
996 /* This is updated atomically, so we can append to it and check for NULL outside the
997    lock, but all other accesses are done under the write lock */
998 static GStaticResource *lazy_register_resources;
999
1000 static void
1001 g_resources_register_unlocked (GResource *resource)
1002 {
1003   registered_resources = g_list_prepend (registered_resources, g_resource_ref (resource));
1004 }
1005
1006 static void
1007 g_resources_unregister_unlocked (GResource *resource)
1008 {
1009   if (g_list_find (registered_resources, resource) == NULL)
1010     {
1011       g_warning ("Tried to remove not registered resource");
1012     }
1013   else
1014     {
1015       registered_resources = g_list_remove (registered_resources, resource);
1016       g_resource_unref (resource);
1017     }
1018 }
1019
1020 /**
1021  * g_resources_register:
1022  * @resource: A #GResource
1023  *
1024  * Registers the resource with the process-global set of resources.
1025  * Once a resource is registered the files in it can be accessed
1026  * with the global resource lookup functions like g_resources_lookup_data().
1027  *
1028  * Since: 2.32
1029  **/
1030 void
1031 g_resources_register (GResource *resource)
1032 {
1033   g_rw_lock_writer_lock (&resources_lock);
1034   g_resources_register_unlocked (resource);
1035   g_rw_lock_writer_unlock (&resources_lock);
1036 }
1037
1038 /**
1039  * g_resources_unregister:
1040  * @resource: A #GResource
1041  *
1042  * Unregisters the resource from the process-global set of resources.
1043  *
1044  * Since: 2.32
1045  **/
1046 void
1047 g_resources_unregister (GResource *resource)
1048 {
1049   g_rw_lock_writer_lock (&resources_lock);
1050   g_resources_unregister_unlocked (resource);
1051   g_rw_lock_writer_unlock (&resources_lock);
1052 }
1053
1054 /**
1055  * g_resources_open_stream:
1056  * @path: A pathname inside the resource
1057  * @lookup_flags: A #GResourceLookupFlags
1058  * @error: return location for a #GError, or %NULL
1059  *
1060  * Looks for a file at the specified @path in the set of
1061  * globally registered resources and returns a #GInputStream
1062  * that lets you read the data.
1063  *
1064  * @lookup_flags controls the behaviour of the lookup.
1065  *
1066  * Returns: (transfer full): #GInputStream or %NULL on error.
1067  *     Free the returned object with g_object_unref()
1068  *
1069  * Since: 2.32
1070  **/
1071 GInputStream *
1072 g_resources_open_stream (const gchar           *path,
1073                          GResourceLookupFlags   lookup_flags,
1074                          GError               **error)
1075 {
1076   GInputStream *res = NULL;
1077   GList *l;
1078   GInputStream *stream;
1079
1080   if (g_resource_find_overlay (path, open_overlay_stream, &res))
1081     return res;
1082
1083   register_lazy_static_resources ();
1084
1085   g_rw_lock_reader_lock (&resources_lock);
1086
1087   for (l = registered_resources; l != NULL; l = l->next)
1088     {
1089       GResource *r = l->data;
1090       GError *my_error = NULL;
1091
1092       stream = g_resource_open_stream (r, path, lookup_flags, &my_error);
1093       if (stream == NULL &&
1094           g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
1095         {
1096           g_clear_error (&my_error);
1097         }
1098       else
1099         {
1100           if (stream == NULL)
1101             g_propagate_error (error, my_error);
1102           res = stream;
1103           break;
1104         }
1105     }
1106
1107   if (l == NULL)
1108     g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
1109                  _("The resource at “%s” does not exist"),
1110                  path);
1111
1112   g_rw_lock_reader_unlock (&resources_lock);
1113
1114   return res;
1115 }
1116
1117 /**
1118  * g_resources_lookup_data:
1119  * @path: A pathname inside the resource
1120  * @lookup_flags: A #GResourceLookupFlags
1121  * @error: return location for a #GError, or %NULL
1122  *
1123  * Looks for a file at the specified @path in the set of
1124  * globally registered resources and returns a #GBytes that
1125  * lets you directly access the data in memory.
1126  *
1127  * The data is always followed by a zero byte, so you
1128  * can safely use the data as a C string. However, that byte
1129  * is not included in the size of the GBytes.
1130  *
1131  * For uncompressed resource files this is a pointer directly into
1132  * the resource bundle, which is typically in some readonly data section
1133  * in the program binary. For compressed files we allocate memory on
1134  * the heap and automatically uncompress the data.
1135  *
1136  * @lookup_flags controls the behaviour of the lookup.
1137  *
1138  * Returns: (transfer full): #GBytes or %NULL on error.
1139  *     Free the returned object with g_bytes_unref()
1140  *
1141  * Since: 2.32
1142  **/
1143 GBytes *
1144 g_resources_lookup_data (const gchar           *path,
1145                          GResourceLookupFlags   lookup_flags,
1146                          GError               **error)
1147 {
1148   GBytes *res = NULL;
1149   GList *l;
1150   GBytes *data;
1151
1152   if (g_resource_find_overlay (path, get_overlay_bytes, &res))
1153     return res;
1154
1155   register_lazy_static_resources ();
1156
1157   g_rw_lock_reader_lock (&resources_lock);
1158
1159   for (l = registered_resources; l != NULL; l = l->next)
1160     {
1161       GResource *r = l->data;
1162       GError *my_error = NULL;
1163
1164       data = g_resource_lookup_data (r, path, lookup_flags, &my_error);
1165       if (data == NULL &&
1166           g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
1167         {
1168           g_clear_error (&my_error);
1169         }
1170       else
1171         {
1172           if (data == NULL)
1173             g_propagate_error (error, my_error);
1174           res = data;
1175           break;
1176         }
1177     }
1178
1179   if (l == NULL)
1180     g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
1181                  _("The resource at “%s” does not exist"),
1182                  path);
1183
1184   g_rw_lock_reader_unlock (&resources_lock);
1185
1186   return res;
1187 }
1188
1189 /**
1190  * g_resources_enumerate_children:
1191  * @path: A pathname inside the resource
1192  * @lookup_flags: A #GResourceLookupFlags
1193  * @error: return location for a #GError, or %NULL
1194  *
1195  * Returns all the names of children at the specified @path in the set of
1196  * globally registered resources.
1197  * The return result is a %NULL terminated list of strings which should
1198  * be released with g_strfreev().
1199  *
1200  * @lookup_flags controls the behaviour of the lookup.
1201  *
1202  * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
1203  *
1204  * Since: 2.32
1205  **/
1206 gchar **
1207 g_resources_enumerate_children (const gchar           *path,
1208                                 GResourceLookupFlags   lookup_flags,
1209                                 GError               **error)
1210 {
1211   GHashTable *hash = NULL;
1212   GList *l;
1213   char **children;
1214   int i;
1215
1216   /* This will enumerate actual files found in overlay directories but
1217    * will not enumerate the overlays themselves.  For example, if we
1218    * have an overlay "/org/gtk=/path/to/files" and we enumerate "/org"
1219    * then we will not see "gtk" in the result set unless it is provided
1220    * by another resource file.
1221    *
1222    * This is probably not going to be a problem since if we are doing
1223    * such an overlay, we probably will already have that path.
1224    */
1225   g_resource_find_overlay (path, enumerate_overlay_dir, &hash);
1226
1227   register_lazy_static_resources ();
1228
1229   g_rw_lock_reader_lock (&resources_lock);
1230
1231   for (l = registered_resources; l != NULL; l = l->next)
1232     {
1233       GResource *r = l->data;
1234
1235       children = g_resource_enumerate_children (r, path, 0, NULL);
1236
1237       if (children != NULL)
1238         {
1239           if (hash == NULL)
1240             /* note: keep in sync with same line above */
1241             hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1242
1243           for (i = 0; children[i] != NULL; i++)
1244             g_hash_table_add (hash, children[i]);
1245           g_free (children);
1246         }
1247     }
1248
1249   g_rw_lock_reader_unlock (&resources_lock);
1250
1251   if (hash == NULL)
1252     {
1253       if (error)
1254         g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
1255                      _("The resource at “%s” does not exist"),
1256                      path);
1257       return NULL;
1258     }
1259   else
1260     {
1261       children = (gchar **) g_hash_table_get_keys_as_array (hash, NULL);
1262       g_hash_table_steal_all (hash);
1263       g_hash_table_destroy (hash);
1264
1265       return children;
1266     }
1267 }
1268
1269 /**
1270  * g_resources_get_info:
1271  * @path: A pathname inside the resource
1272  * @lookup_flags: A #GResourceLookupFlags
1273  * @size:  (out) (optional): a location to place the length of the contents of the file,
1274  *    or %NULL if the length is not needed
1275  * @flags:  (out) (optional): a location to place the #GResourceFlags about the file,
1276  *    or %NULL if the flags are not needed
1277  * @error: return location for a #GError, or %NULL
1278  *
1279  * Looks for a file at the specified @path in the set of
1280  * globally registered resources and if found returns information about it.
1281  *
1282  * @lookup_flags controls the behaviour of the lookup.
1283  *
1284  * Returns: %TRUE if the file was found. %FALSE if there were errors
1285  *
1286  * Since: 2.32
1287  **/
1288 gboolean
1289 g_resources_get_info (const gchar           *path,
1290                       GResourceLookupFlags   lookup_flags,
1291                       gsize                 *size,
1292                       guint32               *flags,
1293                       GError               **error)
1294 {
1295   gboolean res = FALSE;
1296   GList *l;
1297   gboolean r_res;
1298   InfoData info;
1299
1300   if (g_resource_find_overlay (path, get_overlay_info, &info))
1301     {
1302       if (size)
1303         *size = info.size;
1304       if (flags)
1305         *flags = info.flags;
1306
1307       return TRUE;
1308     }
1309
1310   register_lazy_static_resources ();
1311
1312   g_rw_lock_reader_lock (&resources_lock);
1313
1314   for (l = registered_resources; l != NULL; l = l->next)
1315     {
1316       GResource *r = l->data;
1317       GError *my_error = NULL;
1318
1319       r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error);
1320       if (!r_res &&
1321           g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
1322         {
1323           g_clear_error (&my_error);
1324         }
1325       else
1326         {
1327           if (!r_res)
1328             g_propagate_error (error, my_error);
1329           res = r_res;
1330           break;
1331         }
1332     }
1333
1334   if (l == NULL)
1335     g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
1336                  _("The resource at “%s” does not exist"),
1337                  path);
1338
1339   g_rw_lock_reader_unlock (&resources_lock);
1340
1341   return res;
1342 }
1343
1344 /* This code is to handle registration of resources very early, from a constructor.
1345  * At that point we'd like to do minimal work, to avoid ordering issues. For instance,
1346  * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable
1347  * before the first call to g_malloc.
1348  *
1349  * So, what we do at construction time is that we just register a static structure on
1350  * a list of resources that need to be initialized, and then later, when doing any lookups
1351  * in the global list of registered resources, or when getting a reference to the
1352  * lazily initialized resource we lazily create and register all the GResources on
1353  * the lazy list.
1354  *
1355  * To avoid having to use locks in the constructor, and having to grab the writer lock
1356  * when checking the lazy registering list we update lazy_register_resources in
1357  * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all
1358  * operations except:
1359  *  * check if there are any resources to lazily initialize
1360  *  * Add a static resource to the lazy init list
1361  * Do use the full writer lock for protection.
1362  */
1363
1364 static void
1365 register_lazy_static_resources_unlocked (void)
1366 {
1367   GStaticResource *list = g_atomic_pointer_get (&lazy_register_resources);
1368
1369   while (!g_atomic_pointer_compare_and_exchange_full (&lazy_register_resources, list, NULL, &list))
1370     ;
1371
1372   while (list != NULL)
1373     {
1374       GBytes *bytes = g_bytes_new_static (list->data, list->data_len);
1375       GResource *resource = g_resource_new_from_data (bytes, NULL);
1376       if (resource)
1377         {
1378           g_resources_register_unlocked (resource);
1379           g_atomic_pointer_set (&list->resource, resource);
1380         }
1381       g_bytes_unref (bytes);
1382
1383       list = list->next;
1384     }
1385 }
1386
1387 static void
1388 register_lazy_static_resources (void)
1389 {
1390   if (g_atomic_pointer_get (&lazy_register_resources) == NULL)
1391     return;
1392
1393   g_rw_lock_writer_lock (&resources_lock);
1394   register_lazy_static_resources_unlocked ();
1395   g_rw_lock_writer_unlock (&resources_lock);
1396 }
1397
1398 /**
1399  * g_static_resource_init:
1400  * @static_resource: pointer to a static #GStaticResource
1401  *
1402  * Initializes a GResource from static data using a
1403  * GStaticResource.
1404  *
1405  * This is normally used by code generated by
1406  * [glib-compile-resources][glib-compile-resources]
1407  * and is not typically used by other code.
1408  *
1409  * Since: 2.32
1410  **/
1411 void
1412 g_static_resource_init (GStaticResource *static_resource)
1413 {
1414   GStaticResource *next;
1415
1416   do
1417     {
1418       next = g_atomic_pointer_get (&lazy_register_resources);
1419       static_resource->next = next;
1420     }
1421   while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource));
1422 }
1423
1424 /**
1425  * g_static_resource_fini:
1426  * @static_resource: pointer to a static #GStaticResource
1427  *
1428  * Finalized a GResource initialized by g_static_resource_init().
1429  *
1430  * This is normally used by code generated by
1431  * [glib-compile-resources][glib-compile-resources]
1432  * and is not typically used by other code.
1433  *
1434  * Since: 2.32
1435  **/
1436 void
1437 g_static_resource_fini (GStaticResource *static_resource)
1438 {
1439   GResource *resource;
1440
1441   g_rw_lock_writer_lock (&resources_lock);
1442
1443   register_lazy_static_resources_unlocked ();
1444
1445   resource = g_atomic_pointer_exchange (&static_resource->resource, NULL);
1446   if (resource)
1447     {
1448       /* There should be at least two references to the resource now: one for
1449        * static_resource->resource, and one in the registered_resources list. */
1450       g_assert (g_atomic_int_get (&resource->ref_count) >= 2);
1451
1452       g_resources_unregister_unlocked (resource);
1453       g_resource_unref (resource);
1454     }
1455
1456   g_rw_lock_writer_unlock (&resources_lock);
1457 }
1458
1459 /**
1460  * g_static_resource_get_resource:
1461  * @static_resource: pointer to a static #GStaticResource
1462  *
1463  * Gets the GResource that was registered by a call to g_static_resource_init().
1464  *
1465  * This is normally used by code generated by
1466  * [glib-compile-resources][glib-compile-resources]
1467  * and is not typically used by other code.
1468  *
1469  * Returns:  (transfer none): a #GResource
1470  *
1471  * Since: 2.32
1472  **/
1473 GResource *
1474 g_static_resource_get_resource (GStaticResource *static_resource)
1475 {
1476   register_lazy_static_resources ();
1477
1478   return g_atomic_pointer_get (&static_resource->resource);
1479 }