[pacman-dev] [PATCH v2 1/9] pacdiff: Search and give warnings for older pacsave.[0-9]* files
Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index 47779d6..5ce6140 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -25,6 +25,7 @@ diffprog=${DIFFPROG:-vimdiff} diffsearchpath=${DIFFSEARCHPATH:-/etc} locate=0 USE_COLOR='y' +declare -a oldsaves m4_include(../scripts/library/output_format.sh) @@ -47,9 +48,9 @@ version() { cmd() { if [ $locate -eq 1 ]; then - locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave + locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*' else - find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave \) -print0 + find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0 fi } @@ -71,6 +72,12 @@ while IFS= read -u 3 -r -d '' pacfile; do file="${pacfile%.pac*}" file_type="pac${pacfile##*.pac}" + # add matches for pacsave.N to oldsaves array, do not prompt + if [[ $file_type = pacsave.+([0-9]) ]]; then + oldsaves+=("$pacfile") + continue + fi + msg "%s file found for %s" "$file_type" "$file" if [ ! -f "$file" ]; then warning "$file does not exist" @@ -97,6 +104,8 @@ while IFS= read -u 3 -r -d '' pacfile; do fi done 3< <(cmd) +(( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}" + exit 0 # vim: set ts=2 sw=2 noet: -- 1.8.3.2
Clean up and reword --help get rid of all the echos to make it easier to read in source. Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index 5ce6140..6ff8a72 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -32,12 +32,22 @@ m4_include(../scripts/library/output_format.sh) m4_include(../scripts/library/term_colors.sh) usage() { - echo "$myname : a simple pacnew/pacorig/pacsave updater" - echo "Usage : $myname [-l]" - echo " -l/--locate makes $myname use locate rather than find" - echo " DIFFPROG variable allows to override the default vimdiff" - echo " DIFFSEARCHPATH allows to override the default /etc path" - echo "Example : DIFFPROG=meld DIFFSEARCHPATH=\"/boot /etc /usr\" $myname" + cat <<EOF +$myname is a simple pacnew/pacorig/pacsave updater. + +Usage: $myname [-l] + +Options: + -l/--locate scan using locate (default: find) + +Enviroment Variables: + DIFFPROG override the merge program: (default: vimdiff) + DIFFSEARCHPATH override the search path. (only when using find) + (default: /etc) + +Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname + +EOF } version() { -- 1.8.3.2
loop over arguments, this will allow adding options such as --nocolor Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index 6ff8a72..bcaf686 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -64,8 +64,8 @@ cmd() { fi } -if [ $# -gt 0 ]; then - case $1 in +while [[ -n "$1" ]]; do + case "$1" in -l|--locate) locate=1;; -V|--version) @@ -75,7 +75,8 @@ if [ $# -gt 0 ]; then *) usage; exit 1;; esac -fi + shift +done # see http://mywiki.wooledge.org/BashFAQ/020 while IFS= read -u 3 -r -d '' pacfile; do -- 1.8.3.2
Allow colors to be disabled for use on broken/serial terminals. Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index bcaf686..b42f68c 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -29,8 +29,6 @@ declare -a oldsaves m4_include(../scripts/library/output_format.sh) -m4_include(../scripts/library/term_colors.sh) - usage() { cat <<EOF $myname is a simple pacnew/pacorig/pacsave updater. @@ -39,6 +37,7 @@ Usage: $myname [-l] Options: -l/--locate scan using locate (default: find) + --nocolor remove colors from output Enviroment Variables: DIFFPROG override the merge program: (default: vimdiff) @@ -68,6 +67,8 @@ while [[ -n "$1" ]]; do case "$1" in -l|--locate) locate=1;; + --nocolor) + USE_COLOR='n' ;; -V|--version) version; exit 0;; -h|--help) @@ -78,6 +79,8 @@ while [[ -n "$1" ]]; do shift done +m4_include(../scripts/library/term_colors.sh) + # see http://mywiki.wooledge.org/BashFAQ/020 while IFS= read -u 3 -r -d '' pacfile; do file="${pacfile%.pac*}" -- 1.8.3.2
change cmd tests to if (( FIND ))... as it is cleaner. All search cmds have an option and a variable initialized to zero. the active option should be set to 1. Add a switch to exclude multiple search options. set the default when all are equal to zero. Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index b42f68c..f106c17 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -23,9 +23,9 @@ declare -r myver='@PACKAGE_VERSION@' diffprog=${DIFFPROG:-vimdiff} diffsearchpath=${DIFFSEARCHPATH:-/etc} -locate=0 USE_COLOR='y' declare -a oldsaves +declare -i FIND=0 LOCATE=0 m4_include(../scripts/library/output_format.sh) @@ -33,10 +33,13 @@ usage() { cat <<EOF $myname is a simple pacnew/pacorig/pacsave updater. -Usage: $myname [-l] +Usage: $myname [-l | -f] [--nocolor] -Options: - -l/--locate scan using locate (default: find) +Search Options: select one, default: find + -l/--locate scan using locate + -f/--find scan using find + +General Options: --nocolor remove colors from output Enviroment Variables: @@ -56,9 +59,9 @@ version() { } cmd() { - if [ $locate -eq 1 ]; then + if (( LOCATE )); then locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*' - else + elif (( FIND )); then find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0 fi } @@ -66,21 +69,29 @@ cmd() { while [[ -n "$1" ]]; do case "$1" in -l|--locate) - locate=1;; + LOCATE=1;; + -f|--find) + FIND=1;; --nocolor) - USE_COLOR='n' ;; + USE_COLOR='n' ;; -V|--version) - version; exit 0;; + version; exit 0;; -h|--help) - usage; exit 0;; + usage; exit 0;; *) - usage; exit 1;; + usage; exit 1;; esac shift done m4_include(../scripts/library/term_colors.sh) +case $(( FIND+LOCATE )) in + 0) FIND=1;; # set the default search option + [^1]) error "Only one search option may be used at a time" + usage; exit 1;; +esac + # see http://mywiki.wooledge.org/BashFAQ/020 while IFS= read -u 3 -r -d '' pacfile; do file="${pacfile%.pac*}" -- 1.8.3.2
On 23/07/13 11:22, Jonathan Frazier wrote:
change cmd tests to if (( FIND ))... as it is cleaner. All search cmds have an option and a variable initialized to zero. the active option should be set to 1. Add a switch to exclude multiple search options. set the default when all are equal to zero.
Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-)
diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index b42f68c..f106c17 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -23,9 +23,9 @@ declare -r myver='@PACKAGE_VERSION@'
diffprog=${DIFFPROG:-vimdiff} diffsearchpath=${DIFFSEARCHPATH:-/etc} -locate=0 USE_COLOR='y' declare -a oldsaves +declare -i FIND=0 LOCATE=0
I changed these on my working branch to USE_FIND and USE_LOCATE as that is less likely to clash with environmental variables.
m4_include(../scripts/library/output_format.sh)
@@ -33,10 +33,13 @@ usage() { cat <<EOF $myname is a simple pacnew/pacorig/pacsave updater.
-Usage: $myname [-l] +Usage: $myname [-l | -f] [--nocolor]
-Options: - -l/--locate scan using locate (default: find) +Search Options: select one, default: find + -l/--locate scan using locate + -f/--find scan using find + +General Options: --nocolor remove colors from output
Enviroment Variables: @@ -56,9 +59,9 @@ version() { }
cmd() { - if [ $locate -eq 1 ]; then + if (( LOCATE )); then locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*' - else + elif (( FIND )); then find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0 fi } @@ -66,21 +69,29 @@ cmd() { while [[ -n "$1" ]]; do case "$1" in -l|--locate) - locate=1;; + LOCATE=1;; + -f|--find) + FIND=1;; --nocolor) - USE_COLOR='n' ;; + USE_COLOR='n' ;; -V|--version) - version; exit 0;; + version; exit 0;; -h|--help) - usage; exit 0;; + usage; exit 0;; *) - usage; exit 1;; + usage; exit 1;; esac shift done
m4_include(../scripts/library/term_colors.sh)
+case $(( FIND+LOCATE )) in + 0) FIND=1;; # set the default search option + [^1]) error "Only one search option may be used at a time" + usage; exit 1;; +esac + # see http://mywiki.wooledge.org/BashFAQ/020 while IFS= read -u 3 -r -d '' pacfile; do file="${pacfile%.pac*}"
On Tue, Jul 23, 2013 at 9:27 AM, Allan McRae <allan@archlinux.org> wrote: [...]
I changed these on my working branch to USE_FIND and USE_LOCATE as that is less likely to clash with environmental variables.
the tenor among bash coders was that application layer variables should be lowercase in general. So, you say you went too far down the other road? cheers! mar77i
This is a new search type, using -p or --pacmandb options. It reads config file locations directly from the local pacman db. It will find active configs anywhere they are defined in installed packages. It is not dependant on outside configs such as updatedb.conf or scanning a large set of directories for find. This will find more pacnews than find when searching with the current default of /etc, and it is faster than both find and updatedb when searching the entire fs. When run directly after an update, the local db is more likely to be cached than all files in /etc or / as other methods read. This will increase performance further post upgrade. After a package is removed and a pacsave is created, this method will not find these pacsaves until the base config is added to the local db again. These files have no influence in a working system and only take up a few blocks of disk space. Active configs need to be dealt with immediately to keep a system working. pacsaves related to removed configs can remain for weeks or months without problems. I would recommend occasionally running other methods such as --locate to remove them. Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 52 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index f106c17..8f1a0e7 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -18,6 +18,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +shopt -s extglob + declare -r myname='pacdiff' declare -r myver='@PACKAGE_VERSION@' @@ -25,7 +27,7 @@ diffprog=${DIFFPROG:-vimdiff} diffsearchpath=${DIFFSEARCHPATH:-/etc} USE_COLOR='y' declare -a oldsaves -declare -i FIND=0 LOCATE=0 +declare -i FIND=0 LOCATE=0 PACDB=0 m4_include(../scripts/library/output_format.sh) @@ -33,11 +35,12 @@ usage() { cat <<EOF $myname is a simple pacnew/pacorig/pacsave updater. -Usage: $myname [-l | -f] [--nocolor] +Usage: $myname [-l | -f | -p] [--nocolor] Search Options: select one, default: find -l/--locate scan using locate -f/--find scan using find + -p/--pacmandb scan active config files from pacman db General Options: --nocolor remove colors from output @@ -58,11 +61,32 @@ version() { echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>' } +print_existing() { + [[ -f "$1" ]] && printf '%s\0' "$1" +} + +print_existing_pacsave(){ + for f in "${1}"?(.+([0-9])); do + [[ -f $f ]] && printf '%s\0' "$f" + done +} + cmd() { if (( LOCATE )); then locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*' elif (( FIND )); then find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0 + elif (( PACDB )); then + awk '/^%BACKUP%$/ { + while (getline) { + if (/^$/) { nextfile } + print $1 + } + }' "${pac_db}"/*/files | while read -r bkup; do + print_existing "/$bkup.pacnew" + print_existing "/$bkup.pacorig" + print_existing_pacsave "/$bkup.pacsave" + done fi } @@ -72,8 +96,10 @@ while [[ -n "$1" ]]; do LOCATE=1;; -f|--find) FIND=1;; + -p|--pacmandb) + PACDB=1;; --nocolor) - USE_COLOR='n' ;; + USE_COLOR='n';; -V|--version) version; exit 0;; -h|--help) @@ -86,12 +112,26 @@ done m4_include(../scripts/library/term_colors.sh) -case $(( FIND+LOCATE )) in +case $(( FIND+LOCATE+PACDB )) in 0) FIND=1;; # set the default search option [^1]) error "Only one search option may be used at a time" usage; exit 1;; esac +if (( PACDB )); then + if [[ ! -r @sysconfdir@/pacman.conf ]]; then + error "unable to read @sysconfdir@/pacman.conf" + usage; exit 1 + fi + + eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) + pac_db="${DBPath:-@localstatedir@/lib/pacman/}local" + if [[ ! -d "${pac_db}" ]]; then + error "unable to read pacman db %s". "${pac_db}" + usage; exit 1 + fi +fi + # see http://mywiki.wooledge.org/BashFAQ/020 while IFS= read -u 3 -r -d '' pacfile; do file="${pacfile%.pac*}" @@ -120,8 +160,8 @@ while IFS= read -u 3 -r -d '' pacfile; do r|R) rm -v "$pacfile"; break ;; o|O) mv -v "$pacfile" "$file"; break ;; v|V) - $diffprog "$pacfile" "$file" - rm -iv "$pacfile"; break ;; + $diffprog "$pacfile" "$file" + rm -iv "$pacfile"; break ;; s|S) break ;; *) ask "Invalid answer. Try again: [v/s/r/o] "; continue ;; esac -- 1.8.3.2
Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index 8f1a0e7..e76ad4b 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -113,7 +113,7 @@ done m4_include(../scripts/library/term_colors.sh) case $(( FIND+LOCATE+PACDB )) in - 0) FIND=1;; # set the default search option + 0) PACDB=1;; # set the default search option [^1]) error "Only one search option may be used at a time" usage; exit 1;; esac -- 1.8.3.2
On 23/07/13 11:22, Jonathan Frazier wrote:
Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index 8f1a0e7..e76ad4b 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -113,7 +113,7 @@ done m4_include(../scripts/library/term_colors.sh)
case $(( FIND+LOCATE+PACDB )) in - 0) FIND=1;; # set the default search option + 0) PACDB=1;; # set the default search option [^1]) error "Only one search option may be used at a time" usage; exit 1;; esac
You did not update the --help output to reflect this. Done on my working branch. Allan
This is an option to just echo's the pacnews/pacsaves instead of merging or removing them. This can be used to check the config status such as in a cron job without modifying the system. Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index e76ad4b..6b5edb9 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -27,7 +27,7 @@ diffprog=${DIFFPROG:-vimdiff} diffsearchpath=${DIFFSEARCHPATH:-/etc} USE_COLOR='y' declare -a oldsaves -declare -i FIND=0 LOCATE=0 PACDB=0 +declare -i FIND=0 LOCATE=0 PACDB=0 OUTPUTONLY=0 m4_include(../scripts/library/output_format.sh) @@ -43,6 +43,7 @@ Search Options: select one, default: find -p/--pacmandb scan active config files from pacman db General Options: + -o/--output print files instead of merging them --nocolor remove colors from output Enviroment Variables: @@ -51,6 +52,7 @@ Enviroment Variables: (default: /etc) Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname +Example: $myname --output --locate EOF } @@ -98,6 +100,8 @@ while [[ -n "$1" ]]; do FIND=1;; -p|--pacmandb) PACDB=1;; + -o|--output) + OUTPUTONLY=1;; --nocolor) USE_COLOR='n';; -V|--version) @@ -136,6 +140,11 @@ fi while IFS= read -u 3 -r -d '' pacfile; do file="${pacfile%.pac*}" file_type="pac${pacfile##*.pac}" + + if (( OUTPUTONLY )); then + echo "$pacfile" + continue + fi # add matches for pacsave.N to oldsaves array, do not prompt if [[ $file_type = pacsave.+([0-9]) ]]; then -- 1.8.3.2
Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index 6b5edb9..349f6b9 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -28,6 +28,7 @@ diffsearchpath=${DIFFSEARCHPATH:-/etc} USE_COLOR='y' declare -a oldsaves declare -i FIND=0 LOCATE=0 PACDB=0 OUTPUTONLY=0 +declare -i UPDATEDB=0 m4_include(../scripts/library/output_format.sh) @@ -122,6 +123,20 @@ case $(( FIND+LOCATE+PACDB )) in usage; exit 1;; esac +if (( LOCATE )) && (( ! OUTPUTONLY )); then + ask "Run updatedb? [y/n] " + while read c; do + case $c in + Y|y) UPDATEDB=1; break;; #&& (error "updatedb failed"; exit 1);; + N|n) break;; + *) ask "Invalid answer. Try again: [y/n] "; continue ;; + esac + done +fi +if (( UPDATEDB )); then + updatedb || error "updatedb failed" +fi + if (( PACDB )); then if [[ ! -r @sysconfdir@/pacman.conf ]]; then error "unable to read @sysconfdir@/pacman.conf" -- 1.8.3.2
On Mon, Jul 22, 2013 at 09:23:00PM -0400, Jonathan Frazier wrote:
Signed-off-by: Jonathan Frazier <eyeswide@gmail.com> --- contrib/pacdiff.sh.in | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index 6b5edb9..349f6b9 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -28,6 +28,7 @@ diffsearchpath=${DIFFSEARCHPATH:-/etc} USE_COLOR='y' declare -a oldsaves declare -i FIND=0 LOCATE=0 PACDB=0 OUTPUTONLY=0 +declare -i UPDATEDB=0
m4_include(../scripts/library/output_format.sh)
@@ -122,6 +123,20 @@ case $(( FIND+LOCATE+PACDB )) in usage; exit 1;; esac
+if (( LOCATE )) && (( ! OUTPUTONLY )); then + ask "Run updatedb? [y/n] " + while read c; do + case $c in + Y|y) UPDATEDB=1; break;; #&& (error "updatedb failed"; exit 1);;
I'm guessing you commented this out and added the "workaround" below because you didn't understand why the 'exit 1' didn't do anything. You've created a subshell, so calling exit will just exit from the subshell. You can use { ... ; } instead which groups commands without creating a subshell. Better yet, just inline the updatedb call if you really want to go that route. I don't see why the indirection is needed/wanted.
+ N|n) break;; + *) ask "Invalid answer. Try again: [y/n] "; continue ;; + esac + done +fi +if (( UPDATEDB )); then + updatedb || error "updatedb failed"
If you're going to throw an error, you should probably exit. Otherwise, throw a warning instead and point out that results may not be accurate. All that said, I don't really care for this feature.
+fi + if (( PACDB )); then if [[ ! -r @sysconfdir@/pacman.conf ]]; then error "unable to read @sysconfdir@/pacman.conf" -- 1.8.3.2
participants (4)
-
Allan McRae
-
Dave Reisner
-
Jonathan Frazier
-
Martti Kühne