From 110ba0ccf9b9e93fe6f26d19c4b578100fd9757c Mon Sep 17 00:00:00 2001 From: Ronan Pigott Date: Mon, 17 Jul 2023 16:24:36 -0700 Subject: [PATCH] zsh: update default caching policy for units MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The existing caching policy was completely bogus. In the first stanza, despite the comment, the pattern given would consider the cache invalid if it was more than 1 hour old. The second stanza was also incorrect, since the output of `systemctl --all` is not unit file paths, but unit names. When they were being tested against the cachefile mtime, the test would always fail becuase of the nonexistant file (hopefully). In fact it's not very useful to test if the unit files have newer mtime in this case anyway, since we are only caching their names. Also, `systemctl --all` is an unfortunately slow operation to be used in testing for the cache validity — we want this operation to at least be faster than rebuilding the cache. I've rewritten this stanza with my best guess at its original intent. It now checks against the mtime of the parent directories in the search path, which should be updated and cause the cache to rebuild when we add, remove, or rename any unit files. --- shell-completion/zsh/_systemctl.in | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/shell-completion/zsh/_systemctl.in b/shell-completion/zsh/_systemctl.in index 5a6e7d391d..8c004c3bb1 100644 --- a/shell-completion/zsh/_systemctl.in +++ b/shell-completion/zsh/_systemctl.in @@ -424,20 +424,13 @@ done (( $+functions[_systemctl_caching_policy] )) || _systemctl_caching_policy() { - local _sysunits - local -a oldcache - # rebuild if cache is more than a day old - oldcache=( "$1"(mh+1) ) - (( $#oldcache )) && return 0 + [[ -n $1(#qNmd+1) ]] && return 0 - _sysunits=(${${(f)"$(__systemctl --all)"}%% *}) - - if (( $#_sysunits )); then - for unit in $_sysunits; do - [[ "$unit" -nt "$1" ]] && return 0 - done - fi + local pathkind=systemd-search-${1##*--}-unit + for dir in ${(s-:-)^$(_call_program $pathkind systemd-path $pathkind)}; do + [[ $dir -nt $1 ]] && return 0 + done return 1 }