Blame gio/gresource.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright © 2011 Red Hat, Inc
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gresource.h"
Packit ae235b
#include <gvdb/gvdb-reader.h>
Packit ae235b
#include <gi18n-lib.h>
Packit ae235b
#include <gstdio.h>
Packit ae235b
#include <gio/gfile.h>
Packit ae235b
#include <gio/gioerror.h>
Packit ae235b
#include <gio/gmemoryinputstream.h>
Packit ae235b
#include <gio/gzlibdecompressor.h>
Packit ae235b
#include <gio/gconverterinputstream.h>
Packit ae235b
Packit ae235b
struct _GResource
Packit ae235b
{
Packit ae235b
  int ref_count;
Packit ae235b
Packit ae235b
  GvdbTable *table;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void register_lazy_static_resources (void);
Packit ae235b
Packit ae235b
G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gresource
Packit ae235b
 * @short_description: Resource framework
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Applications and libraries often contain binary or textual data that is
Packit ae235b
 * really part of the application, rather than user data. For instance
Packit ae235b
 * #GtkBuilder .ui files, splashscreen images, GMenu markup XML, CSS files,
Packit ae235b
 * icons, etc. These are often shipped as files in `$datadir/appname`, or
Packit ae235b
 * manually included as literal strings in the code.
Packit ae235b
 *
Packit ae235b
 * The #GResource API and the [glib-compile-resources][glib-compile-resources] program
Packit ae235b
 * provide a convenient and efficient alternative to this which has some nice properties. You
Packit ae235b
 * maintain the files as normal files, so its easy to edit them, but during the build the files
Packit ae235b
 * are combined into a binary bundle that is linked into the executable. This means that loading
Packit ae235b
 * the resource files are efficient (as they are already in memory, shared with other instances) and
Packit ae235b
 * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
Packit ae235b
 * also makes it easier to create relocatable applications.
Packit ae235b
 *
Packit ae235b
 * Resource files can also be marked as compressed. Such files will be included in the resource bundle
Packit ae235b
 * in a compressed form, but will be automatically uncompressed when the resource is used. This
Packit ae235b
 * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
Packit ae235b
 *
Packit ae235b
 * Resource files can also be marked to be preprocessed, by setting the value of the
Packit ae235b
 * `preprocess` attribute to a comma-separated list of preprocessing options.
Packit ae235b
 * The only options currently supported are:
Packit ae235b
 *
Packit ae235b
 * `xml-stripblanks` which will use the xmllint command
Packit ae235b
 * to strip ignorable whitespace from the XML file. For this to work,
Packit ae235b
 * the `XMLLINT` environment variable must be set to the full path to
Packit ae235b
 * the xmllint executable, or xmllint must be in the `PATH`; otherwise
Packit ae235b
 * the preprocessing step is skipped.
Packit ae235b
 *
Packit ae235b
 * `to-pixdata` which will use the gdk-pixbuf-pixdata command to convert
Packit ae235b
 * images to the GdkPixdata format, which allows you to create pixbufs directly using the data inside
Packit ae235b
 * the resource file, rather than an (uncompressed) copy if it. For this, the gdk-pixbuf-pixdata
Packit ae235b
 * program must be in the PATH, or the `GDK_PIXBUF_PIXDATA` environment variable must be
Packit ae235b
 * set to the full path to the gdk-pixbuf-pixdata executable; otherwise the resource compiler will
Packit ae235b
 * abort.
Packit ae235b
 *
Packit ae235b
 * Resource files will be exported in the GResource namespace using the
Packit ae235b
 * combination of the given `prefix` and the filename from the `file` element.
Packit ae235b
 * The `alias` attribute can be used to alter the filename to expose them at a
Packit ae235b
 * different location in the resource namespace. Typically, this is used to
Packit ae235b
 * include files from a different source directory without exposing the source
Packit ae235b
 * directory in the resource namespace, as in the example below.
Packit ae235b
 *
Packit ae235b
 * Resource bundles are created by the [glib-compile-resources][glib-compile-resources] program
Packit ae235b
 * which takes an XML file that describes the bundle, and a set of files that the XML references. These
Packit ae235b
 * are combined into a binary resource bundle.
Packit ae235b
 *
Packit ae235b
 * An example resource description:
Packit ae235b
 * |[
Packit ae235b
 * 
Packit ae235b
 * <gresources>
Packit ae235b
 *   <gresource prefix="/org/gtk/Example">
Packit ae235b
 *     <file>data/splashscreen.png</file>
Packit ae235b
 *     <file compressed="true">dialog.ui</file>
Packit ae235b
 *     <file preprocess="xml-stripblanks">menumarkup.xml</file>
Packit ae235b
 *     <file alias="example.css">data/example.css</file>
Packit ae235b
 *   </gresource>
Packit ae235b
 * </gresources>
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * This will create a resource bundle with the following files:
Packit ae235b
 * |[
Packit ae235b
 * /org/gtk/Example/data/splashscreen.png
Packit ae235b
 * /org/gtk/Example/dialog.ui
Packit ae235b
 * /org/gtk/Example/menumarkup.xml
Packit ae235b
 * /org/gtk/Example/example.css
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Note that all resources in the process share the same namespace, so use Java-style
Packit ae235b
 * path prefixes (like in the above example) to avoid conflicts.
Packit ae235b
 *
Packit ae235b
 * You can then use [glib-compile-resources][glib-compile-resources] to compile the XML to a
Packit ae235b
 * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
Packit ae235b
 * --generate-header arguments to create a source file and header to link directly into your application.
Packit ae235b
 * This will generate `get_resource()`, `register_resource()` and
Packit ae235b
 * `unregister_resource()` functions, prefixed by the `--c-name` argument passed
Packit ae235b
 * to [glib-compile-resources][glib-compile-resources]. `get_resource()` returns
Packit ae235b
 * the generated #GResource object. The register and unregister functions
Packit ae235b
 * register the resource so its files can be accessed using
Packit ae235b
 * g_resources_lookup_data().
Packit ae235b
 *
Packit ae235b
 * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
Packit ae235b
 * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
Packit ae235b
 * to the data. You can also use URIs like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
Packit ae235b
 * the resource data.
Packit ae235b
 *
Packit ae235b
 * Some higher-level APIs, such as #GtkApplication, will automatically load
Packit ae235b
 * resources from certain well-known paths in the resource namespace as a
Packit ae235b
 * convenience. See the documentation for those APIs for details.
Packit ae235b
 *
Packit ae235b
 * There are two forms of the generated source, the default version uses the compiler support for constructor
Packit ae235b
 * and destructor functions (where available) to automatically create and register the #GResource on startup
Packit ae235b
 * or library load time. If you pass `--manual-register`, two functions to register/unregister the resource are created
Packit ae235b
 * instead. This requires an explicit initialization call in your application/library, but it works on all platforms,
Packit ae235b
 * even on the minor ones where constructors are not supported. (Constructor support is available for at least Win32, Mac OS and Linux.)
Packit ae235b
 *
Packit ae235b
 * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
Packit ae235b
 * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
Packit ae235b
 * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
Packit ae235b
 * are for your own resources, and resource data is often used once, during parsing, and then released.
Packit ae235b
 *
Packit ae235b
 * When debugging a program or testing a change to an installed version, it is often useful to be able to
Packit ae235b
 * replace resources in the program or library, without recompiling, for debugging or quick hacking and testing
Packit ae235b
 * purposes. Since GLib 2.50, it is possible to use the `G_RESOURCE_OVERLAYS` environment variable to selectively overlay
Packit ae235b
 * resources with replacements from the filesystem.  It is a colon-separated list of substitutions to perform
Packit ae235b
 * during resource lookups.
Packit ae235b
 *
Packit ae235b
 * A substitution has the form
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *    /org/gtk/libgtk=/home/desrt/gtk-overlay
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * The part before the `=` is the resource subpath for which the overlay applies.  The part after is a
Packit ae235b
 * filesystem path which contains files and subdirectories as you would like to be loaded as resources with the
Packit ae235b
 * equivalent names.
Packit ae235b
 *
Packit ae235b
 * In the example above, if an application tried to load a resource with the resource path
Packit ae235b
 * `/org/gtk/libgtk/ui/gtkdialog.ui` then GResource would check the filesystem path
Packit ae235b
 * `/home/desrt/gtk-overlay/ui/gtkdialog.ui`.  If a file was found there, it would be used instead.  This is an
Packit ae235b
 * overlay, not an outright replacement, which means that if a file is not found at that path, the built-in
Packit ae235b
 * version will be used instead.  Whiteouts are not currently supported.
Packit ae235b
 *
Packit ae235b
 * Substitutions must start with a slash, and must not contain a trailing slash before the '='.  The path after
Packit ae235b
 * the slash should ideally be absolute, but this is not strictly required.  It is possible to overlay the
Packit ae235b
 * location of a single resource with an individual file.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GStaticResource:
Packit ae235b
 *
Packit ae235b
 * #GStaticResource is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 **/
Packit ae235b
typedef gboolean (* CheckCandidate) (const gchar *candidate, gpointer user_data);
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
open_overlay_stream (const gchar *candidate,
Packit ae235b
                     gpointer     user_data)
Packit ae235b
{
Packit ae235b
  GInputStream **res = (GInputStream **) user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *file;
Packit ae235b
Packit ae235b
  file = g_file_new_for_path (candidate);
Packit ae235b
  *res = (GInputStream *) g_file_read (file, NULL, &error);
Packit ae235b
Packit ae235b
  if (*res)
Packit ae235b
    {
Packit ae235b
      g_message ("Opened file '%s' as a resource overlay", candidate);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
Packit ae235b
        g_warning ("Can't open overlay file '%s': %s", candidate, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  return *res != NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_overlay_bytes (const gchar *candidate,
Packit ae235b
                   gpointer     user_data)
Packit ae235b
{
Packit ae235b
  GBytes **res = (GBytes **) user_data;
Packit ae235b
  GMappedFile *mapped_file;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  mapped_file = g_mapped_file_new (candidate, FALSE, &error);
Packit ae235b
Packit ae235b
  if (mapped_file)
Packit ae235b
    {
Packit ae235b
      g_message ("Mapped file '%s' as a resource overlay", candidate);
Packit ae235b
      *res = g_mapped_file_get_bytes (mapped_file);
Packit ae235b
      g_mapped_file_unref (mapped_file);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
Packit ae235b
        g_warning ("Can't mmap overlay file '%s': %s", candidate, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return *res != NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
enumerate_overlay_dir (const gchar *candidate,
Packit ae235b
                       gpointer     user_data)
Packit ae235b
{
Packit ae235b
  GHashTable **hash = (GHashTable **) user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GDir *dir;
Packit ae235b
  const gchar *name;
Packit ae235b
Packit ae235b
  dir = g_dir_open (candidate, 0, &error);
Packit ae235b
  if (dir)
Packit ae235b
    {
Packit ae235b
      if (*hash == NULL)
Packit ae235b
        /* note: keep in sync with same line below */
Packit ae235b
        *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
Packit ae235b
Packit ae235b
      g_message ("Enumerating directory '%s' as resource overlay", candidate);
Packit ae235b
Packit ae235b
      while ((name = g_dir_read_name (dir)))
Packit ae235b
        {
Packit ae235b
          gchar *fullname = g_build_filename (candidate, name, NULL);
Packit ae235b
Packit ae235b
          /* match gvdb behaviour by suffixing "/" on dirs */
Packit ae235b
          if (g_file_test (fullname, G_FILE_TEST_IS_DIR))
Packit ae235b
            g_hash_table_add (*hash, g_strconcat (name, "/", NULL));
Packit ae235b
          else
Packit ae235b
            g_hash_table_add (*hash, g_strdup (name));
Packit ae235b
Packit ae235b
          g_free (fullname);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_dir_close (dir);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
Packit ae235b
        g_warning ("Can't enumerate overlay directory '%s': %s", candidate, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* We may want to enumerate results from more than one overlay
Packit ae235b
   * directory.
Packit ae235b
   */
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_resource_find_overlay (const gchar    *path,
Packit ae235b
                         CheckCandidate  check,
Packit ae235b
                         gpointer        user_data)
Packit ae235b
{
Packit ae235b
  /* This is a null-terminated array of replacement strings (with '=' inside) */
Packit ae235b
  static const gchar * const *overlay_dirs;
Packit ae235b
  gboolean res = FALSE;
Packit ae235b
  gint path_len = -1;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  /* We try to be very fast in case there are no overlays.  Otherwise,
Packit ae235b
   * we can take a bit more time...
Packit ae235b
   */
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&overlay_dirs))
Packit ae235b
    {
Packit ae235b
      const gchar * const *result;
Packit ae235b
      const gchar *envvar;
Packit ae235b
Packit ae235b
      envvar = g_getenv ("G_RESOURCE_OVERLAYS");
Packit ae235b
      if (envvar != NULL)
Packit ae235b
        {
Packit ae235b
          gchar **parts;
Packit ae235b
          gint i, j;
Packit ae235b
Packit ae235b
          parts = g_strsplit (envvar, ":", 0);
Packit ae235b
Packit ae235b
          /* Sanity check the parts, dropping those that are invalid.
Packit ae235b
           * 'i' may grow faster than 'j'.
Packit ae235b
           */
Packit ae235b
          for (i = j = 0; parts[i]; i++)
Packit ae235b
            {
Packit ae235b
              gchar *part = parts[i];
Packit ae235b
              gchar *eq;
Packit ae235b
Packit ae235b
              eq = strchr (part, '=');
Packit ae235b
              if (eq == NULL)
Packit ae235b
                {
Packit ae235b
                  g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks '='.  Ignoring.", part);
Packit ae235b
                  g_free (part);
Packit ae235b
                  continue;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              if (eq == part)
Packit ae235b
                {
Packit ae235b
                  g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path before '='.  Ignoring.", part);
Packit ae235b
                  g_free (part);
Packit ae235b
                  continue;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              if (eq[1] == '\0')
Packit ae235b
                {
Packit ae235b
                  g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path after '='.  Ignoring", part);
Packit ae235b
                  g_free (part);
Packit ae235b
                  continue;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              if (part[0] != '/')
Packit ae235b
                {
Packit ae235b
                  g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks leading '/'.  Ignoring.", part);
Packit ae235b
                  g_free (part);
Packit ae235b
                  continue;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              if (eq[-1] == '/')
Packit ae235b
                {
Packit ae235b
                  g_critical ("G_RESOURCE_OVERLAYS segment '%s' has trailing '/' before '='.  Ignoring", part);
Packit ae235b
                  g_free (part);
Packit ae235b
                  continue;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              if (eq[1] != '/')
Packit ae235b
                {
Packit ae235b
                  g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks leading '/' after '='.  Ignoring", part);
Packit ae235b
                  g_free (part);
Packit ae235b
                  continue;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              g_message ("Adding GResources overlay '%s'", part);
Packit ae235b
              parts[j++] = part;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          parts[j] = NULL;
Packit ae235b
Packit ae235b
          result = (const gchar **) parts;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* We go out of the way to avoid malloc() in the normal case
Packit ae235b
           * where the environment variable is not set.
Packit ae235b
           */
Packit ae235b
          static const gchar * const empty_strv[0 + 1];
Packit ae235b
          result = empty_strv;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_once_init_leave (&overlay_dirs, result);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 0; overlay_dirs[i]; i++)
Packit ae235b
    {
Packit ae235b
      const gchar *src;
Packit ae235b
      gint src_len;
Packit ae235b
      const gchar *dst;
Packit ae235b
      gint dst_len;
Packit ae235b
      gchar *candidate;
Packit ae235b
Packit ae235b
      {
Packit ae235b
        gchar *eq;
Packit ae235b
Packit ae235b
        /* split the overlay into src/dst */
Packit ae235b
        src = overlay_dirs[i];
Packit ae235b
        eq = strchr (src, '=');
Packit ae235b
        g_assert (eq); /* we checked this already */
Packit ae235b
        src_len = eq - src;
Packit ae235b
        dst = eq + 1;
Packit ae235b
        /* hold off on dst_len because we will probably fail the checks below */
Packit ae235b
      }
Packit ae235b
Packit ae235b
      if (path_len == -1)
Packit ae235b
        path_len = strlen (path);
Packit ae235b
Packit ae235b
      /* The entire path is too short to match the source */
Packit ae235b
      if (path_len < src_len)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      /* It doesn't match the source */
Packit ae235b
      if (memcmp (path, src, src_len) != 0)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      /* The prefix matches, but it's not a complete path component */
Packit ae235b
      if (path[src_len] && path[src_len] != '/')
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      /* OK.  Now we need this. */
Packit ae235b
      dst_len = strlen (dst);
Packit ae235b
Packit ae235b
      /* The candidate will be composed of:
Packit ae235b
       *
Packit ae235b
       *    dst + remaining_path + nul
Packit ae235b
       */
Packit ae235b
      candidate = g_malloc (dst_len + (path_len - src_len) + 1);
Packit ae235b
      memcpy (candidate, dst, dst_len);
Packit ae235b
      memcpy (candidate + dst_len, path + src_len, path_len - src_len);
Packit ae235b
      candidate[dst_len + (path_len - src_len)] = '\0';
Packit ae235b
Packit ae235b
      /* No matter what, 'r' is what we need, including the case where
Packit ae235b
       * we are trying to enumerate a directory.
Packit ae235b
       */
Packit ae235b
      res = (* check) (candidate, user_data);
Packit ae235b
      g_free (candidate);
Packit ae235b
Packit ae235b
      if (res)
Packit ae235b
        break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_error_quark:
Packit ae235b
 *
Packit ae235b
 * Gets the #GResource Error Quark.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GQuark
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
G_DEFINE_QUARK (g-resource-error-quark, g_resource_error)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_ref:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 *
Packit ae235b
 * Atomically increments the reference count of @resource by one. This
Packit ae235b
 * function is MT-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Returns: The passed in #GResource
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GResource *
Packit ae235b
g_resource_ref (GResource *resource)
Packit ae235b
{
Packit ae235b
  g_atomic_int_inc (&resource->ref_count);
Packit ae235b
  return resource;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_unref:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 *
Packit ae235b
 * Atomically decrements the reference count of @resource by one. If the
Packit ae235b
 * reference count drops to 0, all memory allocated by the resource is
Packit ae235b
 * released. This function is MT-safe and may be called from any
Packit ae235b
 * thread.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_resource_unref (GResource *resource)
Packit ae235b
{
Packit ae235b
  if (g_atomic_int_dec_and_test (&resource->ref_count))
Packit ae235b
    {
Packit ae235b
      gvdb_table_unref (resource->table);
Packit ae235b
      g_free (resource);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< internal >
Packit ae235b
 * g_resource_new_from_table:
Packit ae235b
 * @table: (transfer full): a GvdbTable
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new #GResource for @table
Packit ae235b
 */
Packit ae235b
static GResource *
Packit ae235b
g_resource_new_from_table (GvdbTable *table)
Packit ae235b
{
Packit ae235b
  GResource *resource;
Packit ae235b
Packit ae235b
  resource = g_new (GResource, 1);
Packit ae235b
  resource->ref_count = 1;
Packit ae235b
  resource->table = table;
Packit ae235b
Packit ae235b
  return resource;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_new_from_data:
Packit ae235b
 * @data: A #GBytes
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Creates a GResource from a reference to the binary resource bundle.
Packit ae235b
 * This will keep a reference to @data while the resource lives, so
Packit ae235b
 * the data should not be modified or freed.
Packit ae235b
 *
Packit ae235b
 * If you want to use this resource in the global resource namespace you need
Packit ae235b
 * to register it with g_resources_register().
Packit ae235b
 *
Packit ae235b
 * Note: @data must be backed by memory that is at least pointer aligned.
Packit ae235b
 * Otherwise this function will internally create a copy of the memory since
Packit ae235b
 * GLib 2.56, or in older versions fail and exit the process.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new #GResource, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GResource *
Packit ae235b
g_resource_new_from_data (GBytes  *data,
Packit ae235b
                          GError **error)
Packit ae235b
{
Packit ae235b
  GvdbTable *table;
Packit ae235b
  gboolean unref_data = FALSE;
Packit ae235b
Packit ae235b
  if (((guintptr) g_bytes_get_data (data, NULL)) % sizeof (gpointer) != 0)
Packit ae235b
    {
Packit ae235b
      data = g_bytes_new (g_bytes_get_data (data, NULL),
Packit ae235b
                          g_bytes_get_size (data));
Packit ae235b
      unref_data = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  table = gvdb_table_new_from_data (g_bytes_get_data (data, NULL),
Packit ae235b
                                    g_bytes_get_size (data),
Packit ae235b
                                    TRUE,
Packit ae235b
                                    g_bytes_ref (data),
Packit ae235b
                                    (GvdbRefFunc)g_bytes_ref,
Packit ae235b
                                    (GDestroyNotify)g_bytes_unref,
Packit ae235b
                                    error);
Packit ae235b
Packit ae235b
  if (unref_data)
Packit ae235b
    g_bytes_unref (data);
Packit ae235b
Packit ae235b
  if (table == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return g_resource_new_from_table (table);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_load:
Packit ae235b
 * @filename: (type filename): the path of a filename to load, in the GLib filename encoding
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Loads a binary resource bundle and creates a #GResource representation of it, allowing
Packit ae235b
 * you to query it for data.
Packit ae235b
 *
Packit ae235b
 * If you want to use this resource in the global resource namespace you need
Packit ae235b
 * to register it with g_resources_register().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new #GResource, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GResource *
Packit ae235b
g_resource_load (const gchar  *filename,
Packit ae235b
                 GError      **error)
Packit ae235b
{
Packit ae235b
  GvdbTable *table;
Packit ae235b
Packit ae235b
  table = gvdb_table_new (filename, FALSE, error);
Packit ae235b
  if (table == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return g_resource_new_from_table (table);
Packit ae235b
}
Packit ae235b
Packit ae235b
static
Packit ae235b
gboolean do_lookup (GResource             *resource,
Packit ae235b
                    const gchar           *path,
Packit ae235b
                    GResourceLookupFlags   lookup_flags,
Packit ae235b
                    gsize                 *size,
Packit ae235b
                    guint32               *flags,
Packit ae235b
                    const void           **data,
Packit ae235b
                    gsize                 *data_size,
Packit ae235b
                    GError               **error)
Packit ae235b
{
Packit ae235b
  char *free_path = NULL;
Packit ae235b
  gsize path_len;
Packit ae235b
  gboolean res = FALSE;
Packit ae235b
  GVariant *value;
Packit ae235b
Packit ae235b
  path_len = strlen (path);
Packit ae235b
  if (path[path_len-1] == '/')
Packit ae235b
    {
Packit ae235b
      path = free_path = g_strdup (path);
Packit ae235b
      free_path[path_len-1] = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  value = gvdb_table_get_raw_value (resource->table, path);
Packit ae235b
Packit ae235b
  if (value == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
Packit ae235b
                   _("The resource at “%s” does not exist"),
Packit ae235b
                   path);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      guint32 _size, _flags;
Packit ae235b
      GVariant *array;
Packit ae235b
Packit ae235b
      g_variant_get (value, "(uu@ay)",
Packit ae235b
                     &_size,
Packit ae235b
                     &_flags,
Packit ae235b
                     &array);
Packit ae235b
Packit ae235b
      _size = GUINT32_FROM_LE (_size);
Packit ae235b
      _flags = GUINT32_FROM_LE (_flags);
Packit ae235b
Packit ae235b
      if (size)
Packit ae235b
        *size = _size;
Packit ae235b
      if (flags)
Packit ae235b
        *flags = _flags;
Packit ae235b
      if (data)
Packit ae235b
        *data = g_variant_get_data (array);
Packit ae235b
      if (data_size)
Packit ae235b
        {
Packit ae235b
          /* Don't report trailing newline that non-compressed files has */
Packit ae235b
          if (_flags & G_RESOURCE_FLAGS_COMPRESSED)
Packit ae235b
            *data_size = g_variant_get_size (array);
Packit ae235b
          else
Packit ae235b
            *data_size = g_variant_get_size (array) - 1;
Packit ae235b
        }
Packit ae235b
      g_variant_unref (array);
Packit ae235b
      g_variant_unref (value);
Packit ae235b
Packit ae235b
      res = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (free_path);
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_open_stream:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Looks for a file at the specified @path in the resource and
Packit ae235b
 * returns a #GInputStream that lets you read the data.
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): #GInputStream or %NULL on error.
Packit ae235b
 *     Free the returned object with g_object_unref()
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GInputStream *
Packit ae235b
g_resource_open_stream (GResource             *resource,
Packit ae235b
                        const gchar           *path,
Packit ae235b
                        GResourceLookupFlags   lookup_flags,
Packit ae235b
                        GError               **error)
Packit ae235b
{
Packit ae235b
  const void *data;
Packit ae235b
  gsize data_size;
Packit ae235b
  guint32 flags;
Packit ae235b
  GInputStream *stream, *stream2;
Packit ae235b
Packit ae235b
  if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
Packit ae235b
  g_object_set_data_full (G_OBJECT (stream), "g-resource",
Packit ae235b
                          g_resource_ref (resource),
Packit ae235b
                          (GDestroyNotify)g_resource_unref);
Packit ae235b
Packit ae235b
  if (flags & G_RESOURCE_FLAGS_COMPRESSED)
Packit ae235b
    {
Packit ae235b
      GZlibDecompressor *decompressor =
Packit ae235b
        g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
Packit ae235b
Packit ae235b
      stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor));
Packit ae235b
      g_object_unref (decompressor);
Packit ae235b
      g_object_unref (stream);
Packit ae235b
      stream = stream2;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return stream;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_lookup_data:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Looks for a file at the specified @path in the resource and
Packit ae235b
 * returns a #GBytes that lets you directly access the data in
Packit ae235b
 * memory.
Packit ae235b
 *
Packit ae235b
 * The data is always followed by a zero byte, so you
Packit ae235b
 * can safely use the data as a C string. However, that byte
Packit ae235b
 * is not included in the size of the GBytes.
Packit ae235b
 *
Packit ae235b
 * For uncompressed resource files this is a pointer directly into
Packit ae235b
 * the resource bundle, which is typically in some readonly data section
Packit ae235b
 * in the program binary. For compressed files we allocate memory on
Packit ae235b
 * the heap and automatically uncompress the data.
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): #GBytes or %NULL on error.
Packit ae235b
 *     Free the returned object with g_bytes_unref()
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GBytes *
Packit ae235b
g_resource_lookup_data (GResource             *resource,
Packit ae235b
                        const gchar           *path,
Packit ae235b
                        GResourceLookupFlags   lookup_flags,
Packit ae235b
                        GError               **error)
Packit ae235b
{
Packit ae235b
  const void *data;
Packit ae235b
  guint32 flags;
Packit ae235b
  gsize data_size;
Packit ae235b
  gsize size;
Packit ae235b
Packit ae235b
  if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (flags & G_RESOURCE_FLAGS_COMPRESSED)
Packit ae235b
    {
Packit ae235b
      char *uncompressed, *d;
Packit ae235b
      const char *s;
Packit ae235b
      GConverterResult res;
Packit ae235b
      gsize d_size, s_size;
Packit ae235b
      gsize bytes_read, bytes_written;
Packit ae235b
Packit ae235b
Packit ae235b
      GZlibDecompressor *decompressor =
Packit ae235b
        g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
Packit ae235b
Packit ae235b
      uncompressed = g_malloc (size + 1);
Packit ae235b
Packit ae235b
      s = data;
Packit ae235b
      s_size = data_size;
Packit ae235b
      d = uncompressed;
Packit ae235b
      d_size = size;
Packit ae235b
Packit ae235b
      do
Packit ae235b
        {
Packit ae235b
          res = g_converter_convert (G_CONVERTER (decompressor),
Packit ae235b
                                     s, s_size,
Packit ae235b
                                     d, d_size,
Packit ae235b
                                     G_CONVERTER_INPUT_AT_END,
Packit ae235b
                                     &bytes_read,
Packit ae235b
                                     &bytes_written,
Packit ae235b
                                     NULL);
Packit ae235b
          if (res == G_CONVERTER_ERROR)
Packit ae235b
            {
Packit ae235b
              g_free (uncompressed);
Packit ae235b
              g_object_unref (decompressor);
Packit ae235b
Packit ae235b
              g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL,
Packit ae235b
                           _("The resource at “%s” failed to decompress"),
Packit ae235b
                           path);
Packit ae235b
              return NULL;
Packit ae235b
Packit ae235b
            }
Packit ae235b
          s += bytes_read;
Packit ae235b
          s_size -= bytes_read;
Packit ae235b
          d += bytes_written;
Packit ae235b
          d_size -= bytes_written;
Packit ae235b
        }
Packit ae235b
      while (res != G_CONVERTER_FINISHED);
Packit ae235b
Packit ae235b
      uncompressed[size] = 0; /* Zero terminate */
Packit ae235b
Packit ae235b
      g_object_unref (decompressor);
Packit ae235b
Packit ae235b
      return g_bytes_new_take (uncompressed, size);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_get_info:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @size:  (out) (optional): a location to place the length of the contents of the file,
Packit ae235b
 *    or %NULL if the length is not needed
Packit ae235b
 * @flags:  (out) (optional): a location to place the flags about the file,
Packit ae235b
 *    or %NULL if the length is not needed
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Looks for a file at the specified @path in the resource and
Packit ae235b
 * if found returns information about it.
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the file was found. %FALSE if there were errors
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_resource_get_info (GResource             *resource,
Packit ae235b
                     const gchar           *path,
Packit ae235b
                     GResourceLookupFlags   lookup_flags,
Packit ae235b
                     gsize                 *size,
Packit ae235b
                     guint32               *flags,
Packit ae235b
                     GError               **error)
Packit ae235b
{
Packit ae235b
  return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resource_enumerate_children:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Returns all the names of children at the specified @path in the resource.
Packit ae235b
 * The return result is a %NULL terminated list of strings which should
Packit ae235b
 * be released with g_strfreev().
Packit ae235b
 *
Packit ae235b
 * If @path is invalid or does not exist in the #GResource,
Packit ae235b
 * %G_RESOURCE_ERROR_NOT_FOUND will be returned.
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
g_resource_enumerate_children (GResource             *resource,
Packit ae235b
                               const gchar           *path,
Packit ae235b
                               GResourceLookupFlags   lookup_flags,
Packit ae235b
                               GError               **error)
Packit ae235b
{
Packit ae235b
  gchar local_str[256];
Packit ae235b
  const gchar *path_with_slash;
Packit ae235b
  gchar **children;
Packit ae235b
  gchar *free_path = NULL;
Packit ae235b
  gsize path_len;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Size of 256 is arbitrarily chosen based on being large enough
Packit ae235b
   * for pretty much everything we come across, but not cumbersome
Packit ae235b
   * on the stack. It also matches common cacheline sizes.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  if (*path == 0)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
Packit ae235b
                   _("The resource at “%s” does not exist"),
Packit ae235b
                   path);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  path_len = strlen (path);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (path[path_len-1] != '/')
Packit ae235b
    {
Packit ae235b
      if (path_len < sizeof (local_str) - 2)
Packit ae235b
        {
Packit ae235b
          /*
Packit ae235b
           * We got a path that does not have a trailing /. It is not the
Packit ae235b
           * ideal use of this API as we require trailing / for our lookup
Packit ae235b
           * into gvdb. Some degenerate application configurations can hit
Packit ae235b
           * this code path quite a bit, so we try to avoid using the
Packit ae235b
           * g_strconcat()/g_free().
Packit ae235b
           */
Packit ae235b
          memcpy (local_str, path, path_len);
Packit ae235b
          local_str[path_len] = '/';
Packit ae235b
          local_str[path_len+1] = 0;
Packit ae235b
          path_with_slash = local_str;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          path_with_slash = free_path = g_strconcat (path, "/", NULL);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      path_with_slash = path;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  children = gvdb_table_list (resource->table, path_with_slash);
Packit ae235b
  g_free (free_path);
Packit ae235b
Packit ae235b
  if (children == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
Packit ae235b
                   _("The resource at “%s” does not exist"),
Packit ae235b
                   path);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return children;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GRWLock resources_lock;
Packit ae235b
static GList *registered_resources;
Packit ae235b
Packit ae235b
/* This is updated atomically, so we can append to it and check for NULL outside the
Packit ae235b
   lock, but all other accesses are done under the write lock */
Packit ae235b
static GStaticResource *lazy_register_resources;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_resources_register_unlocked (GResource *resource)
Packit ae235b
{
Packit ae235b
  registered_resources = g_list_prepend (registered_resources, g_resource_ref (resource));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_resources_unregister_unlocked (GResource *resource)
Packit ae235b
{
Packit ae235b
  if (g_list_find (registered_resources, resource) == NULL)
Packit ae235b
    {
Packit ae235b
      g_warning ("Tried to remove not registered resource");
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      registered_resources = g_list_remove (registered_resources, resource);
Packit ae235b
      g_resource_unref (resource);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resources_register:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 *
Packit ae235b
 * Registers the resource with the process-global set of resources.
Packit ae235b
 * Once a resource is registered the files in it can be accessed
Packit ae235b
 * with the global resource lookup functions like g_resources_lookup_data().
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_resources_register (GResource *resource)
Packit ae235b
{
Packit ae235b
  g_rw_lock_writer_lock (&resources_lock);
Packit ae235b
  g_resources_register_unlocked (resource);
Packit ae235b
  g_rw_lock_writer_unlock (&resources_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resources_unregister:
Packit ae235b
 * @resource: A #GResource
Packit ae235b
 *
Packit ae235b
 * Unregisters the resource from the process-global set of resources.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_resources_unregister (GResource *resource)
Packit ae235b
{
Packit ae235b
  g_rw_lock_writer_lock (&resources_lock);
Packit ae235b
  g_resources_unregister_unlocked (resource);
Packit ae235b
  g_rw_lock_writer_unlock (&resources_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resources_open_stream:
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Looks for a file at the specified @path in the set of
Packit ae235b
 * globally registered resources and returns a #GInputStream
Packit ae235b
 * that lets you read the data.
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): #GInputStream or %NULL on error.
Packit ae235b
 *     Free the returned object with g_object_unref()
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GInputStream *
Packit ae235b
g_resources_open_stream (const gchar           *path,
Packit ae235b
                         GResourceLookupFlags   lookup_flags,
Packit ae235b
                         GError               **error)
Packit ae235b
{
Packit ae235b
  GInputStream *res = NULL;
Packit ae235b
  GList *l;
Packit ae235b
  GInputStream *stream;
Packit ae235b
Packit ae235b
  if (g_resource_find_overlay (path, open_overlay_stream, &res))
Packit ae235b
    return res;
Packit ae235b
Packit ae235b
  register_lazy_static_resources ();
Packit ae235b
Packit ae235b
  g_rw_lock_reader_lock (&resources_lock);
Packit ae235b
Packit ae235b
  for (l = registered_resources; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      GResource *r = l->data;
Packit ae235b
      GError *my_error = NULL;
Packit ae235b
Packit ae235b
      stream = g_resource_open_stream (r, path, lookup_flags, &my_error);
Packit ae235b
      if (stream == NULL &&
Packit ae235b
          g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
Packit ae235b
        {
Packit ae235b
          g_clear_error (&my_error);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (stream == NULL)
Packit ae235b
            g_propagate_error (error, my_error);
Packit ae235b
          res = stream;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (l == NULL)
Packit ae235b
    g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
Packit ae235b
                 _("The resource at “%s” does not exist"),
Packit ae235b
                 path);
Packit ae235b
Packit ae235b
  g_rw_lock_reader_unlock (&resources_lock);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resources_lookup_data:
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Looks for a file at the specified @path in the set of
Packit ae235b
 * globally registered resources and returns a #GBytes that
Packit ae235b
 * lets you directly access the data in memory.
Packit ae235b
 *
Packit ae235b
 * The data is always followed by a zero byte, so you
Packit ae235b
 * can safely use the data as a C string. However, that byte
Packit ae235b
 * is not included in the size of the GBytes.
Packit ae235b
 *
Packit ae235b
 * For uncompressed resource files this is a pointer directly into
Packit ae235b
 * the resource bundle, which is typically in some readonly data section
Packit ae235b
 * in the program binary. For compressed files we allocate memory on
Packit ae235b
 * the heap and automatically uncompress the data.
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): #GBytes or %NULL on error.
Packit ae235b
 *     Free the returned object with g_bytes_unref()
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GBytes *
Packit ae235b
g_resources_lookup_data (const gchar           *path,
Packit ae235b
                         GResourceLookupFlags   lookup_flags,
Packit ae235b
                         GError               **error)
Packit ae235b
{
Packit ae235b
  GBytes *res = NULL;
Packit ae235b
  GList *l;
Packit ae235b
  GBytes *data;
Packit ae235b
Packit ae235b
  if (g_resource_find_overlay (path, get_overlay_bytes, &res))
Packit ae235b
    return res;
Packit ae235b
Packit ae235b
  register_lazy_static_resources ();
Packit ae235b
Packit ae235b
  g_rw_lock_reader_lock (&resources_lock);
Packit ae235b
Packit ae235b
  for (l = registered_resources; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      GResource *r = l->data;
Packit ae235b
      GError *my_error = NULL;
Packit ae235b
Packit ae235b
      data = g_resource_lookup_data (r, path, lookup_flags, &my_error);
Packit ae235b
      if (data == NULL &&
Packit ae235b
          g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
Packit ae235b
        {
Packit ae235b
          g_clear_error (&my_error);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (data == NULL)
Packit ae235b
            g_propagate_error (error, my_error);
Packit ae235b
          res = data;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (l == NULL)
Packit ae235b
    g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
Packit ae235b
                 _("The resource at “%s” does not exist"),
Packit ae235b
                 path);
Packit ae235b
Packit ae235b
  g_rw_lock_reader_unlock (&resources_lock);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resources_enumerate_children:
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Returns all the names of children at the specified @path in the set of
Packit ae235b
 * globally registered resources.
Packit ae235b
 * The return result is a %NULL terminated list of strings which should
Packit ae235b
 * be released with g_strfreev().
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
g_resources_enumerate_children (const gchar           *path,
Packit ae235b
                                GResourceLookupFlags   lookup_flags,
Packit ae235b
                                GError               **error)
Packit ae235b
{
Packit ae235b
  GHashTable *hash = NULL;
Packit ae235b
  GList *l;
Packit ae235b
  char **children;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  /* This will enumerate actual files found in overlay directories but
Packit ae235b
   * will not enumerate the overlays themselves.  For example, if we
Packit ae235b
   * have an overlay "/org/gtk=/path/to/files" and we enumerate "/org"
Packit ae235b
   * then we will not see "gtk" in the result set unless it is provided
Packit ae235b
   * by another resource file.
Packit ae235b
   *
Packit ae235b
   * This is probably not going to be a problem since if we are doing
Packit ae235b
   * such an overlay, we probably will already have that path.
Packit ae235b
   */
Packit ae235b
  g_resource_find_overlay (path, enumerate_overlay_dir, &hash);
Packit ae235b
Packit ae235b
  register_lazy_static_resources ();
Packit ae235b
Packit ae235b
  g_rw_lock_reader_lock (&resources_lock);
Packit ae235b
Packit ae235b
  for (l = registered_resources; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      GResource *r = l->data;
Packit ae235b
Packit ae235b
      children = g_resource_enumerate_children (r, path, 0, NULL);
Packit ae235b
Packit ae235b
      if (children != NULL)
Packit ae235b
        {
Packit ae235b
          if (hash == NULL)
Packit ae235b
            /* note: keep in sync with same line above */
Packit ae235b
            hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
Packit ae235b
Packit ae235b
          for (i = 0; children[i] != NULL; i++)
Packit ae235b
            g_hash_table_add (hash, children[i]);
Packit ae235b
          g_free (children);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_rw_lock_reader_unlock (&resources_lock);
Packit ae235b
Packit ae235b
  if (hash == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
Packit ae235b
                   _("The resource at “%s” does not exist"),
Packit ae235b
                   path);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      children = (gchar **) g_hash_table_get_keys_as_array (hash, NULL);
Packit ae235b
      g_hash_table_steal_all (hash);
Packit ae235b
      g_hash_table_destroy (hash);
Packit ae235b
Packit ae235b
      return children;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_resources_get_info:
Packit ae235b
 * @path: A pathname inside the resource
Packit ae235b
 * @lookup_flags: A #GResourceLookupFlags
Packit ae235b
 * @size:  (out) (optional): a location to place the length of the contents of the file,
Packit ae235b
 *    or %NULL if the length is not needed
Packit ae235b
 * @flags:  (out) (optional): a location to place the #GResourceFlags about the file,
Packit ae235b
 *    or %NULL if the flags are not needed
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Looks for a file at the specified @path in the set of
Packit ae235b
 * globally registered resources and if found returns information about it.
Packit ae235b
 *
Packit ae235b
 * @lookup_flags controls the behaviour of the lookup.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the file was found. %FALSE if there were errors
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_resources_get_info (const gchar           *path,
Packit ae235b
                      GResourceLookupFlags   lookup_flags,
Packit ae235b
                      gsize                 *size,
Packit ae235b
                      guint32               *flags,
Packit ae235b
                      GError               **error)
Packit ae235b
{
Packit ae235b
  gboolean res = FALSE;
Packit ae235b
  GList *l;
Packit ae235b
  gboolean r_res;
Packit ae235b
Packit ae235b
  register_lazy_static_resources ();
Packit ae235b
Packit ae235b
  g_rw_lock_reader_lock (&resources_lock);
Packit ae235b
Packit ae235b
  for (l = registered_resources; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      GResource *r = l->data;
Packit ae235b
      GError *my_error = NULL;
Packit ae235b
Packit ae235b
      r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error);
Packit ae235b
      if (!r_res &&
Packit ae235b
          g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
Packit ae235b
        {
Packit ae235b
          g_clear_error (&my_error);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (!r_res)
Packit ae235b
            g_propagate_error (error, my_error);
Packit ae235b
          res = r_res;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (l == NULL)
Packit ae235b
    g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
Packit ae235b
                 _("The resource at “%s” does not exist"),
Packit ae235b
                 path);
Packit ae235b
Packit ae235b
  g_rw_lock_reader_unlock (&resources_lock);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* This code is to handle registration of resources very early, from a constructor.
Packit ae235b
 * At that point we'd like to do minimal work, to avoid ordering issues. For instance,
Packit ae235b
 * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable
Packit ae235b
 * before the first call to g_malloc.
Packit ae235b
 *
Packit ae235b
 * So, what we do at construction time is that we just register a static structure on
Packit ae235b
 * a list of resources that need to be initialized, and then later, when doing any lookups
Packit ae235b
 * in the global list of registered resources, or when getting a reference to the
Packit ae235b
 * lazily initialized resource we lazily create and register all the GResources on
Packit ae235b
 * the lazy list.
Packit ae235b
 *
Packit ae235b
 * To avoid having to use locks in the constructor, and having to grab the writer lock
Packit ae235b
 * when checking the lazy registering list we update lazy_register_resources in
Packit ae235b
 * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all
Packit ae235b
 * operations except:
Packit ae235b
 *  * check if there are any resources to lazily initialize
Packit ae235b
 *  * Add a static resource to the lazy init list
Packit ae235b
 * Do use the full writer lock for protection.
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void
Packit ae235b
register_lazy_static_resources_unlocked (void)
Packit ae235b
{
Packit ae235b
  GStaticResource *list;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    list = lazy_register_resources;
Packit ae235b
  while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, list, NULL));
Packit ae235b
Packit ae235b
  while (list != NULL)
Packit ae235b
    {
Packit ae235b
      GBytes *bytes = g_bytes_new_static (list->data, list->data_len);
Packit ae235b
      GResource *resource = g_resource_new_from_data (bytes, NULL);
Packit ae235b
      if (resource)
Packit ae235b
        {
Packit ae235b
          g_resources_register_unlocked (resource);
Packit ae235b
          g_atomic_pointer_set (&list->resource, resource);
Packit ae235b
        }
Packit ae235b
      g_bytes_unref (bytes);
Packit ae235b
Packit ae235b
      list = list->next;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
register_lazy_static_resources (void)
Packit ae235b
{
Packit ae235b
  if (g_atomic_pointer_get (&lazy_register_resources) == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_rw_lock_writer_lock (&resources_lock);
Packit ae235b
  register_lazy_static_resources_unlocked ();
Packit ae235b
  g_rw_lock_writer_unlock (&resources_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_resource_init:
Packit ae235b
 * @static_resource: pointer to a static #GStaticResource
Packit ae235b
 *
Packit ae235b
 * Initializes a GResource from static data using a
Packit ae235b
 * GStaticResource.
Packit ae235b
 *
Packit ae235b
 * This is normally used by code generated by
Packit ae235b
 * [glib-compile-resources][glib-compile-resources]
Packit ae235b
 * and is not typically used by other code.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_static_resource_init (GStaticResource *static_resource)
Packit ae235b
{
Packit ae235b
  gpointer next;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      next = lazy_register_resources;
Packit ae235b
      static_resource->next = next;
Packit ae235b
    }
Packit ae235b
  while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_resource_fini:
Packit ae235b
 * @static_resource: pointer to a static #GStaticResource
Packit ae235b
 *
Packit ae235b
 * Finalized a GResource initialized by g_static_resource_init().
Packit ae235b
 *
Packit ae235b
 * This is normally used by code generated by
Packit ae235b
 * [glib-compile-resources][glib-compile-resources]
Packit ae235b
 * and is not typically used by other code.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_static_resource_fini (GStaticResource *static_resource)
Packit ae235b
{
Packit ae235b
  GResource *resource;
Packit ae235b
Packit ae235b
  g_rw_lock_writer_lock (&resources_lock);
Packit ae235b
Packit ae235b
  register_lazy_static_resources_unlocked ();
Packit ae235b
Packit ae235b
  resource = g_atomic_pointer_get (&static_resource->resource);
Packit ae235b
  if (resource)
Packit ae235b
    {
Packit ae235b
      g_atomic_pointer_set (&static_resource->resource, NULL);
Packit ae235b
      g_resources_unregister_unlocked (resource);
Packit ae235b
      g_resource_unref (resource);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_rw_lock_writer_unlock (&resources_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_resource_get_resource:
Packit ae235b
 * @static_resource: pointer to a static #GStaticResource
Packit ae235b
 *
Packit ae235b
 * Gets the GResource that was registered by a call to g_static_resource_init().
Packit ae235b
 *
Packit ae235b
 * This is normally used by code generated by
Packit ae235b
 * [glib-compile-resources][glib-compile-resources]
Packit ae235b
 * and is not typically used by other code.
Packit ae235b
 *
Packit ae235b
 * Returns:  (transfer none): a #GResource
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
GResource *
Packit ae235b
g_static_resource_get_resource (GStaticResource *static_resource)
Packit ae235b
{
Packit ae235b
  register_lazy_static_resources ();
Packit ae235b
Packit ae235b
  return g_atomic_pointer_get (&static_resource->resource);
Packit ae235b
}