From 20e42b6a75780cff547441e7966e53536057b462 Mon Sep 17 00:00:00 2001 From: guinux Date: Fri, 22 Sep 2017 11:03:37 +0200 Subject: [PATCH] save cache options in pamac.conf --- .gitignore | 1 + Makefile | 2 ++ data/config/pamac.conf | 6 +++++ data/systemd/pamac-cleancache.service | 2 +- src/Makefile | 9 ++++++- src/clean_cache.vala | 32 +++++++++++++++++++++++ src/pamac_config.vala | 37 +++++++++++++++++++++++++++ src/preferences_dialog.vala | 19 ++++++++++++-- src/system_daemon.vala | 6 ++--- src/transaction.vala | 6 +++-- 10 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 src/clean_cache.vala diff --git a/.gitignore b/.gitignore index 28d5e38..70b1cbb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ src/*.c src/pamac.h src/pamac.vapi src/libpamac.so +src/pamac-clean-cache src/pamac-user-daemon src/pamac-system-daemon src/pamac-tray diff --git a/Makefile b/Makefile index 2b0e16d..56fd1d8 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ install: install_pamac-tray-appindicator install -Dm644 src/pamac.h $(includedir)/pamac.h install -Dm644 src/pamac.vapi $(datadir)/vala/vapi/pamac.vapi install -Dm755 src/libpamac.so $(libdir)/libpamac.so + install -Dm755 src/pamac-clean-cache $(bindir)/pamac-clean-cache install -Dm755 src/pamac-user-daemon $(bindir)/pamac-user-daemon install -Dm744 src/pamac-system-daemon $(bindir)/pamac-system-daemon install -Dm755 src/pamac-tray $(bindir)/pamac-tray @@ -66,6 +67,7 @@ uninstall: rm -f $(includedir)/pamac.h rm -f $(datadir)/vala/vapi/pamac.vapi rm -f $(libdir)/libpamac.so + rm -f $(bindir)/pamac-clean-cache rm -f $(bindir)/pamac-user-daemon rm -f $(bindir)/pamac-system-daemon rm -f $(bindir)/pamac-tray diff --git a/data/config/pamac.conf b/data/config/pamac.conf index 927d5ad..6e7f4f3 100644 --- a/data/config/pamac.conf +++ b/data/config/pamac.conf @@ -18,3 +18,9 @@ RefreshPeriod = 6 ## AUR build directory: BuildDirectory = /tmp + +## Number of versions of each package to keep in the cache: +KeepNumPackages = 3 + +## Remove only the versions of uninstalled packages when clean cache: +#OnlyRmUninstalled diff --git a/data/systemd/pamac-cleancache.service b/data/systemd/pamac-cleancache.service index 29e86f2..a6ce3ad 100644 --- a/data/systemd/pamac-cleancache.service +++ b/data/systemd/pamac-cleancache.service @@ -3,4 +3,4 @@ Description=Clean packages cache [Service] Type=oneshot -ExecStart=/usr/bin/paccache -r +ExecStart=/usr/bin/pamac-clean-cache diff --git a/src/Makefile b/src/Makefile index 62aab0a..024abbc 100644 --- a/src/Makefile +++ b/src/Makefile @@ -39,11 +39,18 @@ MANAGER_GRESOURCE_FILE = ../resources/pamac.manager.gresource.xml INSTALLER_GRESOURCE_FILE = ../resources/pamac.installer.gresource.xml -binaries: pamac-user-daemon pamac-system-daemon pamac-tray pamac-manager pamac-install +binaries: pamac-clean-cache pamac-user-daemon pamac-system-daemon pamac-tray pamac-manager pamac-install clean: rm -f *.c pamac.h pamac.vapi libpamac.so pamac-user-daemon pamac-system-daemon pamac-tray pamac-tray-appindicator pamac-manager pamac-install +pamac-clean-cache: pamac_config.vala clean_cache.vala + valac -o pamac-clean-cache \ + $(COMMON_VALA_FLAGS) \ + --pkg=gio-2.0 \ + pamac_config.vala \ + clean_cache.vala + pamac-tray: $(COMMON_SOURCES) alpm_config.vala tray.vala tray-gtk.vala valac -o pamac-tray \ $(COMMON_VALA_FLAGS) \ diff --git a/src/clean_cache.vala b/src/clean_cache.vala new file mode 100644 index 0000000..982335c --- /dev/null +++ b/src/clean_cache.vala @@ -0,0 +1,32 @@ +/* + * pamac-vala + * + * Copyright (C) 2014-2017 Guillaume Benoit + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a get of the GNU General Public License + * along with this program. If not, see . + */ + +int main () { + var pamac_config = new Pamac.Config ("/etc/pamac.conf"); + string rm_only_uninstalled_str = ""; + if (pamac_config.rm_only_uninstalled) { + rm_only_uninstalled_str = "-u"; + } + try { + Process.spawn_command_line_sync ("paccache -q --nocolor %s -r -k %llu".printf (rm_only_uninstalled_str, pamac_config.keep_num_pkgs)); + } catch (SpawnError e) { + stderr.printf ("SpawnError: %s\n", e.message); + } + return 0; +} diff --git a/src/pamac_config.vala b/src/pamac_config.vala index f910ba4..538935b 100644 --- a/src/pamac_config.vala +++ b/src/pamac_config.vala @@ -28,6 +28,8 @@ namespace Pamac { public bool enable_aur { get; private set; } public string aur_build_dir { get; private set; } public bool check_aur_updates { get; private set; } + public uint64 keep_num_pkgs { get; private set; } + public bool rm_only_uninstalled { get; private set; } public unowned HashTable environment_variables { get { return _environment_variables; @@ -72,6 +74,8 @@ namespace Pamac { enable_aur = false; aur_build_dir = "/tmp"; check_aur_updates = false; + keep_num_pkgs = 3; + rm_only_uninstalled = false; parse_file (conf_path); } @@ -103,6 +107,13 @@ namespace Pamac { unowned string val = splitted[1]._strip (); refresh_period = uint64.parse (val); } + } else if (key == "KeepNumPackages") { + if (splitted.length == 2) { + unowned string val = splitted[1]._strip (); + keep_num_pkgs = uint64.parse (val); + } + } else if (key == "OnlyRmUninstalled") { + rm_only_uninstalled = true; } else if (key == "NoUpdateHideIcon") { no_update_hide_icon = true; } else if (key == "EnableAUR") { @@ -157,6 +168,24 @@ namespace Pamac { } else { data.append (line + "\n"); } + } else if (line.contains ("KeepNumPackages")) { + if (new_conf.lookup_extended ("KeepNumPackages", null, out variant)) { + data.append ("KeepNumPackages = %llu\n".printf (variant.get_uint64 ())); + new_conf.remove ("KeepNumPackages"); + } else { + data.append (line + "\n"); + } + } else if (line.contains ("OnlyRmUninstalled")) { + if (new_conf.lookup_extended ("OnlyRmUninstalled", null, out variant)) { + if (variant.get_boolean ()) { + data.append ("OnlyRmUninstalled\n"); + } else { + data.append ("#OnlyRmUninstalled\n"); + } + new_conf.remove ("OnlyRmUninstalled"); + } else { + data.append (line + "\n"); + } } else if (line.contains ("NoUpdateHideIcon")) { if (new_conf.lookup_extended ("NoUpdateHideIcon", null, out variant)) { if (variant.get_boolean ()) { @@ -224,6 +253,14 @@ namespace Pamac { } } else if (key == "RefreshPeriod") { data.append ("RefreshPeriod = %llu\n".printf (val.get_uint64 ())); + } else if (key == "KeepNumPackages") { + data.append ("KeepNumPackages = %llu\n".printf (val.get_uint64 ())); + } else if (key == "OnlyRmUninstalled") { + if (val.get_boolean ()) { + data.append ("OnlyRmUninstalled\n"); + } else { + data.append ("#OnlyRmUninstalled\n"); + } } else if (key =="NoUpdateHideIcon") { if (val.get_boolean ()) { data.append ("NoUpdateHideIcon\n"); diff --git a/src/preferences_dialog.vala b/src/preferences_dialog.vala index dfb0eab..238d4d8 100644 --- a/src/preferences_dialog.vala +++ b/src/preferences_dialog.vala @@ -91,6 +91,8 @@ namespace Pamac { previous_refresh_period = transaction.refresh_period; } no_update_hide_icon_checkbutton.active = transaction.no_update_hide_icon; + cache_keep_nb_spin_button.value = transaction.keep_num_pkgs; + cache_only_uninstalled_checkbutton.active = transaction.rm_only_uninstalled; // populate ignorepkgs_liststore ignorepkgs_liststore = new Gtk.ListStore (1, typeof (string)); @@ -104,6 +106,8 @@ namespace Pamac { check_updates_button.state_set.connect (on_check_updates_button_state_set); refresh_period_spin_button.value_changed.connect (on_refresh_period_spin_button_value_changed); no_update_hide_icon_checkbutton.toggled.connect (on_no_update_hide_icon_checkbutton_toggled); + cache_keep_nb_spin_button.value_changed.connect (on_cache_keep_nb_spin_button_value_changed); + cache_only_uninstalled_checkbutton.toggled.connect (on_cache_only_uninstalled_checkbutton_toggled); transaction.write_pamac_config_finished.connect (on_write_pamac_config_finished); AlpmPackage pkg = transaction.find_installed_satisfier ("pacman-mirrors"); @@ -181,6 +185,18 @@ namespace Pamac { transaction.start_write_pamac_config (new_pamac_conf); } + void on_cache_keep_nb_spin_button_value_changed () { + var new_pamac_conf = new HashTable (str_hash, str_equal); + new_pamac_conf.insert ("KeepNumPackages", new Variant.uint64 (cache_keep_nb_spin_button.get_value_as_int ())); + transaction.start_write_pamac_config (new_pamac_conf); + } + + void on_cache_only_uninstalled_checkbutton_toggled () { + var new_pamac_conf = new HashTable (str_hash, str_equal); + new_pamac_conf.insert ("OnlyRmUninstalled", new Variant.boolean (cache_only_uninstalled_checkbutton.active)); + transaction.start_write_pamac_config (new_pamac_conf); + } + void on_no_update_hide_icon_checkbutton_toggled () { var new_pamac_conf = new HashTable (str_hash, str_equal); new_pamac_conf.insert ("NoUpdateHideIcon", new Variant.boolean (no_update_hide_icon_checkbutton.active)); @@ -365,8 +381,7 @@ namespace Pamac { [GtkCallback] void on_cache_clean_button_clicked () { - transaction.clean_cache ((uint) cache_keep_nb_spin_button.get_value_as_int (), - cache_only_uninstalled_checkbutton.active); + transaction.clean_cache (transaction.keep_num_pkgs, transaction.rm_only_uninstalled); } } } diff --git a/src/system_daemon.vala b/src/system_daemon.vala index c46735b..61e27b6 100644 --- a/src/system_daemon.vala +++ b/src/system_daemon.vala @@ -397,12 +397,12 @@ namespace Pamac { }); } - public void clean_cache (uint keep_nb, bool only_uninstalled, GLib.BusName sender) { + public void clean_cache (uint64 keep_nb, bool only_uninstalled, GLib.BusName sender) { check_authorization.begin (sender, (obj, res) => { bool authorized = check_authorization.end (res); if (authorized) { - string[] commands = {"paccache", "-rq"}; - commands += "-k%u".printf (keep_nb); + string[] commands = {"paccache", "--nocolor", "-rq"}; + commands += "-k%llu".printf (keep_nb); if (only_uninstalled) { commands += "-u"; } diff --git a/src/transaction.vala b/src/transaction.vala index e4707fe..39e00c5 100644 --- a/src/transaction.vala +++ b/src/transaction.vala @@ -63,7 +63,7 @@ namespace Pamac { public abstract void start_write_alpm_config (HashTable new_alpm_conf) throws IOError; public abstract void start_write_mirrors_config (HashTable new_mirrors_conf) throws IOError; public abstract void start_generate_mirrors_list () throws IOError; - public abstract void clean_cache (uint keep_nb, bool only_uninstalled) throws IOError; + public abstract void clean_cache (uint64 keep_nb, bool only_uninstalled) throws IOError; public abstract void start_set_pkgreason (string pkgname, uint reason) throws IOError; public abstract void start_refresh (bool force) throws IOError; public abstract void start_sysupgrade_prepare (bool enable_downgrade, string[] temporary_ignorepkgs) throws IOError; @@ -121,6 +121,8 @@ namespace Pamac { public bool recurse { get { return pamac_config.recurse; } } public uint64 refresh_period { get { return pamac_config.refresh_period; } } public string aur_build_dir { get { return pamac_config.aur_build_dir; } } + public uint64 keep_num_pkgs { get { return pamac_config.keep_num_pkgs; } } + public bool rm_only_uninstalled { get { return pamac_config.rm_only_uninstalled; } } //Alpm.TransFlag int flags; @@ -448,7 +450,7 @@ namespace Pamac { } } - public void clean_cache (uint keep_nb, bool only_uninstalled) { + public void clean_cache (uint64 keep_nb, bool only_uninstalled) { try { system_daemon.clean_cache (keep_nb, only_uninstalled); } catch (IOError e) {