[gnome-builder/wip/chergert/flatpak-repo] work on repo abstraction




commit 48568a53ef84de0a97f93c3e175963016bc8616a
Author: Christian Hergert <chergert redhat com>
Date:   Mon Aug 23 15:05:15 2021 -0700

    work on repo abstraction

 src/plugins/flatpak/daemon/gnome-builder-flatpak.c |   3 +
 src/plugins/flatpak/daemon/ipc-flatpak-repo.c      | 210 +++++++++++++++++++++
 src/plugins/flatpak/daemon/ipc-flatpak-repo.h      |  36 ++++
 .../flatpak/daemon/ipc-flatpak-service-impl.c      |   6 +
 src/plugins/flatpak/daemon/meson.build             |   1 +
 .../flatpak/gbp-flatpak-application-addin.c        |   3 +
 6 files changed, 259 insertions(+)
---
diff --git a/src/plugins/flatpak/daemon/gnome-builder-flatpak.c 
b/src/plugins/flatpak/daemon/gnome-builder-flatpak.c
index 9321adf18..02a5ec425 100644
--- a/src/plugins/flatpak/daemon/gnome-builder-flatpak.c
+++ b/src/plugins/flatpak/daemon/gnome-builder-flatpak.c
@@ -34,6 +34,7 @@
 #include <sys/procctl.h>
 #endif
 
+#include "ipc-flatpak-repo.h"
 #include "ipc-flatpak-service.h"
 #include "ipc-flatpak-service-impl.h"
 
