Blame SOURCES/flatpak-1.0.4-oci-fixes.patch

109edb
From 3f5235e925ba6555cd9c639684660356867c952f Mon Sep 17 00:00:00 2001
109edb
From: "Owen W. Taylor" <otaylor@fishsoup.net>
109edb
Date: Fri, 30 Nov 2018 16:11:06 -0500
109edb
Subject: [PATCH 1/3] flatpak_cache_http_uri: save downloaded files with
109edb
 permission 0644
109edb
109edb
Previously, downloaded files were being saved with 0600 permissions,
109edb
which prevented OCI icons downloaded by the system helper at appstream
109edb
creation time from being read by users.
109edb
109edb
Closes: #2362
109edb
Approved by: matthiasclasen
109edb
---
109edb
 common/flatpak-utils-http.c | 3 +++
109edb
 1 file changed, 3 insertions(+)
109edb
109edb
diff --git a/common/flatpak-utils-http.c b/common/flatpak-utils-http.c
109edb
index 53074162..997c9db8 100644
109edb
--- a/common/flatpak-utils-http.c
109edb
+++ b/common/flatpak-utils-http.c
109edb
@@ -645,6 +645,9 @@ sync_and_rename_tmpfile (GLnxTmpfile *tmpfile,
109edb
   if (fdatasync (tmpfile->fd) != 0)
109edb
     return glnx_throw_errno_prefix (error, "fdatasync");
109edb
 
109edb
+  if (fchmod (tmpfile->fd, 0644) != 0)
109edb
+    return glnx_throw_errno_prefix (error, "fchmod");
109edb
+
109edb
   if (!glnx_link_tmpfile_at (tmpfile,
109edb
                              GLNX_LINK_TMPFILE_REPLACE,
109edb
                              tmpfile->src_dfd, dest_name, error))
109edb
-- 
109edb
2.19.2
109edb
109edb
109edb
From 3263827dbbd4d84919899e91ca066d2d3cf338bc Mon Sep 17 00:00:00 2001
109edb
From: Alexander Larsson <alexl@redhat.com>
109edb
Date: Fri, 30 Nov 2018 10:30:20 +0100
109edb
Subject: [PATCH 2/3] OCI: Use system helper to generate summary for OCI
109edb
 remotes
109edb
109edb
The OCI support relies on downloading a json index and converting it
109edb
to a ostree-style summary, which we the use in all sorts of operations
109edb
in the client code. Currently this happens in the user code, which means
109edb
that it will fail (due to permissions) in the system installation case.
109edb
109edb
We could do the conversion as the user, but when eventually installing
109edb
something the system-helper will anyway do this download and
109edb
conversion, so that would only double the work and risk things going out
109edb
of sync. Also, the OCI index is not gpg signed, so we can't realy on
109edb
downloads done as the user.
109edb
109edb
So, the solution done here is to add a GenerateOciSummary
109edb
system-helper call which we use instead of directly generating the
109edb
oci summary.
109edb
109edb
This fixes https://github.com/flatpak/flatpak/issues/2350
109edb
109edb
Closes: #2363
109edb
Approved by: matthiasclasen
109edb
---
109edb
 common/flatpak-dir-private.h          |  5 ++
109edb
 common/flatpak-dir.c                  | 94 +++++++++++++++++++--------
109edb
 data/org.freedesktop.Flatpak.xml      |  5 ++
109edb
 system-helper/flatpak-system-helper.c | 52 ++++++++++++++-
109edb
 4 files changed, 129 insertions(+), 27 deletions(-)
109edb
109edb
diff --git a/common/flatpak-dir-private.h b/common/flatpak-dir-private.h
109edb
index 64a72758..f6126056 100644
109edb
--- a/common/flatpak-dir-private.h
109edb
+++ b/common/flatpak-dir-private.h
109edb
@@ -718,6 +718,11 @@ FlatpakRemoteState * flatpak_dir_get_remote_state_for_summary (FlatpakDir   *sel
109edb
                                                                GBytes       *opt_summary_sig,
109edb
                                                                GCancellable *cancellable,
109edb
                                                                GError      **error);
109edb
+gboolean flatpak_dir_remote_make_oci_summary (FlatpakDir   *self,
109edb
+                                              const char   *remote,
109edb
+                                              GBytes      **out_summary,
109edb
+                                              GCancellable *cancellable,
109edb
+                                              GError      **error);
109edb
 FlatpakRemoteState * flatpak_dir_get_remote_state_optional (FlatpakDir   *self,
109edb
                                                             const char   *remote,
109edb
                                                             GCancellable *cancellable,
109edb
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
109edb
index 828945ca..7853b74a 100644
109edb
--- a/common/flatpak-dir.c
109edb
+++ b/common/flatpak-dir.c
109edb
@@ -1385,6 +1385,22 @@ flatpak_dir_system_helper_call_update_summary (FlatpakDir   *self,
109edb
   return ret != NULL;
109edb
 }
109edb
 
109edb
+static gboolean
109edb
+flatpak_dir_system_helper_call_generate_oci_summary (FlatpakDir   *self,
109edb
+                                                     const gchar  *arg_origin,
109edb
+                                                     const gchar  *arg_installation,
109edb
+                                                     GCancellable *cancellable,
109edb
+                                                     GError      **error)
109edb
+{
109edb
+  g_autoptr(GVariant) ret =
109edb
+    flatpak_dir_system_helper_call (self, "GenerateOciSummary",
109edb
+                                    g_variant_new ("(ss)",
109edb
+                                                   arg_origin,
109edb
+                                                   arg_installation),
109edb
+                                    cancellable, error);
109edb
+  return ret != NULL;
109edb
+}
109edb
+
109edb
 static OstreeRepo *
109edb
 system_ostree_repo_new (GFile *repodir)
109edb
 {
109edb
@@ -9088,7 +9104,7 @@ flatpak_dir_cache_summary (FlatpakDir *self,
109edb
   G_UNLOCK (cache);
109edb
 }
109edb
 
109edb
-static gboolean
109edb
+gboolean
109edb
 flatpak_dir_remote_make_oci_summary (FlatpakDir   *self,
109edb
                                      const char   *remote,
109edb
                                      GBytes      **out_summary,
109edb
@@ -9103,42 +9119,68 @@ flatpak_dir_remote_make_oci_summary (FlatpakDir   *self,
109edb
   g_autoptr(GError) local_error = NULL;
109edb
   g_autoptr(GMappedFile) mfile = NULL;
109edb
   g_autoptr(GBytes) cache_bytes = NULL;
109edb
+  g_autoptr(GBytes) summary_bytes = NULL;
109edb
 
109edb
-  self_name = flatpak_dir_get_name (self);
109edb
-
109edb
-  index_cache = flatpak_dir_update_oci_index (self, remote, &index_uri, cancellable, error);
109edb
-  if (index_cache == NULL)
109edb
-    return FALSE;
109edb
+  if (flatpak_dir_use_system_helper (self, NULL))
109edb
+    {
109edb
+      const char *installation = flatpak_dir_get_id (self);
109edb
 
109edb
-  summary_cache = flatpak_dir_get_oci_summary_location (self, remote, error);
109edb
-  if (summary_cache == NULL)
109edb
-    return FALSE;
109edb
+      if (!flatpak_dir_system_helper_call_generate_oci_summary (self, remote,
109edb
+                                                                installation ? installation : "",
109edb
+                                                                cancellable, error))
109edb
+        return FALSE;
109edb
 
109edb
-  if (check_destination_mtime (index_cache, summary_cache, cancellable))
109edb
+      summary_cache = flatpak_dir_get_oci_summary_location (self, remote, error);
109edb
+      if (summary_cache == NULL)
109edb
+        return FALSE;
109edb
+    }
109edb
+  else
109edb
     {
109edb
-      mfile = g_mapped_file_new (flatpak_file_get_path_cached (summary_cache), FALSE, NULL);
109edb
-      if (mfile)
109edb
+      self_name = flatpak_dir_get_name (self);
109edb
+
109edb
+      index_cache = flatpak_dir_update_oci_index (self, remote, &index_uri, cancellable, error);
109edb
+      if (index_cache == NULL)
109edb
+        return FALSE;
109edb
+
109edb
+      summary_cache = flatpak_dir_get_oci_summary_location (self, remote, error);
109edb
+      if (summary_cache == NULL)
109edb
+        return FALSE;
109edb
+
109edb
+      if (!check_destination_mtime (index_cache, summary_cache, cancellable))
109edb
         {
109edb
-          cache_bytes = g_mapped_file_get_bytes (mfile);
109edb
-          *out_summary = g_steal_pointer (&cache_bytes);
109edb
+          summary = flatpak_oci_index_make_summary (index_cache, index_uri, cancellable, &local_error);
109edb
+          if (summary == NULL)
109edb
+            {
109edb
+              g_propagate_error (error, g_steal_pointer (&local_error));
109edb
+              return FALSE;
109edb
+            }
109edb
+
109edb
+          summary_bytes = g_variant_get_data_as_bytes (summary);
109edb
+
109edb
+          if (!g_file_replace_contents (summary_cache,
109edb
+                                        g_bytes_get_data (summary_bytes, NULL),
109edb
+                                        g_bytes_get_size (summary_bytes),
109edb
+                                        NULL, FALSE, 0, NULL, cancellable, error))
109edb
+            {
109edb
+              g_prefix_error (error, _("Failed to write summary cache: "));
109edb
+              return FALSE;
109edb
+            }
109edb
+
109edb
+          if (out_summary)
109edb
+              *out_summary = g_steal_pointer (&summary_bytes);
109edb
           return TRUE;
109edb
         }
109edb
     }
109edb
 
109edb
-  summary = flatpak_oci_index_make_summary (index_cache, index_uri, cancellable, &local_error);
109edb
-  if (summary == NULL)
109edb
+  if (out_summary)
109edb
     {
109edb
-      g_propagate_error (error, g_steal_pointer (&local_error));
109edb
-      return FALSE;
109edb
-    }
109edb
-
109edb
-  *out_summary = g_variant_get_data_as_bytes (summary);
109edb
+      mfile = g_mapped_file_new (flatpak_file_get_path_cached (summary_cache), FALSE, error);
109edb
+      if (mfile == NULL)
109edb
+        return FALSE;
109edb
 
109edb
-  if (!g_file_replace_contents (summary_cache,
109edb
-                                g_bytes_get_data (*out_summary, NULL),
109edb
-                                g_bytes_get_size (*out_summary),
109edb
-                                NULL, FALSE, 0, NULL, cancellable, NULL))
109edb
-    g_warning ("Failed to write summary cache");
109edb
+      cache_bytes = g_mapped_file_get_bytes (mfile);
109edb
+      *out_summary = g_steal_pointer (&cache_bytes);
109edb
+    }
109edb
 
109edb
   return TRUE;
109edb
 }
109edb
diff --git a/data/org.freedesktop.Flatpak.xml b/data/org.freedesktop.Flatpak.xml
109edb
index 25dc8a02..8b1606c6 100644
109edb
--- a/data/org.freedesktop.Flatpak.xml
109edb
+++ b/data/org.freedesktop.Flatpak.xml
109edb
@@ -144,6 +144,11 @@
109edb
       <arg type='s' name='installation' direction='in'/>
109edb
     </method>
109edb
 
109edb
+    <method name="GenerateOciSummary">
109edb
+      <arg type='s' name='origin' direction='in'/>
109edb
+      <arg type='s' name='installation' direction='in'/>
109edb
+    </method>
109edb
+
109edb
   </interface>
109edb
 
109edb
 </node>
109edb
diff --git a/system-helper/flatpak-system-helper.c b/system-helper/flatpak-system-helper.c
109edb
index ce647b6e..29a2d3e1 100644
109edb
--- a/system-helper/flatpak-system-helper.c
109edb
+++ b/system-helper/flatpak-system-helper.c
109edb
@@ -1122,6 +1122,54 @@ handle_update_summary (FlatpakSystemHelper   *object,
109edb
   return TRUE;
109edb
 }
109edb
 
109edb
+static gboolean
109edb
+handle_generate_oci_summary (FlatpakSystemHelper   *object,
109edb
+                             GDBusMethodInvocation *invocation,
109edb
+                             const gchar           *arg_origin,
109edb
+                             const gchar           *arg_installation)
109edb
+{
109edb
+  g_autoptr(FlatpakDir) system = NULL;
109edb
+  g_autoptr(GError) error = NULL;
109edb
+  gboolean is_oci;
109edb
+
109edb
+  g_debug ("GenerateOciSummary %s %s", arg_origin, arg_installation);
109edb
+
109edb
+  system = dir_get_system (arg_installation, &error);
109edb
+  if (system == NULL)
109edb
+    {
109edb
+      g_dbus_method_invocation_return_gerror (invocation, error);
109edb
+      return TRUE;
109edb
+    }
109edb
+
109edb
+  if (!flatpak_dir_ensure_repo (system, NULL, &error))
109edb
+    {
109edb
+      g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
109edb
+                                             "Can't open system repo %s", error->message);
109edb
+      return TRUE;
109edb
+    }
109edb
+
109edb
+  is_oci = flatpak_dir_get_remote_oci (system, arg_origin);
109edb
+  if (!is_oci)
109edb
+    {
109edb
+      g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
109edb
+                                             "%s is not a OCI remote", arg_origin);
109edb
+      return TRUE;
109edb
+    }
109edb
+
109edb
+  if (!flatpak_dir_remote_make_oci_summary (system, arg_origin, NULL, NULL, &error))
109edb
+    {
109edb
+      g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
109edb
+                                             "Failed to update OCI summary: %s", error->message);
109edb
+      return TRUE;
109edb
+    }
109edb
+
109edb
+
109edb
+  flatpak_system_helper_complete_generate_oci_summary (object, invocation);
109edb
+
109edb
+  return TRUE;
109edb
+}
109edb
+
109edb
+
109edb
 static gboolean
