/src/glib/gio/gresource.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
207 | 0 | GInputStream **res = (GInputStream **) user_data; |
208 | 0 | GError *error = NULL; |
209 | 0 | GFile *file; |
210 | |
|
211 | 0 | file = g_file_new_for_path (candidate); |
212 | 0 | *res = (GInputStream *) g_file_read (file, NULL, &error); |
213 | |
|
214 | 0 | if (*res) |
215 | 0 | { |
216 | 0 | g_message ("Opened file '%s' as a resource overlay", candidate); |
217 | 0 | } |
218 | 0 | else |
219 | 0 | { |
220 | 0 | if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) |
221 | 0 | g_warning ("Can't open overlay file '%s': %s", candidate, error->message); |
222 | 0 | g_error_free (error); |
223 | 0 | } |
224 | |
|
225 | 0 | g_object_unref (file); |
226 | |
|
227 | 0 | return *res != NULL; |
228 | 0 | } |
229 | | |
230 | | static gboolean |
231 | | get_overlay_bytes (const gchar *candidate, |
232 | | gpointer user_data) |
233 | 0 | { |
234 | 0 | GBytes **res = (GBytes **) user_data; |
235 | 0 | GMappedFile *mapped_file; |
236 | 0 | GError *error = NULL; |
237 | |
|
238 | 0 | mapped_file = g_mapped_file_new (candidate, FALSE, &error); |
239 | |
|
240 | 0 | if (mapped_file) |
241 | 0 | { |
242 | 0 | g_message ("Mapped file '%s' as a resource overlay", candidate); |
243 | 0 | *res = g_mapped_file_get_bytes (mapped_file); |
244 | 0 | g_mapped_file_unref (mapped_file); |
245 | 0 | } |
246 | 0 | else |
247 | 0 | { |
248 | 0 | if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) |
249 | 0 | g_warning ("Can't mmap overlay file '%s': %s", candidate, error->message); |
250 | 0 | g_error_free (error); |
251 | 0 | } |
252 | |
|
253 | 0 | return *res != NULL; |
254 | 0 | } |
255 | | |
256 | | static gboolean |
257 | | enumerate_overlay_dir (const gchar *candidate, |
258 | | gpointer user_data) |
259 | 0 | { |
260 | 0 | GHashTable **hash = (GHashTable **) user_data; |
261 | 0 | GError *error = NULL; |
262 | 0 | GDir *dir; |
263 | 0 | const gchar *name; |
264 | |
|
265 | 0 | dir = g_dir_open (candidate, 0, &error); |
266 | 0 | if (dir) |
267 | 0 | { |
268 | 0 | if (*hash == NULL) |
269 | | /* note: keep in sync with same line below */ |
270 | 0 | *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
271 | |
|
272 | 0 | g_message ("Enumerating directory '%s' as resource overlay", candidate); |
273 | |
|
274 | 0 | while ((name = g_dir_read_name (dir))) |
275 | 0 | { |
276 | 0 | gchar *fullname = g_build_filename (candidate, name, NULL); |
277 | | |
278 | | /* match gvdb behaviour by suffixing "/" on dirs */ |
279 | 0 | if (g_file_test (fullname, G_FILE_TEST_IS_DIR)) |
280 | 0 | g_hash_table_add (*hash, g_strconcat (name, "/", NULL)); |
281 | 0 | else |
282 | 0 | g_hash_table_add (*hash, g_strdup (name)); |
283 | |
|
284 | 0 | g_free (fullname); |
285 | 0 | } |
286 | |
|
287 | 0 | g_dir_close (dir); |
288 | 0 | } |
289 | 0 | else |
290 | 0 | { |
291 | 0 | if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) |
292 | 0 | g_warning ("Can't enumerate overlay directory '%s': %s", candidate, error->message); |
293 | 0 | g_error_free (error); |
294 | 0 | return FALSE; |
295 | 0 | } |
296 | | |
297 | | /* We may want to enumerate results from more than one overlay |
298 | | * directory. |
299 | | */ |
300 | 0 | return FALSE; |
301 | 0 | } |
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 | 0 | { |
312 | 0 | InfoData *info = user_data; |
313 | 0 | GStatBuf buf; |
314 | |
|
315 | 0 | if (g_stat (candidate, &buf) < 0) |
316 | 0 | return FALSE; |
317 | | |
318 | 0 | info->size = buf.st_size; |
319 | 0 | info->flags = G_RESOURCE_FLAGS_NONE; |
320 | |
|
321 | 0 | return TRUE; |
322 | 0 | } |
323 | | |
324 | | static gboolean |
325 | | g_resource_find_overlay (const gchar *path, |
326 | | CheckCandidate check, |
327 | | gpointer user_data) |
328 | 0 | { |
329 | | /* This is a null-terminated array of replacement strings (with '=' inside) */ |
330 | 0 | static const gchar * const *overlay_dirs; |
331 | 0 | gboolean res = FALSE; |
332 | 0 | gint path_len = -1; |
333 | 0 | 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 | 0 | if (g_once_init_enter (&overlay_dirs)) |
340 | 0 | { |
341 | 0 | gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) (); |
342 | 0 | const gchar * const *result; |
343 | 0 | const gchar *envvar; |
344 | | |
345 | | /* Don’t load overlays if setuid, as they could allow reading privileged |
346 | | * files. */ |
347 | 0 | envvar = !is_setuid ? g_getenv ("G_RESOURCE_OVERLAYS") : NULL; |
348 | 0 | if (envvar != NULL) |
349 | 0 | { |
350 | 0 | gchar **parts; |
351 | 0 | gint j; |
352 | |
|
353 | 0 | 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 | 0 | for (i = j = 0; parts[i]; i++) |
359 | 0 | { |
360 | 0 | gchar *part = parts[i]; |
361 | 0 | gchar *eq; |
362 | |
|
363 | 0 | eq = strchr (part, '='); |
364 | 0 | if (eq == NULL) |
365 | 0 | { |
366 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks '='. Ignoring.", part); |
367 | 0 | g_free (part); |
368 | 0 | continue; |
369 | 0 | } |
370 | | |
371 | 0 | if (eq == part) |
372 | 0 | { |
373 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path before '='. Ignoring.", part); |
374 | 0 | g_free (part); |
375 | 0 | continue; |
376 | 0 | } |
377 | | |
378 | 0 | if (eq[1] == '\0') |
379 | 0 | { |
380 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path after '='. Ignoring", part); |
381 | 0 | g_free (part); |
382 | 0 | continue; |
383 | 0 | } |
384 | | |
385 | 0 | if (part[0] != '/') |
386 | 0 | { |
387 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks leading '/'. Ignoring.", part); |
388 | 0 | g_free (part); |
389 | 0 | continue; |
390 | 0 | } |
391 | | |
392 | 0 | if (eq[-1] == '/') |
393 | 0 | { |
394 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' has trailing '/' before '='. Ignoring", part); |
395 | 0 | g_free (part); |
396 | 0 | continue; |
397 | 0 | } |
398 | | |
399 | 0 | if (!g_path_is_absolute (eq + 1)) |
400 | 0 | { |
401 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' does not have an absolute path after '='. Ignoring", part); |
402 | 0 | g_free (part); |
403 | 0 | continue; |
404 | 0 | } |
405 | | |
406 | 0 | g_message ("Adding GResources overlay '%s'", part); |
407 | 0 | parts[j++] = part; |
408 | 0 | } |
409 | |
|
410 | 0 | parts[j] = NULL; |
411 | |
|
412 | 0 | result = (const gchar **) parts; |
413 | 0 | } |
414 | 0 | else |
415 | 0 | { |
416 | | /* We go out of the way to avoid malloc() in the normal case |
417 | | * where the environment variable is not set. |
418 | | */ |
419 | 0 | static const gchar *const empty_strv[0 + 1] = { 0 }; |
420 | 0 | result = empty_strv; |
421 | 0 | } |
422 | |
|
423 | 0 | g_once_init_leave (&overlay_dirs, result); |
424 | 0 | } |
425 | |
|
426 | 0 | for (i = 0; overlay_dirs[i]; i++) |
427 | 0 | { |
428 | 0 | const gchar *src; |
429 | 0 | gint src_len; |
430 | 0 | const gchar *dst; |
431 | 0 | gint dst_len; |
432 | 0 | gchar *candidate; |
433 | |
|
434 | 0 | { |
435 | 0 | gchar *eq; |
436 | | |
437 | | /* split the overlay into src/dst */ |
438 | 0 | src = overlay_dirs[i]; |
439 | 0 | eq = strchr (src, '='); |
440 | 0 | g_assert (eq); /* we checked this already */ |
441 | 0 | src_len = eq - src; |
442 | 0 | dst = eq + 1; |
443 | | /* hold off on dst_len because we will probably fail the checks below */ |
444 | 0 | } |
445 | | |
446 | 0 | if (path_len == -1) |
447 | 0 | path_len = strlen (path); |
448 | | |
449 | | /* The entire path is too short to match the source */ |
450 | 0 | if (path_len < src_len) |
451 | 0 | continue; |
452 | | |
453 | | /* It doesn't match the source */ |
454 | 0 | if (memcmp (path, src, src_len) != 0) |
455 | 0 | continue; |
456 | | |
457 | | /* The prefix matches, but it's not a complete path component */ |
458 | 0 | if (path[src_len] && path[src_len] != '/') |
459 | 0 | continue; |
460 | | |
461 | | /* OK. Now we need this. */ |
462 | 0 | dst_len = strlen (dst); |
463 | | |
464 | | /* The candidate will be composed of: |
465 | | * |
466 | | * dst + remaining_path + nul |
467 | | */ |
468 | 0 | candidate = g_malloc (dst_len + (path_len - src_len) + 1); |
469 | 0 | memcpy (candidate, dst, dst_len); |
470 | 0 | memcpy (candidate + dst_len, path + src_len, path_len - src_len); |
471 | 0 | 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 | 0 | res = (* check) (candidate, user_data); |
477 | 0 | g_free (candidate); |
478 | |
|
479 | 0 | if (res) |
480 | 0 | break; |
481 | 0 | } |
482 | | |
483 | 0 | return res; |
484 | 0 | } |
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 | 0 | { |
511 | 0 | g_atomic_int_inc (&resource->ref_count); |
512 | 0 | return resource; |
513 | 0 | } |
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 | 0 | { |
529 | 0 | if (g_atomic_int_dec_and_test (&resource->ref_count)) |
530 | 0 | { |
531 | 0 | gvdb_table_free (resource->table); |
532 | 0 | g_free (resource); |
533 | 0 | } |
534 | 0 | } |
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 | 0 | { |
545 | 0 | GResource *resource; |
546 | |
|
547 | 0 | resource = g_new (GResource, 1); |
548 | 0 | resource->ref_count = 1; |
549 | 0 | resource->table = table; |
550 | |
|
551 | 0 | return resource; |
552 | 0 | } |
553 | | |
554 | | static void |
555 | | g_resource_error_from_gvdb_table_error (GError **g_resource_error, |
556 | | GError *gvdb_table_error /* (transfer full) */) |
557 | 0 | { |
558 | 0 | if (g_error_matches (gvdb_table_error, G_FILE_ERROR, G_FILE_ERROR_INVAL)) |
559 | 0 | g_set_error_literal (g_resource_error, |
560 | 0 | G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL, |
561 | 0 | gvdb_table_error->message); |
562 | 0 | else |
563 | 0 | g_propagate_error (g_resource_error, g_steal_pointer (&gvdb_table_error)); |
564 | 0 | g_clear_error (&gvdb_table_error); |
565 | 0 | } |
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 | 0 | { |
593 | 0 | GvdbTable *table; |
594 | 0 | gboolean unref_data = FALSE; |
595 | 0 | GError *local_error = NULL; |
596 | |
|
597 | 0 | if (((guintptr) g_bytes_get_data (data, NULL)) % sizeof (gpointer) != 0) |
598 | 0 | { |
599 | 0 | data = g_bytes_new (g_bytes_get_data (data, NULL), |
600 | 0 | g_bytes_get_size (data)); |
601 | 0 | unref_data = TRUE; |
602 | 0 | } |
603 | |
|
604 | 0 | table = gvdb_table_new_from_bytes (data, TRUE, &local_error); |
605 | |
|
606 | 0 | if (unref_data) |
607 | 0 | g_bytes_unref (data); |
608 | |
|
609 | 0 | if (table == NULL) |
610 | 0 | { |
611 | 0 | g_resource_error_from_gvdb_table_error (error, g_steal_pointer (&local_error)); |
612 | 0 | return NULL; |
613 | 0 | } |
614 | | |
615 | 0 | return g_resource_new_from_table (table); |
616 | 0 | } |
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 | 0 | { |
642 | 0 | GvdbTable *table; |
643 | 0 | GError *local_error = NULL; |
644 | |
|
645 | 0 | table = gvdb_table_new (filename, FALSE, &local_error); |
646 | 0 | if (table == NULL) |
647 | 0 | { |
648 | 0 | g_resource_error_from_gvdb_table_error (error, g_steal_pointer (&local_error)); |
649 | 0 | return NULL; |
650 | 0 | } |
651 | | |
652 | 0 | return g_resource_new_from_table (table); |
653 | 0 | } |
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 | 0 | { |
665 | 0 | char *free_path = NULL; |
666 | 0 | gsize path_len; |
667 | 0 | gboolean res = FALSE; |
668 | 0 | GVariant *value; |
669 | | |
670 | | /* Drop any trailing slash. */ |
671 | 0 | path_len = strlen (path); |
672 | 0 | if (path_len >= 1 && path[path_len-1] == '/') |
673 | 0 | { |
674 | 0 | path = free_path = g_strdup (path); |
675 | 0 | free_path[path_len-1] = 0; |
676 | 0 | } |
677 | |
|
678 | 0 | value = gvdb_table_get_raw_value (resource->table, path); |
679 | |
|
680 | 0 | if (value == NULL) |
681 | 0 | { |
682 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
683 | 0 | _("The resource at “%s” does not exist"), |
684 | 0 | path); |
685 | 0 | } |
686 | 0 | else |
687 | 0 | { |
688 | 0 | guint32 _size, _flags; |
689 | 0 | GVariant *array; |
690 | |
|
691 | 0 | g_variant_get (value, "(uu@ay)", |
692 | 0 | &_size, |
693 | 0 | &_flags, |
694 | 0 | &array); |
695 | |
|
696 | 0 | _size = GUINT32_FROM_LE (_size); |
697 | 0 | _flags = GUINT32_FROM_LE (_flags); |
698 | |
|
699 | 0 | if (size) |
700 | 0 | *size = _size; |
701 | 0 | if (flags) |
702 | 0 | *flags = _flags; |
703 | 0 | if (data) |
704 | 0 | *data = g_variant_get_data (array); |
705 | 0 | if (data_size) |
706 | 0 | { |
707 | | /* Don't report trailing newline that non-compressed files has */ |
708 | 0 | if (_flags & G_RESOURCE_FLAGS_COMPRESSED) |
709 | 0 | *data_size = g_variant_get_size (array); |
710 | 0 | else |
711 | 0 | *data_size = g_variant_get_size (array) - 1; |
712 | 0 | } |
713 | 0 | g_variant_unref (array); |
714 | 0 | g_variant_unref (value); |
715 | |
|
716 | 0 | res = TRUE; |
717 | 0 | } |
718 | |
|
719 | 0 | g_free (free_path); |
720 | 0 | return res; |
721 | 0 | } |
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 | 0 | { |
746 | 0 | const void *data; |
747 | 0 | gsize data_size; |
748 | 0 | guint32 flags; |
749 | 0 | GInputStream *stream, *stream2; |
750 | |
|
751 | 0 | if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error)) |
752 | 0 | return NULL; |
753 | | |
754 | 0 | stream = g_memory_input_stream_new_from_data (data, data_size, NULL); |
755 | 0 | g_object_set_data_full (G_OBJECT (stream), "g-resource", |
756 | 0 | g_resource_ref (resource), |
757 | 0 | (GDestroyNotify)g_resource_unref); |
758 | |
|
759 | 0 | if (flags & G_RESOURCE_FLAGS_COMPRESSED) |
760 | 0 | { |
761 | 0 | GZlibDecompressor *decompressor = |
762 | 0 | g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB); |
763 | |
|
764 | 0 | stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor)); |
765 | 0 | g_object_unref (decompressor); |
766 | 0 | g_object_unref (stream); |
767 | 0 | stream = stream2; |
768 | 0 | } |
769 | |
|
770 | 0 | return stream; |
771 | 0 | } |
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 | 0 | { |
806 | 0 | const void *data; |
807 | 0 | guint32 flags; |
808 | 0 | gsize data_size; |
809 | 0 | gsize size; |
810 | |
|
811 | 0 | if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error)) |
812 | 0 | return NULL; |
813 | | |
814 | 0 | if (size == 0) |
815 | 0 | return g_bytes_new_with_free_func ("", 0, (GDestroyNotify) g_resource_unref, g_resource_ref (resource)); |
816 | 0 | else if (flags & G_RESOURCE_FLAGS_COMPRESSED) |
817 | 0 | { |
818 | 0 | char *uncompressed, *d; |
819 | 0 | const char *s; |
820 | 0 | GConverterResult res; |
821 | 0 | gsize d_size, s_size; |
822 | 0 | gsize bytes_read, bytes_written; |
823 | | |
824 | |
|
825 | 0 | GZlibDecompressor *decompressor = |
826 | 0 | g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB); |
827 | |
|
828 | 0 | uncompressed = g_malloc (size + 1); |
829 | |
|
830 | 0 | s = data; |
831 | 0 | s_size = data_size; |
832 | 0 | d = uncompressed; |
833 | 0 | d_size = size; |
834 | |
|
835 | 0 | do |
836 | 0 | { |
837 | 0 | res = g_converter_convert (G_CONVERTER (decompressor), |
838 | 0 | s, s_size, |
839 | 0 | d, d_size, |
840 | 0 | G_CONVERTER_INPUT_AT_END, |
841 | 0 | &bytes_read, |
842 | 0 | &bytes_written, |
843 | 0 | NULL); |
844 | 0 | if (res == G_CONVERTER_ERROR) |
845 | 0 | { |
846 | 0 | g_free (uncompressed); |
847 | 0 | g_object_unref (decompressor); |
848 | |
|
849 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL, |
850 | 0 | _("The resource at “%s” failed to decompress"), |
851 | 0 | path); |
852 | 0 | return NULL; |
853 | |
|
854 | 0 | } |
855 | 0 | s += bytes_read; |
856 | 0 | s_size -= bytes_read; |
857 | 0 | d += bytes_written; |
858 | 0 | d_size -= bytes_written; |
859 | 0 | } |
860 | 0 | while (res != G_CONVERTER_FINISHED); |
861 | | |
862 | 0 | uncompressed[size] = 0; /* Zero terminate */ |
863 | |
|
864 | 0 | g_object_unref (decompressor); |
865 | |
|
866 | 0 | return g_bytes_new_take (uncompressed, size); |
867 | 0 | } |
868 | 0 | else |
869 | 0 | return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource)); |
870 | 0 | } |
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 | 0 | { |
900 | 0 | return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error); |
901 | 0 | } |
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 | 0 | { |
929 | 0 | gchar local_str[256]; |
930 | 0 | const gchar *path_with_slash; |
931 | 0 | gchar **children; |
932 | 0 | gchar *free_path = NULL; |
933 | 0 | 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 | 0 | if (*path == 0) |
942 | 0 | { |
943 | 0 | if (error) |
944 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
945 | 0 | _("The resource at “%s” does not exist"), |
946 | 0 | path); |
947 | 0 | return NULL; |
948 | 0 | } |
949 | | |
950 | 0 | path_len = strlen (path); |
951 | |
|
952 | 0 | if G_UNLIKELY (path[path_len-1] != '/') |
953 | 0 | { |
954 | 0 | if (path_len < sizeof (local_str) - 2) |
955 | 0 | { |
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 | 0 | memcpy (local_str, path, path_len); |
964 | 0 | local_str[path_len] = '/'; |
965 | 0 | local_str[path_len+1] = 0; |
966 | 0 | path_with_slash = local_str; |
967 | 0 | } |
968 | 0 | else |
969 | 0 | { |
970 | 0 | path_with_slash = free_path = g_strconcat (path, "/", NULL); |
971 | 0 | } |
972 | 0 | } |
973 | 0 | else |
974 | 0 | { |
975 | 0 | path_with_slash = path; |
976 | 0 | } |
977 | |
|
978 | 0 | children = gvdb_table_list (resource->table, path_with_slash); |
979 | 0 | g_free (free_path); |
980 | |
|
981 | 0 | if (children == NULL) |
982 | 0 | { |
983 | 0 | if (error) |
984 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
985 | 0 | _("The resource at “%s” does not exist"), |
986 | 0 | path); |
987 | 0 | return NULL; |
988 | 0 | } |
989 | | |
990 | 0 | return children; |
991 | 0 | } |
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 | 0 | { |
1003 | 0 | registered_resources = g_list_prepend (registered_resources, g_resource_ref (resource)); |
1004 | 0 | } |
1005 | | |
1006 | | static void |
1007 | | g_resources_unregister_unlocked (GResource *resource) |
1008 | 0 | { |
1009 | 0 | if (g_list_find (registered_resources, resource) == NULL) |
1010 | 0 | { |
1011 | 0 | g_warning ("Tried to remove not registered resource"); |
1012 | 0 | } |
1013 | 0 | else |
1014 | 0 | { |
1015 | 0 | registered_resources = g_list_remove (registered_resources, resource); |
1016 | 0 | g_resource_unref (resource); |
1017 | 0 | } |
1018 | 0 | } |
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 | 0 | { |
1033 | 0 | g_rw_lock_writer_lock (&resources_lock); |
1034 | 0 | g_resources_register_unlocked (resource); |
1035 | 0 | g_rw_lock_writer_unlock (&resources_lock); |
1036 | 0 | } |
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 | 0 | { |
1049 | 0 | g_rw_lock_writer_lock (&resources_lock); |
1050 | 0 | g_resources_unregister_unlocked (resource); |
1051 | 0 | g_rw_lock_writer_unlock (&resources_lock); |
1052 | 0 | } |
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 | 0 | { |
1076 | 0 | GInputStream *res = NULL; |
1077 | 0 | GList *l; |
1078 | 0 | GInputStream *stream; |
1079 | |
|
1080 | 0 | if (g_resource_find_overlay (path, open_overlay_stream, &res)) |
1081 | 0 | return res; |
1082 | | |
1083 | 0 | register_lazy_static_resources (); |
1084 | |
|
1085 | 0 | g_rw_lock_reader_lock (&resources_lock); |
1086 | |
|
1087 | 0 | for (l = registered_resources; l != NULL; l = l->next) |
1088 | 0 | { |
1089 | 0 | GResource *r = l->data; |
1090 | 0 | GError *my_error = NULL; |
1091 | |
|
1092 | 0 | stream = g_resource_open_stream (r, path, lookup_flags, &my_error); |
1093 | 0 | if (stream == NULL && |
1094 | 0 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
1095 | 0 | { |
1096 | 0 | g_clear_error (&my_error); |
1097 | 0 | } |
1098 | 0 | else |
1099 | 0 | { |
1100 | 0 | if (stream == NULL) |
1101 | 0 | g_propagate_error (error, my_error); |
1102 | 0 | res = stream; |
1103 | 0 | break; |
1104 | 0 | } |
1105 | 0 | } |
1106 | |
|
1107 | 0 | if (l == NULL) |
1108 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1109 | 0 | _("The resource at “%s” does not exist"), |
1110 | 0 | path); |
1111 | |
|
1112 | 0 | g_rw_lock_reader_unlock (&resources_lock); |
1113 | |
|
1114 | 0 | return res; |
1115 | 0 | } |
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 | 0 | { |
1148 | 0 | GBytes *res = NULL; |
1149 | 0 | GList *l; |
1150 | 0 | GBytes *data; |
1151 | |
|
1152 | 0 | if (g_resource_find_overlay (path, get_overlay_bytes, &res)) |
1153 | 0 | return res; |
1154 | | |
1155 | 0 | register_lazy_static_resources (); |
1156 | |
|
1157 | 0 | g_rw_lock_reader_lock (&resources_lock); |
1158 | |
|
1159 | 0 | for (l = registered_resources; l != NULL; l = l->next) |
1160 | 0 | { |
1161 | 0 | GResource *r = l->data; |
1162 | 0 | GError *my_error = NULL; |
1163 | |
|
1164 | 0 | data = g_resource_lookup_data (r, path, lookup_flags, &my_error); |
1165 | 0 | if (data == NULL && |
1166 | 0 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
1167 | 0 | { |
1168 | 0 | g_clear_error (&my_error); |
1169 | 0 | } |
1170 | 0 | else |
1171 | 0 | { |
1172 | 0 | if (data == NULL) |
1173 | 0 | g_propagate_error (error, my_error); |
1174 | 0 | res = data; |
1175 | 0 | break; |
1176 | 0 | } |
1177 | 0 | } |
1178 | |
|
1179 | 0 | if (l == NULL) |
1180 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1181 | 0 | _("The resource at “%s” does not exist"), |
1182 | 0 | path); |
1183 | |
|
1184 | 0 | g_rw_lock_reader_unlock (&resources_lock); |
1185 | |
|
1186 | 0 | return res; |
1187 | 0 | } |
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 | 0 | { |
1211 | 0 | GHashTable *hash = NULL; |
1212 | 0 | GList *l; |
1213 | 0 | char **children; |
1214 | 0 | 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 | 0 | g_resource_find_overlay (path, enumerate_overlay_dir, &hash); |
1226 | |
|
1227 | 0 | register_lazy_static_resources (); |
1228 | |
|
1229 | 0 | g_rw_lock_reader_lock (&resources_lock); |
1230 | |
|
1231 | 0 | for (l = registered_resources; l != NULL; l = l->next) |
1232 | 0 | { |
1233 | 0 | GResource *r = l->data; |
1234 | |
|
1235 | 0 | children = g_resource_enumerate_children (r, path, 0, NULL); |
1236 | |
|
1237 | 0 | if (children != NULL) |
1238 | 0 | { |
1239 | 0 | if (hash == NULL) |
1240 | | /* note: keep in sync with same line above */ |
1241 | 0 | hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
1242 | |
|
1243 | 0 | for (i = 0; children[i] != NULL; i++) |
1244 | 0 | g_hash_table_add (hash, children[i]); |
1245 | 0 | g_free (children); |
1246 | 0 | } |
1247 | 0 | } |
1248 | |
|
1249 | 0 | g_rw_lock_reader_unlock (&resources_lock); |
1250 | |
|
1251 | 0 | if (hash == NULL) |
1252 | 0 | { |
1253 | 0 | if (error) |
1254 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1255 | 0 | _("The resource at “%s” does not exist"), |
1256 | 0 | path); |
1257 | 0 | return NULL; |
1258 | 0 | } |
1259 | 0 | else |
1260 | 0 | { |
1261 | 0 | children = (gchar **) g_hash_table_get_keys_as_array (hash, NULL); |
1262 | 0 | g_hash_table_steal_all (hash); |
1263 | 0 | g_hash_table_destroy (hash); |
1264 | |
|
1265 | 0 | return children; |
1266 | 0 | } |
1267 | 0 | } |
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 | 0 | { |
1295 | 0 | gboolean res = FALSE; |
1296 | 0 | GList *l; |
1297 | 0 | gboolean r_res; |
1298 | 0 | InfoData info; |
1299 | |
|
1300 | 0 | if (g_resource_find_overlay (path, get_overlay_info, &info)) |
1301 | 0 | { |
1302 | 0 | if (size) |
1303 | 0 | *size = info.size; |
1304 | 0 | if (flags) |
1305 | 0 | *flags = info.flags; |
1306 | |
|
1307 | 0 | return TRUE; |
1308 | 0 | } |
1309 | | |
1310 | 0 | register_lazy_static_resources (); |
1311 | |
|
1312 | 0 | g_rw_lock_reader_lock (&resources_lock); |
1313 | |
|
1314 | 0 | for (l = registered_resources; l != NULL; l = l->next) |
1315 | 0 | { |
1316 | 0 | GResource *r = l->data; |
1317 | 0 | GError *my_error = NULL; |
1318 | |
|
1319 | 0 | r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error); |
1320 | 0 | if (!r_res && |
1321 | 0 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
1322 | 0 | { |
1323 | 0 | g_clear_error (&my_error); |
1324 | 0 | } |
1325 | 0 | else |
1326 | 0 | { |
1327 | 0 | if (!r_res) |
1328 | 0 | g_propagate_error (error, my_error); |
1329 | 0 | res = r_res; |
1330 | 0 | break; |
1331 | 0 | } |
1332 | 0 | } |
1333 | |
|
1334 | 0 | if (l == NULL) |
1335 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1336 | 0 | _("The resource at “%s” does not exist"), |
1337 | 0 | path); |
1338 | |
|
1339 | 0 | g_rw_lock_reader_unlock (&resources_lock); |
1340 | |
|
1341 | 0 | return res; |
1342 | 0 | } |
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 | 0 | { |
1367 | 0 | GStaticResource *list = g_atomic_pointer_get (&lazy_register_resources); |
1368 | |
|
1369 | 0 | while (!g_atomic_pointer_compare_and_exchange_full (&lazy_register_resources, list, NULL, &list)) |
1370 | 0 | ; |
1371 | |
|
1372 | 0 | while (list != NULL) |
1373 | 0 | { |
1374 | 0 | GBytes *bytes = g_bytes_new_static (list->data, list->data_len); |
1375 | 0 | GResource *resource = g_resource_new_from_data (bytes, NULL); |
1376 | 0 | if (resource) |
1377 | 0 | { |
1378 | 0 | g_resources_register_unlocked (resource); |
1379 | 0 | g_atomic_pointer_set (&list->resource, resource); |
1380 | 0 | } |
1381 | 0 | g_bytes_unref (bytes); |
1382 | |
|
1383 | 0 | list = list->next; |
1384 | 0 | } |
1385 | 0 | } |
1386 | | |
1387 | | static void |
1388 | | register_lazy_static_resources (void) |
1389 | 0 | { |
1390 | 0 | if (g_atomic_pointer_get (&lazy_register_resources) == NULL) |
1391 | 0 | return; |
1392 | | |
1393 | 0 | g_rw_lock_writer_lock (&resources_lock); |
1394 | 0 | register_lazy_static_resources_unlocked (); |
1395 | 0 | g_rw_lock_writer_unlock (&resources_lock); |
1396 | 0 | } |
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 | 0 | { |
1414 | 0 | GStaticResource *next; |
1415 | |
|
1416 | 0 | do |
1417 | 0 | { |
1418 | 0 | next = g_atomic_pointer_get (&lazy_register_resources); |
1419 | 0 | static_resource->next = next; |
1420 | 0 | } |
1421 | 0 | while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource)); |
1422 | 0 | } |
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 | 0 | { |
1439 | 0 | GResource *resource; |
1440 | |
|
1441 | 0 | g_rw_lock_writer_lock (&resources_lock); |
1442 | |
|
1443 | 0 | register_lazy_static_resources_unlocked (); |
1444 | |
|
1445 | 0 | resource = g_atomic_pointer_exchange (&static_resource->resource, NULL); |
1446 | 0 | if (resource) |
1447 | 0 | { |
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 | 0 | g_assert (g_atomic_int_get (&resource->ref_count) >= 2); |
1451 | | |
1452 | 0 | g_resources_unregister_unlocked (resource); |
1453 | 0 | g_resource_unref (resource); |
1454 | 0 | } |
1455 | | |
1456 | 0 | g_rw_lock_writer_unlock (&resources_lock); |
1457 | 0 | } |
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 | 0 | { |
1476 | 0 | register_lazy_static_resources (); |
1477 | |
|
1478 | 0 | return g_atomic_pointer_get (&static_resource->resource); |
1479 | 0 | } |