@@ -110,6 +111,8 @@ main (gint argc,
   g_dbus_connection_set_exit_on_close (connection, FALSE);
   g_signal_connect_swapped (connection, "closed", G_CALLBACK (g_main_loop_quit), main_loop);
 
+  (void)ipc_flatpak_repo_get_default ();
+
   service = ipc_flatpak_service_impl_new ();
 
   if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (service),
diff --git a/src/plugins/flatpak/daemon/ipc-flatpak-repo.c b/src/plugins/flatpak/daemon/ipc-flatpak-repo.c
new file mode 100644
index 000000000..ee33cb5b5
--- /dev/null
+++ b/src/plugins/flatpak/daemon/ipc-flatpak-repo.c
@@ -0,0 +1,210 @@
+/* ipc-flatpak-repo.c
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ipc-flatpak-repo"
+
+#include "ipc-flatpak-repo.h"
+
+struct _IpcFlatpakRepo
+{
+  GObject parent_instance;
+  FlatpakInstallation *installation;
+};
+
+G_DEFINE_TYPE (IpcFlatpakRepo, ipc_flatpak_repo, G_TYPE_OBJECT)
+
+static const char filter_file_contents[] = "\
+deny *\n\
+allow runtime/org.freedesktop.*\n\
+allow runtime/org.gnome.*\n\
+";
+
+static const struct {
+  const char *name;
+  const char *url;
+} flatpak_remotes[] = {
+  { "flathub", "https://flathub.org/repo/flathub.flatpakrepo"; },
+  { "flathub-beta", "https://flathub.org/beta-repo/flathub-beta.flatpakrepo"; },
+  { "gnome-nightly", "https://nightly.gnome.org/gnome-nightly.flatpakrepo"; },
+};
+
+static void
+ipc_flatpak_repo_constructed (GObject *object)
+{
+  IpcFlatpakRepo *self = (IpcFlatpakRepo *)object;
+  g_autofree gchar *gnome_builder_conf_data = NULL;
+  g_autoptr(GFile) etc = NULL;
+  g_autoptr(GFile) installations_d = NULL;
+  g_autoptr(GFile) gnome_builder_conf = NULL;
+  g_autoptr(GFile) filter_file = NULL;
+  g_autoptr(GFile) flatpak = NULL;
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GKeyFile) keyfile = NULL;
+
+  g_assert (IPC_IS_FLATPAK_REPO (self));
+
+  G_OBJECT_CLASS (ipc_flatpak_repo_parent_class)->constructed (object);
+
+  flatpak = g_file_new_build_filename (g_get_user_data_dir (), "gnome-builder", "flatpak", NULL);
+  filter_file = g_file_get_child (flatpak, "filter");
+  etc = g_file_get_child (flatpak, "etc");
+  installations_d = g_file_get_child (etc, "installations.d");
+  gnome_builder_conf = g_file_get_child (installations_d, "gnome-builder.conf");
+
+  /* Create installation if it doesn't exist */
+  if (!(self->installation = flatpak_installation_new_for_path (flatpak, TRUE, NULL, &error)))
+    {
+      g_warning ("Failed to create private flatpak installation: %s", error->message);
+      return;
+    }
+
+  /* Create filter list to only allow runtimes */
+  if (!g_file_replace_contents (filter_file, filter_file_contents, strlen (filter_file_contents),
+                                NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL, &error))
+    {
+      g_warning ("Failed to create repository filter file: %s", error->message);
+      return;
+    }
+
+  g_assert (FLATPAK_IS_INSTALLATION (self->installation));
+
+  /* Add repos we need for development to private installation, but filtered to
+   * only include runtimes.
+   */
+  for (guint i = 0; i < G_N_ELEMENTS (flatpak_remotes); i++)
+    {
+      g_autoptr(FlatpakRemote) remote = NULL;
+
+      if (!(remote = flatpak_installation_get_remote_by_name (self->installation, flatpak_remotes[i].name, 
NULL, NULL)))
+        {
+          g_autofree char *title = g_strdup_printf ("Builder (%s)", flatpak_remotes[i].name);
+
+          remote = flatpak_remote_new (flatpak_remotes[i].name);
+          flatpak_remote_set_filter (remote, g_file_peek_path (filter_file));
+          flatpak_remote_set_prio (remote, 0);
+          flatpak_remote_set_title (remote, title);
+          flatpak_remote_set_url (remote, flatpak_remotes[i].url);
+
+          if (!flatpak_installation_add_remote (self->installation, remote, TRUE, NULL, &error))
+            {
+              g_warning ("Failed to add remote %s to flatpak installation: %s",
+                         flatpak_remotes[i].name, error->message);
+              g_clear_error (&error);
+            }
+        }
+    }
+
+#define INSTALLATION_NAME "Installation \"GNOME Builder\""
+
+  keyfile = g_key_file_new ();
+  g_key_file_set_string (keyfile, INSTALLATION_NAME, "Path", g_file_peek_path (flatpak));
+  g_key_file_set_string (keyfile, INSTALLATION_NAME, "DisplayName", "GNOME Builder");
+  g_key_file_set_integer (keyfile, INSTALLATION_NAME, "Priority", 0);
+  g_key_file_set_string (keyfile, INSTALLATION_NAME, "StorageType", "harddisk");
+  gnome_builder_conf_data = g_key_file_to_data (keyfile, NULL, NULL);
+
+  /* Now setup a configuration file that points to all the installations we
+   * know about so that we can use FLATPAK_CONFIG_DIR to initialize them.
+   */
+  if ((!g_file_query_exists (installations_d, NULL) &&
+       !g_file_make_directory_with_parents (installations_d, NULL, &error)) ||
+      !g_file_replace_contents (gnome_builder_conf, gnome_builder_conf_data, strlen 
(gnome_builder_conf_data),
+                                NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL, &error))
+    {
+      g_warning ("Failed to create flatpak site configuration: %s", error->message);
+      return;
+    }
+}
+
+static void
+ipc_flatpak_repo_finalize (GObject *object)
+{
+  IpcFlatpakRepo *self = (IpcFlatpakRepo *)object;
+
+  g_clear_object (&self->installation);
+
+  G_OBJECT_CLASS (ipc_flatpak_repo_parent_class)->finalize (object);
+}
+
+static void
+ipc_flatpak_repo_class_init (IpcFlatpakRepoClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = ipc_flatpak_repo_constructed;
+  object_class->finalize = ipc_flatpak_repo_finalize;
+}
+
+static void
+ipc_flatpak_repo_init (IpcFlatpakRepo *self)
+{
+}
+
+IpcFlatpakRepo *
+ipc_flatpak_repo_get_default (void)
+{
+  static IpcFlatpakRepo *instance;
+
+  if (!instance)
+    {
+      instance = g_object_new (IPC_TYPE_FLATPAK_REPO, NULL);
+      g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *)&instance);
+    }
+
+  return instance;
+}
+
+FlatpakInstallation *
+ipc_flatpak_repo_get_installation (IpcFlatpakRepo *self)
+{
+  g_return_val_if_fail (IPC_IS_FLATPAK_REPO (self), NULL);
+
+  return self->installation;
+}
+
+char *
+ipc_flatpak_repo_get_path (IpcFlatpakRepo *self)
+{
+  g_autoptr(GFile) path = NULL;
+
+  g_return_val_if_fail (IPC_IS_FLATPAK_REPO (self), NULL);
+
+  if (self->installation == NULL)
+    return NULL;
+
+  if (!(path = flatpak_installation_get_path (self->installation)))
+    return NULL;
+
+  return g_file_get_path (path);
+}
+
+char *
+ipc_flatpak_repo_get_config_dir (IpcFlatpakRepo *self)
+{
+  g_autoptr(GFile) path = NULL;
+
+  g_return_val_if_fail (IPC_IS_FLATPAK_REPO (self), NULL);
+
+  return g_build_filename (g_get_user_data_dir (),
+                           "gnome-builder",
+                           "etc",
+                           "flatpak",
+                           NULL);
+}
diff --git a/src/plugins/flatpak/daemon/ipc-flatpak-repo.h b/src/plugins/flatpak/daemon/ipc-flatpak-repo.h
new file mode 100644
index 000000000..f96c7f97a
--- /dev/null
+++ b/src/plugins/flatpak/daemon/ipc-flatpak-repo.h
@@ -0,0 +1,36 @@
+/* ipc-flatpak-service-impl.c
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <flatpak/flatpak.h>
+
+G_BEGIN_DECLS
+
+#define IPC_TYPE_FLATPAK_REPO (ipc_flatpak_repo_get_type())
+
+G_DECLARE_FINAL_TYPE (IpcFlatpakRepo, ipc_flatpak_repo, IPC, FLATPAK_REPO, GObject)
+
+IpcFlatpakRepo      *ipc_flatpak_repo_get_default      (void);
+FlatpakInstallation *ipc_flatpak_repo_get_installation (IpcFlatpakRepo  *self);
+char                *ipc_flatpak_repo_get_path         (IpcFlatpakRepo  *self);
+char                *ipc_flatpak_repo_get_config_dir   (IpcFlatpakRepo  *self);
+
+G_END_DECLS
diff --git a/src/plugins/flatpak/daemon/ipc-flatpak-service-impl.c 
b/src/plugins/flatpak/daemon/ipc-flatpak-service-impl.c
index 3117c4615..39ff1b86e 100644
--- a/src/plugins/flatpak/daemon/ipc-flatpak-service-impl.c
+++ b/src/plugins/flatpak/daemon/ipc-flatpak-service-impl.c
@@ -25,6 +25,7 @@
 #include <flatpak/flatpak.h>
 #include <glib/gi18n.h>
 
+#include "ipc-flatpak-repo.h"
 #include "ipc-flatpak-service-impl.h"
 #include "ipc-flatpak-transfer.h"
 #include "ipc-flatpak-util.h"
@@ -1324,6 +1325,7 @@ ipc_flatpak_service_impl_constructed (GObject *object)
   g_autoptr(GPtrArray) installations = NULL;
   g_autoptr(FlatpakInstallation) user = NULL;
   g_autoptr(GFile) user_file = NULL;
+  FlatpakInstallation *priv_install;
 
   G_OBJECT_CLASS (ipc_flatpak_service_impl_parent_class)->constructed (object);
 
@@ -1338,6 +1340,10 @@ ipc_flatpak_service_impl_constructed (GObject *object)
       for (guint i = 0; i < installations->len; i++)
         add_installation (self, g_ptr_array_index (installations, i), NULL);
     }
+
+  /* Fallback for SDKs not available elsewhere */
+  if ((priv_install = ipc_flatpak_repo_get_installation (ipc_flatpak_repo_get_default ())))
+    add_installation (self, priv_install, NULL);
 }
 
 static void