109edb
 flatpak_authorize_method_handler (GDBusInterfaceSkeleton *interface,
109edb
                                   GDBusMethodInvocation  *invocation,
109edb
@@ -1250,7 +1298,8 @@ flatpak_authorize_method_handler (GDBusInterfaceSkeleton *interface,
109edb
            g_strcmp0 (method_name, "PruneLocalRepo") == 0 ||
109edb
            g_strcmp0 (method_name, "EnsureRepo") == 0 ||
109edb
            g_strcmp0 (method_name, "RunTriggers") == 0 ||
109edb
-           g_strcmp0 (method_name, "UpdateSummary") == 0)
109edb
+           g_strcmp0 (method_name, "UpdateSummary") == 0 ||
109edb
+           g_strcmp0 (method_name, "GenerateOciSummary") == 0)
109edb
     {
109edb
       const char *remote;
109edb
 
109edb
@@ -1321,6 +1370,7 @@ on_bus_acquired (GDBusConnection *connection,
109edb
   g_signal_connect (helper, "handle-ensure-repo", G_CALLBACK (handle_ensure_repo), NULL);
109edb
   g_signal_connect (helper, "handle-run-triggers", G_CALLBACK (handle_run_triggers), NULL);
109edb
   g_signal_connect (helper, "handle-update-summary", G_CALLBACK (handle_update_summary), NULL);
109edb
+  g_signal_connect (helper, "handle-generate-oci-summary", G_CALLBACK (handle_generate_oci_summary), NULL);
109edb
 
109edb
   g_signal_connect (helper, "g-authorize-method",
109edb
                     G_CALLBACK (flatpak_authorize_method_handler),
109edb
-- 
109edb
2.19.2
109edb
109edb
109edb
From b7f1d5118fc4e1df472f7108472f122e279fe2b9 Mon Sep 17 00:00:00 2001
109edb
From: Matthias Clasen <mclasen@redhat.com>
109edb
Date: Fri, 7 Dec 2018 14:39:06 -0500
109edb
Subject: [PATCH 3/3] Fix oci pull progress reporting
109edb
109edb
Comparing the code in flatpak-utils.c:progress_cb,
109edb
we need to set bytes-transferred for the total amount
109edb
of data that has been transferred so far. The value
109edb
we were setting so far, fetched-delta-part-size, refers
109edb
to the size of the objects we already have locally, and
109edb
is subtracted from the total, which explains oci progress
109edb
running backwards.
109edb
109edb
Closes: #2392
109edb
109edb
Closes: #2400
109edb
Approved by: matthiasclasen
109edb
---
109edb
 common/flatpak-dir.c | 2 +-
109edb
 1 file changed, 1 insertion(+), 1 deletion(-)
109edb
109edb
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
109edb
index 7853b74a..51cd1e66 100644
109edb
--- a/common/flatpak-dir.c
109edb
+++ b/common/flatpak-dir.c
109edb
@@ -4154,7 +4154,7 @@ oci_pull_progress_cb (guint64 total_size, guint64 pulled_size,
109edb
                              "total-delta-parts", "u", n_layers,
109edb
                              "fetched-delta-fallbacks", "u", 0,
109edb
                              "total-delta-fallbacks", "u", 0,
109edb
-                             "fetched-delta-part-size", "t", pulled_size,
109edb
+                             "bytes-transferred", "t", pulled_size,
109edb
                              "total-delta-part-size", "t", total_size,
109edb
                              "total-delta-part-usize", "t", total_size,
109edb
                              "total-delta-superblocks", "u", 0,
109edb
-- 
109edb
2.19.2
109edb