[gnome-software: 7/18] gs-utils: Make creating cache directory optional




commit e28b24c69a1e9b9605b73821447584c7402a87e0
Author: Philip Withnall <pwithnall endlessos org>
Date:   Thu Mar 4 22:41:57 2021 +0000

    gs-utils: Make creating cache directory optional
    
    Add a new flag to `GsUtilsCacheFlags` which specifies to create the
    cache directory, and change the default to *not* create the directory.
    This means that, with appropriate flags, `gs_utils_get_cache_filename()`
    can guarantee to not set an error, which means it can be called from
    functions which cannot fail (or which cannot propagate an error).
    
    Change all the existing call sites to pass the new flag
    (`GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY`) where it makes sense to do so.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1147

 lib/gs-plugin.c                                    |  3 ++-
 lib/gs-self-test.c                                 |  6 ++++--
 lib/gs-utils.c                                     |  6 +++++-
 lib/gs-utils.h                                     |  2 ++
 plugins/core/gs-plugin-appstream.c                 |  3 ++-
 .../gs-plugin-external-appstream.c                 |  3 ++-
 .../fedora-langpacks/gs-plugin-fedora-langpacks.c  |  3 ++-
 plugins/fedora-langpacks/gs-self-test.c            |  3 ++-
 .../gs-plugin-fedora-pkgdb-collections.c           |  3 ++-
 plugins/flatpak/gs-flatpak.c                       |  3 ++-
 plugins/flatpak/gs-plugin-flatpak.c                |  3 ++-
 plugins/fwupd/gs-plugin-fwupd.c                    |  8 +++++---
 plugins/odrs/gs-plugin-odrs.c                      | 12 ++++++++----
 src/gs-screenshot-image.c                          | 22 ++++++++++++----------
 14 files changed, 52 insertions(+), 28 deletions(-)