diff --git a/src/plugins/flatpak/daemon/meson.build b/src/plugins/flatpak/daemon/meson.build
index 1e803677b..e6158d0c4 100644
--- a/src/plugins/flatpak/daemon/meson.build
+++ b/src/plugins/flatpak/daemon/meson.build
@@ -22,6 +22,7 @@ ipc_flatpak_transfer_src = gnome.gdbus_codegen('ipc-flatpak-transfer',
 gnome_builder_flatpak_sources = [
   'gnome-builder-flatpak.c',
   'ipc-flatpak-service-impl.c',
+  'ipc-flatpak-repo.c',
   ipc_flatpak_service_src,
   ipc_flatpak_transfer_src,
 ]
diff --git a/src/plugins/flatpak/gbp-flatpak-application-addin.c 
b/src/plugins/flatpak/gbp-flatpak-application-addin.c
index 7553238dd..c93e7a40b 100644
--- a/src/plugins/flatpak/gbp-flatpak-application-addin.c
+++ b/src/plugins/flatpak/gbp-flatpak-application-addin.c
@@ -49,6 +49,7 @@ ensure_remotes_exist_sync (GError **error)
 {
   IDE_ENTRY;
 
+#if 0
   for (guint i = 0; i < G_N_ELEMENTS (builtin_flatpak_repos); i++)
     {
       g_autoptr(IdeSubprocessLauncher) launcher = NULL;
@@ -73,6 +74,8 @@ ensure_remotes_exist_sync (GError **error)
       if (subprocess == NULL || !ide_subprocess_wait_check (subprocess, NULL, error))
         IDE_RETURN (FALSE);
     }
+#endif
+
   IDE_RETURN (TRUE);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]