[gnome-software/1296-calculate-app-space-usage-for-download-context-tile] flatpak: Calculate cache and user data sizes for installed applications



commit eea751bee77d8e59560315af140b7324addb6d67
Author: Milan Crha <mcrha redhat com>
Date:   Wed Jul 28 19:45:21 2021 +0200

    flatpak: Calculate cache and user data sizes for installed applications
    
    It can be done, because the directories for the user data and the cache data
    are known.
    
    Closes https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1296

 plugins/flatpak/gs-flatpak.c | 52 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 7e9c571f8..1d6b9f958 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -17,6 +17,7 @@
 #include <config.h>
 
 #include <glib/gi18n.h>
+#include <glib/gstdio.h>
 #include <xmlb.h>
 
 #include "gs-appstream.h"
@@ -2514,6 +2515,42 @@ gs_flatpak_prune_addons_list (GsFlatpak *self,
        return TRUE;
 }
 
+static guint64
+gs_flatpak_get_app_directory_size (GsApp *app,
+                                  const gchar *subdir_name,
+                                  GCancellable *cancellable)
+{
+       GSList *dirs_to_do = NULL;
+       guint64 size = 0;
+
+       dirs_to_do = g_slist_prepend (dirs_to_do, g_build_filename (g_get_home_dir (), ".var", "app", 
gs_app_get_id (app), subdir_name, NULL));
+       while (dirs_to_do != NULL && !g_cancellable_is_cancelled (cancellable)) {
+               g_autofree gchar *path = dirs_to_do->data;
+               g_autoptr(GDir) dir = NULL;
+
+               dirs_to_do = g_slist_remove (dirs_to_do, path);
+
+               dir = g_dir_open (path, 0, NULL);
+               if (dir) {
+                       const gchar *name;
+                       while (name = g_dir_read_name (dir), name != NULL && !g_cancellable_is_cancelled 
(cancellable)) {
+                               g_autofree gchar *full_path = g_build_filename (path, name, NULL);
+                               GStatBuf st;
+
+                               if (g_stat (full_path, &st) == 0) {
+                                       if (S_ISDIR (st.st_mode)) {
+                                               dirs_to_do = g_slist_prepend (dirs_to_do, g_steal_pointer 
(&full_path));
+                                       } else {
+                                               size += st.st_size;
+                                       }
+                               }
+                       }
+               }
+       }
+       g_slist_free_full (dirs_to_do, g_free);
+       return size;
+}
+
 static gboolean
 gs_plugin_refine_item_size (GsFlatpak *self,
                            GsApp *app,
@@ -3007,6 +3044,21 @@ gs_flatpak_refine_app_unlocked (GsFlatpak *self,
                }
        }
 
+       if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE_DATA) != 0 &&
+           gs_app_is_installed (app) &&
+           gs_app_get_kind (app) != AS_COMPONENT_KIND_RUNTIME) {
+               if (gs_app_get_size_cache_data (app) == GS_APP_SIZE_UNKNOWABLE)
+                       gs_app_set_size_cache_data (app, gs_flatpak_get_app_directory_size (app, "cache", 
cancellable));
+               if (gs_app_get_size_user_data (app) == GS_APP_SIZE_UNKNOWABLE)
+                       gs_app_set_size_user_data (app, gs_flatpak_get_app_directory_size (app, "config", 
cancellable) +
+                                                       gs_flatpak_get_app_directory_size (app, "data", 
cancellable));
+
+               if (g_cancellable_is_cancelled (cancellable)) {
+                       gs_app_set_size_cache_data (app, GS_APP_SIZE_UNKNOWABLE);
+                       gs_app_set_size_user_data (app, GS_APP_SIZE_UNKNOWABLE);
+               }
+       }
+
        /* origin-hostname */
        if (flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ORIGIN_HOSTNAME) {
                if (!gs_plugin_refine_item_origin_hostname (self, app,


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