---
diff --git a/lib/gs-plugin.c b/lib/gs-plugin.c
index d91cd496b..06c233987 100644
--- a/lib/gs-plugin.c
+++ b/lib/gs-plugin.c
@@ -1282,7 +1282,8 @@ gs_plugin_download_rewrite_resource_uri (GsPlugin *plugin,
        /* get cache location */
        cachefn = gs_utils_get_cache_filename ("cssresource", uri,
                                               GS_UTILS_CACHE_FLAG_WRITEABLE |
-                                              GS_UTILS_CACHE_FLAG_USE_HASH,
+                                              GS_UTILS_CACHE_FLAG_USE_HASH |
+                                              GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                               error);
        if (cachefn == NULL)
                return NULL;
diff --git a/lib/gs-self-test.c b/lib/gs-self-test.c
index af862b047..8c8fee93f 100644
--- a/lib/gs-self-test.c
+++ b/lib/gs-self-test.c
@@ -111,7 +111,8 @@ gs_utils_cache_func (void)
 
        fn1 = gs_utils_get_cache_filename ("test",
                                           "http://www.foo.bar/baz";,
-                                          GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                          GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                          GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                           &error);
        g_assert_no_error (error);
        g_assert_cmpstr (fn1, !=, NULL);
@@ -121,7 +122,8 @@ gs_utils_cache_func (void)
        fn2 = gs_utils_get_cache_filename ("test",
                                           "http://www.foo.bar/baz";,
                                           GS_UTILS_CACHE_FLAG_WRITEABLE |
-                                          GS_UTILS_CACHE_FLAG_USE_HASH,
+                                          GS_UTILS_CACHE_FLAG_USE_HASH |
+                                          GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                           &error);
        g_assert_no_error (error);
        g_assert_cmpstr (fn2, !=, NULL);
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index 4528ee5d4..2fbc4184f 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -141,6 +141,9 @@ gs_utils_filename_array_return_newest (GPtrArray *array)
  * -- gnome-software will not ever clean the cache for the plugin.
  * For this reason it is a good idea to use the plugin name as @kind.
  *
+ * This function can only fail if %GS_UTILS_CACHE_FLAG_ENSURE_EMPTY or
+ * %GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY are passed in @flags.
+ *
  * Returns: The full path and filename, which may or may not exist, or %NULL
  **/
 gchar *
@@ -197,7 +200,8 @@ gs_utils_get_cache_filename (const gchar *kind,
                if (!gs_utils_rmtree (cachedir, error))
                        return NULL;
        }
-       if (!g_file_query_exists (cachedir_file, NULL) &&
+       if ((flags & GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY) &&
+           !g_file_query_exists (cachedir_file, NULL) &&
            !g_file_make_directory_with_parents (cachedir_file, NULL, error))
                return NULL;
        g_ptr_array_add (candidates, g_build_filename (cachedir, basename, NULL));
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index ae9deb8bb..1d655676e 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -22,6 +22,7 @@ G_BEGIN_DECLS
  * @GS_UTILS_CACHE_FLAG_WRITEABLE:     A writable directory is required
  * @GS_UTILS_CACHE_FLAG_USE_HASH:      Prefix a hash to the filename
  * @GS_UTILS_CACHE_FLAG_ENSURE_EMPTY:  Clear existing cached items
+ * @GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY:      Create the cache directory (Since: 40)
  *
  * The cache flags.
  **/
@@ -30,6 +31,7 @@ typedef enum {
        GS_UTILS_CACHE_FLAG_WRITEABLE           = 1 << 0,
        GS_UTILS_CACHE_FLAG_USE_HASH            = 1 << 1,
        GS_UTILS_CACHE_FLAG_ENSURE_EMPTY        = 1 << 2,
+       GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY    = 1 << 3,
        GS_UTILS_CACHE_FLAG_LAST  /*< skip >*/
 } GsUtilsCacheFlags;
 
diff --git a/plugins/core/gs-plugin-appstream.c b/plugins/core/gs-plugin-appstream.c
index 5effadd73..6bf7c99d3 100644
--- a/plugins/core/gs-plugin-appstream.c
+++ b/plugins/core/gs-plugin-appstream.c
@@ -589,7 +589,8 @@ gs_plugin_appstream_check_silo (GsPlugin *plugin,
 
        /* create per-user cache */
        blobfn = gs_utils_get_cache_filename ("appstream", "components.xmlb",
-                                             GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                             GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                             GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                              error);
        if (blobfn == NULL)
                return FALSE;
diff --git a/plugins/external-appstream/gs-plugin-external-appstream.c 
b/plugins/external-appstream/gs-plugin-external-appstream.c
index 4a1c9e62a..5eee5497e 100644
--- a/plugins/external-appstream/gs-plugin-external-appstream.c
+++ b/plugins/external-appstream/gs-plugin-external-appstream.c
@@ -156,7 +156,8 @@ gs_plugin_external_appstream_refresh_sys (GsPlugin *plugin,
         * the system */
        tmp_file_path = gs_utils_get_cache_filename ("external-appstream",
                                                     file_name,
-                                                    GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                                    GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                                    GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                     error);
        if (tmp_file_path == NULL)
                return FALSE;
diff --git a/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c 
b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c
index 26534548b..0bbb93615 100644
--- a/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c
+++ b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c
@@ -79,7 +79,8 @@ gs_plugin_add_langpacks (GsPlugin *plugin,
        /* per-user cache */
        langpack_pkgname = g_strconcat ("langpacks-", language_code, NULL);
        cachefn = gs_utils_get_cache_filename ("langpacks", langpack_pkgname,
-                                              GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                              GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                              GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                               error);
        if (cachefn == NULL)
                return FALSE;
diff --git a/plugins/fedora-langpacks/gs-self-test.c b/plugins/fedora-langpacks/gs-self-test.c
index df15e33d5..895f2f8b0 100644
--- a/plugins/fedora-langpacks/gs-self-test.c
+++ b/plugins/fedora-langpacks/gs-self-test.c
@@ -32,7 +32,8 @@ gs_plugins_fedora_langpacks_func (GsPluginLoader *plugin_loader)
 
        /* start with a clean slate */
        cachefn = gs_utils_get_cache_filename ("langpacks", "langpacks-ja",
-                                              GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                              GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                              GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                               &error);
        g_assert_no_error (error);
        g_unlink (cachefn);
diff --git a/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c 
b/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c
index c26f846c8..21c187b11 100644
--- a/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c
+++ b/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c
@@ -114,7 +114,8 @@ gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
        /* get the file to cache */
        priv->cachefn = gs_utils_get_cache_filename ("fedora-pkgdb-collections",
                                                     "fedora.json",
-                                                    GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                                    GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                                    GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                     error);
        if (priv->cachefn == NULL)
                return FALSE;
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 128ba46b7..12bfc9ae1 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -901,7 +901,8 @@ gs_flatpak_rescan_appstream_store (GsFlatpak *self,
        /* create per-user cache */
        blobfn = gs_utils_get_cache_filename (gs_flatpak_get_id (self),
                                              "components.xmlb",
-                                             GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                             GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                             GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                              error);
        if (blobfn == NULL)
                return FALSE;
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index 64e1898ff..231de9bcf 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -1272,7 +1272,8 @@ gs_plugin_flatpak_create_temporary (GsPlugin *plugin, GCancellable *cancellable,
        installation_path = gs_utils_get_cache_filename ("flatpak",
                                                         "installation-tmp",
                                                         GS_UTILS_CACHE_FLAG_WRITEABLE |
-                                                        GS_UTILS_CACHE_FLAG_ENSURE_EMPTY,
+                                                        GS_UTILS_CACHE_FLAG_ENSURE_EMPTY |
+                                                        GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                         error);
        if (installation_path == NULL)
                return NULL;
diff --git a/plugins/fwupd/gs-plugin-fwupd.c b/plugins/fwupd/gs-plugin-fwupd.c
index b46f572ba..cda9093ec 100644
--- a/plugins/fwupd/gs-plugin-fwupd.c
+++ b/plugins/fwupd/gs-plugin-fwupd.c
@@ -442,7 +442,7 @@ gs_plugin_fwupd_new_app (GsPlugin *plugin, FwupdDevice *dev, GError **error)
        basename = g_path_get_basename (update_uri);
        filename_cache = gs_utils_get_cache_filename ("fwupd",
                                                      basename,
-                                                     GS_UTILS_CACHE_FLAG_NONE,
+                                                     GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                      error);
        if (filename_cache == NULL)
                return NULL;
@@ -697,7 +697,8 @@ gs_plugin_fwupd_refresh_remote (GsPlugin *plugin,
        cache_id = g_strdup_printf ("fwupd/remotes.d/%s", fwupd_remote_get_id (remote));
        basename_sig = g_path_get_basename (fwupd_remote_get_filename_cache_sig (remote));
        filename_sig = gs_utils_get_cache_filename (cache_id, basename_sig,
-                                                   GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                                   GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                                   GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                    error);
 
        /* download the signature first, it's smaller */
@@ -738,7 +739,8 @@ gs_plugin_fwupd_refresh_remote (GsPlugin *plugin,
        /* download the payload and save to file */
        basename = g_path_get_basename (fwupd_remote_get_filename_cache (remote));
        filename = gs_utils_get_cache_filename (cache_id, basename,
-                                               GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                               GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                               GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                error);
        if (filename == NULL)
                return FALSE;
diff --git a/plugins/odrs/gs-plugin-odrs.c b/plugins/odrs/gs-plugin-odrs.c
index 86a115568..f0c0c3b91 100644
--- a/plugins/odrs/gs-plugin-odrs.c
+++ b/plugins/odrs/gs-plugin-odrs.c
@@ -282,7 +282,8 @@ gs_plugin_refresh (GsPlugin *plugin,
        /* check cache age */
        cache_filename = gs_utils_get_cache_filename ("odrs",
                                                      "ratings.json",
-                                                     GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                                     GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                                     GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                      error);
        if (cache_filename == NULL)
                return FALSE;
@@ -661,7 +662,8 @@ gs_plugin_odrs_refine_ratings (GsPlugin *plugin,
                   when refresh/download disabled on start */
                cache_filename = gs_utils_get_cache_filename ("odrs",
                                                              "ratings.json",
-                                                             GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                                             GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                                             GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                              error);
 
                if (!cache_filename ||
@@ -770,7 +772,8 @@ gs_plugin_odrs_fetch_for_app (GsPlugin *plugin, GsApp *app, GError **error)
        cachefn_basename = g_strdup_printf ("%s.json", gs_app_get_id (app));
        cachefn = gs_utils_get_cache_filename ("odrs",
                                               cachefn_basename,
-                                              GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                              GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                              GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                               error);
        if (cachefn == NULL)
                return NULL;
@@ -1007,7 +1010,8 @@ gs_plugin_odrs_invalidate_cache (AsReview *review, GError **error)
                                            as_review_get_metadata_item (review, "app_id"));
        cachefn = gs_utils_get_cache_filename ("odrs",
                                               cachefn_basename,
-                                              GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                              GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                              GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                               error);
        if (cachefn == NULL)
                return FALSE;
diff --git a/src/gs-screenshot-image.c b/src/gs-screenshot-image.c
index a3b585675..d445a7ae5 100644
--- a/src/gs-screenshot-image.c
+++ b/src/gs-screenshot-image.c
@@ -277,7 +277,8 @@ gs_screenshot_image_save_downloaded_img (GsScreenshotImage *ssimg,
        size_dir = g_strdup_printf ("%ux%u", width, height);
        cache_kind = g_build_filename ("screenshots", size_dir, NULL);
        filename = gs_utils_get_cache_filename (cache_kind, basename,
-                                               GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                               GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                               GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                &error_local);
 
         if (filename == NULL) {
@@ -530,12 +531,7 @@ gs_screenshot_image_load_async (GsScreenshotImage *ssimg,
                                                       basename,
                                                       GS_UTILS_CACHE_FLAG_NONE,
                                                       NULL);
-       if (ssimg->filename == NULL) {
-               /* TRANSLATORS: this is when we try create the cache directory
-                * but we were out of space or permission was denied */
-               gs_screenshot_image_set_error (ssimg, _("Could not create cache"));
-               return;
-       }
+       g_assert (ssimg->filename != NULL);
 
        /* does local file already exist and has recently been downloaded */
        if (g_file_test (ssimg->filename, G_FILE_TEST_EXISTS)) {
@@ -573,8 +569,7 @@ gs_screenshot_image_load_async (GsScreenshotImage *ssimg,
                                                             basename_thumb,
                                                             GS_UTILS_CACHE_FLAG_NONE,
                                                             NULL);
-               if (cachefn_thumb == NULL)
-                       return;
+               g_assert (cachefn_thumb != NULL);
                if (g_file_test (cachefn_thumb, G_FILE_TEST_EXISTS))
                        gs_screenshot_image_show_blurred (ssimg, cachefn_thumb);
        }
@@ -584,8 +579,15 @@ gs_screenshot_image_load_async (GsScreenshotImage *ssimg,
        g_free (ssimg->filename);
        ssimg->filename = gs_utils_get_cache_filename (cache_kind,
                                                       basename,
-                                                      GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                                      GS_UTILS_CACHE_FLAG_WRITEABLE |
+                                                      GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
                                                       NULL);
+       if (ssimg->filename == NULL) {
+               /* TRANSLATORS: this is when we try create the cache directory
+                * but we were out of space or permission was denied */
+               gs_screenshot_image_set_error (ssimg, _("Could not create cache"));
+               return;
+       }
 
        /* download file */
        g_debug ("downloading %s to %s", url, ssimg->filename);


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