Blame SOURCES/flatpak-empty-var-lib-flatpak.patch

237905
From 6c50142ee524fc8c82e565b09f1e9223a41fb599 Mon Sep 17 00:00:00 2001
237905
From: Simon McVittie <smcv@collabora.com>
237905
Date: Tue, 8 Feb 2022 14:04:50 +0000
237905
Subject: [PATCH 1/7] dir: Pass cancellable through to remote EnsureRepo call
237905
237905
Signed-off-by: Simon McVittie <smcv@collabora.com>
237905
(cherry picked from commit 15c1d4f8cb8bf8266b454a9789fe589c645a5480)
237905
---
237905
 common/flatpak-dir.c | 4 ++--
237905
 1 file changed, 2 insertions(+), 2 deletions(-)
237905
237905
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
237905
index 965e1ca5376b..bc348b51a7e6 100644
237905
--- a/common/flatpak-dir.c
237905
+++ b/common/flatpak-dir.c
237905
@@ -3943,7 +3943,7 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
           if (!flatpak_dir_system_helper_call_ensure_repo (self,
237905
                                                            FLATPAK_HELPER_ENSURE_REPO_FLAGS_NONE,
237905
                                                            installation ? installation : "",
237905
-                                                           NULL, &local_error))
237905
+                                                           cancellable, &local_error))
237905
             {
237905
               if (allow_empty)
237905
                 return TRUE;
237905
@@ -4087,7 +4087,7 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
           if (!flatpak_dir_system_helper_call_ensure_repo (self,
237905
                                                            FLATPAK_HELPER_ENSURE_REPO_FLAGS_NONE,
237905
                                                            installation ? installation : "",
237905
-                                                           NULL, &my_error))
237905
+                                                           cancellable, &my_error))
237905
             {
237905
               if (allow_empty)
237905
                 return TRUE;
237905
-- 
237905
2.34.1
237905
237905
237905
From 03f16b300e662afd324d3afab04a95186bf31e07 Mon Sep 17 00:00:00 2001
237905
From: Simon McVittie <smcv@collabora.com>
237905
Date: Tue, 8 Feb 2022 14:09:05 +0000
237905
Subject: [PATCH 2/7] dir: Factor out common code to call EnsureRepo on system
237905
 helper
237905
237905
Signed-off-by: Simon McVittie <smcv@collabora.com>
237905
(cherry picked from commit 951b111d2627c6f62bfb5e20d86974beadcf2587)
237905
---
237905
 common/flatpak-dir.c | 54 +++++++++++++++++++++++---------------------
237905
 1 file changed, 28 insertions(+), 26 deletions(-)
237905
237905
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
237905
index bc348b51a7e6..8f7b7f011048 100644
237905
--- a/common/flatpak-dir.c
237905
+++ b/common/flatpak-dir.c
237905
@@ -3918,6 +3918,30 @@ apply_new_flatpakrepo (const char *remote_name,
237905
   return TRUE;
237905
 }
237905
 
237905
+static gboolean
237905
+system_helper_maybe_ensure_repo (FlatpakDir *self,
237905
+                                 gboolean allow_empty,
237905
+                                 GCancellable *cancellable,
237905
+                                 GError **error)
237905
+{
237905
+  g_autoptr(GError) local_error = NULL;
237905
+  const char *installation = flatpak_dir_get_id (self);
237905
+
237905
+  if (!flatpak_dir_system_helper_call_ensure_repo (self,
237905
+                                                   FLATPAK_HELPER_ENSURE_REPO_FLAGS_NONE,
237905
+                                                   installation ? installation : "",
237905
+                                                   cancellable, &local_error))
237905
+    {
237905
+      if (allow_empty)
237905
+        return TRUE;
237905
+
237905
+      g_propagate_error (error, g_steal_pointer (&local_error));
237905
+      return FALSE;
237905
+    }
237905
+
237905
+  return TRUE;
237905
+}
237905
+
237905
 static gboolean
237905
 _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
                           gboolean      allow_empty,
237905
@@ -3937,20 +3961,8 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
     {
237905
       if (flatpak_dir_use_system_helper (self, NULL))
237905
         {
237905
-          g_autoptr(GError) local_error = NULL;
237905
-          const char *installation = flatpak_dir_get_id (self);
237905
-
237905
-          if (!flatpak_dir_system_helper_call_ensure_repo (self,
237905
-                                                           FLATPAK_HELPER_ENSURE_REPO_FLAGS_NONE,
237905
-                                                           installation ? installation : "",
237905
-                                                           cancellable, &local_error))
237905
-            {
237905
-              if (allow_empty)
237905
-                return TRUE;
237905
-
237905
-              g_propagate_error (error, g_steal_pointer (&local_error));
237905
-              return FALSE;
237905
-            }
237905
+          if (!system_helper_maybe_ensure_repo (self, allow_empty, cancellable, error))
237905
+            return FALSE;
237905
         }
237905
       else
237905
         {
237905
@@ -4083,18 +4095,8 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
     {
237905
       if (flatpak_dir_use_system_helper (self, NULL))
237905
         {
237905
-          const char *installation = flatpak_dir_get_id (self);
237905
-          if (!flatpak_dir_system_helper_call_ensure_repo (self,
237905
-                                                           FLATPAK_HELPER_ENSURE_REPO_FLAGS_NONE,
237905
-                                                           installation ? installation : "",
237905
-                                                           cancellable, &my_error))
237905
-            {
237905
-              if (allow_empty)
237905
-                return TRUE;
237905
-
237905
-              g_propagate_error (error, g_steal_pointer (&my_error));
237905
-              return FALSE;
237905
-            }
237905
+          if (!system_helper_maybe_ensure_repo (self, allow_empty, cancellable, error))
237905
+            return FALSE;
237905
 
237905
           if (!ostree_repo_reload_config (repo, cancellable, error))
237905
             return FALSE;
237905
-- 
237905
2.34.1
237905
237905
237905
From 46d2abd78561c9a1686896292a19280d09deaf77 Mon Sep 17 00:00:00 2001
237905
From: Simon McVittie <smcv@collabora.com>
237905
Date: Tue, 8 Feb 2022 14:12:01 +0000
237905
Subject: [PATCH 3/7] dir: Factor out function to open the libostree repository
237905
237905
I'm about to add another caller for this.
237905
237905
Signed-off-by: Simon McVittie <smcv@collabora.com>
237905
(cherry picked from commit 8537b3412ad70479b16ad2c880c73ba37daf80d0)
237905
---
237905
 common/flatpak-dir.c | 27 +++++++++++++++++++--------
237905
 1 file changed, 19 insertions(+), 8 deletions(-)
237905
237905
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
237905
index 8f7b7f011048..f44241c40290 100644
237905
--- a/common/flatpak-dir.c
237905
+++ b/common/flatpak-dir.c
237905
@@ -3942,6 +3942,23 @@ system_helper_maybe_ensure_repo (FlatpakDir *self,
237905
   return TRUE;
237905
 }
237905
 
237905
+static gboolean
237905
+ensure_repo_opened (OstreeRepo *repo,
237905
+                    GCancellable *cancellable,
237905
+                    GError **error)
237905
+{
237905
+  if (!ostree_repo_open (repo, cancellable, error))
237905
+    {
237905
+      g_autofree char *repopath = NULL;
237905
+
237905
+      repopath = g_file_get_path (ostree_repo_get_path (repo));
237905
+      g_prefix_error (error, _("While opening repository %s: "), repopath);
237905
+      return FALSE;
237905
+    }
237905
+
237905
+  return TRUE;
237905
+}
237905
+
237905
 static gboolean
237905
 _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
                           gboolean      allow_empty,
237905
@@ -4008,14 +4025,8 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
     }
237905
   else
237905
     {
237905
-      if (!ostree_repo_open (repo, cancellable, error))
237905
-        {
237905
-          g_autofree char *repopath = NULL;
237905
-
237905
-          repopath = g_file_get_path (repodir);
237905
-          g_prefix_error (error, _("While opening repository %s: "), repopath);
237905
-          return FALSE;
237905
-        }
237905
+      if (!ensure_repo_opened (repo, cancellable, error))
237905
+        return FALSE;
237905
     }
237905
 
237905
   /* In the system-helper case we're directly using the global repo, and we can't write any
237905
-- 
237905
2.34.1
237905
237905
237905
From 4c831cad673e3271aafcb03da717515aec197636 Mon Sep 17 00:00:00 2001
237905
From: Simon McVittie <smcv@collabora.com>
237905
Date: Tue, 8 Feb 2022 16:39:06 +0000
237905
Subject: [PATCH 4/7] dir: Use system helper to create system repo if necessary
237905
237905
Previously, if /var/lib/flatpak didn't exist then we would use the
237905
system helper to create and populate it, but if it existed and was empty,
237905
we could only populate it if we had privileges. This led to errors from
237905
libostree:
237905
237905
    Creating repo: mkdirat: Permission denied
237905
237905
The EnsureRepo method call is allowed by default for active local users,
237905
so do this even if allow_empty is true: this will incorporate
237905
/etc/flatpak/remotes.d into the repository, whether it is newly-created
237905
or not. This makes a `flatpak search` work immediately, without having
237905
to fetch metadata explicitly.
237905
237905
Signed-off-by: Simon McVittie <smcv@collabora.com>
237905
(cherry picked from commit 2489b915efae3a78ca7040c39161f8c304e4c7a5)
237905
---
237905
 common/flatpak-dir.c | 35 +++++++++++++++++++++++------------
237905
 1 file changed, 23 insertions(+), 12 deletions(-)
237905
237905
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
237905
index f44241c40290..aef3a75392dc 100644
237905
--- a/common/flatpak-dir.c
237905
+++ b/common/flatpak-dir.c
237905
@@ -4005,22 +4005,33 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
          that still user bare-user */
237905
       OstreeRepoMode mode = OSTREE_REPO_MODE_BARE_USER_ONLY;
237905
 
237905
-      if (!ostree_repo_create (repo, mode, cancellable, &my_error))
237905
+      if (flatpak_dir_use_system_helper (self, NULL))
237905
         {
237905
-          flatpak_rm_rf (repodir, cancellable, NULL);
237905
-
237905
-          if (allow_empty)
237905
-            return TRUE;
237905
+          if (!system_helper_maybe_ensure_repo (self, allow_empty, cancellable, error))
237905
+            return FALSE;
237905
 
237905
-          g_propagate_error (error, g_steal_pointer (&my_error));
237905
-          return FALSE;
237905
+          if (!ensure_repo_opened (repo, cancellable, error))
237905
+            return FALSE;
237905
         }
237905
-
237905
-      /* Create .changed file early to avoid polling non-existing file in monitor */
237905
-      if (!flatpak_dir_mark_changed (self, &my_error))
237905
+      else
237905
         {
237905
-          g_warning ("Error marking directory as changed: %s", my_error->message);
237905
-          g_clear_error (&my_error);
237905
+          if (!ostree_repo_create (repo, mode, cancellable, &my_error))
237905
+            {
237905
+              flatpak_rm_rf (repodir, cancellable, NULL);
237905
+
237905
+              if (allow_empty)
237905
+                return TRUE;
237905
+
237905
+              g_propagate_error (error, g_steal_pointer (&my_error));
237905
+              return FALSE;
237905
+            }
237905
+
237905
+          /* Create .changed file early to avoid polling non-existing file in monitor */
237905
+          if (!flatpak_dir_mark_changed (self, &my_error))
237905
+            {
237905
+              g_warning ("Error marking directory as changed: %s", my_error->message);
237905
+              g_clear_error (&my_error);
237905
+            }
237905
         }
237905
     }
237905
   else
237905
-- 
237905
2.34.1
237905
237905
237905
From 30b7778dc412143a8f06052298f63bdeb7bd5186 Mon Sep 17 00:00:00 2001
237905
From: Simon McVittie <smcv@collabora.com>
237905
Date: Tue, 8 Feb 2022 16:11:31 +0000
237905
Subject: [PATCH 5/7] dir: Avoid polkit prompts for EnsureRepo in most CLI
237905
 commands
237905
237905
If we are running a CLI command in the background, then EnsureRepo
237905
might require authorization. Silently skip it if allow_empty was true,
237905
as it is for commands that iterate through all repositories.
237905
237905
Signed-off-by: Simon McVittie <smcv@collabora.com>
237905
(cherry picked from commit 48f40d45045071c0e073061b72a1c32fb0562c3f)
237905
---
237905
 common/flatpak-dir.c | 14 ++++++++++----
237905
 1 file changed, 10 insertions(+), 4 deletions(-)
237905
237905
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
237905
index aef3a75392dc..597f8906b1ce 100644
237905
--- a/common/flatpak-dir.c
237905
+++ b/common/flatpak-dir.c
237905
@@ -3920,6 +3920,7 @@ apply_new_flatpakrepo (const char *remote_name,
237905
 
237905
 static gboolean
237905
 system_helper_maybe_ensure_repo (FlatpakDir *self,
237905
+                                 FlatpakHelperEnsureRepoFlags flags,
237905
                                  gboolean allow_empty,
237905
                                  GCancellable *cancellable,
237905
                                  GError **error)
237905
@@ -3928,7 +3929,7 @@ system_helper_maybe_ensure_repo (FlatpakDir *self,
237905
   const char *installation = flatpak_dir_get_id (self);
237905
 
237905
   if (!flatpak_dir_system_helper_call_ensure_repo (self,
237905
-                                                   FLATPAK_HELPER_ENSURE_REPO_FLAGS_NONE,
237905
+                                                   flags,
237905
                                                    installation ? installation : "",
237905
                                                    cancellable, &local_error))
237905
     {
237905
@@ -3970,15 +3971,20 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
   g_autoptr(GError) my_error = NULL;
237905
   g_autoptr(GFile) cache_dir = NULL;
237905
   g_autoptr(GHashTable) flatpakrepos = NULL;
237905
+  FlatpakHelperEnsureRepoFlags ensure_flags = FLATPAK_HELPER_ENSURE_REPO_FLAGS_NONE;
237905
 
237905
   if (self->repo != NULL)
237905
     return TRUE;
237905
 
237905
+  /* Don't trigger polkit prompts if we are just doing this opportunistically */
237905
+  if (allow_empty)
237905
+    ensure_flags |= FLATPAK_HELPER_ENSURE_REPO_FLAGS_NO_INTERACTION;
237905
+
237905
   if (!g_file_query_exists (self->basedir, cancellable))
237905
     {
237905
       if (flatpak_dir_use_system_helper (self, NULL))
237905
         {
237905
-          if (!system_helper_maybe_ensure_repo (self, allow_empty, cancellable, error))
237905
+          if (!system_helper_maybe_ensure_repo (self, ensure_flags, allow_empty, cancellable, error))
237905
             return FALSE;
237905
         }
237905
       else
237905
@@ -4007,7 +4013,7 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
 
237905
       if (flatpak_dir_use_system_helper (self, NULL))
237905
         {
237905
-          if (!system_helper_maybe_ensure_repo (self, allow_empty, cancellable, error))
237905
+          if (!system_helper_maybe_ensure_repo (self, ensure_flags, allow_empty, cancellable, error))
237905
             return FALSE;
237905
 
237905
           if (!ensure_repo_opened (repo, cancellable, error))
237905
@@ -4117,7 +4123,7 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
     {
237905
       if (flatpak_dir_use_system_helper (self, NULL))
237905
         {
237905
-          if (!system_helper_maybe_ensure_repo (self, allow_empty, cancellable, error))
237905
+          if (!system_helper_maybe_ensure_repo (self, ensure_flags, allow_empty, cancellable, error))
237905
             return FALSE;
237905
 
237905
           if (!ostree_repo_reload_config (repo, cancellable, error))
237905
-- 
237905
2.34.1
237905
237905
237905
From f2747123d890f836cc7f1c5c88edd67b8095f72b Mon Sep 17 00:00:00 2001
237905
From: Simon McVittie <smcv@collabora.com>
237905
Date: Tue, 8 Feb 2022 16:40:49 +0000
237905
Subject: [PATCH 6/7] dir: Include repo path in error message if unable to
237905
 create it
237905
237905
libostree makes heavy use of fd-based I/O, which has the disadvantage
237905
that it is rarely obvious what path an error message is referring to.
237905
237905
Signed-off-by: Simon McVittie <smcv@collabora.com>
237905
(cherry picked from commit d106384446ae57c78a76f4a0a0360797df62c31f)
237905
---
237905
 common/flatpak-dir.c | 14 +++++++++++++-
237905
 1 file changed, 13 insertions(+), 1 deletion(-)
237905
237905
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
237905
index 597f8906b1ce..f6a43aafa612 100644
237905
--- a/common/flatpak-dir.c
237905
+++ b/common/flatpak-dir.c
237905
@@ -4023,12 +4023,24 @@ _flatpak_dir_ensure_repo (FlatpakDir   *self,
237905
         {
237905
           if (!ostree_repo_create (repo, mode, cancellable, &my_error))
237905
             {
237905
+              const char *repo_path = flatpak_file_get_path_cached (repodir);
237905
+
237905
               flatpak_rm_rf (repodir, cancellable, NULL);
237905
 
237905
               if (allow_empty)
237905
                 return TRUE;
237905
 
237905
-              g_propagate_error (error, g_steal_pointer (&my_error));
237905
+              /* As of 2022, the error message from libostree is not the most helpful:
237905
+               * Creating repo: mkdirat: Permission denied
237905
+               * If the repository path is in the error message, assume this
237905
+               * has been fixed. If not, add it. */
237905
+              if (strstr (my_error->message, repo_path) != NULL)
237905
+                g_propagate_error (error, g_steal_pointer (&my_error));
237905
+              else
237905
+                g_set_error (error, my_error->domain, my_error->code,
237905
+                             "Unable to create repository at %s (%s)",
237905
+                             repo_path, my_error->message);
237905
+
237905
               return FALSE;
237905
             }
237905
 
237905
-- 
237905
2.34.1
237905
237905
237905
From 4772e9935f5a2b2178efa3005027b19e997ec8e5 Mon Sep 17 00:00:00 2001
237905
From: Simon McVittie <smcv@collabora.com>
237905
Date: Tue, 8 Feb 2022 14:47:24 +0000
237905
Subject: [PATCH 7/7] app: Make ALL_DIRS and STANDARD_DIRS imply OPTIONAL_REPO
237905
237905
It is already the case that when we are using ALL_DIRS, we always
237905
combine it with OPTIONAL_REPO, meaning no need to populate empty
237905
installations. ALL_DIRS is used for commands that iterate through all
237905
known installations to enumerate apps/runtimes, such as `flatpak run`
237905
and `flatpak list`; for these commands, it's reasonable to say that
237905
if the installation does not have a libostree repository, then that's
237905
equivalent to it having a libostree repository with no apps and no
237905
runtimes. Make this happen automatically if forgotten.
237905
237905
For STANDARD_DIRS, we were inconsistent about this: `flatpak remote-list`
237905
had OPTIONAL_REPO, but the other commands did not.
237905
STANDARD_DIRS is used for `flatpak create-usb`, and for all the commands
237905
that manipulate remotes.
237905
237905
For the commands that manipulate remotes, it seems reasonable to say
237905
that if an installation has no libostree repository and we are unable
237905
to create one, then that's equivalent to an installation with a
237905
libostree repository but no remotes.
237905
237905
Similarly, for create-usb, an installation where we are unable to create
237905
a libostree repository seems like it should be equivalent to an
237905
installation whose libostree repository does not contain any of the
237905
refs we are interested in.
237905
237905
Resolves: https://github.com/flatpak/flatpak/issues/4111
237905
Signed-off-by: Simon McVittie <smcv@collabora.com>
237905
(cherry picked from commit 3f144d1e02fa060680eba18a9dd81969682a3170)
237905
---
237905
 app/flatpak-builtins.h | 6 ++++--
237905
 app/flatpak-main.c     | 4 +++-
237905
 2 files changed, 7 insertions(+), 3 deletions(-)
237905
237905
diff --git a/app/flatpak-builtins.h b/app/flatpak-builtins.h
237905
index ec1ac4a4abdb..d925c8cd389e 100644
237905
--- a/app/flatpak-builtins.h
237905
+++ b/app/flatpak-builtins.h
237905
@@ -39,10 +39,12 @@ G_BEGIN_DECLS
237905
  * @FLATPAK_BUILTIN_FLAG_ONE_DIR: Allow a single --user/--system/--installation option
237905
  *    and return a single dir. If no option is specified, default to --system
237905
  * @FLATPAK_BUILTIN_FLAG_STANDARD_DIRS: Allow repeated use of --user/--system/--installation
237905
- *    and return multiple dirs. If no option is specified return system(default)+user
237905
+ *    and return multiple dirs. If no option is specified return system(default)+user.
237905
+ *    Implies %FLATPAK_BUILTIN_FLAG_OPTIONAL_REPO.
237905
  * @FLATPAK_BUILTIN_FLAG_ALL_DIRS: Allow repeated use of --user/--system/--installation
237905
  *    and return multiple dirs. If no option is specified, return all installations,
237905
- *    starting with system(default)+user
237905
+ *    starting with system(default)+user.
237905
+ *    Implies %FLATPAK_BUILTIN_FLAG_OPTIONAL_REPO.
237905
  *
237905
  * Flags affecting the behavior of flatpak_option_context_parse().
237905
  *
237905
diff --git a/app/flatpak-main.c b/app/flatpak-main.c
237905
index e79df52eff25..4b4d65673645 100644
237905
--- a/app/flatpak-main.c
237905
+++ b/app/flatpak-main.c
237905
@@ -460,7 +460,9 @@ flatpak_option_context_parse (GOptionContext     *context,
237905
         {
237905
           FlatpakDir *dir = g_ptr_array_index (dirs, i);
237905
 
237905
-          if (flags & FLATPAK_BUILTIN_FLAG_OPTIONAL_REPO)
237905
+          if (flags & (FLATPAK_BUILTIN_FLAG_OPTIONAL_REPO |
237905
+                       FLATPAK_BUILTIN_FLAG_ALL_DIRS |
237905
+                       FLATPAK_BUILTIN_FLAG_STANDARD_DIRS))
237905
             {
237905
               if (!flatpak_dir_maybe_ensure_repo (dir, cancellable, error))
237905
                 return FALSE;
237905
-- 
237905
2.34.1
237905