Skip to content

Instantly share code, notes, and snippets.

@albertored11
Last active August 25, 2023 16:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save albertored11/bfc0068f4e43ca0d7ce0af968f7314db to your computer and use it in GitHub Desktop.
Save albertored11/bfc0068f4e43ca0d7ce0af968f7314db to your computer and use it in GitHub Desktop.
Script and hook to clean pacman and yay cache
#!/usr/bin/env bash
# Assuming yay is run by user with UID 1000
admin="$(id -nu 1000)"
cachedir="/home/$admin/.cache/yay"
removed="$(comm -23 <(basename -a $(find $cachedir -mindepth 1 -maxdepth 1 -type d) | sort) <(pacman -Qqm) | xargs -r printf "$cachedir/%s\n")"
# Remove yay cache for foreign packages that are not installed anymore
rm -rf $removed
pkgcache="$(find $cachedir -mindepth 1 -maxdepth 1 -type d | xargs -r printf "-c %s\n")"
for pkgdir in "$cachedir"/*/; do
pkgname=$(basename "$pkgdir")
# Remove untracked files (e. g. source/build files) excepting package files and main source files for installed version if non-git package
if [[ ! "$pkgname" =~ ^.*-git$ ]]; then
pkgver="$(pacman -Q $pkgname | cut -d ' ' -f2 | cut -d '-' -f1 | cut -d ':' -f2)"
cd "$pkgdir"
rm -f $(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir/%s\n")
fi
rm -rf "$pkgdir"/src/
done
# Remove everything for uninstalled foreign packages, keep latest version for uninstalled native packages, keep two latest versions for installed packages
/usr/bin/paccache -qruk0 $pkgcache
/usr/bin/paccache -qruk1
/usr/bin/paccache -qrk2 -c /var/cache/pacman/pkg $pkgcache
[Trigger]
Operation = Upgrade
Operation = Remove
Type = Package
Target = *
[Action]
Description = Cleaning pacman and yay cache...
When = PostTransaction
Exec = /home/herbort/.local/bin/yaycache
Depends = pacman-contrib
@albertored11
Copy link
Author

@albertored11
Copy link
Author

In yaycache.hook, replace Exec path with the actual path where you put yaycache script.

@xfzv
Copy link

xfzv commented Feb 9, 2021

Hi,

Thanks for the script, it works fine with paru too after some minor adjustements.

This command is giving me some issues with devel packages though:

rm -f $(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir%s\n")

It seems that it deletes way too much stuff in ~/.cache/paru/clone/package_name/git_repo_name/.

As a result, both paru and yay fail when trying to reinstall or update devel packages and output the following error:

# Example: when trying to reinstall paru-git
ERROR: /home/"${USER}"/.cache/paru/clone/paru-git/paru is not a clone of https://github.com/morganamilo/paru
Aborting...

tree ~/.cache/paru/clone/paru-git/paru:

Before cleaning

├── branches
├── config
├── description
├── FETCH_HEAD
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-merge-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── push-to-checkout.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
│       ├── pack-10f0d98de6ae92d224633b508b0b505d9360f817.idx
│       └── pack-10f0d98de6ae92d224633b508b0b505d9360f817.pack
├── packed-refs
└── refs
    ├── heads
    └── tags

9 directories, 21 files

After cleaning

.
├── branches
├── hooks
├── info
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

9 directories, 0 files

Trying to reinstall paru-git after the cleaning:

  • paru -Syu --devel paru-git:
[sudo] password for user:
no new news
:: Synchronizing package databases...
testing is up to date
core is up to date
extra is up to date
community-testing is up to date
community is up to date
multilib-testing is up to date
multilib is up to date
:: Looking for AUR upgrades
:: Looking for devel upgrades
:: Resolving dependencies...
:: Calculating conflicts...
:: Calculating inner conflicts...

Aur (1) paru-git-1.2.2.r0.g19882b7-1

:: Proceed to review? [Y/n]: y

:: Downloading PKGBUILDs...
PKGBUILDs up to date
nothing new to review
:: Proceed with installation? [Y/n]: y
fetching devel info...
==> Making package: paru-git 1.2.2.r14.ga927cfd-1 (Tue Feb 9 2021 15:10:06)
==> Retrieving sources...
==> ERROR: /home/user/.cache/paru/clone/paru-git/paru is not a clone of https://github.com/morganamilo/paru
   Aborting...
:: Packages failed to build: paru-git-1.2.2.r0.g19882b7-1

Same error with yay:

  • yay -Syu --devel paru-git:
:: Synchronizing package databases...
 testing is up to date
 core is up to date
 extra is up to date
 community-testing is up to date
 community                                                  5,4 MiB   352 KiB/s 00:16 [-------------------------------------------------] 100%
 multilib-testing is up to date
 multilib is up to date
:: Starting full system upgrade...
 there is nothing to do
:: Searching databases for updates...
:: Searching AUR for updates...
:: Checking development packages...
:: Checking for conflicts...
:: Checking for inner conflicts...
[Aur:1]  paru-git-1.2.2.r0.g19882b7-1

  1 paru-git                         (Installed) (Build Files Exist)
==> Packages to cleanBuild?
==> [N]one [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)
==>
:: PKGBUILD up to date, Skipping (1/1): paru-git
  1 paru-git                         (Installed) (Build Files Exist)
==> Diffs to show?
==> [N]one [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)
==> n
:: (1/1) Parsing SRCINFO: paru-git
==> Making package: paru-git 1.2.2.r0.g19882b7-1 (Tue Feb 9 2021 15:32:16)
==> Retrieving sources...
==> ERROR: /home/user/.cache/yay/paru-git/paru is not a clone of https://github.com/morganamilo/paru
    Aborting...
error downloading sources: paru-git

Any idea?

Cheers.

@albertored11
Copy link
Author

Hi, @xfzv. Sorry for taking so long to reply. Thanks for taking a look at my script and for your feedback, I really appreciate that.

Just like you said, that line breaks directories of git packages. I also noticed, but honestly at that time I was too lazy to check what was wrong and I just stopped using it.

I've just updated the script, so that command only runs for non-git packages, and I changed other parts of the code, too. Take a look, I tested it and it should work fine.

@xfzv
Copy link

xfzv commented Mar 14, 2021

I've just tried again with your last changes and it works just fine. Thank you very much!

@albertored11
Copy link
Author

Little fix in line 23.

@xfzv
Copy link

xfzv commented Apr 13, 2022

As a result of this, after updating to Git 2.35.2, line 23 fails with the following error for each AUR package directory (when the script is run as root with the pacman hook):

fatal: unsafe repository ('/home/xfzv/.cache/paru/clone/aur-pkg-name' is owned by someone else)
To add an exception for this directory, call:

        git config --global --add safe.directory /home/xfzv/.cache/paru/clone/aur-pkg-name

Adding an exception as suggested (for root git config) works as expected but I'd like to avoid doing this if possible, not to mention it should be done for each AUR package. It would be a pain to maintain in the long run. Edit: Git 2.35.3 now includes safe.directory="*" to completely disable this security check.

I guess there are other solutions such as chowning ~/.cache/paru/clone/ to root:root but it's obviously a terrible idea. Another one I come up with is to run the git ls-files command as regular user with:

su $(id -nu 1000) -c "rm -f $(git ls-files --others [...])"

but for some reasons I keep getting the same error with the full rm -f $(git ls-files ..) command.

I also tried modifying the pacman hook so that the script is run as regular user:

[Action]
Exec = /bin/su xfzv -c "/home/xfzv/.local/scripts/paccache.sh"

But I'm getting an error:

==> Privilege escalation required
==> ERROR: Unable to escalate privileges using sudo
error: command failed to execute correctly

Hopefully you can come with a workaround!

Thanks in advance for your time. 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment