[pacman-dev] [PATCH 06/10] makepkg: less code repetition when sanitizing variable contents

Andres P aepd87 at gmail.com
Fri Jun 25 19:16:41 EDT 2010


During check_sanity, use regex and abstract the series of variable checks into
a list.

Also add descriptive error message to exceptions involving backup array
members, given that "invalid backup entry" isn't all that communicative.

Signed-off-by: Andres P <aepd87 at gmail.com>
---
 scripts/makepkg.sh.in |   55 +++++++++++++++++-------------------------------
 1 files changed, 20 insertions(+), 35 deletions(-)

diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index 81a842e..0958797 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -1170,6 +1170,19 @@ install_package() {
 	fi
 }
 
+var_lint() {
+	local pattern="$1"
+	local message="$2"
+	shift 2
+
+	local i
+	for i; do
+		[[ $i =~ $pattern ]] || continue
+		error "$(gettext "%s")" "$message"
+		return 1
+	done
+}
+
 check_sanity() {
 	# check for no-no's in the build script
 	local i
@@ -1179,27 +1192,15 @@ check_sanity() {
 			return 1
 		fi
 	done
+		
+	var_lint '-' "pkgver is not allowed to contain hyphens" "$pkgver"
+	var_lint '-' "pkgrel is not allowed to contain hyphens" "$pkgrel"
 
-	local name
-	for name in "${pkgname[@]}"; do
-		if [[ ${name:0:1} = "-" ]]; then
-			error "$(gettext "%s is not allowed to start with a hyphen.")" "pkgname"
-			return 1
-		fi
-	done
+	var_lint '^-' "pkgname entries are not allowed to start with a hyphen" "${pkgname[@]}"
+	var_lint '^-' "pkgbase is not allowed to start with a hyphen" "$pkgbase"
 
-	if [[ ${pkgbase:0:1} = "-" ]]; then
-		error "$(gettext "%s is not allowed to start with a hyphen.")" "pkgbase"
-		return 1
-	fi
-	if [[ $pkgver != ${pkgver//-/} ]]; then
-		error "$(gettext "%s is not allowed to contain hyphens.")" "pkgver"
-		return 1
-	fi
-	if [[ $pkgrel != ${pkgrel//-/} ]]; then
-		error "$(gettext "%s is not allowed to contain hyphens.")" "pkgrel"
-		return 1
-	fi
+	var_lint '[<>]' "provides entries are not allowed to contain comparison (< or >) characters" "${provides[@]}"
+	var_lint '^/'   "backup entries are not allowed to start with a slash" "${backup[@]}"
 
 	if [[ $arch != 'any' ]]; then
 		if ! in_array $CARCH ${arch[@]}; then
@@ -1212,22 +1213,6 @@ check_sanity() {
 		fi
 	fi
 
-	local provide
-	for provide in ${provides[@]}; do
-		if [[ $provide != ${provide//</} || $provide != ${provide//>/} ]]; then
-			error "$(gettext "Provides array cannot contain comparison (< or >) operators.")"
-			return 1
-		fi
-	done
-
-	local file
-	for file in "${backup[@]}"; do
-		if [[ ${file:0:1} = "/" ]]; then
-			error "$(gettext "Invalid backup entry : %s")" "$file"
-			return 1
-		fi
-	done
-
 	local optdepend
 	for optdepend in "${optdepends[@]}"; do
 		local pkg=${optdepend%%:*}
-- 
1.7.1



More information about the pacman-dev mailing list