[PATCH setup] Add view mode "Unneeded"

Christian Franke Christian.Franke@t-online.de
Tue Aug 2 12:17:26 GMT 2022


In long standing cygwin installations, many no longer needed 
automatically installed packages (e.g. libicuNN) accumulate. This patch 
adds a new view which is possibly helpful to cleanup packages manually.

Some possible later enhancements:
- automatically refresh this view (a few seconds) after the user changed 
a package status as this may add or remove entries.
- add a keyboard shortcut (^U) to the list view for "Uninstall this 
package and then select next package"

-- 
Regards,
Christian

-------------- next part --------------
From 07a2e561b7ba19817a0bebb2cbf542a9b18a6d4e Mon Sep 17 00:00:00 2001
From: Christian Franke <christian.franke@t-online.de>
Date: Tue, 2 Aug 2022 13:54:30 +0200
Subject: [PATCH] Add view mode "Unneeded"

This view shows installed packages which could be safely removed.
---
 PickView.cc   | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 PickView.h    |  1 +
 res/en/res.rc |  1 +
 res/fr/res.rc |  1 +
 resource.h    |  1 +
 5 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/PickView.cc b/PickView.cc
index c961b9f..b7a37b5 100644
--- a/PickView.cc
+++ b/PickView.cc
@@ -17,6 +17,7 @@
 #include "PickPackageLine.h"
 #include "PickCategoryLine.h"
 #include <algorithm>
+#include <set>
 #include <limits.h>
 #include <shlwapi.h>
 
@@ -28,6 +29,40 @@
 #include "LogSingleton.h"
 #include "Exception.h"
 
+// Scan installed or desired packages and collect the names of packages
+// which provide the dependencies of other packages or are member of
+// category "Base".
+static void FindNeededPackages (const packagedb & db, std::set<std::string> & needed)
+{
+  std::map<std::string, std::string> providedBy;
+  for (const auto & p : db.packages)
+    {
+      const packagemeta & pkg = *p.second;
+      if (!pkg.isBinary ())
+        continue;
+      if (!(pkg.desired && (pkg.installed || pkg.picked ())))
+        continue;
+      for (const PackageSpecification *s : pkg.desired.provides ())
+        providedBy.insert ({s->packageName (), pkg.name});
+    }
+  for (const auto & p : db.packages)
+    {
+      const packagemeta & pkg = *p.second;
+      if (!pkg.isBinary ())
+        continue;
+      if (pkg.categories.count ("Base"))
+        needed.insert (pkg.name);
+      if (!(pkg.desired && (pkg.installed || pkg.picked ())))
+        continue;
+      for (const PackageSpecification *s : pkg.desired.depends ()) {
+        const auto i = providedBy.find (s->packageName ());
+        if (i == providedBy.end ())
+          continue;
+        needed.insert (i->second);
+      }
+    }
+}
+
 void
 PickView::setViewMode (views mode)
 {
@@ -47,6 +82,10 @@ PickView::setViewMode (views mode)
     }
   else
     {
+      std::set<std::string> needed;
+      if (view_mode == PickView::views::PackageUnneeded)
+        FindNeededPackages (db, needed);
+
       // iterate through every package
       for (packagedb::packagecollection::iterator i = db.packages.begin ();
             i != db.packages.end (); ++i)
@@ -77,7 +116,11 @@ PickView::setViewMode (views mode)
 
               // "UserPick" : installed packages that were picked by user
               || (view_mode == PickView::views::PackageUserPicked &&
-                  (pkg.installed && pkg.user_picked)))
+                  (pkg.installed && pkg.user_picked))
+
+              // "Unneeded" : installed packages which could be safely removed
+              || (view_mode == PickView::views::PackageUnneeded &&
+                  (pkg.installed && !needed.count (pkg.name))))
             {
               // Filter by package name
               if (packageFilterString.empty ()
@@ -111,6 +154,8 @@ PickView::mode_caption (views mode)
       return IDS_VIEW_NOTINSTALLED;
     case views::PackageUserPicked:
       return IDS_VIEW_PICKED;
+    case views::PackageUnneeded:
+      return IDS_VIEW_UNNEEDED;
     case views::Category:
       return IDS_VIEW_CATEGORY;
     default:
diff --git a/PickView.h b/PickView.h
index 1e14a74..f2dbfa9 100644
--- a/PickView.h
+++ b/PickView.h
@@ -36,6 +36,7 @@ public:
     PackageKeeps,
     PackageSkips,
     PackageUserPicked,
+    PackageUnneeded,
     Category,
   };
 
diff --git a/res/en/res.rc b/res/en/res.rc
index 644b252..9517bb2 100644
--- a/res/en/res.rc
+++ b/res/en/res.rc
@@ -578,6 +578,7 @@ BEGIN
     IDS_VIEW_UPTODATE "Up To Date"
     IDS_VIEW_NOTINSTALLED "Not Installed"
     IDS_VIEW_PICKED "Picked"
+    IDS_VIEW_UNNEEDED "Unneeded"
     IDS_VIEW_CATEGORY "Category"
     IDS_COLUMN_PACKAGE "Package"
     IDS_COLUMN_CURRENT "Current"
diff --git a/res/fr/res.rc b/res/fr/res.rc
index a0a7909..d787fdb 100644
--- a/res/fr/res.rc
+++ b/res/fr/res.rc
@@ -564,6 +564,7 @@ BEGIN
     IDS_VIEW_UPTODATE "À jour"
     IDS_VIEW_NOTINSTALLED "Non installé"
     IDS_VIEW_PICKED "Sélectionné"
+    // IDS_VIEW_UNNEEDED "XXX: missing translation"
     IDS_VIEW_CATEGORY  "Catégorie"
     IDS_COLUMN_PACKAGE "Paquet"
     IDS_COLUMN_CURRENT "Actuel"
diff --git a/resource.h b/resource.h
index 2668dd9..8c33794 100644
--- a/resource.h
+++ b/resource.h
@@ -105,6 +105,7 @@
 #define IDS_FILE_INUSE_MSG               1208
 #define IDS_DEPRECATED_WINDOWS_VERSION   1209
 #define IDS_DEPRECATED_WINDOWS_ARCH      1210
+#define IDS_VIEW_UNNEEDED                1211
 
 #define IDS_HELPTEXT_COMPACTOS           1500
 #define IDS_HELPTEXT_PUBKEY              1501
-- 
2.37.1



More information about the Cygwin-apps mailing list