[pacman-dev] [PATCH] pacdiff using pacman db v2

Jonathan Frazier eyeswide at gmail.com
Sun Jun 30 20:00:08 EDT 2013


 Implement the default search using the local pacman db.
 Use @sysconfdir@/pacman.conf to find the local db 
 Search for pacsave.0 files.

Signed-off-by: Jonathan Frazier <eyeswide at gmail.com>
---
 contrib/pacdiff.sh.in | 86 ++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 64 insertions(+), 22 deletions(-)

diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in
index 47779d6..ee80c9c 100644
--- a/contrib/pacdiff.sh.in
+++ b/contrib/pacdiff.sh.in
@@ -21,9 +21,8 @@
 declare -r myname='pacdiff'
 declare -r myver='@PACKAGE_VERSION@'
 
-diffprog=${DIFFPROG:-vimdiff}
-diffsearchpath=${DIFFSEARCHPATH:-/etc}
-locate=0
+diffprog=${DIFF_PROG:-vimdiff}
+difffindpath="${DIFF_FIND_PATH:-/etc}"
 USE_COLOR='y'
 
 m4_include(../scripts/library/output_format.sh)
@@ -32,11 +31,19 @@ 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"
+	echo ""
+	echo "Usage : $myname [-lf]"
+	echo "  -l/--locate makes $myname search using locate"
+	echo "  -f/--find   makes $myname search using find"
+	echo ""
+	echo "Note: the default search looks for backup files in the local pacman db"
+	echo "      this generally will not find *.pacsave files"
+	echo ""
+	echo "  DIFF_PROG variable will override the default editor: vimdiff"
+	echo "  DIFF_FIND_PATH will override the default path when using --find"
+	echo ""
+	echo "Example : DIFF_PROG=meld $myname"
+	echo "Example : DIFF_FIND_PATH=\"/boot /etc /usr\" $myname -f"
 }
 
 version() {
@@ -45,27 +52,61 @@ version() {
 	echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev at archlinux.org>'
 }
 
-cmd() {
-	if [ $locate -eq 1 ]; then
-		locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave
-	else
-		find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave \) -print0
-	fi
-}
-
 if [ $# -gt 0 ]; then
 	case $1 in
 		-l|--locate)
-		locate=1;;
+			locate=$(type -P locate);;
+		-f|--find)
+			find=$(type -P find);;
 		-V|--version)
-		version; exit 0;;
+			version; exit 0;;
 		-h|--help)
-		usage; exit 0;;
+			usage; exit 0;;
 		*)
-		usage; exit 1;;
+			usage; exit 1;;
 	esac
 fi
 
+if [[ ! -n $locate && ! -n $find ]]; 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:- at localstatedir@/lib/pacman/}local"
+	if [[ ! -d "${pac_db}" ]]; then
+	  error "unable to read pacman db %s". "${pac_db}"
+		usage; exit 1
+	fi
+fi
+
+check_backup() {
+  [[ -f "$1" ]] && printf "$1"'\0'
+}
+
+cmd() {
+	if [[ -n $locate ]]; then
+		locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]'
+	elif [[ -n $find ]]; then
+		find "${difffindpath}" \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]' \) -print0 2> /dev/null
+	else
+		# parse local pacman db for backup files and look for pac* based on them
+		awk '/^%BACKUP%$/ {
+			while (getline) {
+				if (/^$/) { nextfile }
+				print $1
+			}
+		}' "${pac_db}"/*/files | while read -r bkup; do
+			check_backup "/$bkup.pacnew"
+			check_backup "/$bkup.pacorig"
+			for f in "/$bkup.pacsave"{,.{0..9}}; do
+				check_backup "$f"
+			done
+		done
+	fi
+}
+
 # see http://mywiki.wooledge.org/BashFAQ/020
 while IFS= read -u 3 -r -d '' pacfile; do
 	file="${pacfile%.pac*}"
@@ -82,16 +123,17 @@ while IFS= read -u 3 -r -d '' pacfile; do
 		msg2 "Files are identical, removing..."
 		rm -v "$pacfile"
 	else
-		ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s: [v/s/r/o] " "$file_type" "$file_type"
+		ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
 		while read c; do
 			case $c in
+				q|Q) exit 1;;
 				r|R) rm -v "$pacfile"; break ;;
 				o|O) mv -v "$pacfile" "$file"; break ;;
 				v|V)
 				$diffprog "$pacfile" "$file"
 				rm -iv "$pacfile"; break ;;
 				s|S) break ;;
-				*) ask "Invalid answer. Try again: [v/s/r/o] "; continue ;;
+				*) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
 			esac
 		done
 	fi
-- 
1.8.3.1



More information about the pacman-dev mailing list