From 22abe27bfa4f4e6c3ce3dfc345bb45a1f0666ab8 Mon Sep 17 00:00:00 2001 From: lolilolicon Date: Thu, 6 Oct 2011 14:14:46 +0800 Subject: bacman: use globbing to get local package db path The original code- pkg_dir="$(echo $pac_db/$pkg_name-[0-9]*)" is problematic in several ways: - $pac_db and $pkg_name should be quoted, obviously. - It assumes pkgver always starts with an integer, while in fact it just can't contain ':' and '-'. Counterexample: the code breaks on lshw B.02.15-1. - It assumes there are no more than one directory matching the pattern. While this should be the case if everything works perfectly, it certainly relies on external conditions. Counterexample: if the local db contains two packages named foo and foo-3g, even if everything else is perfect, the code will match two directories. Don't make assumptions, use what is known. Signed-off-by: lolilolicon --- contrib/bacman.in | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/contrib/bacman.in b/contrib/bacman.in index adc2cb36..96360911 100755 --- a/contrib/bacman.in +++ b/contrib/bacman.in @@ -93,8 +93,8 @@ pkg_dest="${PKGDEST:-$PWD}" pkg_pkger=${PACKAGER:-'Unknown Packager'} pkg_name="$1" -pkg_dir="$(echo $pac_db/$pkg_name-[0-9]*)" -pkg_namver="${pkg_dir##*/}" +pkg_dir=("$pac_db/$pkg_name"-+([^-])-+([0-9])) +pkg_namver=("${pkg_dir[@]##*/}") # # Checks everything is in place @@ -104,8 +104,16 @@ if [[ ! -d $pac_db ]]; then exit 1 fi +if (( ${#pkg_dir[@]} != 1 )); then + printf "ERROR: %d entries for package %s found in pacman database\n" \ + ${#pkg_dir[@]} "${pkg_name}" + printf "%s\n" "${pkg_dir[@]}" + exit 1 +fi + if [[ ! -d $pkg_dir ]]; then - echo "ERROR: package ${pkg_name} not found in pacman database" + printf "ERROR: package %s is found in pacman database,\n" "${pkg_name}" + printf " but \`%s' is not a directory\n" "${pkg_dir}" exit 1 fi -- cgit v1.2.3-70-g09d2