[Pkg-utopia-commits] r100 - packages/hal/trunk/debian

Sjoerd Simons sjoerd@haydn.debian.org
Thu, 18 Nov 2004 10:48:54 -0700


Author: sjoerd
Date: 2004-11-18 10:48:32 -0700 (Thu, 18 Nov 2004)
New Revision: 100

Modified:
   packages/hal/trunk/debian/hal.udev.device-removable.sh
Log:
Made big improvements with the help of Martin Pitt. It is now smaller, looks
better, more robust and actually works on machines other then my powerbook :)



Modified: packages/hal/trunk/debian/hal.udev.device-removable.sh
===================================================================
--- packages/hal/trunk/debian/hal.udev.device-removable.sh	2004-11-18 15:30:02 UTC (rev 99)
+++ packages/hal/trunk/debian/hal.udev.device-removable.sh	2004-11-18 17:48:32 UTC (rev 100)
@@ -2,65 +2,38 @@
 # print "1" if device $1 is either removable, on the ieee1394 or on the usb bus,
 # and "0" otherwise.
 
-get_devpath() {
-  # Turn the symlink to a device into a the path inside the sysfs tree
-  path=$(/bin/readlink $1)
-  # strip the leading "../"'s
-  prev=""
-  while [ "${path}" != "${prev}" ]; do 
-    prev=${path}
-    path=${prev#../}
-  done
-  echo "/${path}"
-}
-
-is_prefix() {
-  # check if the first argument is a path prefix of the section
-  prefix=$1
-  compare=$2
-  while [ ${#compare} -gt ${#prefix} ] ; do
-    compare="${compare%/*}"
-  done
-  if [ "${compare}" != "${prefix}" ]; then
-   return 1;
-  fi
-  return 0;
-}
-
 check_bus() {
   # check if the DEVICE is on the given bus 
   # This is done by checking if any of the devices on the bus is a prefix 
   # of the device
   BUSDEVP="/sys/bus/$1/devices"
   for x in $BUSDEVP/*; do
-    if is_prefix $(get_devpath $x) $DEVICE; then 
-      IS_REMOVABLE=1
-      return
+    [ -L "$x" ] || continue
+    if echo "$DEVICE" | grep -q "^$(readlink -f $x)"; then 
+      return 0
     fi
   done
+  return 1
 }
 
 DEV="${1%[0-9]*}"
 BLOCKPATH="/sys/block/$DEV"
 
-if [ ! -d ${BLOCKPATH} ]; then 
+if [ ! -d "${BLOCKPATH}" ]; then 
   exit 1
 fi
 
-REMOVABLE=${BLOCKPATH}/removable
-DEVICE=$(get_devpath ${BLOCKPATH}/device)
-IS_REMOVABLE=0
+REMOVABLE="${BLOCKPATH}/removable"
+DEVICE="$(readlink -f "${BLOCKPATH}/device")"
+IS_REMOVABLE="0"
+
 if [ -e "$REMOVABLE" ]; then
-    IS_REMOVABLE=$(cat $REMOVABLE)
+  IS_REMOVABLE="$(cat $REMOVABLE)"
 fi
 
-if [ $IS_REMOVABLE -eq 0 ]; then 
-  check_bus "usb"
+if [ "$IS_REMOVABLE" = "1" ] || check_bus "usb" || check_bus "ieee1394" ; then 
+  echo 1
+else 
+  echo 0
 fi
-
-if [ $IS_REMOVABLE -eq 0 ]; then 
-  check_bus "ieee1394"
-fi
-
-echo $IS_REMOVABLE
 exit 0