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

Sjoerd Simons sjoerd@haydn.debian.org
Wed, 17 Nov 2004 15:33:28 -0700


Author: sjoerd
Date: 2004-11-17 15:33:06 -0700 (Wed, 17 Nov 2004)
New Revision: 93

Modified:
   packages/hal/trunk/debian/hal.udev.removable.sh
Log:
Use my non-existing shell program skills to get removable.sh to check both the
removable tag and whether or not the device is on either the ieee1394 bus or
the usb bus.. Just like pmount does


Modified: packages/hal/trunk/debian/hal.udev.removable.sh
===================================================================
--- packages/hal/trunk/debian/hal.udev.removable.sh	2004-11-17 18:32:05 UTC (rev 92)
+++ packages/hal/trunk/debian/hal.udev.removable.sh	2004-11-17 22:33:06 UTC (rev 93)
@@ -1,15 +1,66 @@
 #!/bin/sh -e
+# print "1" if device $1 is either removable, on the ieee1394 or on the usb bus,
+# and "0" otherwise.
 
-# print "1" if device $1 is removable, "0" otherwise.
-# The "removable" attribute appeared in Linux 2.6.8; this script will always
-# print "0" for earlier kernels.
+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
+    fi
+  done
+}
+
 DEV="${1%[0-9]*}"
-REMOVABLE="/sys/block/$DEV/removable"
+BLOCKPATH="/sys/block/$DEV"
 
+if [ ! -d ${BLOCKPATH} ]; then 
+  exit 1
+fi
+
+REMOVABLE=${BLOCKPATH}/removable
+DEVICE=$(get_devpath ${BLOCKPATH}/device)
+IS_REMOVABLE=0
 if [ -e "$REMOVABLE" ]; then
-    cat "$REMOVABLE"
-else
-    echo "0"
+    IS_REMOVABLE=$(cat $REMOVABLE)
 fi
+
+if [ $IS_REMOVABLE -eq 0 ]; then 
+  check_bus "usb"
+fi
+
+if [ $IS_REMOVABLE -eq 0 ]; then 
+  check_bus "ieee1394"
+fi
+
+echo $IS_REMOVABLE
 exit 0