[gnome-software/1909-external-appstream-installer-leaves-old-files-behind-in-legacy-locations] gs-install-appstream: Remove old files from legacy path



commit b949a2c3d3594d39391e6ad19f72d41d44e5575c
Author: Milan Crha <mcrha redhat com>
Date:   Thu Oct 20 17:54:09 2022 +0200

    gs-install-appstream: Remove old files from legacy path
    
    Since the path the external appstream data files are stored changed,
    users with old files updating gnome-software to a version with the changed
    path would have left the old files with no chance to have them deleted
    other than manually by themselves. This tries to delete the file in
    the legacy path first and moves the file to the new location.
    
    Related to https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1909

 gs-install-appstream/gs-install-appstream.c | 10 ++++++++++
 lib/gs-external-appstream-utils.c           |  9 +++++++++
 lib/gs-external-appstream-utils.h           |  2 ++
 3 files changed, 21 insertions(+)
---
diff --git a/gs-install-appstream/gs-install-appstream.c b/gs-install-appstream/gs-install-appstream.c
index 995eeff02..95a940624 100644
--- a/gs-install-appstream/gs-install-appstream.c
+++ b/gs-install-appstream/gs-install-appstream.c
@@ -22,12 +22,14 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <locale.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 
 #include <xmlb.h>
 #include <glib/gi18n.h>
+#include <glib/gstdio.h>
 
 #include "gs-external-appstream-utils.h"
 
@@ -35,10 +37,18 @@ static gboolean
 gs_install_appstream_move_file (GFile *file, GError **error)
 {
        g_autofree gchar *basename = g_file_get_basename (file);
+       g_autofree gchar *legacy_cachefn = gs_external_appstream_utils_get_legacy_file_cache_path (basename);
        g_autofree gchar *cachefn = gs_external_appstream_utils_get_file_cache_path (basename);
        g_autoptr(GFile) cachefn_file = g_file_new_for_path (cachefn);
        g_autoptr(GFile) cachedir_file = g_file_get_parent (cachefn_file);
 
+       /* Try to cleanup the old cache directory, but do not panic, when it fails */
+       if (g_unlink (legacy_cachefn) == -1) {
+               int errn = errno;
+               if (errn != ENOENT)
+                       g_debug ("Failed to unlink '%s': %s", legacy_cachefn, g_strerror (errn));
+       }
+
        /* make sure the parent directory exists, but if not then create with
         * the ownership and permissions of the current process */
        if (!g_file_query_exists (cachedir_file, NULL)) {
diff --git a/lib/gs-external-appstream-utils.c b/lib/gs-external-appstream-utils.c
index e22b642b0..e9e0a12f8 100644
--- a/lib/gs-external-appstream-utils.c
+++ b/lib/gs-external-appstream-utils.c
@@ -66,6 +66,15 @@ gs_external_appstream_utils_get_file_cache_path (const gchar *file_name)
        return g_build_filename (APPSTREAM_SYSTEM_DIR, prefixed_file_name, NULL);
 }
 
+/* To be able to delete old files, when the path changed */
+gchar *
+gs_external_appstream_utils_get_legacy_file_cache_path (const gchar *file_name)
+{
+       g_autofree gchar *prefixed_file_name = g_strdup_printf (EXTERNAL_APPSTREAM_PREFIX "-%s",
+                                                               file_name);
+       return g_build_filename (LOCALSTATEDIR "/cache/app-info/xmls", prefixed_file_name, NULL);
+}
+
 const gchar *
 gs_external_appstream_utils_get_system_dir (void)
 {
diff --git a/lib/gs-external-appstream-utils.h b/lib/gs-external-appstream-utils.h
index 17876114a..63e67f855 100644
--- a/lib/gs-external-appstream-utils.h
+++ b/lib/gs-external-appstream-utils.h
@@ -38,6 +38,8 @@ GQuark                 gs_external_appstream_error_quark (void);
 
 const gchar    *gs_external_appstream_utils_get_system_dir (void);
 gchar          *gs_external_appstream_utils_get_file_cache_path (const gchar   *file_name);
+gchar          *gs_external_appstream_utils_get_legacy_file_cache_path (const gchar *file_name);
+
 void            gs_external_appstream_refresh_async (guint64                     cache_age_secs,
                                                      GsDownloadProgressCallback  progress_callback,
                                                      gpointer                    progress_user_data,


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