From a28ced36c7d70ad549b910ae114bd407b512aee4 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 8 Dec 2015 11:48:22 +0100 Subject: [PATCH] lib: Add xdg_app_installation_load_app_overrides() --- common/xdg-app-dir.c | 68 +++++++++++++++++++++++++++----------- common/xdg-app-dir.h | 4 +++ lib/xdg-app-installation.c | 24 ++++++++++++-- lib/xdg-app-installation.h | 4 +++ 4 files changed, 77 insertions(+), 23 deletions(-) diff --git a/common/xdg-app-dir.c b/common/xdg-app-dir.c index cc9b02a03..9ff47c718 100644 --- a/common/xdg-app-dir.c +++ b/common/xdg-app-dir.c @@ -262,6 +262,30 @@ xdg_app_dir_get_path (XdgAppDir *self) return self->basedir; } +char * +xdg_app_dir_load_override (XdgAppDir *self, + const char *app_id, + gsize *length, + GError **error) +{ + g_autoptr(GFile) override_dir = NULL; + g_autoptr(GFile) file = NULL; + char *metadata_contents; + + override_dir = g_file_get_child (self->basedir, "overrides"); + file = g_file_get_child (override_dir, app_id); + + if (!g_file_load_contents (file, NULL, + &metadata_contents, length, NULL, NULL)) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, + "No overrides found for %s", app_id); + return NULL; + } + + return metadata_contents; +} + GKeyFile * xdg_app_load_override_keyfile (const char *app_id, gboolean user, GError **error) { @@ -271,24 +295,19 @@ xdg_app_load_override_keyfile (const char *app_id, gboolean user, GError **error g_autofree char *metadata_contents = NULL; gsize metadata_size; g_autoptr(GKeyFile) metakey = g_key_file_new (); + g_autoptr(XdgAppDir) dir = NULL; - if (user) - base_dir = xdg_app_get_user_base_dir_location (); - else - base_dir = xdg_app_get_system_base_dir_location (); + dir = xdg_app_dir_get (user); - override_dir = g_file_get_child (base_dir, "overrides"); - file = g_file_get_child (override_dir, app_id); + metadata_contents = xdg_app_dir_load_override (dir, app_id, &metadata_size, error); + if (metadata_contents == NULL) + return NULL; - if (g_file_load_contents (file, NULL, - &metadata_contents, &metadata_size, NULL, NULL)) - { - - if (!g_key_file_load_from_data (metakey, - metadata_contents, metadata_size, - 0, error)) - return NULL; - } + if (!g_key_file_load_from_data (metakey, + metadata_contents, + metadata_size, + 0, error)) + return NULL; return g_steal_pointer (&metakey); } @@ -298,13 +317,22 @@ xdg_app_load_override_file (const char *app_id, gboolean user, GError **error) { XdgAppContext *overrides = xdg_app_context_new (); g_autoptr(GKeyFile) metakey = NULL; + g_autoptr(GError) my_error = NULL; - metakey = xdg_app_load_override_keyfile (app_id, user, error); + metakey = xdg_app_load_override_keyfile (app_id, user, &my_error); if (metakey == NULL) - return NULL; - - if (!xdg_app_context_load_metadata (overrides, metakey, error)) - return NULL; + { + if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) + { + g_propagate_error (error, g_steal_pointer (&error)); + return NULL; + } + } + else + { + if (!xdg_app_context_load_metadata (overrides, metakey, error)) + return NULL; + } return g_steal_pointer (&overrides); } diff --git a/common/xdg-app-dir.h b/common/xdg-app-dir.h index 771a0e7e1..f75897754 100644 --- a/common/xdg-app-dir.h +++ b/common/xdg-app-dir.h @@ -92,6 +92,10 @@ XdgAppDeploy *xdg_app_dir_load_deployed (XdgAppDir *self, const char *checksum, GCancellable *cancellable, GError **error); +char * xdg_app_dir_load_override (XdgAppDir *dir, + const char *app_id, + gsize *length, + GError **error); OstreeRepo *xdg_app_dir_get_repo (XdgAppDir *self); gboolean xdg_app_dir_ensure_path (XdgAppDir *self, GCancellable *cancellable, diff --git a/lib/xdg-app-installation.c b/lib/xdg-app-installation.c index 57553ed52..1c0134d71 100644 --- a/lib/xdg-app-installation.c +++ b/lib/xdg-app-installation.c @@ -53,9 +53,9 @@ xdg_app_installation_finalize (GObject *object) static void xdg_app_installation_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) + guint prop_id, + const GValue *value, + GParamSpec *pspec) { switch (prop_id) @@ -327,3 +327,21 @@ xdg_app_installation_list_remotes (XdgAppInstallation *self, g_ptr_array_add (remotes, NULL); return (XdgAppRemote **)g_ptr_array_free (g_steal_pointer (&remotes), FALSE); } + +char * +xdg_app_installation_load_app_overrides (XdgAppInstallation *self, + const char *app_id, + GCancellable *cancellable, + GError **error) +{ + XdgAppInstallationPrivate *priv = xdg_app_installation_get_instance_private (self); + g_autoptr(GBytes) bytes = NULL; + g_autofree char *metadata_contents = NULL; + gsize metadata_size; + + metadata_contents = xdg_app_dir_load_override (priv->dir, app_id, &metadata_size, error); + if (metadata_contents == NULL) + return NULL; + + return metadata_contents; +} diff --git a/lib/xdg-app-installation.h b/lib/xdg-app-installation.h index 95c3ca36a..d35fecde5 100644 --- a/lib/xdg-app-installation.h +++ b/lib/xdg-app-installation.h @@ -74,5 +74,9 @@ XDG_APP_EXTERN XdgAppInstalledRef * xdg_app_installation_get_current_installed_a XDG_APP_EXTERN XdgAppRemote ** xdg_app_installation_list_remotes (XdgAppInstallation *self, GCancellable *cancellable, GError **error); +XDG_APP_EXTERN char * xdg_app_installation_load_app_overrides (XdgAppInstallation *self, + const char *app_id, + GCancellable *cancellable, + GError **error); #endif /* __XDG_APP_INSTALLATION_H__ */